hgext3rd/topic/topicmap.py
author Anton Shestakov <av6@dwimlabs.net>
Wed, 06 Mar 2019 21:47:08 +0800
changeset 4431 f109917f5c28
parent 4394 c6d1b0a6babe
child 4451 a019067fea9e
permissions -rw-r--r--
evolvecmd: compatibility with older geteffectflag() that took a tuple Before bae6f1418a95 the function took a tuple of 2 arguments instead of these 2 arguments directly.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1949
79c08d17a3d7 topicmap: move the 'usetopicmap' context manager into the topicmap module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1937
diff changeset
     1
import contextlib
1968
08cbfeb15a1a compat: mercurial dropped alias for hashlib.sha1
timeless@gmail.com
parents: 1953
diff changeset
     2
import hashlib
1949
79c08d17a3d7 topicmap: move the 'usetopicmap' context manager into the topicmap module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1937
diff changeset
     3
3397
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
     4
from mercurial.i18n import _
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
     5
from mercurial.node import nullid
1937
60b7de2b3dd1 topicmap: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1928
diff changeset
     6
from mercurial import (
60b7de2b3dd1 topicmap: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1928
diff changeset
     7
    branchmap,
1950
99c1a26abf3f topicmap: move 'cgapply' wrapping into the topicmap module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1949
diff changeset
     8
    changegroup,
1953
bdc5bb223b50 commit: wrap "commitstatus" to take topic into account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1950
diff changeset
     9
    cmdutil,
1950
99c1a26abf3f topicmap: move 'cgapply' wrapping into the topicmap module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1949
diff changeset
    10
    extensions,
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    11
    repoview,
1937
60b7de2b3dd1 topicmap: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1928
diff changeset
    12
)
1885
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    13
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    14
basefilter = set(['base', 'immutable'])
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    15
def topicfilter(name):
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    16
    """return a "topic" version of a filter level"""
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    17
    if name in basefilter:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    18
        return name
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    19
    elif name is None:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    20
        return None
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    21
    elif name.endswith('-topic'):
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    22
        return name
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    23
    else:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    24
        return name + '-topic'
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    25
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    26
def istopicfilter(filtername):
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    27
    if filtername is None:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    28
        return False
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    29
    return filtername.endswith('-topic')
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    30
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    31
def gettopicrepo(repo):
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    32
    filtername = topicfilter(repo.filtername)
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    33
    if filtername == repo.filtername:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    34
        return repo
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    35
    return repo.filtered(filtername)
1885
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    36
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    37
def _setuptopicfilter(ui):
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    38
    """extend the filter related mapping with topic related one"""
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    39
    funcmap = repoview.filtertable
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    40
    partialmap = branchmap.subsettable
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    41
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    42
    # filter level not affected by topic that we should not override
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    43
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    44
    for plainname in list(funcmap):
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    45
        newfilter = topicfilter(plainname)
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    46
        if newfilter == plainname:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    47
            continue
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    48
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    49
        def revsfunc(repo, name=plainname):
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    50
            return repoview.filterrevs(repo, name)
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    51
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    52
        base = topicfilter(partialmap[plainname])
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    53
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    54
        if newfilter not in funcmap:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    55
            funcmap[newfilter] = revsfunc
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    56
            partialmap[newfilter] = base
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    57
    funcmap['unfiltered-topic'] = lambda repo: frozenset()
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    58
    partialmap['unfiltered-topic'] = 'visible-topic'
1885
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    59
1890
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    60
def _phaseshash(repo, maxrev):
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    61
    """uniq ID for a phase matching a set of rev"""
1890
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    62
    revs = set()
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    63
    cl = repo.changelog
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    64
    fr = cl.filteredrevs
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    65
    nm = cl.nodemap
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    66
    for roots in repo._phasecache.phaseroots[1:]:
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    67
        for n in roots:
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    68
            r = nm.get(n)
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    69
            if r not in fr and r < maxrev:
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    70
                revs.add(r)
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    71
    key = nullid
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    72
    revs = sorted(revs)
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    73
    if revs:
1968
08cbfeb15a1a compat: mercurial dropped alias for hashlib.sha1
timeless@gmail.com
parents: 1953
diff changeset
    74
        s = hashlib.sha1()
