mercurial/branchmap.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Mon, 24 Dec 2012 02:22:04 +0100
changeset 18130 1b05ffce47bd
parent 18129 3264d3ce53a0
child 18131 f0eeb9b3444a
permissions -rw-r--r--
branchmap: make update responsible to update the cache key The update function have all necessary data to keep the branchcache key up to date with its value. This saves assignment to the cache key that each caller of update had to do by hand. The strip case is a bit more complicated to handles from inside the function but I do not expect any impact.
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
18120
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    45
def update(repo, partial, ctxgen):
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    46
    """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
    47
    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
    48
    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
    49
    """
18129
3264d3ce53a0 branchmap: factorise changelog access in update
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18128
diff changeset
    50
    cl = repo.changelog
18120
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    51
    # collect new branch entries
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    52
    newbranches = {}
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    53
    for c in ctxgen:
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    54
        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
    55
    # 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
    56
    # 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
    57
    # 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
    58
    for branch, newnodes in newbranches.iteritems():
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    59
        bheads = partial.setdefault(branch, [])
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    60
        # 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
    61
        # 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
    62
        # 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
    63
        # recursively.
18129
3264d3ce53a0 branchmap: factorise changelog access in update
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18128
diff changeset
    64
        bheadrevs = [cl.rev(node) for node in bheads
3264d3ce53a0 branchmap: factorise changelog access in update
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18128
diff changeset
    65
                     if cl.hasnode(node)]
3264d3ce53a0 branchmap: factorise changelog access in update
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18128
diff changeset
    66
        newheadrevs = [cl.rev(node) for node in newnodes
3264d3ce53a0 branchmap: factorise changelog access in update
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18128
diff changeset
    67
                       if cl.hasnode(node)]
18120
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    68
        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
    69
        # 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
    70
        # 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
    71
        # 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
    72
        bheadrevs = sorted(set(bheadrevs).union(newheadrevs))
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    73
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    74
        # 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
    75
        # 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
    76
        # 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
    77
        if ctxisnew:
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    78
            iterrevs = sorted(newheadrevs)
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    79
        else:
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    80
            iterrevs = list(bheadrevs)
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    81
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    82
        # 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
    83
        # 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
    84
        # 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
    85
        while iterrevs:
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    86
            latest = iterrevs.pop()
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    87
            if latest not in bheadrevs:
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    88
                continue
18129
3264d3ce53a0 branchmap: factorise changelog access in update
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18128
diff changeset
    89
            ancestors = set(cl.ancestors([latest],
18120
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    90
                                                     bheadrevs[0]))
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    91
            if ancestors:
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    92
                bheadrevs = [b for b in bheadrevs if b not in ancestors]
18129
3264d3ce53a0 branchmap: factorise changelog access in update
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18128
diff changeset
    93
        partial[branch] = [cl.node(rev) for rev in bheadrevs]
18130
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
    94
        tiprev = max(bheadrevs)
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
    95
        if tiprev > partial.tiprev:
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
    96
            partial.tipnode = cl.node(tiprev)
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
    97
            partial.tiprev = tiprev
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
    98
18120
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
    99
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   100
    # 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
   101
    # 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
   102
    # 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
   103
    # 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
   104
    # last commit in a branch will be the parent branch.
18130
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   105
    droppednodes = []
18120
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   106
    for branch in partial.keys():
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   107
        nodes = [head for head in partial[branch]
18129
3264d3ce53a0 branchmap: factorise changelog access in update
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18128
diff changeset
   108
                 if cl.hasnode(head)]
18120
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   109
        if not nodes:
18130
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   110
            droppednodes.extend(nodes)
18120
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   111
            del partial[branch]
18130
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   112
    try:
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   113
        node = cl.node(partial.tiprev)
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   114
    except IndexError:
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   115
        node = None
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   116
    if ((partial.tipnode != node)
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   117
        or (partial.tipnode in droppednodes)):
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   118
        # cache key are not valid anymore
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   119
        partial.tipnode = nullid
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   120
        partial.tiprev = nullrev
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   121
        for heads in partial.values():
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   122
            tiprev = max(cl.rev(node) for node in heads)
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   123
            if tiprev > partial.tiprev:
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   124
                partial.tipnode = cl.node(tiprev)
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   125
                partial.tiprev = tiprev
1b05ffce47bd branchmap: make update responsible to update the cache key
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18129
diff changeset
   126
18120
88990d3e3d75 branchmap: extract _updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18118
diff changeset
   127
18121
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   128
def updatecache(repo):
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   129
    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
   130
    cl = repo.changelog
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   131
    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
   132
    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
   133
    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
   134
        return
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   135
18125
ad194a8ab5c1 branchmap: add the tipnode (cache key) on the branchcache object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18124
diff changeset
   136
    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
   137
        partial = read(repo)
18121
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   138
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   139
    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
   140
    # 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
   141
    # 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
   142
    #                             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
   143
    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
   144
        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
   145
        update(repo, partial, ctxgen)
18128
f0d56efaa35a branchmap: make write a method on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18127
diff changeset
   146
        partial.write(repo)
18121
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   147
    # 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
   148
    # 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
   149
    # 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
   150
    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
   151
    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
   152
        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
   153
        update(repo, partial, ctxgen)
f8a13f061a8a branchmap: extract updatebranchcache from repo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18120
diff changeset
   154
    repo._branchcache = partial
18124
79db6d40bced branchmap: store branchcache in a dedicated object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18121
diff changeset
   155
79db6d40bced branchmap: store branchcache in a dedicated object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18121
diff changeset
   156
class branchcache(dict):
79db6d40bced branchmap: store branchcache in a dedicated object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18121
diff changeset
   157
    """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
   158
18126
090ada0acddb branchmap: add the tiprev (cache key) on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18125
diff changeset
   159
    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
   160
        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
   161
        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
   162
        self.tiprev = tiprev
18128
f0d56efaa35a branchmap: make write a method on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18127
diff changeset
   163
f0d56efaa35a branchmap: make write a method on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18127
diff changeset
   164
    def write(self, repo):
f0d56efaa35a branchmap: make write a method on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18127
diff changeset
   165
        try:
f0d56efaa35a branchmap: make write a method on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18127
diff changeset
   166
            f = repo.opener("cache/branchheads", "w", atomictemp=True)
f0d56efaa35a branchmap: make write a method on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18127
diff changeset
   167
            f.write("%s %s\n" % (hex(self.tipnode), self.tiprev))
f0d56efaa35a branchmap: make write a method on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18127
diff changeset
   168
            for label, nodes in self.iteritems():
f0d56efaa35a branchmap: make write a method on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18127
diff changeset
   169
                for node in nodes:
f0d56efaa35a branchmap: make write a method on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18127
diff changeset
   170
                    f.write("%s %s\n" % (hex(node), encoding.fromlocal(label)))
f0d56efaa35a branchmap: make write a method on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18127
diff changeset
   171
            f.close()
f0d56efaa35a branchmap: make write a method on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18127
diff changeset
   172
        except (IOError, OSError):
f0d56efaa35a branchmap: make write a method on the branchmap object
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 18127
diff changeset
   173
            pass