hgext3rd/topic/stack.py
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
Sun, 14 Aug 2016 20:40:46 +0200
changeset 1976 ebdc2a6a9a25
parent 1961 d9c7fced94fc
child 1977 137f8b04901e
permissions -rw-r--r--
topic: add color for the active topic We reuse the same color that the one used for the active bookmark.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1895
c8e4c6e03957 stack: add a very first version of stack display with 'hg topic --list'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     1
# stack.py - code related to stack workflow
c8e4c6e03957 stack: add a very first version of stack display with 'hg topic --list'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     2
#
c8e4c6e03957 stack: add a very first version of stack display with 'hg topic --list'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     3
# This software may be used and distributed according to the terms of the
c8e4c6e03957 stack: add a very first version of stack display with 'hg topic --list'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     4
# GNU General Public License version 2 or any later version.
1897
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
     5
import collections
1895
c8e4c6e03957 stack: add a very first version of stack display with 'hg topic --list'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     6
from mercurial.i18n import _
1936
31583ddda6d9 stack: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1925
diff changeset
     7
from mercurial import (
31583ddda6d9 stack: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1925
diff changeset
     8
    error,
1961
d9c7fced94fc stack: prevent crash when topic is rooted on nullid
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1957
diff changeset
     9
    node,
1936
31583ddda6d9 stack: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1925
diff changeset
    10
    obsolete,
31583ddda6d9 stack: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1925
diff changeset
    11
)
1895
c8e4c6e03957 stack: add a very first version of stack display with 'hg topic --list'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    12
1904
f52c02bf47b7 stack: allow to refer to changeset using "t2" form
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1901
diff changeset
    13
def getstack(repo, topic):
1895
c8e4c6e03957 stack: add a very first version of stack display with 'hg topic --list'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    14
    # XXX need sorting
1897
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    15
    trevs = repo.revs("topic(%s) - obsolete()", topic)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    16
    return _orderrevs(repo, trevs)
1895
c8e4c6e03957 stack: add a very first version of stack display with 'hg topic --list'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    17
1907
95874e8fc5f2 stack: add basic formatter and label support
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1906
diff changeset
    18
def showstack(ui, repo, topic, opts):
1895
c8e4c6e03957 stack: add a very first version of stack display with 'hg topic --list'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    19
    if not topic:
c8e4c6e03957 stack: add a very first version of stack display with 'hg topic --list'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    20
        topic = repo.currenttopic
c8e4c6e03957 stack: add a very first version of stack display with 'hg topic --list'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    21
    if not topic:
c8e4c6e03957 stack: add a very first version of stack display with 'hg topic --list'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    22
        raise error.Abort(_('no active topic to list'))
1907
95874e8fc5f2 stack: add basic formatter and label support
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1906
diff changeset
    23
    fm = ui.formatter('topicstack', opts)
1909
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    24
    prev = None
1955
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    25
    entries = []
1954
61f36480740f stack: start indexing at 't1' instead of 't0'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1936
diff changeset
    26
    for idx, r in enumerate(getstack(repo, topic), 1):
1925
8f8a48a2e97d stack: whitespace
Sean Farley <sean@farley.io>
parents: 1924
diff changeset
    27
        ctx = repo[r]
1909
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    28
        p1 = ctx.p1()
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    29
        if p1.obsolete():
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    30
            p1 = repo[_singlesuccessor(repo, p1)]
1961
d9c7fced94fc stack: prevent crash when topic is rooted on nullid
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1957
diff changeset
    31
        if p1.rev() != prev and p1.node() != node.nullid:
1955
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    32
            entries.append((None, p1))
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    33
        entries.append((idx, ctx))
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    34
        prev = r
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    35
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    36
    # super crude initial version
1956
d8f1e432b16a stack: reverse the display order
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1955
diff changeset
    37
    for idx, ctx in entries[::-1]:
1955
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    38
        if idx is None:
1957
ea5553e47027 stack: change the ascii symbold for base
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1956
diff changeset
    39
            symbol = '^'
1955
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    40
            state = 'base'
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    41
        elif repo.revs('%d and parents()', ctx.rev()):
1906
5e9ce6310720 stack: show the currently active changeset and unstable ones
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1905
diff changeset
    42
            symbol = '@'
5e9ce6310720 stack: show the currently active changeset and unstable ones
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1905
diff changeset
    43
            state = 'current'
1955
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    44
        elif repo.revs('%d and unstable()', ctx.rev()):
1906
5e9ce6310720 stack: show the currently active changeset and unstable ones
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1905
diff changeset
    45
            symbol = '$'
5e9ce6310720 stack: show the currently active changeset and unstable ones
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1905
diff changeset
    46
            state = 'unstable'
1955
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    47
        else:
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    48
            symbol = ':'
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    49
            state = 'clean'
1907
95874e8fc5f2 stack: add basic formatter and label support
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1906
diff changeset
    50
        fm.startitem()
1955
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    51
        if idx is None:
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    52
            fm.plain('  ')
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    53
        else:
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    54
            fm.write('topic.stack.index', 't%d', idx,
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    55
                     label='topic.stack.index topic.stack.index.%s' % state)
