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.
37 * If a NULL graph is supplied, the strbuf is printed as-is.
39 static void graph_show_strbuf(struct git_graph
*graph
,
41 struct strbuf
const *sb
);
45 * - Limit the number of columns, similar to the way gitk does.
46 * If we reach more than a specified number of columns, omit
47 * sections of some columns.
52 * The parent commit of this column.
54 struct commit
*commit
;
56 * The color to (optionally) print this column in. This is an
57 * index into column_colors.
71 static void graph_show_line_prefix(const struct diff_options
*diffopt
)
73 if (!diffopt
|| !diffopt
->line_prefix
)
76 fwrite(diffopt
->line_prefix
,
78 diffopt
->line_prefix_length
,
82 static const char **column_colors
;
83 static unsigned short column_colors_max
;
85 static void parse_graph_colors_config(struct argv_array
*colors
, const char *string
)
87 const char *end
, *start
;
90 end
= string
+ strlen(string
);
92 const char *comma
= strchrnul(start
, ',');
93 char color
[COLOR_MAXLEN
];
95 if (!color_parse_mem(start
, comma
- start
, color
))
96 argv_array_push(colors
, color
);
98 warning(_("ignore invalid color '%.*s' in log.graphColors"),
99 (int)(comma
- start
), start
);
102 argv_array_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 static void strbuf_write_column(struct strbuf
*sb
, const struct column
*c
,
119 if (c
->color
< column_colors_max
)
120 strbuf_addstr(sb
, column_get_color_code(c
->color
));
121 strbuf_addch(sb
, col_char
);
122 if (c
->color
< column_colors_max
)
123 strbuf_addstr(sb
, column_get_color_code(column_colors_max
));
128 * The commit currently being processed
130 struct commit
*commit
;
131 /* The rev-info used for the current traversal */
132 struct rev_info
*revs
;
134 * The number of interesting parents that this commit has.
136 * Note that this is not the same as the actual number of parents.
137 * This count excludes parents that won't be printed in the graph
138 * output, as determined by graph_is_interesting().
142 * The width of the graph output for this commit.
143 * All rows for this commit are padded to this width, so that
144 * messages printed after the graph output are aligned.
148 * The next expansion row to print
149 * when state is GRAPH_PRE_COMMIT
153 * The current output state.
154 * This tells us what kind of line graph_next_line() should output.
156 enum graph_state state
;
158 * The output state for the previous line of output.
159 * This is primarily used to determine how the first merge line
160 * should appear, based on the last line of the previous commit.
162 enum graph_state prev_state
;
164 * The index of the column that refers to this commit.
166 * If none of the incoming columns refer to this commit,
167 * this will be equal to num_columns.
171 * The commit_index for the previously displayed commit.
173 * This is used to determine how the first line of a merge
174 * graph output should appear, based on the last line of the
177 int prev_commit_index
;
179 * The maximum number of columns that can be stored in the columns
180 * and new_columns arrays. This is also half the number of entries
181 * that can be stored in the mapping and new_mapping arrays.
185 * The number of columns (also called "branch lines" in some places)
189 * The number of columns in the new_columns array
193 * The number of entries in the mapping array
197 * The column state before we output the current commit.
199 struct column
*columns
;
201 * The new column state after we output the current commit.
202 * Only valid when state is GRAPH_COLLAPSING.
204 struct column
*new_columns
;
206 * An array that tracks the current state of each
207 * character in the output line during state GRAPH_COLLAPSING.
208 * Each entry is -1 if this character is empty, or a non-negative
209 * integer if the character contains a branch line. The value of
210 * the integer indicates the target position for this branch line.
211 * (I.e., this array maps the current column positions to their
212 * desired positions.)
214 * The maximum capacity of this array is always
215 * sizeof(int) * 2 * column_capacity.
219 * A temporary array for computing the next mapping state
220 * while we are outputting a mapping line. This is stored as part
221 * of the git_graph simply so we don't have to allocate a new
222 * temporary array each time we have to output a collapsing line.
226 * The current default column color being used. This is
227 * stored as an index into the array column_colors.
229 unsigned short default_column_color
;
232 static struct strbuf
*diff_output_prefix_callback(struct diff_options
*opt
, void *data
)
234 struct git_graph
*graph
= data
;
235 static struct strbuf msgbuf
= STRBUF_INIT
;
239 strbuf_reset(&msgbuf
);
240 if (opt
->line_prefix
)
241 strbuf_add(&msgbuf
, opt
->line_prefix
,
242 opt
->line_prefix_length
);
244 graph_padding_line(graph
, &msgbuf
);
248 static const struct diff_options
*default_diffopt
;
250 void graph_setup_line_prefix(struct diff_options
*diffopt
)
252 default_diffopt
= diffopt
;
254 /* setup an output prefix callback if necessary */
255 if (diffopt
&& !diffopt
->output_prefix
)
256 diffopt
->output_prefix
= diff_output_prefix_callback
;
260 struct git_graph
*graph_init(struct rev_info
*opt
)
262 struct git_graph
*graph
= xmalloc(sizeof(struct git_graph
));
264 if (!column_colors
) {
266 if (git_config_get_string("log.graphcolors", &string
)) {
267 /* not configured -- use default */
268 graph_set_column_colors(column_colors_ansi
,
269 column_colors_ansi_max
);
271 static struct argv_array custom_colors
= ARGV_ARRAY_INIT
;
272 argv_array_clear(&custom_colors
);
273 parse_graph_colors_config(&custom_colors
, string
);
275 /* graph_set_column_colors takes a max-index, not a count */
276 graph_set_column_colors(custom_colors
.argv
,
277 custom_colors
.argc
- 1);
281 graph
->commit
= NULL
;
283 graph
->num_parents
= 0;
284 graph
->expansion_row
= 0;
285 graph
->state
= GRAPH_PADDING
;
286 graph
->prev_state
= GRAPH_PADDING
;
287 graph
->commit_index
= 0;
288 graph
->prev_commit_index
= 0;
289 graph
->num_columns
= 0;
290 graph
->num_new_columns
= 0;
291 graph
->mapping_size
= 0;
293 * Start the column color at the maximum value, since we'll
294 * always increment it for the first commit we output.
295 * This way we start at 0 for the first commit.
297 graph
->default_column_color
= column_colors_max
- 1;
300 * Allocate a reasonably large default number of columns
301 * We'll automatically grow columns later if we need more room.
303 graph
->column_capacity
= 30;
304 ALLOC_ARRAY(graph
->columns
, graph
->column_capacity
);
305 ALLOC_ARRAY(graph
->new_columns
, graph
->column_capacity
);
306 ALLOC_ARRAY(graph
->mapping
, 2 * graph
->column_capacity
);
307 ALLOC_ARRAY(graph
->new_mapping
, 2 * graph
->column_capacity
);
310 * The diff output prefix callback, with this we can make
311 * all the diff output to align with the graph lines.
313 opt
->diffopt
.output_prefix
= diff_output_prefix_callback
;
314 opt
->diffopt
.output_prefix_data
= graph
;
319 static void graph_update_state(struct git_graph
*graph
, enum graph_state s
)
321 graph
->prev_state
= graph
->state
;
325 static void graph_ensure_capacity(struct git_graph
*graph
, int num_columns
)
327 if (graph
->column_capacity
>= num_columns
)
331 graph
->column_capacity
*= 2;
332 } while (graph
->column_capacity
< num_columns
);
334 REALLOC_ARRAY(graph
->columns
, graph
->column_capacity
);
335 REALLOC_ARRAY(graph
->new_columns
, graph
->column_capacity
);
336 REALLOC_ARRAY(graph
->mapping
, graph
->column_capacity
* 2);
337 REALLOC_ARRAY(graph
->new_mapping
, graph
->column_capacity
* 2);
341 * Returns 1 if the commit will be printed in the graph output,
344 static int graph_is_interesting(struct git_graph
*graph
, struct commit
*commit
)
347 * If revs->boundary is set, commits whose children have
348 * been shown are always interesting, even if they have the
349 * UNINTERESTING or TREESAME flags set.
351 if (graph
->revs
&& graph
->revs
->boundary
) {
352 if (commit
->object
.flags
& CHILD_SHOWN
)
357 * Otherwise, use get_commit_action() to see if this commit is
360 return get_commit_action(graph
->revs
, commit
) == commit_show
;
363 static struct commit_list
*next_interesting_parent(struct git_graph
*graph
,
364 struct commit_list
*orig
)
366 struct commit_list
*list
;
369 * If revs->first_parent_only is set, only the first
370 * parent is interesting. None of the others are.
372 if (graph
->revs
->first_parent_only
)
376 * Return the next interesting commit after orig
378 for (list
= orig
->next
; list
; list
= list
->next
) {
379 if (graph_is_interesting(graph
, list
->item
))
386 static struct commit_list
*first_interesting_parent(struct git_graph
*graph
)
388 struct commit_list
*parents
= graph
->commit
->parents
;
391 * If this commit has no parents, ignore it
397 * If the first parent is interesting, return it
399 if (graph_is_interesting(graph
, parents
->item
))
403 * Otherwise, call next_interesting_parent() to get
404 * the next interesting parent
406 return next_interesting_parent(graph
, parents
);
409 static unsigned short graph_get_current_column_color(const struct git_graph
*graph
)
411 if (!want_color(graph
->revs
->diffopt
.use_color
))
412 return column_colors_max
;
413 return graph
->default_column_color
;
417 * Update the graph's default column color.
419 static void graph_increment_column_color(struct git_graph
*graph
)
421 graph
->default_column_color
= (graph
->default_column_color
+ 1) %
425 static unsigned short graph_find_commit_color(const struct git_graph
*graph
,
426 const struct commit
*commit
)
429 for (i
= 0; i
< graph
->num_columns
; i
++) {
430 if (graph
->columns
[i
].commit
== commit
)
431 return graph
->columns
[i
].color
;
433 return graph_get_current_column_color(graph
);
436 static void graph_insert_into_new_columns(struct git_graph
*graph
,
437 struct commit
*commit
,
443 * If the commit is already in the new_columns list, we don't need to
444 * add it. Just update the mapping correctly.
446 for (i
= 0; i
< graph
->num_new_columns
; i
++) {
447 if (graph
->new_columns
[i
].commit
== commit
) {
448 graph
->mapping
[*mapping_index
] = i
;
455 * This commit isn't already in new_columns. Add it.
457 graph
->new_columns
[graph
->num_new_columns
].commit
= commit
;
458 graph
->new_columns
[graph
->num_new_columns
].color
= graph_find_commit_color(graph
, commit
);
459 graph
->mapping
[*mapping_index
] = graph
->num_new_columns
;
461 graph
->num_new_columns
++;
464 static void graph_update_width(struct git_graph
*graph
,
465 int is_commit_in_existing_columns
)
468 * Compute the width needed to display the graph for this commit.
469 * This is the maximum width needed for any row. All other rows
470 * will be padded to this width.
472 * Compute the number of columns in the widest row:
473 * Count each existing column (graph->num_columns), and each new
474 * column added by this commit.
476 int max_cols
= graph
->num_columns
+ graph
->num_parents
;
479 * Even if the current commit has no parents to be printed, it
480 * still takes up a column for itself.
482 if (graph
->num_parents
< 1)
486 * We added a column for the current commit as part of
487 * graph->num_parents. If the current commit was already in
488 * graph->columns, then we have double counted it.
490 if (is_commit_in_existing_columns
)
494 * Each column takes up 2 spaces
496 graph
->width
= max_cols
* 2;
499 static void graph_update_columns(struct git_graph
*graph
)
501 struct commit_list
*parent
;
504 int i
, seen_this
, is_commit_in_columns
;
507 * Swap graph->columns with graph->new_columns
508 * graph->columns contains the state for the previous commit,
509 * and new_columns now contains the state for our commit.
511 * We'll re-use the old columns array as storage to compute the new
512 * columns list for the commit after this one.
514 SWAP(graph
->columns
, graph
->new_columns
);
515 graph
->num_columns
= graph
->num_new_columns
;
516 graph
->num_new_columns
= 0;
519 * Now update new_columns and mapping with the information for the
520 * commit after this one.
522 * First, make sure we have enough room. At most, there will
523 * be graph->num_columns + graph->num_parents columns for the next
526 max_new_columns
= graph
->num_columns
+ graph
->num_parents
;
527 graph_ensure_capacity(graph
, max_new_columns
);
530 * Clear out graph->mapping
532 graph
->mapping_size
= 2 * max_new_columns
;
533 for (i
= 0; i
< graph
->mapping_size
; i
++)
534 graph
->mapping
[i
] = -1;
537 * Populate graph->new_columns and graph->mapping
539 * Some of the parents of this commit may already be in
540 * graph->columns. If so, graph->new_columns should only contain a
541 * single entry for each such commit. graph->mapping should
542 * contain information about where each current branch line is
543 * supposed to end up after the collapsing is performed.
547 is_commit_in_columns
= 1;
548 for (i
= 0; i
<= graph
->num_columns
; i
++) {
549 struct commit
*col_commit
;
550 if (i
== graph
->num_columns
) {
553 is_commit_in_columns
= 0;
554 col_commit
= graph
->commit
;
556 col_commit
= graph
->columns
[i
].commit
;
559 if (col_commit
== graph
->commit
) {
560 int old_mapping_idx
= mapping_idx
;
562 graph
->commit_index
= i
;
563 for (parent
= first_interesting_parent(graph
);
565 parent
= next_interesting_parent(graph
, parent
)) {
567 * If this is a merge, or the start of a new
568 * childless column, increment the current
571 if (graph
->num_parents
> 1 ||
572 !is_commit_in_columns
) {
573 graph_increment_column_color(graph
);
575 graph_insert_into_new_columns(graph
,
580 * We always need to increment mapping_idx by at
581 * least 2, even if it has no interesting parents.
582 * The current commit always takes up at least 2
585 if (mapping_idx
== old_mapping_idx
)
588 graph_insert_into_new_columns(graph
, col_commit
,
594 * Shrink mapping_size to be the minimum necessary
596 while (graph
->mapping_size
> 1 &&
597 graph
->mapping
[graph
->mapping_size
- 1] < 0)
598 graph
->mapping_size
--;
601 * Compute graph->width for this commit
603 graph_update_width(graph
, is_commit_in_columns
);
606 void graph_update(struct git_graph
*graph
, struct commit
*commit
)
608 struct commit_list
*parent
;
613 graph
->commit
= commit
;
616 * Count how many interesting parents this commit has
618 graph
->num_parents
= 0;
619 for (parent
= first_interesting_parent(graph
);
621 parent
= next_interesting_parent(graph
, parent
))
623 graph
->num_parents
++;
627 * Store the old commit_index in prev_commit_index.
628 * graph_update_columns() will update graph->commit_index for this
631 graph
->prev_commit_index
= graph
->commit_index
;
634 * Call graph_update_columns() to update
635 * columns, new_columns, and mapping.
637 graph_update_columns(graph
);
639 graph
->expansion_row
= 0;
642 * Update graph->state.
643 * Note that we don't call graph_update_state() here, since
644 * we don't want to update graph->prev_state. No line for
645 * graph->state was ever printed.
647 * If the previous commit didn't get to the GRAPH_PADDING state,
648 * it never finished its output. Goto GRAPH_SKIP, to print out
649 * a line to indicate that portion of the graph is missing.
651 * If there are 3 or more parents, we may need to print extra rows
652 * before the commit, to expand the branch lines around it and make
653 * room for it. We need to do this only if there is a branch row
654 * (or more) to the right of this commit.
656 * If there are less than 3 parents, we can immediately print the
659 if (graph
->state
!= GRAPH_PADDING
)
660 graph
->state
= GRAPH_SKIP
;
661 else if (graph
->num_parents
>= 3 &&
662 graph
->commit_index
< (graph
->num_columns
- 1))
663 graph
->state
= GRAPH_PRE_COMMIT
;
665 graph
->state
= GRAPH_COMMIT
;
668 static int graph_is_mapping_correct(struct git_graph
*graph
)
673 * The mapping is up to date if each entry is at its target,
674 * or is 1 greater than its target.
675 * (If it is 1 greater than the target, '/' will be printed, so it
676 * will look correct on the next row.)
678 for (i
= 0; i
< graph
->mapping_size
; i
++) {
679 int target
= graph
->mapping
[i
];
682 if (target
== (i
/ 2))
690 static void graph_pad_horizontally(struct git_graph
*graph
, struct strbuf
*sb
,
694 * Add additional spaces to the end of the strbuf, so that all
695 * lines for a particular commit have the same width.
697 * This way, fields printed to the right of the graph will remain
698 * aligned for the entire commit.
700 if (chars_written
< graph
->width
)
701 strbuf_addchars(sb
, ' ', graph
->width
- chars_written
);
704 static void graph_output_padding_line(struct git_graph
*graph
,
710 * We could conceivable be called with a NULL commit
711 * if our caller has a bug, and invokes graph_next_line()
712 * immediately after graph_init(), without first calling
713 * graph_update(). Return without outputting anything in this
720 * Output a padding row, that leaves all branch lines unchanged
722 for (i
= 0; i
< graph
->num_new_columns
; i
++) {
723 strbuf_write_column(sb
, &graph
->new_columns
[i
], '|');
724 strbuf_addch(sb
, ' ');
727 graph_pad_horizontally(graph
, sb
, graph
->num_new_columns
* 2);
731 int graph_width(struct git_graph
*graph
)
737 static void graph_output_skip_line(struct git_graph
*graph
, struct strbuf
*sb
)
740 * Output an ellipsis to indicate that a portion
741 * of the graph is missing.
743 strbuf_addstr(sb
, "...");
744 graph_pad_horizontally(graph
, sb
, 3);
746 if (graph
->num_parents
>= 3 &&
747 graph
->commit_index
< (graph
->num_columns
- 1))
748 graph_update_state(graph
, GRAPH_PRE_COMMIT
);
750 graph_update_state(graph
, GRAPH_COMMIT
);
753 static void graph_output_pre_commit_line(struct git_graph
*graph
,
756 int num_expansion_rows
;
761 * This function formats a row that increases the space around a commit
762 * with multiple parents, to make room for it. It should only be
763 * called when there are 3 or more parents.
765 * We need 2 extra rows for every parent over 2.
767 assert(graph
->num_parents
>= 3);
768 num_expansion_rows
= (graph
->num_parents
- 2) * 2;
771 * graph->expansion_row tracks the current expansion row we are on.
772 * It should be in the range [0, num_expansion_rows - 1]
774 assert(0 <= graph
->expansion_row
&&
775 graph
->expansion_row
< num_expansion_rows
);
782 for (i
= 0; i
< graph
->num_columns
; i
++) {
783 struct column
*col
= &graph
->columns
[i
];
784 if (col
->commit
== graph
->commit
) {
786 strbuf_write_column(sb
, col
, '|');
787 strbuf_addchars(sb
, ' ', graph
->expansion_row
);
788 chars_written
+= 1 + graph
->expansion_row
;
789 } else if (seen_this
&& (graph
->expansion_row
== 0)) {
791 * This is the first line of the pre-commit output.
792 * If the previous commit was a merge commit and
793 * ended in the GRAPH_POST_MERGE state, all branch
794 * lines after graph->prev_commit_index were
795 * printed as "\" on the previous line. Continue
796 * to print them as "\" on this line. Otherwise,
797 * print the branch lines as "|".
799 if (graph
->prev_state
== GRAPH_POST_MERGE
&&
800 graph
->prev_commit_index
< i
)
801 strbuf_write_column(sb
, col
, '\\');
803 strbuf_write_column(sb
, col
, '|');
805 } else if (seen_this
&& (graph
->expansion_row
> 0)) {
806 strbuf_write_column(sb
, col
, '\\');
809 strbuf_write_column(sb
, col
, '|');
812 strbuf_addch(sb
, ' ');
816 graph_pad_horizontally(graph
, sb
, chars_written
);
819 * Increment graph->expansion_row,
820 * and move to state GRAPH_COMMIT if necessary
822 graph
->expansion_row
++;
823 if (graph
->expansion_row
>= num_expansion_rows
)
824 graph_update_state(graph
, GRAPH_COMMIT
);
827 static void graph_output_commit_char(struct git_graph
*graph
, struct strbuf
*sb
)
830 * For boundary commits, print 'o'
831 * (We should only see boundary commits when revs->boundary is set.)
833 if (graph
->commit
->object
.flags
& BOUNDARY
) {
834 assert(graph
->revs
->boundary
);
835 strbuf_addch(sb
, 'o');
840 * get_revision_mark() handles all other cases without assert()
842 strbuf_addstr(sb
, get_revision_mark(graph
->revs
, graph
->commit
));
846 * Draw the horizontal dashes of an octopus merge and return the number of
847 * characters written.
849 static int graph_draw_octopus_merge(struct git_graph
*graph
,
853 * Here dashless_parents represents the number of parents which don't
854 * need to have dashes (the edges labeled "0" and "1"). And
855 * dashful_parents are the remaining ones.
863 const int dashless_parents
= 2;
864 int dashful_parents
= graph
->num_parents
- dashless_parents
;
867 * Usually, we add one new column for each parent (like the diagram
868 * above) but sometimes the first parent goes into an existing column,
876 * In which case the number of parents will be one greater than the
877 * number of added columns.
879 int added_cols
= (graph
->num_new_columns
- graph
->num_columns
);
880 int parent_in_old_cols
= graph
->num_parents
- added_cols
;
883 * In both cases, commit_index corresponds to the edge labeled "0".
885 int first_col
= graph
->commit_index
+ dashless_parents
886 - parent_in_old_cols
;
889 for (i
= 0; i
< dashful_parents
; i
++) {
890 strbuf_write_column(sb
, &graph
->new_columns
[i
+first_col
], '-');
891 strbuf_write_column(sb
, &graph
->new_columns
[i
+first_col
],
892 i
== dashful_parents
-1 ? '.' : '-');
894 return 2 * dashful_parents
;
897 static void graph_output_commit_line(struct git_graph
*graph
, struct strbuf
*sb
)
900 int i
, chars_written
;
903 * Output the row containing this commit
904 * Iterate up to and including graph->num_columns,
905 * since the current commit may not be in any of the existing
906 * columns. (This happens when the current commit doesn't have any
907 * children that we have already processed.)
911 for (i
= 0; i
<= graph
->num_columns
; i
++) {
912 struct column
*col
= &graph
->columns
[i
];
913 struct commit
*col_commit
;
914 if (i
== graph
->num_columns
) {
917 col_commit
= graph
->commit
;
919 col_commit
= graph
->columns
[i
].commit
;
922 if (col_commit
== graph
->commit
) {
924 graph_output_commit_char(graph
, sb
);
927 if (graph
->num_parents
> 2)
928 chars_written
+= graph_draw_octopus_merge(graph
,
930 } else if (seen_this
&& (graph
->num_parents
> 2)) {
931 strbuf_write_column(sb
, col
, '\\');
933 } else if (seen_this
&& (graph
->num_parents
== 2)) {
935 * This is a 2-way merge commit.
936 * There is no GRAPH_PRE_COMMIT stage for 2-way
937 * merges, so this is the first line of output
938 * for this commit. Check to see what the previous
939 * line of output was.
941 * If it was GRAPH_POST_MERGE, the branch line
942 * coming into this commit may have been '\',
943 * and not '|' or '/'. If so, output the branch
944 * line as '\' on this line, instead of '|'. This
945 * makes the output look nicer.
947 if (graph
->prev_state
== GRAPH_POST_MERGE
&&
948 graph
->prev_commit_index
< i
)
949 strbuf_write_column(sb
, col
, '\\');
951 strbuf_write_column(sb
, col
, '|');
954 strbuf_write_column(sb
, col
, '|');
957 strbuf_addch(sb
, ' ');
961 graph_pad_horizontally(graph
, sb
, chars_written
);
964 * Update graph->state
966 if (graph
->num_parents
> 1)
967 graph_update_state(graph
, GRAPH_POST_MERGE
);
968 else if (graph_is_mapping_correct(graph
))
969 graph_update_state(graph
, GRAPH_PADDING
);
971 graph_update_state(graph
, GRAPH_COLLAPSING
);
974 static struct column
*find_new_column_by_commit(struct git_graph
*graph
,
975 struct commit
*commit
)
978 for (i
= 0; i
< graph
->num_new_columns
; i
++) {
979 if (graph
->new_columns
[i
].commit
== commit
)
980 return &graph
->new_columns
[i
];
985 static void graph_output_post_merge_line(struct git_graph
*graph
, struct strbuf
*sb
)
988 int i
, j
, chars_written
;
991 * Output the post-merge row
994 for (i
= 0; i
<= graph
->num_columns
; i
++) {
995 struct column
*col
= &graph
->columns
[i
];
996 struct commit
*col_commit
;
997 if (i
== graph
->num_columns
) {
1000 col_commit
= graph
->commit
;
1002 col_commit
= col
->commit
;
1005 if (col_commit
== graph
->commit
) {
1007 * Since the current commit is a merge find
1008 * the columns for the parent commits in
1009 * new_columns and use those to format the
1012 struct commit_list
*parents
= NULL
;
1013 struct column
*par_column
;
1015 parents
= first_interesting_parent(graph
);
1017 par_column
= find_new_column_by_commit(graph
, parents
->item
);
1020 strbuf_write_column(sb
, par_column
, '|');
1022 for (j
= 0; j
< graph
->num_parents
- 1; j
++) {
1023 parents
= next_interesting_parent(graph
, parents
);
1025 par_column
= find_new_column_by_commit(graph
, parents
->item
);
1027 strbuf_write_column(sb
, par_column
, '\\');
1028 strbuf_addch(sb
, ' ');
1030 chars_written
+= j
* 2;
1031 } else if (seen_this
) {
1032 strbuf_write_column(sb
, col
, '\\');
1033 strbuf_addch(sb
, ' ');
1036 strbuf_write_column(sb
, col
, '|');
1037 strbuf_addch(sb
, ' ');
1042 graph_pad_horizontally(graph
, sb
, chars_written
);
1045 * Update graph->state
1047 if (graph_is_mapping_correct(graph
))
1048 graph_update_state(graph
, GRAPH_PADDING
);
1050 graph_update_state(graph
, GRAPH_COLLAPSING
);
1053 static void graph_output_collapsing_line(struct git_graph
*graph
, struct strbuf
*sb
)
1056 short used_horizontal
= 0;
1057 int horizontal_edge
= -1;
1058 int horizontal_edge_target
= -1;
1061 * Clear out the new_mapping array
1063 for (i
= 0; i
< graph
->mapping_size
; i
++)
1064 graph
->new_mapping
[i
] = -1;
1066 for (i
= 0; i
< graph
->mapping_size
; i
++) {
1067 int target
= graph
->mapping
[i
];
1072 * Since update_columns() always inserts the leftmost
1073 * column first, each branch's target location should
1074 * always be either its current location or to the left of
1075 * its current location.
1077 * We never have to move branches to the right. This makes
1078 * the graph much more legible, since whenever branches
1079 * cross, only one is moving directions.
1081 assert(target
* 2 <= i
);
1083 if (target
* 2 == i
) {
1085 * This column is already in the
1088 assert(graph
->new_mapping
[i
] == -1);
1089 graph
->new_mapping
[i
] = target
;
1090 } else if (graph
->new_mapping
[i
- 1] < 0) {
1092 * Nothing is to the left.
1093 * Move to the left by one
1095 graph
->new_mapping
[i
- 1] = target
;
1097 * If there isn't already an edge moving horizontally
1100 if (horizontal_edge
== -1) {
1102 horizontal_edge
= i
;
1103 horizontal_edge_target
= target
;
1105 * The variable target is the index of the graph
1106 * column, and therefore target*2+3 is the
1107 * actual screen column of the first horizontal
1110 for (j
= (target
* 2)+3; j
< (i
- 2); j
+= 2)
1111 graph
->new_mapping
[j
] = target
;
1113 } else if (graph
->new_mapping
[i
- 1] == target
) {
1115 * There is a branch line to our left
1116 * already, and it is our target. We
1117 * combine with this line, since we share
1118 * the same parent commit.
1120 * We don't have to add anything to the
1121 * output or new_mapping, since the
1122 * existing branch line has already taken
1127 * There is a branch line to our left,
1128 * but it isn't our target. We need to
1131 * The space just to the left of this
1132 * branch should always be empty.
1134 * The branch to the left of that space
1135 * should be our eventual target.
1137 assert(graph
->new_mapping
[i
- 1] > target
);
1138 assert(graph
->new_mapping
[i
- 2] < 0);
1139 assert(graph
->new_mapping
[i
- 3] == target
);
1140 graph
->new_mapping
[i
- 2] = target
;
1142 * Mark this branch as the horizontal edge to
1143 * prevent any other edges from moving
1146 if (horizontal_edge
== -1)
1147 horizontal_edge
= i
;
1152 * The new mapping may be 1 smaller than the old mapping
1154 if (graph
->new_mapping
[graph
->mapping_size
- 1] < 0)
1155 graph
->mapping_size
--;
1158 * Output out a line based on the new mapping info
1160 for (i
= 0; i
< graph
->mapping_size
; i
++) {
1161 int target
= graph
->new_mapping
[i
];
1163 strbuf_addch(sb
, ' ');
1164 else if (target
* 2 == i
)
1165 strbuf_write_column(sb
, &graph
->new_columns
[target
], '|');
1166 else if (target
== horizontal_edge_target
&&
1167 i
!= horizontal_edge
- 1) {
1169 * Set the mappings for all but the
1170 * first segment to -1 so that they
1171 * won't continue into the next line.
1173 if (i
!= (target
* 2)+3)
1174 graph
->new_mapping
[i
] = -1;
1175 used_horizontal
= 1;
1176 strbuf_write_column(sb
, &graph
->new_columns
[target
], '_');
1178 if (used_horizontal
&& i
< horizontal_edge
)
1179 graph
->new_mapping
[i
] = -1;
1180 strbuf_write_column(sb
, &graph
->new_columns
[target
], '/');
1185 graph_pad_horizontally(graph
, sb
, graph
->mapping_size
);
1188 * Swap mapping and new_mapping
1190 SWAP(graph
->mapping
, graph
->new_mapping
);
1193 * If graph->mapping indicates that all of the branch lines
1194 * are already in the correct positions, we are done.
1195 * Otherwise, we need to collapse some branch lines together.
1197 if (graph_is_mapping_correct(graph
))
1198 graph_update_state(graph
, GRAPH_PADDING
);
1201 int graph_next_line(struct git_graph
*graph
, struct strbuf
*sb
)
1203 switch (graph
->state
) {
1205 graph_output_padding_line(graph
, sb
);
1208 graph_output_skip_line(graph
, sb
);
1210 case GRAPH_PRE_COMMIT
:
1211 graph_output_pre_commit_line(graph
, sb
);
1214 graph_output_commit_line(graph
, sb
);
1216 case GRAPH_POST_MERGE
:
1217 graph_output_post_merge_line(graph
, sb
);
1219 case GRAPH_COLLAPSING
:
1220 graph_output_collapsing_line(graph
, sb
);
1228 static void graph_padding_line(struct git_graph
*graph
, struct strbuf
*sb
)
1231 int chars_written
= 0;
1233 if (graph
->state
!= GRAPH_COMMIT
) {
1234 graph_next_line(graph
, sb
);
1239 * Output the row containing this commit
1240 * Iterate up to and including graph->num_columns,
1241 * since the current commit may not be in any of the existing
1242 * columns. (This happens when the current commit doesn't have any
1243 * children that we have already processed.)
1245 for (i
= 0; i
< graph
->num_columns
; i
++) {
1246 struct column
*col
= &graph
->columns
[i
];
1248 strbuf_write_column(sb
, col
, '|');
1251 if (col
->commit
== graph
->commit
&& graph
->num_parents
> 2) {
1252 int len
= (graph
->num_parents
- 2) * 2;
1253 strbuf_addchars(sb
, ' ', len
);
1254 chars_written
+= len
;
1256 strbuf_addch(sb
, ' ');
1261 graph_pad_horizontally(graph
, sb
, chars_written
);
1264 * Update graph->prev_state since we have output a padding line
1266 graph
->prev_state
= GRAPH_PADDING
;
1269 int graph_is_commit_finished(struct git_graph
const *graph
)
1271 return (graph
->state
== GRAPH_PADDING
);
1274 void graph_show_commit(struct git_graph
*graph
)
1276 struct strbuf msgbuf
= STRBUF_INIT
;
1277 int shown_commit_line
= 0;
1279 graph_show_line_prefix(default_diffopt
);
1285 * When showing a diff of a merge against each of its parents, we
1286 * are called once for each parent without graph_update having been
1287 * called. In this case, simply output a single padding line.
1289 if (graph_is_commit_finished(graph
)) {
1290 graph_show_padding(graph
);
1291 shown_commit_line
= 1;
1294 while (!shown_commit_line
&& !graph_is_commit_finished(graph
)) {
1295 shown_commit_line
= graph_next_line(graph
, &msgbuf
);
1296 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
,
1297 graph
->revs
->diffopt
.file
);
1298 if (!shown_commit_line
) {
1299 putc('\n', graph
->revs
->diffopt
.file
);
1300 graph_show_line_prefix(&graph
->revs
->diffopt
);
1302 strbuf_setlen(&msgbuf
, 0);
1305 strbuf_release(&msgbuf
);
1308 void graph_show_oneline(struct git_graph
*graph
)
1310 struct strbuf msgbuf
= STRBUF_INIT
;
1312 graph_show_line_prefix(default_diffopt
);
1317 graph_next_line(graph
, &msgbuf
);
1318 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
, graph
->revs
->diffopt
.file
);
1319 strbuf_release(&msgbuf
);
1322 void graph_show_padding(struct git_graph
*graph
)
1324 struct strbuf msgbuf
= STRBUF_INIT
;
1326 graph_show_line_prefix(default_diffopt
);
1331 graph_padding_line(graph
, &msgbuf
);
1332 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
, graph
->revs
->diffopt
.file
);
1333 strbuf_release(&msgbuf
);
1336 int graph_show_remainder(struct git_graph
*graph
)
1338 struct strbuf msgbuf
= STRBUF_INIT
;
1341 graph_show_line_prefix(default_diffopt
);
1346 if (graph_is_commit_finished(graph
))
1350 graph_next_line(graph
, &msgbuf
);
1351 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
,
1352 graph
->revs
->diffopt
.file
);
1353 strbuf_setlen(&msgbuf
, 0);
1356 if (!graph_is_commit_finished(graph
)) {
1357 putc('\n', graph
->revs
->diffopt
.file
);
1358 graph_show_line_prefix(&graph
->revs
->diffopt
);
1363 strbuf_release(&msgbuf
);
1368 static void graph_show_strbuf(struct git_graph
*graph
,
1370 struct strbuf
const *sb
)
1375 * Print the strbuf line by line,
1376 * and display the graph info before each line but the first.
1381 char *next_p
= strchr(p
, '\n');
1386 len
= (sb
->buf
+ sb
->len
) - p
;
1388 fwrite(p
, sizeof(char), len
, file
);
1389 if (next_p
&& *next_p
!= '\0')
1390 graph_show_oneline(graph
);
1395 void graph_show_commit_msg(struct git_graph
*graph
,
1397 struct strbuf
const *sb
)
1399 int newline_terminated
;
1402 * Show the commit message
1404 graph_show_strbuf(graph
, file
, sb
);
1409 newline_terminated
= (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\n');
1412 * If there is more output needed for this commit, show it now
1414 if (!graph_is_commit_finished(graph
)) {
1416 * If sb doesn't have a terminating newline, print one now,
1417 * so we can start the remainder of the graph output on a
1420 if (!newline_terminated
)
1423 graph_show_remainder(graph
);
1426 * If sb ends with a newline, our output should too.
1428 if (newline_terminated
)