bookmarks: factor out adding a list of bookmarks logic from commands
authorSean Farley <sean@farley.io>
Tue, 20 Jun 2017 15:18:40 -0700
changeset 38309 ee081f91b179a60d069976c5d75443f501a58586
parent 38308 e0a8dd6c87c70a89d8168a19cc6cbbc5a580c039
child 38310 ac57603a44feaac4579130ac31ecf20587f42f8a
push id534
push usergszorc@mozilla.com
push dateSat, 24 Jun 2017 07:33:46 +0000
bookmarks: factor out adding a list of bookmarks logic from commands We keep the lock in the caller so that future devs are aware of the locking implications.
mercurial/bookmarks.py
mercurial/commands.py
--- a/mercurial/bookmarks.py
+++ b/mercurial/bookmarks.py
@@ -721,8 +721,40 @@ def rename(repo, tr, old, new, force=Fal
     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)
+
+def addbookmarks(repo, tr, names, rev=None, force=False, inactive=False):
+    """add a list of bookmarks
+
+    If force is specified, then the new name can overwrite an existing
+    bookmark.
+
+    If inactive is specified, then do not activate any bookmark. Otherwise, the
+    first bookmark is activated.
+
+    Raises an abort error if old is not in the bookmark store.
+    """
+    marks = repo._bookmarks
+    cur = repo.changectx('.').node()
+    newact = None
+    for mark in names:
+        mark = checkformat(repo, mark)
+        if newact is None:
+            newact = mark
+        if inactive and mark == repo._activebookmark:
+            deactivate(repo)
+            return
+        tgt = cur
+        if rev:
+            tgt = scmutil.revsingle(repo, rev).node()
+        marks.checkconflict(mark, force, tgt)
+        marks[mark] = tgt
+    if not inactive and cur == marks[newact] and not rev:
+        activate(repo, newact)
+    elif cur != tgt and newact == repo._activebookmark:
+        deactivate(repo)
+    marks.recordchange(tr)
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -966,47 +966,30 @@ def bookmark(ui, repo, *names, **opts):
     if not names and (delete or rev):
         raise error.Abort(_("bookmark name required"))
 
     if delete or rename or names or inactive:
         wlock = lock = tr = None
         try:
             wlock = repo.wlock()
             lock = repo.lock()
-            cur = repo.changectx('.').node()
             marks = repo._bookmarks
             if delete:
                 tr = repo.transaction('bookmark')
                 bookmarks.delete(repo, tr, 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"))
                 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)
-                    if newact is None:
-                        newact = mark
-                    if inactive and mark == repo._activebookmark:
-                        bookmarks.deactivate(repo)
-                        return
-                    tgt = cur
-                    if rev:
-                        tgt = scmutil.revsingle(repo, rev).node()
-                    marks.checkconflict(mark, force, tgt)
-                    marks[mark] = tgt
-                if not inactive and cur == marks[newact] and not rev:
-                    bookmarks.activate(repo, newact)
-                elif cur != tgt and newact == repo._activebookmark:
-                    bookmarks.deactivate(repo)
+                bookmarks.addbookmarks(repo, tr, names, rev, force, inactive)
             elif inactive:
                 if len(marks) == 0:
                     ui.status(_("no bookmarks set\n"))
                 elif not repo._activebookmark:
                     ui.status(_("no active bookmark\n"))
                 else:
                     bookmarks.deactivate(repo)
             if tr is not None: