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