hgext3rd/topic/destination.py
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
Fri, 12 Aug 2016 11:09:26 +0200
changeset 1973 e97458bf53be
parent 1969 a604423c1500
child 1980 3c4a20244771
permissions -rw-r--r--
stack: introduce and explicite command to display the stack If is worth testing having the feature lives outside 'hg topic --list'. The 'stack' name is sub optimal because the conflict with 'status'. using "hg st" would still work as 'st' is hard coded, but this mean 'hg stack' has no short version.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1933
ca8674a8fce4 destination: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1912
diff changeset
     1
from __future__ import absolute_import
ca8674a8fce4 destination: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1912
diff changeset
     2
1871
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
     3
from mercurial.i18n import _
1933
ca8674a8fce4 destination: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1912
diff changeset
     4
from mercurial import (
ca8674a8fce4 destination: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1912
diff changeset
     5
    bookmarks,
ca8674a8fce4 destination: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1912
diff changeset
     6
    destutil,
ca8674a8fce4 destination: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1912
diff changeset
     7
    error,
ca8674a8fce4 destination: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1912
diff changeset
     8
    extensions,
ca8674a8fce4 destination: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1912
diff changeset
     9
    util,
ca8674a8fce4 destination: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1912
diff changeset
    10
)
1871
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    11
1962
c00815786813 compat: adapt to the new destspace argument of destmerge
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1941
diff changeset
    12
def _destmergebranch(orig, repo, action='merge', sourceset=None,
c00815786813 compat: adapt to the new destspace argument of destmerge
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1941
diff changeset
    13
                     onheadcheck=True, destspace=None):
1967
c5d4e856ad0e destination: add an XXX about handling destspace in destmerge
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1964
diff changeset
    14
    # XXX: take destspace into account
1871
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    15
    p1 = repo['.']
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    16
    top = p1.topic()
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    17
    if top:
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    18
        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
    19
        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
    20
            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
    21
        elif 1 == len(heads):
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    22
            # 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
    23
            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
    24
            if not bhead:
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    25
                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
    26
            elif 1 == len(bhead):
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    27
                return bhead.first()
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    28
            else:
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    29
                raise error.Abort(_("branch '%s' has %d heads - "
1964
43f3c7ce73be destination: flake8
Sean Farley <sean@farley.io>
parents: 1962
diff changeset
    30
                                    "please merge with an explicit rev")
43f3c7ce73be destination: flake8
Sean Farley <sean@farley.io>
parents: 1962
diff changeset
    31
                                  % (p1.branch(), len(bhead)),
43f3c7ce73be destination: flake8
Sean Farley <sean@farley.io>
parents: 1962
diff changeset
    32
                                  hint=_("run 'hg heads .' to see heads"))
1871
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    33
        elif 2 == len(heads):
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    34
            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
    35
            # 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
    36
            if 1 < len(heads):
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    37
                raise error.Abort(_('working directory not at a head revision'),
1964
43f3c7ce73be destination: flake8
Sean Farley <sean@farley.io>
parents: 1962
diff changeset
    38
                                  hint=_("use 'hg update' or merge with an "
43f3c7ce73be destination: flake8
Sean Farley <sean@farley.io>
parents: 1962
diff changeset
    39
                                         "explicit revision"))
1871
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    40
            return heads[0]
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    41
        elif 2 < len(heads):
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    42
            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
    43
                                "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
    44
                              % (top, len(heads)))
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    45
        else:
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    46
            assert False # that's impossible
1962
c00815786813 compat: adapt to the new destspace argument of destmerge
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1941
diff changeset
    47
    if len(getattr(orig, 'func_defaults', ())) == 3: # version hg-3.7
1878
c44f161575ba compat: adapt to change in mercurial core
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1871
diff changeset
    48
        return orig(repo, action, sourceset, onheadcheck)
1962
c00815786813 compat: adapt to the new destspace argument of destmerge
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1941
diff changeset
    49
    if 3 < len(getattr(orig, 'func_defaults', ())): # version hg-3.8 and above
c00815786813 compat: adapt to the new destspace argument of destmerge
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1941
diff changeset
    50
        return orig(repo, action, sourceset, onheadcheck, destspace=destspace)
1878
c44f161575ba compat: adapt to change in mercurial core
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1871
diff changeset
    51
    else:
c44f161575ba compat: adapt to change in mercurial core
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1871
diff changeset
    52
        return orig(repo)
1871
58ef5699fb35 merge: use topic to pick default destination
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1870
diff changeset
    53
1892
b1fadc089b82 update: change default update destination to take topic in account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1891
diff changeset
    54
def _destupdatetopic(repo, clean, check):
b1fadc089b82 update: change default update destination to take topic in account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1891
diff changeset
    55
    """decide on an update destination from current topic"""
b1fadc089b82 update: change default update destination to take topic in account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1891
diff changeset
    56
    movemark = node = None
b1fadc089b82 update: change default update destination to take topic in account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1891
diff changeset
    57
    topic = repo.currenttopic
b1fadc089b82 update: change default update destination to take topic in account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1891
diff changeset
    58
    revs = repo.revs('.::topic("%s")' % topic)
b1fadc089b82 update: change default update destination to take topic in account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1891
diff changeset
    59
    if not revs:
b1fadc089b82 update: change default update destination to take topic in account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1891
diff changeset
    60
        return None, None, None
b1fadc089b82 update: change default update destination to take topic in account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1891
diff changeset
    61
    node = revs.last()
