[PATCH 3 of 3] bookmarks: treat bookmark names as fnmatch() patterns

Will Maier willmaier at ml1.net
Wed Sep 15 05:15:10 CDT 2010


# HG changeset patch
# User Will Maier <willmaier at ml1.net>
# Date 1284521115 18000
# Node ID bd2666c126d819ade2cd353ee8f145a8acf1ee3d
# Parent  0e087d121d7125cd0f1ddaac1baa099c6e096de0
bookmarks: treat bookmark names as fnmatch() patterns

diff --git a/hgext/bookmarks.py b/hgext/bookmarks.py
--- a/hgext/bookmarks.py
+++ b/hgext/bookmarks.py
@@ -31,6 +31,7 @@ branching.
 from mercurial.i18n import _
 from mercurial.node import nullid, nullrev, hex, short
 from mercurial import util, commands, repair, extensions, pushkey, hg, url
+import fnmatch
 import os
 
 def write(repo):
@@ -406,16 +407,20 @@ def pull(oldpull, ui, repo, source="defa
         other = hg.repository(hg.remoteui(repo, opts), source)
         rb = other.listkeys('bookmarks')
 
+        pullrb = []
         for b in opts['bookmark']:
-            if b not in rb:
-                raise util.Abort(_('remote bookmark %s not found!') % b)
-            opts.setdefault('rev', []).append(b)
+            matches = [bn for bn in rb if fnmatch.fnmatch(bn, b)]
+            if not matches:
+                raise util.Abort(_('pattern %s matches no remote bookmarks!') % b)
+            opts.setdefault('rev', []).extend(matches)
+            pullrb.extend(matches)
+        pullrb.sort()
 
     result = oldpull(ui, repo, source, **opts)
 
     # update specified bookmarks
     if opts.get('bookmark'):
-        for b in opts['bookmark']:
+        for b in pullrb:
             # explicit pull overrides local bookmark if any
             ui.status(_("importing bookmark %s\n") % b)
             repo._bookmarks[b] = repo[rb[b]].node()
@@ -427,10 +432,16 @@ def push(oldpush, ui, repo, dest=None, *
     dopush = True
     if opts.get('bookmark'):
         dopush = False
+        pushrb = []
         for b in opts['bookmark']:
-            if b in repo._bookmarks:
+            matches = [bn for bn in repo._bookmarks if fnmatch.fnmatch(bn, b)]
+            if not matches:
+                raise util.Abort(_('pattern %s matches no local bookmarks!') % b)
+            else:
                 dopush = True
-                opts.setdefault('rev', []).append(b)
+                opts.setdefault('rev', []).extend(matches)
+                pushrb.extend(matches)
+        pushrb.sort()
 
     result = 0
     if dopush:
@@ -442,7 +453,7 @@ def push(oldpush, ui, repo, dest=None, *
         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 b in pushrb:
             # explicit push overrides remote bookmark if any
             if b in repo._bookmarks:
                 ui.status(_("exporting bookmark %s\n") % b)
@@ -504,11 +515,11 @@ def uisetup(ui):
 
     entry = extensions.wrapcommand(commands.table, 'pull', pull)
     entry[1].append(('B', 'bookmark', [],
-                     _("bookmark to import"),
+                     _("bookmark pattern to import"),
                      _('BOOKMARK')))
     entry = extensions.wrapcommand(commands.table, 'push', push)
     entry[1].append(('B', 'bookmark', [],
-                     _("bookmark to export"),
+                     _("bookmark pattern to export"),
                      _('BOOKMARK')))
     entry = extensions.wrapcommand(commands.table, 'incoming', incoming)
     entry[1].append(('B', 'bookmarks', False,
diff --git a/tests/test-bookmarks-pushpull.t b/tests/test-bookmarks-pushpull.t
--- a/tests/test-bookmarks-pushpull.t
+++ b/tests/test-bookmarks-pushpull.t
@@ -40,8 +40,24 @@ import bookmark by name
   $ hg bookmark
      X                         0:4e3505fd9583
 
+import bookmark by pattern
+
+  $ hg pull -B \* ../a
+  pulling from ../a
+  searching for changes
+  no changes found
+  importing bookmark X
+  importing bookmark Y
+  importing bookmark Z
+  $ hg bookmarks
+     Y                         0:4e3505fd9583
+     X                         0:4e3505fd9583
+     Z                         0:4e3505fd9583
+
 export bookmark by name
 
+  $ hg up
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
   $ hg bookmark W
   $ hg bookmark foo
   $ hg bookmark foobar
@@ -54,11 +70,31 @@ export bookmark by name
      Y                         0:4e3505fd9583
      X                         0:4e3505fd9583
    * Z                         0:4e3505fd9583
-     W                         -1:000000000000
+     W                         0:4e3505fd9583
 
-push/pull name that doesn't exist
+export bookmark by pattern
 
-  $ hg push -B badname ../a
-  bookmark badname does not exist on the local or remote repository!
-  $ hg pull -B anotherbadname ../a
-  abort: remote bookmark anotherbadname not found!
+  $ hg push -B \* ../a
+  pushing to ../a
+  searching for changes
+  no changes found
+  exporting bookmark W
+  exporting bookmark X
+  exporting bookmark Y
+  exporting bookmark Z
+  exporting bookmark foo
+  exporting bookmark foobar
+  $ hg -R ../a bookmarks
+     foo                       0:4e3505fd9583
+     foobar                    0:4e3505fd9583
+     W                         0:4e3505fd9583
+     Y                         0:4e3505fd9583
+     X                         0:4e3505fd9583
+   * Z                         0:4e3505fd9583
+
+push/pull pattern that doesn't match
+
+  $ hg push -B badpattern ../a
+  abort: pattern badpattern matches no local bookmarks!
+  $ hg pull -B anotherbadpattern ../a
+  abort: pattern anotherbadpattern matches no remote bookmarks!


More information about the Mercurial-devel mailing list