changeset 612:dca70c0b1f74

Test tags, branches and hgingore information get refreshed on external (and/or internal) change
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 10 May 2013 14:29:35 +0200
parents 7fc7fba4df30
children f41dd9a3b8af
files src/org/tmatesoft/hg/internal/CommitFacility.java test/org/tmatesoft/hg/test/TestCommit.java test/org/tmatesoft/hg/test/TestIgnore.java
diffstat 3 files changed, 80 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/CommitFacility.java	Thu May 09 21:52:01 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/CommitFacility.java	Fri May 10 14:29:35 2013 +0200
@@ -16,8 +16,13 @@
  */
 package org.tmatesoft.hg.internal;
 
+import static org.tmatesoft.hg.repo.HgRepository.DEFAULT_BRANCH_NAME;
 import static org.tmatesoft.hg.repo.HgRepository.NO_REVISION;
+import static org.tmatesoft.hg.repo.HgRepositoryFiles.Branch;
+import static org.tmatesoft.hg.util.LogFacility.Severity.Error;
 
+import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
@@ -34,10 +39,8 @@
 import org.tmatesoft.hg.core.Nodeid;
 import org.tmatesoft.hg.repo.HgChangelog;
 import org.tmatesoft.hg.repo.HgDataFile;
-import org.tmatesoft.hg.repo.HgRepository;
 import org.tmatesoft.hg.util.Pair;
 import org.tmatesoft.hg.util.Path;
