changeset 313:47d10459fa24

uncommit: handle bookmarks Extract the bookmarks handling code from amend and share it with uncommit.
author Patrick Mezard <patrick@mezard.eu>
date Tue, 26 Jun 2012 16:12:52 +0200
parents a7b5989d1d92
children ebd286f06879
files hgext/evolve.py tests/test-uncommit.t
diffstat 2 files changed, 50 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/evolve.py	Tue Jun 26 18:12:31 2012 +0200
+++ b/hgext/evolve.py	Tue Jun 26 16:12:52 2012 +0200
@@ -63,7 +63,7 @@
     if len(old.parents()) > 1: #XXX remove this unecessary limitation.
         raise error.Abort(_('cannot amend merge changesets'))
     base = old.p1()
-    bm = bookmarks.readcurrent(repo)
+    updatebookmarks = _bookmarksupdater(repo, old.node())
 
     wlock = repo.wlock()
     try:
@@ -134,19 +134,10 @@
         new = repo[newid]
         created = len(repo) != revcount
         if created:
-            # update the bookmark
-            if bm:
-                repo._bookmarks[bm] = newid
-                bookmarks.write(repo)
-
+            updatebookmarks(newid)
             # add evolution metadata
             collapsed = set([u.node() for u in updates] + [old.node()])
             repo.addcollapsedobsolete(collapsed, new.node())
-            oldbookmarks = repo.nodebookmarks(old.node())
-            for book in oldbookmarks:
-                repo._bookmarks[book] = new.node()
-            if oldbookmarks:
-                bookmarks.write(repo)
         else:
             # newid is an existing revision. It could make sense to
             # replace revisions with existing ones but probably not by
@@ -216,6 +207,25 @@
             return unstables[0]
     return None
 
+def _bookmarksupdater(repo, oldid):
+    """Return a callable update(newid) updating the current bookmark
+    and bookmarks bound to oldid to newid.
+    """
+    bm = bookmarks.readcurrent(repo)
+    def updatebookmarks(newid):
+        dirty = False
+        if bm:
+            repo._bookmarks[bm] = newid
+            dirty = True
+        oldbookmarks = repo.nodebookmarks(oldid)
+        if oldbookmarks:
+            for b in oldbookmarks:
+                repo._bookmarks[b] = newid
+            dirty = True
+        if dirty:
+            bookmarks.write(repo)
+    return updatebookmarks
+
 ### new command
 #############################
 cmdtable = {}
@@ -584,6 +594,7 @@
             if len(old.parents()) > 1:
                 raise util.Abort(_("cannot uncommit merge changeset"))
             oldphase = old.phase()
+            updatebookmarks = _bookmarksupdater(repo, old.node())
             # Recommit the filtered changeset
             newid = None
             if pats or opts.get('include') or opts.get('exclude'):
@@ -596,6 +607,7 @@
             phases.retractboundary(repo, oldphase, [newid])
             repo.dirstate.setparents(newid, node.nullid)
             _uncommitdirstate(repo, old, match)
+            updatebookmarks(newid)
         finally:
             wlock.release()
     finally:
--- a/tests/test-uncommit.t	Tue Jun 26 18:12:31 2012 +0200
+++ b/tests/test-uncommit.t	Tue Jun 26 16:12:52 2012 +0200
@@ -117,6 +117,25 @@
   l
   o
 
+Add a couple of bookmarks
+
+  $ glog --hidden
+  @  3:5eb72dbe0cb4@bar(stable/draft) touncommit
+  |
+  o    2:f63b90038565@default(stable/draft) merge
+  |\
+  | o  1:f15c744d48e8@default(stable/draft) addmore
+  |
+  o  0:07f494440405@default(stable/draft) adda
+  
+  $ hg bookmark -r 2 unrelated
+  $ hg bookmark touncommit-bm
+  $ hg bookmark --inactive touncommit-bm-inactive
+  $ hg bookmarks
+   * touncommit-bm             3:5eb72dbe0cb4
+     touncommit-bm-inactive    3:5eb72dbe0cb4
+     unrelated                 2:f63b90038565
+
 Prepare complicated working directory
 
   $ hg branch foo
@@ -135,6 +154,10 @@
   $ hg uncommit
   abort: nothing to uncommit
   [255]
+  $ hg bookmarks
+   * touncommit-bm             3:5eb72dbe0cb4
+     touncommit-bm-inactive    3:5eb72dbe0cb4
+     unrelated                 2:f63b90038565
 
 Test no matches
 
@@ -205,6 +228,10 @@
   |
   o  0:07f494440405@default(stable/draft) adda
   
+  $ hg bookmarks
+   * touncommit-bm             4:e8db4aa611f6
+     touncommit-bm-inactive    4:e8db4aa611f6
+     unrelated                 2:f63b90038565
   $ hg debugsuccessors
   5eb72dbe0cb4 e8db4aa611f6