Line | Count | Source |
1 | | #define DISABLE_SIGN_COMPARE_WARNINGS |
2 | | |
3 | | #include "git-compat-util.h" |
4 | | #include "gettext.h" |
5 | | #include "config.h" |
6 | | #include "commit.h" |
7 | | #include "color.h" |
8 | | #include "graph.h" |
9 | | #include "revision.h" |
10 | | #include "strvec.h" |
11 | | |
12 | | /* Internal API */ |
13 | | |
14 | | /* |
15 | | * Output a padding line in the graph. |
16 | | * This is similar to graph_next_line(). However, it is guaranteed to |
17 | | * never print the current commit line. Instead, if the commit line is |
18 | | * next, it will simply output a line of vertical padding, extending the |
19 | | * branch lines downwards, but leaving them otherwise unchanged. |
20 | | */ |
21 | | static void graph_padding_line(struct git_graph *graph, struct strbuf *sb); |
22 | | |
23 | | /* |
24 | | * Print a strbuf. If the graph is non-NULL, all lines but the first will be |
25 | | * prefixed with the graph output. |
26 | | * |
27 | | * If the strbuf ends with a newline, the output will end after this |
28 | | * newline. A new graph line will not be printed after the final newline. |
29 | | * If the strbuf is empty, no output will be printed. |
30 | | * |
31 | | * Since the first line will not include the graph output, the caller is |
32 | | * responsible for printing this line's graph (perhaps via |
33 | | * graph_show_commit() or graph_show_oneline()) before calling |
34 | | * graph_show_strbuf(). |
35 | | * |
36 | | * Note that unlike some other graph display functions, you must pass the file |
37 | | * handle directly. It is assumed that this is the same file handle as the |
38 | | * file specified by the graph diff options. This is necessary so that |
39 | | * graph_show_strbuf can be called even with a NULL graph. |
40 | | * If a NULL graph is supplied, the strbuf is printed as-is. |
41 | | */ |
42 | | static void graph_show_strbuf(struct git_graph *graph, |
43 | | FILE *file, |
44 | | struct strbuf const *sb); |
45 | | |
46 | | /* |
47 | | * TODO: |
48 | | * - Limit the number of columns, similar to the way gitk does. |
49 | | * If we reach more than a specified number of columns, omit |
50 | | * sections of some columns. |
51 | | */ |
52 | | |
53 | | struct column { |
54 | | /* |
55 | | * The parent commit of this column. |
56 | | */ |
57 | | struct commit *commit; |
58 | | /* |
59 | | * The color to (optionally) print this column in. This is an |
60 | | * index into column_colors. |
61 | | */ |
62 | | unsigned short color; |
63 | | }; |
64 | | |
65 | | enum graph_state { |
66 | | GRAPH_PADDING, |
67 | | GRAPH_SKIP, |
68 | | GRAPH_PRE_COMMIT, |
69 | | GRAPH_COMMIT, |
70 | | GRAPH_POST_MERGE, |
71 | | GRAPH_COLLAPSING |
72 | | }; |
73 | | |
74 | | static void graph_show_line_prefix(const struct diff_options *diffopt) |
75 | 0 | { |
76 | 0 | if (!diffopt || !diffopt->line_prefix) |
77 | 0 | return; |
78 | | |
79 | 0 | fputs(diffopt->line_prefix, diffopt->file); |
80 | 0 | } |
81 | | |
82 | | static const char **column_colors; |
83 | | static unsigned short column_colors_max; |
84 | | |
85 | | static void parse_graph_colors_config(struct strvec *colors, const char *string) |
86 | 0 | { |
87 | 0 | const char *end, *start; |
88 | |
|
89 | 0 | start = string; |
90 | 0 | end = string + strlen(string); |
91 | 0 | while (start < end) { |
92 | 0 | const char *comma = strchrnul(start, ','); |
93 | 0 | char color[COLOR_MAXLEN]; |
94 | |
|
95 | 0 | if (!color_parse_mem(start, comma - start, color)) |
96 | 0 | strvec_push(colors, color); |
97 | 0 | else |
98 | 0 | warning(_("ignored invalid color '%.*s' in log.graphColors"), |
99 | 0 | (int)(comma - start), start); |
100 | 0 | start = comma + 1; |
101 | 0 | } |
102 | 0 | strvec_push(colors, GIT_COLOR_RESET); |
103 | 0 | } |
104 | | |
105 | | void graph_set_column_colors(const char **colors, unsigned short colors_max) |
106 | 0 | { |
107 | 0 | column_colors = colors; |
108 | 0 | column_colors_max = colors_max; |
109 | 0 | } |
110 | | |
111 | | static const char *column_get_color_code(unsigned short color) |
112 | 0 | { |
113 | 0 | return column_colors[color]; |
114 | 0 | } |
115 | | |
116 | | struct graph_line { |
117 | | struct strbuf *buf; |
118 | | size_t width; |
119 | | }; |
120 | | |
121 | | static inline void graph_line_addch(struct graph_line *line, int c) |
122 | 0 | { |
123 | 0 | strbuf_addch(line->buf, c); |
124 | 0 | line->width++; |
125 | 0 | } |
126 | | |
127 | | static inline void graph_line_addchars(struct graph_line *line, int c, size_t n) |
128 | 0 | { |
129 | 0 | strbuf_addchars(line->buf, c, n); |
130 | 0 | line->width += n; |
131 | 0 | } |
132 | | |
133 | | static inline void graph_line_addstr(struct graph_line *line, const char *s) |
134 | 0 | { |
135 | 0 | strbuf_addstr(line->buf, s); |
136 | 0 | line->width += strlen(s); |
137 | 0 | } |
138 | | |
139 | | static inline void graph_line_addcolor(struct graph_line *line, unsigned short color) |
140 | 0 | { |
141 | 0 | strbuf_addstr(line->buf, column_get_color_code(color)); |
142 | 0 | } |
143 | | |
144 | | static void graph_line_write_column(struct graph_line *line, const struct column *c, |
145 | | char col_char) |
146 | 0 | { |
147 | 0 | if (c->color < column_colors_max) |
148 | 0 | graph_line_addcolor(line, c->color); |
149 | 0 | graph_line_addch(line, col_char); |
150 | 0 | if (c->color < column_colors_max) |
151 | 0 | graph_line_addcolor(line, column_colors_max); |
152 | 0 | } |
153 | | |
154 | | struct git_graph { |
155 | | /* |
156 | | * The commit currently being processed |
157 | | */ |
158 | | struct commit *commit; |
159 | | /* The rev-info used for the current traversal */ |
160 | | struct rev_info *revs; |
161 | | /* |
162 | | * The number of interesting parents that this commit has. |
163 | | * |
164 | | * Note that this is not the same as the actual number of parents. |
165 | | * This count excludes parents that won't be printed in the graph |
166 | | * output, as determined by graph_is_interesting(). |
167 | | */ |
168 | | int num_parents; |
169 | | /* |
170 | | * The width of the graph output for this commit. |
171 | | * All rows for this commit are padded to this width, so that |
172 | | * messages printed after the graph output are aligned. |
173 | | */ |
174 | | int width; |
175 | | /* |
176 | | * The next expansion row to print |
177 | | * when state is GRAPH_PRE_COMMIT |
178 | | */ |
179 | | int expansion_row; |
180 | | /* |
181 | | * The current output state. |
182 | | * This tells us what kind of line graph_next_line() should output. |
183 | | */ |
184 | | enum graph_state state; |
185 | | /* |
186 | | * The output state for the previous line of output. |
187 | | * This is primarily used to determine how the first merge line |
188 | | * should appear, based on the last line of the previous commit. |
189 | | */ |
190 | | enum graph_state prev_state; |
191 | | /* |
192 | | * The index of the column that refers to this commit. |
193 | | * |
194 | | * If none of the incoming columns refer to this commit, |
195 | | * this will be equal to num_columns. |
196 | | */ |
197 | | int commit_index; |
198 | | /* |
199 | | * The commit_index for the previously displayed commit. |
200 | | * |
201 | | * This is used to determine how the first line of a merge |
202 | | * graph output should appear, based on the last line of the |
203 | | * previous commit. |
204 | | */ |
205 | | int prev_commit_index; |
206 | | /* |
207 | | * Which layout variant to use to display merge commits. If the |
208 | | * commit's first parent is known to be in a column to the left of the |
209 | | * merge, then this value is 0 and we use the layout on the left. |
210 | | * Otherwise, the value is 1 and the layout on the right is used. This |
211 | | * field tells us how many columns the first parent occupies. |
212 | | * |
213 | | * 0) 1) |
214 | | * |
215 | | * | | | *-. | | *---. |
216 | | * | |_|/|\ \ | | |\ \ \ |
217 | | * |/| | | | | | | | | | * |
218 | | */ |
219 | | int merge_layout; |
220 | | /* |
221 | | * The number of columns added to the graph by the current commit. For |
222 | | * 2-way and octopus merges, this is usually one less than the |
223 | | * number of parents: |
224 | | * |
225 | | * | | | | | \ |
226 | | * | * | | *---. \ |
227 | | * | |\ \ | |\ \ \ \ |
228 | | * | | | | | | | | | | |
229 | | * |
230 | | * num_parents: 2 num_parents: 4 |
231 | | * edges_added: 1 edges_added: 3 |
232 | | * |
233 | | * For left-skewed merges, the first parent fuses with its neighbor and |
234 | | * so one less column is added: |
235 | | * |
236 | | * | | | | | \ |
237 | | * | * | | *-. \ |
238 | | * |/| | |/|\ \ \ |
239 | | * | | | | | | | | |
240 | | * |
241 | | * num_parents: 2 num_parents: 4 |
242 | | * edges_added: 0 edges_added: 2 |
243 | | * |
244 | | * This number determines how edges to the right of the merge are |
245 | | * displayed in commit and post-merge lines; if no columns have been |
246 | | * added then a vertical line should be used where a right-tracking |
247 | | * line would otherwise be used. |
248 | | * |
249 | | * | * \ | * | |
250 | | * | |\ \ |/| | |
251 | | * | | * \ | * | |
252 | | */ |
253 | | int edges_added; |
254 | | /* |
255 | | * The number of columns added by the previous commit, which is used to |
256 | | * smooth edges appearing to the right of a commit in a commit line |
257 | | * following a post-merge line. |
258 | | */ |
259 | | int prev_edges_added; |
260 | | /* |
261 | | * The maximum number of columns that can be stored in the columns |
262 | | * and new_columns arrays. This is also half the number of entries |
263 | | * that can be stored in the mapping and old_mapping arrays. |
264 | | */ |
265 | | int column_capacity; |
266 | | /* |
267 | | * The number of columns (also called "branch lines" in some places) |
268 | | */ |
269 | | int num_columns; |
270 | | /* |
271 | | * The number of columns in the new_columns array |
272 | | */ |
273 | | int num_new_columns; |
274 | | /* |
275 | | * The number of entries in the mapping array |
276 | | */ |
277 | | int mapping_size; |
278 | | /* |
279 | | * The column state before we output the current commit. |
280 | | */ |
281 | | struct column *columns; |
282 | | /* |
283 | | * The new column state after we output the current commit. |
284 | | * Only valid when state is GRAPH_COLLAPSING. |
285 | | */ |
286 | | struct column *new_columns; |
287 | | /* |
288 | | * An array that tracks the current state of each |
289 | | * character in the output line during state GRAPH_COLLAPSING. |
290 | | * Each entry is -1 if this character is empty, or a non-negative |
291 | | * integer if the character contains a branch line. The value of |
292 | | * the integer indicates the target position for this branch line. |
293 | | * (I.e., this array maps the current column positions to their |
294 | | * desired positions.) |
295 | | * |
296 | | * The maximum capacity of this array is always |
297 | | * sizeof(int) * 2 * column_capacity. |
298 | | */ |
299 | | int *mapping; |
300 | | /* |
301 | | * A copy of the contents of the mapping array from the last commit, |
302 | | * which we use to improve the display of columns that are tracking |
303 | | * from right to left through a commit line. We also use this to |
304 | | * avoid allocating a fresh array when we compute the next mapping. |
305 | | */ |
306 | | int *old_mapping; |
307 | | /* |
308 | | * The current default column color being used. This is |
309 | | * stored as an index into the array column_colors. |
310 | | */ |
311 | | unsigned short default_column_color; |
312 | | |
313 | | /* |
314 | | * Scratch buffer for generating prefixes to be used with |
315 | | * diff_output_prefix_callback(). |
316 | | */ |
317 | | struct strbuf prefix_buf; |
318 | | }; |
319 | | |
320 | | static const char *diff_output_prefix_callback(struct diff_options *opt, void *data) |
321 | 0 | { |
322 | 0 | struct git_graph *graph = data; |
323 | |
|
324 | 0 | assert(opt); |
325 | |
|
326 | 0 | if (!graph) |
327 | 0 | return opt->line_prefix; |
328 | | |
329 | 0 | strbuf_reset(&graph->prefix_buf); |
330 | 0 | if (opt->line_prefix) |
331 | 0 | strbuf_addstr(&graph->prefix_buf, opt->line_prefix); |
332 | 0 | graph_padding_line(graph, &graph->prefix_buf); |
333 | 0 | return graph->prefix_buf.buf; |
334 | 0 | } |
335 | | |
336 | | static const struct diff_options *default_diffopt; |
337 | | |
338 | | void graph_setup_line_prefix(struct diff_options *diffopt) |
339 | 0 | { |
340 | 0 | default_diffopt = diffopt; |
341 | | |
342 | | /* setup an output prefix callback if necessary */ |
343 | 0 | if (diffopt && !diffopt->output_prefix) |
344 | 0 | diffopt->output_prefix = diff_output_prefix_callback; |
345 | 0 | } |
346 | | |
347 | | struct git_graph *graph_init(struct rev_info *opt) |
348 | 0 | { |
349 | 0 | struct git_graph *graph = xmalloc(sizeof(struct git_graph)); |
350 | |
|
351 | 0 | if (!column_colors) { |
352 | 0 | char *string; |
353 | 0 | if (repo_config_get_string(opt->repo, "log.graphcolors", &string)) { |
354 | | /* not configured -- use default */ |
355 | 0 | graph_set_column_colors(column_colors_ansi, |
356 | 0 | column_colors_ansi_max); |
357 | 0 | } else { |
358 | 0 | static struct strvec custom_colors = STRVEC_INIT; |
359 | 0 | strvec_clear(&custom_colors); |
360 | 0 | parse_graph_colors_config(&custom_colors, string); |
361 | 0 | free(string); |
362 | | /* graph_set_column_colors takes a max-index, not a count */ |
363 | 0 | graph_set_column_colors(custom_colors.v, |
364 | 0 | custom_colors.nr - 1); |
365 | 0 | } |
366 | 0 | } |
367 | |
|
368 | 0 | graph->commit = NULL; |
369 | 0 | graph->revs = opt; |
370 | 0 | graph->num_parents = 0; |
371 | 0 | graph->expansion_row = 0; |
372 | 0 | graph->state = GRAPH_PADDING; |
373 | 0 | graph->prev_state = GRAPH_PADDING; |
374 | 0 | graph->commit_index = 0; |
375 | 0 | graph->prev_commit_index = 0; |
376 | 0 | graph->merge_layout = 0; |
377 | 0 | graph->edges_added = 0; |
378 | 0 | graph->prev_edges_added = 0; |
379 | 0 | graph->num_columns = 0; |
380 | 0 | graph->num_new_columns = 0; |
381 | 0 | graph->mapping_size = 0; |
382 | | /* |
383 | | * Start the column color at the maximum value, since we'll |
384 | | * always increment it for the first commit we output. |
385 | | * This way we start at 0 for the first commit. |
386 | | */ |
387 | 0 | graph->default_column_color = column_colors_max - 1; |
388 | | |
389 | | /* |
390 | | * Allocate a reasonably large default number of columns |
391 | | * We'll automatically grow columns later if we need more room. |
392 | | */ |
393 | 0 | graph->column_capacity = 30; |
394 | 0 | ALLOC_ARRAY(graph->columns, graph->column_capacity); |
395 | 0 | ALLOC_ARRAY(graph->new_columns, graph->column_capacity); |
396 | 0 | ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity); |
397 | 0 | ALLOC_ARRAY(graph->old_mapping, 2 * graph->column_capacity); |
398 | | |
399 | | /* |
400 | | * The diff output prefix callback, with this we can make |
401 | | * all the diff output to align with the graph lines. |
402 | | */ |
403 | 0 | strbuf_init(&graph->prefix_buf, 0); |
404 | 0 | opt->diffopt.output_prefix = diff_output_prefix_callback; |
405 | 0 | opt->diffopt.output_prefix_data = graph; |
406 | |
|
407 | 0 | return graph; |
408 | 0 | } |
409 | | |
410 | | void graph_clear(struct git_graph *graph) |
411 | 0 | { |
412 | 0 | if (!graph) |
413 | 0 | return; |
414 | | |
415 | 0 | free(graph->columns); |
416 | 0 | free(graph->new_columns); |
417 | 0 | free(graph->mapping); |
418 | 0 | free(graph->old_mapping); |
419 | 0 | strbuf_release(&graph->prefix_buf); |
420 | 0 | free(graph); |
421 | 0 | } |
422 | | |
423 | | static void graph_update_state(struct git_graph *graph, enum graph_state s) |
424 | 0 | { |
425 | 0 | graph->prev_state = graph->state; |
426 | 0 | graph->state = s; |
427 | 0 | } |
428 | | |
429 | | static void graph_ensure_capacity(struct git_graph *graph, int num_columns) |
430 | 0 | { |
431 | 0 | if (graph->column_capacity >= num_columns) |
432 | 0 | return; |
433 | | |
434 | 0 | do { |
435 | 0 | graph->column_capacity *= 2; |
436 | 0 | } while (graph->column_capacity < num_columns); |
437 | |
|
438 | 0 | REALLOC_ARRAY(graph->columns, graph->column_capacity); |
439 | 0 | REALLOC_ARRAY(graph->new_columns, graph->column_capacity); |
440 | 0 | REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2); |
441 | 0 | REALLOC_ARRAY(graph->old_mapping, graph->column_capacity * 2); |
442 | 0 | } |
443 | | |
444 | | /* |
445 | | * Returns 1 if the commit will be printed in the graph output, |
446 | | * and 0 otherwise. |
447 | | */ |
448 | | static int graph_is_interesting(struct git_graph *graph, struct commit *commit) |
449 | 0 | { |
450 | | /* |
451 | | * If revs->boundary is set, commits whose children have |
452 | | * been shown are always interesting, even if they have the |
453 | | * UNINTERESTING or TREESAME flags set. |
454 | | */ |
455 | 0 | if (graph->revs && graph->revs->boundary) { |
456 | 0 | if (commit->object.flags & CHILD_SHOWN) |
457 | 0 | return 1; |
458 | 0 | } |
459 | | |
460 | | /* |
461 | | * Otherwise, use get_commit_action() to see if this commit is |
462 | | * interesting |
463 | | */ |
464 | 0 | return get_commit_action(graph->revs, commit) == commit_show; |
465 | 0 | } |
466 | | |
467 | | static struct commit_list *next_interesting_parent(struct git_graph *graph, |
468 | | struct commit_list *orig) |
469 | 0 | { |
470 | 0 | struct commit_list *list; |
471 | | |
472 | | /* |
473 | | * If revs->first_parent_only is set, only the first |
474 | | * parent is interesting. None of the others are. |
475 | | */ |
476 | 0 | if (graph->revs->first_parent_only) |
477 | 0 | return NULL; |
478 | | |
479 | | /* |
480 | | * Return the next interesting commit after orig |
481 | | */ |
482 | 0 | for (list = orig->next; list; list = list->next) { |
483 | 0 | if (graph_is_interesting(graph, list->item)) |
484 | 0 | return list; |
485 | 0 | } |
486 | | |
487 | 0 | return NULL; |
488 | 0 | } |
489 | | |
490 | | static struct commit_list *first_interesting_parent(struct git_graph *graph) |
491 | 0 | { |
492 | 0 | struct commit_list *parents = graph->commit->parents; |
493 | | |
494 | | /* |
495 | | * If this commit has no parents, ignore it |
496 | | */ |
497 | 0 | if (!parents) |
498 | 0 | return NULL; |
499 | | |
500 | | /* |
501 | | * If the first parent is interesting, return it |
502 | | */ |
503 | 0 | if (graph_is_interesting(graph, parents->item)) |
504 | 0 | return parents; |
505 | | |
506 | | /* |
507 | | * Otherwise, call next_interesting_parent() to get |
508 | | * the next interesting parent |
509 | | */ |
510 | 0 | return next_interesting_parent(graph, parents); |
511 | 0 | } |
512 | | |
513 | | static unsigned short graph_get_current_column_color(const struct git_graph *graph) |
514 | 0 | { |
515 | 0 | if (!want_color(graph->revs->diffopt.use_color)) |
516 | 0 | return column_colors_max; |
517 | 0 | return graph->default_column_color; |
518 | 0 | } |
519 | | |
520 | | /* |
521 | | * Update the graph's default column color. |
522 | | */ |
523 | | static void graph_increment_column_color(struct git_graph *graph) |
524 | 0 | { |
525 | 0 | graph->default_column_color = (graph->default_column_color + 1) % |
526 | 0 | column_colors_max; |
527 | 0 | } |
528 | | |
529 | | static unsigned short graph_find_commit_color(const struct git_graph *graph, |
530 | | const struct commit *commit) |
531 | 0 | { |
532 | 0 | int i; |
533 | 0 | for (i = 0; i < graph->num_columns; i++) { |
534 | 0 | if (graph->columns[i].commit == commit) |
535 | 0 | return graph->columns[i].color; |
536 | 0 | } |
537 | 0 | return graph_get_current_column_color(graph); |
538 | 0 | } |
539 | | |
540 | | static int graph_find_new_column_by_commit(struct git_graph *graph, |
541 | | struct commit *commit) |
542 | 0 | { |
543 | 0 | int i; |
544 | 0 | for (i = 0; i < graph->num_new_columns; i++) { |
545 | 0 | if (graph->new_columns[i].commit == commit) |
546 | 0 | return i; |
547 | 0 | } |
548 | 0 | return -1; |
549 | 0 | } |
550 | | |
551 | | static void graph_insert_into_new_columns(struct git_graph *graph, |
552 | | struct commit *commit, |
553 | | int idx) |
554 | 0 | { |
555 | 0 | int i = graph_find_new_column_by_commit(graph, commit); |
556 | 0 | int mapping_idx; |
557 | | |
558 | | /* |
559 | | * If the commit is not already in the new_columns array, then add it |
560 | | * and record it as being in the final column. |
561 | | */ |
562 | 0 | if (i < 0) { |
563 | 0 | i = graph->num_new_columns++; |
564 | 0 | graph->new_columns[i].commit = commit; |
565 | 0 | graph->new_columns[i].color = graph_find_commit_color(graph, commit); |
566 | 0 | } |
567 | |
|
568 | 0 | if (graph->num_parents > 1 && idx > -1 && graph->merge_layout == -1) { |
569 | | /* |
570 | | * If this is the first parent of a merge, choose a layout for |
571 | | * the merge line based on whether the parent appears in a |
572 | | * column to the left of the merge |
573 | | */ |
574 | 0 | int dist, shift; |
575 | |
|
576 | 0 | dist = idx - i; |
577 | 0 | shift = (dist > 1) ? 2 * dist - 3 : 1; |
578 | |
|
579 | 0 | graph->merge_layout = (dist > 0) ? 0 : 1; |
580 | 0 | graph->edges_added = graph->num_parents + graph->merge_layout - 2; |
581 | |
|
582 | 0 | mapping_idx = graph->width + (graph->merge_layout - 1) * shift; |
583 | 0 | graph->width += 2 * graph->merge_layout; |
584 | |
|
585 | 0 | } else if (graph->edges_added > 0 && i == graph->mapping[graph->width - 2]) { |
586 | | /* |
587 | | * If some columns have been added by a merge, but this commit |
588 | | * was found in the last existing column, then adjust the |
589 | | * numbers so that the two edges immediately join, i.e.: |
590 | | * |
591 | | * * | * | |
592 | | * |\ \ => |\| |
593 | | * | |/ | * |
594 | | * | * |
595 | | */ |
596 | 0 | mapping_idx = graph->width - 2; |
597 | 0 | graph->edges_added = -1; |
598 | 0 | } else { |
599 | 0 | mapping_idx = graph->width; |
600 | 0 | graph->width += 2; |
601 | 0 | } |
602 | |
|
603 | 0 | graph->mapping[mapping_idx] = i; |
604 | 0 | } |
605 | | |
606 | | static void graph_update_columns(struct git_graph *graph) |
607 | 0 | { |
608 | 0 | struct commit_list *parent; |
609 | 0 | int max_new_columns; |
610 | 0 | int i, seen_this, is_commit_in_columns; |
611 | | |
612 | | /* |
613 | | * Swap graph->columns with graph->new_columns |
614 | | * graph->columns contains the state for the previous commit, |
615 | | * and new_columns now contains the state for our commit. |
616 | | * |
617 | | * We'll re-use the old columns array as storage to compute the new |
618 | | * columns list for the commit after this one. |
619 | | */ |
620 | 0 | SWAP(graph->columns, graph->new_columns); |
621 | 0 | graph->num_columns = graph->num_new_columns; |
622 | 0 | graph->num_new_columns = 0; |
623 | | |
624 | | /* |
625 | | * Now update new_columns and mapping with the information for the |
626 | | * commit after this one. |
627 | | * |
628 | | * First, make sure we have enough room. At most, there will |
629 | | * be graph->num_columns + graph->num_parents columns for the next |
630 | | * commit. |
631 | | */ |
632 | 0 | max_new_columns = graph->num_columns + graph->num_parents; |
633 | 0 | graph_ensure_capacity(graph, max_new_columns); |
634 | | |
635 | | /* |
636 | | * Clear out graph->mapping |
637 | | */ |
638 | 0 | graph->mapping_size = 2 * max_new_columns; |
639 | 0 | for (i = 0; i < graph->mapping_size; i++) |
640 | 0 | graph->mapping[i] = -1; |
641 | |
|
642 | 0 | graph->width = 0; |
643 | 0 | graph->prev_edges_added = graph->edges_added; |
644 | 0 | graph->edges_added = 0; |
645 | | |
646 | | /* |
647 | | * Populate graph->new_columns and graph->mapping |
648 | | * |
649 | | * Some of the parents of this commit may already be in |
650 | | * graph->columns. If so, graph->new_columns should only contain a |
651 | | * single entry for each such commit. graph->mapping should |
652 | | * contain information about where each current branch line is |
653 | | * supposed to end up after the collapsing is performed. |
654 | | */ |
655 | 0 | seen_this = 0; |
656 | 0 | is_commit_in_columns = 1; |
657 | 0 | for (i = 0; i <= graph->num_columns; i++) { |
658 | 0 | struct commit *col_commit; |
659 | 0 | if (i == graph->num_columns) { |
660 | 0 | if (seen_this) |
661 | 0 | break; |
662 | 0 | is_commit_in_columns = 0; |
663 | 0 | col_commit = graph->commit; |
664 | 0 | } else { |
665 | 0 | col_commit = graph->columns[i].commit; |
666 | 0 | } |
667 | | |
668 | 0 | if (col_commit == graph->commit) { |
669 | 0 | seen_this = 1; |
670 | 0 | graph->commit_index = i; |
671 | 0 | graph->merge_layout = -1; |
672 | 0 | for (parent = first_interesting_parent(graph); |
673 | 0 | parent; |
674 | 0 | parent = next_interesting_parent(graph, parent)) { |
675 | | /* |
676 | | * If this is a merge, or the start of a new |
677 | | * childless column, increment the current |
678 | | * color. |
679 | | */ |
680 | 0 | if (graph->num_parents > 1 || |
681 | 0 | !is_commit_in_columns) { |
682 | 0 | graph_increment_column_color(graph); |
683 | 0 | } |
684 | 0 | graph_insert_into_new_columns(graph, parent->item, i); |
685 | 0 | } |
686 | | /* |
687 | | * We always need to increment graph->width by at |
688 | | * least 2, even if it has no interesting parents. |
689 | | * The current commit always takes up at least 2 |
690 | | * spaces. |
691 | | */ |
692 | 0 | if (graph->num_parents == 0) |
693 | 0 | graph->width += 2; |
694 | 0 | } else { |
695 | 0 | graph_insert_into_new_columns(graph, col_commit, -1); |
696 | 0 | } |
697 | 0 | } |
698 | | |
699 | | /* |
700 | | * Shrink mapping_size to be the minimum necessary |
701 | | */ |
702 | 0 | while (graph->mapping_size > 1 && |
703 | 0 | graph->mapping[graph->mapping_size - 1] < 0) |
704 | 0 | graph->mapping_size--; |
705 | 0 | } |
706 | | |
707 | | static int graph_num_dashed_parents(struct git_graph *graph) |
708 | 0 | { |
709 | 0 | return graph->num_parents + graph->merge_layout - 3; |
710 | 0 | } |
711 | | |
712 | | static int graph_num_expansion_rows(struct git_graph *graph) |
713 | 0 | { |
714 | | /* |
715 | | * Normally, we need two expansion rows for each dashed parent line from |
716 | | * an octopus merge: |
717 | | * |
718 | | * | * |
719 | | * | |\ |
720 | | * | | \ |
721 | | * | | \ |
722 | | * | *-. \ |
723 | | * | |\ \ \ |
724 | | * |
725 | | * If the merge is skewed to the left, then its parents occupy one less |
726 | | * column, and we don't need as many expansion rows to route around it; |
727 | | * in some cases that means we don't need any expansion rows at all: |
728 | | * |
729 | | * | * |
730 | | * | |\ |
731 | | * | * \ |
732 | | * |/|\ \ |
733 | | */ |
734 | 0 | return graph_num_dashed_parents(graph) * 2; |
735 | 0 | } |
736 | | |
737 | | static int graph_needs_pre_commit_line(struct git_graph *graph) |
738 | 0 | { |
739 | 0 | return graph->num_parents >= 3 && |
740 | 0 | graph->commit_index < (graph->num_columns - 1) && |
741 | 0 | graph->expansion_row < graph_num_expansion_rows(graph); |
742 | 0 | } |
743 | | |
744 | | void graph_update(struct git_graph *graph, struct commit *commit) |
745 | 0 | { |
746 | 0 | struct commit_list *parent; |
747 | | |
748 | | /* |
749 | | * Set the new commit |
750 | | */ |
751 | 0 | graph->commit = commit; |
752 | | |
753 | | /* |
754 | | * Count how many interesting parents this commit has |
755 | | */ |
756 | 0 | graph->num_parents = 0; |
757 | 0 | for (parent = first_interesting_parent(graph); |
758 | 0 | parent; |
759 | 0 | parent = next_interesting_parent(graph, parent)) |
760 | 0 | { |
761 | 0 | graph->num_parents++; |
762 | 0 | } |
763 | | |
764 | | /* |
765 | | * Store the old commit_index in prev_commit_index. |
766 | | * graph_update_columns() will update graph->commit_index for this |
767 | | * commit. |
768 | | */ |
769 | 0 | graph->prev_commit_index = graph->commit_index; |
770 | | |
771 | | /* |
772 | | * Call graph_update_columns() to update |
773 | | * columns, new_columns, and mapping. |
774 | | */ |
775 | 0 | graph_update_columns(graph); |
776 | |
|
777 | 0 | graph->expansion_row = 0; |
778 | | |
779 | | /* |
780 | | * Update graph->state. |
781 | | * Note that we don't call graph_update_state() here, since |
782 | | * we don't want to update graph->prev_state. No line for |
783 | | * graph->state was ever printed. |
784 | | * |
785 | | * If the previous commit didn't get to the GRAPH_PADDING state, |
786 | | * it never finished its output. Goto GRAPH_SKIP, to print out |
787 | | * a line to indicate that portion of the graph is missing. |
788 | | * |
789 | | * If there are 3 or more parents, we may need to print extra rows |
790 | | * before the commit, to expand the branch lines around it and make |
791 | | * room for it. We need to do this only if there is a branch row |
792 | | * (or more) to the right of this commit. |
793 | | * |
794 | | * If there are less than 3 parents, we can immediately print the |
795 | | * commit line. |
796 | | */ |
797 | 0 | if (graph->state != GRAPH_PADDING) |
798 | 0 | graph->state = GRAPH_SKIP; |
799 | 0 | else if (graph_needs_pre_commit_line(graph)) |
800 | 0 | graph->state = GRAPH_PRE_COMMIT; |
801 | 0 | else |
802 | 0 | graph->state = GRAPH_COMMIT; |
803 | 0 | } |
804 | | |
805 | | static int graph_is_mapping_correct(struct git_graph *graph) |
806 | 0 | { |
807 | 0 | int i; |
808 | | |
809 | | /* |
810 | | * The mapping is up to date if each entry is at its target, |
811 | | * or is 1 greater than its target. |
812 | | * (If it is 1 greater than the target, '/' will be printed, so it |
813 | | * will look correct on the next row.) |
814 | | */ |
815 | 0 | for (i = 0; i < graph->mapping_size; i++) { |
816 | 0 | int target = graph->mapping[i]; |
817 | 0 | if (target < 0) |
818 | 0 | continue; |
819 | 0 | if (target == (i / 2)) |
820 | 0 | continue; |
821 | 0 | return 0; |
822 | 0 | } |
823 | | |
824 | 0 | return 1; |
825 | 0 | } |
826 | | |
827 | | static void graph_pad_horizontally(struct git_graph *graph, struct graph_line *line) |
828 | 0 | { |
829 | | /* |
830 | | * Add additional spaces to the end of the strbuf, so that all |
831 | | * lines for a particular commit have the same width. |
832 | | * |
833 | | * This way, fields printed to the right of the graph will remain |
834 | | * aligned for the entire commit. |
835 | | */ |
836 | 0 | if (line->width < graph->width) |
837 | 0 | graph_line_addchars(line, ' ', graph->width - line->width); |
838 | 0 | } |
839 | | |
840 | | static void graph_output_padding_line(struct git_graph *graph, |
841 | | struct graph_line *line) |
842 | 0 | { |
843 | 0 | int i; |
844 | | |
845 | | /* |
846 | | * Output a padding row, that leaves all branch lines unchanged |
847 | | */ |
848 | 0 | for (i = 0; i < graph->num_new_columns; i++) { |
849 | 0 | graph_line_write_column(line, &graph->new_columns[i], '|'); |
850 | 0 | graph_line_addch(line, ' '); |
851 | 0 | } |
852 | 0 | } |
853 | | |
854 | | |
855 | | int graph_width(struct git_graph *graph) |
856 | 0 | { |
857 | 0 | return graph->width; |
858 | 0 | } |
859 | | |
860 | | |
861 | | static void graph_output_skip_line(struct git_graph *graph, struct graph_line *line) |
862 | 0 | { |
863 | | /* |
864 | | * Output an ellipsis to indicate that a portion |
865 | | * of the graph is missing. |
866 | | */ |
867 | 0 | graph_line_addstr(line, "..."); |
868 | |
|
869 | 0 | if (graph_needs_pre_commit_line(graph)) |
870 | 0 | graph_update_state(graph, GRAPH_PRE_COMMIT); |
871 | 0 | else |
872 | 0 | graph_update_state(graph, GRAPH_COMMIT); |
873 | 0 | } |
874 | | |
875 | | static void graph_output_pre_commit_line(struct git_graph *graph, |
876 | | struct graph_line *line) |
877 | 0 | { |
878 | 0 | int i, seen_this; |
879 | | |
880 | | /* |
881 | | * This function formats a row that increases the space around a commit |
882 | | * with multiple parents, to make room for it. It should only be |
883 | | * called when there are 3 or more parents. |
884 | | * |
885 | | * We need 2 extra rows for every parent over 2. |
886 | | */ |
887 | 0 | assert(graph->num_parents >= 3); |
888 | | |
889 | | /* |
890 | | * graph->expansion_row tracks the current expansion row we are on. |
891 | | * It should be in the range [0, num_expansion_rows - 1] |
892 | | */ |
893 | 0 | assert(0 <= graph->expansion_row && |
894 | 0 | graph->expansion_row < graph_num_expansion_rows(graph)); |
895 | | |
896 | | /* |
897 | | * Output the row |
898 | | */ |
899 | 0 | seen_this = 0; |
900 | 0 | for (i = 0; i < graph->num_columns; i++) { |
901 | 0 | struct column *col = &graph->columns[i]; |
902 | 0 | if (col->commit == graph->commit) { |
903 | 0 | seen_this = 1; |
904 | 0 | graph_line_write_column(line, col, '|'); |
905 | 0 | graph_line_addchars(line, ' ', graph->expansion_row); |
906 | 0 | } else if (seen_this && (graph->expansion_row == 0)) { |
907 | | /* |
908 | | * This is the first line of the pre-commit output. |
909 | | * If the previous commit was a merge commit and |
910 | | * ended in the GRAPH_POST_MERGE state, all branch |
911 | | * lines after graph->prev_commit_index were |
912 | | * printed as "\" on the previous line. Continue |
913 | | * to print them as "\" on this line. Otherwise, |
914 | | * print the branch lines as "|". |
915 | | */ |
916 | 0 | if (graph->prev_state == GRAPH_POST_MERGE && |
917 | 0 | graph->prev_commit_index < i) |
918 | 0 | graph_line_write_column(line, col, '\\'); |
919 | 0 | else |
920 | 0 | graph_line_write_column(line, col, '|'); |
921 | 0 | } else if (seen_this && (graph->expansion_row > 0)) { |
922 | 0 | graph_line_write_column(line, col, '\\'); |
923 | 0 | } else { |
924 | 0 | graph_line_write_column(line, col, '|'); |
925 | 0 | } |
926 | 0 | graph_line_addch(line, ' '); |
927 | 0 | } |
928 | | |
929 | | /* |
930 | | * Increment graph->expansion_row, |
931 | | * and move to state GRAPH_COMMIT if necessary |
932 | | */ |
933 | 0 | graph->expansion_row++; |
934 | 0 | if (!graph_needs_pre_commit_line(graph)) |
935 | 0 | graph_update_state(graph, GRAPH_COMMIT); |
936 | 0 | } |
937 | | |
938 | | static void graph_output_commit_char(struct git_graph *graph, struct graph_line *line) |
939 | 0 | { |
940 | | /* |
941 | | * For boundary commits, print 'o' |
942 | | * (We should only see boundary commits when revs->boundary is set.) |
943 | | */ |
944 | 0 | if (graph->commit->object.flags & BOUNDARY) { |
945 | 0 | assert(graph->revs->boundary); |
946 | 0 | graph_line_addch(line, 'o'); |
947 | 0 | return; |
948 | 0 | } |
949 | | |
950 | | /* |
951 | | * get_revision_mark() handles all other cases without assert() |
952 | | */ |
953 | 0 | graph_line_addstr(line, get_revision_mark(graph->revs, graph->commit)); |
954 | 0 | } |
955 | | |
956 | | /* |
957 | | * Draw the horizontal dashes of an octopus merge. |
958 | | */ |
959 | | static void graph_draw_octopus_merge(struct git_graph *graph, struct graph_line *line) |
960 | 0 | { |
961 | | /* |
962 | | * The parents of a merge commit can be arbitrarily reordered as they |
963 | | * are mapped onto display columns, for example this is a valid merge: |
964 | | * |
965 | | * | | *---. |
966 | | * | | |\ \ \ |
967 | | * | | |/ / / |
968 | | * | |/| | / |
969 | | * | |_|_|/ |
970 | | * |/| | | |
971 | | * 3 1 0 2 |
972 | | * |
973 | | * The numbers denote which parent of the merge each visual column |
974 | | * corresponds to; we can't assume that the parents will initially |
975 | | * display in the order given by new_columns. |
976 | | * |
977 | | * To find the right color for each dash, we need to consult the |
978 | | * mapping array, starting from the column 2 places to the right of the |
979 | | * merge commit, and use that to find out which logical column each |
980 | | * edge will collapse to. |
981 | | * |
982 | | * Commits are rendered once all edges have collapsed to their correct |
983 | | * logcial column, so commit_index gives us the right visual offset for |
984 | | * the merge commit. |
985 | | */ |
986 | |
|
987 | 0 | int i, j; |
988 | 0 | struct column *col; |
989 | |
|
990 | 0 | int dashed_parents = graph_num_dashed_parents(graph); |
991 | |
|
992 | 0 | for (i = 0; i < dashed_parents; i++) { |
993 | 0 | j = graph->mapping[(graph->commit_index + i + 2) * 2]; |
994 | 0 | col = &graph->new_columns[j]; |
995 | |
|
996 | 0 | graph_line_write_column(line, col, '-'); |
997 | 0 | graph_line_write_column(line, col, (i == dashed_parents - 1) ? '.' : '-'); |
998 | 0 | } |
999 | |
|
1000 | 0 | return; |
1001 | 0 | } |
1002 | | |
1003 | | static void graph_output_commit_line(struct git_graph *graph, struct graph_line *line) |
1004 | 0 | { |
1005 | 0 | int seen_this = 0; |
1006 | 0 | int i; |
1007 | | |
1008 | | /* |
1009 | | * Output the row containing this commit |
1010 | | * Iterate up to and including graph->num_columns, |
1011 | | * since the current commit may not be in any of the existing |
1012 | | * columns. (This happens when the current commit doesn't have any |
1013 | | * children that we have already processed.) |
1014 | | */ |
1015 | 0 | seen_this = 0; |
1016 | 0 | for (i = 0; i <= graph->num_columns; i++) { |
1017 | 0 | struct column *col = &graph->columns[i]; |
1018 | 0 | struct commit *col_commit; |
1019 | 0 | if (i == graph->num_columns) { |
1020 | 0 | if (seen_this) |
1021 | 0 | break; |
1022 | 0 | col_commit = graph->commit; |
1023 | 0 | } else { |
1024 | 0 | col_commit = graph->columns[i].commit; |
1025 | 0 | } |
1026 | | |
1027 | 0 | if (col_commit == graph->commit) { |
1028 | 0 | seen_this = 1; |
1029 | 0 | graph_output_commit_char(graph, line); |
1030 | |
|
1031 | 0 | if (graph->num_parents > 2) |
1032 | 0 | graph_draw_octopus_merge(graph, line); |
1033 | 0 | } else if (seen_this && (graph->edges_added > 1)) { |
1034 | 0 | graph_line_write_column(line, col, '\\'); |
1035 | 0 | } else if (seen_this && (graph->edges_added == 1)) { |
1036 | | /* |
1037 | | * This is either a right-skewed 2-way merge |
1038 | | * commit, or a left-skewed 3-way merge. |
1039 | | * There is no GRAPH_PRE_COMMIT stage for such |
1040 | | * merges, so this is the first line of output |
1041 | | * for this commit. Check to see what the previous |
1042 | | * line of output was. |
1043 | | * |
1044 | | * If it was GRAPH_POST_MERGE, the branch line |
1045 | | * coming into this commit may have been '\', |
1046 | | * and not '|' or '/'. If so, output the branch |
1047 | | * line as '\' on this line, instead of '|'. This |
1048 | | * makes the output look nicer. |
1049 | | */ |
1050 | 0 | if (graph->prev_state == GRAPH_POST_MERGE && |
1051 | 0 | graph->prev_edges_added > 0 && |
1052 | 0 | graph->prev_commit_index < i) |
1053 | 0 | graph_line_write_column(line, col, '\\'); |
1054 | 0 | else |
1055 | 0 | graph_line_write_column(line, col, '|'); |
1056 | 0 | } else if (graph->prev_state == GRAPH_COLLAPSING && |
1057 | 0 | graph->old_mapping[2 * i + 1] == i && |
1058 | 0 | graph->mapping[2 * i] < i) { |
1059 | 0 | graph_line_write_column(line, col, '/'); |
1060 | 0 | } else { |
1061 | 0 | graph_line_write_column(line, col, '|'); |
1062 | 0 | } |
1063 | 0 | graph_line_addch(line, ' '); |
1064 | 0 | } |
1065 | | |
1066 | | /* |
1067 | | * Update graph->state |
1068 | | */ |
1069 | 0 | if (graph->num_parents > 1) |
1070 | 0 | graph_update_state(graph, GRAPH_POST_MERGE); |
1071 | 0 | else if (graph_is_mapping_correct(graph)) |
1072 | 0 | graph_update_state(graph, GRAPH_PADDING); |
1073 | 0 | else |
1074 | 0 | graph_update_state(graph, GRAPH_COLLAPSING); |
1075 | 0 | } |
1076 | | |
1077 | | static const char merge_chars[] = {'/', '|', '\\'}; |
1078 | | |
1079 | | static void graph_output_post_merge_line(struct git_graph *graph, struct graph_line *line) |
1080 | 0 | { |
1081 | 0 | int seen_this = 0; |
1082 | 0 | int i, j; |
1083 | |
|
1084 | 0 | struct commit_list *first_parent = first_interesting_parent(graph); |
1085 | 0 | struct column *parent_col = NULL; |
1086 | | |
1087 | | /* |
1088 | | * Output the post-merge row |
1089 | | */ |
1090 | 0 | for (i = 0; i <= graph->num_columns; i++) { |
1091 | 0 | struct column *col = &graph->columns[i]; |
1092 | 0 | struct commit *col_commit; |
1093 | 0 | if (i == graph->num_columns) { |
1094 | 0 | if (seen_this) |
1095 | 0 | break; |
1096 | 0 | col_commit = graph->commit; |
1097 | 0 | } else { |
1098 | 0 | col_commit = col->commit; |
1099 | 0 | } |
1100 | | |
1101 | 0 | if (col_commit == graph->commit) { |
1102 | | /* |
1103 | | * Since the current commit is a merge find |
1104 | | * the columns for the parent commits in |
1105 | | * new_columns and use those to format the |
1106 | | * edges. |
1107 | | */ |
1108 | 0 | struct commit_list *parents = first_parent; |
1109 | 0 | int par_column; |
1110 | 0 | int idx = graph->merge_layout; |
1111 | 0 | char c; |
1112 | 0 | seen_this = 1; |
1113 | |
|
1114 | 0 | for (j = 0; j < graph->num_parents; j++) { |
1115 | 0 | par_column = graph_find_new_column_by_commit(graph, parents->item); |
1116 | 0 | assert(par_column >= 0); |
1117 | |
|
1118 | 0 | c = merge_chars[idx]; |
1119 | 0 | graph_line_write_column(line, &graph->new_columns[par_column], c); |
1120 | 0 | if (idx == 2) { |
1121 | 0 | if (graph->edges_added > 0 || j < graph->num_parents - 1) |
1122 | 0 | graph_line_addch(line, ' '); |
1123 | 0 | } else { |
1124 | 0 | idx++; |
1125 | 0 | } |
1126 | 0 | parents = next_interesting_parent(graph, parents); |
1127 | 0 | } |
1128 | 0 | if (graph->edges_added == 0) |
1129 | 0 | graph_line_addch(line, ' '); |
1130 | |
|
1131 | 0 | } else if (seen_this) { |
1132 | 0 | if (graph->edges_added > 0) |
1133 | 0 | graph_line_write_column(line, col, '\\'); |
1134 | 0 | else |
1135 | 0 | graph_line_write_column(line, col, '|'); |
1136 | 0 | graph_line_addch(line, ' '); |
1137 | 0 | } else { |
1138 | 0 | graph_line_write_column(line, col, '|'); |
1139 | 0 | if (graph->merge_layout != 0 || i != graph->commit_index - 1) { |
1140 | 0 | if (parent_col) |
1141 | 0 | graph_line_write_column( |
1142 | 0 | line, parent_col, '_'); |
1143 | 0 | else |
1144 | 0 | graph_line_addch(line, ' '); |
1145 | 0 | } |
1146 | 0 | } |
1147 | |
|
1148 | 0 | if (col_commit == first_parent->item) |
1149 | 0 | parent_col = col; |
1150 | 0 | } |
1151 | | |
1152 | | /* |
1153 | | * Update graph->state |
1154 | | */ |
1155 | 0 | if (graph_is_mapping_correct(graph)) |
1156 | 0 | graph_update_state(graph, GRAPH_PADDING); |
1157 | 0 | else |
1158 | 0 | graph_update_state(graph, GRAPH_COLLAPSING); |
1159 | 0 | } |
1160 | | |
1161 | | static void graph_output_collapsing_line(struct git_graph *graph, struct graph_line *line) |
1162 | 0 | { |
1163 | 0 | int i; |
1164 | 0 | short used_horizontal = 0; |
1165 | 0 | int horizontal_edge = -1; |
1166 | 0 | int horizontal_edge_target = -1; |
1167 | | |
1168 | | /* |
1169 | | * Swap the mapping and old_mapping arrays |
1170 | | */ |
1171 | 0 | SWAP(graph->mapping, graph->old_mapping); |
1172 | | |
1173 | | /* |
1174 | | * Clear out the mapping array |
1175 | | */ |
1176 | 0 | for (i = 0; i < graph->mapping_size; i++) |
1177 | 0 | graph->mapping[i] = -1; |
1178 | |
|
1179 | 0 | for (i = 0; i < graph->mapping_size; i++) { |
1180 | 0 | int target = graph->old_mapping[i]; |
1181 | 0 | if (target < 0) |
1182 | 0 | continue; |
1183 | | |
1184 | | /* |
1185 | | * Since update_columns() always inserts the leftmost |
1186 | | * column first, each branch's target location should |
1187 | | * always be either its current location or to the left of |
1188 | | * its current location. |
1189 | | * |
1190 | | * We never have to move branches to the right. This makes |
1191 | | * the graph much more legible, since whenever branches |
1192 | | * cross, only one is moving directions. |
1193 | | */ |
1194 | 0 | assert(target * 2 <= i); |
1195 | |
|
1196 | 0 | if (target * 2 == i) { |
1197 | | /* |
1198 | | * This column is already in the |
1199 | | * correct place |
1200 | | */ |
1201 | 0 | assert(graph->mapping[i] == -1); |
1202 | 0 | graph->mapping[i] = target; |
1203 | 0 | } else if (graph->mapping[i - 1] < 0) { |
1204 | | /* |
1205 | | * Nothing is to the left. |
1206 | | * Move to the left by one |
1207 | | */ |
1208 | 0 | graph->mapping[i - 1] = target; |
1209 | | /* |
1210 | | * If there isn't already an edge moving horizontally |
1211 | | * select this one. |
1212 | | */ |
1213 | 0 | if (horizontal_edge == -1) { |
1214 | 0 | int j; |
1215 | 0 | horizontal_edge = i; |
1216 | 0 | horizontal_edge_target = target; |
1217 | | /* |
1218 | | * The variable target is the index of the graph |
1219 | | * column, and therefore target*2+3 is the |
1220 | | * actual screen column of the first horizontal |
1221 | | * line. |
1222 | | */ |
1223 | 0 | for (j = (target * 2)+3; j < (i - 2); j += 2) |
1224 | 0 | graph->mapping[j] = target; |
1225 | 0 | } |
1226 | 0 | } else if (graph->mapping[i - 1] == target) { |
1227 | | /* |
1228 | | * There is a branch line to our left |
1229 | | * already, and it is our target. We |
1230 | | * combine with this line, since we share |
1231 | | * the same parent commit. |
1232 | | * |
1233 | | * We don't have to add anything to the |
1234 | | * output or mapping, since the |
1235 | | * existing branch line has already taken |
1236 | | * care of it. |
1237 | | */ |
1238 | 0 | } else { |
1239 | | /* |
1240 | | * There is a branch line to our left, |
1241 | | * but it isn't our target. We need to |
1242 | | * cross over it. |
1243 | | * |
1244 | | * The space just to the left of this |
1245 | | * branch should always be empty. |
1246 | | */ |
1247 | 0 | assert(graph->mapping[i - 1] > target); |
1248 | 0 | assert(graph->mapping[i - 2] < 0); |
1249 | 0 | graph->mapping[i - 2] = target; |
1250 | | /* |
1251 | | * Mark this branch as the horizontal edge to |
1252 | | * prevent any other edges from moving |
1253 | | * horizontally. |
1254 | | */ |
1255 | 0 | if (horizontal_edge == -1) { |
1256 | 0 | int j; |
1257 | 0 | horizontal_edge_target = target; |
1258 | 0 | horizontal_edge = i - 1; |
1259 | |
|
1260 | 0 | for (j = (target * 2) + 3; j < (i - 2); j += 2) |
1261 | 0 | graph->mapping[j] = target; |
1262 | 0 | } |
1263 | 0 | } |
1264 | 0 | } |
1265 | | |
1266 | | /* |
1267 | | * Copy the current mapping array into old_mapping |
1268 | | */ |
1269 | 0 | COPY_ARRAY(graph->old_mapping, graph->mapping, graph->mapping_size); |
1270 | | |
1271 | | /* |
1272 | | * The new mapping may be 1 smaller than the old mapping |
1273 | | */ |
1274 | 0 | if (graph->mapping[graph->mapping_size - 1] < 0) |
1275 | 0 | graph->mapping_size--; |
1276 | | |
1277 | | /* |
1278 | | * Output out a line based on the new mapping info |
1279 | | */ |
1280 | 0 | for (i = 0; i < graph->mapping_size; i++) { |
1281 | 0 | int target = graph->mapping[i]; |
1282 | 0 | if (target < 0) |
1283 | 0 | graph_line_addch(line, ' '); |
1284 | 0 | else if (target * 2 == i) |
1285 | 0 | graph_line_write_column(line, &graph->new_columns[target], '|'); |
1286 | 0 | else if (target == horizontal_edge_target && |
1287 | 0 | i != horizontal_edge - 1) { |
1288 | | /* |
1289 | | * Set the mappings for all but the |
1290 | | * first segment to -1 so that they |
1291 | | * won't continue into the next line. |
1292 | | */ |
1293 | 0 | if (i != (target * 2)+3) |
1294 | 0 | graph->mapping[i] = -1; |
1295 | 0 | used_horizontal = 1; |
1296 | 0 | graph_line_write_column(line, &graph->new_columns[target], '_'); |
1297 | 0 | } else { |
1298 | 0 | if (used_horizontal && i < horizontal_edge) |
1299 | 0 | graph->mapping[i] = -1; |
1300 | 0 | graph_line_write_column(line, &graph->new_columns[target], '/'); |
1301 | |
|
1302 | 0 | } |
1303 | 0 | } |
1304 | | |
1305 | | /* |
1306 | | * If graph->mapping indicates that all of the branch lines |
1307 | | * are already in the correct positions, we are done. |
1308 | | * Otherwise, we need to collapse some branch lines together. |
1309 | | */ |
1310 | 0 | if (graph_is_mapping_correct(graph)) |
1311 | 0 | graph_update_state(graph, GRAPH_PADDING); |
1312 | 0 | } |
1313 | | |
1314 | | int graph_next_line(struct git_graph *graph, struct strbuf *sb) |
1315 | 0 | { |
1316 | 0 | int shown_commit_line = 0; |
1317 | 0 | struct graph_line line = { .buf = sb, .width = 0 }; |
1318 | | |
1319 | | /* |
1320 | | * We could conceivable be called with a NULL commit |
1321 | | * if our caller has a bug, and invokes graph_next_line() |
1322 | | * immediately after graph_init(), without first calling |
1323 | | * graph_update(). Return without outputting anything in this |
1324 | | * case. |
1325 | | */ |
1326 | 0 | if (!graph->commit) |
1327 | 0 | return -1; |
1328 | | |
1329 | 0 | switch (graph->state) { |
1330 | 0 | case GRAPH_PADDING: |
1331 | 0 | graph_output_padding_line(graph, &line); |
1332 | 0 | break; |
1333 | 0 | case GRAPH_SKIP: |
1334 | 0 | graph_output_skip_line(graph, &line); |
1335 | 0 | break; |
1336 | 0 | case GRAPH_PRE_COMMIT: |
1337 | 0 | graph_output_pre_commit_line(graph, &line); |
1338 | 0 | break; |
1339 | 0 | case GRAPH_COMMIT: |
1340 | 0 | graph_output_commit_line(graph, &line); |
1341 | 0 | shown_commit_line = 1; |
1342 | 0 | break; |
1343 | 0 | case GRAPH_POST_MERGE: |
1344 | 0 | graph_output_post_merge_line(graph, &line); |
1345 | 0 | break; |
1346 | 0 | case GRAPH_COLLAPSING: |
1347 | 0 | graph_output_collapsing_line(graph, &line); |
1348 | 0 | break; |
1349 | 0 | } |
1350 | | |
1351 | 0 | graph_pad_horizontally(graph, &line); |
1352 | 0 | return shown_commit_line; |
1353 | 0 | } |
1354 | | |
1355 | | static void graph_padding_line(struct git_graph *graph, struct strbuf *sb) |
1356 | 0 | { |
1357 | 0 | int i; |
1358 | 0 | struct graph_line line = { .buf = sb, .width = 0 }; |
1359 | |
|
1360 | 0 | if (graph->state != GRAPH_COMMIT) { |
1361 | 0 | graph_next_line(graph, sb); |
1362 | 0 | return; |
1363 | 0 | } |
1364 | | |
1365 | | /* |
1366 | | * Output the row containing this commit |
1367 | | * Iterate up to and including graph->num_columns, |
1368 | | * since the current commit may not be in any of the existing |
1369 | | * columns. (This happens when the current commit doesn't have any |
1370 | | * children that we have already processed.) |
1371 | | */ |
1372 | 0 | for (i = 0; i < graph->num_columns; i++) { |
1373 | 0 | struct column *col = &graph->columns[i]; |
1374 | |
|
1375 | 0 | graph_line_write_column(&line, col, '|'); |
1376 | |
|
1377 | 0 | if (col->commit == graph->commit && graph->num_parents > 2) { |
1378 | 0 | int len = (graph->num_parents - 2) * 2; |
1379 | 0 | graph_line_addchars(&line, ' ', len); |
1380 | 0 | } else { |
1381 | 0 | graph_line_addch(&line, ' '); |
1382 | 0 | } |
1383 | 0 | } |
1384 | |
|
1385 | 0 | graph_pad_horizontally(graph, &line); |
1386 | | |
1387 | | /* |
1388 | | * Update graph->prev_state since we have output a padding line |
1389 | | */ |
1390 | 0 | graph->prev_state = GRAPH_PADDING; |
1391 | 0 | } |
1392 | | |
1393 | | int graph_is_commit_finished(struct git_graph const *graph) |
1394 | 0 | { |
1395 | 0 | return (graph->state == GRAPH_PADDING); |
1396 | 0 | } |
1397 | | |
1398 | | void graph_show_commit(struct git_graph *graph) |
1399 | 0 | { |
1400 | 0 | struct strbuf msgbuf = STRBUF_INIT; |
1401 | 0 | int shown_commit_line = 0; |
1402 | |
|
1403 | 0 | graph_show_line_prefix(default_diffopt); |
1404 | |
|
1405 | 0 | if (!graph) |
1406 | 0 | return; |
1407 | | |
1408 | | /* |
1409 | | * When showing a diff of a merge against each of its parents, we |
1410 | | * are called once for each parent without graph_update having been |
1411 | | * called. In this case, simply output a single padding line. |
1412 | | */ |
1413 | 0 | if (graph_is_commit_finished(graph)) { |
1414 | 0 | graph_show_padding(graph); |
1415 | 0 | shown_commit_line = 1; |
1416 | 0 | } |
1417 | |
|
1418 | 0 | while (!shown_commit_line && !graph_is_commit_finished(graph)) { |
1419 | 0 | shown_commit_line = graph_next_line(graph, &msgbuf); |
1420 | 0 | fwrite(msgbuf.buf, sizeof(char), msgbuf.len, |
1421 | 0 | graph->revs->diffopt.file); |
1422 | 0 | if (!shown_commit_line) { |
1423 | 0 | putc('\n', graph->revs->diffopt.file); |
1424 | 0 | graph_show_line_prefix(&graph->revs->diffopt); |
1425 | 0 | } |
1426 | 0 | strbuf_setlen(&msgbuf, 0); |
1427 | 0 | } |
1428 | |
|
1429 | 0 | strbuf_release(&msgbuf); |
1430 | 0 | } |
1431 | | |
1432 | | void graph_show_oneline(struct git_graph *graph) |
1433 | 0 | { |
1434 | 0 | struct strbuf msgbuf = STRBUF_INIT; |
1435 | |
|
1436 | 0 | graph_show_line_prefix(default_diffopt); |
1437 | |
|
1438 | 0 | if (!graph) |
1439 | 0 | return; |
1440 | | |
1441 | 0 | graph_next_line(graph, &msgbuf); |
1442 | 0 | fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file); |
1443 | 0 | strbuf_release(&msgbuf); |
1444 | 0 | } |
1445 | | |
1446 | | void graph_show_padding(struct git_graph *graph) |
1447 | 0 | { |
1448 | 0 | struct strbuf msgbuf = STRBUF_INIT; |
1449 | |
|
1450 | 0 | graph_show_line_prefix(default_diffopt); |
1451 | |
|
1452 | 0 | if (!graph) |
1453 | 0 | return; |
1454 | | |
1455 | 0 | graph_padding_line(graph, &msgbuf); |
1456 | 0 | fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file); |
1457 | 0 | strbuf_release(&msgbuf); |
1458 | 0 | } |
1459 | | |
1460 | | int graph_show_remainder(struct git_graph *graph) |
1461 | 0 | { |
1462 | 0 | struct strbuf msgbuf = STRBUF_INIT; |
1463 | 0 | int shown = 0; |
1464 | |
|
1465 | 0 | graph_show_line_prefix(default_diffopt); |
1466 | |
|
1467 | 0 | if (!graph) |
1468 | 0 | return 0; |
1469 | | |
1470 | 0 | if (graph_is_commit_finished(graph)) |
1471 | 0 | return 0; |
1472 | | |
1473 | 0 | for (;;) { |
1474 | 0 | graph_next_line(graph, &msgbuf); |
1475 | 0 | fwrite(msgbuf.buf, sizeof(char), msgbuf.len, |
1476 | 0 | graph->revs->diffopt.file); |
1477 | 0 | strbuf_setlen(&msgbuf, 0); |
1478 | 0 | shown = 1; |
1479 | |
|
1480 | 0 | if (!graph_is_commit_finished(graph)) { |
1481 | 0 | putc('\n', graph->revs->diffopt.file); |
1482 | 0 | graph_show_line_prefix(&graph->revs->diffopt); |
1483 | 0 | } else { |
1484 | 0 | break; |
1485 | 0 | } |
1486 | 0 | } |
1487 | 0 | strbuf_release(&msgbuf); |
1488 | |
|
1489 | 0 | return shown; |
1490 | 0 | } |
1491 | | |
1492 | | static void graph_show_strbuf(struct git_graph *graph, |
1493 | | FILE *file, |
1494 | | struct strbuf const *sb) |
1495 | 0 | { |
1496 | 0 | char *p; |
1497 | | |
1498 | | /* |
1499 | | * Print the strbuf line by line, |
1500 | | * and display the graph info before each line but the first. |
1501 | | */ |
1502 | 0 | p = sb->buf; |
1503 | 0 | while (p) { |
1504 | 0 | size_t len; |
1505 | 0 | char *next_p = strchr(p, '\n'); |
1506 | 0 | if (next_p) { |
1507 | 0 | next_p++; |
1508 | 0 | len = next_p - p; |
1509 | 0 | } else { |
1510 | 0 | len = (sb->buf + sb->len) - p; |
1511 | 0 | } |
1512 | 0 | fwrite(p, sizeof(char), len, file); |
1513 | 0 | if (next_p && *next_p != '\0') |
1514 | 0 | graph_show_oneline(graph); |
1515 | 0 | p = next_p; |
1516 | 0 | } |
1517 | 0 | } |
1518 | | |
1519 | | void graph_show_commit_msg(struct git_graph *graph, |
1520 | | FILE *file, |
1521 | | struct strbuf const *sb) |
1522 | 0 | { |
1523 | 0 | int newline_terminated; |
1524 | | |
1525 | | /* |
1526 | | * Show the commit message |
1527 | | */ |
1528 | 0 | graph_show_strbuf(graph, file, sb); |
1529 | |
|
1530 | 0 | if (!graph) |
1531 | 0 | return; |
1532 | | |
1533 | 0 | newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n'); |
1534 | | |
1535 | | /* |
1536 | | * If there is more output needed for this commit, show it now |
1537 | | */ |
1538 | 0 | if (!graph_is_commit_finished(graph)) { |
1539 | | /* |
1540 | | * If sb doesn't have a terminating newline, print one now, |
1541 | | * so we can start the remainder of the graph output on a |
1542 | | * new line. |
1543 | | */ |
1544 | 0 | if (!newline_terminated) |
1545 | 0 | putc('\n', file); |
1546 | |
|
1547 | 0 | graph_show_remainder(graph); |
1548 | | |
1549 | | /* |
1550 | | * If sb ends with a newline, our output should too. |
1551 | | */ |
1552 | 0 | if (newline_terminated) |
1553 | 0 | putc('\n', file); |
1554 | 0 | } |
1555 | 0 | } |