Patchwork [4,of,7,V4] exchange: add `pushbookmarks` part generator

login
register
mail settings
Submitter Stanislau Hlebik
Date Sept. 4, 2016, 10:46 p.m.
Message ID <db526b94eeb678879c2f.1473029216@dev1918.lla1.facebook.com>
Download mbox | patch
Permalink /patch/16544/
State Accepted
Headers show

Comments

Stanislau Hlebik - Sept. 4, 2016, 10:46 p.m.
# HG changeset patch
# User Stanislau Hlebik <stash@fb.com>
# Date 1473022588 25200
#      Sun Sep 04 13:56:28 2016 -0700
# Node ID db526b94eeb678879c2faceb40c64aebbe1054af
# Parent  70a86f2bd0e7684eda82310dc9024b346f59dbf6
exchange: add `pushbookmarks` part generator
Pierre-Yves David - Sept. 6, 2016, 12:59 p.m.
On 09/05/2016 12:46 AM, Stanislau Hlebik wrote:
> # HG changeset patch
> # User Stanislau Hlebik <stash@fb.com>
> # Date 1473022588 25200
> #      Sun Sep 04 13:56:28 2016 -0700
> # Node ID db526b94eeb678879c2faceb40c64aebbe1054af
> # Parent  70a86f2bd0e7684eda82310dc9024b346f59dbf6
> exchange: add `pushbookmarks` part generator
>
> diff --git a/mercurial/exchange.py b/mercurial/exchange.py
> --- a/mercurial/exchange.py
> +++ b/mercurial/exchange.py
> @@ -806,6 +806,50 @@
>          markers = sorted(pushop.outobsmarkers)
>          buildobsmarkerspart(bundler, markers)
>
> +@b2partsgenerator('pushbookmarks')
> +def _pushb2bookmarksnew(pushop, bundler):
> +    if 'bookmarks' in pushop.stepsdone:
> +        return
> +    b2caps = bundle2.bundle2caps(pushop.remote)
> +    if 'pushbookmarks' not in b2caps:
> +        return
> +    pushop.stepsdone.add('bookmarks')
> +    if not pushop.outbookmarks:
> +        return
> +    booktoaction = {}
> +    bookmarksdata = []
> +    enc = encoding.fromlocal
> +    for book, old, new in pushop.outbookmarks:
> +        action = 'update'
> +        if not old:
> +            action = 'export'
> +        elif not new:
> +            action = 'delete'
> +        booktoaction[book] = action
> +        bookmarksdata.append('%s %s %s' % (enc(book), enc(old), enc(new)))
> +    part = bundler.newpart('pushbookmarks', data='\n'.join(bookmarksdata))

This part seems common with your the generation during pull, we should 
probably build a "part-building-function" and use that in both place.

Patch

diff --git a/mercurial/exchange.py b/mercurial/exchange.py
--- a/mercurial/exchange.py
+++ b/mercurial/exchange.py
@@ -806,6 +806,50 @@ 
         markers = sorted(pushop.outobsmarkers)
         buildobsmarkerspart(bundler, markers)
 
+@b2partsgenerator('pushbookmarks')
+def _pushb2bookmarksnew(pushop, bundler):
+    if 'bookmarks' in pushop.stepsdone:
+        return
+    b2caps = bundle2.bundle2caps(pushop.remote)
+    if 'pushbookmarks' not in b2caps:
+        return
+    pushop.stepsdone.add('bookmarks')
+    if not pushop.outbookmarks:
+        return
+    booktoaction = {}
+    bookmarksdata = []
+    enc = encoding.fromlocal
+    for book, old, new in pushop.outbookmarks:
+        action = 'update'
+        if not old:
+            action = 'export'
+        elif not new:
+            action = 'delete'
+        booktoaction[book] = action
+        bookmarksdata.append('%s %s %s' % (enc(book), enc(old), enc(new)))
+    part = bundler.newpart('pushbookmarks', data='\n'.join(bookmarksdata))
+
+    def handlereply(op):
+        ui = pushop.ui
+        partrep = op.records.getreplies(part.id)
+        replies = {}
+        for reply in partrep['pushbookmarks']:
+            bookmark = reply['bookmark']
+            ret = int(reply['return'])
+            replies[bookmark] = ret
+        for book, action in booktoaction.items():
+            if book not in replies:
+                pushop.ui.warn(_('server ignored bookmark %s update\n') % book)
+            else:
+                ret = replies[book]
+                if ret:
+                    ui.status(bookmsgmap[action][0] % book)
+                else:
+                    ui.warn(bookmsgmap[action][1] % book)
+                    if pushop.bkresult is not None:
+                        pushop.bkresult = 1
+    return handlereply
+
 @b2partsgenerator('bookmarks')
 def _pushb2bookmarks(pushop, bundler):
     """handle bookmark push through bundle2"""