hgext/parentrevspec.py
author Cédric Duval <cedricduval@free.fr>
Mon, 22 Jun 2009 15:48:08 +0200
changeset 8894 868670dbc237
parent 8873 e872ef2e6758
child 9070 1933220e0284
permissions -rw-r--r--
extensions: improve the consistency of synopses Trying as much as possible to consistently: - use a present tense predicate followed by a direct object - verb referring directly to the functionality provided (ie. not "add command that does this" but simple "do that") - keep simple and to the point, leaving details for the long help (width is tight, possibly even more so for translations) Thanks to timeless, Martin Geisler, Rafael Villar Burke, Dan Villiom Podlaski Christiansen and others for the helpful suggestions.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     1
# Mercurial extension to make it easy to refer to the parent of a revision
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     2
#
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     3
# Copyright (C) 2007 Alexis S. L. Carvalho <alexis@cecm.usp.br>
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 7996
diff changeset
     5
# This software may be used and distributed according to the terms of the
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 7996
diff changeset
     6
# GNU General Public License version 2, incorporated herein by reference.
8228
eee2319c5895 add blank line after copyright notices and after header
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
     7
8894
868670dbc237 extensions: improve the consistency of synopses
Cédric Duval <cedricduval@free.fr>
parents: 8873
diff changeset
     8
'''interpret suffixes to refer to ancestor revisions
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
     9
7996
a999dbc915f2 parentrevspec: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7873
diff changeset
    10
This extension allows you to use git-style suffixes to refer to the
a999dbc915f2 parentrevspec: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7873
diff changeset
    11
ancestors of a specific revision.
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    12
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    13
For example, if you can refer to a revision as "foo", then:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    14
8618
cf6f567e27e9 parentrevspec: remove a trailing colon
Cédric Duval <cedricduval@free.fr>
parents: 8228
diff changeset
    15
- foo^N = Nth parent of foo
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    16
  foo^0 = foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    17
  foo^1 = first parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    18
  foo^2 = second parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    19
  foo^  = foo^1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    20
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    21
- foo~N = Nth first grandparent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    22
  foo~0 = foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    23
  foo~1 = foo^1 = foo^ = first parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    24
  foo~2 = foo^1^1 = foo^^ = first parent of first parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    25
'''
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 5194
diff changeset
    26
from mercurial import error
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    27
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    28
def reposetup(ui, repo):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    29
    if not repo.local():
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    30
        return
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    31
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    32
    class parentrevspecrepo(repo.__class__):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    33
        def lookup(self, key):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    34
            try:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    35
                _super = super(parentrevspecrepo, self)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    36
                return _super.lookup(key)
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 5194
diff changeset
    37
            except error.RepoError:
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    38
                pass
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    39
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    40
            circ = key.find('^')
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    41
            tilde = key.find('~')
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    42
            if circ < 0 and tilde < 0:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    43
                raise
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    44
            elif circ >= 0 and tilde >= 0:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    45
                end = min(circ, tilde)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    46
            else:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    47
                end = max(circ, tilde)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    48
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    49
            cl = self.changelog
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    50
            base = key[:end]
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    51
            try:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    52
                node = _super.lookup(base)
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 5194
diff changeset
    53
            except error.RepoError:
5194
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    54
                # eek - reraise the first error
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    55
                return _super.lookup(key)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    56
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    57
            rev = cl.rev(node)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    58
            suffix = key[end:]
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    59
            i = 0
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    60
            while i < len(suffix):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    61
                # foo^N => Nth parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    62
                # foo^0 == foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    63
                # foo^1 == foo^ == 1st parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    64
                # foo^2 == 2nd parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    65
                if suffix[i] == '^':
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    66
                    j = i + 1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    67
                    p = cl.parentrevs(rev)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    68
                    if j < len(suffix) and suffix[j].isdigit():
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    69
                        j += 1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    70
                        n = int(suffix[i+1:j])
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    71
                        if n > 2 or n == 2 and p[1] == -1:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    72
                            raise
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    73
                    else:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    74
                        n = 1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    75
                    if n:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    76
                        rev = p[n - 1]
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    77
                    i = j
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    78
                # foo~N => Nth first grandparent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    79
                # foo~0 = foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    80
                # foo~1 = foo^1 == foo^ == 1st parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    81
                # foo~2 = foo^1^1 == foo^^ == 1st parent of 1st parent of foo
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    82
                elif suffix[i] == '~':
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    83
                    j = i + 1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    84
                    while j < len(suffix) and suffix[j].isdigit():
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    85
                        j += 1
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    86
                    if j == i + 1:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    87
                        raise
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    88
                    n = int(suffix[i+1:j])
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    89
                    for k in xrange(n):
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    90
                        rev = cl.parentrevs(rev)[0]
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    91
                    i = j
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    92
                else:
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    93
                    raise
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    94
            return cl.node(rev)
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    95
b111e9a907b1 Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff changeset
    96
    repo.__class__ = parentrevspecrepo