[PATCH 1 of 2] commit: save commit message so it's not destroyed by rollback

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


# HG changeset patch
# User Greg Ward <greg-hg at gerg.ca>
# Date 1259114919 18000
# Branch stable
# Node ID e190a90750c1a83810bc544ad49f805c4968c221
# Parent  b22ff29a3fbb1c1841c1c5b62f510c5ca2c782c3
commit: save commit message so it's not destroyed by rollback.
(issue1635)

Rationale: if a pretxncommit hook rejects this commit, the transaction
is rolled back and the user's commit message is irrevocably lost.
So save a copy in .hg/last-message.txt, just in case.  Also handy if
the user deliberately rolls back a commit in order to amend it.

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -829,6 +829,15 @@
                     state[s] = (state[s][0], sr)
                 subrepo.writestate(self, state)
 
+            # Save commit message in case this transaction gets rolled back
+            # (e.g. by a pretxncommit hook).  (Save in text mode in case a
+            # Windows user wants to edit it with Notepad.  Normalize
+            # trailing whitespace so the file always looks the same --
+            # makes testing easier.)
+            msgfile = self.opener('last-message.txt', 'w')
+            msgfile.write(cctx._text.rstrip() + '\n')
+            msgfile.close()
+
             ret = self.commitctx(cctx, True)
 
             # update dirstate and mergestate
diff --git a/tests/test-fncache.out b/tests/test-fncache.out
--- a/tests/test-fncache.out
+++ b/tests/test-fncache.out
@@ -50,6 +50,7 @@
 .hg/data/tst.d.hg
 .hg/data/tst.d.hg/foo.i
 .hg/dirstate
+.hg/last-message.txt
 .hg/requires
 .hg/undo
 .hg/undo.branch
@@ -59,6 +60,7 @@
 .hg
 .hg/00changelog.i
 .hg/dirstate
+.hg/last-message.txt
 .hg/requires
 .hg/store
 .hg/store/00changelog.i
diff --git a/tests/test-inherit-mode.out b/tests/test-inherit-mode.out
--- a/tests/test-inherit-mode.out
+++ b/tests/test-inherit-mode.out
@@ -14,6 +14,7 @@
 00700 ./.hg/
 00600 ./.hg/00changelog.i
 00660 ./.hg/dirstate
+00660 ./.hg/last-message.txt
 00600 ./.hg/requires
 00770 ./.hg/store/
 00660 ./.hg/store/00changelog.i
diff --git a/tests/test-rollback b/tests/test-rollback
--- a/tests/test-rollback
+++ b/tests/test-rollback
@@ -15,14 +15,25 @@
 hg status
 
 echo % Test issue 902
-hg commit -m "test"
+hg commit -m "test2"
 hg branch test
 hg rollback
 hg branch
 
+echo '% Test issue 1635 (commit message saved)'
+echo '.hg/last-message.txt:'
+cat .hg/last-message.txt
+
 echo % Test rollback of hg before issue 902 was fixed
-hg commit -m "test"
+hg commit -m "test3"
 hg branch test
 rm .hg/undo.branch
 hg rollback
 hg branch
+
+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
diff --git a/tests/test-rollback.out b/tests/test-rollback.out
--- a/tests/test-rollback.out
+++ b/tests/test-rollback.out
@@ -20,8 +20,17 @@
 marked working directory as branch test
 rolling back last transaction
 default
+% Test issue 1635 (commit message saved)
+.hg/last-message.txt:
+test2
 % Test rollback of hg before issue 902 was fixed
 marked working directory as branch test
 rolling back last transaction
 Named branch could not be reset, current branch still is: test
 test
+% rollback by pretxncommit saves commit message (issue 1635)
+transaction abort!
+rollback completed
+abort: pretxncommit hook exited with status 1
+.hg/last-message.txt:
+precious commit message


More information about the Mercurial-devel mailing list