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