Mercurial > hg
annotate hgext/graphlog.py @ 6335:e29557d687c9
hgweb: only accept POST requests for unbundle
author | Dirkjan Ochtman <dirkjan@ochtman.nl> |
---|---|
date | Fri, 21 Mar 2008 00:55:53 +0100 |
parents | e75aab656f46 |
children | 53465a7464e2 d76f64cd4cb2 |
rev | line source |
---|---|
4344 | 1 # ASCII graph log extension for Mercurial |
2 # | |
3 # Copyright 2007 Joel Rosdahl <joel@rosdahl.net> | |
4516
96d8a56d4ef9
Removed trailing whitespace and tabs from python files
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4509
diff
changeset
|
4 # |
4344 | 5 # This software may be used and distributed according to the terms of |
6 # the GNU General Public License, incorporated herein by reference. | |
7 | |
5938
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
8 import os |
4344 | 9 import sys |
10 from mercurial.cmdutil import revrange, show_changeset | |
6192
cd65a67aff31
Introduce templateopts and logopts to reduce duplicate option definitions.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5968
diff
changeset
|
11 from mercurial.commands import templateopts |
4344 | 12 from mercurial.i18n import _ |
6212 | 13 from mercurial.node import nullrev |
5938
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
14 from mercurial.util import Abort, canonpath |
4344 | 15 |
16 def revision_grapher(repo, start_rev, stop_rev): | |
17 """incremental revision grapher | |
18 | |
19 This generator function walks through the revision history from | |
20 revision start_rev to revision stop_rev (which must be less than | |
21 or equal to start_rev) and for each revision emits tuples with the | |
22 following elements: | |
23 | |
24 - Current revision. | |
25 - Current node. | |
26 - Column of the current node in the set of ongoing edges. | |
27 - Edges; a list of (col, next_col) indicating the edges between | |
28 the current node and its parents. | |
29 - Number of columns (ongoing edges) in the current revision. | |
30 - The difference between the number of columns (ongoing edges) | |
31 in the next revision and the number of columns (ongoing edges) | |
32 in the current revision. That is: -1 means one column removed; | |
33 0 means no columns added or removed; 1 means one column added. | |
34 """ | |
35 | |
36 assert start_rev >= stop_rev | |
37 curr_rev = start_rev | |
38 revs = [] | |
39 while curr_rev >= stop_rev: | |
40 node = repo.changelog.node(curr_rev) | |
41 | |
42 # Compute revs and next_revs. | |
43 if curr_rev not in revs: | |
44 # New head. | |
45 revs.append(curr_rev) | |
46 rev_index = revs.index(curr_rev) | |
47 next_revs = revs[:] | |
48 | |
49 # Add parents to next_revs. | |
50 parents = get_rev_parents(repo, curr_rev) | |
51 parents_to_add = [] | |
52 for parent in parents: | |
53 if parent not in next_revs: | |
54 parents_to_add.append(parent) | |
55 parents_to_add.sort() | |
56 next_revs[rev_index:rev_index + 1] = parents_to_add | |
57 | |
58 edges = [] | |
59 for parent in parents: | |
60 edges.append((rev_index, next_revs.index(parent))) | |
61 | |
62 n_columns_diff = len(next_revs) - len(revs) | |
63 yield (curr_rev, node, rev_index, edges, len(revs), n_columns_diff) | |
64 | |
65 revs = next_revs | |
66 curr_rev -= 1 | |
67 | |
5938
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
68 def filelog_grapher(repo, path, start_rev, stop_rev): |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
69 """incremental file log grapher |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
70 |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
71 This generator function walks through the revision history of a |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
72 single file from revision start_rev to revision stop_rev (which must |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
73 be less than or equal to start_rev) and for each revision emits |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
74 tuples with the following elements: |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
75 |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
76 - Current revision. |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
77 - Current node. |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
78 - Column of the current node in the set of ongoing edges. |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
79 - Edges; a list of (col, next_col) indicating the edges between |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
80 the current node and its parents. |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
81 - Number of columns (ongoing edges) in the current revision. |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
82 - The difference between the number of columns (ongoing edges) |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
83 in the next revision and the number of columns (ongoing edges) |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
84 in the current revision. That is: -1 means one column removed; |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
85 0 means no columns added or removed; 1 means one column added. |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
86 """ |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
87 |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
88 assert start_rev >= stop_rev |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
89 curr_rev = start_rev |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
90 revs = [] |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
91 filerev = repo.file(path).count() - 1 |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
92 while filerev >= 0: |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
93 fctx = repo.filectx(path, fileid=filerev) |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
94 |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
95 # Compute revs and next_revs. |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
96 if filerev not in revs: |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
97 revs.append(filerev) |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
98 rev_index = revs.index(filerev) |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
99 next_revs = revs[:] |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
100 |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
101 # Add parents to next_revs. |
5968
6dcc190ffc36
graphlog: skip filectx parents in other filelogs
Steve Borho <steve@borho.org>
parents:
5942
diff
changeset
|
102 parents = [f.filerev() for f in fctx.parents() if f.path() == path] |
5938
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
103 parents_to_add = [] |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
104 for parent in parents: |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
105 if parent not in next_revs: |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
106 parents_to_add.append(parent) |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
107 parents_to_add.sort() |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
108 next_revs[rev_index:rev_index + 1] = parents_to_add |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
109 |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
110 edges = [] |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
111 for parent in parents: |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
112 edges.append((rev_index, next_revs.index(parent))) |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
113 |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
114 changerev = fctx.linkrev() |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
115 if changerev <= start_rev: |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
116 node = repo.changelog.node(changerev) |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
117 n_columns_diff = len(next_revs) - len(revs) |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
118 yield (changerev, node, rev_index, edges, len(revs), n_columns_diff) |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
119 if changerev <= stop_rev: |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
120 break |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
121 revs = next_revs |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
122 filerev -= 1 |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
123 |
4344 | 124 def get_rev_parents(repo, rev): |
125 return [x for x in repo.changelog.parentrevs(rev) if x != nullrev] | |
126 | |
127 def fix_long_right_edges(edges): | |
128 for (i, (start, end)) in enumerate(edges): | |
129 if end > start: | |
130 edges[i] = (start, end + 1) | |
131 | |
132 def draw_edges(edges, nodeline, interline): | |
133 for (start, end) in edges: | |
134 if start == end + 1: | |
135 interline[2 * end + 1] = "/" | |
136 elif start == end - 1: | |
137 interline[2 * start + 1] = "\\" | |
138 elif start == end: | |
139 interline[2 * start] = "|" | |
140 else: | |
141 nodeline[2 * end] = "+" | |
142 if start > end: | |
143 (start, end) = (end,start) | |
144 for i in range(2 * start + 1, 2 * end): | |
145 if nodeline[i] != "+": | |
146 nodeline[i] = "-" | |
147 | |
148 def format_line(line, level, logstr): | |
149 text = "%-*s %s" % (2 * level, "".join(line), logstr) | |
150 return "%s\n" % text.rstrip() | |
151 | |
152 def get_nodeline_edges_tail( | |
153 node_index, p_node_index, n_columns, n_columns_diff, p_diff, fix_tail): | |
154 if fix_tail and n_columns_diff == p_diff and n_columns_diff != 0: | |
155 # Still going in the same non-vertical direction. | |
156 if n_columns_diff == -1: | |
157 start = max(node_index + 1, p_node_index) | |
158 tail = ["|", " "] * (start - node_index - 1) | |
159 tail.extend(["/", " "] * (n_columns - start)) | |
160 return tail | |
161 else: | |
162 return ["\\", " "] * (n_columns - node_index - 1) | |
163 else: | |
164 return ["|", " "] * (n_columns - node_index - 1) | |
165 | |
166 def get_padding_line(ni, n_columns, edges): | |
167 line = [] | |
168 line.extend(["|", " "] * ni) | |
169 if (ni, ni - 1) in edges or (ni, ni) in edges: | |
170 # (ni, ni - 1) (ni, ni) | |
171 # | | | | | | | | | |
172 # +---o | | o---+ | |
173 # | | c | | c | | | |
174 # | |/ / | |/ / | |
175 # | | | | | | | |
176 c = "|" | |
177 else: | |
178 c = " " | |
179 line.extend([c, " "]) | |
180 line.extend(["|", " "] * (n_columns - ni - 1)) | |
181 return line | |
182 | |
183 def get_limit(limit_opt): | |
184 if limit_opt: | |
185 try: | |
186 limit = int(limit_opt) | |
187 except ValueError: | |
188 raise Abort(_("limit must be a positive integer")) | |
189 if limit <= 0: | |
190 raise Abort(_("limit must be positive")) | |
191 else: | |
192 limit = sys.maxint | |
193 return limit | |
194 | |
195 def get_revs(repo, rev_opt): | |
196 if rev_opt: | |
197 revs = revrange(repo, rev_opt) | |
198 return (max(revs), min(revs)) | |
199 else: | |
200 return (repo.changelog.count() - 1, 0) | |
201 | |
5938
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
202 def graphlog(ui, repo, path=None, **opts): |
4344 | 203 """show revision history alongside an ASCII revision graph |
204 | |
205 Print a revision history alongside a revision graph drawn with | |
206 ASCII characters. | |
207 | |
4583
11cf78983961
Reverted changesets 9d1380e5c8c5 and 1d46169ec197: show @ as glog parent again.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4516
diff
changeset
|
208 Nodes printed as an @ character are parents of the working |
4344 | 209 directory. |
210 """ | |
211 | |
212 limit = get_limit(opts["limit"]) | |
213 (start_rev, stop_rev) = get_revs(repo, opts["rev"]) | |
214 stop_rev = max(stop_rev, start_rev - limit + 1) | |
215 if start_rev == nullrev: | |
216 return | |
217 cs_printer = show_changeset(ui, repo, opts) | |
5938
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
218 if path: |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
219 cpath = canonpath(repo.root, os.getcwd(), path) |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
220 grapher = filelog_grapher(repo, cpath, start_rev, stop_rev) |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
221 else: |
9ed100559851
graphlog: add filelog revision grapher
Steve Borho <steve@borho.org>
parents:
4735
diff
changeset
|
222 grapher = revision_grapher(repo, start_rev, stop_rev) |
4344 | 223 repo_parents = repo.dirstate.parents() |
224 prev_n_columns_diff = 0 | |
225 prev_node_index = 0 | |
226 | |
227 for (rev, node, node_index, edges, n_columns, n_columns_diff) in grapher: | |
228 # log_strings is the list of all log strings to draw alongside | |
229 # the graph. | |
230 ui.pushbuffer() | |
231 cs_printer.show(rev, node) | |
232 log_strings = ui.popbuffer().split("\n")[:-1] | |
233 | |
234 if n_columns_diff == -1: | |
235 # Transform | |
236 # | |
237 # | | | | | | | |
238 # o | | into o---+ | |
239 # |X / |/ / | |
240 # | | | | | |
241 fix_long_right_edges(edges) | |
242 | |
243 # add_padding_line says whether to rewrite | |
244 # | |
245 # | | | | | | | | | |
246 # | o---+ into | o---+ | |
247 # | / / | | | # <--- padding line | |
248 # o | | | / / | |
249 # o | | | |
4633
ff7253a0d1da
Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4583
diff
changeset
|
250 add_padding_line = (len(log_strings) > 2 and |
ff7253a0d1da
Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4583
diff
changeset
|
251 n_columns_diff == -1 and |
ff7253a0d1da
Cleanup of whitespace, indentation and line continuation.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4583
diff
changeset
|
252 [x for (x, y) in edges if x + 1 < y]) |
4344 | 253 |
254 # fix_nodeline_tail says whether to rewrite | |
255 # | |
256 # | | o | | | | o | | | |
257 # | | |/ / | | |/ / | |
258 # | o | | into | o / / # <--- fixed nodeline tail | |
259 # | |/ / | |/ / | |
260 # o | | o | | | |
261 fix_nodeline_tail = len(log_strings) <= 2 and not add_padding_line | |
262 | |
4583
11cf78983961
Reverted changesets 9d1380e5c8c5 and 1d46169ec197: show @ as glog parent again.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4516
diff
changeset
|
263 # nodeline is the line containing the node character (@ or o). |
4344 | 264 nodeline = ["|", " "] * node_index |
265 if node in repo_parents: | |
4583
11cf78983961
Reverted changesets 9d1380e5c8c5 and 1d46169ec197: show @ as glog parent again.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4516
diff
changeset
|
266 node_ch = "@" |
4344 | 267 else: |
268 node_ch = "o" | |
269 nodeline.extend([node_ch, " "]) | |
270 | |
271 nodeline.extend( | |
272 get_nodeline_edges_tail( | |
273 node_index, prev_node_index, n_columns, n_columns_diff, | |
274 prev_n_columns_diff, fix_nodeline_tail)) | |
275 | |
276 # shift_interline is the line containing the non-vertical | |
277 # edges between this entry and the next. | |
278 shift_interline = ["|", " "] * node_index | |
279 if n_columns_diff == -1: | |
280 n_spaces = 1 | |
281 edge_ch = "/" | |
282 elif n_columns_diff == 0: | |
283 n_spaces = 2 | |
284 edge_ch = "|" | |
285 else: | |
286 n_spaces = 3 | |
287 edge_ch = "\\" | |
288 shift_interline.extend(n_spaces * [" "]) | |
289 shift_interline.extend([edge_ch, " "] * (n_columns - node_index - 1)) | |
290 | |
291 # Draw edges from the current node to its parents. | |
292 draw_edges(edges, nodeline, shift_interline) | |
293 | |
294 # lines is the list of all graph lines to print. | |
295 lines = [nodeline] | |
296 if add_padding_line: | |
297 lines.append(get_padding_line(node_index, n_columns, edges)) | |
298 lines.append(shift_interline) | |
299 | |
300 # Make sure that there are as many graph lines as there are | |
301 # log strings. | |
302 while len(log_strings) < len(lines): | |
303 log_strings.append("") | |
304 if len(lines) < len(log_strings): | |
305 extra_interline = ["|", " "] * (n_columns + n_columns_diff) | |
306 while len(lines) < len(log_strings): | |
307 lines.append(extra_interline) | |
308 | |
309 # Print lines. | |
310 indentation_level = max(n_columns, n_columns + n_columns_diff) | |
311 for (line, logstr) in zip(lines, log_strings): | |
312 ui.write(format_line(line, indentation_level, logstr)) | |
313 | |
314 # ...and start over. | |
315 prev_node_index = node_index | |
316 prev_n_columns_diff = n_columns_diff | |
317 | |
318 cmdtable = { | |
319 "glog": | |
4730
eadfaa9ec487
Updated command tables in commands.py and hgext extensions.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4633
diff
changeset
|
320 (graphlog, |
eadfaa9ec487
Updated command tables in commands.py and hgext extensions.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4633
diff
changeset
|
321 [('l', 'limit', '', _('limit number of changes displayed')), |
eadfaa9ec487
Updated command tables in commands.py and hgext extensions.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4633
diff
changeset
|
322 ('p', 'patch', False, _('show patch')), |
eadfaa9ec487
Updated command tables in commands.py and hgext extensions.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4633
diff
changeset
|
323 ('r', 'rev', [], _('show the specified revision or range')), |
6192
cd65a67aff31
Introduce templateopts and logopts to reduce duplicate option definitions.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5968
diff
changeset
|
324 ] + templateopts, |
5942
b75105de8573
glog shows at most one file: correct synopsis
Thomas Arendsen Hein <thomas@intevation.de>
parents:
5940
diff
changeset
|
325 _('hg glog [OPTION]... [FILE]')), |
4344 | 326 } |