-import org.tmatesoft.hg.util.LogFacility.Severity;
 
 /**
  * WORK IN PROGRESS
@@ -177,7 +180,7 @@
 		// Changelog
 		final ChangelogEntryBuilder changelogBuilder = new ChangelogEntryBuilder();
 		changelogBuilder.setModified(files.keySet());
-		changelogBuilder.branch(branch == null ? HgRepository.DEFAULT_BRANCH_NAME : branch);
+		changelogBuilder.branch(branch == null ? DEFAULT_BRANCH_NAME : branch);
 		changelogBuilder.user(String.valueOf(user));
 		byte[] clogContent = changelogBuilder.build(manifestRev, message);
 		RevlogStreamWriter changelogWriter = new RevlogStreamWriter(repo, repo.getImplAccess().getChangelogStream());
@@ -192,7 +195,28 @@
 				fncache.write();
 			} catch (IOException ex) {
 				// see comment above for fnchache.read()
-				repo.getLog().dump(getClass(), Severity.Error, ex, "Failed to write fncache, error ignored");
+				repo.getLog().dump(getClass(), Error, ex, "Failed to write fncache, error ignored");
+			}
+		}
+		String oldBranchValue = DirstateReader.readBranch(repo);
+		String newBranchValue = branch == null ? DEFAULT_BRANCH_NAME : branch;
+		if (!oldBranchValue.equals(newBranchValue)) {
+			File branchFile = repo.getRepositoryFile(Branch);
+			FileOutputStream fos = null;
+			try {
+				fos = new FileOutputStream(branchFile);
+				fos.write(newBranchValue.getBytes(EncodingHelper.getUTF8()));
+				fos.flush();
+			} catch (IOException ex) {
+				repo.getLog().dump(getClass(), Error, ex, "Failed to write branch information, error ignored");
+			} finally {
+				try {
+					if (fos != null) {
+						fos.close();
+					}
+				} catch (IOException ex) {
+					repo.getLog().dump(getClass(), Error, ex, null);
+				}
 			}
 		}
 		// bring dirstate up to commit state
--- a/test/org/tmatesoft/hg/test/TestCommit.java	Thu May 09 21:52:01 2013 +0200
+++ b/test/org/tmatesoft/hg/test/TestCommit.java	Fri May 10 14:29:35 2013 +0200
@@ -271,7 +271,7 @@
 	
 	@Test
 	public void testUpdateActiveBookmark() throws Exception {
-		File repoLoc = RepoUtils.cloneRepoToTempLocation("log-1", "test-commit-cmd", false);
+		File repoLoc = RepoUtils.cloneRepoToTempLocation("log-1", "test-commit-bookmark-update", false);
 		ExecHelper eh = new ExecHelper(new OutputParser.Stub(), repoLoc);
 		String activeBookmark = "bm1";
 		eh.run("hg", "bookmarks", activeBookmark);
@@ -299,6 +299,33 @@
 		errorCollector.assertEquals(activeBookmark, hgRepo.getBookmarks().getActiveBookmarkName());
 		errorCollector.assertEquals(c, hgRepo.getBookmarks().getRevision(activeBookmark));
 	}
+
+	@Test
+	public void testRefreshTagsAndBranches() throws Exception {
+		File repoLoc = RepoUtils.cloneRepoToTempLocation("log-branches", "test-refresh-after-commit", false);
+		final String tag = "tag.refresh", branch = "branch-refresh";
+		HgRepository hgRepo = new HgLookup().detect(repoLoc);
+		assertFalse(hgRepo.getTags().getAllTags().containsKey(tag));
+		assertNull(hgRepo.getBranches().getBranch(branch));
+		RepoUtils.modifyFileAppend(new File(repoLoc, "a"), "whatever");
+		//
+		final int parentCsetRevIndex = hgRepo.getChangelog().getLastRevision();
+		// HgCommitCommand can't do branch yet
+		CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), parentCsetRevIndex);
+		cf.add(hgRepo.getFileNode("a"), new FileContentSupplier(new File(repoLoc, "a")));
+		cf.branch(branch);
+		Nodeid commit = cf.commit("FIRST");
+		errorCollector.assertEquals("commit with branch shall update WC", branch, hgRepo.getWorkingCopyBranchName());
+		
+		ExecHelper eh = new ExecHelper(new OutputParser.Stub(), repoLoc);
+		eh.run("hg", "tag", tag);
+		assertEquals("[sanity]", 0, eh.getExitValue());
+		
+		errorCollector.assertTrue(hgRepo.getTags().getAllTags().containsKey(tag));
+		errorCollector.assertFalse(hgRepo.getBranches().getBranch(branch) == null);
+		errorCollector.assertTrue(hgRepo.getTags().tagged(tag).contains(commit));
+		errorCollector.assertTrue(hgRepo.getTags().tags(commit).contains(tag));
+	}
 	
 	private void assertHgVerifyOk(File repoLoc) throws InterruptedException, IOException {
 		ExecHelper verifyRun = new ExecHelper(new OutputParser.Stub(), repoLoc);
--- a/test/org/tmatesoft/hg/test/TestIgnore.java	Thu May 09 21:52:01 2013 +0200
+++ b/test/org/tmatesoft/hg/test/TestIgnore.java	Fri May 10 14:29:35 2013 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2012 TMate Software Ltd
+ * Copyright (c) 2011-2013 TMate Software Ltd
  *  
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,6 +28,9 @@
 import org.tmatesoft.hg.internal.WinToNixPathRewrite;
 import org.tmatesoft.hg.repo.HgIgnore;
 import org.tmatesoft.hg.repo.HgInternals;
+import org.tmatesoft.hg.repo.HgLookup;
+import org.tmatesoft.hg.repo.HgRepository;
+import org.tmatesoft.hg.repo.HgRepositoryFiles;
 import org.tmatesoft.hg.util.Path;
 
 /**
@@ -189,6 +192,26 @@
 		doAssert(hgIgnore, toIgnore, toPass);
 	}
 	
+	@Test
+	public void testRefreshOnChange() throws Exception {
+		File repoLoc = RepoUtils.cloneRepoToTempLocation("log-1", "test-refresh-hgignore", false);
+		File hgignoreFile = new File(repoLoc, HgRepositoryFiles.HgIgnore.getPath());
+		RepoUtils.createFile(hgignoreFile, "bin/");
+		HgRepository hgRepo = new HgLookup().detect(repoLoc);
+		final Path p1 = Path.create("bin/a/b/c");
+		final Path p2 = Path.create("src/a/b/c");
+		HgIgnore ignore = hgRepo.getIgnore();
+		errorCollector.assertTrue(ignore.isIgnored(p1));
+		errorCollector.assertFalse(ignore.isIgnored(p2));
+		Thread.sleep(1000); // Linux granularity for modification time is 1 second 
+		// file of the same length
+		RepoUtils.createFile(hgignoreFile, "src/");
+		ignore = hgRepo.getIgnore();
+		errorCollector.assertFalse(ignore.isIgnored(p1));
+		errorCollector.assertTrue(ignore.isIgnored(p2));
+		
+	}
+	
 	private void doAssert(HgIgnore hgIgnore, Path[] toIgnore, Path[] toPass) {
 		if (toIgnore == null && toPass == null) {
 			throw new IllegalArgumentException();