7 #include "argv-array.h"
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.
18 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb);
21 * Print a strbuf. If the graph is non-NULL, all lines but the first will be
22 * prefixed with the graph output.
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.
28 * Since the first line will not include the graph output, the caller is
29 * responsible for printing this line's graph (perhaps via
30 * graph_show_commit() or graph_show_oneline()) before calling
31 * graph_show_strbuf().
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.
38 static void graph_show_strbuf(struct git_graph *graph,
40 struct strbuf const *sb);
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.
51 * The parent commit of this column.
53 struct commit *commit;
55 * The color to (optionally) print this column in. This is an
56 * index into column_colors.
70 static void graph_show_line_prefix(const struct diff_options *diffopt)
72 if (!diffopt || !diffopt->line_prefix)
75 fwrite(diffopt->line_prefix,
77 diffopt->line_prefix_length,
81 static const char **column_colors;
82 static unsigned short column_colors_max;
84 static void parse_graph_colors_config(struct argv_array *colors, const char *string)
86 const char *end, *start;
89 end = string + strlen(string);
91 const char *comma = strchrnul(start, ',');
92 char color[COLOR_MAXLEN];
94 if (!color_parse_mem(start, comma - start, color))
95 argv_array_push(colors, color);
97 warning(_("ignore invalid color '%.*s' in log.graphColors"),
98 (int)(comma - start), start);
101 argv_array_push(colors, GIT_COLOR_RESET);
104 void graph_set_column_colors(const char **colors, unsigned short colors_max)
106 column_colors = colors;
107 column_colors_max = colors_max;
110 static const char *column_get_color_code(unsigned short color)
112 return column_colors[color];
120 static inline void graph_line_addch(struct graph_line *line, int c)
122 strbuf_addch(line->buf, c);
126 static inline void graph_line_addchars(struct graph_line *line, int c, size_t n)
128 strbuf_addchars(line->buf, c, n);
132 static inline void graph_line_addstr(struct graph_line *line, const char *s)
134 strbuf_addstr(line->buf, s);
135 line->width += strlen(s);
138 static inline void graph_line_addcolor(struct graph_line *line, unsigned short color)
140 strbuf_addstr(line->buf, column_get_color_code(color));
143 static void graph_line_write_column(struct graph_line *line, const struct column *c,
146 if (c->color < column_colors_max)
147 graph_line_addcolor(line, c->color);
148 graph_line_addch(line, col_char);
149 if (c->color < column_colors_max)
150 graph_line_addcolor(line, column_colors_max);
155 * The commit currently being processed
157 struct commit *commit;
158 /* The rev-info used for the current traversal */
159 struct rev_info *revs;
161 * The number of interesting parents that this commit has.
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().
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.
175 * The next expansion row to print
176 * when state is GRAPH_PRE_COMMIT
180 * The current output state.
181 * This tells us what kind of line graph_next_line() should output.
183 enum graph_state state;
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.
189 enum graph_state prev_state;
191 * The index of the column that refers to this commit.
193 * If none of the incoming columns refer to this commit,
194 * this will be equal to num_columns.
198 * The commit_index for the previously displayed commit.
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
204 int prev_commit_index;
206 * The maximum number of columns that can be stored in the columns
207 * and new_columns arrays. This is also half the number of entries
208 * that can be stored in the mapping and new_mapping arrays.
212 * The number of columns (also called "branch lines" in some places)
216 * The number of columns in the new_columns array
220 * The number of entries in the mapping array
224 * The column state before we output the current commit.
226 struct column *columns;
228 * The new column state after we output the current commit.
229 * Only valid when state is GRAPH_COLLAPSING.
231 struct column *new_columns;
233 * An array that tracks the current state of each
234 * character in the output line during state GRAPH_COLLAPSING.
235 * Each entry is -1 if this character is empty, or a non-negative
236 * integer if the character contains a branch line. The value of
237 * the integer indicates the target position for this branch line.
238 * (I.e., this array maps the current column positions to their
239 * desired positions.)
241 * The maximum capacity of this array is always
242 * sizeof(int) * 2 * column_capacity.
246 * A temporary array for computing the next mapping state
247 * while we are outputting a mapping line. This is stored as part
248 * of the git_graph simply so we don't have to allocate a new
249 * temporary array each time we have to output a collapsing line.
253 * The current default column color being used. This is
254 * stored as an index into the array column_colors.
256 unsigned short default_column_color;
259 static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void *data)
261 struct git_graph *graph = data;
262 static struct strbuf msgbuf = STRBUF_INIT;
266 strbuf_reset(&msgbuf);
267 if (opt->line_prefix)
268 strbuf_add(&msgbuf, opt->line_prefix,
269 opt->line_prefix_length);
271 graph_padding_line(graph, &msgbuf);
275 static const struct diff_options *default_diffopt;
277 void graph_setup_line_prefix(struct diff_options *diffopt)
279 default_diffopt = diffopt;
281 /* setup an output prefix callback if necessary */
282 if (diffopt && !diffopt->output_prefix)
283 diffopt->output_prefix = diff_output_prefix_callback;
287 struct git_graph *graph_init(struct rev_info *opt)
289 struct git_graph *graph = xmalloc(sizeof(struct git_graph));
291 if (!column_colors) {
293 if (git_config_get_string("log.graphcolors", &string)) {
294 /* not configured -- use default */
295 graph_set_column_colors(column_colors_ansi,
296 column_colors_ansi_max);
298 static struct argv_array custom_colors = ARGV_ARRAY_INIT;
299 argv_array_clear(&custom_colors);
300 parse_graph_colors_config(&custom_colors, string);
302 /* graph_set_column_colors takes a max-index, not a count */
303 graph_set_column_colors(custom_colors.argv,
304 custom_colors.argc - 1);
308 graph->commit = NULL;
310 graph->num_parents = 0;
311 graph->expansion_row = 0;
312 graph->state = GRAPH_PADDING;
313 graph->prev_state = GRAPH_PADDING;
314 graph->commit_index = 0;
315 graph->prev_commit_index = 0;
316 graph->num_columns = 0;
317 graph->num_new_columns = 0;
318 graph->mapping_size = 0;
320 * Start the column color at the maximum value, since we'll
321 * always increment it for the first commit we output.
322 * This way we start at 0 for the first commit.
324 graph->default_column_color = column_colors_max - 1;
327 * Allocate a reasonably large default number of columns
328 * We'll automatically grow columns later if we need more room.
330 graph->column_capacity = 30;
331 ALLOC_ARRAY(graph->columns, graph->column_capacity);
332 ALLOC_ARRAY(graph->new_columns, graph->column_capacity);
333 ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity);
334 ALLOC_ARRAY(graph->new_mapping, 2 * graph->column_capacity);
337 * The diff output prefix callback, with this we can make
338 * all the diff output to align with the graph lines.
340 opt->diffopt.output_prefix = diff_output_prefix_callback;
341 opt->diffopt.output_prefix_data = graph;
346 static void graph_update_state(struct git_graph *graph, enum graph_state s)
348 graph->prev_state = graph->state;
352 static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
354 if (graph->column_capacity >= num_columns)
358 graph->column_capacity *= 2;
359 } while (graph->column_capacity < num_columns);
361 REALLOC_ARRAY(graph->columns, graph->column_capacity);
362 REALLOC_ARRAY(graph->new_columns, graph->column_capacity);
363 REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2);
364 REALLOC_ARRAY(graph->new_mapping, graph->column_capacity * 2);
368 * Returns 1 if the commit will be printed in the graph output,
371 static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
374 * If revs->boundary is set, commits whose children have
375 * been shown are always interesting, even if they have the
376 * UNINTERESTING or TREESAME flags set.
378 if (graph->revs && graph->revs->boundary) {
379 if (commit->object.flags & CHILD_SHOWN)
384 * Otherwise, use get_commit_action() to see if this commit is
387 return get_commit_action(graph->revs, commit) == commit_show;
390 static struct commit_list *next_interesting_parent(struct git_graph *graph,
391 struct commit_list *orig)
393 struct commit_list *list;
396 * If revs->first_parent_only is set, only the first
397 * parent is interesting. None of the others are.
399 if (graph->revs->first_parent_only)
403 * Return the next interesting commit after orig
405 for (list = orig->next; list; list = list->next) {
406 if (graph_is_interesting(graph, list->item))
413 static struct commit_list *first_interesting_parent(struct git_graph *graph)
415 struct commit_list *parents = graph->commit->parents;
418 * If this commit has no parents, ignore it
424 * If the first parent is interesting, return it
426 if (graph_is_interesting(graph, parents->item))
430 * Otherwise, call next_interesting_parent() to get
431 * the next interesting parent
433 return next_interesting_parent(graph, parents);
436 static unsigned short graph_get_current_column_color(const struct git_graph *graph)
438 if (!want_color(graph->revs->diffopt.use_color))
439 return column_colors_max;
440 return graph->default_column_color;
444 * Update the graph's default column color.
446 static void graph_increment_column_color(struct git_graph *graph)
448 graph->default_column_color = (graph->default_column_color + 1) %
452 static unsigned short graph_find_commit_color(const struct git_graph *graph,
453 const struct commit *commit)
456 for (i = 0; i < graph->num_columns; i++) {
457 if (graph->columns[i].commit == commit)
458 return graph->columns[i].color;
460 return graph_get_current_column_color(graph);
463 static void graph_insert_into_new_columns(struct git_graph *graph,
464 struct commit *commit,
470 * If the commit is already in the new_columns list, we don't need to
471 * add it. Just update the mapping correctly.
473 for (i = 0; i < graph->num_new_columns; i++) {
474 if (graph->new_columns[i].commit == commit) {
475 graph->mapping[*mapping_index] = i;
482 * This commit isn't already in new_columns. Add it.
484 graph->new_columns[graph->num_new_columns].commit = commit;
485 graph->new_columns[graph->num_new_columns].color = graph_find_commit_color(graph, commit);
486 graph->mapping[*mapping_index] = graph->num_new_columns;
488 graph->num_new_columns++;
491 static void graph_update_width(struct git_graph *graph,
492 int is_commit_in_existing_columns)
495 * Compute the width needed to display the graph for this commit.
496 * This is the maximum width needed for any row. All other rows
497 * will be padded to this width.
499 * Compute the number of columns in the widest row:
500 * Count each existing column (graph->num_columns), and each new
501 * column added by this commit.
503 int max_cols = graph->num_columns + graph->num_parents;
506 * Even if the current commit has no parents to be printed, it
507 * still takes up a column for itself.
509 if (graph->num_parents < 1)
513 * We added a column for the current commit as part of
514 * graph->num_parents. If the current commit was already in
515 * graph->columns, then we have double counted it.
517 if (is_commit_in_existing_columns)
521 * Each column takes up 2 spaces
523 graph->width = max_cols * 2;
526 static void graph_update_columns(struct git_graph *graph)
528 struct commit_list *parent;
531 int i, seen_this, is_commit_in_columns;
534 * Swap graph->columns with graph->new_columns
535 * graph->columns contains the state for the previous commit,
536 * and new_columns now contains the state for our commit.
538 * We'll re-use the old columns array as storage to compute the new
539 * columns list for the commit after this one.
541 SWAP(graph->columns, graph->new_columns);
542 graph->num_columns = graph->num_new_columns;
543 graph->num_new_columns = 0;
546 * Now update new_columns and mapping with the information for the
547 * commit after this one.
549 * First, make sure we have enough room. At most, there will
550 * be graph->num_columns + graph->num_parents columns for the next
553 max_new_columns = graph->num_columns + graph->num_parents;
554 graph_ensure_capacity(graph, max_new_columns);
557 * Clear out graph->mapping
559 graph->mapping_size = 2 * max_new_columns;
560 for (i = 0; i < graph->mapping_size; i++)
561 graph->mapping[i] = -1;
564 * Populate graph->new_columns and graph->mapping
566 * Some of the parents of this commit may already be in
567 * graph->columns. If so, graph->new_columns should only contain a
568 * single entry for each such commit. graph->mapping should
569 * contain information about where each current branch line is
570 * supposed to end up after the collapsing is performed.
574 is_commit_in_columns = 1;
575 for (i = 0; i <= graph->num_columns; i++) {
576 struct commit *col_commit;
577 if (i == graph->num_columns) {
580 is_commit_in_columns = 0;
581 col_commit = graph->commit;
583 col_commit = graph->columns[i].commit;
586 if (col_commit == graph->commit) {
587 int old_mapping_idx = mapping_idx;
589 graph->commit_index = i;
590 for (parent = first_interesting_parent(graph);
592 parent = next_interesting_parent(graph, parent)) {
594 * If this is a merge, or the start of a new
595 * childless column, increment the current
598 if (graph->num_parents > 1 ||
599 !is_commit_in_columns) {
600 graph_increment_column_color(graph);
602 graph_insert_into_new_columns(graph,
607 * We always need to increment mapping_idx by at
608 * least 2, even if it has no interesting parents.
609 * The current commit always takes up at least 2
612 if (mapping_idx == old_mapping_idx)
615 graph_insert_into_new_columns(graph, col_commit,
621 * Shrink mapping_size to be the minimum necessary
623 while (graph->mapping_size > 1 &&
624 graph->mapping[graph->mapping_size - 1] < 0)
625 graph->mapping_size--;
628 * Compute graph->width for this commit
630 graph_update_width(graph, is_commit_in_columns);
633 void graph_update(struct git_graph *graph, struct commit *commit)
635 struct commit_list *parent;
640 graph->commit = commit;
643 * Count how many interesting parents this commit has
645 graph->num_parents = 0;
646 for (parent = first_interesting_parent(graph);
648 parent = next_interesting_parent(graph, parent))
650 graph->num_parents++;
654 * Store the old commit_index in prev_commit_index.
655 * graph_update_columns() will update graph->commit_index for this
658 graph->prev_commit_index = graph->commit_index;
661 * Call graph_update_columns() to update
662 * columns, new_columns, and mapping.
664 graph_update_columns(graph);
666 graph->expansion_row = 0;
669 * Update graph->state.
670 * Note that we don't call graph_update_state() here, since
671 * we don't want to update graph->prev_state. No line for
672 * graph->state was ever printed.
674 * If the previous commit didn't get to the GRAPH_PADDING state,
675 * it never finished its output. Goto GRAPH_SKIP, to print out
676 * a line to indicate that portion of the graph is missing.
678 * If there are 3 or more parents, we may need to print extra rows
679 * before the commit, to expand the branch lines around it and make
680 * room for it. We need to do this only if there is a branch row
681 * (or more) to the right of this commit.
683 * If there are less than 3 parents, we can immediately print the
686 if (graph->state != GRAPH_PADDING)
687 graph->state = GRAPH_SKIP;
688 else if (graph->num_parents >= 3 &&
689 graph->commit_index < (graph->num_columns - 1))
690 graph->state = GRAPH_PRE_COMMIT;
692 graph->state = GRAPH_COMMIT;
695 static int graph_is_mapping_correct(struct git_graph *graph)
700 * The mapping is up to date if each entry is at its target,
701 * or is 1 greater than its target.
702 * (If it is 1 greater than the target, '/' will be printed, so it
703 * will look correct on the next row.)
705 for (i = 0; i < graph->mapping_size; i++) {
706 int target = graph->mapping[i];
709 if (target == (i / 2))
717 static void graph_pad_horizontally(struct git_graph *graph, struct graph_line *line)
720 * Add additional spaces to the end of the strbuf, so that all
721 * lines for a particular commit have the same width.
723 * This way, fields printed to the right of the graph will remain
724 * aligned for the entire commit.
726 if (line->width < graph->width)
727 graph_line_addchars(line, ' ', graph->width - line->width);
730 static void graph_output_padding_line(struct git_graph *graph,
731 struct graph_line *line)
736 * We could conceivable be called with a NULL commit
737 * if our caller has a bug, and invokes graph_next_line()
738 * immediately after graph_init(), without first calling
739 * graph_update(). Return without outputting anything in this
746 * Output a padding row, that leaves all branch lines unchanged
748 for (i = 0; i < graph->num_new_columns; i++) {
749 graph_line_write_column(line, &graph->new_columns[i], '|');
750 graph_line_addch(line, ' ');
753 graph_pad_horizontally(graph, line);
757 int graph_width(struct git_graph *graph)
763 static void graph_output_skip_line(struct git_graph *graph, struct graph_line *line)
766 * Output an ellipsis to indicate that a portion
767 * of the graph is missing.
769 graph_line_addstr(line, "...");
770 graph_pad_horizontally(graph, line);
772 if (graph->num_parents >= 3 &&
773 graph->commit_index < (graph->num_columns - 1))
774 graph_update_state(graph, GRAPH_PRE_COMMIT);
776 graph_update_state(graph, GRAPH_COMMIT);
779 static void graph_output_pre_commit_line(struct git_graph *graph,
780 struct graph_line *line)
782 int num_expansion_rows;
786 * This function formats a row that increases the space around a commit
787 * with multiple parents, to make room for it. It should only be
788 * called when there are 3 or more parents.
790 * We need 2 extra rows for every parent over 2.
792 assert(graph->num_parents >= 3);
793 num_expansion_rows = (graph->num_parents - 2) * 2;
796 * graph->expansion_row tracks the current expansion row we are on.
797 * It should be in the range [0, num_expansion_rows - 1]
799 assert(0 <= graph->expansion_row &&
800 graph->expansion_row < num_expansion_rows);
806 for (i = 0; i < graph->num_columns; i++) {
807 struct column *col = &graph->columns[i];
808 if (col->commit == graph->commit) {
810 graph_line_write_column(line, col, '|');
811 graph_line_addchars(line, ' ', graph->expansion_row);
812 } else if (seen_this && (graph->expansion_row == 0)) {
814 * This is the first line of the pre-commit output.
815 * If the previous commit was a merge commit and
816 * ended in the GRAPH_POST_MERGE state, all branch
817 * lines after graph->prev_commit_index were
818 * printed as "\" on the previous line. Continue
819 * to print them as "\" on this line. Otherwise,
820 * print the branch lines as "|".
822 if (graph->prev_state == GRAPH_POST_MERGE &&
823 graph->prev_commit_index < i)
824 graph_line_write_column(line, col, '\\');
826 graph_line_write_column(line, col, '|');
827 } else if (seen_this && (graph->expansion_row > 0)) {
828 graph_line_write_column(line, col, '\\');
830 graph_line_write_column(line, col, '|');
832 graph_line_addch(line, ' ');
835 graph_pad_horizontally(graph, line);
838 * Increment graph->expansion_row,
839 * and move to state GRAPH_COMMIT if necessary
841 graph->expansion_row++;
842 if (graph->expansion_row >= num_expansion_rows)
843 graph_update_state(graph, GRAPH_COMMIT);
846 static void graph_output_commit_char(struct git_graph *graph, struct graph_line *line)
849 * For boundary commits, print 'o'
850 * (We should only see boundary commits when revs->boundary is set.)
852 if (graph->commit->object.flags & BOUNDARY) {
853 assert(graph->revs->boundary);
854 graph_line_addch(line, 'o');
859 * get_revision_mark() handles all other cases without assert()
861 graph_line_addstr(line, get_revision_mark(graph->revs, graph->commit));
865 * Draw the horizontal dashes of an octopus merge.
867 static void graph_draw_octopus_merge(struct git_graph *graph, struct graph_line *line)
870 * Here dashless_parents represents the number of parents which don't
871 * need to have dashes (the edges labeled "0" and "1"). And
872 * dashful_parents are the remaining ones.
880 const int dashless_parents = 2;
881 int dashful_parents = graph->num_parents - dashless_parents;
884 * Usually, we add one new column for each parent (like the diagram
885 * above) but sometimes the first parent goes into an existing column,
893 * In which case the number of parents will be one greater than the
894 * number of added columns.
896 int added_cols = (graph->num_new_columns - graph->num_columns);
897 int parent_in_old_cols = graph->num_parents - added_cols;
900 * In both cases, commit_index corresponds to the edge labeled "0".
902 int first_col = graph->commit_index + dashless_parents
903 - parent_in_old_cols;
906 for (i = 0; i < dashful_parents; i++) {
907 graph_line_write_column(line, &graph->new_columns[i+first_col], '-');
908 graph_line_write_column(line, &graph->new_columns[i+first_col],
909 i == dashful_parents-1 ? '.' : '-');
913 static void graph_output_commit_line(struct git_graph *graph, struct graph_line *line)
919 * Output the row containing this commit
920 * Iterate up to and including graph->num_columns,
921 * since the current commit may not be in any of the existing
922 * columns. (This happens when the current commit doesn't have any
923 * children that we have already processed.)
926 for (i = 0; i <= graph->num_columns; i++) {
927 struct column *col = &graph->columns[i];
928 struct commit *col_commit;
929 if (i == graph->num_columns) {
932 col_commit = graph->commit;
934 col_commit = graph->columns[i].commit;
937 if (col_commit == graph->commit) {
939 graph_output_commit_char(graph, line);
941 if (graph->num_parents > 2)
942 graph_draw_octopus_merge(graph, line);
943 } else if (seen_this && (graph->num_parents > 2)) {
944 graph_line_write_column(line, col, '\\');
945 } else if (seen_this && (graph->num_parents == 2)) {
947 * This is a 2-way merge commit.
948 * There is no GRAPH_PRE_COMMIT stage for 2-way
949 * merges, so this is the first line of output
950 * for this commit. Check to see what the previous
951 * line of output was.
953 * If it was GRAPH_POST_MERGE, the branch line
954 * coming into this commit may have been '\',
955 * and not '|' or '/'. If so, output the branch
956 * line as '\' on this line, instead of '|'. This
957 * makes the output look nicer.
959 if (graph->prev_state == GRAPH_POST_MERGE &&
960 graph->prev_commit_index < i)
961 graph_line_write_column(line, col, '\\');
963 graph_line_write_column(line, col, '|');
965 graph_line_write_column(line, col, '|');
967 graph_line_addch(line, ' ');
970 graph_pad_horizontally(graph, line);
973 * Update graph->state
975 if (graph->num_parents > 1)
976 graph_update_state(graph, GRAPH_POST_MERGE);
977 else if (graph_is_mapping_correct(graph))
978 graph_update_state(graph, GRAPH_PADDING);
980 graph_update_state(graph, GRAPH_COLLAPSING);
983 static struct column *find_new_column_by_commit(struct git_graph *graph,
984 struct commit *commit)
987 for (i = 0; i < graph->num_new_columns; i++) {
988 if (graph->new_columns[i].commit == commit)
989 return &graph->new_columns[i];
994 static void graph_output_post_merge_line(struct git_graph *graph, struct graph_line *line)
1000 * Output the post-merge row
1002 for (i = 0; i <= graph->num_columns; i++) {
1003 struct column *col = &graph->columns[i];
1004 struct commit *col_commit;
1005 if (i == graph->num_columns) {
1008 col_commit = graph->commit;
1010 col_commit = col->commit;
1013 if (col_commit == graph->commit) {
1015 * Since the current commit is a merge find
1016 * the columns for the parent commits in
1017 * new_columns and use those to format the
1020 struct commit_list *parents = NULL;
1021 struct column *par_column;
1023 parents = first_interesting_parent(graph);
1025 par_column = find_new_column_by_commit(graph, parents->item);
1028 graph_line_write_column(line, par_column, '|');
1029 for (j = 0; j < graph->num_parents - 1; j++) {
1030 parents = next_interesting_parent(graph, parents);
1032 par_column = find_new_column_by_commit(graph, parents->item);
1034 graph_line_write_column(line, par_column, '\\');
1035 graph_line_addch(line, ' ');
1037 } else if (seen_this) {
1038 graph_line_write_column(line, col, '\\');
1039 graph_line_addch(line, ' ');
1041 graph_line_write_column(line, col, '|');
1042 graph_line_addch(line, ' ');
1046 graph_pad_horizontally(graph, line);
1049 * Update graph->state
1051 if (graph_is_mapping_correct(graph))
1052 graph_update_state(graph, GRAPH_PADDING);
1054 graph_update_state(graph, GRAPH_COLLAPSING);
1057 static void graph_output_collapsing_line(struct git_graph *graph, struct graph_line *line)
1060 short used_horizontal = 0;
1061 int horizontal_edge = -1;
1062 int horizontal_edge_target = -1;
1065 * Clear out the new_mapping array
1067 for (i = 0; i < graph->mapping_size; i++)
1068 graph->new_mapping[i] = -1;
1070 for (i = 0; i < graph->mapping_size; i++) {
1071 int target = graph->mapping[i];
1076 * Since update_columns() always inserts the leftmost
1077 * column first, each branch's target location should
1078 * always be either its current location or to the left of
1079 * its current location.
1081 * We never have to move branches to the right. This makes
1082 * the graph much more legible, since whenever branches
1083 * cross, only one is moving directions.
1085 assert(target * 2 <= i);
1087 if (target * 2 == i) {
1089 * This column is already in the
1092 assert(graph->new_mapping[i] == -1);
1093 graph->new_mapping[i] = target;
1094 } else if (graph->new_mapping[i - 1] < 0) {
1096 * Nothing is to the left.
1097 * Move to the left by one
1099 graph->new_mapping[i - 1] = target;
1101 * If there isn't already an edge moving horizontally
1104 if (horizontal_edge == -1) {
1106 horizontal_edge = i;
1107 horizontal_edge_target = target;
1109 * The variable target is the index of the graph
1110 * column, and therefore target*2+3 is the
1111 * actual screen column of the first horizontal
1114 for (j = (target * 2)+3; j < (i - 2); j += 2)
1115 graph->new_mapping[j] = target;
1117 } else if (graph->new_mapping[i - 1] == target) {
1119 * There is a branch line to our left
1120 * already, and it is our target. We
1121 * combine with this line, since we share
1122 * the same parent commit.
1124 * We don't have to add anything to the
1125 * output or new_mapping, since the
1126 * existing branch line has already taken
1131 * There is a branch line to our left,
1132 * but it isn't our target. We need to
1135 * The space just to the left of this
1136 * branch should always be empty.
1138 * The branch to the left of that space
1139 * should be our eventual target.
1141 assert(graph->new_mapping[i - 1] > target);
1142 assert(graph->new_mapping[i - 2] < 0);
1143 assert(graph->new_mapping[i - 3] == target);
1144 graph->new_mapping[i - 2] = target;
1146 * Mark this branch as the horizontal edge to
1147 * prevent any other edges from moving
1150 if (horizontal_edge == -1)
1151 horizontal_edge = i;
1156 * The new mapping may be 1 smaller than the old mapping
1158 if (graph->new_mapping[graph->mapping_size - 1] < 0)
1159 graph->mapping_size--;
1162 * Output out a line based on the new mapping info
1164 for (i = 0; i < graph->mapping_size; i++) {
1165 int target = graph->new_mapping[i];
1167 graph_line_addch(line, ' ');
1168 else if (target * 2 == i)
1169 graph_line_write_column(line, &graph->new_columns[target], '|');
1170 else if (target == horizontal_edge_target &&
1171 i != horizontal_edge - 1) {
1173 * Set the mappings for all but the
1174 * first segment to -1 so that they
1175 * won't continue into the next line.
1177 if (i != (target * 2)+3)
1178 graph->new_mapping[i] = -1;
1179 used_horizontal = 1;
1180 graph_line_write_column(line, &graph->new_columns[target], '_');
1182 if (used_horizontal && i < horizontal_edge)
1183 graph->new_mapping[i] = -1;
1184 graph_line_write_column(line, &graph->new_columns[target], '/');
1189 graph_pad_horizontally(graph, line);
1192 * Swap mapping and new_mapping
1194 SWAP(graph->mapping, graph->new_mapping);
1197 * If graph->mapping indicates that all of the branch lines
1198 * are already in the correct positions, we are done.
1199 * Otherwise, we need to collapse some branch lines together.
1201 if (graph_is_mapping_correct(graph))
1202 graph_update_state(graph, GRAPH_PADDING);
1205 int graph_next_line(struct git_graph *graph, struct strbuf *sb)
1207 struct graph_line line = { .buf = sb, .width = 0 };
1209 switch (graph->state) {
1211 graph_output_padding_line(graph, &line);
1214 graph_output_skip_line(graph, &line);
1216 case GRAPH_PRE_COMMIT:
1217 graph_output_pre_commit_line(graph, &line);
1220 graph_output_commit_line(graph, &line);
1222 case GRAPH_POST_MERGE:
1223 graph_output_post_merge_line(graph, &line);
1225 case GRAPH_COLLAPSING:
1226 graph_output_collapsing_line(graph, &line);
1234 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
1237 struct graph_line line = { .buf = sb, .width = 0 };
1239 if (graph->state != GRAPH_COMMIT) {
1240 graph_next_line(graph, sb);
1245 * Output the row containing this commit
1246 * Iterate up to and including graph->num_columns,
1247 * since the current commit may not be in any of the existing
1248 * columns. (This happens when the current commit doesn't have any
1249 * children that we have already processed.)
1251 for (i = 0; i < graph->num_columns; i++) {
1252 struct column *col = &graph->columns[i];
1254 graph_line_write_column(&line, col, '|');
1256 if (col->commit == graph->commit && graph->num_parents > 2) {
1257 int len = (graph->num_parents - 2) * 2;
1258 graph_line_addchars(&line, ' ', len);
1260 graph_line_addch(&line, ' ');
1264 graph_pad_horizontally(graph, &line);
1267 * Update graph->prev_state since we have output a padding line
1269 graph->prev_state = GRAPH_PADDING;
1272 int graph_is_commit_finished(struct git_graph const *graph)
1274 return (graph->state == GRAPH_PADDING);
1277 void graph_show_commit(struct git_graph *graph)
1279 struct strbuf msgbuf = STRBUF_INIT;
1280 int shown_commit_line = 0;
1282 graph_show_line_prefix(default_diffopt);
1288 * When showing a diff of a merge against each of its parents, we
1289 * are called once for each parent without graph_update having been
1290 * called. In this case, simply output a single padding line.
1292 if (graph_is_commit_finished(graph)) {
1293 graph_show_padding(graph);
1294 shown_commit_line = 1;
1297 while (!shown_commit_line && !graph_is_commit_finished(graph)) {
1298 shown_commit_line = graph_next_line(graph, &msgbuf);
1299 fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
1300 graph->revs->diffopt.file);
1301 if (!shown_commit_line) {
1302 putc('\n', graph->revs->diffopt.file);
1303 graph_show_line_prefix(&graph->revs->diffopt);
1305 strbuf_setlen(&msgbuf, 0);
1308 strbuf_release(&msgbuf);
1311 void graph_show_oneline(struct git_graph *graph)
1313 struct strbuf msgbuf = STRBUF_INIT;
1315 graph_show_line_prefix(default_diffopt);
1320 graph_next_line(graph, &msgbuf);
1321 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
1322 strbuf_release(&msgbuf);
1325 void graph_show_padding(struct git_graph *graph)
1327 struct strbuf msgbuf = STRBUF_INIT;
1329 graph_show_line_prefix(default_diffopt);
1334 graph_padding_line(graph, &msgbuf);
1335 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
1336 strbuf_release(&msgbuf);
1339 int graph_show_remainder(struct git_graph *graph)
1341 struct strbuf msgbuf = STRBUF_INIT;
1344 graph_show_line_prefix(default_diffopt);
1349 if (graph_is_commit_finished(graph))
1353 graph_next_line(graph, &msgbuf);
1354 fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
1355 graph->revs->diffopt.file);
1356 strbuf_setlen(&msgbuf, 0);
1359 if (!graph_is_commit_finished(graph)) {
1360 putc('\n', graph->revs->diffopt.file);
1361 graph_show_line_prefix(&graph->revs->diffopt);
1366 strbuf_release(&msgbuf);
1371 static void graph_show_strbuf(struct git_graph *graph,
1373 struct strbuf const *sb)
1378 * Print the strbuf line by line,
1379 * and display the graph info before each line but the first.
1384 char *next_p = strchr(p, '\n');
1389 len = (sb->buf + sb->len) - p;
1391 fwrite(p, sizeof(char), len, file);
1392 if (next_p && *next_p != '\0')
1393 graph_show_oneline(graph);
1398 void graph_show_commit_msg(struct git_graph *graph,
1400 struct strbuf const *sb)
1402 int newline_terminated;
1405 * Show the commit message
1407 graph_show_strbuf(graph, file, sb);
1412 newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
1415 * If there is more output needed for this commit, show it now
1417 if (!graph_is_commit_finished(graph)) {
1419 * If sb doesn't have a terminating newline, print one now,
1420 * so we can start the remainder of the graph output on a
1423 if (!newline_terminated)
1426 graph_show_remainder(graph);
1429 * If sb ends with a newline, our output should too.
1431 if (newline_terminated)