Patchwork [2,of,7,bm-refactor,V3] bookmarks: factor out rename logic from commands

login
register
mail settings
Submitter Sean Farley
Date June 23, 2017, 6:33 p.m.
Message ID <49e4d804d8520243b36c.1498242798@1.0.0.127.in-addr.arpa>
Download mbox | patch
Permalink /patch/21640/
State Accepted
Headers show

Comments

Sean Farley - June 23, 2017, 6:33 p.m.
# HG changeset patch
# User Sean Farley <sean@farley.io>
# Date 1497377422 25200
#      Tue Jun 13 11:10:22 2017 -0700
# Branch bm-refactor
# Node ID 49e4d804d8520243b36c90237d0547ccb55cfb32
# Parent  d8b560800ad7ae1c0cbb83a2e810af891d26c0d5
bookmarks: factor out rename logic from commands

We keep the lock in the caller so that future devs are aware of the
locking implications.
via Mercurial-devel - June 23, 2017, 6:55 p.m.
On Fri, Jun 23, 2017 at 11:33 AM, Sean Farley <sean@farley.io> wrote:
> +        raise error.Abort(_("bookmark '%s' does not exist")
> +                          % old)

I've joined the line here in flight. Similar in patch 1.
Sean Farley - June 23, 2017, 7:02 p.m.
Martin von Zweigbergk <martinvonz@google.com> writes:

> On Fri, Jun 23, 2017 at 11:33 AM, Sean Farley <sean@farley.io> wrote:
>> +        raise error.Abort(_("bookmark '%s' does not exist")
>> +                          % old)
>
> I've joined the line here in flight. Similar in patch 1.

Ah, yeah, good idea.

Patch

diff --git a/mercurial/bookmarks.py b/mercurial/bookmarks.py
index 0371a52..f0b9f5f 100644
--- a/mercurial/bookmarks.py
+++ b/mercurial/bookmarks.py
@@ -704,5 +704,27 @@  def delete(repo, tr, names):
                               mark)
         if mark == repo._activebookmark:
             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)
diff --git a/mercurial/commands.py b/mercurial/commands.py
index 876fb3e..e882567 100644
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -980,19 +980,11 @@  def bookmark(ui, repo, *names, **opts):
                 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, tr, rename, names[0], force, inactive)
             elif names:
                 tr = repo.transaction('bookmark')
                 newact = None
                 for mark in names:
                     mark = bookmarks.checkformat(repo, mark)