mercurial/tags.py
author Gregory Szorc <gregory.szorc@gmail.com>
Tue, 24 Mar 2015 20:28:39 -0700
changeset 24445 c71edbafe603
parent 24143 7b09dbbbd502
child 24532 f5de208a635c
permissions -rw-r--r--
tags: improve documentation The documentation for tags.py was making comprehension difficult. This patch rewrites most of the comments in the file to make it easier for mere mortals to understand what's going on.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     1
# tags.py - read tag info from local repository
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     2
#
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     3
# Copyright 2009 Matt Mackall <mpm@selenic.com>
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     4
# Copyright 2009 Greg Ward <greg@gerg.ca>
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     5
#
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     6
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9678
diff changeset
     7
# GNU General Public License version 2 or any later version.
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
     8
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
     9
# Currently this module only deals with reading and caching tags.
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
    10
# Eventually, it could take care of updating (adding/removing/moving)
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
    11
# tags too.
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    12
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
    13
from node import nullid, bin, hex, short
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    14
from i18n import _
21814
5125856a28cf tags: read tag info into a sorted dict (rather than into a regular dict)
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21030
diff changeset
    15
import util
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    16
import encoding
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    17
import error
14038
0e6f622f31ca tags: loosen IOError filtering when reading localtags
Idan Kamara <idankk86@gmail.com>
parents: 14020
diff changeset
    18
import errno
21030
9ea132aee96c tags: log events related to tags cache
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19646
diff changeset
    19
import time
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
    20
24445
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    21
# The tags cache stores information about heads and the history of tags.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    22
#
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    23
# The cache file consists of two parts. The first part maps head nodes
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    24
# to .hgtags filenodes. The second part is a history of tags. The two
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    25
# parts are separated by an empty line.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    26
#
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    27
# The first part consists of lines of the form:
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    28
#
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    29
#   <headrev> <headnode> [<hgtagsnode>]
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    30
#
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    31
# <headrev> is an integer revision and <headnode> is a 40 character hex
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    32
# node for that changeset. These redundantly identify a repository
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    33
# head from the time the cache was written.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    34
#
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    35
# <tagnode> is the filenode of .hgtags on that head. Heads with no .hgtags
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    36
# file will have no <hgtagsnode> (just 2 values per line).
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    37
#
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    38
# The filenode cache is ordered from tip to oldest (which is part of why
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    39
# <headrev> is there: a quick check of the tip from when the cache was
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    40
# written against the current tip is all that is needed to check whether
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    41
# the cache is up to date).
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    42
#
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    43
# The purpose of the filenode cache is to avoid the most expensive part
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    44
# of finding global tags, which is looking up the .hgtags filenode in the
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    45
# manifest for each head. This can take over a minute on repositories
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    46
# that have large manifests and many heads.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    47
#
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    48
# The second part of the tags cache consists of lines of the form:
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    49
#
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    50
#   <node> <tag>
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    51
#
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    52
# (This format is identical to that of .hgtags files.)
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    53
#
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    54
# <tag> is the tag name and <node> is the 40 character hex changeset
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    55
# the tag is associated with.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    56
#
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    57
# Tags are written sorted by tag name.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    58
#
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    59
# Tags associated with multiple changesets have an entry for each changeset.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    60
# The most recent changeset (in terms of revlog ordering for the head
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    61
# setting it) for each tag is last.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    62
11351
1cdc8b5e5729 tags: remove the old non-caching implementation of findglobaltags().
Greg Ward <greg-hg@gerg.ca>
parents: 11078
diff changeset
    63
def findglobaltags(ui, repo, alltags, tagtypes):
24445
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    64
    '''Find global tags in a repo.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    65
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    66
    "alltags" maps tag name to (node, hist) 2-tuples.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    67
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    68
    "tagtypes" maps tag name to tag type. Global tags always have the
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    69
    "global" tag type.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    70
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    71
    The "alltags" and "tagtypes" dicts are updated in place. Empty dicts
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    72
    should be passed in.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    73
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    74
    The tags cache is read and updated as a side-effect of calling.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    75
    '''
