hgext3rd/topic/stack.py
author Pierre-Yves David <pierre-yves.david@fb.com>
Fri, 01 Apr 2016 16:40:19 -0700
changeset 1954 61f36480740f
parent 1936 31583ddda6d9
child 1955 5452a575b4e5
permissions -rw-r--r--
stack: start indexing at 't1' instead of 't0' I think 't0' could have special meaning (eg: before 't1', but still with topic activated) so let's keep room for that. Indexing from 1 is not crazy anyway.

# stack.py - code related to stack workflow
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
import collections
from mercurial.i18n import _
from mercurial import (
    error,
    obsolete,
)

def getstack(repo, topic):
    # XXX need sorting
    trevs = repo.revs("topic(%s) - obsolete()", topic)
    return _orderrevs(repo, trevs)

def showstack(ui, repo, topic, opts):
    if not topic:
        topic = repo.currenttopic
    if not topic:
        raise error.Abort(_('no active topic to list'))
    fm = ui.formatter('topicstack', opts)
    prev = None
    for idx, r in enumerate(getstack(repo, topic), 1):
        ctx = repo[r]
        p1 = ctx.p1()
        if p1.obsolete():
            p1 = repo[_singlesuccessor(repo, p1)]
        if p1.rev() != prev:
            # display the parent of the changeset
            state = 'base'
            symbol = '_'
            fm.startitem()
            fm.plain('  ') # XXX 2 is the right padding by chance
            fm.write('topic.stack.state', '%s', symbol,
                     label='topic.stack.state topic.stack.state.%s' % state)
            fm.plain(' ')
            fm.write('topic.stack.desc', '%s',
                     p1.description().splitlines()[0],
                     label='topic.stack.desc topic.stack.desc.%s' % state)
            fm.plain('\n')
            fm.end()
        # super crude initial version
        symbol = ':'
        state = 'clean'
        if repo.revs('%d and parents()', r):
            symbol = '@'
            state = 'current'
        if repo.revs('%d and unstable()', r):
            symbol = '$'
            state = 'unstable'
        fm.startitem()
        fm.write('topic.stack.index', 't%d', idx,
                 label='topic.stack.index topic.stack.index.%s' % state)
        fm.write('topic.stack.state.symbol', '%s', symbol,
                 label='topic.stack.state topic.stack.state.%s' % state)
        fm.plain(' ')
        fm.write('topic.stack.desc', '%s', ctx.description().splitlines()[0],
                 label='topic.stack.desc topic.stack.desc.%s' % state)
        fm.condwrite(state != 'clean', 'topic.stack.state', ' (%s)', state,
                     label='topic.stack.state topic.stack.state.%s' % state)
        fm.plain('\n')
        fm.end()
        prev = r

# Copied from evolve 081605c2e9b6

def _orderrevs(repo, revs):
    """Compute an ordering to solve instability for the given revs

    revs is a list of unstable revisions.

    Returns the same revisions ordered to solve their instability from the
    bottom to the top of the stack that the stabilization process will produce
    eventually.

    This ensures the minimal number of stabilizations, as we can stabilize each
    revision on its final stabilized destination.
    """
    # Step 1: Build the dependency graph
    dependencies, rdependencies = builddependencies(repo, revs)
    # Step 2: Build the ordering
    # Remove the revisions with no dependency(A) and add them to the ordering.
    # Removing these revisions leads to new revisions with no dependency (the
    # one depending on A) that we can remove from the dependency graph and add
    # to the ordering. We progress in a similar fashion until the ordering is
    # built
    solvablerevs = [r for r in sorted(dependencies.keys())
                    if not dependencies[r]]
    ordering = []
    while solvablerevs:
        rev = solvablerevs.pop()
        for dependent in rdependencies[rev]:
            dependencies[dependent].remove(rev)
            if not dependencies[dependent]:
                solvablerevs.append(dependent)
        del dependencies[rev]
        ordering.append(rev)

    ordering.extend(sorted(dependencies))
    return ordering

def builddependencies(repo, revs):
    """returns dependency graphs giving an order to solve instability of revs
    (see _orderrevs for more information on usage)"""

    # For each troubled revision we keep track of what instability if any should
    # be resolved in order to resolve it. Example:
    # dependencies = {3: [6], 6:[]}
    # Means that: 6 has no dependency, 3 depends on 6 to be solved
    dependencies = {}
    # rdependencies is the inverted dict of dependencies
    rdependencies = collections.defaultdict(set)

    for r in revs:
        dependencies[r] = set()
        for p in repo[r].parents():
            try:
                succ = _singlesuccessor(repo, p)
            except MultipleSuccessorsError as exc:
                dependencies[r] = exc.successorssets
                continue
            if succ in revs:
                dependencies[r].add(succ)
                rdependencies[succ].add(r)
    return dependencies, rdependencies

def _singlesuccessor(repo, p):
    """returns p (as rev) if not obsolete or its unique latest successors

    fail if there are no such successor"""

    if not p.obsolete():
        return p.rev()
    obs = repo[p]
    ui = repo.ui
    newer = obsolete.successorssets(repo, obs.node())
    # search of a parent which is not killed
    while not newer:
        ui.debug("stabilize target %s is plain dead,"
                 " trying to stabilize on its parent\n" %
                 obs)
        obs = obs.parents()[0]
        newer = obsolete.successorssets(repo, obs.node())
    if len(newer) > 1 or len(newer[0]) > 1:
        raise MultipleSuccessorsError(newer)

    return repo[newer[0][0]].rev()

class MultipleSuccessorsError(RuntimeError):
    """Exception raised by _singlesuccessor when multiple successor sets exists

    The object contains the list of successorssets in its 'successorssets'
    attribute to call to easily recover.
    """

    def __init__(self, successorssets):
        self.successorssets = successorssets