Patchwork [4,of,8,V7] bundle2: add `bookmarks` part handler

login
register
mail settings
Submitter Stanislau Hlebik
Date Nov. 2, 2016, 2:06 p.m.
Message ID <2f89680108366512c1a6.1478095606@dev1918.lla1.facebook.com>
Download mbox | patch
Permalink /patch/17282/
State Superseded
Headers show

Comments

Stanislau Hlebik - Nov. 2, 2016, 2:06 p.m.
# HG changeset patch
# User Stanislau Hlebik <stash@fb.com>
# Date 1478016027 25200
#      Tue Nov 01 09:00:27 2016 -0700
# Branch stable
# Node ID 2f89680108366512c1a6345abec5cebdb85170ac
# Parent  d752daa2b736f0336a18760940104c59d6bd1a5c
bundle2: add `bookmarks` part handler

Applies bookmarks part to the local repo. `processbookmarksmode` determines
how remote bookmarks are handled. They are either ignored ('ignore' mode),
diverged ('diverge' mode) or applied ('apply' mode).

Patch

diff --git a/mercurial/bundle2.py b/mercurial/bundle2.py
--- a/mercurial/bundle2.py
+++ b/mercurial/bundle2.py
@@ -154,7 +154,9 @@ 
 import sys
 
 from .i18n import _
+from .node import hex
 from . import (
+    bookmarks as bookmod,
     changegroup,
     error,
     obsolete,
@@ -287,13 +289,21 @@ 
     * a way to construct a bundle response when applicable.
     """
 
-    def __init__(self, repo, transactiongetter, captureoutput=True):
+    def __init__(self, repo, transactiongetter, captureoutput=True,
+                 input=None):
+        """
+        `input` is a dictionary that is passed to part handlers to tweak
+        their behaviour
+        """
         self.repo = repo
         self.ui = repo.ui
         self.records = unbundlerecords()
         self.gettransaction = transactiongetter
         self.reply = None
         self.captureoutput = captureoutput
+        if input is None:
+            input = {}
+        self.input = input
 
 class TransactionUnavailable(RuntimeError):
     pass
@@ -1624,3 +1634,33 @@ 
 
     cache.write()
     op.ui.debug('applied %i hgtags fnodes cache entries\n' % count)
+
+@parthandler('bookmarks')
+def handlebookmarks(op, inpart):
+    """Processes bookmarks part.
+
+    `processbookmarksmode` determines how remote bookmarks are handled. They are
+    either ignored ('ignore' mode), diverged ('diverge' mode) or applied
+    ('apply' mode). 'ignore' mode is used to get bookmarks and process them
+    later, 'diverge' mode is used to process bookmarks during pull, 'apply'
+    mode is used during push.
+    """
+
+    bookmarks = {}
+    bookmarks = bookmod.decodebookmarks(inpart.read())
+    processbookmarksmode = op.input.get('processbookmarksmode', 'ignore')
+    if processbookmarksmode == 'apply':
+        for bookmark, node in bookmarks.items():
+            if node:
+                op.repo._bookmarks[bookmark] = node
+            else:
+                del op.repo._bookmarks[bookmark]
+        op.repo._bookmarks.recordchange(op.gettransaction())
+    elif processbookmarksmode == 'diverge':
+        remotepath = op.input.get('remotepath', '')
+        explicitbookmarks = op.input.get('explicitbookmarks', ())
+        bookmod.updatefromremote(op.ui, op.repo, bookmarks,
+                                 remotepath, op.gettransaction,
+                                 explicit=explicitbookmarks,
+                                 srchex=hex)
+    op.records.add('bookmarks', bookmarks)