bookmarks: add support for push --bookmark to export bookmarks
authorMatt Mackall <mpm@selenic.com>
Thu, 17 Jun 2010 15:54:26 -0500
changeset 11379 e1a145eebb6a
parent 11378 cb21fb1b55ba
child 11381 a4d84792338b
child 11386 b2c0bc41165f
bookmarks: add support for push --bookmark to export bookmarks
hgext/bookmarks.py
--- a/hgext/bookmarks.py	Thu Jun 17 14:26:23 2010 -0500
+++ b/hgext/bookmarks.py	Thu Jun 17 15:54:26 2010 -0500
@@ -409,6 +409,42 @@
 
     return result
 
+def push(oldpush, ui, repo, dest=None, **opts):
+    dopush = True
+    if opts.get('bookmark'):
+        dopush = False
+        for b in opts['bookmark']:
+            if b in repo._bookmarks:
+                dopush = True
+                opts.setdefault('rev', []).append(b)
+
+    result = 0
+    if dopush:
+        result = oldpush(ui, repo, dest, **opts)
+
+    if opts.get('bookmark'):
+        # this is an unpleasant hack as push will do this internally
+        dest = ui.expandpath(dest or 'default-push', dest or 'default')
+        dest, branches = hg.parseurl(dest, opts.get('branch'))
+        other = hg.repository(hg.remoteui(repo, opts), dest)
+        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()
+            else:
+                ui.status(_("deleting remote bookmark %s\n") % b)
+                new = '' # delete
+            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
+
+    return result
+
 def uisetup(ui):
     extensions.wrapfunction(repair, "strip", strip)
     if ui.configbool('bookmarks', 'track.current'):
@@ -417,6 +453,9 @@
     entry = extensions.wrapcommand(commands.table, 'pull', pull)
     entry[1].append(('B', 'bookmark', [],
                      _("bookmark to import")))
+    entry = extensions.wrapcommand(commands.table, 'push', push)
+    entry[1].append(('B', 'bookmark', [],
+                     _("bookmark to export")))
 
     pushkey.register('bookmarks', pushbookmark, listbookmarks)