Patchwork [03,of,12,V2] bookmarks: rewrite pushing local bookmarks in "commands.push()" by "compare()"

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Sept. 22, 2013, 2:05 p.m.
Message ID <474ac158844b0e554239.1379858729@feefifofum>
Download mbox | patch
Permalink /patch/2598/
State Superseded
Headers show

Comments

Katsunori FUJIWARA - Sept. 22, 2013, 2:05 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1379858556 -32400
#      Sun Sep 22 23:02:36 2013 +0900
# Node ID 474ac158844b0e55423971e4b8345318796e6305
# Parent  6f60d541bdf5bd90778c0e8395b3504d09d985d6
bookmarks: rewrite pushing local bookmarks in "commands.push()" by "compare()"

This patch adds "pushtoremote()", which uses "compare()" to compare
bookmarks between the local and the remote repositories, to replace
pushing local bookmarks in "commands.push()".

Patch

diff --git a/mercurial/bookmarks.py b/mercurial/bookmarks.py
--- a/mercurial/bookmarks.py
+++ b/mercurial/bookmarks.py
@@ -362,6 +362,46 @@ 
     if changed:
         localmarks.write()
 
+def pushtoremote(ui, repo, remote, targets):
+    class InvalidBookmark(KeyError):
+        """Exception raised when specified bookmark doesn't exist on
+        the local or remote repository"""
+
+    def push(b, old, new):
+        r = remote.pushkey('bookmarks', b, old, new)
+        if not r:
+            ui.warn(_('updating bookmark %s failed!\n') % b)
+            return True
+    def export(b, scid, dcid):
+        ui.status(_("exporting bookmark %s\n") % b)
+        if dcid is None:
+            dcid = ''
+        return push(b, dcid, scid)
+    def delete(b, scid, dcid):
+        # treat as "deleted locally"
+        ui.status(_("deleting remote bookmark %s\n") % b)
+        return push(b, dcid, '')
+    def invalid(b, scid, dcid):
+        raise InvalidBookmark(b)
+
+    try:
+        if _execactions(compare(repo, repo._bookmarks,
+                                remote.listkeys('bookmarks'),
+                                srchex=hex, targets=targets),
+                        {'addsrc': export,
+                         'adddst': delete,
+                         'advsrc': export,
+                         'advdst': export,
+                         'diverge': export,
+                         'differ': export,
+                         'invalid': invalid,
+                         }):
+            return 1
+    except InvalidBookmark, inst:
+        ui.warn(_('bookmark %s does not exist on the local '
+                  'or remote repository!\n') % inst.args[0])
+        return 2
+
 def diff(ui, dst, src):
     ui.status(_("searching for changed bookmarks\n"))
 
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -4698,25 +4698,11 @@ 
     result = not result
 
     if opts.get('bookmark'):
-        rb = other.listkeys('bookmarks')
-        for b in opts['bookmark']:
-            # explicit push overrides remote bookmark if any
-            if b in repo._bookmarks:
-                ui.status(_("exporting bookmark %s\n") % b)
-                new = repo[b].hex()
-            elif b in rb:
-                ui.status(_("deleting remote bookmark %s\n") % b)
-                new = '' # delete
-            else:
-                ui.warn(_('bookmark %s does not exist on the local '
-                          'or remote repository!\n') % b)
-                return 2
-            old = rb.get(b, '')
-            r = other.pushkey('bookmarks', b, old, new)
-            if not r:
-                ui.warn(_('updating bookmark %s failed!\n') % b)
-                if not result:
-                    result = 2
+        bresult = bookmarks.pushtoremote(ui, repo, other, opts['bookmark'])
+        if bresult == 2:
+            return 2
+        if not result and bresult:
+            result = 2
 
     return result