evolve: extract logic of pre-checking divergence risk to its own function
authorSushil khanchi <sushilkhanchi97@gmail.com>
Wed, 06 Nov 2019 18:15:00 +0530
changeset 5038 f583d9eedbce
parent 5035 c5efcbbd0dc4
child 5039 778286176016
evolve: extract logic of pre-checking divergence risk to its own function Extracting the logic (which is used to check if rewriting an obsolete revision will create divergence) to a function. This function will be used in upcoming patches of pre-checking. This is how we check if rewriting an obsolete revision will create divergence. We need to check two cases: If there is any 1) non-obsolete successor. -> easily done by `obsutil.successorssets()` 2) precursor with non-obsolete successor. -> using `evolvecmd.divergentsets()`
hgext3rd/evolve/cmdrewrite.py
hgext3rd/evolve/rewriteutil.py
--- a/hgext3rd/evolve/cmdrewrite.py	Thu Dec 26 21:23:30 2019 +0100
+++ b/hgext3rd/evolve/cmdrewrite.py	Wed Nov 06 18:15:00 2019 +0530
@@ -26,7 +26,6 @@
     merge,
     node,
     obsolete,
-    obsutil,
     patch,
     phases,
     pycompat,
@@ -56,6 +55,7 @@
 commitopts2 = commands.commitopts2
 mergetoolopts = commands.mergetoolopts
 stringio = util.stringio
+contentdivrisk = rewriteutil.contentdivrisk
 
 # option added by evolve
 
@@ -1400,27 +1400,7 @@
         p2 = newmapping.get(p2, p2)
 
         if not (duplicate or allowdivergence):
-            # If reviving this cset creates divergence, let's ask user
-            # what to do: create divergence or duplicate
-
-            # We need to check two cases that can cause divergence:
-            # case 1: the rev being revived has a non-obsolete successor (easily
-            #     detected by successorssets)
-            sset = obsutil.successorssets(repo, ctx.node())
-            nodivergencerisk = (len(sset) == 0
-                                or (len(sset) == 1
-                                    and len(sset[0]) == 1
-                                    and repo[sset[0][0]].rev() == ctx.rev()
-                                ))
-            if nodivergencerisk:
-                # case 2: one of the precursors of the rev being revived has a
-                #     non-obsolete successor (we need divergentsets for this)
-                from . import evolvecmd
-                if evolvecmd.divergentsets(repo, ctx):
-                    nodivergencerisk = False
-            if nodivergencerisk:
-                duplicate = False
-            else:
+            if contentdivrisk(repo, ctx):
                 displayer.show(ctx)
                 index = ui.promptchoice(
                     _(b"reviving this changeset will create divergence"
@@ -1428,9 +1408,7 @@
                       b" (d)uplicate the changeset? $$ &Allowdivergence $$ "
                       b"&Duplicate"), 0)
                 choice = [b'allowdivergence', b'duplicate'][index]
-                if choice == b'allowdivergence':
-                    duplicate = False
-                else:
+                if choice == b'duplicate':
                     duplicate = True
 
         updates = []
--- a/hgext3rd/evolve/rewriteutil.py	Thu Dec 26 21:23:30 2019 +0100
+++ b/hgext3rd/evolve/rewriteutil.py	Wed Nov 06 18:15:00 2019 +0530
@@ -21,6 +21,7 @@
     lock as lockmod,
     node,
     obsolete,
+    obsutil,
     phases,
     revset,
     util,
@@ -254,3 +255,22 @@
         return newid, created
     finally:
         lockmod.release(tr, lock, wlock)
+
+def contentdivrisk(repo, ctx):
+    """logic to check if rewriting an obsolete cset will create divergence"""
+    # We need to check two cases that can cause divergence:
+    # case 1: the rev being rewritten has a non-obsolete successor (easily
+    #     detected by successorssets)
+    sset = obsutil.successorssets(repo, ctx.node())
+    nodivergencerisk = (len(sset) == 0
+                        or (len(sset) == 1
+                            and len(sset[0]) == 1
+                            and repo[sset[0][0]].rev() == ctx.rev()
+                        ))
+    if nodivergencerisk:
+        # case 2: one of the precursors of the rev being revived has a
+        #     non-obsolete successor (we need divergentsets for this)
+        from . import evolvecmd
+        if evolvecmd.divergentsets(repo, ctx):
+            nodivergencerisk = False
+    return not nodivergencerisk