changeset 33331:4bae3c117b57

scmutil: make cleanupnodes delete divergent bookmarks cleanupnodes takes care of bookmark movement, and bookmark movement could cause bookmark divergent resolution as a side effect. This patch adds such bookmark divergent resolution logic so future rebase migration will be easier. The revset is carefully written to be equivalent to what rebase does today. Although I think it might make sense to remove divergent bookmarks more aggressively, for example: F book@1 | E book@2 | | D book | | | C |/ B book@3 | A When rebase -s C -d E, "book@1" will be removed, "book@3" will be kept, and the end result is: D book | C | F | E book@2 (?) | B book@3 | A The question is should we keep book@2? The current logic keeps it. If we choose not to (makes some sense to me), the "deleterevs" revset could be simplified to "newnode % oldnode". For now, I just make it compatible with the existing behavior. If we want to make the "deleterevs" revset simpler, we can always do it in the future.
author Jun Wu <quark@fb.com>
date Mon, 26 Jun 2017 13:13:51 -0700
parents ba43e5ee9c6d
children 3b7cb3d17137
files mercurial/scmutil.py tests/test-strip.t
diffstat 2 files changed, 28 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/scmutil.py	Mon Jun 26 15:08:37 2017 -0700
+++ b/mercurial/scmutil.py	Mon Jun 26 13:13:51 2017 -0700
@@ -567,6 +567,16 @@
 
     return fullorigpath + ".orig"
 
+class _containsnode(object):
+    """proxy __contains__(node) to container.__contains__ which accepts revs"""
+
+    def __init__(self, repo, revcontainer):
+        self._torev = repo.changelog.rev
+        self._revcontains = revcontainer.__contains__
+
+    def __contains__(self, node):
+        return self._revcontains(self._torev(node))
+
 def cleanupnodes(repo, mapping, operation):
     """do common cleanups when old nodes are replaced by new nodes
 
@@ -583,17 +593,16 @@
         # Move bookmarks
         bmarks = repo._bookmarks
         bmarkchanged = False
+        allnewnodes = [n for ns in mapping.values() for n in ns]
         for oldnode, newnodes in mapping.items():
             oldbmarks = repo.nodebookmarks(oldnode)
             if not oldbmarks:
                 continue
+            from . import bookmarks # avoid import cycle
             bmarkchanged = True
             if len(newnodes) > 1:
-                heads = list(repo.set('heads(%ln)', newnodes))
-                if len(heads) != 1:
-                    raise error.ProgrammingError(
-                        'cannot figure out bookmark movement')
-                newnode = heads[0].node()
+                # usually a split, take the one with biggest rev number
+                newnode = next(repo.set('max(%ln)', newnodes)).node()
             elif len(newnodes) == 0:
                 # move bookmark backwards
                 roots = list(repo.set('max((::%n) - %ln)', oldnode,
@@ -606,8 +615,13 @@
                 newnode = newnodes[0]
             repo.ui.debug('moving bookmarks %r from %s to %s\n' %
                           (oldbmarks, hex(oldnode), hex(newnode)))
+            # Delete divergent bookmarks being parents of related newnodes
+            deleterevs = repo.revs('parents(roots(%ln & (::%n))) - parents(%n)',
+                                   allnewnodes, newnode, oldnode)
+            deletenodes = _containsnode(repo, deleterevs)
             for name in oldbmarks:
                 bmarks[name] = newnode
+                bookmarks.deletedivergent(repo, deletenodes, name)
         if bmarkchanged:
             bmarks.recordchange(tr)
 
--- a/tests/test-strip.t	Mon Jun 26 15:08:37 2017 -0700
+++ b/tests/test-strip.t	Mon Jun 26 13:13:51 2017 -0700
@@ -1002,6 +1002,9 @@
   $ for i in B C D F G I Z; do
   >     hg bookmark -i -r $i b-$i
   > done
+  $ hg bookmark -i -r E 'b-F@divergent1'
+  $ hg bookmark -i -r H 'b-F@divergent2'
+  $ hg bookmark -i -r G 'b-F@divergent3'
   $ cp -R . ../scmutilcleanup.obsstore
 
   $ cat > $TESTTMP/scmutilcleanup.py <<EOF
@@ -1025,13 +1028,13 @@
   saved backup bundle to $TESTTMP/scmutilcleanup/.hg/strip-backup/f585351a92f8-73fb7c03-replace.hg (glob)
   
   $ hg log -G -T '{rev}:{node|short} {desc} {bookmarks}' -r 'sort(all(), topo)'
-  o  8:1473d4b996d1 G2 b-G
+  o  8:1473d4b996d1 G2 b-F@divergent3 b-G
   |
   | o  7:d94e89b773b6 F2 b-F
   | |
   | o  5:7fe5bac4c918 H
   |/|
-  | o  3:7fb047a69f22 E
+  | o  3:7fb047a69f22 E b-F@divergent1
   | |
   | | o  6:7c78f703e465 D2 b-D
   | | |
@@ -1048,6 +1051,8 @@
      b-C                       0:426bada5c675
      b-D                       6:7c78f703e465
      b-F                       7:d94e89b773b6
+     b-F@divergent1            3:7fb047a69f22
+     b-F@divergent3            8:1473d4b996d1
      b-G                       8:1473d4b996d1
      b-I                       0:426bada5c675
      b-Z                       -1:000000000000
@@ -1066,13 +1071,13 @@
   
   $ rm .hg/localtags
   $ hg log -G -T '{rev}:{node|short} {desc} {bookmarks}' -r 'sort(all(), topo)'
-  o  12:1473d4b996d1 G2 b-G
+  o  12:1473d4b996d1 G2 b-F@divergent3 b-G
   |
   | o  11:d94e89b773b6 F2 b-F
   | |
   | o  8:7fe5bac4c918 H
   |/|
-  | o  4:7fb047a69f22 E
+  | o  4:7fb047a69f22 E b-F@divergent1
   | |
   | | o  10:7c78f703e465 D2 b-D
   | | |