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

Greg Ward greg-hg at gerg.ca
Sun Nov 22 12:11:12 CST 2009


# HG changeset patch
# User Greg Ward <greg-hg at gerg.ca>
# Date 1258913378 18000
# Node ID 6ea4ff1d9cc2992d7fd7ee69f4597b1ba015b3b7
# Parent  ac73cd788853749074dfcddbc1f66ac2432f898e
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/message, 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,12 @@
                     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)
+            msgfile = self.opener('message', 'wt')
+            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/message
 .hg/requires
 .hg/undo
 .hg/undo.branch
@@ -59,6 +60,7 @@
 .hg
 .hg/00changelog.i
 .hg/dirstate
+.hg/message
 .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/message
 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/message:'
+cat .hg/message
+
 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/message:'
+cat .hg/message
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/message:
+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/message:
+precious commit message


More information about the Mercurial-devel mailing list