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