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