changeset 481:a458f9fb00ce

Access to user-supplied message of last commit
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 07 Aug 2012 14:02:28 +0200
parents f3fab7a20841
children 6c67debed07e
files cmdline/org/tmatesoft/hg/console/Main.java src/org/tmatesoft/hg/repo/HgRepository.java src/org/tmatesoft/hg/repo/HgRepositoryFiles.java
diffstat 3 files changed, 41 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/cmdline/org/tmatesoft/hg/console/Main.java	Tue Jul 17 22:14:21 2012 +0200
+++ b/cmdline/org/tmatesoft/hg/console/Main.java	Tue Aug 07 14:02:28 2012 +0200
@@ -109,6 +109,7 @@
 
 	public static void main(String[] args) throws Exception {
 		Main m = new Main(args);
+		m.dumpCommitLastMessage();
 //		m.buildFileLog();
 //		m.testConsoleLog();
 //		m.testTreeTraversal();
@@ -129,6 +130,10 @@
 //		m.dumpCompleteManifestHigh();
 //		m.bunchOfTests();
 	}
+	
+	private void dumpCommitLastMessage() throws Exception {
+		System.out.println(hgRepo.getCommitLastMessage());
+	}
 
 	private void buildFileLog() throws Exception {
 		final long start = System.nanoTime();
--- a/src/org/tmatesoft/hg/repo/HgRepository.java	Tue Jul 17 22:14:21 2012 +0200
+++ b/src/org/tmatesoft/hg/repo/HgRepository.java	Tue Aug 07 14:02:28 2012 +0200
@@ -19,9 +19,11 @@
 import static org.tmatesoft.hg.util.LogFacility.Severity.*;
 
 import java.io.File;
+import java.io.FileReader;
 import java.io.IOException;
 import java.io.StringReader;
 import java.lang.ref.SoftReference;
+import java.nio.CharBuffer;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -357,11 +359,11 @@
 		// TODO read config for additional locations
 		if (ignore == null) {
 			ignore = new HgIgnore(getToRepoPathHelper());
-			File ignoreFile = new File(getWorkingDir(), ".hgignore");
+			File ignoreFile = new File(getWorkingDir(), HgRepositoryFiles.HgIgnore.getPath());
 			try {
 				final List<String> errors = ignore.read(ignoreFile);
 				if (errors != null) {
-					getContext().getLog().dump(getClass(), Warn, "Syntax errors parsing .hgignore:\n%s", Internals.join(errors, ",\n"));
+					getContext().getLog().dump(getClass(), Warn, "Syntax errors parsing %s:\n%s", ignoreFile.getName(), Internals.join(errors, ",\n"));
 				}
 			} catch (IOException ex) {
 				final String m = "Error reading .hgignore file";
@@ -371,6 +373,36 @@
 		}
 		return ignore;
 	}
+	
+	/**
+	 * Mercurial saves message user has supplied for a commit to facilitate message re-use in case commit fails.
+	 * This method provides this saved message.
+	 *  
+	 * @return message used for last commit attempt, or <code>null</code> if none
+	 */
+	public String getCommitLastMessage() {
+		File lastMessage = new File(getRepositoryRoot(), HgRepositoryFiles.LastMessage.getPath());
+		if (!lastMessage.canRead()) {
+			return null;
+		}
+		FileReader fr = null;
+		try {
+			fr = new FileReader(lastMessage);
+			CharBuffer cb = CharBuffer.allocate(Internals.ltoi(lastMessage.length()));
+			fr.read(cb);
+			return cb.flip().toString();
+		} 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) {
+					getContext().getLog().dump(getClass(), Warn, "Failed to close %s after read", lastMessage);
+				}
+			}
+		}
+	}
 
 	/*package-local*/ DataAccessProvider getDataAccess() {
 		return dataAccess;
--- a/src/org/tmatesoft/hg/repo/HgRepositoryFiles.java	Tue Jul 17 22:14:21 2012 +0200
+++ b/src/org/tmatesoft/hg/repo/HgRepositoryFiles.java	Tue Aug 07 14:02:28 2012 +0200
@@ -27,7 +27,8 @@
 
 	HgIgnore(".hgignore"), HgTags(".hgtags"), HgEol(".hgeol"), 
 	Dirstate(".hg/dirstate"), HgLocalTags(".hg/localtags"),
-	HgSub(".hgsub"), HgSubstate(".hgsubstate");
+	HgSub(".hgsub"), HgSubstate(".hgsubstate"),
+	LastMessage("last-message.txt");
 
 	private String fname;