hgext3rd/topic/stack.py
author Pierre-Yves David <pierre-yves.david@fb.com>
Wed, 30 Mar 2016 22:03:44 -0700
changeset 1946 72246b13bd72
parent 1936 31583ddda6d9
child 1954 61f36480740f
permissions -rw-r--r--
patch: document the section about import/export
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,
31583ddda6d9 stack: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1925
diff changeset
     9
    obsolete,
31583ddda6d9 stack: move to new style import
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1925
diff changeset
    10
)
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
    11
1904
f52c02bf47b7 stack: allow to refer to changeset using "t2" form
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1901
diff changeset
    12
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
    13
    # 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
    14
    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
    15
    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
    16
1907
95874e8fc5f2 stack: add basic formatter and label support
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1906
diff changeset
    17
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
    18
    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
    19
        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
    20
    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
    21
        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
    22
    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
    23
    prev = None
1904
f52c02bf47b7 stack: allow to refer to changeset using "t2" form
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1901
diff changeset
    24
    for idx, r in enumerate(getstack(repo, topic)):
1925
8f8a48a2e97d stack: whitespace
Sean Farley <sean@farley.io>
parents: 1924
diff changeset
    25
        ctx = repo[r]
1909
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    26
        p1 = ctx.p1()
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    27
        if p1.obsolete():
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    28
            p1 = repo[_singlesuccessor(repo, p1)]
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    29
        if p1.rev() != prev:
1914
17782631d7e8 spelling: changeset
timeless@gmail.com
parents: 1909
diff changeset
    30
            # display the parent of the changeset
1909
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    31
            state = 'base'
1925
8f8a48a2e97d stack: whitespace
Sean Farley <sean@farley.io>
parents: 1924
diff changeset
    32
            symbol = '_'
1909
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    33
            fm.startitem()
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    34
            fm.plain('  ') # XXX 2 is the right padding by chance
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    35
            fm.write('topic.stack.state', '%s', symbol,
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    36
                     label='topic.stack.state topic.stack.state.%s' % state)
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    37
            fm.plain(' ')
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    38
            fm.write('topic.stack.desc', '%s',
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    39
                     p1.description().splitlines()[0],
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    40
                     label='topic.stack.desc topic.stack.desc.%s' % state)
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    41
            fm.plain('\n')
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    42
            fm.end()
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
    43
        # super crude initial version
1906
5e9ce6310720 stack: show the currently active changeset and unstable ones
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1905
diff changeset
    44
        symbol = ':'
5e9ce6310720 stack: show the currently active changeset and unstable ones
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1905
diff changeset
    45
        state = 'clean'
5e9ce6310720 stack: show the currently active changeset and unstable ones
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1905
diff changeset
    46
        if repo.revs('%d and parents()', r):
5e9ce6310720 stack: show the currently active changeset and unstable ones
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1905
diff changeset
    47
            symbol = '@'
5e9ce6310720 stack: show the currently active changeset and unstable ones
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1905
diff changeset
    48
            state = 'current'
5e9ce6310720 stack: show the currently active changeset and unstable ones
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1905
diff changeset
    49
        if repo.revs('%d and unstable()', r):
5e9ce6310720 stack: show the currently active changeset and unstable ones
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1905
diff changeset
    50
            symbol = '$'
5e9ce6310720 stack: show the currently active changeset and unstable ones
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1905
diff changeset
    51
            state = 'unstable'
1907
95874e8fc5f2 stack: add basic formatter and label support
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1906
diff changeset
    52
        fm.startitem()
95874e8fc5f2 stack: add basic formatter and label support
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1906
diff changeset
    53
        fm.write('topic.stack.index', 't%d', idx,
1925
8f8a48a2e97d stack: whitespace
Sean Farley <sean@farley.io>
parents: 1924
diff changeset
    54
                 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
    55
        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
    56
                 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
    57
        fm.plain(' ')
1909
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    58
        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
    59
                 label='topic.stack.desc topic.stack.desc.%s' % state)
95874e8fc5f2 stack: add basic formatter and label support
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1906
diff changeset
    60
        fm.condwrite(state != 'clean', 'topic.stack.state', ' (%s)', state,
1925
8f8a48a2e97d stack: whitespace
Sean Farley <sean@farley.io>
parents: 1924
diff changeset
    61
                     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
    62
        fm.plain('\n')
95874e8fc5f2 stack: add basic formatter and label support
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1906
diff changeset
    63
        fm.end()
1909
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    64
        prev = r
1897
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    65
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    66
# 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
    67
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    68
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
    69
    """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
    70
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    71
    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
    72
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    73
    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
    74
    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
    75
    eventually.
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    76
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    77
    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
    78
    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
    79
    """
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    80
    # 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
    81
    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
    82
    # 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
    83
    # 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
    84
    # 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
    85
    # 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
    86
    # 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
    87
    # built
1909
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    88
    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
    89
                    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
    90
    ordering = []
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    91
    while solvablerevs:
1909
36112e361ee4 stack: display the base of the stack
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1907
diff changeset
    92
        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
    93
        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
    94
            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
    95
            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
    96
                solvablerevs.append(dependent)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    97
        del dependencies[rev]
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    98
        ordering.append(rev)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
    99
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   100
    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
   101
    return ordering
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   102
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   103
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
   104
    """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
   105
    (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
   106
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   107
    # 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
   108
    # 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
   109
    # 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
   110
    # 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
   111
    dependencies = {}
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   112
    # 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
   113
    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
   114
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   115
    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
   116
        dependencies[r] = set()
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   117
        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
   118
            try:
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   119
                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
   120
            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
   121
                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
   122
                continue
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   123
            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
   124
                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
   125
                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
   126
    return dependencies, rdependencies
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   127
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   128
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
   129
    """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
   130
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   131
    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
   132
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   133
    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
   134
        return p.rev()
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   135
    obs = repo[p]
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   136
    ui = repo.ui
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   137
    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
   138
    # 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
   139
    while not newer:
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   140
        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
   141
                 " 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
   142
                 obs)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   143
        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
   144
        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
   145
    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
   146
        raise MultipleSuccessorsError(newer)
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   147
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   148
    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
   149
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   150
class MultipleSuccessorsError(RuntimeError):
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   151
    """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
   152
38570c53b1cf stack: fix printing order in case of unstability
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1896
diff changeset
   153
    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
   154
    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
   155
    """
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
    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
   158
        self.successorssets = successorssets