[PATCH 5 of 9 V2] tryimportone: use dirstateguard instead of beginparentchange/endparentchange

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Sat May 2 10:59:40 CDT 2015

# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1430582197 -32400
#      Sun May 03 00:56:37 2015 +0900
# Node ID c0b3c5c755e2f3a9e79462eddb0c9b999c585fc0
# Parent  f0826bbe149010938a73288e8cfe12a012728f54
tryimportone: use dirstateguard instead of beginparentchange/endparentchange

To fix the issue that recent (in memory) dirstate isn't visible to
external process (e.g. "precommit" hook), subsequent patch makes
"localrepository.commit()" invoke "dirstate.write()" in it.

This change will make "beginparentchange()" and "endparentchange()" on
dirstate in "cmdutil.tryimportone()" meaningless, because:

  - "dirstate.write()" writes changed data into ".hg/dirstate", but

  - aborting between "beginparentchange()" and "endparentchange()"
    doesn't cause any restoring ".hg/dirstate"

    it just discards changes in memory.

This patch uses "dirstateguard" instead of "beginparentchange()" and
"endparentchange()" in "cmdutil.tryimportone()" to restore
".hg/dirstate" at failure even if "dirstate.write()" is executed
before failure.

This patch uses "lockmod.release(dsguard)" instead of
"dsguard.relese()", because processing may be aborted before
assignment to "dsguard" , and "if dsguard" examination for safety is

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -818,6 +818,7 @@
     msg = _('applied to working directory')
     rejects = False
+    dsguard = None
         cmdline_message = logmessage(ui, opts)
@@ -859,7 +860,7 @@
         n = None
         if update:
-            repo.dirstate.beginparentchange()
+            dsguard = dirstateguard(repo, 'tryimportone')
             if p1 != parents[0]:
                 updatefunc(repo, p1.node())
             if p2 != parents[1]:
@@ -899,7 +900,7 @@
                 n = repo.commit(message, opts.get('user') or user,
                                 opts.get('date') or date, match=m,
                                 editor=editor, force=partial)
-            repo.dirstate.endparentchange()
+            dsguard.close()
             if opts.get('exact') or opts.get('import_branch'):
                 branch = branch or 'default'
@@ -937,6 +938,7 @@
             msg = _('created %s') % short(n)
         return (msg, n, rejects)
+        lockmod.release(dsguard)
 def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False,

More information about the Mercurial-devel mailing list