9152
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
    76
    # This is so we can be lazy and assume alltags contains only global
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
    77
    # tags when we pass it to _writetagcache().
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
    78
    assert len(alltags) == len(tagtypes) == 0, \
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
    79
           "findglobaltags() should be called first"
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
    80
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
    81
    (heads, tagfnode, cachetags, shouldwrite) = _readtagcache(ui, repo)
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
    82
    if cachetags is not None:
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
    83
        assert not shouldwrite
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
    84
        # XXX is this really 100% correct?  are there oddball special
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
    85
        # cases where a global tag should outrank a local tag but won't,
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
    86
        # because cachetags does not contain rank info?
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
    87
        _updatetags(cachetags, 'global', alltags, tagtypes)
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
    88
        return
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
    89
24445
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    90
    seen = set()  # set of fnode
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
    91
    fctx = None
24445
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
    92
    for head in reversed(heads):  # oldest to newest
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
    93
        assert head in repo.changelog.nodemap, \
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
    94
               "tag cache returned bogus head %s" % short(head)
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
    95
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
    96
        fnode = tagfnode.get(head)
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
    97
        if fnode and fnode not in seen:
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
    98
            seen.add(fnode)
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
    99
            if not fctx:
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   100
                fctx = repo.filectx('.hgtags', fileid=fnode)
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   101
            else:
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   102
                fctx = fctx.filectx(fnode)
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   103
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   104
            filetags = _readtags(ui, repo, fctx.data().splitlines(), fctx)
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   105
            _updatetags(filetags, 'global', alltags, tagtypes)
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   106
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   107
    # and update the cache (if necessary)
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   108
    if shouldwrite:
9152
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   109
        _writetagcache(ui, repo, heads, tagfnode, alltags)
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   110
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   111
def readlocaltags(ui, repo, alltags, tagtypes):
24445
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   112
    '''Read local tags in repo. Update alltags and tagtypes.'''
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   113
    try:
23877
7cc77030c557 localrepo: remove all external users of localrepo.opener
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 23139
diff changeset
   114
        data = repo.vfs.read("localtags")
14038
0e6f622f31ca tags: loosen IOError filtering when reading localtags
Idan Kamara <idankk86@gmail.com>
parents: 14020
diff changeset
   115
    except IOError, inst:
0e6f622f31ca tags: loosen IOError filtering when reading localtags
Idan Kamara <idankk86@gmail.com>
parents: 14020
diff changeset
   116
        if inst.errno != errno.ENOENT:
0e6f622f31ca tags: loosen IOError filtering when reading localtags
Idan Kamara <idankk86@gmail.com>
parents: 14020
diff changeset
   117
            raise
0e6f622f31ca tags: loosen IOError filtering when reading localtags
Idan Kamara <idankk86@gmail.com>
parents: 14020
diff changeset
   118
        return
0e6f622f31ca tags: loosen IOError filtering when reading localtags
Idan Kamara <idankk86@gmail.com>
parents: 14020
diff changeset
   119
0e6f622f31ca tags: loosen IOError filtering when reading localtags
Idan Kamara <idankk86@gmail.com>
parents: 14020
diff changeset
   120
    # localtags is in the local encoding; re-encode to UTF-8 on
0e6f622f31ca tags: loosen IOError filtering when reading localtags
Idan Kamara <idankk86@gmail.com>
parents: 14020
diff changeset
   121
    # input for consistency with the rest of this module.
0e6f622f31ca tags: loosen IOError filtering when reading localtags
Idan Kamara <idankk86@gmail.com>
parents: 14020
diff changeset
   122
    filetags = _readtags(
0e6f622f31ca tags: loosen IOError filtering when reading localtags
Idan Kamara <idankk86@gmail.com>
parents: 14020
diff changeset
   123
        ui, repo, data.splitlines(), "localtags",
0e6f622f31ca tags: loosen IOError filtering when reading localtags
Idan Kamara <idankk86@gmail.com>
parents: 14020
diff changeset
   124
        recode=encoding.fromlocal)
