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