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

Greg Ward greg-hg at gerg.ca
Sat Nov 21 16:11:33 CST 2009


# HG changeset patch
# User Greg Ward <greg-hg at gerg.ca>
# Date 1258840300 18000
# Node ID 14424eaaae621395167ace250d6e48488ff75519
# Parent  b5170b8b32a5a8a77a9062da5726c0e4a667410d
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 lost irrevocably.
This saves a copy in .hg/message, just in case (similar to svn).
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)
+            f = self.opener('message', 'wt')
+            f.write(cctx._text.rstrip() + '\n')
+            f.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,21 @@
 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 '.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,13 @@
 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
+.hg/message:
+test3


More information about the Mercurial-devel mailing list