[PATCH 2 of 6] commands: centralize code to update with extra care for non-file components

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Fri Mar 11 14:41:07 EST 2016


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1457724942 -32400
#      Sat Mar 12 04:35:42 2016 +0900
# Node ID 11fba7de558eba79a4c80c3bfd0e85113e23eeb8
# Parent  4895764323c150d4d34a85cde20da91250eddaf0
commands: centralize code to update with extra care for non-file components

This patch centralizes similar code paths to update the working
directory with extra care for non-file components (e.g. bookmark) into
newly added function updatetotally().

'if True' at the beginning of updatetotally() is redundant at this
patch, but useful to reduce amount of changes in subsequent patch.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -5618,35 +5618,12 @@ def postincoming(ui, repo, modheads, opt
     if modheads == 0:
         return
     if optupdate:
-        warndest = False
         try:
-            movemarkfrom = None
-            if not checkout:
-                warndest = True
-                updata = destutil.destupdate(repo)
-                checkout, movemarkfrom, brev = updata
-            ret = hg.update(repo, checkout)
-            if warndest:
-                destutil.statusotherdests(ui, repo)
+            return hg.updatetotally(ui, repo, checkout, brev)
         except error.UpdateAbort as inst:
             msg = _("not updating: %s") % str(inst)
             hint = inst.hint
             raise error.UpdateAbort(msg, hint=hint)
-        if not ret and movemarkfrom:
-            if movemarkfrom == repo['.'].node():
-                pass # no-op update
-            elif bookmarks.update(repo, [movemarkfrom], repo['.'].node()):
-                ui.status(_("updating bookmark %s\n") % repo._activebookmark)
-        elif brev in repo._bookmarks:
-            if brev != repo._activebookmark:
-                ui.status(_("(activating bookmark %s)\n") % brev)
-            bookmarks.activate(repo, brev)
-        elif brev:
-            if repo._activebookmark:
-                ui.status(_("(leaving bookmark %s)\n") %
-                          repo._activebookmark)
-            bookmarks.deactivate(repo)
-        return ret
     if modheads > 1:
         currentbranchheads = len(repo.branchheads())
         if currentbranchheads == modheads:
@@ -7071,7 +7048,6 @@ def update(ui, repo, node=None, rev=None
 
     Returns 0 on success, 1 if there are unresolved files.
     """
-    movemarkfrom = None
     if rev and node:
         raise error.Abort(_("please specify just one revision"))
 
@@ -7084,8 +7060,6 @@ def update(ui, repo, node=None, rev=None
     if check and clean:
         raise error.Abort(_("cannot specify both -c/--check and -C/--clean"))
 
-    warndest = False
-
     with repo.wlock():
         cmdutil.clearunfinished(repo)
 
@@ -7098,40 +7072,10 @@ def update(ui, repo, node=None, rev=None
 
         if check:
             cmdutil.bailifchanged(repo, merge=False)
-        if rev is None:
-            updata = destutil.destupdate(repo, clean=clean, check=check)
-            rev, movemarkfrom, brev = updata
-            warndest = True
 
         repo.ui.setconfig('ui', 'forcemerge', tool, 'update')
 
-        if clean:
-            ret = hg.clean(repo, rev)
-        else:
-            ret = hg.update(repo, rev)
-
-        if not ret and movemarkfrom:
-            if movemarkfrom == repo['.'].node():
-                pass # no-op update
-            elif bookmarks.update(repo, [movemarkfrom], repo['.'].node()):
-                ui.status(_("updating bookmark %s\n") % repo._activebookmark)
-            else:
-                # this can happen with a non-linear update
-                ui.status(_("(leaving bookmark %s)\n") %
-                          repo._activebookmark)
-                bookmarks.deactivate(repo)
-        elif brev in repo._bookmarks:
-            if brev != repo._activebookmark:
-                ui.status(_("(activating bookmark %s)\n") % brev)
-            bookmarks.activate(repo, brev)
-        elif brev:
-            if repo._activebookmark:
-                ui.status(_("(leaving bookmark %s)\n") %
-                          repo._activebookmark)
-            bookmarks.deactivate(repo)
-        if warndest:
-            destutil.statusotherdests(ui, repo)
-    return ret
+        return hg.updatetotally(ui, repo, rev, brev, clean=clean, check=check)
 
 @command('verify', [])
 def verify(ui, repo):
diff --git a/mercurial/hg.py b/mercurial/hg.py
--- a/mercurial/hg.py
+++ b/mercurial/hg.py
@@ -19,6 +19,7 @@ from . import (
     bookmarks,
     bundlerepo,
     cmdutil,
+    destutil,
     discovery,
     error,
     exchange,
@@ -694,6 +695,63 @@ def clean(repo, node, show_stats=True, q
         _showstats(repo, stats, quietempty)
     return stats[3] > 0
 
+# naming conflict in updatetotally()
+_clean = clean
+
+def updatetotally(ui, repo, checkout, brev, clean=False, check=False):
+    """Update the working directory with extra care for non-file components
+
+    This takes care of non-file components below:
+
+    :bookmark: might be advanced or (in)activated
+
+    This takes arguments below:
+
+    :checkout: to which revision the working directory is updated
+    :brev: a name, which might be a bookmark to be activated after updating
+    :clean: whether changes in the working directory can be discarded
+    :check: whether changes in the working directory should be checked
+
+    This returns whether conflict is detected at updating or not.
+    """
+    if True:
+        movemarkfrom = None
+        warndest = False
+        if checkout is None:
+            updata = destutil.destupdate(repo, clean=clean, check=check)
+            checkout, movemarkfrom, brev = updata
+            warndest = True
+
+        if clean:
+            ret = _clean(repo, checkout)
+        else:
+            ret = _update(repo, checkout)
+
+        if not ret and movemarkfrom:
+            if movemarkfrom == repo['.'].node():
+                pass # no-op update
+            elif bookmarks.update(repo, [movemarkfrom], repo['.'].node()):
+                ui.status(_("updating bookmark %s\n") % repo._activebookmark)
+            else:
+                # this can happen with a non-linear update
+                ui.status(_("(leaving bookmark %s)\n") %
+                          repo._activebookmark)
+                bookmarks.deactivate(repo)
+        elif brev in repo._bookmarks:
+            if brev != repo._activebookmark:
+                ui.status(_("(activating bookmark %s)\n") % brev)
+            bookmarks.activate(repo, brev)
+        elif brev:
+            if repo._activebookmark:
+                ui.status(_("(leaving bookmark %s)\n") %
+                          repo._activebookmark)
+            bookmarks.deactivate(repo)
+
+        if warndest:
+            destutil.statusotherdests(ui, repo)
+
+    return ret
+
 def merge(repo, node, force=None, remind=True, mergeforce=False):
     """Branch merge with node, resolving changes. Return true if any
     unresolved conflicts."""


More information about the Mercurial-devel mailing list