[PATCH v2] mq: introduce -B option to remove a bookmark

David Soria Parra dsp at php.net
Sun May 13 14:52:24 CDT 2012


# HG changeset patch
# User David Soria Parra <dsp at php.net>
# Date 1336938726 -7200
# Node ID 95d6cceaabd43d70b0f9239d0beae8a783ee6fb6
# Parent  a0a756c91dadafdefb6cfef4eeef20fc33c52f1f
mq: introduce -B option to remove a bookmark

Add a -B option to remove a bookmark. All revisions are unreachable
from a different head or a different bookmark will be removed too.

This helps with topic branch workflow. You can create a topic branch
and remove it if not needed anymore with hg strip -B topic/xyz.

diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -2884,8 +2884,10 @@
           ('', 'no-backup', None, _('no backups')),
           ('', 'nobackup', None, _('no backups (DEPRECATED)')),
           ('n', '', None, _('ignored  (DEPRECATED)')),
-          ('k', 'keep', None, _("do not modify working copy during strip"))],
-          _('hg strip [-k] [-f] [-n] REV...'))
+          ('k', 'keep', None, _("do not modify working copy during strip")),
+          ('B', 'bookmark', '', _("remove revs only reachable from given"
+                                  " bookmark"))],
+          _('hg strip [-k] [-f] [-n] [-B bookmark] REV...'))
 def strip(ui, repo, *revs, **opts):
     """strip changesets and all their descendants from the repository
 
@@ -2920,6 +2922,33 @@
     cl = repo.changelog
     revs = list(revs) + opts.get('rev')
     revs = set(scmutil.revrange(repo, revs))
+
+    if opts.get('bookmark'):
+        mark = opts.get('bookmark')
+        marks = repo._bookmarks
+        if mark not in marks:
+            raise util.Abort(_("bookmark '%s' not found") % mark)
+
+        # If the requested bookmark is not the only one pointing to a a revision
+        # we have to only delete the bookmark and not strip anything. revsets
+        # cannot detect that case.
+        uniquebm = True
+        for m, n in marks.iteritems():
+            if m != mark and n == repo[mark].node():
+                uniquebm = False
+                break
+        if uniquebm:
+            rmatch = revset.formatspec("ancestors(bookmark(%s)) - "
+                                   "ancestors(head() and not bookmark(%s)) - "
+                                   "ancestors(bookmark() and not bookmark(%s))",
+                                   mark, mark, mark)
+            rsrevs = revset.match(ui, rmatch)(repo, range(len(repo)))
+            revs.update(set(rsrevs))
+        if not revs:
+            del marks[mark]
+            repo._writebookmarks(mark)
+            ui.write(_("bookmark '%s' deleted\n") % mark)
+
     if not revs:
         raise util.Abort(_('empty revision set'))
 
@@ -2967,6 +2996,12 @@
 
     repo.mq.strip(repo, revs, backup=backup, update=update,
                   force=opts.get('force'))
+
+    if opts.get('bookmark'):
+        del marks[mark]
+        repo._writebookmarks(marks)
+        ui.write(_("bookmark '%s' deleted\n") % mark)
+
     return 0
 
 @command("qselect",
diff --git a/tests/test-mq-strip.t b/tests/test-mq-strip.t
--- a/tests/test-mq-strip.t
+++ b/tests/test-mq-strip.t
@@ -430,3 +430,37 @@
   $ hg strip 'not ancestors(x)'
   saved backup bundle to $TESTTMP/issue3299/.hg/strip-backup/*-backup.hg (glob)
 
+test hg strip -B bookmark
+
+  $ cd ..
+  $ hg init bookmarks
+  $ cd bookmarks
+  $ hg debugbuilddag '..<2.*1/2:m<2+3:c<m+3:a<2.:b'
+  $ hg bookmark -r 'a' 'todelete'
+  $ hg bookmark -r 'b' 'B'
+  $ hg bookmark -r 'b' 'nostrip'
+  $ hg bookmark -r 'c' 'delete'
+  $ hg up -C todelete
+  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ hg strip -B nostrip
+  bookmark 'nostrip' deleted
+  abort: empty revision set
+  [255]
+  $ hg strip -B todelete
+  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  saved backup bundle to $TESTTMP/bookmarks/.hg/strip-backup/*-backup.hg (glob)
+  bookmark 'todelete' deleted
+  $ hg id -ir dcbb326fdec2
+  abort: unknown revision 'dcbb326fdec2'!
+  [255]
+  $ hg id -ir d62d843c9a01
+  d62d843c9a01
+  $ hg bookmarks
+     B                         9:ff43616e5d0f
+     delete                    6:2702dd0c91e7
+  $ hg strip -B delete
+  saved backup bundle to $TESTTMP/bookmarks/.hg/strip-backup/*-backup.hg (glob)
+  bookmark 'delete' deleted
+  $ hg id -ir 6:2702dd0c91e7
+  abort: unknown revision '2702dd0c91e7'!
+  [255]


More information about the Mercurial-devel mailing list