1890
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    75
        for rev in revs:
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    76
            s.update('%s;' % rev)
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    77
        key = s.digest()
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    78
    return key
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
    79
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    80
def modsetup(ui):
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    81
    """call at uisetup time to install various wrappings"""
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    82
    _setuptopicfilter(ui)
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    83
    _wrapbmcache(ui)
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    84
    extensions.wrapfunction(changegroup.cg1unpacker, 'apply', cgapply)
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    85
    extensions.wrapfunction(cmdutil, 'commitstatus', commitstatus)
1949
79c08d17a3d7 topicmap: move the 'usetopicmap' context manager into the topicmap module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1937
diff changeset
    86
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    87
def cgapply(orig, self, repo, *args, **kwargs):
1950
99c1a26abf3f topicmap: move 'cgapply' wrapping into the topicmap module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1949
diff changeset
    88
    """make sure a topicmap is used when applying a changegroup"""
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    89
    other = repo.filtered(topicfilter(repo.filtername))
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    90
    return orig(self, other, *args, **kwargs)
1950
99c1a26abf3f topicmap: move 'cgapply' wrapping into the topicmap module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1949
diff changeset
    91
1953
bdc5bb223b50 commit: wrap "commitstatus" to take topic into account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1950
diff changeset
    92
def commitstatus(orig, repo, node, branch, bheads=None, opts=None):
bdc5bb223b50 commit: wrap "commitstatus" to take topic into account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1950
diff changeset
    93
    # wrap commit status use the topic branch heads
bdc5bb223b50 commit: wrap "commitstatus" to take topic into account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1950
diff changeset
    94
    ctx = repo[node]
bdc5bb223b50 commit: wrap "commitstatus" to take topic into account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1950
diff changeset
    95
    if ctx.topic() and ctx.branch() == branch:
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    96
        subbranch = "%s:%s" % (branch, ctx.topic())
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
    97
        bheads = repo.branchheads("%s:%s" % (subbranch, ctx.topic()))
3397
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
    98
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
    99
    ret = orig(repo, node, branch, bheads=bheads, opts=opts)
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   100
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   101
    # logic copy-pasted from cmdutil.commitstatus()
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   102
    if opts is None:
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   103
        opts = {}
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   104
    ctx = repo[node]
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   105
    if ctx.topic():
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   106
        return ret
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   107
    parents = ctx.parents()
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   108
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   109
    if (not opts.get('amend') and bheads and node not in bheads and not
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   110
        [x for x in parents if x.node() in bheads and x.branch() == branch]):
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   111
        repo.ui.status(_("(consider using topic for lightweight branches."
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   112
                         " See 'hg help topic')\n"))
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   113
f7129e3d5a38 topic: suggest using topic when user creates a new head on branch
Pulkit Goyal <7895pulkit@gmail.com>
parents: 2655
diff changeset
   114
    return ret
1953
bdc5bb223b50 commit: wrap "commitstatus" to take topic into account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1950
diff changeset
   115
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   116
def _wrapbmcache(ui):
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   117
    class topiccache(_topiccache, branchmap.branchcache):
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   118
        pass
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   119
    branchmap.branchcache = topiccache
4394
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   120
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   121
    try:
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   122
        # Mercurial 4.9
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   123
        class remotetopiccache(_topiccache, branchmap.remotebranchcache):
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   124
            pass
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   125
        branchmap.remotebranchcache = remotetopiccache
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   126
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   127
        def _wrapupdatebmcachemethod(orig, self, repo):
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   128
            # pass in the bound method as the original
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   129
            return _wrapupdatebmcache(orig.__get__(self), repo)
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   130
        extensions.wrapfunction(branchmap.BranchMapCache, 'updatecache', _wrapupdatebmcachemethod)
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   131
    except AttributeError:
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   132
        # Mercurial 4.8 and before
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   133
        extensions.wrapfunction(branchmap, 'updatecache', _wrapupdatebmcache)
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4187
diff changeset
   134
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   135
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   136
def _wrapupdatebmcache(orig, repo):
2655
417490bdf28a topic: avoid crash when topic is loaded but not enabled for a repository
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2653
diff changeset
   137
    previous = getattr(repo, '_autobranchmaptopic', False)
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   138
    try:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   139
        repo._autobranchmaptopic = False
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   140
        return orig(repo)
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   141
    finally:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   142
        repo._autobranchmaptopic = previous
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   143
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   144
# needed to prevent reference used for 'super()' call using in branchmap.py to
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   145
# no go into cycle. (yes, URG)
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   146
_oldbranchmap = branchmap.branchcache
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   147
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   148
@contextlib.contextmanager
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   149
def oldbranchmap():
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   150
    previous = branchmap.branchcache
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   151
    try:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   152
        branchmap.branchcache = _oldbranchmap
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   153
        yield
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   154
    finally:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   155
        branchmap.branchcache = previous
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   156
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   157
class _topiccache(object): # combine me with branchmap.branchcache
1885
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   158
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   159
    def __init__(self, *args, **kwargs):
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   160
        # super() call may fail otherwise
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   161
        with oldbranchmap():
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   162
            super(_topiccache, self).__init__(*args, **kwargs)
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   163
        self.phaseshash = None
