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