Patchwork [2,of,2] bookmarks: fix check of hash-like name to not abort by ambiguous identifier

login
register
mail settings
Submitter Yuya Nishihara
Date May 25, 2017, 2:56 p.m.
Message ID <5751a63a255082d23ffe.1495724192@mimosa>
Download mbox | patch
Permalink /patch/20900/
State Accepted
Headers show

Comments

Yuya Nishihara - May 25, 2017, 2:56 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1495722000 -32400
#      Thu May 25 23:20:00 2017 +0900
# Node ID 5751a63a255082d23ffe3ff0cf347497c25bff54
# Parent  2dd7893c39fb0df46cfa6fbaa901f8da95beffda
bookmarks: fix check of hash-like name to not abort by ambiguous identifier

'mark in repo' may raise LookupError. I set it to not be warned since bookmark
names shorter than 4 chars aren't checked and short names are likely to be
ambiguous.
via Mercurial-devel - May 27, 2017, 9:43 p.m.
On Thu, May 25, 2017 at 7:56 AM, Yuya Nishihara <yuya@tcha.org> wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1495722000 -32400
> #      Thu May 25 23:20:00 2017 +0900
> # Node ID 5751a63a255082d23ffe3ff0cf347497c25bff54
> # Parent  2dd7893c39fb0df46cfa6fbaa901f8da95beffda
> bookmarks: fix check of hash-like name to not abort by ambiguous identifier
>
> 'mark in repo' may raise LookupError. I set it to not be warned since bookmark
> names shorter than 4 chars aren't checked and short names are likely to be
> ambiguous.

It's a little weird that one gets a warning when there's a conflict
with one hash, but not when there's a conflict with two. On the other
hand, it seems unlikely to ever make a difference in practice. Will
queue, thanks.

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -968,11 +968,16 @@  def bookmark(ui, repo, *names, **opts):
             and not force):
             raise error.Abort(
                 _("a bookmark cannot have the name of an existing branch"))
-        if len(mark) > 3 and mark in repo and not force:
-            repo.ui.warn(
-                _("bookmark %s matches a changeset hash\n"
-                  "(did you leave a -r out of an 'hg bookmark' command?)\n") %
-                mark)
+        if len(mark) > 3 and not force:
+            try:
+                shadowhash = (mark in repo)
+            except error.LookupError: # ambiguous identifier
+                shadowhash = False
+            if shadowhash:
+                repo.ui.warn(
+                    _("bookmark %s matches a changeset hash\n"
+                      "(did you leave a -r out of an 'hg bookmark' command?)\n")
+                    % mark)
 
     if delete and rename:
         raise error.Abort(_("--delete and --rename are incompatible"))
diff --git a/tests/test-bookmarks.t b/tests/test-bookmarks.t
--- a/tests/test-bookmarks.t
+++ b/tests/test-bookmarks.t
@@ -320,8 +320,36 @@  bookmark with a name that matches a node
   $ hg bookmark -d 925d80f479bb
   $ hg bookmark -d db815d6d32e6
 
+  $ cd ..
+
+bookmark with a name that matches an ambiguous node id
+
+  $ hg init ambiguous
+  $ cd ambiguous
+  $ echo 0 > a
+  $ hg ci -qAm 0
+  $ for i in 1057 2857 4025; do
+  >   hg up -q 0
+  >   echo $i > a
+  >   hg ci -qm $i
+  > done
+  $ hg up -q null
+  $ hg log -r0: -T '{rev}:{node}\n'
+  0:b4e73ffab476aa0ee32ed81ca51e07169844bc6a
+  1:c56256a09cd28e5764f32e8e2810d0f01e2e357a
+  2:c5623987d205cd6d9d8389bfc40fff9dbb670b48
+  3:c562ddd9c94164376c20b86b0b4991636a3bf84f
+
+  $ hg bookmark -r0 c562
+  $ hg bookmarks
+     c562                      0:b4e73ffab476
+
+  $ cd ..
+
 incompatible options
 
+  $ cd repo
+
   $ hg bookmark -m Y -d Z
   abort: --delete and --rename are incompatible
   [255]