Patchwork [3,of,7,bm-refactor,V2] bookmarks: factor out adding a list of bookmarks from commands

login
register
mail settings
Submitter Sean Farley
Date June 21, 2017, 8:45 p.m.
Message ID <03933080a047f8438b1e.1498077910@1.0.0.127.in-addr.arpa>
Download mbox | patch
Permalink /patch/21601/
State Changes Requested
Headers show

Comments

Sean Farley - June 21, 2017, 8:45 p.m.
# HG changeset patch
# User Sean Farley <sean@farley.io>
# Date 1497997120 25200
#      Tue Jun 20 15:18:40 2017 -0700
# Branch bm-refactor
# Node ID 03933080a047f8438b1e400b513a7909779382a8
# Parent  50f0aad403c1c79e5c6fff4788c215eceac18118
bookmarks: factor out adding a list of bookmarks from commands

While we're here, let's use fancy context managers. I believe this
should still work since our locks are re-entrant.
via Mercurial-devel - June 22, 2017, 5:58 a.m.
On Wed, Jun 21, 2017 at 1:45 PM, Sean Farley <sean@farley.io> wrote:
> # HG changeset patch
> # User Sean Farley <sean@farley.io>
> # Date 1497997120 25200
> #      Tue Jun 20 15:18:40 2017 -0700
> # Branch bm-refactor
> # Node ID 03933080a047f8438b1e400b513a7909779382a8
> # Parent  50f0aad403c1c79e5c6fff4788c215eceac18118
> bookmarks: factor out adding a list of bookmarks 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 f0b9f5f..d1bac53 100644
> --- a/mercurial/bookmarks.py
> +++ b/mercurial/bookmarks.py
> @@ -726,5 +726,37 @@ def rename(repo, tr, old, new, force=Fal
>      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()

Looks like the corresponding line should have been removed from
commands.py (says test-check-pyflakes.t).

> +    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)
> diff --git a/mercurial/commands.py b/mercurial/commands.py
> index e882567..9d4088b 100644
> --- a/mercurial/commands.py
> +++ b/mercurial/commands.py
> @@ -983,27 +983,11 @@ def bookmark(ui, repo, *names, **opts):
>                  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"))
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/bookmarks.py b/mercurial/bookmarks.py
index f0b9f5f..d1bac53 100644
--- a/mercurial/bookmarks.py
+++ b/mercurial/bookmarks.py
@@ -726,5 +726,37 @@  def rename(repo, tr, old, new, force=Fal
     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)
diff --git a/mercurial/commands.py b/mercurial/commands.py
index e882567..9d4088b 100644
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -983,27 +983,11 @@  def bookmark(ui, repo, *names, **opts):
                 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"))