author | John Mulligan <phlogistonjohn@asynchrono.us> |
Wed, 14 Jan 2009 21:47:38 -0500 | |
changeset 7654 | 816b708f23af |
parent 7637 | 1d54e2f6c0b7 |
child 7873 | 4a4c7f6a5912 |
permissions | -rw-r--r-- |
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 |
# |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
6 |
# of the GNU General Public License, incorporated herein by reference. |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
7 |
'''\ |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
8 |
use suffixes to refer to ancestor revisions |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
9 |
|
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
10 |
This extension allows you to use git-style suffixes to refer to |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
11 |
the ancestors of a specific revision. |
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 |
|
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
15 |
- foo^N = Nth parent of foo: |
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 |
''' |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
26 |
import mercurial.repo |
7637 | 27 |
from mercurial import error |
5194
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
28 |
|
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
29 |
def reposetup(ui, repo): |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
30 |
if not repo.local(): |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
31 |
return |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
32 |
|
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
33 |
class parentrevspecrepo(repo.__class__): |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
34 |
def lookup(self, key): |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
35 |
try: |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
36 |
_super = super(parentrevspecrepo, self) |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
37 |
return _super.lookup(key) |
7637 | 38 |
except error.RepoError: |
5194
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
39 |
pass |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
40 |
|
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
41 |
circ = key.find('^') |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
42 |
tilde = key.find('~') |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
43 |
if circ < 0 and tilde < 0: |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
44 |
raise |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
45 |
elif circ >= 0 and tilde >= 0: |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
46 |
end = min(circ, tilde) |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
47 |
else: |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
48 |
end = max(circ, tilde) |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
49 |
|
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
50 |
cl = self.changelog |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
51 |
base = key[:end] |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
52 |
try: |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
53 |
node = _super.lookup(base) |
7637 | 54 |
except error.RepoError: |
5194
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
55 |
# eek - reraise the first error |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
56 |
return _super.lookup(key) |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
57 |
|
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
58 |
rev = cl.rev(node) |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
59 |
suffix = key[end:] |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
60 |
i = 0 |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
61 |
while i < len(suffix): |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
62 |
# foo^N => Nth parent of foo |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
63 |
# foo^0 == foo |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
64 |
# foo^1 == foo^ == 1st parent of foo |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
65 |
# foo^2 == 2nd parent of foo |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
66 |
if suffix[i] == '^': |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
67 |
j = i + 1 |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
68 |
p = cl.parentrevs(rev) |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
69 |
if j < len(suffix) and suffix[j].isdigit(): |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
70 |
j += 1 |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
71 |
n = int(suffix[i+1:j]) |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
72 |
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
|
73 |
raise |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
74 |
else: |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
75 |
n = 1 |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
76 |
if n: |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
77 |
rev = p[n - 1] |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
78 |
i = j |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
79 |
# foo~N => Nth first grandparent of foo |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
80 |
# foo~0 = foo |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
81 |
# foo~1 = foo^1 == foo^ == 1st parent of foo |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
82 |
# 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
|
83 |
elif suffix[i] == '~': |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
84 |
j = i + 1 |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
85 |
while j < len(suffix) and suffix[j].isdigit(): |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
86 |
j += 1 |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
87 |
if j == i + 1: |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
88 |
raise |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
89 |
n = int(suffix[i+1:j]) |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
90 |
for k in xrange(n): |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
91 |
rev = cl.parentrevs(rev)[0] |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
92 |
i = j |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
93 |
else: |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
94 |
raise |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
95 |
return cl.node(rev) |
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
96 |
|
b111e9a907b1
Add parentrevspec extension
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
diff
changeset
|
97 |
repo.__class__ = parentrevspecrepo |