21823
925d1bb9a971 repoview: do not crash when localtags refers to non existing revisions
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21030
diff changeset
   125
925d1bb9a971 repoview: do not crash when localtags refers to non existing revisions
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21030
diff changeset
   126
    # remove tags pointing to invalid nodes
925d1bb9a971 repoview: do not crash when localtags refers to non existing revisions
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21030
diff changeset
   127
    cl = repo.changelog
925d1bb9a971 repoview: do not crash when localtags refers to non existing revisions
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21030
diff changeset
   128
    for t in filetags.keys():
925d1bb9a971 repoview: do not crash when localtags refers to non existing revisions
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21030
diff changeset
   129
        try:
925d1bb9a971 repoview: do not crash when localtags refers to non existing revisions
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21030
diff changeset
   130
            cl.rev(filetags[t][0])
925d1bb9a971 repoview: do not crash when localtags refers to non existing revisions
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21030
diff changeset
   131
        except (LookupError, ValueError):
925d1bb9a971 repoview: do not crash when localtags refers to non existing revisions
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21030
diff changeset
   132
            del filetags[t]
925d1bb9a971 repoview: do not crash when localtags refers to non existing revisions
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21030
diff changeset
   133
14038
0e6f622f31ca tags: loosen IOError filtering when reading localtags
Idan Kamara <idankk86@gmail.com>
parents: 14020
diff changeset
   134
    _updatetags(filetags, "local", alltags, tagtypes)
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   135
21892
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   136
def _readtaghist(ui, repo, lines, fn, recode=None, calcnodelines=False):
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   137
    '''Read tag definitions from a file (or any source of lines).
24445
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   138
21892
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   139
    This function returns two sortdicts with similar information:
24445
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   140
23139
e53f6b72a0e4 spelling: fixes from proofreading of spell checker issues
Mads Kiilerich <madski@unity3d.com>
parents: 21892
diff changeset
   141
    - the first dict, bintaghist, contains the tag information as expected by
21892
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   142
      the _readtags function, i.e. a mapping from tag name to (node, hist):
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   143
        - node is the node id from the last line read for that name,
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   144
        - hist is the list of node ids previously associated with it (in file
24445
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   145
          order). All node ids are binary, not hex.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   146
21892
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   147
    - the second dict, hextaglines, is a mapping from tag name to a list of
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   148
      [hexnode, line number] pairs, ordered from the oldest to the newest node.
24445
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   149
21892
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   150
    When calcnodelines is False the hextaglines dict is not calculated (an
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   151
    empty dict is returned). This is done to improve this function's
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   152
    performance in cases where the line numbers are not needed.
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   153
    '''
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   154
21892
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   155
    bintaghist = util.sortdict()
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   156
    hextaglines = util.sortdict()
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   157
    count = 0
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   158
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   159
    def warn(msg):
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   160
        ui.warn(_("%s, line %s: %s\n") % (fn, count, msg))
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   161
21892
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   162
    for nline, line in enumerate(lines):
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   163
        count += 1
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   164
        if not line:
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   165
            continue
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   166
        try:
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   167
            (nodehex, name) = line.split(" ", 1)
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   168
        except ValueError:
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   169
            warn(_("cannot parse entry"))
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   170
            continue
9152
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   171
        name = name.strip()
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   172
        if recode:
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   173
            name = recode(name)
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   174
        try:
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   175
            nodebin = bin(nodehex)
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   176
        except TypeError:
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   177
            warn(_("node '%s' is not well formed") % nodehex)
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   178
            continue
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   179
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   180
        # update filetags
21892
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   181
        if calcnodelines:
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   182
            # map tag name to a list of line numbers
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   183
            if name not in hextaglines:
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   184
                hextaglines[name] = []
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   185
            hextaglines[name].append([nodehex, nline])
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   186
            continue
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   187
        # map tag name to (node, hist)
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   188
        if name not in bintaghist:
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   189
            bintaghist[name] = []
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   190
        bintaghist[name].append(nodebin)
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   191
    return bintaghist, hextaglines
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   192
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   193
def _readtags(ui, repo, lines, fn, recode=None, calcnodelines=False):
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   194
    '''Read tag definitions from a file (or any source of lines).
24445
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   195
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   196
    Returns a mapping from tag name to (node, hist).
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   197
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   198
    "node" is the node id from the last line read for that name. "hist"
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   199
    is the list of node ids previously associated with it (in file order).
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   200
    All node ids are binary, not hex.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   201
    '''
