comparison mercurial/revset.py @ 46809:56d441256e82

revset: introduce a `nodefromfile` revset I though we had one, but actually we don't seem to. So here is a revset to reuse a list of node previously stored. Differential Revision: https://phab.mercurial-scm.org/D10230
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Wed, 17 Mar 2021 20:06:35 +0100
parents 66fb04552122
children d4ba4d51f85f
comparison
equal deleted inserted replaced
46808:b26f9560f40d 46809:56d441256e82
1333 iterasc=False, 1333 iterasc=False,
1334 ) 1334 )
1335 return subset & rs 1335 return subset & rs
1336 1336
1337 1337
1338 @predicate(b'nodefromfile(path)')
1339 def nodefromfile(repo, subset, x):
1340 """
1341 An alias for ``::.`` (ancestors of the working directory's first parent).
1342 If file pattern is specified, the histories of files matching given
1343 pattern in the revision given by startrev are followed, including copies.
1344 """
1345 path = getstring(x, _(b"nodefromfile require a file path"))
1346 listed_rev = set()
1347 try:
1348 with pycompat.open(path, 'rb') as f:
1349 for line in f:
1350 n = line.strip()
1351 rn = _node(repo, n)
1352 if rn is not None:
1353 listed_rev.add(rn)
1354 except IOError as exc:
1355 m = _(b'cannot open nodes file "%s": %s')
1356 m %= (path, encoding.strtolocal(exc.strerror))
1357 raise error.Abort(m)
1358 return subset & baseset(listed_rev)
1359
1360
1338 @predicate(b'all()', safe=True) 1361 @predicate(b'all()', safe=True)
1339 def getall(repo, subset, x): 1362 def getall(repo, subset, x):
1340 """All changesets, the same as ``0:tip``.""" 1363 """All changesets, the same as ``0:tip``."""
1341 # i18n: "all" is a keyword 1364 # i18n: "all" is a keyword
1342 getargs(x, 0, 0, _(b"all takes no arguments")) 1365 getargs(x, 0, 0, _(b"all takes no arguments"))
1695 1718
1696 names -= {nullrev} 1719 names -= {nullrev}
1697 return subset & names 1720 return subset & names
1698 1721
1699 1722
1700 @predicate(b'id(string)', safe=True) 1723 def _node(repo, n):
1701 def node_(repo, subset, x): 1724 """process a node input"""
1702 """Revision non-ambiguously specified by the given hex string prefix.""" 1725 rn = None
1703 # i18n: "id" is a keyword
1704 l = getargs(x, 1, 1, _(b"id requires one argument"))
1705 # i18n: "id" is a keyword
1706 n = getstring(l[0], _(b"id requires a string"))
1707 if len(n) == 40: 1726 if len(n) == 40:
1708 try: 1727 try:
1709 rn = repo.changelog.rev(bin(n)) 1728 rn = repo.changelog.rev(bin(n))
1710 except error.WdirUnsupported: 1729 except error.WdirUnsupported:
1711 rn = wdirrev 1730 rn = wdirrev
1712 except (LookupError, TypeError): 1731 except (LookupError, TypeError):
1713 rn = None 1732 rn = None
1714 else: 1733 else:
1715 rn = None
1716 try: 1734 try:
1717 pm = scmutil.resolvehexnodeidprefix(repo, n) 1735 pm = scmutil.resolvehexnodeidprefix(repo, n)
1718 if pm is not None: 1736 if pm is not None:
1719 rn = repo.changelog.rev(pm) 1737 rn = repo.changelog.rev(pm)
1720 except LookupError: 1738 except LookupError:
1721 pass 1739 pass
1722 except error.WdirUnsupported: 1740 except error.WdirUnsupported:
1723 rn = wdirrev 1741 rn = wdirrev
1742 return rn
1743
1744
1745 @predicate(b'id(string)', safe=True)
1746 def node_(repo, subset, x):
1747 """Revision non-ambiguously specified by the given hex string prefix."""
1748 # i18n: "id" is a keyword
1749 l = getargs(x, 1, 1, _(b"id requires one argument"))
1750 # i18n: "id" is a keyword
1751 n = getstring(l[0], _(b"id requires a string"))
1752 rn = _node(repo, n)
1724 1753
1725 if rn is None: 1754 if rn is None:
1726 return baseset() 1755 return baseset()
1727 result = baseset([rn]) 1756 result = baseset([rn])
1728 return result & subset 1757 return result & subset