destupdate: extract validation logic
authorPierre-Yves David <pierre-yves.david@fb.com>
Thu, 15 Oct 2015 14:10:57 +0100
changeset 26720 6c22a17faa18
parent 26719 8bed1eae99df
child 26721 3d094fbedf74
destupdate: extract validation logic One of the main goal of having consolidated destination function is to allow extension to play with this logic. We extract sub logic to make is wrapping more practical.
mercurial/destutil.py
--- a/mercurial/destutil.py	Thu Oct 15 01:56:03 2015 +0100
+++ b/mercurial/destutil.py	Thu Oct 15 14:10:57 2015 +0100
@@ -12,6 +12,34 @@
     obsolete,
 )
 
+def _destupdatevalidate(repo, rev, clean, check):
+    """validate that the destination comply to various rules
+
+    This exists as its own function to help wrapping from extensions."""
+    wc = repo[None]
+    p1 = wc.p1()
+    if not clean:
+        # Check that the update is linear.
+        #
+        # Mercurial do not allow update-merge for non linear pattern
+        # (that would be technically possible but was considered too confusing
+        # for user a long time ago)
+        #
+        # See mercurial.merge.update for details
+        if p1.rev() not in repo.changelog.ancestors([rev], inclusive=True):
+            dirty = wc.dirty(missing=True)
+            foreground = obsolete.foreground(repo, [p1.node()])
+            if not repo[rev].node() in foreground:
+                if dirty:
+                    msg = _("uncommitted changes")
+                    hint = _("commit and merge, or update --clean to"
+                             " discard changes")
+                    raise error.UpdateAbort(msg, hint=hint)
+                elif not check:  # destination is not a descendant.
+                    msg = _("not a linear update")
+                    hint = _("merge or update --check to force update")
+                    raise error.UpdateAbort(msg, hint=hint)
+
 def destupdate(repo, clean=False, check=False):
     """destination for bare update operation
 
@@ -68,27 +96,7 @@
             node = repo.revs('max(%ln)', successors).first()
     rev = repo[node].rev()
 
-    if not clean:
-        # Check that the update is linear.
-        #
-        # Mercurial do not allow update-merge for non linear pattern
-        # (that would be technically possible but was considered too confusing
-        # for user a long time ago)
-        #
-        # See mercurial.merge.update for details
-        if p1.rev() not in repo.changelog.ancestors([rev], inclusive=True):
-            dirty = wc.dirty(missing=True)
-            foreground = obsolete.foreground(repo, [p1.node()])
-            if not repo[rev].node() in foreground:
-                if dirty:
-                    msg = _("uncommitted changes")
-                    hint = _("commit and merge, or update --clean to"
-                             " discard changes")
-                    raise error.UpdateAbort(msg, hint=hint)
-                elif not check:  # destination is not a descendant.
-                    msg = _("not a linear update")
-                    hint = _("merge or update --check to force update")
-                    raise error.UpdateAbort(msg, hint=hint)
+    _destupdatevalidate(repo, rev, clean, check)
 
     return rev, movemark, activemark