[PATCH] bookmarks: support -B '<regexp>' in push/pull wrappers #2

Edouard Gomez ed.gomez at free.fr
Thu Jul 8 17:21:34 CDT 2010


# HG changeset patch
# User Edouard Gomez <ed.gomez at free.fr>
# Date 1278627510 -7200
# Node ID ffdeb0f6afa86adf3141506fefa5b3ae7290dd32
# Parent  774c01d92fa6d31a1de36731248ddf2a8728838d
bookmarks: support -B '<regexp>' in push/pull wrappers

diff --git a/hgext/bookmarks.py b/hgext/bookmarks.py
--- a/hgext/bookmarks.py
+++ b/hgext/bookmarks.py
@@ -32,6 +32,7 @@
 from mercurial.node import nullid, nullrev, hex, short
 from mercurial import util, commands, repair, extensions, pushkey, hg, url
 import os
+import re
 
 def write(repo):
     '''Write bookmarks
@@ -398,19 +399,30 @@
         other = hg.repository(hg.remoteui(repo, opts), source)
         rb = other.listkeys('bookmarks')
 
-        for b in opts['bookmark']:
-            if b not in rb:
-                raise util.Abort(_('remote bookmark %s not found!') % b)
-            opts.setdefault('rev', []).append(b)
+        for bre in opts['bookmark']:
+            try:
+                reprog = re.compile(bre)
+                rmatching = [b for b in rb if reprog.match(b)]
+                if not len(rmatching):
+                    raise util.Abort(_('remote bookmark %s not found!') % bre)
+                else:
+                    for b in rmatching:
+                        opts.setdefault('rev', []).append(b)
+            except re.error:
+                ui.warn(_('ignoring invalid bookmark regexp %s\n' % bre))
+                opts['bookmark'].remove(bre)
 
     result = oldpull(ui, repo, source, **opts)
 
     # update specified bookmarks
     if opts.get('bookmark'):
-        for b in opts['bookmark']:
-            # explicit pull overrides local bookmark if any
-            ui.status(_("importing bookmark %s\n") % b)
-            repo._bookmarks[b] = repo[rb[b]].node()
+        for bre in opts['bookmark']:
+            reprog = re.compile(bre)
+            rmatching = [b for b in rb if reprog.match(b)]
+            for b in rmatching:
+                # explicit pull overrides local bookmark if any
+                ui.status(_("importing bookmark %s\n") % b)
+                repo._bookmarks[b] = repo[rb[b]].node()
         write(repo)
 
     return result
@@ -419,8 +431,14 @@
     dopush = True
     if opts.get('bookmark'):
         dopush = False
-        for b in opts['bookmark']:
-            if b in repo._bookmarks:
+        for bre in opts['bookmark']:
+            try:
+                reprog = re.compile(bre)
+                lmatching = [b for b in repo._bookmarks if reprog.match(b)]
+            except re.error:
+                ui.warn(_('ignoring invalid bookmark regexp %s\n' % bre))
+                opts['bookmark'].remove(bre)
+            for b in lmatching:
                 dopush = True
                 opts.setdefault('rev', []).append(b)
 
@@ -434,21 +452,31 @@
         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']:
+        for bre in opts['bookmark']:
+            reprog = re.compile(bre)
+            lmatching = [b for b in repo._bookmarks if reprog.match(b)]
             # explicit push overrides remote bookmark if any
-            if b in repo._bookmarks:
+            for b in lmatching:
                 ui.status(_("exporting bookmark %s\n") % b)
                 new = repo[b].hex()
-            else:
+                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
+            # if a remote bookmark matches and it's not found in local
+            # bookmarks, it's meant for deletion
+            rmatching = [b for b in rb if reprog.match(b) and b not in lmatching]
+            for b in rmatching:
                 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
-
+                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 diffbookmarks(ui, repo, remote):
@@ -492,10 +520,10 @@
 
     entry = extensions.wrapcommand(commands.table, 'pull', pull)
     entry[1].append(('B', 'bookmark', [],
-                     _("bookmark to import")))
+                     _("bookmark(s) to import"), _('REGEXP')))
     entry = extensions.wrapcommand(commands.table, 'push', push)
     entry[1].append(('B', 'bookmark', [],
-                     _("bookmark to export")))
+                     _("bookmark(s) to export"), _('REGEXP')))
     entry = extensions.wrapcommand(commands.table, 'incoming', incoming)
     entry[1].append(('B', 'bookmarks', False,
                      _("compare bookmark")))

-- 
Edouard Gomez


More information about the Mercurial-devel mailing list