author | Wagner Bruna <wbruna@softwareexpress.com.br> |
Mon, 16 Nov 2009 14:37:31 -0200 | |
changeset 9865 | 1a2bc8e55396 |
parent 9727 | d00cee04a746 |
child 10084 | 4c844f16bf39 |
permissions | -rw-r--r-- |
6691 | 1 |
# Revision graph generator for Mercurial |
2 |
# |
|
3 |
# Copyright 2008 Dirkjan Ochtman <dirkjan@ochtman.nl> |
|
4 |
# Copyright 2007 Joel Rosdahl <joel@rosdahl.net> |
|
5 |
# |
|
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
7873
diff
changeset
|
6 |
# 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:
7873
diff
changeset
|
7 |
# GNU General Public License version 2, incorporated herein by reference. |
6691 | 8 |
|
8840
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
9 |
"""supports walking the history as DAGs suitable for graphical output |
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
10 |
|
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
11 |
The most basic format we use is that of:: |
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
12 |
|
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
13 |
(id, type, data, [parentids]) |
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
14 |
|
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
15 |
The node and parent ids are arbitrary integers which identify a node in the |
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
16 |
context of the graph returned. Type is a constant specifying the node type. |
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
17 |
Data depends on type. |
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
18 |
""" |
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
19 |
|
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
20 |
from mercurial.node import nullrev |
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
21 |
|
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
22 |
CHANGESET = 'C' |
6691 | 23 |
|
8836
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
24 |
def revisions(repo, start, stop): |
8840
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
25 |
"""cset DAG generator yielding (id, CHANGESET, ctx, [parentids]) tuples |
8836
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
26 |
|
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
27 |
This generator function walks through the revision history from revision |
8840
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
28 |
start to revision stop (which must be less than or equal to start). It |
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
29 |
returns a tuple for each node. The node and parent ids are arbitrary |
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
30 |
integers which identify a node in the context of the graph returned. |
8836
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
31 |
""" |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
32 |
cur = start |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
33 |
while cur >= stop: |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
34 |
ctx = repo[cur] |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
35 |
parents = [p.rev() for p in ctx.parents() if p.rev() != nullrev] |
8840
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
36 |
yield (cur, CHANGESET, ctx, sorted(parents)) |
8836
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
37 |
cur -= 1 |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
38 |
|
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
39 |
def filerevs(repo, path, start, stop): |
8840
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
40 |
"""file cset DAG generator yielding (id, CHANGESET, ctx, [parentids]) tuples |
8836
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
41 |
|
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
42 |
This generator function walks through the revision history of a single |
8842
acd03a6e2426
graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8841
diff
changeset
|
43 |
file from revision start down to revision stop. |
8836
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
44 |
""" |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
45 |
filerev = len(repo.file(path)) - 1 |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
46 |
while filerev >= 0: |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
47 |
fctx = repo.filectx(path, fileid=filerev) |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
48 |
parents = [f.linkrev() for f in fctx.parents() if f.path() == path] |
8840
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
49 |
rev = fctx.rev() |
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
50 |
if rev <= start: |
9727
d00cee04a746
graphlog: don't pass filectxs to the templater (issue1896)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8842
diff
changeset
|
51 |
yield (rev, CHANGESET, fctx.changectx(), sorted(parents)) |
8840
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
52 |
if rev <= stop: |
8836
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
53 |
break |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
54 |
filerev -= 1 |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
55 |
|
8837
d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8836
diff
changeset
|
56 |
def nodes(repo, nodes): |
8840
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
57 |
"""cset DAG generator yielding (id, CHANGESET, ctx, [parentids]) tuples |
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
58 |
|
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
59 |
This generator function walks the given nodes. It only returns parents |
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
60 |
that are in nodes, too. |
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
61 |
""" |
8837
d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8836
diff
changeset
|
62 |
include = set(nodes) |
d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8836
diff
changeset
|
63 |
for node in nodes: |
d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8836
diff
changeset
|
64 |
ctx = repo[node] |
d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8836
diff
changeset
|
65 |
parents = [p.rev() for p in ctx.parents() if p.node() in include] |
8840
d9acbe7b0049
graphmod/graphlog: make dag walks carry data as type, payload
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8837
diff
changeset
|
66 |
yield (ctx.rev(), CHANGESET, ctx, sorted(parents)) |
8837
d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8836
diff
changeset
|
67 |
|
8842
acd03a6e2426
graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8841
diff
changeset
|
68 |
def colored(dag): |
acd03a6e2426
graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8841
diff
changeset
|
69 |
"""annotates a DAG with colored edge information |
acd03a6e2426
graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8841
diff
changeset
|
70 |
|
acd03a6e2426
graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8841
diff
changeset
|
71 |
For each DAG node this function emits tuples:: |
6691 | 72 |
|
8842
acd03a6e2426
graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8841
diff
changeset
|
73 |
(id, type, data, (col, color), [(col, nextcol, color)]) |
6691 | 74 |
|
8842
acd03a6e2426
graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8841
diff
changeset
|
75 |
with the following new elements: |
acd03a6e2426
graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8841
diff
changeset
|
76 |
|
8835
ec5483efc31f
graphmod: code cleanup and doc fix
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8225
diff
changeset
|
77 |
- Tuple (col, color) with column and color index for the current node |
8842
acd03a6e2426
graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8841
diff
changeset
|
78 |
- A list of tuples indicating the edges between the current node and its |
acd03a6e2426
graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8841
diff
changeset
|
79 |
parents. |
6691 | 80 |
""" |
8841
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
81 |
seen = [] |
6691 | 82 |
colors = {} |
8841
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
83 |
newcolor = 1 |
8842
acd03a6e2426
graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8841
diff
changeset
|
84 |
for (cur, type, data, parents) in dag: |
6691 | 85 |
|
8841
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
86 |
# Compute seen and next |
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
87 |
if cur not in seen: |
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
88 |
seen.append(cur) # new head |
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
89 |
colors[cur] = newcolor |
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
90 |
newcolor += 1 |
6691 | 91 |
|
8841
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
92 |
col = seen.index(cur) |
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
93 |
color = colors.pop(cur) |
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
94 |
next = seen[:] |
6691 | 95 |
|
8842
acd03a6e2426
graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8841
diff
changeset
|
96 |
# Add parents to next |
6691 | 97 |
addparents = [p for p in parents if p not in next] |
8841
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
98 |
next[col:col + 1] = addparents |
6691 | 99 |
|
100 |
# Set colors for the parents |
|
101 |
for i, p in enumerate(addparents): |
|
102 |
if not i: |
|
103 |
colors[p] = color |
|
104 |
else: |
|
8841
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
105 |
colors[p] = newcolor |
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
106 |
newcolor += 1 |
6691 | 107 |
|
108 |
# Add edges to the graph |
|
109 |
edges = [] |
|
8841
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
110 |
for ecol, eid in enumerate(seen): |
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
111 |
if eid in next: |
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
112 |
edges.append((ecol, next.index(eid), colors[eid])) |
8842
acd03a6e2426
graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8841
diff
changeset
|
113 |
elif eid == cur: |
6691 | 114 |
for p in parents: |
8841
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
115 |
edges.append((ecol, next.index(p), colors[p])) |
6691 | 116 |
|
117 |
# Yield and move on |
|
8842
acd03a6e2426
graphmod/webcommands: use generic DAG walks
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8841
diff
changeset
|
118 |
yield (cur, type, data, (col, color), edges) |
8841
94ac080e7af9
graphmod: rename a bunch of vars in graph()
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8840
diff
changeset
|
119 |
seen = next |