Show More
@@ -328,6 +328,45 b' def rstdoc(context, mapping, args):' | |||||
328 |
|
328 | |||
329 | return minirst.format(text, style=style, keep=['verbose']) |
|
329 | return minirst.format(text, style=style, keep=['verbose']) | |
330 |
|
330 | |||
|
331 | def shortest(context, mapping, args): | |||
|
332 | """usage: shortest(node, minlength=4) | |||
|
333 | """ | |||
|
334 | if not (1 <= len(args) <= 2): | |||
|
335 | raise error.ParseError(_("shortest() expects one or two arguments")) | |||
|
336 | ||||
|
337 | node = stringify(args[0][0](context, mapping, args[0][1])) | |||
|
338 | ||||
|
339 | minlength = 4 | |||
|
340 | if len(args) > 1: | |||
|
341 | minlength = int(args[1][1]) | |||
|
342 | ||||
|
343 | cl = mapping['ctx']._repo.changelog | |||
|
344 | def isvalid(test): | |||
|
345 | try: | |||
|
346 | cl.index.partialmatch(test) | |||
|
347 | try: | |||
|
348 | int(test) | |||
|
349 | return False | |||
|
350 | except ValueError: | |||
|
351 | return True | |||
|
352 | except error.RevlogError: | |||
|
353 | return False | |||
|
354 | ||||
|
355 | shortest = node | |||
|
356 | startlength = max(6, minlength) | |||
|
357 | length = startlength | |||
|
358 | while True: | |||
|
359 | test = node[:length] | |||
|
360 | if isvalid(test): | |||
|
361 | shortest = test | |||
|
362 | if length == minlength or length > startlength: | |||
|
363 | return shortest | |||
|
364 | length -= 1 | |||
|
365 | else: | |||
|
366 | length += 1 | |||
|
367 | if len(shortest) <= length: | |||
|
368 | return shortest | |||
|
369 | ||||
331 | def strip(context, mapping, args): |
|
370 | def strip(context, mapping, args): | |
332 | if not (1 <= len(args) <= 2): |
|
371 | if not (1 <= len(args) <= 2): | |
333 | raise error.ParseError(_("strip expects one or two arguments")) |
|
372 | raise error.ParseError(_("strip expects one or two arguments")) | |
@@ -369,6 +408,7 b' funcs = {' | |||||
369 | "join": join, |
|
408 | "join": join, | |
370 | "label": label, |
|
409 | "label": label, | |
371 | "rstdoc": rstdoc, |
|
410 | "rstdoc": rstdoc, | |
|
411 | "shortest": shortest, | |||
372 | "strip": strip, |
|
412 | "strip": strip, | |
373 | "sub": sub, |
|
413 | "sub": sub, | |
374 | } |
|
414 | } |
@@ -1626,3 +1626,14 b' Test branches inside if statement:' | |||||
1626 |
|
1626 | |||
1627 | $ hg log -r 0 --template '{if(branches, "yes", "no")}\n' |
|
1627 | $ hg log -r 0 --template '{if(branches, "yes", "no")}\n' | |
1628 | no |
|
1628 | no | |
|
1629 | ||||
|
1630 | Test shortest(node) function: | |||
|
1631 | ||||
|
1632 | $ echo b > b | |||
|
1633 | $ hg ci -qAm b | |||
|
1634 | $ hg log --template '{shortest(node)}\n' | |||
|
1635 | d97c | |||
|
1636 | f776 | |||
|
1637 | $ hg log --template '{shortest(node, 10)}\n' | |||
|
1638 | d97c383ae3 | |||
|
1639 | f7769ec2ab |
General Comments 0
You need to be logged in to leave comments.
Login now