##// END OF EJS Templates
scmutil: make shortesthexnodeidprefix() take a full binary nodeid...
Martin von Zweigbergk -
r37963:7b295562 default
parent child Browse files
Show More
@@ -29,7 +29,6 b' from __future__ import absolute_import'
29
29
30 from mercurial.i18n import _
30 from mercurial.i18n import _
31 from mercurial.node import (
31 from mercurial.node import (
32 hex,
33 nullrev,
32 nullrev,
34 )
33 )
35 from mercurial import (
34 from mercurial import (
@@ -448,8 +447,8 b' def longestshortest(repo, revs, minlen=4'
448 if not revs:
447 if not revs:
449 return minlen
448 return minlen
450 cl = repo.changelog
449 cl = repo.changelog
451 return max(len(scmutil.shortesthexnodeidprefix(repo, hex(cl.node(r)),
450 return max(len(scmutil.shortesthexnodeidprefix(repo, cl.node(r), minlen))
452 minlen)) for r in revs)
451 for r in revs)
453
452
454 # Adjust the docstring of the show command so it shows all registered views.
453 # Adjust the docstring of the show command so it shows all registered views.
455 # This is a bit hacky because it runs at the end of module load. When moved
454 # This is a bit hacky because it runs at the end of module load. When moved
@@ -443,12 +443,12 b' def resolvehexnodeidprefix(repo, prefix)'
443 repo.changelog.rev(node) # make sure node isn't filtered
443 repo.changelog.rev(node) # make sure node isn't filtered
444 return node
444 return node
445
445
446 def shortesthexnodeidprefix(repo, hexnode, minlength=1):
446 def shortesthexnodeidprefix(repo, node, minlength=1):
447 """Find the shortest unambiguous prefix that matches hexnode."""
447 """Find the shortest unambiguous prefix that matches hexnode."""
448 # _partialmatch() of filtered changelog could take O(len(repo)) time,
448 # _partialmatch() of filtered changelog could take O(len(repo)) time,
449 # which would be unacceptably slow. so we look for hash collision in
449 # which would be unacceptably slow. so we look for hash collision in
450 # unfiltered space, which means some hashes may be slightly longer.
450 # unfiltered space, which means some hashes may be slightly longer.
451 return repo.unfiltered().changelog.shortest(hexnode, minlength)
451 return repo.unfiltered().changelog.shortest(hex(node), minlength)
452
452
453 def isrevsymbol(repo, symbol):
453 def isrevsymbol(repo, symbol):
454 """Checks if a symbol exists in the repo.
454 """Checks if a symbol exists in the repo.
@@ -10,6 +10,9 b' from __future__ import absolute_import'
10 import re
10 import re
11
11
12 from .i18n import _
12 from .i18n import _
13 from .node import (
14 bin,
15 )
13 from . import (
16 from . import (
14 color,
17 color,
15 encoding,
18 encoding,
@@ -579,7 +582,7 b' def shortest(context, mapping, args):'
579 # i18n: "shortest" is a keyword
582 # i18n: "shortest" is a keyword
580 raise error.ParseError(_("shortest() expects one or two arguments"))
583 raise error.ParseError(_("shortest() expects one or two arguments"))
581
584
582 node = evalstring(context, mapping, args[0])
585 hexnode = evalstring(context, mapping, args[0])
583
586
584 minlength = 4
587 minlength = 4
585 if len(args) > 1:
588 if len(args) > 1:
@@ -588,6 +591,20 b' def shortest(context, mapping, args):'
588 _("shortest() expects an integer minlength"))
591 _("shortest() expects an integer minlength"))
589
592
590 repo = context.resource(mapping, 'ctx')._repo
593 repo = context.resource(mapping, 'ctx')._repo
594 if len(hexnode) > 40:
595 return hexnode
596 elif len(hexnode) == 40:
597 try:
598 node = bin(hexnode)
599 except TypeError:
600 return hexnode
601 else:
602 try:
603 node = scmutil.resolvehexnodeidprefix(repo, hexnode)
604 except (error.LookupError, error.WdirUnsupported):
605 return hexnode
606 if not node:
607 return hexnode
591 return scmutil.shortesthexnodeidprefix(repo, node, minlength)
608 return scmutil.shortesthexnodeidprefix(repo, node, minlength)
592
609
593 @templatefunc('strip(text[, chars])')
610 @templatefunc('strip(text[, chars])')
@@ -3900,6 +3900,21 b' Test shortest(node) function:'
3900 $ hg log -r 'wdir()' -T '{node|shortest}\n'
3900 $ hg log -r 'wdir()' -T '{node|shortest}\n'
3901 ffff
3901 ffff
3902
3902
3903 $ hg log --template '{shortest("f")}\n' -l1
3904 f
3905
3906 $ hg log --template '{shortest("0123456789012345678901234567890123456789")}\n' -l1
3907 0123456789012345678901234567890123456789
3908
3909 $ hg log --template '{shortest("01234567890123456789012345678901234567890123456789")}\n' -l1
3910 01234567890123456789012345678901234567890123456789
3911
3912 $ hg log --template '{shortest("not a hex string")}\n' -l1
3913 not a hex string
3914
3915 $ hg log --template '{shortest("not a hex string, but it'\''s 40 bytes long")}\n' -l1
3916 not a hex string, but it's 40 bytes long
3917
3903 $ cd ..
3918 $ cd ..
3904
3919
3905 Test shortest(node) with the repo having short hash collision:
3920 Test shortest(node) with the repo having short hash collision:
General Comments 0
You need to be logged in to leave comments. Login now