diff src/org/tmatesoft/hg/internal/CommitFacility.java @ 636:ffce73efa2c2

HgCommitCommand: save last commit message
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 06 Jun 2013 19:39:06 +0200
parents 6526d8adbc0f
children cd77bf51b562
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));