1907
95874e8fc5f2 stack: add basic formatter and label support
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1906
diff changeset
    56
        fm.write('topic.stack.state.symbol', '%s', symbol,
95874e8fc5f2 stack: add basic formatter and label support
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1906
diff changeset
    57
                 label='topic.stack.state topic.stack.state.%s' % state)
95874e8fc5f2 stack: add basic formatter and label support
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1906
diff changeset
    58
        fm.plain(' ')
1909
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    59
        fm.write('topic.stack.desc', '%s', ctx.description().splitlines()[0],
1907
95874e8fc5f2 stack: add basic formatter and label support
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1906
diff changeset
    60
                 label='topic.stack.desc topic.stack.desc.%s' % state)
1955
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    61
        fm.condwrite(state != 'clean' and idx is not None, 'topic.stack.state',
5452a575b4e5 topic: extract display from entry computation
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1954
diff changeset
    62
                     ' (%s)', state,
1925
8f8a48a2e97d stack: whitespace
Sean Farley <sean@farley.io>
parents: 1924
diff changeset
    63
                     label='topic.stack.state topic.stack.state.%s' % state)
1907
95874e8fc5f2 stack: add basic formatter and label support
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1906
diff changeset
    64
        fm.plain('\n')
95874e8fc5f2 stack: add basic formatter and label support
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1906
diff changeset
    65
        fm.end()
1897
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    66
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    67
# Copied from evolve 081605c2e9b6
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    68
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    69
def _orderrevs(repo, revs):
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    70
    """Compute an ordering to solve instability for the given revs
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    71
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    72
    revs is a list of unstable revisions.
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    73
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    74
    Returns the same revisions ordered to solve their instability from the
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    75
    bottom to the top of the stack that the stabilization process will produce
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    76
    eventually.
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    77
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    78
    This ensures the minimal number of stabilizations, as we can stabilize each
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    79
    revision on its final stabilized destination.
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    80
    """
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    81
    # Step 1: Build the dependency graph
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    82
    dependencies, rdependencies = builddependencies(repo, revs)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    83
    # Step 2: Build the ordering
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    84
    # Remove the revisions with no dependency(A) and add them to the ordering.
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    85
    # Removing these revisions leads to new revisions with no dependency (the
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    86
    # one depending on A) that we can remove from the dependency graph and add
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    87
    # to the ordering. We progress in a similar fashion until the ordering is
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    88
    # built
1909
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    89
    solvablerevs = [r for r in sorted(dependencies.keys())
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    90
                    if not dependencies[r]]
1897
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    91
    ordering = []
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    92
    while solvablerevs:
1909
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    93
        rev = solvablerevs.pop()
1897
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    94
        for dependent in rdependencies[rev]:
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    95
            dependencies[dependent].remove(rev)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    96
            if not dependencies[dependent]:
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    97
                solvablerevs.append(dependent)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    98
        del dependencies[rev]
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    99
        ordering.append(rev)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   100
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   101
    ordering.extend(sorted(dependencies))
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   102
    return ordering
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   103
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   104
def builddependencies(repo, revs):
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   105
    """returns dependency graphs giving an order to solve instability of revs
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   106
    (see _orderrevs for more information on usage)"""
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   107
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   108
    # For each troubled revision we keep track of what instability if any should
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   109
    # be resolved in order to resolve it. Example:
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   110
    # dependencies = {3: [6], 6:[]}
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   111
    # Means that: 6 has no dependency, 3 depends on 6 to be solved
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   112
    dependencies = {}
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   113
    # rdependencies is the inverted dict of dependencies
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   114
    rdependencies = collections.defaultdict(set)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   115
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   116
    for r in revs:
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   117
        dependencies[r] = set()
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   118
        for p in repo[r].parents():
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   119
            try:
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   120
                succ = _singlesuccessor(repo, p)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   121
            except MultipleSuccessorsError as exc:
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   122
                dependencies[r] = exc.successorssets
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   123
                continue
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   124
            if succ in revs:
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   125
                dependencies[r].add(succ)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   126
                rdependencies[succ].add(r)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   127
    return dependencies, rdependencies
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   128
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   129
def _singlesuccessor(repo, p):
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   130
    """returns p (as rev) if not obsolete or its unique latest successors
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   131
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   132
    fail if there are no such successor"""
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   133
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   134
    if not p.obsolete():
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   135
        return p.rev()
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   136
    obs = repo[p]
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   137
    ui = repo.ui
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   138
    newer = obsolete.successorssets(repo, obs.node())
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   139
    # search of a parent which is not killed
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   140
    while not newer:
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   141
        ui.debug("stabilize target %s is plain dead,"
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   142
                 " trying to stabilize on its parent\n" %
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   143
                 obs)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   144
        obs = obs.parents()[0]
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   145
        newer = obsolete.successorssets(repo, obs.node())
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   146
    if len(newer) > 1 or len(newer[0]) > 1:
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   147
        raise MultipleSuccessorsError(newer)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   148
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   149
    return repo[newer[0][0]].rev()
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   150
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   151
class MultipleSuccessorsError(RuntimeError):
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   152
    """Exception raised by _singlesuccessor when multiple successor sets exists
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   153
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   154
    The object contains the list of successorssets in its 'successorssets'
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   155
    attribute to call to easily recover.
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   156
    """
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   157
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   158
    def __init__(self, successorssets):
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   159
        self.successorssets = successorssets