Patrick Mézard avatar Patrick Mézard committed 47d1045

uncommit: handle bookmarks

Extract the bookmarks handling code from amend and share it with
uncommit.

Comments (0)

Files changed (2)

     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:
         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
             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 = {}
             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'):
             phases.retractboundary(repo, oldphase, [newid])
             repo.dirstate.setparents(newid, node.nullid)
             _uncommitdirstate(repo, old, match)
+            updatebookmarks(newid)
         finally:
             wlock.release()
     finally:

tests/test-uncommit.t

   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
   $ hg uncommit
   abort: nothing to uncommit
   [255]
+  $ hg bookmarks
+   * touncommit-bm             3:5eb72dbe0cb4
+     touncommit-bm-inactive    3:5eb72dbe0cb4
+     unrelated                 2:f63b90038565
 
 Test no matches
 
   |
   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
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.