diff hggit/__init__.py @ 999:a128b9a53b5a

revset_gitnode: allow abbreviated hashes The previous implementation only allowed passing 40-hexdigit hashes or 12-hexdigit abbreviations to `gitnode(hash)`. Shorter or longer hashes were accepted, but failed silently. With this fix, any unambiguous abbreviation is accepted. `gitnode(a5b)` selects the revision whose Git hash starts with `a5b`, if there is one, and aborts if there are several.
author Sietse Brouwer <sbbrouwer@gmail.com>
date Thu, 06 Oct 2016 10:11:14 +0200
parents 9c15c89088fd
children 6d1c19dceaf4
line wrap: on
line diff
--- a/hggit/__init__.py	Mon Sep 12 07:40:42 2016 -0700
+++ b/hggit/__init__.py	Thu Oct 06 10:11:14 2016 +0200
@@ -30,6 +30,7 @@
 from bisect import insort
 from git_handler import GitHandler
 from mercurial.node import hex
+from mercurial.error import LookupError
 from mercurial.i18n import _
 from mercurial import (
     bundlerepo,
@@ -382,7 +383,9 @@
 
 def revset_gitnode(repo, subset, x):
     '''``gitnode(hash)``
-    Select changesets that originate in the given Git revision.
+    Select the changeset that originates in the given Git revision. The hash
+    may be abbreviated: `gitnode(a5b)` selects the revision whose Git hash
+    starts with `a5b`. Aborts if multiple changesets match the abbreviation.
     '''
     args = revset.getargs(x, 1, 1, "gitnode takes one argument")
     rev = revset.getstring(args[0],
@@ -394,8 +397,11 @@
         gitnode = git.map_git_get(hex(node(r)))
         if gitnode is None:
             return False
-        return rev in [gitnode, gitnode[:12]]
-    return baseset(r for r in subset if matches(r))
+        return gitnode.startswith(rev)
+    result = baseset(r for r in subset if matches(r))
+    if 0 <= len(result) < 2:
+        return result
+    raise LookupError(rev, git.map_file, _('ambiguous identifier'))
 
 def gitnodekw(**args):
     """:gitnode: String.  The Git changeset identification hash, as a 40 hexadecimal digit string."""