[PATCH 4 of 9 bm-refactor] bookmarks: factor out rename logic from commands

Sean Farley sean at farley.io
Tue Jun 20 20:29:27 EDT 2017


# HG changeset patch
# User Sean Farley <sean at farley.io>
# Date 1497377422 25200
#      Tue Jun 13 11:10:22 2017 -0700
# Branch bm-refactor
# Node ID c14bfdac2be657612b693fca8ec7dea2f3c47182
# Parent  9f36f5280f7d2a0230efe1fd1dcf1a1d57f56ae8
bookmarks: factor out rename logic from commands

While we're here, let's use fancy context managers. I believe this
should still work since our locks are re-entrant.

diff --git a/mercurial/bookmarks.py b/mercurial/bookmarks.py
index c344cfc..8ba4f75 100644
--- a/mercurial/bookmarks.py
+++ b/mercurial/bookmarks.py
@@ -705,5 +705,28 @@ def delete(repo, names):
                                   mark)
             if mark == repo._activebookmark:
                 deactivate(repo)
             del marks[mark]
         marks.recordchange(tr)
+
+def rename(repo, 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.
+    """
+    with repo.wlock(), repo.lock(), repo.transaction('bookmark') as tr:
+        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)
diff --git a/mercurial/commands.py b/mercurial/commands.py
index d017f0a..8867bc5 100644
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -974,24 +974,15 @@ def bookmark(ui, repo, *names, **opts):
             cur = repo.changectx('.').node()
             marks = repo._bookmarks
             if delete:
                 bookmarks.delete(repo, names)
             elif rename:
-                tr = repo.transaction('bookmark')
                 if not names:
                     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, rename, names[0], force, inactive)
             elif names:
                 tr = repo.transaction('bookmark')
                 newact = None
                 for mark in names:
                     mark = bookmarks.checkformat(repo, mark)


More information about the Mercurial-devel mailing list