changeset 48033:62f325f9b347

bookmarks: add an option to make pull mirror remote bookmarks For backups for instance. Merging bookmarks is not a useful behavior in that case. Differential Revision: https://phab.mercurial-scm.org/D11490
author Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
date Wed, 22 Sep 2021 17:14:54 -0400
parents 12966768595a
children 37a41267d000
files mercurial/bookmarks.py mercurial/configitems.py mercurial/helptext/config.txt tests/test-bookmarks-pushpull.t
diffstat 4 files changed, 68 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/bookmarks.py	Thu Sep 23 09:42:20 2021 -0700
+++ b/mercurial/bookmarks.py	Wed Sep 22 17:14:54 2021 -0400
@@ -680,8 +680,25 @@
     return books
 
 
-def updatefromremote(ui, repo, remotemarks, path, trfunc, explicit=()):
-    ui.debug(b"checking for updated bookmarks\n")
+def mirroring_remote(ui, repo, remotemarks):
+    """computes the bookmark changes that set the local bookmarks to
+    remotemarks"""
+    changed = []
+    localmarks = repo._bookmarks
+    for (b, id) in pycompat.iteritems(remotemarks):
+        if id != localmarks.get(b, None) and id in repo:
+            changed.append((b, id, ui.debug, _(b"updating bookmark %s\n") % b))
+    for b in localmarks:
+        if b not in remotemarks:
+            changed.append(
+                (b, None, ui.debug, _(b"removing bookmark %s\n") % b)
+            )
+    return changed
+
+
+def merging_from_remote(ui, repo, remotemarks, path, explicit=()):
+    """computes the bookmark changes that merge remote bookmarks into the
+    local bookmarks, based on comparebookmarks"""
     localmarks = repo._bookmarks
     (
         addsrc,
@@ -752,6 +769,15 @@
                 _(b"remote bookmark %s points to locally missing %s\n")
                 % (b, hex(scid)[:12])
             )
+    return changed
+
+
+def updatefromremote(ui, repo, remotemarks, path, trfunc, explicit=()):
+    ui.debug(b"checking for updated bookmarks\n")
+    if ui.configbool(b'bookmarks', b'mirror'):
+        changed = mirroring_remote(ui, repo, remotemarks)
+    else:
+        changed = merging_from_remote(ui, repo, remotemarks, path, explicit)
 
     if changed:
         tr = trfunc()
@@ -760,7 +786,7 @@
         for b, node, writer, msg in sorted(changed, key=key):
             changes.append((b, node))
             writer(msg)
-        localmarks.applychanges(repo, tr, changes)
+        repo._bookmarks.applychanges(repo, tr, changes)
 
 
 def incoming(ui, repo, peer):
--- a/mercurial/configitems.py	Thu Sep 23 09:42:20 2021 -0700
+++ b/mercurial/configitems.py	Wed Sep 22 17:14:54 2021 -0400
@@ -207,6 +207,11 @@
     b'pushing',
     default=list,
 )
+coreconfigitem(
+    b'bookmarks',
+    b'mirror',
+    default=False,
+)
 # bundle.mainreporoot: internal hack for bundlerepo
 coreconfigitem(
     b'bundle',
--- a/mercurial/helptext/config.txt	Thu Sep 23 09:42:20 2021 -0700
+++ b/mercurial/helptext/config.txt	Wed Sep 22 17:14:54 2021 -0400
@@ -418,6 +418,16 @@
 If no suitable authentication entry is found, the user is prompted
 for credentials as usual if required by the remote.
 
+``bookmarks``
+-------------
+
+Controls some aspect of bookmarks.
+
+``mirror``
+    When pulling, instead of merging local bookmarks and remote bookmarks,
+    replace local bookmarks by remote bookmarks. This is useful to replicate
+    a repository, or as an optimization. (default: False)
+
 ``cmdserver``
 -------------
 
--- a/tests/test-bookmarks-pushpull.t	Thu Sep 23 09:42:20 2021 -0700
+++ b/tests/test-bookmarks-pushpull.t	Wed Sep 22 17:14:54 2021 -0400
@@ -490,6 +490,30 @@
      Y                         0:4e3505fd9583
      Z                         1:0d2164f0ce0d
 
+mirroring bookmarks
+
+  $ hg book
+     @                         1:9b140be10808
+     @foo                      2:0d2164f0ce0d
+     X                         1:9b140be10808
+     X@foo                     2:0d2164f0ce0d
+     Y                         0:4e3505fd9583
+     Z                         2:0d2164f0ce0d
+     foo                       -1:000000000000
+   * foobar                    1:9b140be10808
+  $ cp .hg/bookmarks .hg/bookmarks.bak
+  $ hg book -d X
+  $ hg pull ../a --config bookmarks.mirror=true
+  pulling from ../a
+  searching for changes
+  no changes found
+  $ hg book
+     @                         2:0d2164f0ce0d
+     X                         2:0d2164f0ce0d
+     Y                         0:4e3505fd9583
+     Z                         2:0d2164f0ce0d
+  $ mv .hg/bookmarks.bak .hg/bookmarks
+
 explicit pull should overwrite the local version (issue4439)
 
   $ hg update -r X