21892
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   202
    filetags, nodelines = _readtaghist(ui, repo, lines, fn, recode=recode,
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   203
                                       calcnodelines=calcnodelines)
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   204
    for tag, taghist in filetags.items():
89cdebc31cda tags: introduce _readtaghist function
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 21832
diff changeset
   205
        filetags[tag] = (taghist[-1], taghist[:-1])
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   206
    return filetags
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   207
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   208
def _updatetags(filetags, tagtype, alltags, tagtypes):
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   209
    '''Incorporate the tag info read from one file into the two
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   210
    dictionaries, alltags and tagtypes, that contain all tag
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   211
    info (global across all heads plus local).'''
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   212
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   213
    for name, nodehist in filetags.iteritems():
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   214
        if name not in alltags:
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   215
            alltags[name] = nodehist
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   216
            tagtypes[name] = tagtype
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   217
            continue
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   218
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   219
        # we prefer alltags[name] if:
17424
e7cfe3587ea4 fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 17256
diff changeset
   220
        #  it supersedes us OR
e7cfe3587ea4 fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 17256
diff changeset
   221
        #  mutual supersedes and it has a higher rank
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   222
        # otherwise we win because we're tip-most
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   223
        anode, ahist = nodehist
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   224
        bnode, bhist = alltags[name]
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   225
        if (bnode != anode and anode in bhist and
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   226
            (bnode not in ahist or len(bhist) > len(ahist))):
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   227
            anode = bnode
19108
cb95716da5fe tags: update tag type only if tag node is updated (issue3911)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17424
diff changeset
   228
        else:
cb95716da5fe tags: update tag type only if tag node is updated (issue3911)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 17424
diff changeset
   229
            tagtypes[name] = tagtype
9149
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   230
        ahist.extend([n for n in bhist if n not in ahist])
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   231
        alltags[name] = anode, ahist
abb7d4d43a5f Factor tags module out of localrepo (issue548).
Greg Ward <greg-hg@gerg.ca>
parents:
diff changeset
   232
24445
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   233
def _readtagcache(ui, repo):
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   234
    '''Read the tag cache.
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   235
24445
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   236
    Returns a tuple (heads, fnodes, cachetags, shouldwrite).
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   237
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   238
    If the cache is completely up-to-date, "cachetags" is a dict of the
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   239
    form returned by _readtags() and "heads" and "fnodes" are None and
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   240
    "shouldwrite" is False.
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   241
24445
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   242
    If the cache is not up to date, "cachetags" is None. "heads" is a list
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   243
    of all heads currently in the repository, ordered from tip to oldest.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   244
    "fnodes" is a mapping from head to .hgtags filenode. "shouldwrite" is
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   245
    True.
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   246
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   247
    If the cache is not up to date, the caller is responsible for reading tag
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   248
    info from each returned head. (See findglobaltags().)
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   249
    '''
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   250
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   251
    try:
23877
7cc77030c557 localrepo: remove all external users of localrepo.opener
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 23139
diff changeset
   252
        cachefile = repo.vfs('cache/tags', 'r')
11066
26abd91d9e84 static-http: mimic more closely localrepo (issue2164: allow clone -r )
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 10263
diff changeset
   253
        # force reading the file for static-http
26abd91d9e84 static-http: mimic more closely localrepo (issue2164: allow clone -r )
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 10263
diff changeset
   254
        cachelines = iter(cachefile)
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   255
    except IOError:
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   256
        cachefile = None
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   257
24445
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   258
    cacherevs = []  # list of headrev
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   259
    cacheheads = [] # list of headnode
