diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py --- a/mercurial/scmutil.py +++ b/mercurial/scmutil.py @@ -418,6 +418,43 @@ tree = revsetlang.parse(revspec) return tree and tree[0] in ('range', 'rangepre', 'rangepost', 'rangeall') +def shortest(revlog, node, minlength): + def isvalid(test): + try: + if revlog._partialmatch(test) is None: + return False + + try: + i = int(test) + # if we are a pure int, then starting with zero will not be + # confused as a rev; or, obviously, if the int is larger than + # the value of the tip rev + if test[0] == '0' or i > len(revlog): + return True + return False + except ValueError: + return True + except error.RevlogError: + return False + except error.WdirUnsupported: + # single 'ff...' match + return True + + shortest = node + startlength = max(6, minlength) + length = startlength + while True: + test = node[:length] + if isvalid(test): + shortest = test + if length == minlength or length > startlength: + return shortest + length -= 1 + else: + length += 1 + if len(shortest) <= length: + return shortest + def revpair(repo, revs): if not revs: return repo.dirstate.p1(), None diff --git a/mercurial/templater.py b/mercurial/templater.py --- a/mercurial/templater.py +++ b/mercurial/templater.py @@ -24,6 +24,7 @@ registrar, revset as revsetmod, revsetlang, + scmutil, templatefilters, templatekw, util, @@ -1023,41 +1024,7 @@ # which would be unacceptably slow. so we look for hash collision in # unfiltered space, which means some hashes may be slightly longer. cl = mapping['ctx']._repo.unfiltered().changelog - def isvalid(test): - try: - if cl._partialmatch(test) is None: - return False - - try: - i = int(test) - # if we are a pure int, then starting with zero will not be - # confused as a rev; or, obviously, if the int is larger than - # the value of the tip rev - if test[0] == '0' or i > len(cl): - return True - return False - except ValueError: - return True - except error.RevlogError: - return False - except error.WdirUnsupported: - # single 'ff...' match - return True - - shortest = node - startlength = max(6, minlength) - length = startlength - while True: - test = node[:length] - if isvalid(test): - shortest = test - if length == minlength or length > startlength: - return shortest - length -= 1 - else: - length += 1 - if len(shortest) <= length: - return shortest + return scmutil.shortest(cl, node, minlength) @templatefunc('strip(text[, chars])') def strip(context, mapping, args):