changeset 636:ffce73efa2c2

HgCommitCommand: save last commit message
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 06 Jun 2013 19:39:06 +0200
parents 4ec2d44e2bf3
children 4a0bab2c6da1
files src/org/tmatesoft/hg/internal/CommitFacility.java src/org/tmatesoft/hg/internal/FileUtils.java src/org/tmatesoft/hg/repo/HgRepository.java test/org/tmatesoft/hg/test/ComplexTest.java test/org/tmatesoft/hg/test/TestCommit.java
diffstat 5 files changed, 43 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/CommitFacility.java	Thu Jun 06 18:42:38 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/CommitFacility.java	Thu Jun 06 19:39:06 2013 +0200
@@ -18,12 +18,14 @@
 
 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.*;
 import static org.tmatesoft.hg.repo.HgRepositoryFiles.Branch;
 import static org.tmatesoft.hg.repo.HgRepositoryFiles.UndoBranch;
 import static org.tmatesoft.hg.util.LogFacility.Severity.Error;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -136,6 +138,8 @@
 			newManifestRevision.remove(p);
 		}
 		//
+		saveCommitMessage(message);
+		//
 		// Register new/changed
 		LinkedHashMap<Path, RevlogStream> newlyAddedFiles = new LinkedHashMap<Path, RevlogStream>();
 		ArrayList<Path> touchInDirstate = new ArrayList<Path>();
@@ -235,6 +239,24 @@
 		// The same notification might come useful once Pull is implemented
 		return changesetRev;
 	}
+	
+	private void saveCommitMessage(String message) throws HgIOException {
+		File lastMessage = repo.getRepositoryFile(LastMessage);
+		// do not attempt to write if we are going to fail anyway
+		if ((lastMessage.isFile() && !lastMessage.canWrite()) || !lastMessage.getParentFile().canWrite()) {
+			return;
+		}
+		FileWriter w = null;
+		try {
+			w = new FileWriter(lastMessage);
+			w.write(message == null ? new String() : message);
+			w.flush();
+		} catch (IOException ex) {
+			throw new HgIOException("Failed to save last commit message", ex, lastMessage);
+		} finally {
+			new FileUtils(repo.getLog()).closeQuietly(w, lastMessage);
+		}
+	}
 /*
 	private Pair<Integer, Integer> getManifestParents() {
 		return new Pair<Integer, Integer>(extractManifestRevisionIndex(p1Commit), extractManifestRevisionIndex(p2Commit));
--- a/src/org/tmatesoft/hg/internal/FileUtils.java	Thu Jun 06 18:42:38 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/FileUtils.java	Thu Jun 06 19:39:06 2013 +0200
@@ -77,8 +77,8 @@
 			fis = null;
 		} catch (IOException ex) {
 			// not in finally because I don't want to loose exception from fos.close()
-			closeQuietly(fis);
-			closeQuietly(fos);
+			closeQuietly(fis, from);
+			closeQuietly(fos, to);
 			String m = String.format("Failed to copy %s to %s", from.getName(), to.getName());
 			throw new HgIOException(m, ex, from);
 		}
@@ -87,14 +87,24 @@
 		 * Windows uncached run: 1.6 seconds
 		 */
 	}
-	
+
 	public void closeQuietly(Closeable stream) {
+		closeQuietly(stream, null);
+	}
+
+	public void closeQuietly(Closeable stream, File f) {
 		if (stream != null) {
 			try {
 				stream.close();
 			} catch (IOException ex) {
 				// ignore
-				log.dump(getClass(), Severity.Warn, ex, "Exception while closing stream quietly");
+				final String msg;
+				if (f == null) {
+					msg = "Exception while closing stream quietly";
+				} else {
+					msg = String.format("Failed to close %s", f);
+				}
+				log.dump(getClass(), Severity.Warn, ex, msg);
 			}
 		}
 	}
--- a/src/org/tmatesoft/hg/repo/HgRepository.java	Thu Jun 06 18:42:38 2013 +0200
+++ b/src/org/tmatesoft/hg/repo/HgRepository.java	Thu Jun 06 19:39:06 2013 +0200
@@ -32,6 +32,7 @@
 import org.tmatesoft.hg.core.SessionContext;
 import org.tmatesoft.hg.internal.ConfigFile;
 import org.tmatesoft.hg.internal.DirstateReader;
+import org.tmatesoft.hg.internal.FileUtils;
 import org.tmatesoft.hg.internal.Filter;
 import org.tmatesoft.hg.internal.Internals;
 import org.tmatesoft.hg.internal.PropertyMarshal;
@@ -359,7 +360,7 @@
 	 * @throws HgRuntimeException subclass thereof to indicate issues with the library. <em>Runtime exception</em>
 	 */
 	public String getCommitLastMessage() throws HgInvalidControlFileException {
-		File lastMessage = impl.getFileFromRepoDir(LastMessage.getPath());
+		File lastMessage = impl.getRepositoryFile(LastMessage);
 		if (!lastMessage.canRead()) {
 			return null;
 		}
@@ -372,13 +373,7 @@
 		} catch (IOException ex) {
 			throw new HgInvalidControlFileException("Can't retrieve message of last commit attempt", ex, lastMessage);
 		} finally {
-			if (fr != null) {
-				try {
-					fr.close();
-				} catch (IOException ex) {
-					getSessionContext().getLog().dump(getClass(), Warn, "Failed to close %s after read", lastMessage);
-				}
-			}
+			new FileUtils(getSessionContext().getLog()).closeQuietly(fr, lastMessage);
 		}
 	}
 
--- a/test/org/tmatesoft/hg/test/ComplexTest.java	Thu Jun 06 18:42:38 2013 +0200
+++ b/test/org/tmatesoft/hg/test/ComplexTest.java	Thu Jun 06 19:39:06 2013 +0200
@@ -41,7 +41,7 @@
 	public ErrorCollectorExt errorCollector = new ErrorCollectorExt();
 
 	/**
-	 * Regular work sequence with checkout, add, remove, and commit
+	 * Regular work sequence with checkout, add, remove, revert and commit
 	 */
 	@Test
 	public void testLocalScenario1() throws Exception {
@@ -70,8 +70,8 @@
 		new HgAddRemoveCommand(hgRepo).add(fc).remove(fb).execute();
 		new HgCommitCommand(hgRepo).message("SECOND").execute();
 		//
-		// TODO hgRepo.getCommitLastMessage() shall be updated from HgCommitCommand
 		assertEquals(2, hgRepo.getChangelog().getRevisionCount());
+		errorCollector.assertEquals("SECOND", hgRepo.getCommitLastMessage());
 		// checkout previous version
 		new HgCheckoutCommand(hgRepo).changeset(0).clean(true).execute();
 		assertTrue(fileA.isFile());
--- a/test/org/tmatesoft/hg/test/TestCommit.java	Thu Jun 06 18:42:38 2013 +0200
+++ b/test/org/tmatesoft/hg/test/TestCommit.java	Thu Jun 06 19:39:06 2013 +0200
@@ -277,6 +277,8 @@
 		errorCollector.assertTrue(r.isOk());
 		Nodeid c2 = cmd.getCommittedRevision();
 		//
+		errorCollector.assertEquals("SECOND", hgRepo.getCommitLastMessage());
+		//
 		int lastRev = hgRepo.getChangelog().getLastRevision();
 		List<HgChangeset> csets = new HgLogCommand(hgRepo).range(lastRev-1, lastRev).execute();
 		errorCollector.assertEquals(csets.get(0).getNodeid(), c1);