c71edbafe603 tags: improve documentation
Gregory Szorc <gregory.szorc@gmail.com>
parents: 24143
diff changeset
   260
    cachefnode = {} # map headnode to filenode
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   261
    if cachefile:
12758
2d754eae430c tags: do not fail if tags.cache is corrupted (issue2444)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11352
diff changeset
   262
        try:
2d754eae430c tags: do not fail if tags.cache is corrupted (issue2444)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11352
diff changeset
   263
            for line in cachelines:
2d754eae430c tags: do not fail if tags.cache is corrupted (issue2444)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11352
diff changeset
   264
                if line == "\n":
2d754eae430c tags: do not fail if tags.cache is corrupted (issue2444)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11352
diff changeset
   265
                    break
16589
fe9a53726484 tags: line.rstrip().split() can be replaced with line.split()
Martin Geisler <mg@aragost.com>
parents: 15057
diff changeset
   266
                line = line.split()
12758
2d754eae430c tags: do not fail if tags.cache is corrupted (issue2444)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11352
diff changeset
   267
                cacherevs.append(int(line[0]))
2d754eae430c tags: do not fail if tags.cache is corrupted (issue2444)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11352
diff changeset
   268
                headnode = bin(line[1])
2d754eae430c tags: do not fail if tags.cache is corrupted (issue2444)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11352
diff changeset
   269
                cacheheads.append(headnode)
2d754eae430c tags: do not fail if tags.cache is corrupted (issue2444)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11352
diff changeset
   270
                if len(line) == 3:
2d754eae430c tags: do not fail if tags.cache is corrupted (issue2444)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11352
diff changeset
   271
                    fnode = bin(line[2])
2d754eae430c tags: do not fail if tags.cache is corrupted (issue2444)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11352
diff changeset
   272
                    cachefnode[headnode] = fnode
14020
98f79a5c3086 tags: catch more corruption during cache parsing (issue2779)
Matt Mackall <mpm@selenic.com>
parents: 13341
diff changeset
   273
        except Exception:
13272
5ccdca7df211 move tags.cache and branchheads.cache to a collected cache folder .hg/cache/
jfh <jason@jasonfharris.com>
parents: 13257
diff changeset
   274
            # corruption of the tags cache, just recompute it
5ccdca7df211 move tags.cache and branchheads.cache to a collected cache folder .hg/cache/
jfh <jason@jasonfharris.com>
parents: 13257
diff changeset
   275
            ui.warn(_('.hg/cache/tags is corrupt, rebuilding it\n'))
12758
2d754eae430c tags: do not fail if tags.cache is corrupted (issue2444)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11352
diff changeset
   276
            cacheheads = []
2d754eae430c tags: do not fail if tags.cache is corrupted (issue2444)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11352
diff changeset
   277
            cacherevs = []
2d754eae430c tags: do not fail if tags.cache is corrupted (issue2444)
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 11352
diff changeset
   278
            cachefnode = {}
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   279
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   280
    tipnode = repo.changelog.tip()
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   281
    tiprev = len(repo.changelog) - 1
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   282
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   283
    # Case 1 (common): tip is the same, so nothing has changed.
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   284
    # (Unchanged tip trivially means no changesets have been added.
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   285
    # But, thanks to localrepository.destroyed(), it also means none
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   286
    # have been destroyed by strip or rollback.)
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   287
    if cacheheads and cacheheads[0] == tipnode and cacherevs[0] == tiprev:
11066
26abd91d9e84 static-http: mimic more closely localrepo (issue2164: allow clone -r )
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 10263
diff changeset
   288
        tags = _readtags(ui, repo, cachelines, cachefile.name)
9152
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   289
        cachefile.close()
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   290
        return (None, None, tags, False)
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   291
    if cachefile:
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   292
        cachefile.close()               # ignore rest of file
9312
c5f0825c1dbb kill trailing whitespace
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 9152
diff changeset
   293
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   294
    repoheads = repo.heads()
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   295
    # Case 2 (uncommon): empty repo; get out quickly and don't bother
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   296
    # writing an empty cache.
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   297
    if repoheads == [nullid]:
