mercurial/branchmap.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Sat, 22 Dec 2012 02:04:49 +0100
changeset 18127 dcd43ac7572d
parent 18126 090ada0acddb
child 18128 f0d56efaa35a
permissions -rw-r--r--
branchmap: simplify write signature All necessary data (cache value and key) are now stored in the branchcache object. Any extra parameter is superfluous.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18116
bcee63733aad branchmap: create a mercurial.branchmap module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     1
# branchmap.py - logic to computes, maintain and stores branchmap for local repo
bcee63733aad branchmap: create a mercurial.branchmap module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     2
#
bcee63733aad branchmap: create a mercurial.branchmap module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     3
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
bcee63733aad branchmap: create a mercurial.branchmap module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     4
#
bcee63733aad branchmap: create a mercurial.branchmap module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
bcee63733aad branchmap: create a mercurial.branchmap module
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
18117
526e7ec5c96e branchmap: extract write logic from localrepo
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18116
diff changeset
     7
18118
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
     8
from node import bin, hex, nullid, nullrev
18117
526e7ec5c96e branchmap: extract write logic from localrepo
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18116
diff changeset
     9
import encoding
526e7ec5c96e branchmap: extract write logic from localrepo
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18116
diff changeset
    10
18118
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    11
def read(repo):
18124
79db6d40bced branchmap: store branchcache in a dedicated object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18121
diff changeset
    12
    partial = branchcache()
18118
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    13
    try:
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    14
        f = repo.opener("cache/branchheads")
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    15
        lines = f.read().split('\n')
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    16
        f.close()
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    17
    except (IOError, OSError):
18126
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
    18
        return branchcache()
18118
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    19
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    20
    try:
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    21
        last, lrev = lines.pop(0).split(" ", 1)
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    22
        last, lrev = bin(last), int(lrev)
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    23
        if lrev >= len(repo) or repo[lrev].node() != last:
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    24
            # invalidate the cache
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    25
            raise ValueError('invalidating branch cache (tip differs)')
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    26
        for l in lines:
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    27
            if not l:
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    28
                continue
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    29
            node, label = l.split(" ", 1)
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    30
            label = encoding.tolocal(label.strip())
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    31
            if not node in repo:
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    32
                raise ValueError('invalidating branch cache because node '+
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    33
                                 '%s does not exist' % node)
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    34
            partial.setdefault(label, []).append(bin(node))
18125
ad194a8ab5c1 branchmap: add the tipnode (cache key) on the branchcache object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18124
diff changeset
    35
        partial.tipnode = last
18126
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
    36
        partial.tiprev = lrev
18118
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    37
    except KeyboardInterrupt:
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    38
        raise
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    39
    except Exception, inst:
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    40
        if repo.ui.debugflag:
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    41
            repo.ui.warn(str(inst), '\n')
18126
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
    42
        partial = branchcache()
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
    43
    return partial
18118
e70ff1e599f4 branchmap: extract read logic from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18117
diff changeset
    44
18127
dcd43ac7572d branchmap: simplify write signature
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18126
diff changeset
    45
def write(repo, cache):
18117
526e7ec5c96e branchmap: extract write logic from localrepo
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18116
diff changeset
    46
    try:
526e7ec5c96e branchmap: extract write logic from localrepo
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18116
diff changeset
    47
        f = repo.opener("cache/branchheads", "w", atomictemp=True)
18127
dcd43ac7572d branchmap: simplify write signature
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18126
diff changeset
    48
        f.write("%s %s\n" % (hex(cache.tipnode), cache.tiprev))
dcd43ac7572d branchmap: simplify write signature
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18126
diff changeset
    49
        for label, nodes in cache.iteritems():
18117
526e7ec5c96e branchmap: extract write logic from localrepo
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18116
diff changeset
    50
            for node in nodes:
526e7ec5c96e branchmap: extract write logic from localrepo
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18116
diff changeset
    51
                f.write("%s %s\n" % (hex(node), encoding.fromlocal(label)))
526e7ec5c96e branchmap: extract write logic from localrepo
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18116
diff changeset
    52
        f.close()
526e7ec5c96e branchmap: extract write logic from localrepo
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18116
diff changeset
    53
    except (IOError, OSError):
526e7ec5c96e branchmap: extract write logic from localrepo
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18116
diff changeset
    54
        pass
