Mercurial > public > mercurial-scm > hg
comparison mercurial/revlog.py @ 34250:448725a2ef73
templater: extract shortest() logic from template function
It can be useful for extensions to be able to produce the shortest
unambiguous hash (including the in-tree "show" extension). That logic
is currently inside the shortest() template function. Let's move it
out of the templater. I've put it on revlog since it's closely related
to revlog._partialmatch. We may also want a convenience method on
context, but I'll leave that for a later patch.
Differential Revision: https://phab.mercurial-scm.org/D724
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Fri, 15 Sep 2017 00:01:57 -0700 |
parents | c8b6ed51386b |
children | 1db9abf407c5 |
comparison
equal
deleted
inserted
replaced
34249:ab379eed2e31 | 34250:448725a2ef73 |
---|---|
1126 if n: | 1126 if n: |
1127 return n | 1127 return n |
1128 | 1128 |
1129 raise LookupError(id, self.indexfile, _('no match found')) | 1129 raise LookupError(id, self.indexfile, _('no match found')) |
1130 | 1130 |
1131 def shortest(self, hexnode, minlength=1): | |
1132 """Find the shortest unambiguous prefix that matches hexnode.""" | |
1133 def isvalid(test): | |
1134 try: | |
1135 if self._partialmatch(test) is None: | |
1136 return False | |
1137 | |
1138 try: | |
1139 i = int(test) | |
1140 # if we are a pure int, then starting with zero will not be | |
1141 # confused as a rev; or, obviously, if the int is larger | |
1142 # than the value of the tip rev | |
1143 if test[0] == '0' or i > len(self): | |
1144 return True | |
1145 return False | |
1146 except ValueError: | |
1147 return True | |
1148 except error.RevlogError: | |
1149 return False | |
1150 except error.WdirUnsupported: | |
1151 # single 'ff...' match | |
1152 return True | |
1153 | |
1154 shortest = hexnode | |
1155 startlength = max(6, minlength) | |
1156 length = startlength | |
1157 while True: | |
1158 test = hexnode[:length] | |
1159 if isvalid(test): | |
1160 shortest = test | |
1161 if length == minlength or length > startlength: | |
1162 return shortest | |
1163 length -= 1 | |
1164 else: | |
1165 length += 1 | |
1166 if len(shortest) <= length: | |
1167 return shortest | |
1168 | |
1131 def cmp(self, node, text): | 1169 def cmp(self, node, text): |
1132 """compare text with a given file revision | 1170 """compare text with a given file revision |
1133 | 1171 |
1134 returns True if text is different than what is stored. | 1172 returns True if text is different than what is stored. |
1135 """ | 1173 """ |