[PATCH 2 of 2] commit: if relevant, tell user their commit message was saved

Greg Ward greg-hg at gerg.ca
Tue Nov 24 20:09:13 CST 2009


# HG changeset patch
# User Greg Ward <greg-hg at gerg.ca>
# Date 1259114920 18000
# Branch stable
# Node ID 3a0f6887a72382ec3fec030c2bd2b0bf6f9895ca
# Parent  e190a90750c1a83810bc544ad49f805c4968c221
commit: if relevant, tell user their commit message was saved.
(issue1635)

Specifically, if:
  1) the user edited the message (it didn't come straight from -m) and
  2) the commit was aborted by an exception
then the saved commit message in .hg/message could come in handy, so
mention it with a ui.write().

This doesn't help users who manually rollback to amend a changeset:
the fact that the message was saved to .hg/last-message.txt is
invisible in that case.

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -819,6 +819,7 @@
                                       extra, changes)
             if editor:
                 cctx._text = editor(self, cctx, subs)
+            edited = (text != cctx._text)
 
             # commit subs
             if subs:
@@ -838,7 +839,14 @@
             msgfile.write(cctx._text.rstrip() + '\n')
             msgfile.close()
 
-            ret = self.commitctx(cctx, True)
+            try:
+                ret = self.commitctx(cctx, True)
+            except:
+                if edited:
+                    msgfn = self.pathto(msgfile.name[len(self.root)+1:])
+                    self.ui.write(
+                        _('note: commit message saved in %s\n') % msgfn)
+                raise
 
             # update dirstate and mergestate
             for f in changes[0] + changes[1]:
diff --git a/tests/test-rollback b/tests/test-rollback
--- a/tests/test-rollback
+++ b/tests/test-rollback
@@ -34,6 +34,15 @@
 echo '% rollback by pretxncommit saves commit message (issue 1635)'
 echo a >> a
 hg --config hooks.pretxncommit=/bin/false commit -m"precious commit message"
-
 echo '.hg/last-message.txt:'
 cat .hg/last-message.txt
+
+echo '% same thing, but run $EDITOR'
+cat > $HGTMP/editor <<'__EOF__'
+#!/bin/sh
+echo "another precious commit message" > "$1"
+__EOF__
+chmod +x $HGTMP/editor
+HGEDITOR=$HGTMP/editor hg --config hooks.pretxncommit=/bin/false commit
+echo '.hg/last-message.txt:'
+cat .hg/last-message.txt
diff --git a/tests/test-rollback.out b/tests/test-rollback.out
--- a/tests/test-rollback.out
+++ b/tests/test-rollback.out
@@ -34,3 +34,10 @@
 abort: pretxncommit hook exited with status 1
 .hg/last-message.txt:
 precious commit message
+% same thing, but run $EDITOR
+transaction abort!
+rollback completed
+note: commit message saved in .hg/last-message.txt
+abort: pretxncommit hook exited with status 1
+.hg/last-message.txt:
+another precious commit message


More information about the Mercurial-devel mailing list