18120
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    55
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    56
def update(repo, partial, ctxgen):
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    57
    """Given a branchhead cache, partial, that may have extra nodes or be
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    58
    missing heads, and a generator of nodes that are at least a superset of
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    59
    heads missing, this function updates partial to be correct.
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    60
    """
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    61
    # collect new branch entries
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    62
    newbranches = {}
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    63
    for c in ctxgen:
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    64
        newbranches.setdefault(c.branch(), []).append(c.node())
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    65
    # if older branchheads are reachable from new ones, they aren't
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    66
    # really branchheads. Note checking parents is insufficient:
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    67
    # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    68
    for branch, newnodes in newbranches.iteritems():
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    69
        bheads = partial.setdefault(branch, [])
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    70
        # Remove candidate heads that no longer are in the repo (e.g., as
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    71
        # the result of a strip that just happened).  Avoid using 'node in
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    72
        # self' here because that dives down into branchcache code somewhat
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    73
        # recursively.
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    74
        bheadrevs = [repo.changelog.rev(node) for node in bheads
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    75
                     if repo.changelog.hasnode(node)]
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    76
        newheadrevs = [repo.changelog.rev(node) for node in newnodes
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    77
                       if repo.changelog.hasnode(node)]
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    78
        ctxisnew = bheadrevs and min(newheadrevs) > max(bheadrevs)
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    79
        # Remove duplicates - nodes that are in newheadrevs and are already
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    80
        # in bheadrevs.  This can happen if you strip a node whose parent
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    81
        # was already a head (because they're on different branches).
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    82
        bheadrevs = sorted(set(bheadrevs).union(newheadrevs))
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    83
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    84
        # Starting from tip means fewer passes over reachable.  If we know
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    85
        # the new candidates are not ancestors of existing heads, we don't
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    86
        # have to examine ancestors of existing heads
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    87
        if ctxisnew:
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    88
            iterrevs = sorted(newheadrevs)
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    89
        else:
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    90
            iterrevs = list(bheadrevs)
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    91
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    92
        # This loop prunes out two kinds of heads - heads that are
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    93
        # superseded by a head in newheadrevs, and newheadrevs that are not
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    94
        # heads because an existing head is their descendant.
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    95
        while iterrevs:
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    96
            latest = iterrevs.pop()
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    97
            if latest not in bheadrevs:
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    98
                continue
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    99
            ancestors = set(repo.changelog.ancestors([latest],
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   100
                                                     bheadrevs[0]))
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   101
            if ancestors:
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   102
                bheadrevs = [b for b in bheadrevs if b not in ancestors]
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   103
        partial[branch] = [repo.changelog.node(rev) for rev in bheadrevs]
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   104
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   105
    # There may be branches that cease to exist when the last commit in the
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   106
    # branch was stripped.  This code filters them out.  Note that the
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   107
    # branch that ceased to exist may not be in newbranches because
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   108
    # newbranches is the set of candidate heads, which when you strip the
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   109
    # last commit in a branch will be the parent branch.
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   110
    for branch in partial.keys():
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   111
        nodes = [head for head in partial[branch]
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   112
                 if repo.changelog.hasnode(head)]
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   113
        if not nodes:
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   114
            del partial[branch]
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   115
18121
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   116
def updatecache(repo):
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   117
    repo = repo.unfiltered()  # Until we get a smarter cache management
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   118
    cl = repo.changelog
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   119
    tip = cl.tip()
18125
ad194a8ab5c1 branchmap: add the tipnode (cache key) on the branchcache object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18124
diff changeset
   120
    partial = repo._branchcache
ad194a8ab5c1 branchmap: add the tipnode (cache key) on the branchcache object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18124
diff changeset
   121
    if partial is not None and partial.tipnode == tip:
18121
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   122
        return
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   123
18125
ad194a8ab5c1 branchmap: add the tipnode (cache key) on the branchcache object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18124
diff changeset
   124
    if partial is None or partial.tipnode not in cl.nodemap:
18126
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
   125
        partial = read(repo)
18121
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   126
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   127
    catip = repo._cacheabletip()
18126
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
   128
    # if partial.tiprev == catip: cache is already up to date
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
   129
    # if partial.tiprev >  catip: we have uncachable element in `partial` can't
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
   130
    #                             write on disk
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
   131
    if partial.tiprev < catip:
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
   132
        ctxgen = (repo[r] for r in cl.revs(partial.tiprev + 1, catip))
18121
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   133
        update(repo, partial, ctxgen)
18125
ad194a8ab5c1 branchmap: add the tipnode (cache key) on the branchcache object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18124
diff changeset
   134
        partial.tipnode = cl.node(catip)
18126
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
   135
        partial.tiprev = catip
18127
dcd43ac7572d branchmap: simplify write signature
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18126
diff changeset
   136
        write(repo, partial)
18121
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   137
    # If cacheable tip were lower than actual tip, we need to update the
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   138
    # cache up to tip. This update (from cacheable to actual tip) is not
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   139
    # written to disk since it's not cacheable.
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   140
    tiprev = len(repo) - 1
18126
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
   141
    if partial.tiprev < tiprev:
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
   142
        ctxgen = (repo[r] for r in cl.revs(partial.tiprev + 1, tiprev))
18121
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   143
        update(repo, partial, ctxgen)
18125
ad194a8ab5c1 branchmap: add the tipnode (cache key) on the branchcache object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18124
diff changeset
   144
        partial.tipnode = cl.node(tiprev)
18126
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
   145
        partial.tiprev = tiprev
18121
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   146
    repo._branchcache = partial
18124
79db6d40bced branchmap: store branchcache in a dedicated object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18121
diff changeset
   147
79db6d40bced branchmap: store branchcache in a dedicated object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18121
diff changeset
   148
class branchcache(dict):
79db6d40bced branchmap: store branchcache in a dedicated object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18121
diff changeset
   149
    """A dict like object that hold branches heads cache"""
79db6d40bced branchmap: store branchcache in a dedicated object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18121
diff changeset
   150
18126
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
   151
    def __init__(self, entries=(), tipnode=nullid, tiprev=nullrev):
18125
ad194a8ab5c1 branchmap: add the tipnode (cache key) on the branchcache object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18124
diff changeset
   152
        super(branchcache, self).__init__(entries)
ad194a8ab5c1 branchmap: add the tipnode (cache key) on the branchcache object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18124
diff changeset
   153
        self.tipnode = tipnode
18126
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
   154
        self.tiprev = tiprev