b1fadc089b82 update: change default update destination to take topic in account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1891
diff changeset
    62
    if bookmarks.isactivewdirparent(repo):
b1fadc089b82 update: change default update destination to take topic in account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1891
diff changeset
    63
        movemark = repo['.'].node()
b1fadc089b82 update: change default update destination to take topic in account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1891
diff changeset
    64
    return node, movemark, None
b1fadc089b82 update: change default update destination to take topic in account
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1891
diff changeset
    65
1911
442a7cb8404e histedit: restrict default edited set to current topic when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1902
diff changeset
    66
def desthistedit(orig, ui, repo):
442a7cb8404e histedit: restrict default edited set to current topic when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1902
diff changeset
    67
    if not (ui.config('histedit', 'defaultrev', None) is None
442a7cb8404e histedit: restrict default edited set to current topic when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1902
diff changeset
    68
            and repo.currenttopic):
442a7cb8404e histedit: restrict default edited set to current topic when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1902
diff changeset
    69
        return orig(ui, repo)
442a7cb8404e histedit: restrict default edited set to current topic when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1902
diff changeset
    70
    revs = repo.revs('::. and stack()')
442a7cb8404e histedit: restrict default edited set to current topic when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1902
diff changeset
    71
    if revs:
442a7cb8404e histedit: restrict default edited set to current topic when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1902
diff changeset
    72
        return revs.min()
442a7cb8404e histedit: restrict default edited set to current topic when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1902
diff changeset
    73
    return None
442a7cb8404e histedit: restrict default edited set to current topic when possible
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1902
diff changeset
    74
1870
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    75
def ngtip(repo, branch, all=False):
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    76
    """tip new generation"""
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    77
    ## search for untopiced heads of branch
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    78
    # could be heads((::branch(x) - topic()))
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    79
    # but that is expensive
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    80
    #
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    81
    # we should write plain code instead
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    82
    subquery = '''heads(
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    83
                    parents(
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    84
                       ancestor(
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    85
                         (head() and branch(%s)
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    86
                         or (topic() and branch(%s)))))
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    87
                   ::(head() and branch(%s))
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    88
                   - topic())'''
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    89
    if not all:
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    90
        subquery = 'max(%s)' % subquery
8dd5200b4086 topic: introduce a 'ngtip' concept
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    91
    return repo.revs(subquery, branch, branch, branch)
1938
7e31d3e79806 destination: make sure 'setupdest' is the latest function in the module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1933
diff changeset
    92
1941
7eb737b7a902 destination: rename 'setupdest' to 'modsetup'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1940
diff changeset
    93
def modsetup(ui):
1940
6d53f81d3b64 destination: document 'setupdest'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1939
diff changeset
    94
    """run a uisetup time to install all destinations wrapping"""
1938
7e31d3e79806 destination: make sure 'setupdest' is the latest function in the module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1933
diff changeset
    95
    if util.safehasattr(destutil, '_destmergebranch'):
7e31d3e79806 destination: make sure 'setupdest' is the latest function in the module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1933
diff changeset
    96
        extensions.wrapfunction(destutil, '_destmergebranch', _destmergebranch)
1969
a604423c1500 compat: tolerate missing rebase extension
timeless@gmail.com
parents: 1967
diff changeset
    97
    try:
a604423c1500 compat: tolerate missing rebase extension
timeless@gmail.com
parents: 1967
diff changeset
    98
        rebase = extensions.find('rebase')
a604423c1500 compat: tolerate missing rebase extension
timeless@gmail.com
parents: 1967
diff changeset
    99
    except KeyError:
a604423c1500 compat: tolerate missing rebase extension
timeless@gmail.com
parents: 1967
diff changeset
   100
        rebase = None
1938
7e31d3e79806 destination: make sure 'setupdest' is the latest function in the module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1933
diff changeset
   101
    if (util.safehasattr(rebase, '_destrebase')
7e31d3e79806 destination: make sure 'setupdest' is the latest function in the module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1933
diff changeset
   102
            # logic not shared with merge yet < hg-3.8
7e31d3e79806 destination: make sure 'setupdest' is the latest function in the module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1933
diff changeset
   103
            and not util.safehasattr(rebase, '_definesets')):
7e31d3e79806 destination: make sure 'setupdest' is the latest function in the module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1933
diff changeset
   104
        extensions.wrapfunction(rebase, '_destrebase', _destmergebranch)
7e31d3e79806 destination: make sure 'setupdest' is the latest function in the module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1933
diff changeset
   105
    if util.safehasattr(destutil, 'destupdatesteps'):
7e31d3e79806 destination: make sure 'setupdest' is the latest function in the module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1933
diff changeset
   106
        bridx = destutil.destupdatesteps.index('branch')
7e31d3e79806 destination: make sure 'setupdest' is the latest function in the module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1933
diff changeset
   107
        destutil.destupdatesteps.insert(bridx, 'topic')
7e31d3e79806 destination: make sure 'setupdest' is the latest function in the module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1933
diff changeset
   108
        destutil.destupdatestepmap['topic'] = _destupdatetopic
7e31d3e79806 destination: make sure 'setupdest' is the latest function in the module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1933
diff changeset
   109
    if util.safehasattr(destutil, 'desthistedit'):
7e31d3e79806 destination: make sure 'setupdest' is the latest function in the module
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1933
diff changeset
   110
        extensions.wrapfunction(destutil, 'desthistedit', desthistedit)