src/topic/destination.py
author Pierre-Yves David <pierre-yves.david@fb.com>
Sun, 13 Mar 2016 23:44:04 +0000
changeset 1890 e846b8f402d0
parent 1878 c44f161575ba
child 1891 077c40f206d1
permissions -rw-r--r--
topicmap: write and read format from disc To prevent too awful performance we allow writing and reading topicmap cache. This is done with a lot of code duplication from core because core is not extensible enough.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1870
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     1
from mercurial import error
1871
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
     2
from mercurial import util
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
     3
from mercurial import destutil
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
     4
from mercurial import extensions
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
     5
from mercurial.i18n import _
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
     6
1878
c44f161575ba compat: adapt to change in mercurial core
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1871
diff changeset
     7
def _destmergebranch(orig, repo, action='merge', sourceset=None, onheadcheck=True):
1871
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
     8
    p1 = repo['.']
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
     9
    top = p1.topic()
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    10
    if top:
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    11
        heads = repo.revs('heads(topic(.)::topic(.))')
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    12
        if p1.rev() not in heads:
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    13
            raise error.Abort(_("not at topic head, update or explicit"))
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    14
        elif 1 == len(heads):
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    15
            # should look at all branch involved but... later
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    16
            bhead = ngtip(repo, p1.branch(), all=True)
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    17
            if not bhead:
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    18
                raise error.Abort(_("nothing to merge"))
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    19
            elif 1 == len(bhead):
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    20
                return bhead.first()
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    21
            else:
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    22
                raise error.Abort(_("branch '%s' has %d heads - "
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    23
                                   "please merge with an explicit rev")
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    24
                                 % (p1.branch(), len(bhead)),
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    25
                                 hint=_("run 'hg heads .' to see heads"))
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    26
        elif 2 == len(heads):
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    27
            heads = [r for r in heads if r != p1.rev()]
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    28
            # XXX: bla bla bla bla bla
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    29
            if 1 < len(heads):
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    30
                raise error.Abort(_('working directory not at a head revision'),
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    31
                                 hint=_("use 'hg update' or merge with an "
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    32
                                        "explicit revision"))
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    33
            return heads[0]
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    34
        elif 2 < len(heads):
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    35
            raise error.Abort(_("topic '%s' has %d heads - "
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    36
                                "please merge with an explicit rev")
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    37
                              % (top, len(heads)))
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    38
        else:
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    39
            assert False # that's impossible
1878
c44f161575ba compat: adapt to change in mercurial core
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1871
diff changeset
    40
    if orig.func_default: # version above hg-3.7
c44f161575ba compat: adapt to change in mercurial core
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1871
diff changeset
    41
        return orig(repo, action, sourceset, onheadcheck)
c44f161575ba compat: adapt to change in mercurial core
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1871
diff changeset
    42
    else:
c44f161575ba compat: adapt to change in mercurial core
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1871
diff changeset
    43
        return orig(repo)
1871
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    44
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    45
def setupdest():
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    46
    if util.safehasattr(destutil, '_destmergebranch'):
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    47
        extensions.wrapfunction(destutil, '_destmergebranch', _destmergebranch)
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    48
1870
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    49
def ngtip(repo, branch, all=False):
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    50
    """tip new generation"""
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    51
    ## search for untopiced heads of branch
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    52
    # could be heads((::branch(x) - topic()))
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    53
    # but that is expensive
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    54
    #
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    55
    # we should write plain code instead
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    56
    subquery = '''heads(
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    57
                    parents(
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    58
                       ancestor(
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    59
                         (head() and branch(%s)
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    60
                         or (topic() and branch(%s)))))
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    61
                   ::(head() and branch(%s))
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    62
                   - topic())'''
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    63
    if not all:
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    64
        subquery = 'max(%s)' % subquery
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    65
    return repo.revs(subquery, branch, branch, branch)