[PATCH 2 of 2] update: update to current bookmark if it moved out from under us (issue3682)

Kevin Bullock kbullock+mercurial at ringworld.org
Mon Jan 21 13:48:05 CST 2013


# HG changeset patch
# User Kevin Bullock <kbullock at ringworld.org>
# Date 1358797630 21600
# Branch stable
# Node ID e3a54977eda7d9d675f7a440ce3dc6d069084107
# Parent  911229b0565cb0c031baaae36f21a5829f78e3b1
update: update to current bookmark if it moved out from under us (issue3682)

If the current bookmark (the one listed in .hg/bookmarks.current)
doesn't point to a parent of the working directory, e.g. if it was moved
by a pull, use that as the update target instead of the tipmost
descendent.

A small predicate is (finally) added to the bookmarks module to check
whether the current bookmark is also active.

diff --git a/mercurial/bookmarks.py b/mercurial/bookmarks.py
--- a/mercurial/bookmarks.py
+++ b/mercurial/bookmarks.py
@@ -134,6 +134,19 @@ def unsetcurrent(repo):
     finally:
         wlock.release()
 
+def iscurrent(repo, mark=None, parents=None):
+    '''Tell whether the current bookmark is also active
+
+    I.e., the bookmark listed in .hg/bookmarks.current also points to a
+    parent of the working directory.
+    '''
+    if not mark:
+        mark = repo._bookmarkcurrent
+    if not parents:
+        parents = [p.node() for p in repo[None].parents()]
+    marks = repo._bookmarks
+    return (mark in marks and marks[mark] in parents)
+
 def updatecurrentbookmark(repo, oldnode, curbranch):
     try:
         return update(repo, oldnode, repo.branchtip(curbranch))
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -5961,7 +5961,12 @@ def update(ui, repo, node=None, rev=None
     # with no argument, we also move the current bookmark, if any
     movemarkfrom = None
     if rev is None:
-        movemarkfrom = repo['.'].node()
+        curmark = repo._bookmarkcurrent
+        if bookmarks.iscurrent(repo):
+            movemarkfrom = repo['.'].node()
+        else:
+            ui.status(_("updating to active bookmark %s\n") % curmark)
+            rev = repo._bookmarks[curmark]
 
     # if we defined a bookmark, we have to remember the original bookmark name
     brev = rev
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
@@ -173,6 +173,9 @@ divergent bookmarks
 
 update a remote bookmark from a non-head to a head
 
+  $ cd ../a
+  $ hg up -q Y
+  $ cd ../b
   $ hg up -q Y
   $ echo c3 > f2
   $ hg ci -Am3
@@ -188,10 +191,19 @@ update a remote bookmark from a non-head
   updating bookmark Y
   $ hg -R ../a book
      @                         1:0d2164f0ce0d
-   * X                         1:0d2164f0ce0d
+     X                         1:0d2164f0ce0d
      Y                         3:f6fc62dde3c0
      Z                         1:0d2164f0ce0d
 
+update to current bookmark if it's not the parent
+
+  $ cd ../a
+  $ hg up
+  updating to active bookmark Y
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ hg up -q X
+  $ cd ../b
+
 diverging a remote bookmark fails
 
   $ hg up -q 4e3505fd9583


More information about the Mercurial-devel mailing list