9152
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   298
        return ([], {}, {}, False)
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   299
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   300
    # Case 3 (uncommon): cache file missing or empty.
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   301
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   302
    # Case 4 (uncommon): tip rev decreased.  This should only happen
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   303
    # when we're called from localrepository.destroyed().  Refresh the
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   304
    # cache so future invocations will not see disappeared heads in the
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   305
    # cache.
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   306
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   307
    # Case 5 (common): tip has changed, so we've added/replaced heads.
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   308
11352
b19067ee4507 tags: remove inactive debugging code.
Greg Ward <greg-hg@gerg.ca>
parents: 11351
diff changeset
   309
    # As it happens, the code to handle cases 3, 4, 5 is the same.
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   310
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   311
    # N.B. in case 4 (nodes destroyed), "new head" really means "newly
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   312
    # exposed".
16730
dd4ce44ff53c tags: short-circuit if no tags have ever been committed
Bryan O'Sullivan <bryano@fb.com>
parents: 16589
diff changeset
   313
    if not len(repo.file('.hgtags')):
dd4ce44ff53c tags: short-circuit if no tags have ever been committed
Bryan O'Sullivan <bryano@fb.com>
parents: 16589
diff changeset
   314
        # No tags have ever been committed, so we can avoid a
dd4ce44ff53c tags: short-circuit if no tags have ever been committed
Bryan O'Sullivan <bryano@fb.com>
parents: 16589
diff changeset
   315
        # potentially expensive search.
dd4ce44ff53c tags: short-circuit if no tags have ever been committed
Bryan O'Sullivan <bryano@fb.com>
parents: 16589
diff changeset
   316
        return (repoheads, cachefnode, None, True)
dd4ce44ff53c tags: short-circuit if no tags have ever been committed
Bryan O'Sullivan <bryano@fb.com>
parents: 16589
diff changeset
   317
21030
9ea132aee96c tags: log events related to tags cache
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19646
diff changeset
   318
    starttime = time.time()
9ea132aee96c tags: log events related to tags cache
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19646
diff changeset
   319
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   320
    newheads = [head
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   321
                for head in repoheads
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   322
                if head not in set(cacheheads)]
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   323
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   324
    # Now we have to lookup the .hgtags filenode for every new head.
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   325
    # This is the most expensive part of finding tags, so performance
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   326
    # depends primarily on the size of newheads.  Worst case: no cache
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   327
    # file, so newheads == repoheads.
17256
707cbbf950ea tags: visit new heads in forward order when rebuilding cache
Matt Mackall <mpm@selenic.com>
parents: 16730
diff changeset
   328
    for head in reversed(newheads):
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   329
        cctx = repo[head]
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   330
        try:
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   331
            fnode = cctx.filenode('.hgtags')
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   332
            cachefnode[head] = fnode
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   333
        except error.LookupError:
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   334
            # no .hgtags file on this head
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   335
            pass
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   336
21030
9ea132aee96c tags: log events related to tags cache
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19646
diff changeset
   337
    duration = time.time() - starttime
9ea132aee96c tags: log events related to tags cache
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19646
diff changeset
   338
    ui.log('tagscache',
9ea132aee96c tags: log events related to tags cache
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19646
diff changeset
   339
           'resolved %d tags cache entries from %d manifests in %0.4f '
9ea132aee96c tags: log events related to tags cache
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19646
diff changeset
   340
           'seconds\n',
9ea132aee96c tags: log events related to tags cache
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19646
diff changeset
   341
           len(cachefnode), len(newheads), duration)
9ea132aee96c tags: log events related to tags cache
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19646
diff changeset
   342
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   343
    # Caller has to iterate over all heads, but can use the filenodes in
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   344
    # cachefnode to get to each .hgtags revision quickly.
9152
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   345
    return (repoheads, cachefnode, None, True)
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   346
9152
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   347
def _writetagcache(ui, repo, heads, tagfnode, cachetags):
9366
9ff178e7b627 tags: don't crash if unable to write tag cache
Greg Ward <greg-hg@gerg.ca>
parents: 9312
diff changeset
   348
    try:
