changeset 33006:e0a8dd6c87c7

bookmarks: factor out rename logic from commands We keep the lock in the caller so that future devs are aware of the locking implications.
author Sean Farley <sean@farley.io>
date Tue, 13 Jun 2017 11:10:22 -0700
parents 9343fce87789
children ee081f91b179
files mercurial/bookmarks.py mercurial/commands.py
diffstat 2 files changed, 22 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/bookmarks.py	Mon Jun 12 23:02:48 2017 -0700
+++ b/mercurial/bookmarks.py	Tue Jun 13 11:10:22 2017 -0700
@@ -705,3 +705,24 @@
             deactivate(repo)
         del marks[mark]
     marks.recordchange(tr)
+
+def rename(repo, tr, old, new, force=False, inactive=False):
+    """rename a bookmark from old to new
+
+    If force is specified, then the new name can overwrite an existing
+    bookmark.
+
+    If inactive is specified, then do not activate the new bookmark.
+
+    Raises an abort error if old is not in the bookmark store.
+    """
+    marks = repo._bookmarks
+    mark = checkformat(repo, new)
+    if old not in marks:
+        raise error.Abort(_("bookmark '%s' does not exist") % old)
+    marks.checkconflict(mark, force)
+    marks[mark] = marks[old]
+    if repo._activebookmark == old and not inactive:
+        activate(repo, mark)
+    del marks[old]
+    marks.recordchange(tr)
--- a/mercurial/commands.py	Mon Jun 12 23:02:48 2017 -0700
+++ b/mercurial/commands.py	Tue Jun 13 11:10:22 2017 -0700
@@ -982,15 +982,7 @@
                     raise error.Abort(_("new bookmark name required"))
                 elif len(names) > 1:
                     raise error.Abort(_("only one new bookmark name allowed"))
-                mark = bookmarks.checkformat(repo, names[0])
-                if rename not in marks:
-                    raise error.Abort(_("bookmark '%s' does not exist")
-                                      % rename)
-                marks.checkconflict(mark, force)
-                marks[mark] = marks[rename]
-                if repo._activebookmark == rename and not inactive:
-                    bookmarks.activate(repo, mark)
-                del marks[rename]
+                bookmarks.rename(repo, tr, rename, names[0], force, inactive)
             elif names:
                 tr = repo.transaction('bookmark')
                 newact = None