[PATCH 2 of 6 V2] localrepo: restore dirstate to one before rollbacking if not parent-gone

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Tue Oct 13 12:54:51 CDT 2015


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1444758556 -32400
#      Wed Oct 14 02:49:16 2015 +0900
# Node ID 04f94f9be97fcff64587440aa1e1ec3afb4f7639
# Parent  b441136da58f3542910d71e9ad3353340ce8fb90
localrepo: restore dirstate to one before rollbacking if not parent-gone

'localrepository.rollback()' explicilty restores dirstate, only if at
least one of current parents of the working directory is removed at
rollbacking (a.k.a "parent-gone").

After DirstateTransactionPlan, 'dirstate.write()' will cause marking
'.hg/dirstate' as a file to be restored at rollbacking.

    https://mercurial.selenic.com/wiki/DirstateTransactionPlan

Then, 'transaction.rollback()' restores '.hg/dirstate' regardless of
parents of the working directory at that time, and this causes
unexpected dirstate changes if not "parent-gone" (e.g.  "hg update" to
another branch after "hg commit" or so, then "hg rollback").

To avoid such situation, this patch restores dirstate to one before
rollbacking if not "parent-gone".

  before:
    b1. restore dirstate explicitly, if "parent-gone"

  after:
    a1. save dirstate before actual rollbacking via dirstateguard
    a2. restore dirstate via 'transaction.rollback()'
    a3. if "parent-gone"
        - discard backup (a1)
        - restore dirstate from 'undo.dirstate'
    a4. otherwise, restore dirstate from backup (a1)

Even though restoring dirstate at (a3) after (a2) seems redundant,
this patch keeps this existing code path, because:

  - it isn't ensured that 'dirstate.write()' was invoked at least once
    while transaction running

    If not, '.hg/dirstate' isn't restored at (a2).

    In addition to it, rude 3rd party extension invoking
    'dirstate.write()' without 'repo' while transaction running (see
    subsequent patches for detail) may break consistency of a file
    backup-ed by transaction.

  - this patch mainly focuses on changes for DirstateTransactionPlan

    Restoring dirstate at (a3) itself should be cheaper enough than
    rollbacking itself. Redundancy will be removed in next step.

Newly added test is almost meaningless at this point. It will be used
to detect regression while implementing delayed dirstate write out.

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -1084,20 +1084,24 @@
             lock.release()
 
     def rollback(self, dryrun=False, force=False):
-        wlock = lock = None
+        wlock = lock = dsguard = None
         try:
             wlock = self.wlock()
             lock = self.lock()
             if self.svfs.exists("undo"):
-                return self._rollback(dryrun, force)
+                # delay importing avoids cyclic dependency
+                from cmdutil import dirstateguard
+                dsguard = dirstateguard(self, 'rollback')
+
+                return self._rollback(dryrun, force, dsguard)
             else:
                 self.ui.warn(_("no rollback information available\n"))
                 return 1
         finally:
-            release(lock, wlock)
+            release(dsguard, lock, wlock)
 
     @unfilteredmethod # Until we get smarter cache management
-    def _rollback(self, dryrun, force):
+    def _rollback(self, dryrun, force, dsguard):
         ui = self.ui
         try:
             args = self.vfs.read('undo.desc').splitlines()
@@ -1140,6 +1144,9 @@
         parentgone = (parents[0] not in self.changelog.nodemap or
                       parents[1] not in self.changelog.nodemap)
         if parentgone:
+            # prevent dirstateguard from overwriting already restored one
+            dsguard.close()
+
             self.vfs.rename('undo.dirstate', 'dirstate')
             try:
                 branch = self.vfs.read('undo.branch')
diff --git a/tests/test-import.t b/tests/test-import.t
--- a/tests/test-import.t
+++ b/tests/test-import.t
@@ -428,6 +428,25 @@
   working directory now based on revision 0
   $ hg --cwd b parents --template 'parent: {rev}\n'
   parent: 0
+
+Test that "hg rollback" doesn't restore dirstate to one at the
+beginning of the rollbacked transaction in not-"parent-gone" case.
+
+invoking pretxncommit hook will cause marking '.hg/dirstate' as a file
+to be restored at rollbacking, after DirstateTransactionPlan (see wiki
+page for detail).
+
+  $ hg --cwd b branch -q foobar
+  $ hg --cwd b commit -m foobar
+  $ hg --cwd b update 0 -q
+  $ hg --cwd b import ../patch1 ../patch2 --config hooks.pretxncommit=true
+  applying ../patch1
+  applying ../patch2
+  $ hg --cwd b update -q 1
+  $ hg --cwd b rollback -q
+  $ hg --cwd b parents --template 'parent: {rev}\n'
+  parent: 1
+
   $ rm -r b
 
 


More information about the Mercurial-devel mailing list