[PATCH 2 of 5] scmutil: make cleanupnodes delete divergent bookmarks

Jun Wu quark at fb.com
Mon Jun 26 18:35:29 EDT 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1498508031 25200
#      Mon Jun 26 13:13:51 2017 -0700
# Node ID 845ce766fcf0bc75a5bfff22cf5a121ad6777c05
# Parent  99027f9f17140c037882ebc037ef7125989eaca4
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 845ce766fcf0
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 at 1
    |
    E   book at 2
    |
    | D book
    | |
    | C
    |/
    B   book at 3
    |
    A

When rebase -s C -d E, "book at 1" will be removed, "book at 3" will be kept,
and the end result is:

    D   book
    |
    C
    |
    F
    |
    E   book at 2 (?)
    |
    B   book at 3
    |
    A

The question is should we keep book at 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.

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -565,4 +565,14 @@ def origpath(ui, repo, filepath):
     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
@@ -581,8 +591,10 @@ def cleanupnodes(repo, mapping, operatio
         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:
@@ -604,6 +616,11 @@ def cleanupnodes(repo, mapping, operatio
             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)
diff --git a/tests/test-strip.t b/tests/test-strip.t
--- a/tests/test-strip.t
+++ b/tests/test-strip.t
@@ -1003,4 +1003,7 @@ Test high-level scmutil.cleanupnodes API
   >     hg bookmark -i -r $i b-$i
   > done
+  $ hg bookmark -i -r E 'b-F at divergent1'
+  $ hg bookmark -i -r H 'b-F at divergent2'
+  $ hg bookmark -i -r G 'b-F at divergent3'
   $ cp -R . ../scmutilcleanup.obsstore
 
@@ -1026,5 +1029,5 @@ Test high-level scmutil.cleanupnodes API
   
   $ 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 at divergent3 b-G
   |
   | o  7:d94e89b773b6 F2 b-F
@@ -1032,5 +1035,5 @@ Test high-level scmutil.cleanupnodes API
   | o  5:7fe5bac4c918 H
   |/|
-  | o  3:7fb047a69f22 E
+  | o  3:7fb047a69f22 E b-F at divergent1
   | |
   | | o  6:7c78f703e465 D2 b-D
@@ -1049,4 +1052,6 @@ Test high-level scmutil.cleanupnodes API
      b-D                       6:7c78f703e465
      b-F                       7:d94e89b773b6
+     b-F at divergent1            3:7fb047a69f22
+     b-F at divergent3            8:1473d4b996d1
      b-G                       8:1473d4b996d1
      b-I                       0:426bada5c675
@@ -1067,5 +1072,5 @@ we have reusable code here
   $ 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 at divergent3 b-G
   |
   | o  11:d94e89b773b6 F2 b-F
@@ -1073,5 +1078,5 @@ we have reusable code here
   | o  8:7fe5bac4c918 H
   |/|
-  | o  4:7fb047a69f22 E
+  | o  4:7fb047a69f22 E b-F at divergent1
   | |
   | | o  10:7c78f703e465 D2 b-D


More information about the Mercurial-devel mailing list