23877
7cc77030c557 localrepo: remove all external users of localrepo.opener
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 23139
diff changeset
   349
        cachefile = repo.vfs('cache/tags', 'w', atomictemp=True)
9366
9ff178e7b627 tags: don't crash if unable to write tag cache
Greg Ward <greg-hg@gerg.ca>
parents: 9312
diff changeset
   350
    except (OSError, IOError):
9ff178e7b627 tags: don't crash if unable to write tag cache
Greg Ward <greg-hg@gerg.ca>
parents: 9312
diff changeset
   351
        return
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   352
21030
9ea132aee96c tags: log events related to tags cache
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19646
diff changeset
   353
    ui.log('tagscache', 'writing tags cache file with %d heads and %d tags\n',
9ea132aee96c tags: log events related to tags cache
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19646
diff changeset
   354
            len(heads), len(cachetags))
9ea132aee96c tags: log events related to tags cache
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19646
diff changeset
   355
9151
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   356
    realheads = repo.heads()            # for sanity checks below
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   357
    for head in heads:
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   358
        # temporary sanity checks; these can probably be removed
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   359
        # once this code has been in crew for a few weeks
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   360
        assert head in repo.changelog.nodemap, \
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   361
               'trying to write non-existent node %s to tag cache' % short(head)
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   362
        assert head in realheads, \
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   363
               'trying to write non-head %s to tag cache' % short(head)
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   364
        assert head != nullid, \
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   365
               'trying to write nullid to tag cache'
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   366
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   367
        # This can't fail because of the first assert above.  When/if we
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   368
        # remove that assert, we might want to catch LookupError here
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   369
        # and downgrade it to a warning.
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   370
        rev = repo.changelog.rev(head)
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   371
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   372
        fnode = tagfnode.get(head)
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   373
        if fnode:
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   374
            cachefile.write('%d %s %s\n' % (rev, hex(head), hex(fnode)))
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   375
        else:
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   376
            cachefile.write('%d %s\n' % (rev, hex(head)))
f528d1a93491 tags: implement persistent tag caching (issue548).
Greg Ward <greg-hg@gerg.ca>
parents: 9149
diff changeset
   377
9152
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   378
    # Tag names in the cache are in UTF-8 -- which is the whole reason
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   379
    # we keep them in UTF-8 throughout this module.  If we converted
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   380
    # them local encoding on input, we would lose info writing them to
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   381
    # the cache.
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   382
    cachefile.write('\n')
24143
7b09dbbbd502 tags: write tags cache deterministically
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23877
diff changeset
   383
    for (name, (node, hist)) in sorted(cachetags.iteritems()):
19646
335a558f81dc tags: write tag overwriting history also into tag cache file (issue3911)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19108
diff changeset
   384
        for n in hist:
335a558f81dc tags: write tag overwriting history also into tag cache file (issue3911)
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19108
diff changeset
   385
            cachefile.write("%s %s\n" % (hex(n), name))
9152
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   386
        cachefile.write("%s %s\n" % (hex(node), name))
4017291c4c48 tags: support 'instant' tag retrieval (issue548)
Greg Ward <greg-hg@gerg.ca>
parents: 9151
diff changeset
   387
14662
2b30124c7d8a tags: don't allow environment errors to be raised from _writetagscache
Steve Borho <steve@borho.org>
parents: 14168
diff changeset
   388
    try:
15057
774da7121fc9 atomictempfile: make close() consistent with other file-like objects.
Greg Ward <greg@gerg.ca>
parents: 14662
diff changeset
   389
        cachefile.close()
14662
2b30124c7d8a tags: don't allow environment errors to be raised from _writetagscache
Steve Borho <steve@borho.org>
parents: 14168
diff changeset
   390
    except (OSError, IOError):
2b30124c7d8a tags: don't allow environment errors to be raised from _writetagscache
Steve Borho <steve@borho.org>
parents: 14168
diff changeset
   391
        pass