1885
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   164
1890
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
   165
    def copy(self):
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
   166
        """return an deep copy of the branchcache object"""
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   167
        new = self.__class__(self, self.tipnode, self.tiprev, self.filteredhash,
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   168
                             self._closednodes)
1890
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
   169
        new.phaseshash = self.phaseshash
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
   170
        return new
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
   171
1885
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   172
    def branchtip(self, branch, topic=''):
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   173
        '''Return the tipmost open head on branch head, otherwise return the
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   174
        tipmost closed head on branch.
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   175
        Raise KeyError for unknown branch.'''
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   176
        if topic:
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   177
            branch = '%s:%s' % (branch, topic)
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   178
        return super(_topiccache, self).branchtip(branch)
1885
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   179
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   180
    def branchheads(self, branch, closed=False, topic=''):
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   181
        if topic:
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   182
            branch = '%s:%s' % (branch, topic)
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   183
        return super(_topiccache, self).branchheads(branch, closed=closed)
1885
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   184
1890
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
   185
    def validfor(self, repo):
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
   186
        """Is the cache content valid regarding a repo
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
   187
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
   188
        - False when cached tipnode is unknown or if we detect a strip.
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
   189
        - True when cache is up to date or a subset of current repo."""
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   190
        valid = super(_topiccache, self).validfor(repo)
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   191
        if not valid:
1890
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
   192
            return False
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   193
        elif not istopicfilter(repo.filtername) or self.phaseshash is None:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   194
            # phasehash at None means this is a branchmap
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   195
            # come from non topic thing
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   196
            return True
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   197
        else:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   198
            try:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   199
                valid = self.phaseshash == _phaseshash(repo, self.tiprev)
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   200
                return valid
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   201
            except IndexError:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   202
                return False
1890
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
   203
1885
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   204
    def write(self, repo):
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   205
        # we expect mutable set to be small enough to be that computing it all
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   206
        # the time will be fast enough
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   207
        if not istopicfilter(repo.filtername):
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   208
            super(_topiccache, self).write(repo)
1885
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   209
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   210
    def update(self, repo, revgen):
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   211
        """Given a branchhead cache, self, that may have extra nodes or be
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   212
        missing heads, and a generator of nodes that are strictly a superset of
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   213
        heads missing, this function updates self to be correct.
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   214
        """
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   215
        if not istopicfilter(repo.filtername):
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   216
            return super(_topiccache, self).update(repo, revgen)
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   217
        unfi = repo.unfiltered()
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   218
        oldgetbranchinfo = unfi.revbranchcache().branchinfo
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   219
4187
49d442a2207f topic: add the changelog argument to branchinfo()
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 3397
diff changeset
   220
        def branchinfo(r, changelog=None):
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   221
            info = oldgetbranchinfo(r)
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   222
            topic = ''
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   223
            ctx = unfi[r]
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   224
            if ctx.mutable():
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   225
                topic = ctx.topic()
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   226
            branch = info[0]
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   227
            if topic:
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   228
                branch = '%s:%s' % (branch, topic)
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   229
            return (branch, info[1])
1885
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   230
        try:
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   231
            unfi.revbranchcache().branchinfo = branchinfo
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   232
            super(_topiccache, self).update(repo, revgen)
1890
e846b8f402d0 topicmap: write and read format from disc
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1885
diff changeset
   233
            self.phaseshash = _phaseshash(repo, self.tiprev)
1885
d49f75eab6a3 topic: take topic in account for all branch head computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
   234
        finally:
2653
13313d0cab71 topicmap: massive rework
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 2004
diff changeset
   235
            unfi.revbranchcache().branchinfo = oldgetbranchinfo