tags.py
915 lines
| 29.7 KiB
| text/x-python
|
PythonLexer
/ mercurial / tags.py
Greg Ward
|
r9149 | # tags.py - read tag info from local repository | ||
# | ||||
Raphaël Gomès
|
r47575 | # Copyright 2009 Olivia Mackall <olivia@selenic.com> | ||
Greg Ward
|
r9149 | # Copyright 2009 Greg Ward <greg@gerg.ca> | ||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Greg Ward
|
r9149 | |||
Greg Ward
|
r9151 | # Currently this module only deals with reading and caching tags. | ||
# Eventually, it could take care of updating (adding/removing/moving) | ||||
# tags too. | ||||
Greg Ward
|
r9149 | |||
Gregory Szorc
|
r25982 | from __future__ import absolute_import | ||
Idan Kamara
|
r14038 | import errno | ||
Augie Fackler
|
r42767 | import io | ||
Greg Ward
|
r9149 | |||
Gregory Szorc
|
r25982 | from .node import ( | ||
bin, | ||||
hex, | ||||
r42423 | nullrev, | |||
Gregory Szorc
|
r25982 | short, | ||
) | ||||
Pierre-Yves David
|
r31668 | from .i18n import _ | ||
Gregory Szorc
|
r25982 | from . import ( | ||
encoding, | ||||
error, | ||||
Pierre-Yves David
|
r31668 | match as matchmod, | ||
Gregory Szorc
|
r43376 | pycompat, | ||
Yuya Nishihara
|
r31025 | scmutil, | ||
Gregory Szorc
|
r25982 | util, | ||
) | ||||
Augie Fackler
|
r43346 | from .utils import stringutil | ||
Gregory Szorc
|
r25982 | |||
Gregory Szorc
|
r24735 | # Tags computation can be expensive and caches exist to make it fast in | ||
# the common case. | ||||
# | ||||
# The "hgtagsfnodes1" cache file caches the .hgtags filenode values for | ||||
# each revision in the repository. The file is effectively an array of | ||||
# fixed length records. Read the docs for "hgtagsfnodescache" for technical | ||||
# details. | ||||
# | ||||
# The .hgtags filenode cache grows in proportion to the length of the | ||||
# changelog. The file is truncated when the # changelog is stripped. | ||||
# | ||||
# The purpose of the filenode cache is to avoid the most expensive part | ||||
# of finding global tags, which is looking up the .hgtags filenode in the | ||||
# manifest for each head. This can take dozens or over 100ms for | ||||
# repositories with very large manifests. Multiplied by dozens or even | ||||
# hundreds of heads and there is a significant performance concern. | ||||
# | ||||
Gregory Szorc
|
r24762 | # There also exist a separate cache file for each repository filter. | ||
# These "tags-*" files store information about the history of tags. | ||||
Gregory Szorc
|
r24445 | # | ||
Gregory Szorc
|
r24762 | # The tags cache files consists of a cache validation line followed by | ||
# a history of tags. | ||||
Gregory Szorc
|
r24735 | # | ||
Gregory Szorc
|
r24760 | # The cache validation line has the format: | ||
Gregory Szorc
|
r24445 | # | ||
Gregory Szorc
|
r24760 | # <tiprev> <tipnode> [<filteredhash>] | ||
Gregory Szorc
|
r24445 | # | ||
Gregory Szorc
|
r24760 | # <tiprev> is an integer revision and <tipnode> is a 40 character hex | ||
# node for that changeset. These redundantly identify the repository | ||||
# tip from the time the cache was written. In addition, <filteredhash>, | ||||
# if present, is a 40 character hex hash of the contents of the filtered | ||||
# revisions for this filter. If the set of filtered revs changes, the | ||||
# hash will change and invalidate the cache. | ||||
Gregory Szorc
|
r24445 | # | ||
Gregory Szorc
|
r24760 | # The history part of the tags cache consists of lines of the form: | ||
Gregory Szorc
|
r24445 | # | ||
# <node> <tag> | ||||
# | ||||
# (This format is identical to that of .hgtags files.) | ||||
# | ||||
# <tag> is the tag name and <node> is the 40 character hex changeset | ||||
# the tag is associated with. | ||||
# | ||||
# Tags are written sorted by tag name. | ||||
# | ||||
# Tags associated with multiple changesets have an entry for each changeset. | ||||
# The most recent changeset (in terms of revlog ordering for the head | ||||
# setting it) for each tag is last. | ||||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r31993 | def fnoderevs(ui, repo, revs): | ||
"""return the list of '.hgtags' fnodes used in a set revisions | ||||
This is returned as list of unique fnodes. We use a list instead of a set | ||||
because order matters when it comes to tags.""" | ||||
unfi = repo.unfiltered() | ||||
tonode = unfi.changelog.node | ||||
nodes = [tonode(r) for r in revs] | ||||
Martin von Zweigbergk
|
r42425 | fnodes = _getfnodes(ui, repo, nodes) | ||
Pierre-Yves David
|
r31993 | fnodes = _filterfnodes(fnodes, nodes) | ||
return fnodes | ||||
Augie Fackler
|
r43346 | |||
Joerg Sonnenberger
|
r47771 | def _nulltonone(repo, value): | ||
Pierre-Yves David
|
r31995 | """convert nullid to None | ||
For tag value, nullid means "deleted". This small utility function helps | ||||
translating that to None.""" | ||||
Joerg Sonnenberger
|
r47771 | if value == repo.nullid: | ||
Pierre-Yves David
|
r31995 | return None | ||
return value | ||||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r31995 | def difftags(ui, repo, oldfnodes, newfnodes): | ||
"""list differences between tags expressed in two set of file-nodes | ||||
The list contains entries in the form: (tagname, oldvalue, new value). | ||||
None is used to expressed missing value: | ||||
('foo', None, 'abcd') is a new tag, | ||||
('bar', 'ef01', None) is a deletion, | ||||
('baz', 'abcd', 'ef01') is a tag movement. | ||||
""" | ||||
if oldfnodes == newfnodes: | ||||
return [] | ||||
oldtags = _tagsfromfnodes(ui, repo, oldfnodes) | ||||
newtags = _tagsfromfnodes(ui, repo, newfnodes) | ||||
# list of (tag, old, new): None means missing | ||||
entries = [] | ||||
for tag, (new, __) in newtags.items(): | ||||
Joerg Sonnenberger
|
r47771 | new = _nulltonone(repo, new) | ||
Pierre-Yves David
|
r31995 | old, __ = oldtags.pop(tag, (None, None)) | ||
Joerg Sonnenberger
|
r47771 | old = _nulltonone(repo, old) | ||
Pierre-Yves David
|
r31995 | if old != new: | ||
entries.append((tag, old, new)) | ||||
# handle deleted tags | ||||
for tag, (old, __) in oldtags.items(): | ||||
Joerg Sonnenberger
|
r47771 | old = _nulltonone(repo, old) | ||
Pierre-Yves David
|
r31995 | if old is not None: | ||
entries.append((tag, old, None)) | ||||
entries.sort() | ||||
return entries | ||||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r31996 | def writediff(fp, difflist): | ||
"""write tags diff information to a file. | ||||
Data are stored with a line based format: | ||||
<action> <hex-node> <tag-name>\n | ||||
Action are defined as follow: | ||||
-R tag is removed, | ||||
+A tag is added, | ||||
-M tag is moved (old value), | ||||
+M tag is moved (new value), | ||||
Example: | ||||
+A 875517b4806a848f942811a315a5bce30804ae85 t5 | ||||
See documentation of difftags output for details about the input. | ||||
""" | ||||
Augie Fackler
|
r43347 | add = b'+A %s %s\n' | ||
remove = b'-R %s %s\n' | ||||
updateold = b'-M %s %s\n' | ||||
updatenew = b'+M %s %s\n' | ||||
Pierre-Yves David
|
r31996 | for tag, old, new in difflist: | ||
# translate to hex | ||||
if old is not None: | ||||
old = hex(old) | ||||
if new is not None: | ||||
new = hex(new) | ||||
# write to file | ||||
if old is None: | ||||
fp.write(add % (new, tag)) | ||||
elif new is None: | ||||
fp.write(remove % (old, tag)) | ||||
else: | ||||
fp.write(updateold % (old, tag)) | ||||
fp.write(updatenew % (new, tag)) | ||||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r31706 | def findglobaltags(ui, repo): | ||
Augie Fackler
|
r46554 | """Find global tags in a repo: return a tagsmap | ||
Gregory Szorc
|
r24445 | |||
Pierre-Yves David
|
r31709 | tagsmap: tag name to (node, hist) 2-tuples. | ||
Gregory Szorc
|
r24445 | |||
The tags cache is read and updated as a side-effect of calling. | ||||
Augie Fackler
|
r46554 | """ | ||
Gregory Szorc
|
r24760 | (heads, tagfnode, valid, cachetags, shouldwrite) = _readtagcache(ui, repo) | ||
Greg Ward
|
r9152 | if cachetags is not None: | ||
assert not shouldwrite | ||||
# XXX is this really 100% correct? are there oddball special | ||||
# cases where a global tag should outrank a local tag but won't, | ||||
# because cachetags does not contain rank info? | ||||
Pierre-Yves David
|
r31710 | alltags = {} | ||
Pierre-Yves David
|
r31709 | _updatetags(cachetags, alltags) | ||
return alltags | ||||
Greg Ward
|
r9151 | |||
Gregory Szorc
|
r24445 | for head in reversed(heads): # oldest to newest | ||
r43943 | assert repo.changelog.index.has_node( | |||
head | ||||
Augie Fackler
|
r43347 | ), b"tag cache returned bogus head %s" % short(head) | ||
Pierre-Yves David
|
r31711 | fnodes = _filterfnodes(tagfnode, reversed(heads)) | ||
Pierre-Yves David
|
r31710 | alltags = _tagsfromfnodes(ui, repo, fnodes) | ||
Greg Ward
|
r9151 | |||
# and update the cache (if necessary) | ||||
if shouldwrite: | ||||
Gregory Szorc
|
r24760 | _writetagcache(ui, repo, valid, alltags) | ||
Pierre-Yves David
|
r31709 | return alltags | ||
Greg Ward
|
r9151 | |||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r31711 | def _filterfnodes(tagfnode, nodes): | ||
"""return a list of unique fnodes | ||||
The order of this list matches the order of "nodes". Preserving this order | ||||
is important as reading tags in different order provides different | ||||
results.""" | ||||
seen = set() # set of fnode | ||||
fnodes = [] | ||||
for no in nodes: # oldest to newest | ||||
fnode = tagfnode.get(no) | ||||
if fnode and fnode not in seen: | ||||
seen.add(fnode) | ||||
fnodes.append(fnode) | ||||
return fnodes | ||||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r31710 | def _tagsfromfnodes(ui, repo, fnodes): | ||
"""return a tagsmap from a list of file-node | ||||
tagsmap: tag name to (node, hist) 2-tuples. | ||||
The order of the list matters.""" | ||||
alltags = {} | ||||
fctx = None | ||||
for fnode in fnodes: | ||||
if fctx is None: | ||||
Augie Fackler
|
r43347 | fctx = repo.filectx(b'.hgtags', fileid=fnode) | ||
Pierre-Yves David
|
r31710 | else: | ||
fctx = fctx.filectx(fnode) | ||||
filetags = _readtags(ui, repo, fctx.data().splitlines(), fctx) | ||||
_updatetags(filetags, alltags) | ||||
return alltags | ||||
Augie Fackler
|
r43346 | |||
Greg Ward
|
r9149 | def readlocaltags(ui, repo, alltags, tagtypes): | ||
Gregory Szorc
|
r24445 | '''Read local tags in repo. Update alltags and tagtypes.''' | ||
Greg Ward
|
r9149 | try: | ||
Augie Fackler
|
r43347 | data = repo.vfs.read(b"localtags") | ||
Gregory Szorc
|
r25660 | except IOError as inst: | ||
Idan Kamara
|
r14038 | if inst.errno != errno.ENOENT: | ||
raise | ||||
return | ||||
# localtags is in the local encoding; re-encode to UTF-8 on | ||||
# input for consistency with the rest of this module. | ||||
filetags = _readtags( | ||||
Augie Fackler
|
r43347 | ui, repo, data.splitlines(), b"localtags", recode=encoding.fromlocal | ||
Augie Fackler
|
r43346 | ) | ||
Angel Ezquerra
|
r21823 | |||
# remove tags pointing to invalid nodes | ||||
cl = repo.changelog | ||||
Augie Fackler
|
r35846 | for t in list(filetags): | ||
Angel Ezquerra
|
r21823 | try: | ||
cl.rev(filetags[t][0]) | ||||
except (LookupError, ValueError): | ||||
del filetags[t] | ||||
Augie Fackler
|
r43347 | _updatetags(filetags, alltags, b'local', tagtypes) | ||
Greg Ward
|
r9149 | |||
Augie Fackler
|
r43346 | |||
Angel Ezquerra
|
r21892 | def _readtaghist(ui, repo, lines, fn, recode=None, calcnodelines=False): | ||
Augie Fackler
|
r46554 | """Read tag definitions from a file (or any source of lines). | ||
Gregory Szorc
|
r24445 | |||
Angel Ezquerra
|
r21892 | This function returns two sortdicts with similar information: | ||
Gregory Szorc
|
r24445 | |||
Mads Kiilerich
|
r23139 | - the first dict, bintaghist, contains the tag information as expected by | ||
Angel Ezquerra
|
r21892 | the _readtags function, i.e. a mapping from tag name to (node, hist): | ||
- node is the node id from the last line read for that name, | ||||
- hist is the list of node ids previously associated with it (in file | ||||
Gregory Szorc
|
r24445 | order). All node ids are binary, not hex. | ||
Angel Ezquerra
|
r21892 | - the second dict, hextaglines, is a mapping from tag name to a list of | ||
[hexnode, line number] pairs, ordered from the oldest to the newest node. | ||||
Gregory Szorc
|
r24445 | |||
Angel Ezquerra
|
r21892 | When calcnodelines is False the hextaglines dict is not calculated (an | ||
empty dict is returned). This is done to improve this function's | ||||
performance in cases where the line numbers are not needed. | ||||
Augie Fackler
|
r46554 | """ | ||
Greg Ward
|
r9149 | |||
Angel Ezquerra
|
r21892 | bintaghist = util.sortdict() | ||
hextaglines = util.sortdict() | ||||
Greg Ward
|
r9149 | count = 0 | ||
Matt Mackall
|
r29038 | def dbg(msg): | ||
Augie Fackler
|
r43347 | ui.debug(b"%s, line %d: %s\n" % (fn, count, msg)) | ||
Greg Ward
|
r9149 | |||
Angel Ezquerra
|
r21892 | for nline, line in enumerate(lines): | ||
Greg Ward
|
r9149 | count += 1 | ||
if not line: | ||||
continue | ||||
try: | ||||
Augie Fackler
|
r43347 | (nodehex, name) = line.split(b" ", 1) | ||
Greg Ward
|
r9149 | except ValueError: | ||
Augie Fackler
|
r43347 | dbg(b"cannot parse entry") | ||
Greg Ward
|
r9149 | continue | ||
Greg Ward
|
r9152 | name = name.strip() | ||
if recode: | ||||
name = recode(name) | ||||
Greg Ward
|
r9149 | try: | ||
nodebin = bin(nodehex) | ||||
except TypeError: | ||||
Augie Fackler
|
r43347 | dbg(b"node '%s' is not well formed" % nodehex) | ||
Greg Ward
|
r9149 | continue | ||
# update filetags | ||||
Angel Ezquerra
|
r21892 | if calcnodelines: | ||
# map tag name to a list of line numbers | ||||
if name not in hextaglines: | ||||
hextaglines[name] = [] | ||||
hextaglines[name].append([nodehex, nline]) | ||||
continue | ||||
# map tag name to (node, hist) | ||||
if name not in bintaghist: | ||||
bintaghist[name] = [] | ||||
bintaghist[name].append(nodebin) | ||||
return bintaghist, hextaglines | ||||
Augie Fackler
|
r43346 | |||
Angel Ezquerra
|
r21892 | def _readtags(ui, repo, lines, fn, recode=None, calcnodelines=False): | ||
Augie Fackler
|
r46554 | """Read tag definitions from a file (or any source of lines). | ||
Gregory Szorc
|
r24445 | |||
Returns a mapping from tag name to (node, hist). | ||||
"node" is the node id from the last line read for that name. "hist" | ||||
is the list of node ids previously associated with it (in file order). | ||||
All node ids are binary, not hex. | ||||
Augie Fackler
|
r46554 | """ | ||
Augie Fackler
|
r43346 | filetags, nodelines = _readtaghist( | ||
ui, repo, lines, fn, recode=recode, calcnodelines=calcnodelines | ||||
) | ||||
Gregory Szorc
|
r26945 | # util.sortdict().__setitem__ is much slower at replacing then inserting | ||
# new entries. The difference can matter if there are thousands of tags. | ||||
# Create a new sortdict to avoid the performance penalty. | ||||
newtags = util.sortdict() | ||||
Angel Ezquerra
|
r21892 | for tag, taghist in filetags.items(): | ||
Gregory Szorc
|
r26945 | newtags[tag] = (taghist[-1], taghist[:-1]) | ||
return newtags | ||||
Greg Ward
|
r9149 | |||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r31708 | def _updatetags(filetags, alltags, tagtype=None, tagtypes=None): | ||
"""Incorporate the tag info read from one file into dictionnaries | ||||
The first one, 'alltags', is a "tagmaps" (see 'findglobaltags' for details). | ||||
The second one, 'tagtypes', is optional and will be updated to track the | ||||
"tagtype" of entries in the tagmaps. When set, the 'tagtype' argument also | ||||
needs to be set.""" | ||||
if tagtype is None: | ||||
assert tagtypes is None | ||||
Greg Ward
|
r9149 | |||
Gregory Szorc
|
r43376 | for name, nodehist in pycompat.iteritems(filetags): | ||
Greg Ward
|
r9149 | if name not in alltags: | ||
alltags[name] = nodehist | ||||
Pierre-Yves David
|
r31708 | if tagtype is not None: | ||
tagtypes[name] = tagtype | ||||
Greg Ward
|
r9149 | continue | ||
# we prefer alltags[name] if: | ||||
Mads Kiilerich
|
r17424 | # it supersedes us OR | ||
# mutual supersedes and it has a higher rank | ||||
Greg Ward
|
r9149 | # otherwise we win because we're tip-most | ||
anode, ahist = nodehist | ||||
bnode, bhist = alltags[name] | ||||
Augie Fackler
|
r43346 | if ( | ||
bnode != anode | ||||
and anode in bhist | ||||
and (bnode not in ahist or len(bhist) > len(ahist)) | ||||
): | ||||
Greg Ward
|
r9149 | anode = bnode | ||
Pierre-Yves David
|
r31708 | elif tagtype is not None: | ||
FUJIWARA Katsunori
|
r19108 | tagtypes[name] = tagtype | ||
Greg Ward
|
r9149 | ahist.extend([n for n in bhist if n not in ahist]) | ||
alltags[name] = anode, ahist | ||||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r24737 | def _filename(repo): | ||
"""name of a tagcache file for a given repo or repoview""" | ||||
Augie Fackler
|
r43347 | filename = b'tags2' | ||
Pierre-Yves David
|
r24737 | if repo.filtername: | ||
Augie Fackler
|
r43347 | filename = b'%s-%s' % (filename, repo.filtername) | ||
Pierre-Yves David
|
r24737 | return filename | ||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r24445 | def _readtagcache(ui, repo): | ||
Augie Fackler
|
r46554 | """Read the tag cache. | ||
Greg Ward
|
r9151 | |||
Gregory Szorc
|
r24760 | Returns a tuple (heads, fnodes, validinfo, cachetags, shouldwrite). | ||
Gregory Szorc
|
r24445 | |||
If the cache is completely up-to-date, "cachetags" is a dict of the | ||||
Gregory Szorc
|
r24760 | form returned by _readtags() and "heads", "fnodes", and "validinfo" are | ||
None and "shouldwrite" is False. | ||||
Greg Ward
|
r9151 | |||
Gregory Szorc
|
r24445 | If the cache is not up to date, "cachetags" is None. "heads" is a list | ||
of all heads currently in the repository, ordered from tip to oldest. | ||||
Gregory Szorc
|
r24760 | "validinfo" is a tuple describing cache validation info. This is used | ||
when writing the tags cache. "fnodes" is a mapping from head to .hgtags | ||||
filenode. "shouldwrite" is True. | ||||
Gregory Szorc
|
r24445 | |||
If the cache is not up to date, the caller is responsible for reading tag | ||||
info from each returned head. (See findglobaltags().) | ||||
Augie Fackler
|
r46554 | """ | ||
Greg Ward
|
r9151 | try: | ||
Augie Fackler
|
r43347 | cachefile = repo.cachevfs(_filename(repo), b'r') | ||
Nicolas Dumazet
|
r11066 | # force reading the file for static-http | ||
cachelines = iter(cachefile) | ||||
Greg Ward
|
r9151 | except IOError: | ||
cachefile = None | ||||
Gregory Szorc
|
r24760 | cacherev = None | ||
cachenode = None | ||||
cachehash = None | ||||
Greg Ward
|
r9151 | if cachefile: | ||
Nicolas Dumazet
|
r12758 | try: | ||
timeless
|
r29216 | validline = next(cachelines) | ||
Gregory Szorc
|
r24760 | validline = validline.split() | ||
cacherev = int(validline[0]) | ||||
cachenode = bin(validline[1]) | ||||
if len(validline) > 2: | ||||
cachehash = bin(validline[2]) | ||||
Matt Mackall
|
r14020 | except Exception: | ||
Gregory Szorc
|
r24759 | # corruption of the cache, just recompute it. | ||
pass | ||||
Greg Ward
|
r9151 | |||
tipnode = repo.changelog.tip() | ||||
tiprev = len(repo.changelog) - 1 | ||||
# Case 1 (common): tip is the same, so nothing has changed. | ||||
# (Unchanged tip trivially means no changesets have been added. | ||||
# But, thanks to localrepository.destroyed(), it also means none | ||||
# have been destroyed by strip or rollback.) | ||||
Augie Fackler
|
r43346 | if ( | ||
cacherev == tiprev | ||||
and cachenode == tipnode | ||||
and cachehash == scmutil.filteredhash(repo, tiprev) | ||||
): | ||||
Nicolas Dumazet
|
r11066 | tags = _readtags(ui, repo, cachelines, cachefile.name) | ||
Greg Ward
|
r9152 | cachefile.close() | ||
Gregory Szorc
|
r24760 | return (None, None, None, tags, False) | ||
Greg Ward
|
r9152 | if cachefile: | ||
Augie Fackler
|
r43346 | cachefile.close() # ignore rest of file | ||
Dirkjan Ochtman
|
r9312 | |||
Gregory Szorc
|
r24760 | valid = (tiprev, tipnode, scmutil.filteredhash(repo, tiprev)) | ||
Greg Ward
|
r9151 | repoheads = repo.heads() | ||
# Case 2 (uncommon): empty repo; get out quickly and don't bother | ||||
# writing an empty cache. | ||||
Joerg Sonnenberger
|
r47771 | if repoheads == [repo.nullid]: | ||
Gregory Szorc
|
r24760 | return ([], {}, valid, {}, False) | ||
Greg Ward
|
r9151 | |||
# Case 3 (uncommon): cache file missing or empty. | ||||
# Case 4 (uncommon): tip rev decreased. This should only happen | ||||
# when we're called from localrepository.destroyed(). Refresh the | ||||
# cache so future invocations will not see disappeared heads in the | ||||
# cache. | ||||
# Case 5 (common): tip has changed, so we've added/replaced heads. | ||||
Greg Ward
|
r11352 | # As it happens, the code to handle cases 3, 4, 5 is the same. | ||
Greg Ward
|
r9151 | |||
# N.B. in case 4 (nodes destroyed), "new head" really means "newly | ||||
# exposed". | ||||
Augie Fackler
|
r43347 | if not len(repo.file(b'.hgtags')): | ||
Bryan O'Sullivan
|
r16730 | # No tags have ever been committed, so we can avoid a | ||
# potentially expensive search. | ||||
Gregory Szorc
|
r24761 | return ([], {}, valid, None, True) | ||
Bryan O'Sullivan
|
r16730 | |||
Greg Ward
|
r9151 | # Now we have to lookup the .hgtags filenode for every new head. | ||
# This is the most expensive part of finding tags, so performance | ||||
# depends primarily on the size of newheads. Worst case: no cache | ||||
# file, so newheads == repoheads. | ||||
Martin von Zweigbergk
|
r42425 | # Reversed order helps the cache ('repoheads' is in descending order) | ||
cachefnode = _getfnodes(ui, repo, reversed(repoheads)) | ||||
Pierre-Yves David
|
r31705 | |||
# Caller has to iterate over all heads, but can use the filenodes in | ||||
# cachefnode to get to each .hgtags revision quickly. | ||||
return (repoheads, cachefnode, valid, None, True) | ||||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r31705 | def _getfnodes(ui, repo, nodes): | ||
"""return .hgtags fnodes for a list of changeset nodes | ||||
Return value is a {node: fnode} mapping. There will be no entry for nodes | ||||
without a '.hgtags' file. | ||||
""" | ||||
starttime = util.timer() | ||||
Gregory Szorc
|
r24735 | fnodescache = hgtagsfnodescache(repo.unfiltered()) | ||
Gregory Szorc
|
r24759 | cachefnode = {} | ||
Pulkit Goyal
|
r47402 | validated_fnodes = set() | ||
unknown_entries = set() | ||||
Martin von Zweigbergk
|
r42425 | for node in nodes: | ||
Martin von Zweigbergk
|
r31788 | fnode = fnodescache.getfnode(node) | ||
Pulkit Goyal
|
r47402 | flog = repo.file(b'.hgtags') | ||
Joerg Sonnenberger
|
r47771 | if fnode != repo.nullid: | ||
Pulkit Goyal
|
r47402 | if fnode not in validated_fnodes: | ||
if flog.hasnode(fnode): | ||||
validated_fnodes.add(fnode) | ||||
else: | ||||
unknown_entries.add(node) | ||||
Martin von Zweigbergk
|
r31788 | cachefnode[node] = fnode | ||
Gregory Szorc
|
r24735 | |||
Pulkit Goyal
|
r47402 | if unknown_entries: | ||
fixed_nodemap = fnodescache.refresh_invalid_nodes(unknown_entries) | ||||
for node, fnode in pycompat.iteritems(fixed_nodemap): | ||||
Joerg Sonnenberger
|
r47771 | if fnode != repo.nullid: | ||
Pulkit Goyal
|
r47402 | cachefnode[node] = fnode | ||
Gregory Szorc
|
r24735 | fnodescache.write() | ||
Greg Ward
|
r9151 | |||
Simon Farnsworth
|
r30975 | duration = util.timer() - starttime | ||
Augie Fackler
|
r43346 | ui.log( | ||
Augie Fackler
|
r43347 | b'tagscache', | ||
b'%d/%d cache hits/lookups in %0.4f seconds\n', | ||||
Augie Fackler
|
r43346 | fnodescache.hitcount, | ||
fnodescache.lookupcount, | ||||
duration, | ||||
) | ||||
Pierre-Yves David
|
r31705 | return cachefnode | ||
Greg Ward
|
r9151 | |||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r24760 | def _writetagcache(ui, repo, valid, cachetags): | ||
Gregory Szorc
|
r24763 | filename = _filename(repo) | ||
Greg Ward
|
r9366 | try: | ||
Augie Fackler
|
r43347 | cachefile = repo.cachevfs(filename, b'w', atomictemp=True) | ||
Greg Ward
|
r9366 | except (OSError, IOError): | ||
return | ||||
Greg Ward
|
r9151 | |||
Augie Fackler
|
r43346 | ui.log( | ||
Augie Fackler
|
r43347 | b'tagscache', | ||
b'writing .hg/cache/%s with %d tags\n', | ||||
Augie Fackler
|
r43346 | filename, | ||
len(cachetags), | ||||
) | ||||
Gregory Szorc
|
r21030 | |||
Gregory Szorc
|
r24760 | if valid[2]: | ||
Augie Fackler
|
r43347 | cachefile.write( | ||
b'%d %s %s\n' % (valid[0], hex(valid[1]), hex(valid[2])) | ||||
) | ||||
Gregory Szorc
|
r24760 | else: | ||
Augie Fackler
|
r43347 | cachefile.write(b'%d %s\n' % (valid[0], hex(valid[1]))) | ||
Greg Ward
|
r9151 | |||
Greg Ward
|
r9152 | # Tag names in the cache are in UTF-8 -- which is the whole reason | ||
# we keep them in UTF-8 throughout this module. If we converted | ||||
# them local encoding on input, we would lose info writing them to | ||||
# the cache. | ||||
Gregory Szorc
|
r43376 | for (name, (node, hist)) in sorted(pycompat.iteritems(cachetags)): | ||
FUJIWARA Katsunori
|
r19646 | for n in hist: | ||
Augie Fackler
|
r43347 | cachefile.write(b"%s %s\n" % (hex(n), name)) | ||
cachefile.write(b"%s %s\n" % (hex(node), name)) | ||||
Greg Ward
|
r9152 | |||
Steve Borho
|
r14662 | try: | ||
Greg Ward
|
r15057 | cachefile.close() | ||
Steve Borho
|
r14662 | except (OSError, IOError): | ||
pass | ||||
Gregory Szorc
|
r24735 | |||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r31669 | def tag(repo, names, node, message, local, user, date, editor=False): | ||
Augie Fackler
|
r46554 | """tag a revision with one or more symbolic names. | ||
Pierre-Yves David
|
r31669 | |||
names is a list of strings or, when adding a single tag, names may be a | ||||
string. | ||||
if local is True, the tags are stored in a per-repository file. | ||||
otherwise, they are stored in the .hgtags file, and a new | ||||
changeset is committed with the change. | ||||
keyword arguments: | ||||
local: whether to store tags in non-version-controlled file | ||||
(default False) | ||||
message: commit message to use if committing | ||||
user: name of user to use if committing | ||||
Augie Fackler
|
r46554 | date: date tuple to use if committing""" | ||
Pierre-Yves David
|
r31669 | |||
if not local: | ||||
Augie Fackler
|
r43347 | m = matchmod.exact([b'.hgtags']) | ||
Augie Fackler
|
r44051 | st = repo.status(match=m, unknown=True, ignored=True) | ||
if any( | ||||
( | ||||
st.modified, | ||||
st.added, | ||||
st.removed, | ||||
st.deleted, | ||||
st.unknown, | ||||
st.ignored, | ||||
) | ||||
): | ||||
Augie Fackler
|
r43346 | raise error.Abort( | ||
Augie Fackler
|
r43347 | _(b'working copy of .hgtags is changed'), | ||
hint=_(b'please commit .hgtags manually'), | ||||
Augie Fackler
|
r43346 | ) | ||
Pierre-Yves David
|
r31669 | |||
r33253 | with repo.wlock(): | |||
Augie Fackler
|
r43346 | repo.tags() # instantiate the cache | ||
_tag(repo, names, node, message, local, user, date, editor=editor) | ||||
Pierre-Yves David
|
r31669 | |||
Augie Fackler
|
r43346 | def _tag( | ||
repo, names, node, message, local, user, date, extra=None, editor=False | ||||
): | ||||
Pulkit Goyal
|
r42007 | if isinstance(names, bytes): | ||
Pierre-Yves David
|
r31668 | names = (names,) | ||
branches = repo.branchmap() | ||||
for name in names: | ||||
Augie Fackler
|
r43347 | repo.hook(b'pretag', throw=True, node=hex(node), tag=name, local=local) | ||
Pierre-Yves David
|
r31668 | if name in branches: | ||
Augie Fackler
|
r43346 | repo.ui.warn( | ||
Martin von Zweigbergk
|
r43387 | _(b"warning: tag %s conflicts with existing branch name\n") | ||
Augie Fackler
|
r43346 | % name | ||
) | ||||
Pierre-Yves David
|
r31668 | |||
def writetags(fp, names, munge, prevtags): | ||||
Augie Fackler
|
r42767 | fp.seek(0, io.SEEK_END) | ||
Augie Fackler
|
r43347 | if prevtags and not prevtags.endswith(b'\n'): | ||
fp.write(b'\n') | ||||
Pierre-Yves David
|
r31668 | for name in names: | ||
if munge: | ||||
m = munge(name) | ||||
else: | ||||
m = name | ||||
Augie Fackler
|
r43346 | if repo._tagscache.tagtypes and name in repo._tagscache.tagtypes: | ||
Joerg Sonnenberger
|
r47771 | old = repo.tags().get(name, repo.nullid) | ||
Augie Fackler
|
r43347 | fp.write(b'%s %s\n' % (hex(old), m)) | ||
fp.write(b'%s %s\n' % (hex(node), m)) | ||||
Pierre-Yves David
|
r31668 | fp.close() | ||
Augie Fackler
|
r43347 | prevtags = b'' | ||
Pierre-Yves David
|
r31668 | if local: | ||
try: | ||||
Augie Fackler
|
r43347 | fp = repo.vfs(b'localtags', b'r+') | ||
Pierre-Yves David
|
r31668 | except IOError: | ||
Augie Fackler
|
r43347 | fp = repo.vfs(b'localtags', b'a') | ||
Pierre-Yves David
|
r31668 | else: | ||
prevtags = fp.read() | ||||
# local tags are stored in the current charset | ||||
writetags(fp, names, None, prevtags) | ||||
for name in names: | ||||
Augie Fackler
|
r43347 | repo.hook(b'tag', node=hex(node), tag=name, local=local) | ||
Pierre-Yves David
|
r31668 | return | ||
try: | ||||
Augie Fackler
|
r43347 | fp = repo.wvfs(b'.hgtags', b'rb+') | ||
Pierre-Yves David
|
r31668 | except IOError as e: | ||
if e.errno != errno.ENOENT: | ||||
raise | ||||
Augie Fackler
|
r43347 | fp = repo.wvfs(b'.hgtags', b'ab') | ||
Pierre-Yves David
|
r31668 | else: | ||
prevtags = fp.read() | ||||
# committed tags are stored in UTF-8 | ||||
writetags(fp, names, encoding.fromlocal, prevtags) | ||||
fp.close() | ||||
repo.invalidatecaches() | ||||
Augie Fackler
|
r43347 | if b'.hgtags' not in repo.dirstate: | ||
repo[None].add([b'.hgtags']) | ||||
Pierre-Yves David
|
r31668 | |||
Augie Fackler
|
r43347 | m = matchmod.exact([b'.hgtags']) | ||
Augie Fackler
|
r43346 | tagnode = repo.commit( | ||
message, user, date, extra=extra, match=m, editor=editor | ||||
) | ||||
Pierre-Yves David
|
r31668 | |||
for name in names: | ||||
Augie Fackler
|
r43347 | repo.hook(b'tag', node=hex(node), tag=name, local=local) | ||
Pierre-Yves David
|
r31668 | |||
return tagnode | ||||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r43347 | _fnodescachefile = b'hgtagsfnodes1' | ||
Augie Fackler
|
r43346 | _fnodesrecsize = 4 + 20 # changeset fragment + filenode | ||
Augie Fackler
|
r43347 | _fnodesmissingrec = b'\xff' * 24 | ||
Gregory Szorc
|
r24735 | |||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r24735 | class hgtagsfnodescache(object): | ||
"""Persistent cache mapping revisions to .hgtags filenodes. | ||||
The cache is an array of records. Each item in the array corresponds to | ||||
a changelog revision. Values in the array contain the first 4 bytes of | ||||
the node hash and the 20 bytes .hgtags filenode for that revision. | ||||
The first 4 bytes are present as a form of verification. Repository | ||||
stripping and rewriting may change the node at a numeric revision in the | ||||
changelog. The changeset fragment serves as a verifier to detect | ||||
rewriting. This logic is shared with the rev branch cache (see | ||||
branchmap.py). | ||||
The instance holds in memory the full cache content but entries are | ||||
only parsed on read. | ||||
Instances behave like lists. ``c[i]`` works where i is a rev or | ||||
changeset node. Missing indexes are populated automatically on access. | ||||
""" | ||||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r24735 | def __init__(self, repo): | ||
assert repo.filtername is None | ||||
self._repo = repo | ||||
# Only for reporting purposes. | ||||
self.lookupcount = 0 | ||||
self.hitcount = 0 | ||||
Matt Mackall
|
r29039 | try: | ||
Boris Feld
|
r33537 | data = repo.cachevfs.read(_fnodescachefile) | ||
Matt Mackall
|
r29039 | except (OSError, IOError): | ||
Augie Fackler
|
r43347 | data = b"" | ||
Augie Fackler
|
r31346 | self._raw = bytearray(data) | ||
Gregory Szorc
|
r24735 | |||
# The end state of self._raw is an array that is of the exact length | ||||
# required to hold a record for every revision in the repository. | ||||
# We truncate or extend the array as necessary. self._dirtyoffset is | ||||
# defined to be the start offset at which we need to write the output | ||||
# file. This offset is also adjusted when new entries are calculated | ||||
# for array members. | ||||
cllen = len(repo.changelog) | ||||
wantedlen = cllen * _fnodesrecsize | ||||
rawlen = len(self._raw) | ||||
self._dirtyoffset = None | ||||
Augie Fackler
|
r44787 | rawlentokeep = min( | ||
Augie Fackler
|
r44825 | wantedlen, (rawlen // _fnodesrecsize) * _fnodesrecsize | ||
Augie Fackler
|
r44787 | ) | ||
Valentin Gatien-Baron
|
r44774 | if rawlen > rawlentokeep: | ||
Gregory Szorc
|
r24735 | # There's no easy way to truncate array instances. This seems | ||
# slightly less evil than copying a potentially large array slice. | ||||
Valentin Gatien-Baron
|
r44774 | for i in range(rawlen - rawlentokeep): | ||
Gregory Szorc
|
r24735 | self._raw.pop() | ||
Valentin Gatien-Baron
|
r44774 | rawlen = len(self._raw) | ||
self._dirtyoffset = rawlen | ||||
if rawlen < wantedlen: | ||||
if self._dirtyoffset is None: | ||||
self._dirtyoffset = rawlen | ||||
Matt Harbison
|
r47245 | # TODO: zero fill entire record, because it's invalid not missing? | ||
Valentin Gatien-Baron
|
r44774 | self._raw.extend(b'\xff' * (wantedlen - rawlen)) | ||
Gregory Szorc
|
r24735 | |||
Gregory Szorc
|
r25380 | def getfnode(self, node, computemissing=True): | ||
Gregory Szorc
|
r24735 | """Obtain the filenode of the .hgtags file at a specified revision. | ||
If the value is in the cache, the entry will be validated and returned. | ||||
Gregory Szorc
|
r25380 | Otherwise, the filenode will be computed and returned unless | ||
Matt Harbison
|
r47245 | "computemissing" is False. In that case, None will be returned if | ||
the entry is missing or False if the entry is invalid without | ||||
Gregory Szorc
|
r25380 | any potentially expensive computation being performed. | ||
Gregory Szorc
|
r24735 | |||
If an .hgtags does not exist at the specified revision, nullid is | ||||
returned. | ||||
""" | ||||
Joerg Sonnenberger
|
r47771 | if node == self._repo.nullid: | ||
return node | ||||
r42422 | ||||
Gregory Szorc
|
r24735 | ctx = self._repo[node] | ||
rev = ctx.rev() | ||||
self.lookupcount += 1 | ||||
offset = rev * _fnodesrecsize | ||||
Augie Fackler
|
r43347 | record = b'%s' % self._raw[offset : offset + _fnodesrecsize] | ||
Gregory Szorc
|
r24735 | properprefix = node[0:4] | ||
# Validate and return existing entry. | ||||
Pulkit Goyal
|
r46372 | if record != _fnodesmissingrec and len(record) == _fnodesrecsize: | ||
Gregory Szorc
|
r24735 | fileprefix = record[0:4] | ||
if fileprefix == properprefix: | ||||
self.hitcount += 1 | ||||
return record[4:] | ||||
# Fall through. | ||||
Gregory Szorc
|
r25380 | # If we get here, the entry is either missing or invalid. | ||
if not computemissing: | ||||
Matt Harbison
|
r47245 | if record != _fnodesmissingrec: | ||
return False | ||||
Gregory Szorc
|
r25380 | return None | ||
Pulkit Goyal
|
r47398 | fnode = self._computefnode(node) | ||
self._writeentry(offset, properprefix, fnode) | ||||
return fnode | ||||
def _computefnode(self, node): | ||||
"""Finds the tag filenode for a node which is missing or invalid | ||||
in cache""" | ||||
ctx = self._repo[node] | ||||
rev = ctx.rev() | ||||
r42423 | fnode = None | |||
cl = self._repo.changelog | ||||
p1rev, p2rev = cl._uncheckedparentrevs(rev) | ||||
p1node = cl.node(p1rev) | ||||
p1fnode = self.getfnode(p1node, computemissing=False) | ||||
if p2rev != nullrev: | ||||
# There is some no-merge changeset where p1 is null and p2 is set | ||||
# Processing them as merge is just slower, but still gives a good | ||||
# result. | ||||
p2node = cl.node(p1rev) | ||||
p2fnode = self.getfnode(p2node, computemissing=False) | ||||
if p1fnode != p2fnode: | ||||
# we cannot rely on readfast because we don't know against what | ||||
# parent the readfast delta is computed | ||||
p1fnode = None | ||||
Matt Harbison
|
r47245 | if p1fnode: | ||
r42423 | mctx = ctx.manifestctx() | |||
Augie Fackler
|
r43347 | fnode = mctx.readfast().get(b'.hgtags') | ||
r42423 | if fnode is None: | |||
fnode = p1fnode | ||||
if fnode is None: | ||||
# Populate missing entry. | ||||
try: | ||||
Augie Fackler
|
r43347 | fnode = ctx.filenode(b'.hgtags') | ||
r42423 | except error.LookupError: | |||
# No .hgtags file on this revision. | ||||
Joerg Sonnenberger
|
r47771 | fnode = self._repo.nullid | ||
Gregory Szorc
|
r25381 | return fnode | ||
def setfnode(self, node, fnode): | ||||
"""Set the .hgtags filenode for a given changeset.""" | ||||
assert len(fnode) == 20 | ||||
ctx = self._repo[node] | ||||
# Do a lookup first to avoid writing if nothing has changed. | ||||
if self.getfnode(ctx.node(), computemissing=False) == fnode: | ||||
return | ||||
self._writeentry(ctx.rev() * _fnodesrecsize, node[0:4], fnode) | ||||
Pulkit Goyal
|
r47402 | def refresh_invalid_nodes(self, nodes): | ||
"""recomputes file nodes for a given set of nodes which has unknown | ||||
filenodes for them in the cache | ||||
Also updates the in-memory cache with the correct filenode. | ||||
Caller needs to take care about calling `.write()` so that updates are | ||||
persisted. | ||||
Returns a map {node: recomputed fnode} | ||||
""" | ||||
fixed_nodemap = {} | ||||
for node in nodes: | ||||
fnode = self._computefnode(node) | ||||
fixed_nodemap[node] = fnode | ||||
self.setfnode(node, fnode) | ||||
return fixed_nodemap | ||||
Gregory Szorc
|
r25381 | def _writeentry(self, offset, prefix, fnode): | ||
Gregory Szorc
|
r24735 | # Slices on array instances only accept other array. | ||
Augie Fackler
|
r31346 | entry = bytearray(prefix + fnode) | ||
Augie Fackler
|
r43346 | self._raw[offset : offset + _fnodesrecsize] = entry | ||
Gregory Szorc
|
r24735 | # self._dirtyoffset could be None. | ||
Augie Fackler
|
r36291 | self._dirtyoffset = min(self._dirtyoffset or 0, offset or 0) | ||
Gregory Szorc
|
r24735 | |||
def write(self): | ||||
"""Perform all necessary writes to cache file. | ||||
This may no-op if no writes are needed or if a write lock could | ||||
not be obtained. | ||||
""" | ||||
if self._dirtyoffset is None: | ||||
return | ||||
Augie Fackler
|
r43346 | data = self._raw[self._dirtyoffset :] | ||
Gregory Szorc
|
r24735 | if not data: | ||
return | ||||
repo = self._repo | ||||
try: | ||||
Pulkit Goyal
|
r46005 | lock = repo.lock(wait=False) | ||
Yuya Nishihara
|
r24806 | except error.LockError: | ||
Augie Fackler
|
r43346 | repo.ui.log( | ||
Augie Fackler
|
r43347 | b'tagscache', | ||
b'not writing .hg/cache/%s because ' | ||||
b'lock cannot be acquired\n' % _fnodescachefile, | ||||
Augie Fackler
|
r43346 | ) | ||
Gregory Szorc
|
r24735 | return | ||
try: | ||||
Augie Fackler
|
r43347 | f = repo.cachevfs.open(_fnodescachefile, b'ab') | ||
Gregory Szorc
|
r24735 | try: | ||
Matt Mackall
|
r25087 | # if the file has been truncated | ||
actualoffset = f.tell() | ||||
if actualoffset < self._dirtyoffset: | ||||
self._dirtyoffset = actualoffset | ||||
Augie Fackler
|
r43346 | data = self._raw[self._dirtyoffset :] | ||
Matt Mackall
|
r25087 | f.seek(self._dirtyoffset) | ||
f.truncate() | ||||
Augie Fackler
|
r43346 | repo.ui.log( | ||
Augie Fackler
|
r43347 | b'tagscache', | ||
b'writing %d bytes to cache/%s\n' | ||||
Augie Fackler
|
r43346 | % (len(data), _fnodescachefile), | ||
) | ||||
Matt Mackall
|
r25087 | f.write(data) | ||
self._dirtyoffset = None | ||||
finally: | ||||
f.close() | ||||
Gregory Szorc
|
r25660 | except (IOError, OSError) as inst: | ||
Augie Fackler
|
r43346 | repo.ui.log( | ||
Augie Fackler
|
r43347 | b'tagscache', | ||
b"couldn't write cache/%s: %s\n" | ||||
Augie Fackler
|
r43346 | % (_fnodescachefile, stringutil.forcebytestr(inst)), | ||
) | ||||
Gregory Szorc
|
r24735 | finally: | ||
lock.release() | ||||