Sync with 'maint'
[git/gitster.git] / graph.c
blobbf000fdbe11f09a771a0b8a8254004d8348d7895
1 #define USE_THE_REPOSITORY_VARIABLE
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"
12 /* Internal API */
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.
21 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb);
24 * Print a strbuf. If the graph is non-NULL, all lines but the first will be
25 * prefixed with the graph output.
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.
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().
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.
42 static void graph_show_strbuf(struct git_graph *graph,
43 FILE *file,
44 struct strbuf const *sb);
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.
53 struct column {
55 * The parent commit of this column.
57 struct commit *commit;
59 * The color to (optionally) print this column in. This is an
60 * index into column_colors.
62 unsigned short color;
65 enum graph_state {
66 GRAPH_PADDING,
67 GRAPH_SKIP,
68 GRAPH_PRE_COMMIT,
69 GRAPH_COMMIT,
70 GRAPH_POST_MERGE,
71 GRAPH_COLLAPSING
74 static void graph_show_line_prefix(const struct diff_options *diffopt)
76 if (!diffopt || !diffopt->line_prefix)
77 return;
79 fputs(diffopt->line_prefix, diffopt->file);
82 static const char **column_colors;
83 static unsigned short column_colors_max;
85 static void parse_graph_colors_config(struct strvec *colors, const char *string)
87 const char *end, *start;
89 start = string;
90 end = string + strlen(string);
91 while (start < end) {
92 const char *comma = strchrnul(start, ',');
93 char color[COLOR_MAXLEN];
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;
102 strvec_push(colors, GIT_COLOR_RESET);
105 void graph_set_column_colors(const char **colors, unsigned short colors_max)
107 column_colors = colors;
108 column_colors_max = colors_max;
111 static const char *column_get_color_code(unsigned short color)
113 return column_colors[color];
116 struct graph_line {
117 struct strbuf *buf;
118 size_t width;
121 static inline void graph_line_addch(struct graph_line *line, int c)
123 strbuf_addch(line->buf, c);
124 line->width++;
127 static inline void graph_line_addchars(struct graph_line *line, int c, size_t n)
129 strbuf_addchars(line->buf, c, n);
130 line->width += n;
133 static inline void graph_line_addstr(struct graph_line *line, const char *s)
135 strbuf_addstr(line->buf, s);
136 line->width += strlen(s);
139 static inline void graph_line_addcolor(struct graph_line *line, unsigned short color)
141 strbuf_addstr(line->buf, column_get_color_code(color));
144 static void graph_line_write_column(struct graph_line *line, const struct column *c,
145 char col_char)
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);
154 struct git_graph {
156 * The commit currently being processed
158 struct commit *commit;
159 /* The rev-info used for the current traversal */
160 struct rev_info *revs;
162 * The number of interesting parents that this commit has.
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().
168 int num_parents;
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.
174 int width;
176 * The next expansion row to print
177 * when state is GRAPH_PRE_COMMIT
179 int expansion_row;
181 * The current output state.
182 * This tells us what kind of line graph_next_line() should output.
184 enum graph_state state;
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.
190 enum graph_state prev_state;
192 * The index of the column that refers to this commit.
194 * If none of the incoming columns refer to this commit,
195 * this will be equal to num_columns.
197 int commit_index;
199 * The commit_index for the previously displayed commit.
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.
205 int prev_commit_index;
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.
213 * 0) 1)
215 * | | | *-. | | *---.
216 * | |_|/|\ \ | | |\ \ \
217 * |/| | | | | | | | | | *
219 int merge_layout;
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:
225 * | | | | | \
226 * | * | | *---. \
227 * | |\ \ | |\ \ \ \
228 * | | | | | | | | | |
230 * num_parents: 2 num_parents: 4
231 * edges_added: 1 edges_added: 3
233 * For left-skewed merges, the first parent fuses with its neighbor and
234 * so one less column is added:
236 * | | | | | \
237 * | * | | *-. \
238 * |/| | |/|\ \ \
239 * | | | | | | | |
241 * num_parents: 2 num_parents: 4
242 * edges_added: 0 edges_added: 2
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.
249 * | * \ | * |
250 * | |\ \ |/| |
251 * | | * \ | * |
253 int edges_added;
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.
259 int prev_edges_added;
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.
265 int column_capacity;
267 * The number of columns (also called "branch lines" in some places)
269 int num_columns;
271 * The number of columns in the new_columns array
273 int num_new_columns;
275 * The number of entries in the mapping array
277 int mapping_size;
279 * The column state before we output the current commit.
281 struct column *columns;
283 * The new column state after we output the current commit.
284 * Only valid when state is GRAPH_COLLAPSING.
286 struct column *new_columns;
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.)
296 * The maximum capacity of this array is always
297 * sizeof(int) * 2 * column_capacity.
299 int *mapping;
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.
306 int *old_mapping;
308 * The current default column color being used. This is
309 * stored as an index into the array column_colors.
311 unsigned short default_column_color;
314 * Scratch buffer for generating prefixes to be used with
315 * diff_output_prefix_callback().
317 struct strbuf prefix_buf;
320 static const char *diff_output_prefix_callback(struct diff_options *opt, void *data)
322 struct git_graph *graph = data;
324 assert(opt);
326 if (!graph)
327 return opt->line_prefix;
329 strbuf_reset(&graph->prefix_buf);
330 if (opt->line_prefix)
331 strbuf_addstr(&graph->prefix_buf, opt->line_prefix);
332 graph_padding_line(graph, &graph->prefix_buf);
333 return graph->prefix_buf.buf;
336 static const struct diff_options *default_diffopt;
338 void graph_setup_line_prefix(struct diff_options *diffopt)
340 default_diffopt = diffopt;
342 /* setup an output prefix callback if necessary */
343 if (diffopt && !diffopt->output_prefix)
344 diffopt->output_prefix = diff_output_prefix_callback;
347 struct git_graph *graph_init(struct rev_info *opt)
349 struct git_graph *graph = xmalloc(sizeof(struct git_graph));
351 if (!column_colors) {
352 char *string;
353 if (git_config_get_string("log.graphcolors", &string)) {
354 /* not configured -- use default */
355 graph_set_column_colors(column_colors_ansi,
356 column_colors_ansi_max);
357 } else {
358 static struct strvec custom_colors = STRVEC_INIT;
359 strvec_clear(&custom_colors);
360 parse_graph_colors_config(&custom_colors, string);
361 free(string);
362 /* graph_set_column_colors takes a max-index, not a count */
363 graph_set_column_colors(custom_colors.v,
364 custom_colors.nr - 1);
368 graph->commit = NULL;
369 graph->revs = opt;
370 graph->num_parents = 0;
371 graph->expansion_row = 0;
372 graph->state = GRAPH_PADDING;
373 graph->prev_state = GRAPH_PADDING;
374 graph->commit_index = 0;
375 graph->prev_commit_index = 0;
376 graph->merge_layout = 0;
377 graph->edges_added = 0;
378 graph->prev_edges_added = 0;
379 graph->num_columns = 0;
380 graph->num_new_columns = 0;
381 graph->mapping_size = 0;
383 * Start the column color at the maximum value, since we'll
384 * always increment it for the first commit we output.
385 * This way we start at 0 for the first commit.
387 graph->default_column_color = column_colors_max - 1;
390 * Allocate a reasonably large default number of columns
391 * We'll automatically grow columns later if we need more room.
393 graph->column_capacity = 30;
394 ALLOC_ARRAY(graph->columns, graph->column_capacity);
395 ALLOC_ARRAY(graph->new_columns, graph->column_capacity);
396 ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity);
397 ALLOC_ARRAY(graph->old_mapping, 2 * graph->column_capacity);
400 * The diff output prefix callback, with this we can make
401 * all the diff output to align with the graph lines.
403 strbuf_init(&graph->prefix_buf, 0);
404 opt->diffopt.output_prefix = diff_output_prefix_callback;
405 opt->diffopt.output_prefix_data = graph;
407 return graph;
410 void graph_clear(struct git_graph *graph)
412 if (!graph)
413 return;
415 free(graph->columns);
416 free(graph->new_columns);
417 free(graph->mapping);
418 free(graph->old_mapping);
419 strbuf_release(&graph->prefix_buf);
420 free(graph);
423 static void graph_update_state(struct git_graph *graph, enum graph_state s)
425 graph->prev_state = graph->state;
426 graph->state = s;
429 static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
431 if (graph->column_capacity >= num_columns)
432 return;
434 do {
435 graph->column_capacity *= 2;
436 } while (graph->column_capacity < num_columns);
438 REALLOC_ARRAY(graph->columns, graph->column_capacity);
439 REALLOC_ARRAY(graph->new_columns, graph->column_capacity);
440 REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2);
441 REALLOC_ARRAY(graph->old_mapping, graph->column_capacity * 2);
445 * Returns 1 if the commit will be printed in the graph output,
446 * and 0 otherwise.
448 static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
451 * If revs->boundary is set, commits whose children have
452 * been shown are always interesting, even if they have the
453 * UNINTERESTING or TREESAME flags set.
455 if (graph->revs && graph->revs->boundary) {
456 if (commit->object.flags & CHILD_SHOWN)
457 return 1;
461 * Otherwise, use get_commit_action() to see if this commit is
462 * interesting
464 return get_commit_action(graph->revs, commit) == commit_show;
467 static struct commit_list *next_interesting_parent(struct git_graph *graph,
468 struct commit_list *orig)
470 struct commit_list *list;
473 * If revs->first_parent_only is set, only the first
474 * parent is interesting. None of the others are.
476 if (graph->revs->first_parent_only)
477 return NULL;
480 * Return the next interesting commit after orig
482 for (list = orig->next; list; list = list->next) {
483 if (graph_is_interesting(graph, list->item))
484 return list;
487 return NULL;
490 static struct commit_list *first_interesting_parent(struct git_graph *graph)
492 struct commit_list *parents = graph->commit->parents;
495 * If this commit has no parents, ignore it
497 if (!parents)
498 return NULL;
501 * If the first parent is interesting, return it
503 if (graph_is_interesting(graph, parents->item))
504 return parents;
507 * Otherwise, call next_interesting_parent() to get
508 * the next interesting parent
510 return next_interesting_parent(graph, parents);
513 static unsigned short graph_get_current_column_color(const struct git_graph *graph)
515 if (!want_color(graph->revs->diffopt.use_color))
516 return column_colors_max;
517 return graph->default_column_color;
521 * Update the graph's default column color.
523 static void graph_increment_column_color(struct git_graph *graph)
525 graph->default_column_color = (graph->default_column_color + 1) %
526 column_colors_max;
529 static unsigned short graph_find_commit_color(const struct git_graph *graph,
530 const struct commit *commit)
532 int i;
533 for (i = 0; i < graph->num_columns; i++) {
534 if (graph->columns[i].commit == commit)
535 return graph->columns[i].color;
537 return graph_get_current_column_color(graph);
540 static int graph_find_new_column_by_commit(struct git_graph *graph,
541 struct commit *commit)
543 int i;
544 for (i = 0; i < graph->num_new_columns; i++) {
545 if (graph->new_columns[i].commit == commit)
546 return i;
548 return -1;
551 static void graph_insert_into_new_columns(struct git_graph *graph,
552 struct commit *commit,
553 int idx)
555 int i = graph_find_new_column_by_commit(graph, commit);
556 int mapping_idx;
559 * If the commit is not already in the new_columns array, then add it
560 * and record it as being in the final column.
562 if (i < 0) {
563 i = graph->num_new_columns++;
564 graph->new_columns[i].commit = commit;
565 graph->new_columns[i].color = graph_find_commit_color(graph, commit);
568 if (graph->num_parents > 1 && idx > -1 && graph->merge_layout == -1) {
570 * If this is the first parent of a merge, choose a layout for
571 * the merge line based on whether the parent appears in a
572 * column to the left of the merge
574 int dist, shift;
576 dist = idx - i;
577 shift = (dist > 1) ? 2 * dist - 3 : 1;
579 graph->merge_layout = (dist > 0) ? 0 : 1;
580 graph->edges_added = graph->num_parents + graph->merge_layout - 2;
582 mapping_idx = graph->width + (graph->merge_layout - 1) * shift;
583 graph->width += 2 * graph->merge_layout;
585 } else if (graph->edges_added > 0 && i == graph->mapping[graph->width - 2]) {
587 * If some columns have been added by a merge, but this commit
588 * was found in the last existing column, then adjust the
589 * numbers so that the two edges immediately join, i.e.:
591 * * | * |
592 * |\ \ => |\|
593 * | |/ | *
594 * | *
596 mapping_idx = graph->width - 2;
597 graph->edges_added = -1;
598 } else {
599 mapping_idx = graph->width;
600 graph->width += 2;
603 graph->mapping[mapping_idx] = i;
606 static void graph_update_columns(struct git_graph *graph)
608 struct commit_list *parent;
609 int max_new_columns;
610 int i, seen_this, is_commit_in_columns;
613 * Swap graph->columns with graph->new_columns
614 * graph->columns contains the state for the previous commit,
615 * and new_columns now contains the state for our commit.
617 * We'll re-use the old columns array as storage to compute the new
618 * columns list for the commit after this one.
620 SWAP(graph->columns, graph->new_columns);
621 graph->num_columns = graph->num_new_columns;
622 graph->num_new_columns = 0;
625 * Now update new_columns and mapping with the information for the
626 * commit after this one.
628 * First, make sure we have enough room. At most, there will
629 * be graph->num_columns + graph->num_parents columns for the next
630 * commit.
632 max_new_columns = graph->num_columns + graph->num_parents;
633 graph_ensure_capacity(graph, max_new_columns);
636 * Clear out graph->mapping
638 graph->mapping_size = 2 * max_new_columns;
639 for (i = 0; i < graph->mapping_size; i++)
640 graph->mapping[i] = -1;
642 graph->width = 0;
643 graph->prev_edges_added = graph->edges_added;
644 graph->edges_added = 0;
647 * Populate graph->new_columns and graph->mapping
649 * Some of the parents of this commit may already be in
650 * graph->columns. If so, graph->new_columns should only contain a
651 * single entry for each such commit. graph->mapping should
652 * contain information about where each current branch line is
653 * supposed to end up after the collapsing is performed.
655 seen_this = 0;
656 is_commit_in_columns = 1;
657 for (i = 0; i <= graph->num_columns; i++) {
658 struct commit *col_commit;
659 if (i == graph->num_columns) {
660 if (seen_this)
661 break;
662 is_commit_in_columns = 0;
663 col_commit = graph->commit;
664 } else {
665 col_commit = graph->columns[i].commit;
668 if (col_commit == graph->commit) {
669 seen_this = 1;
670 graph->commit_index = i;
671 graph->merge_layout = -1;
672 for (parent = first_interesting_parent(graph);
673 parent;
674 parent = next_interesting_parent(graph, parent)) {
676 * If this is a merge, or the start of a new
677 * childless column, increment the current
678 * color.
680 if (graph->num_parents > 1 ||
681 !is_commit_in_columns) {
682 graph_increment_column_color(graph);
684 graph_insert_into_new_columns(graph, parent->item, i);
687 * We always need to increment graph->width by at
688 * least 2, even if it has no interesting parents.
689 * The current commit always takes up at least 2
690 * spaces.
692 if (graph->num_parents == 0)
693 graph->width += 2;
694 } else {
695 graph_insert_into_new_columns(graph, col_commit, -1);
700 * Shrink mapping_size to be the minimum necessary
702 while (graph->mapping_size > 1 &&
703 graph->mapping[graph->mapping_size - 1] < 0)
704 graph->mapping_size--;
707 static int graph_num_dashed_parents(struct git_graph *graph)
709 return graph->num_parents + graph->merge_layout - 3;
712 static int graph_num_expansion_rows(struct git_graph *graph)
715 * Normally, we need two expansion rows for each dashed parent line from
716 * an octopus merge:
718 * | *
719 * | |\
720 * | | \
721 * | | \
722 * | *-. \
723 * | |\ \ \
725 * If the merge is skewed to the left, then its parents occupy one less
726 * column, and we don't need as many expansion rows to route around it;
727 * in some cases that means we don't need any expansion rows at all:
729 * | *
730 * | |\
731 * | * \
732 * |/|\ \
734 return graph_num_dashed_parents(graph) * 2;
737 static int graph_needs_pre_commit_line(struct git_graph *graph)
739 return graph->num_parents >= 3 &&
740 graph->commit_index < (graph->num_columns - 1) &&
741 graph->expansion_row < graph_num_expansion_rows(graph);
744 void graph_update(struct git_graph *graph, struct commit *commit)
746 struct commit_list *parent;
749 * Set the new commit
751 graph->commit = commit;
754 * Count how many interesting parents this commit has
756 graph->num_parents = 0;
757 for (parent = first_interesting_parent(graph);
758 parent;
759 parent = next_interesting_parent(graph, parent))
761 graph->num_parents++;
765 * Store the old commit_index in prev_commit_index.
766 * graph_update_columns() will update graph->commit_index for this
767 * commit.
769 graph->prev_commit_index = graph->commit_index;
772 * Call graph_update_columns() to update
773 * columns, new_columns, and mapping.
775 graph_update_columns(graph);
777 graph->expansion_row = 0;
780 * Update graph->state.
781 * Note that we don't call graph_update_state() here, since
782 * we don't want to update graph->prev_state. No line for
783 * graph->state was ever printed.
785 * If the previous commit didn't get to the GRAPH_PADDING state,
786 * it never finished its output. Goto GRAPH_SKIP, to print out
787 * a line to indicate that portion of the graph is missing.
789 * If there are 3 or more parents, we may need to print extra rows
790 * before the commit, to expand the branch lines around it and make
791 * room for it. We need to do this only if there is a branch row
792 * (or more) to the right of this commit.
794 * If there are less than 3 parents, we can immediately print the
795 * commit line.
797 if (graph->state != GRAPH_PADDING)
798 graph->state = GRAPH_SKIP;
799 else if (graph_needs_pre_commit_line(graph))
800 graph->state = GRAPH_PRE_COMMIT;
801 else
802 graph->state = GRAPH_COMMIT;
805 static int graph_is_mapping_correct(struct git_graph *graph)
807 int i;
810 * The mapping is up to date if each entry is at its target,
811 * or is 1 greater than its target.
812 * (If it is 1 greater than the target, '/' will be printed, so it
813 * will look correct on the next row.)
815 for (i = 0; i < graph->mapping_size; i++) {
816 int target = graph->mapping[i];
817 if (target < 0)
818 continue;
819 if (target == (i / 2))
820 continue;
821 return 0;
824 return 1;
827 static void graph_pad_horizontally(struct git_graph *graph, struct graph_line *line)
830 * Add additional spaces to the end of the strbuf, so that all
831 * lines for a particular commit have the same width.
833 * This way, fields printed to the right of the graph will remain
834 * aligned for the entire commit.
836 if (line->width < graph->width)
837 graph_line_addchars(line, ' ', graph->width - line->width);
840 static void graph_output_padding_line(struct git_graph *graph,
841 struct graph_line *line)
843 int i;
846 * Output a padding row, that leaves all branch lines unchanged
848 for (i = 0; i < graph->num_new_columns; i++) {
849 graph_line_write_column(line, &graph->new_columns[i], '|');
850 graph_line_addch(line, ' ');
855 int graph_width(struct git_graph *graph)
857 return graph->width;
861 static void graph_output_skip_line(struct git_graph *graph, struct graph_line *line)
864 * Output an ellipsis to indicate that a portion
865 * of the graph is missing.
867 graph_line_addstr(line, "...");
869 if (graph_needs_pre_commit_line(graph))
870 graph_update_state(graph, GRAPH_PRE_COMMIT);
871 else
872 graph_update_state(graph, GRAPH_COMMIT);
875 static void graph_output_pre_commit_line(struct git_graph *graph,
876 struct graph_line *line)
878 int i, seen_this;
881 * This function formats a row that increases the space around a commit
882 * with multiple parents, to make room for it. It should only be
883 * called when there are 3 or more parents.
885 * We need 2 extra rows for every parent over 2.
887 assert(graph->num_parents >= 3);
890 * graph->expansion_row tracks the current expansion row we are on.
891 * It should be in the range [0, num_expansion_rows - 1]
893 assert(0 <= graph->expansion_row &&
894 graph->expansion_row < graph_num_expansion_rows(graph));
897 * Output the row
899 seen_this = 0;
900 for (i = 0; i < graph->num_columns; i++) {
901 struct column *col = &graph->columns[i];
902 if (col->commit == graph->commit) {
903 seen_this = 1;
904 graph_line_write_column(line, col, '|');
905 graph_line_addchars(line, ' ', graph->expansion_row);
906 } else if (seen_this && (graph->expansion_row == 0)) {
908 * This is the first line of the pre-commit output.
909 * If the previous commit was a merge commit and
910 * ended in the GRAPH_POST_MERGE state, all branch
911 * lines after graph->prev_commit_index were
912 * printed as "\" on the previous line. Continue
913 * to print them as "\" on this line. Otherwise,
914 * print the branch lines as "|".
916 if (graph->prev_state == GRAPH_POST_MERGE &&
917 graph->prev_commit_index < i)
918 graph_line_write_column(line, col, '\\');
919 else
920 graph_line_write_column(line, col, '|');
921 } else if (seen_this && (graph->expansion_row > 0)) {
922 graph_line_write_column(line, col, '\\');
923 } else {
924 graph_line_write_column(line, col, '|');
926 graph_line_addch(line, ' ');
930 * Increment graph->expansion_row,
931 * and move to state GRAPH_COMMIT if necessary
933 graph->expansion_row++;
934 if (!graph_needs_pre_commit_line(graph))
935 graph_update_state(graph, GRAPH_COMMIT);
938 static void graph_output_commit_char(struct git_graph *graph, struct graph_line *line)
941 * For boundary commits, print 'o'
942 * (We should only see boundary commits when revs->boundary is set.)
944 if (graph->commit->object.flags & BOUNDARY) {
945 assert(graph->revs->boundary);
946 graph_line_addch(line, 'o');
947 return;
951 * get_revision_mark() handles all other cases without assert()
953 graph_line_addstr(line, get_revision_mark(graph->revs, graph->commit));
957 * Draw the horizontal dashes of an octopus merge.
959 static void graph_draw_octopus_merge(struct git_graph *graph, struct graph_line *line)
962 * The parents of a merge commit can be arbitrarily reordered as they
963 * are mapped onto display columns, for example this is a valid merge:
965 * | | *---.
966 * | | |\ \ \
967 * | | |/ / /
968 * | |/| | /
969 * | |_|_|/
970 * |/| | |
971 * 3 1 0 2
973 * The numbers denote which parent of the merge each visual column
974 * corresponds to; we can't assume that the parents will initially
975 * display in the order given by new_columns.
977 * To find the right color for each dash, we need to consult the
978 * mapping array, starting from the column 2 places to the right of the
979 * merge commit, and use that to find out which logical column each
980 * edge will collapse to.
982 * Commits are rendered once all edges have collapsed to their correct
983 * logcial column, so commit_index gives us the right visual offset for
984 * the merge commit.
987 int i, j;
988 struct column *col;
990 int dashed_parents = graph_num_dashed_parents(graph);
992 for (i = 0; i < dashed_parents; i++) {
993 j = graph->mapping[(graph->commit_index + i + 2) * 2];
994 col = &graph->new_columns[j];
996 graph_line_write_column(line, col, '-');
997 graph_line_write_column(line, col, (i == dashed_parents - 1) ? '.' : '-');
1000 return;
1003 static void graph_output_commit_line(struct git_graph *graph, struct graph_line *line)
1005 int seen_this = 0;
1006 int i;
1009 * Output the row containing this commit
1010 * Iterate up to and including graph->num_columns,
1011 * since the current commit may not be in any of the existing
1012 * columns. (This happens when the current commit doesn't have any
1013 * children that we have already processed.)
1015 seen_this = 0;
1016 for (i = 0; i <= graph->num_columns; i++) {
1017 struct column *col = &graph->columns[i];
1018 struct commit *col_commit;
1019 if (i == graph->num_columns) {
1020 if (seen_this)
1021 break;
1022 col_commit = graph->commit;
1023 } else {
1024 col_commit = graph->columns[i].commit;
1027 if (col_commit == graph->commit) {
1028 seen_this = 1;
1029 graph_output_commit_char(graph, line);
1031 if (graph->num_parents > 2)
1032 graph_draw_octopus_merge(graph, line);
1033 } else if (seen_this && (graph->edges_added > 1)) {
1034 graph_line_write_column(line, col, '\\');
1035 } else if (seen_this && (graph->edges_added == 1)) {
1037 * This is either a right-skewed 2-way merge
1038 * commit, or a left-skewed 3-way merge.
1039 * There is no GRAPH_PRE_COMMIT stage for such
1040 * merges, so this is the first line of output
1041 * for this commit. Check to see what the previous
1042 * line of output was.
1044 * If it was GRAPH_POST_MERGE, the branch line
1045 * coming into this commit may have been '\',
1046 * and not '|' or '/'. If so, output the branch
1047 * line as '\' on this line, instead of '|'. This
1048 * makes the output look nicer.
1050 if (graph->prev_state == GRAPH_POST_MERGE &&
1051 graph->prev_edges_added > 0 &&
1052 graph->prev_commit_index < i)
1053 graph_line_write_column(line, col, '\\');
1054 else
1055 graph_line_write_column(line, col, '|');
1056 } else if (graph->prev_state == GRAPH_COLLAPSING &&
1057 graph->old_mapping[2 * i + 1] == i &&
1058 graph->mapping[2 * i] < i) {
1059 graph_line_write_column(line, col, '/');
1060 } else {
1061 graph_line_write_column(line, col, '|');
1063 graph_line_addch(line, ' ');
1067 * Update graph->state
1069 if (graph->num_parents > 1)
1070 graph_update_state(graph, GRAPH_POST_MERGE);
1071 else if (graph_is_mapping_correct(graph))
1072 graph_update_state(graph, GRAPH_PADDING);
1073 else
1074 graph_update_state(graph, GRAPH_COLLAPSING);
1077 static const char merge_chars[] = {'/', '|', '\\'};
1079 static void graph_output_post_merge_line(struct git_graph *graph, struct graph_line *line)
1081 int seen_this = 0;
1082 int i, j;
1084 struct commit_list *first_parent = first_interesting_parent(graph);
1085 struct column *parent_col = NULL;
1088 * Output the post-merge row
1090 for (i = 0; i <= graph->num_columns; i++) {
1091 struct column *col = &graph->columns[i];
1092 struct commit *col_commit;
1093 if (i == graph->num_columns) {
1094 if (seen_this)
1095 break;
1096 col_commit = graph->commit;
1097 } else {
1098 col_commit = col->commit;
1101 if (col_commit == graph->commit) {
1103 * Since the current commit is a merge find
1104 * the columns for the parent commits in
1105 * new_columns and use those to format the
1106 * edges.
1108 struct commit_list *parents = first_parent;
1109 int par_column;
1110 int idx = graph->merge_layout;
1111 char c;
1112 seen_this = 1;
1114 for (j = 0; j < graph->num_parents; j++) {
1115 par_column = graph_find_new_column_by_commit(graph, parents->item);
1116 assert(par_column >= 0);
1118 c = merge_chars[idx];
1119 graph_line_write_column(line, &graph->new_columns[par_column], c);
1120 if (idx == 2) {
1121 if (graph->edges_added > 0 || j < graph->num_parents - 1)
1122 graph_line_addch(line, ' ');
1123 } else {
1124 idx++;
1126 parents = next_interesting_parent(graph, parents);
1128 if (graph->edges_added == 0)
1129 graph_line_addch(line, ' ');
1131 } else if (seen_this) {
1132 if (graph->edges_added > 0)
1133 graph_line_write_column(line, col, '\\');
1134 else
1135 graph_line_write_column(line, col, '|');
1136 graph_line_addch(line, ' ');
1137 } else {
1138 graph_line_write_column(line, col, '|');
1139 if (graph->merge_layout != 0 || i != graph->commit_index - 1) {
1140 if (parent_col)
1141 graph_line_write_column(
1142 line, parent_col, '_');
1143 else
1144 graph_line_addch(line, ' ');
1148 if (col_commit == first_parent->item)
1149 parent_col = col;
1153 * Update graph->state
1155 if (graph_is_mapping_correct(graph))
1156 graph_update_state(graph, GRAPH_PADDING);
1157 else
1158 graph_update_state(graph, GRAPH_COLLAPSING);
1161 static void graph_output_collapsing_line(struct git_graph *graph, struct graph_line *line)
1163 int i;
1164 short used_horizontal = 0;
1165 int horizontal_edge = -1;
1166 int horizontal_edge_target = -1;
1169 * Swap the mapping and old_mapping arrays
1171 SWAP(graph->mapping, graph->old_mapping);
1174 * Clear out the mapping array
1176 for (i = 0; i < graph->mapping_size; i++)
1177 graph->mapping[i] = -1;
1179 for (i = 0; i < graph->mapping_size; i++) {
1180 int target = graph->old_mapping[i];
1181 if (target < 0)
1182 continue;
1185 * Since update_columns() always inserts the leftmost
1186 * column first, each branch's target location should
1187 * always be either its current location or to the left of
1188 * its current location.
1190 * We never have to move branches to the right. This makes
1191 * the graph much more legible, since whenever branches
1192 * cross, only one is moving directions.
1194 assert(target * 2 <= i);
1196 if (target * 2 == i) {
1198 * This column is already in the
1199 * correct place
1201 assert(graph->mapping[i] == -1);
1202 graph->mapping[i] = target;
1203 } else if (graph->mapping[i - 1] < 0) {
1205 * Nothing is to the left.
1206 * Move to the left by one
1208 graph->mapping[i - 1] = target;
1210 * If there isn't already an edge moving horizontally
1211 * select this one.
1213 if (horizontal_edge == -1) {
1214 int j;
1215 horizontal_edge = i;
1216 horizontal_edge_target = target;
1218 * The variable target is the index of the graph
1219 * column, and therefore target*2+3 is the
1220 * actual screen column of the first horizontal
1221 * line.
1223 for (j = (target * 2)+3; j < (i - 2); j += 2)
1224 graph->mapping[j] = target;
1226 } else if (graph->mapping[i - 1] == target) {
1228 * There is a branch line to our left
1229 * already, and it is our target. We
1230 * combine with this line, since we share
1231 * the same parent commit.
1233 * We don't have to add anything to the
1234 * output or mapping, since the
1235 * existing branch line has already taken
1236 * care of it.
1238 } else {
1240 * There is a branch line to our left,
1241 * but it isn't our target. We need to
1242 * cross over it.
1244 * The space just to the left of this
1245 * branch should always be empty.
1247 assert(graph->mapping[i - 1] > target);
1248 assert(graph->mapping[i - 2] < 0);
1249 graph->mapping[i - 2] = target;
1251 * Mark this branch as the horizontal edge to
1252 * prevent any other edges from moving
1253 * horizontally.
1255 if (horizontal_edge == -1) {
1256 int j;
1257 horizontal_edge_target = target;
1258 horizontal_edge = i - 1;
1260 for (j = (target * 2) + 3; j < (i - 2); j += 2)
1261 graph->mapping[j] = target;
1267 * Copy the current mapping array into old_mapping
1269 COPY_ARRAY(graph->old_mapping, graph->mapping, graph->mapping_size);
1272 * The new mapping may be 1 smaller than the old mapping
1274 if (graph->mapping[graph->mapping_size - 1] < 0)
1275 graph->mapping_size--;
1278 * Output out a line based on the new mapping info
1280 for (i = 0; i < graph->mapping_size; i++) {
1281 int target = graph->mapping[i];
1282 if (target < 0)
1283 graph_line_addch(line, ' ');
1284 else if (target * 2 == i)
1285 graph_line_write_column(line, &graph->new_columns[target], '|');
1286 else if (target == horizontal_edge_target &&
1287 i != horizontal_edge - 1) {
1289 * Set the mappings for all but the
1290 * first segment to -1 so that they
1291 * won't continue into the next line.
1293 if (i != (target * 2)+3)
1294 graph->mapping[i] = -1;
1295 used_horizontal = 1;
1296 graph_line_write_column(line, &graph->new_columns[target], '_');
1297 } else {
1298 if (used_horizontal && i < horizontal_edge)
1299 graph->mapping[i] = -1;
1300 graph_line_write_column(line, &graph->new_columns[target], '/');
1306 * If graph->mapping indicates that all of the branch lines
1307 * are already in the correct positions, we are done.
1308 * Otherwise, we need to collapse some branch lines together.
1310 if (graph_is_mapping_correct(graph))
1311 graph_update_state(graph, GRAPH_PADDING);
1314 int graph_next_line(struct git_graph *graph, struct strbuf *sb)
1316 int shown_commit_line = 0;
1317 struct graph_line line = { .buf = sb, .width = 0 };
1320 * We could conceivable be called with a NULL commit
1321 * if our caller has a bug, and invokes graph_next_line()
1322 * immediately after graph_init(), without first calling
1323 * graph_update(). Return without outputting anything in this
1324 * case.
1326 if (!graph->commit)
1327 return -1;
1329 switch (graph->state) {
1330 case GRAPH_PADDING:
1331 graph_output_padding_line(graph, &line);
1332 break;
1333 case GRAPH_SKIP:
1334 graph_output_skip_line(graph, &line);
1335 break;
1336 case GRAPH_PRE_COMMIT:
1337 graph_output_pre_commit_line(graph, &line);
1338 break;
1339 case GRAPH_COMMIT:
1340 graph_output_commit_line(graph, &line);
1341 shown_commit_line = 1;
1342 break;
1343 case GRAPH_POST_MERGE:
1344 graph_output_post_merge_line(graph, &line);
1345 break;
1346 case GRAPH_COLLAPSING:
1347 graph_output_collapsing_line(graph, &line);
1348 break;
1351 graph_pad_horizontally(graph, &line);
1352 return shown_commit_line;
1355 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
1357 int i;
1358 struct graph_line line = { .buf = sb, .width = 0 };
1360 if (graph->state != GRAPH_COMMIT) {
1361 graph_next_line(graph, sb);
1362 return;
1366 * Output the row containing this commit
1367 * Iterate up to and including graph->num_columns,
1368 * since the current commit may not be in any of the existing
1369 * columns. (This happens when the current commit doesn't have any
1370 * children that we have already processed.)
1372 for (i = 0; i < graph->num_columns; i++) {
1373 struct column *col = &graph->columns[i];
1375 graph_line_write_column(&line, col, '|');
1377 if (col->commit == graph->commit && graph->num_parents > 2) {
1378 int len = (graph->num_parents - 2) * 2;
1379 graph_line_addchars(&line, ' ', len);
1380 } else {
1381 graph_line_addch(&line, ' ');
1385 graph_pad_horizontally(graph, &line);
1388 * Update graph->prev_state since we have output a padding line
1390 graph->prev_state = GRAPH_PADDING;
1393 int graph_is_commit_finished(struct git_graph const *graph)
1395 return (graph->state == GRAPH_PADDING);
1398 void graph_show_commit(struct git_graph *graph)
1400 struct strbuf msgbuf = STRBUF_INIT;
1401 int shown_commit_line = 0;
1403 graph_show_line_prefix(default_diffopt);
1405 if (!graph)
1406 return;
1409 * When showing a diff of a merge against each of its parents, we
1410 * are called once for each parent without graph_update having been
1411 * called. In this case, simply output a single padding line.
1413 if (graph_is_commit_finished(graph)) {
1414 graph_show_padding(graph);
1415 shown_commit_line = 1;
1418 while (!shown_commit_line && !graph_is_commit_finished(graph)) {
1419 shown_commit_line = graph_next_line(graph, &msgbuf);
1420 fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
1421 graph->revs->diffopt.file);
1422 if (!shown_commit_line) {
1423 putc('\n', graph->revs->diffopt.file);
1424 graph_show_line_prefix(&graph->revs->diffopt);
1426 strbuf_setlen(&msgbuf, 0);
1429 strbuf_release(&msgbuf);
1432 void graph_show_oneline(struct git_graph *graph)
1434 struct strbuf msgbuf = STRBUF_INIT;
1436 graph_show_line_prefix(default_diffopt);
1438 if (!graph)
1439 return;
1441 graph_next_line(graph, &msgbuf);
1442 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
1443 strbuf_release(&msgbuf);
1446 void graph_show_padding(struct git_graph *graph)
1448 struct strbuf msgbuf = STRBUF_INIT;
1450 graph_show_line_prefix(default_diffopt);
1452 if (!graph)
1453 return;
1455 graph_padding_line(graph, &msgbuf);
1456 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
1457 strbuf_release(&msgbuf);
1460 int graph_show_remainder(struct git_graph *graph)
1462 struct strbuf msgbuf = STRBUF_INIT;
1463 int shown = 0;
1465 graph_show_line_prefix(default_diffopt);
1467 if (!graph)
1468 return 0;
1470 if (graph_is_commit_finished(graph))
1471 return 0;
1473 for (;;) {
1474 graph_next_line(graph, &msgbuf);
1475 fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
1476 graph->revs->diffopt.file);
1477 strbuf_setlen(&msgbuf, 0);
1478 shown = 1;
1480 if (!graph_is_commit_finished(graph)) {
1481 putc('\n', graph->revs->diffopt.file);
1482 graph_show_line_prefix(&graph->revs->diffopt);
1483 } else {
1484 break;
1487 strbuf_release(&msgbuf);
1489 return shown;
1492 static void graph_show_strbuf(struct git_graph *graph,
1493 FILE *file,
1494 struct strbuf const *sb)
1496 char *p;
1499 * Print the strbuf line by line,
1500 * and display the graph info before each line but the first.
1502 p = sb->buf;
1503 while (p) {
1504 size_t len;
1505 char *next_p = strchr(p, '\n');
1506 if (next_p) {
1507 next_p++;
1508 len = next_p - p;
1509 } else {
1510 len = (sb->buf + sb->len) - p;
1512 fwrite(p, sizeof(char), len, file);
1513 if (next_p && *next_p != '\0')
1514 graph_show_oneline(graph);
1515 p = next_p;
1519 void graph_show_commit_msg(struct git_graph *graph,
1520 FILE *file,
1521 struct strbuf const *sb)
1523 int newline_terminated;
1526 * Show the commit message
1528 graph_show_strbuf(graph, file, sb);
1530 if (!graph)
1531 return;
1533 newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
1536 * If there is more output needed for this commit, show it now
1538 if (!graph_is_commit_finished(graph)) {
1540 * If sb doesn't have a terminating newline, print one now,
1541 * so we can start the remainder of the graph output on a
1542 * new line.
1544 if (!newline_terminated)
1545 putc('\n', file);
1547 graph_show_remainder(graph);
1550 * If sb ends with a newline, our output should too.
1552 if (newline_terminated)
1553 putc('\n', file);