Mercurial > hg-stable
annotate mercurial/graphmod.py @ 8837:d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
author | Peter Arrenbrecht <peter.arrenbrecht@gmail.com> |
---|---|
date | Fri, 19 Jun 2009 13:14:45 +0200 |
parents | 11ff34956ee7 |
children | d9acbe7b0049 |
rev | line source |
---|---|
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 |
7873
4a4c7f6a5912
cleanup: drop unused imports
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7565
diff
changeset
|
9 from node import nullrev |
6691 | 10 |
8836
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
11 def revisions(repo, start, stop): |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
12 """cset DAG generator yielding (rev, node, [parents]) tuples |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
13 |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
14 This generator function walks through the revision history from revision |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
15 start to revision stop (which must be less than or equal to start). |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
16 """ |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
17 assert start >= stop |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
18 cur = start |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
19 while cur >= stop: |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
20 ctx = repo[cur] |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
21 parents = [p.rev() for p in ctx.parents() if p.rev() != nullrev] |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
22 parents.sort() |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
23 yield (ctx, parents) |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
24 cur -= 1 |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
25 |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
26 def filerevs(repo, path, start, stop): |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
27 """file cset DAG generator yielding (rev, node, [parents]) tuples |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
28 |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
29 This generator function walks through the revision history of a single |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
30 file from revision start to revision stop (which must be less than or |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
31 equal to start). |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
32 """ |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
33 assert start >= stop |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
34 filerev = len(repo.file(path)) - 1 |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
35 while filerev >= 0: |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
36 fctx = repo.filectx(path, fileid=filerev) |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
37 parents = [f.linkrev() for f in fctx.parents() if f.path() == path] |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
38 parents.sort() |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
39 if fctx.rev() <= start: |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
40 yield (fctx, parents) |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
41 if fctx.rev() <= stop: |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
42 break |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
43 filerev -= 1 |
11ff34956ee7
graphmod/graphlog: move log walks to graphmod
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8835
diff
changeset
|
44 |
8837
d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8836
diff
changeset
|
45 def nodes(repo, nodes): |
d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8836
diff
changeset
|
46 include = set(nodes) |
d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8836
diff
changeset
|
47 for node in nodes: |
d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8836
diff
changeset
|
48 ctx = repo[node] |
d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8836
diff
changeset
|
49 parents = [p.rev() for p in ctx.parents() if p.node() in include] |
d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8836
diff
changeset
|
50 parents.sort() |
d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8836
diff
changeset
|
51 yield (ctx, parents) |
d8e3a98018cb
graphmod/graphlog: extract nodelistwalk
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8836
diff
changeset
|
52 |
6691 | 53 def graph(repo, start_rev, stop_rev): |
54 """incremental revision grapher | |
55 | |
56 This generator function walks through the revision history from | |
57 revision start_rev to revision stop_rev (which must be less than | |
58 or equal to start_rev) and for each revision emits tuples with the | |
59 following elements: | |
60 | |
8835
ec5483efc31f
graphmod: code cleanup and doc fix
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8225
diff
changeset
|
61 - Context of the current node |
ec5483efc31f
graphmod: code cleanup and doc fix
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
8225
diff
changeset
|
62 - Tuple (col, color) with column and color index for the current node |
6691 | 63 - Edges; a list of (col, next_col, color) indicating the edges between |
64 the current node and its parents. | |
65 """ | |
66 | |
7565
5f162f61e479
hgweb: fix problems with empty repositories
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7280
diff
changeset
|
67 if start_rev == nullrev and not stop_rev: |
5f162f61e479
hgweb: fix problems with empty repositories
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7280
diff
changeset
|
68 return |
5f162f61e479
hgweb: fix problems with empty repositories
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7280
diff
changeset
|
69 |
6691 | 70 assert start_rev >= stop_rev |
7030
20a5dd5d6dd9
hgweb: let the web graph cope with low revisions/new repositories (issue1293)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6747
diff
changeset
|
71 assert stop_rev >= 0 |
6691 | 72 curr_rev = start_rev |
73 revs = [] | |
74 cl = repo.changelog | |
75 colors = {} | |
76 new_color = 1 | |
77 | |
78 while curr_rev >= stop_rev: | |
79 # Compute revs and next_revs | |
80 if curr_rev not in revs: | |
81 revs.append(curr_rev) # new head | |
82 colors[curr_rev] = new_color | |
83 new_color += 1 | |
84 | |
85 idx = revs.index(curr_rev) | |
86 color = colors.pop(curr_rev) | |
87 next = revs[:] | |
88 | |
89 # Add parents to next_revs | |
90 parents = [x for x in cl.parentrevs(curr_rev) if x != nullrev] | |
91 addparents = [p for p in parents if p not in next] | |
92 next[idx:idx + 1] = addparents | |
93 | |
94 # Set colors for the parents | |
95 for i, p in enumerate(addparents): | |
96 if not i: | |
97 colors[p] = color | |
98 else: | |
99 colors[p] = new_color | |
100 new_color += 1 | |
101 | |
102 # Add edges to the graph | |
103 edges = [] | |
104 for col, r in enumerate(revs): | |
105 if r in next: | |
106 edges.append((col, next.index(r), colors[r])) | |
107 elif r == curr_rev: | |
108 for p in parents: | |
109 edges.append((col, next.index(p), colors[p])) | |
110 | |
111 # Yield and move on | |
6747
f6c00b17387c
use repo[changeid] to get a changectx
Matt Mackall <mpm@selenic.com>
parents:
6691
diff
changeset
|
112 yield (repo[curr_rev], (idx, color), edges) |
6691 | 113 revs = next |
114 curr_rev -= 1 |