[PATCH 1 of 9 V3] cmdutil: add the class to restore dirstate at unexpected failure easily

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Thu May 7 03:15:48 UTC 2015


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1430968030 -32400
#      Thu May 07 12:07:10 2015 +0900
# Node ID 449a46109f0c96bb8d82d4edf2c8855341558c2f
# Parent  8174d27576a3fff28fb6f952a4c89d5c0b900214
cmdutil: add the class to restore dirstate at unexpected failure easily

Before this patch, after "dirstate.write()" execution, there is no way
to restore dirstate to the original status before "dirstate.write()".

In some code paths, "dirstate.invalidate()" is used as a kind of
"restore .hg/dirstate to the original status". But it just avoids
writing changes in memory out, and doesn't actually restore
".hg/dirstate" file. "dirstate.write()" prevents it from working as
expected.

To fix the issue that recent (in memory) dirstate isn't visible to
external process (e.g. "precommit" hooks), "dirstate.write()" should
be invoked before invocation of external process. But at the same
time, ".hg/dirstate" should be restored to the status before
"dirstate.write()" at unexpected failure in some cases.

This patch adds the class "dirstateguard" to easily restore
".hg/dirstate" at unexpected failure. Typical usecase of it is:

    # (1) build dirstate up
    ....

    # (2) write dirstate out, and backup ".hg/dirstate"
    dsguard = dirstateguard(repo, 'scopename')
    try:
        # (3) execute somethig to do:
        #     this may imply making some additional changes on dirstate
        ....

        # (4) unlink backup-ed dirstate file at the end of dsguard scope
        dsguard.close()
    finally:
        # (5) if execution is aborted before "dsguard.close()",
        #     ".hg/dirstate" is restored from the backup
        dsguard.release()

For this kind of issue, "extending transaction" approach (in
https://titanpad.com/mercurial32-sprint) seems not to be suitable,
because:

  - transaction nesting occurs in some cases (e.g. "shelve => rebase"), and

  - "dirstate" may be already modified since the beginning of OUTER
    transaction scope, then

  - dirstate should be backed up into the file other than
    "dirstate.journal" at the beginning of INNER transaction scope, but

  - such alternative backup files are useless for transaction itself,
    and increases complication of its implementation

"transaction" and "dirstateguard" differ from each other also in "what
it should do for .hg/dirstate" in cases other than success.

  ============== ======= ======== =============
  type           success fail     "hg rollback"
  ============== ======= ======== =============
  transaction    keep     keep     restore
  dirstateguard  keep     restore  (not implied)
  ============== ======= ======== =============

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -3259,3 +3259,59 @@
     for f, clearable, allowcommit, msg, hint in unfinishedstates:
         if clearable and repo.vfs.exists(f):
             util.unlink(repo.join(f))
+
+class dirstateguard(object):
+    '''Restore dirstate at unexpected failure.
+
+    At the construction, this class does:
+
+    - write current ``repo.dirstate`` out, and
+    - save ``.hg/dirstate`` into the backup file
+
+    This restores ``.hg/dirstate`` from backup file, if ``release()``
+    is invoked before ``close()``.
+
+    This just removes the backup file at ``close()`` before ``release()``.
+    '''
+
+    def __init__(self, repo, name):
+        repo.dirstate.write()
+        self.repo = repo
+        self.name = 'dirstate.backup.%s' % name
+        repo.vfs.write(self.name, repo.vfs.tryread('dirstate'))
+        self.active = True
+        self.closed = False
+
+    def __del__(self):
+        if self.active: # still active
+            # this may occur, even if this class is used correctly:
+            # for example, releasing other resources like transaction
+            # may raise exception before ``dirstateguard.release`` in
+            # ``release(tr, ....)``.
+            self._abort()
+
+    def close(self):
+        if not self.active: # already inactivated
+            msg = (_("can't close already inactivated backup: %s")
+                   % self.name)
+            raise util.Abort(msg)
+
+        self.repo.vfs.unlink(self.name)
+        self.active = False
+        self.closed = True
+
+    def _abort(self):
+        # this "invalidate()" prevents "wlock.release()" from writing
+        # changes of dirstate out after restoring to original status
+        self.repo.dirstate.invalidate()
+
+        self.repo.vfs.rename(self.name, 'dirstate')
+        self.active = False
+
+    def release(self):
+        if not self.closed:
+            if not self.active: # already inactivated
+                msg = (_("can't release already inactivated backup: %s")
+                       % self.name)
+                raise util.Abort(msg)
+            self._abort()


More information about the Mercurial-devel mailing list