11 * Output the next line for a graph.
12 * This formats the next graph line into the specified strbuf. It is not
13 * terminated with a newline.
15 * Returns 1 if the line includes the current commit, and 0 otherwise.
16 * graph_next_line() will return 1 exactly once for each time
17 * graph_update() is called.
19 static int graph_next_line(struct git_graph
*graph
, struct strbuf
*sb
);
22 * Output a padding line in the graph.
23 * This is similar to graph_next_line(). However, it is guaranteed to
24 * never print the current commit line. Instead, if the commit line is
25 * next, it will simply output a line of vertical padding, extending the
26 * branch lines downwards, but leaving them otherwise unchanged.
28 static void graph_padding_line(struct git_graph
*graph
, struct strbuf
*sb
);
31 * Print a strbuf to stdout. If the graph is non-NULL, all lines but the
32 * first will be prefixed with the graph output.
34 * If the strbuf ends with a newline, the output will end after this
35 * newline. A new graph line will not be printed after the final newline.
36 * If the strbuf is empty, no output will be printed.
38 * Since the first line will not include the graph output, the caller is
39 * responsible for printing this line's graph (perhaps via
40 * graph_show_commit() or graph_show_oneline()) before calling
41 * graph_show_strbuf().
43 static void graph_show_strbuf(struct git_graph
*graph
, struct strbuf
const *sb
);
47 * - Limit the number of columns, similar to the way gitk does.
48 * If we reach more than a specified number of columns, omit
49 * sections of some columns.
54 * The parent commit of this column.
56 struct commit
*commit
;
58 * The color to (optionally) print this column in. This is an
59 * index into column_colors.
74 * The list of available column colors.
76 static char column_colors
[][COLOR_MAXLEN
] = {
85 GIT_COLOR_BOLD_YELLOW
,
87 GIT_COLOR_BOLD_MAGENTA
,
91 #define COLUMN_COLORS_MAX (ARRAY_SIZE(column_colors))
93 static const char *column_get_color_code(const struct column
*c
)
95 return column_colors
[c
->color
];
98 static void strbuf_write_column(struct strbuf
*sb
, const struct column
*c
,
101 if (c
->color
< COLUMN_COLORS_MAX
)
102 strbuf_addstr(sb
, column_get_color_code(c
));
103 strbuf_addch(sb
, col_char
);
104 if (c
->color
< COLUMN_COLORS_MAX
)
105 strbuf_addstr(sb
, GIT_COLOR_RESET
);
110 * The commit currently being processed
112 struct commit
*commit
;
113 /* The rev-info used for the current traversal */
114 struct rev_info
*revs
;
116 * The number of interesting parents that this commit has.
118 * Note that this is not the same as the actual number of parents.
119 * This count excludes parents that won't be printed in the graph
120 * output, as determined by graph_is_interesting().
124 * The width of the graph output for this commit.
125 * All rows for this commit are padded to this width, so that
126 * messages printed after the graph output are aligned.
130 * The next expansion row to print
131 * when state is GRAPH_PRE_COMMIT
135 * The current output state.
136 * This tells us what kind of line graph_next_line() should output.
138 enum graph_state state
;
140 * The output state for the previous line of output.
141 * This is primarily used to determine how the first merge line
142 * should appear, based on the last line of the previous commit.
144 enum graph_state prev_state
;
146 * The index of the column that refers to this commit.
148 * If none of the incoming columns refer to this commit,
149 * this will be equal to num_columns.
153 * The commit_index for the previously displayed commit.
155 * This is used to determine how the first line of a merge
156 * graph output should appear, based on the last line of the
159 int prev_commit_index
;
161 * The maximum number of columns that can be stored in the columns
162 * and new_columns arrays. This is also half the number of entries
163 * that can be stored in the mapping and new_mapping arrays.
167 * The number of columns (also called "branch lines" in some places)
171 * The number of columns in the new_columns array
175 * The number of entries in the mapping array
179 * The column state before we output the current commit.
181 struct column
*columns
;
183 * The new column state after we output the current commit.
184 * Only valid when state is GRAPH_COLLAPSING.
186 struct column
*new_columns
;
188 * An array that tracks the current state of each
189 * character in the output line during state GRAPH_COLLAPSING.
190 * Each entry is -1 if this character is empty, or a non-negative
191 * integer if the character contains a branch line. The value of
192 * the integer indicates the target position for this branch line.
193 * (I.e., this array maps the current column positions to their
194 * desired positions.)
196 * The maximum capacity of this array is always
197 * sizeof(int) * 2 * column_capacity.
201 * A temporary array for computing the next mapping state
202 * while we are outputting a mapping line. This is stored as part
203 * of the git_graph simply so we don't have to allocate a new
204 * temporary array each time we have to output a collapsing line.
208 * The current default column color being used. This is
209 * stored as an index into the array column_colors.
211 unsigned short default_column_color
;
214 struct git_graph
*graph_init(struct rev_info
*opt
)
216 struct git_graph
*graph
= xmalloc(sizeof(struct git_graph
));
217 graph
->commit
= NULL
;
219 graph
->num_parents
= 0;
220 graph
->expansion_row
= 0;
221 graph
->state
= GRAPH_PADDING
;
222 graph
->prev_state
= GRAPH_PADDING
;
223 graph
->commit_index
= 0;
224 graph
->prev_commit_index
= 0;
225 graph
->num_columns
= 0;
226 graph
->num_new_columns
= 0;
227 graph
->mapping_size
= 0;
229 * Start the column color at the maximum value, since we'll
230 * always increment it for the first commit we output.
231 * This way we start at 0 for the first commit.
233 graph
->default_column_color
= COLUMN_COLORS_MAX
- 1;
236 * Allocate a reasonably large default number of columns
237 * We'll automatically grow columns later if we need more room.
239 graph
->column_capacity
= 30;
240 graph
->columns
= xmalloc(sizeof(struct column
) *
241 graph
->column_capacity
);
242 graph
->new_columns
= xmalloc(sizeof(struct column
) *
243 graph
->column_capacity
);
244 graph
->mapping
= xmalloc(sizeof(int) * 2 * graph
->column_capacity
);
245 graph
->new_mapping
= xmalloc(sizeof(int) * 2 * graph
->column_capacity
);
250 static void graph_update_state(struct git_graph
*graph
, enum graph_state s
)
252 graph
->prev_state
= graph
->state
;
256 static void graph_ensure_capacity(struct git_graph
*graph
, int num_columns
)
258 if (graph
->column_capacity
>= num_columns
)
262 graph
->column_capacity
*= 2;
263 } while (graph
->column_capacity
< num_columns
);
265 graph
->columns
= xrealloc(graph
->columns
,
266 sizeof(struct column
) *
267 graph
->column_capacity
);
268 graph
->new_columns
= xrealloc(graph
->new_columns
,
269 sizeof(struct column
) *
270 graph
->column_capacity
);
271 graph
->mapping
= xrealloc(graph
->mapping
,
272 sizeof(int) * 2 * graph
->column_capacity
);
273 graph
->new_mapping
= xrealloc(graph
->new_mapping
,
274 sizeof(int) * 2 * graph
->column_capacity
);
278 * Returns 1 if the commit will be printed in the graph output,
281 static int graph_is_interesting(struct git_graph
*graph
, struct commit
*commit
)
284 * If revs->boundary is set, commits whose children have
285 * been shown are always interesting, even if they have the
286 * UNINTERESTING or TREESAME flags set.
288 if (graph
->revs
&& graph
->revs
->boundary
) {
289 if (commit
->object
.flags
& CHILD_SHOWN
)
294 * Otherwise, use get_commit_action() to see if this commit is
297 return get_commit_action(graph
->revs
, commit
) == commit_show
;
300 static struct commit_list
*next_interesting_parent(struct git_graph
*graph
,
301 struct commit_list
*orig
)
303 struct commit_list
*list
;
306 * If revs->first_parent_only is set, only the first
307 * parent is interesting. None of the others are.
309 if (graph
->revs
->first_parent_only
)
313 * Return the next interesting commit after orig
315 for (list
= orig
->next
; list
; list
= list
->next
) {
316 if (graph_is_interesting(graph
, list
->item
))
323 static struct commit_list
*first_interesting_parent(struct git_graph
*graph
)
325 struct commit_list
*parents
= graph
->commit
->parents
;
328 * If this commit has no parents, ignore it
334 * If the first parent is interesting, return it
336 if (graph_is_interesting(graph
, parents
->item
))
340 * Otherwise, call next_interesting_parent() to get
341 * the next interesting parent
343 return next_interesting_parent(graph
, parents
);
346 static unsigned short graph_get_current_column_color(const struct git_graph
*graph
)
348 if (!DIFF_OPT_TST(&graph
->revs
->diffopt
, COLOR_DIFF
))
349 return COLUMN_COLORS_MAX
;
350 return graph
->default_column_color
;
354 * Update the graph's default column color.
356 static void graph_increment_column_color(struct git_graph
*graph
)
358 graph
->default_column_color
= (graph
->default_column_color
+ 1) %
362 static unsigned short graph_find_commit_color(const struct git_graph
*graph
,
363 const struct commit
*commit
)
366 for (i
= 0; i
< graph
->num_columns
; i
++) {
367 if (graph
->columns
[i
].commit
== commit
)
368 return graph
->columns
[i
].color
;
370 return graph_get_current_column_color(graph
);
373 static void graph_insert_into_new_columns(struct git_graph
*graph
,
374 struct commit
*commit
,
380 * If the commit is already in the new_columns list, we don't need to
381 * add it. Just update the mapping correctly.
383 for (i
= 0; i
< graph
->num_new_columns
; i
++) {
384 if (graph
->new_columns
[i
].commit
== commit
) {
385 graph
->mapping
[*mapping_index
] = i
;
392 * This commit isn't already in new_columns. Add it.
394 graph
->new_columns
[graph
->num_new_columns
].commit
= commit
;
395 graph
->new_columns
[graph
->num_new_columns
].color
= graph_find_commit_color(graph
, commit
);
396 graph
->mapping
[*mapping_index
] = graph
->num_new_columns
;
398 graph
->num_new_columns
++;
401 static void graph_update_width(struct git_graph
*graph
,
402 int is_commit_in_existing_columns
)
405 * Compute the width needed to display the graph for this commit.
406 * This is the maximum width needed for any row. All other rows
407 * will be padded to this width.
409 * Compute the number of columns in the widest row:
410 * Count each existing column (graph->num_columns), and each new
411 * column added by this commit.
413 int max_cols
= graph
->num_columns
+ graph
->num_parents
;
416 * Even if the current commit has no parents to be printed, it
417 * still takes up a column for itself.
419 if (graph
->num_parents
< 1)
423 * We added a column for the the current commit as part of
424 * graph->num_parents. If the current commit was already in
425 * graph->columns, then we have double counted it.
427 if (is_commit_in_existing_columns
)
431 * Each column takes up 2 spaces
433 graph
->width
= max_cols
* 2;
436 static void graph_update_columns(struct git_graph
*graph
)
438 struct commit_list
*parent
;
439 struct column
*tmp_columns
;
442 int i
, seen_this
, is_commit_in_columns
;
445 * Swap graph->columns with graph->new_columns
446 * graph->columns contains the state for the previous commit,
447 * and new_columns now contains the state for our commit.
449 * We'll re-use the old columns array as storage to compute the new
450 * columns list for the commit after this one.
452 tmp_columns
= graph
->columns
;
453 graph
->columns
= graph
->new_columns
;
454 graph
->num_columns
= graph
->num_new_columns
;
456 graph
->new_columns
= tmp_columns
;
457 graph
->num_new_columns
= 0;
460 * Now update new_columns and mapping with the information for the
461 * commit after this one.
463 * First, make sure we have enough room. At most, there will
464 * be graph->num_columns + graph->num_parents columns for the next
467 max_new_columns
= graph
->num_columns
+ graph
->num_parents
;
468 graph_ensure_capacity(graph
, max_new_columns
);
471 * Clear out graph->mapping
473 graph
->mapping_size
= 2 * max_new_columns
;
474 for (i
= 0; i
< graph
->mapping_size
; i
++)
475 graph
->mapping
[i
] = -1;
478 * Populate graph->new_columns and graph->mapping
480 * Some of the parents of this commit may already be in
481 * graph->columns. If so, graph->new_columns should only contain a
482 * single entry for each such commit. graph->mapping should
483 * contain information about where each current branch line is
484 * supposed to end up after the collapsing is performed.
488 is_commit_in_columns
= 1;
489 for (i
= 0; i
<= graph
->num_columns
; i
++) {
490 struct commit
*col_commit
;
491 if (i
== graph
->num_columns
) {
494 is_commit_in_columns
= 0;
495 col_commit
= graph
->commit
;
497 col_commit
= graph
->columns
[i
].commit
;
500 if (col_commit
== graph
->commit
) {
501 int old_mapping_idx
= mapping_idx
;
503 graph
->commit_index
= i
;
504 for (parent
= first_interesting_parent(graph
);
506 parent
= next_interesting_parent(graph
, parent
)) {
508 * If this is a merge, or the start of a new
509 * childless column, increment the current
512 if (graph
->num_parents
> 1 ||
513 !is_commit_in_columns
) {
514 graph_increment_column_color(graph
);
516 graph_insert_into_new_columns(graph
,
521 * We always need to increment mapping_idx by at
522 * least 2, even if it has no interesting parents.
523 * The current commit always takes up at least 2
526 if (mapping_idx
== old_mapping_idx
)
529 graph_insert_into_new_columns(graph
, col_commit
,
535 * Shrink mapping_size to be the minimum necessary
537 while (graph
->mapping_size
> 1 &&
538 graph
->mapping
[graph
->mapping_size
- 1] < 0)
539 graph
->mapping_size
--;
542 * Compute graph->width for this commit
544 graph_update_width(graph
, is_commit_in_columns
);
547 void graph_update(struct git_graph
*graph
, struct commit
*commit
)
549 struct commit_list
*parent
;
554 graph
->commit
= commit
;
557 * Count how many interesting parents this commit has
559 graph
->num_parents
= 0;
560 for (parent
= first_interesting_parent(graph
);
562 parent
= next_interesting_parent(graph
, parent
))
564 graph
->num_parents
++;
568 * Store the old commit_index in prev_commit_index.
569 * graph_update_columns() will update graph->commit_index for this
572 graph
->prev_commit_index
= graph
->commit_index
;
575 * Call graph_update_columns() to update
576 * columns, new_columns, and mapping.
578 graph_update_columns(graph
);
580 graph
->expansion_row
= 0;
583 * Update graph->state.
584 * Note that we don't call graph_update_state() here, since
585 * we don't want to update graph->prev_state. No line for
586 * graph->state was ever printed.
588 * If the previous commit didn't get to the GRAPH_PADDING state,
589 * it never finished its output. Goto GRAPH_SKIP, to print out
590 * a line to indicate that portion of the graph is missing.
592 * If there are 3 or more parents, we may need to print extra rows
593 * before the commit, to expand the branch lines around it and make
594 * room for it. We need to do this only if there is a branch row
595 * (or more) to the right of this commit.
597 * If there are less than 3 parents, we can immediately print the
600 if (graph
->state
!= GRAPH_PADDING
)
601 graph
->state
= GRAPH_SKIP
;
602 else if (graph
->num_parents
>= 3 &&
603 graph
->commit_index
< (graph
->num_columns
- 1))
604 graph
->state
= GRAPH_PRE_COMMIT
;
606 graph
->state
= GRAPH_COMMIT
;
609 static int graph_is_mapping_correct(struct git_graph
*graph
)
614 * The mapping is up to date if each entry is at its target,
615 * or is 1 greater than its target.
616 * (If it is 1 greater than the target, '/' will be printed, so it
617 * will look correct on the next row.)
619 for (i
= 0; i
< graph
->mapping_size
; i
++) {
620 int target
= graph
->mapping
[i
];
623 if (target
== (i
/ 2))
631 static void graph_pad_horizontally(struct git_graph
*graph
, struct strbuf
*sb
,
635 * Add additional spaces to the end of the strbuf, so that all
636 * lines for a particular commit have the same width.
638 * This way, fields printed to the right of the graph will remain
639 * aligned for the entire commit.
642 if (chars_written
>= graph
->width
)
645 extra
= graph
->width
- chars_written
;
646 strbuf_addf(sb
, "%*s", (int) extra
, "");
649 static void graph_output_padding_line(struct git_graph
*graph
,
655 * We could conceivable be called with a NULL commit
656 * if our caller has a bug, and invokes graph_next_line()
657 * immediately after graph_init(), without first calling
658 * graph_update(). Return without outputting anything in this
665 * Output a padding row, that leaves all branch lines unchanged
667 for (i
= 0; i
< graph
->num_new_columns
; i
++) {
668 strbuf_write_column(sb
, &graph
->new_columns
[i
], '|');
669 strbuf_addch(sb
, ' ');
672 graph_pad_horizontally(graph
, sb
, graph
->num_new_columns
* 2);
675 static void graph_output_skip_line(struct git_graph
*graph
, struct strbuf
*sb
)
678 * Output an ellipsis to indicate that a portion
679 * of the graph is missing.
681 strbuf_addstr(sb
, "...");
682 graph_pad_horizontally(graph
, sb
, 3);
684 if (graph
->num_parents
>= 3 &&
685 graph
->commit_index
< (graph
->num_columns
- 1))
686 graph_update_state(graph
, GRAPH_PRE_COMMIT
);
688 graph_update_state(graph
, GRAPH_COMMIT
);
691 static void graph_output_pre_commit_line(struct git_graph
*graph
,
694 int num_expansion_rows
;
699 * This function formats a row that increases the space around a commit
700 * with multiple parents, to make room for it. It should only be
701 * called when there are 3 or more parents.
703 * We need 2 extra rows for every parent over 2.
705 assert(graph
->num_parents
>= 3);
706 num_expansion_rows
= (graph
->num_parents
- 2) * 2;
709 * graph->expansion_row tracks the current expansion row we are on.
710 * It should be in the range [0, num_expansion_rows - 1]
712 assert(0 <= graph
->expansion_row
&&
713 graph
->expansion_row
< num_expansion_rows
);
720 for (i
= 0; i
< graph
->num_columns
; i
++) {
721 struct column
*col
= &graph
->columns
[i
];
722 if (col
->commit
== graph
->commit
) {
724 strbuf_write_column(sb
, col
, '|');
725 strbuf_addf(sb
, "%*s", graph
->expansion_row
, "");
726 chars_written
+= 1 + graph
->expansion_row
;
727 } else if (seen_this
&& (graph
->expansion_row
== 0)) {
729 * This is the first line of the pre-commit output.
730 * If the previous commit was a merge commit and
731 * ended in the GRAPH_POST_MERGE state, all branch
732 * lines after graph->prev_commit_index were
733 * printed as "\" on the previous line. Continue
734 * to print them as "\" on this line. Otherwise,
735 * print the branch lines as "|".
737 if (graph
->prev_state
== GRAPH_POST_MERGE
&&
738 graph
->prev_commit_index
< i
)
739 strbuf_write_column(sb
, col
, '\\');
741 strbuf_write_column(sb
, col
, '|');
743 } else if (seen_this
&& (graph
->expansion_row
> 0)) {
744 strbuf_write_column(sb
, col
, '\\');
747 strbuf_write_column(sb
, col
, '|');
750 strbuf_addch(sb
, ' ');
754 graph_pad_horizontally(graph
, sb
, chars_written
);
757 * Increment graph->expansion_row,
758 * and move to state GRAPH_COMMIT if necessary
760 graph
->expansion_row
++;
761 if (graph
->expansion_row
>= num_expansion_rows
)
762 graph_update_state(graph
, GRAPH_COMMIT
);
765 static void graph_output_commit_char(struct git_graph
*graph
, struct strbuf
*sb
)
768 * For boundary commits, print 'o'
769 * (We should only see boundary commits when revs->boundary is set.)
771 if (graph
->commit
->object
.flags
& BOUNDARY
) {
772 assert(graph
->revs
->boundary
);
773 strbuf_addch(sb
, 'o');
778 * If revs->left_right is set, print '<' for commits that
779 * come from the left side, and '>' for commits from the right
782 if (graph
->revs
&& graph
->revs
->left_right
) {
783 if (graph
->commit
->object
.flags
& SYMMETRIC_LEFT
)
784 strbuf_addch(sb
, '<');
786 strbuf_addch(sb
, '>');
791 * Print '*' in all other cases
793 strbuf_addch(sb
, '*');
797 * Draw an octopus merge and return the number of characters written.
799 static int graph_draw_octopus_merge(struct git_graph
*graph
,
803 * Here dashless_commits represents the number of parents
804 * which don't need to have dashes (because their edges fit
805 * neatly under the commit).
807 const int dashless_commits
= 2;
810 ((graph
->num_parents
- dashless_commits
) * 2) - 1;
811 for (i
= 0; i
< num_dashes
; i
++) {
812 col_num
= (i
/ 2) + dashless_commits
;
813 strbuf_write_column(sb
, &graph
->new_columns
[col_num
], '-');
815 col_num
= (i
/ 2) + dashless_commits
;
816 strbuf_write_column(sb
, &graph
->new_columns
[col_num
], '.');
817 return num_dashes
+ 1;
820 static void graph_output_commit_line(struct git_graph
*graph
, struct strbuf
*sb
)
823 int i
, chars_written
;
826 * Output the row containing this commit
827 * Iterate up to and including graph->num_columns,
828 * since the current commit may not be in any of the existing
829 * columns. (This happens when the current commit doesn't have any
830 * children that we have already processed.)
834 for (i
= 0; i
<= graph
->num_columns
; i
++) {
835 struct column
*col
= &graph
->columns
[i
];
836 struct commit
*col_commit
;
837 if (i
== graph
->num_columns
) {
840 col_commit
= graph
->commit
;
842 col_commit
= graph
->columns
[i
].commit
;
845 if (col_commit
== graph
->commit
) {
847 graph_output_commit_char(graph
, sb
);
850 if (graph
->num_parents
> 2)
851 chars_written
+= graph_draw_octopus_merge(graph
,
853 } else if (seen_this
&& (graph
->num_parents
> 2)) {
854 strbuf_write_column(sb
, col
, '\\');
856 } else if (seen_this
&& (graph
->num_parents
== 2)) {
858 * This is a 2-way merge commit.
859 * There is no GRAPH_PRE_COMMIT stage for 2-way
860 * merges, so this is the first line of output
861 * for this commit. Check to see what the previous
862 * line of output was.
864 * If it was GRAPH_POST_MERGE, the branch line
865 * coming into this commit may have been '\',
866 * and not '|' or '/'. If so, output the branch
867 * line as '\' on this line, instead of '|'. This
868 * makes the output look nicer.
870 if (graph
->prev_state
== GRAPH_POST_MERGE
&&
871 graph
->prev_commit_index
< i
)
872 strbuf_write_column(sb
, col
, '\\');
874 strbuf_write_column(sb
, col
, '|');
877 strbuf_write_column(sb
, col
, '|');
880 strbuf_addch(sb
, ' ');
884 graph_pad_horizontally(graph
, sb
, chars_written
);
887 * Update graph->state
889 if (graph
->num_parents
> 1)
890 graph_update_state(graph
, GRAPH_POST_MERGE
);
891 else if (graph_is_mapping_correct(graph
))
892 graph_update_state(graph
, GRAPH_PADDING
);
894 graph_update_state(graph
, GRAPH_COLLAPSING
);
897 static struct column
*find_new_column_by_commit(struct git_graph
*graph
,
898 struct commit
*commit
)
901 for (i
= 0; i
< graph
->num_new_columns
; i
++) {
902 if (graph
->new_columns
[i
].commit
== commit
)
903 return &graph
->new_columns
[i
];
908 static void graph_output_post_merge_line(struct git_graph
*graph
, struct strbuf
*sb
)
911 int i
, j
, chars_written
;
914 * Output the post-merge row
917 for (i
= 0; i
<= graph
->num_columns
; i
++) {
918 struct column
*col
= &graph
->columns
[i
];
919 struct commit
*col_commit
;
920 if (i
== graph
->num_columns
) {
923 col_commit
= graph
->commit
;
925 col_commit
= col
->commit
;
928 if (col_commit
== graph
->commit
) {
930 * Since the current commit is a merge find
931 * the columns for the parent commits in
932 * new_columns and use those to format the
935 struct commit_list
*parents
= NULL
;
936 struct column
*par_column
;
938 parents
= first_interesting_parent(graph
);
940 par_column
= find_new_column_by_commit(graph
, parents
->item
);
943 strbuf_write_column(sb
, par_column
, '|');
945 for (j
= 0; j
< graph
->num_parents
- 1; j
++) {
946 parents
= next_interesting_parent(graph
, parents
);
948 par_column
= find_new_column_by_commit(graph
, parents
->item
);
950 strbuf_write_column(sb
, par_column
, '\\');
951 strbuf_addch(sb
, ' ');
953 chars_written
+= j
* 2;
954 } else if (seen_this
) {
955 strbuf_write_column(sb
, col
, '\\');
956 strbuf_addch(sb
, ' ');
959 strbuf_write_column(sb
, col
, '|');
960 strbuf_addch(sb
, ' ');
965 graph_pad_horizontally(graph
, sb
, chars_written
);
968 * Update graph->state
970 if (graph_is_mapping_correct(graph
))
971 graph_update_state(graph
, GRAPH_PADDING
);
973 graph_update_state(graph
, GRAPH_COLLAPSING
);
976 static void graph_output_collapsing_line(struct git_graph
*graph
, struct strbuf
*sb
)
980 short used_horizontal
= 0;
981 int horizontal_edge
= -1;
982 int horizontal_edge_target
= -1;
985 * Clear out the new_mapping array
987 for (i
= 0; i
< graph
->mapping_size
; i
++)
988 graph
->new_mapping
[i
] = -1;
990 for (i
= 0; i
< graph
->mapping_size
; i
++) {
991 int target
= graph
->mapping
[i
];
996 * Since update_columns() always inserts the leftmost
997 * column first, each branch's target location should
998 * always be either its current location or to the left of
999 * its current location.
1001 * We never have to move branches to the right. This makes
1002 * the graph much more legible, since whenever branches
1003 * cross, only one is moving directions.
1005 assert(target
* 2 <= i
);
1007 if (target
* 2 == i
) {
1009 * This column is already in the
1012 assert(graph
->new_mapping
[i
] == -1);
1013 graph
->new_mapping
[i
] = target
;
1014 } else if (graph
->new_mapping
[i
- 1] < 0) {
1016 * Nothing is to the left.
1017 * Move to the left by one
1019 graph
->new_mapping
[i
- 1] = target
;
1021 * If there isn't already an edge moving horizontally
1024 if (horizontal_edge
== -1) {
1026 horizontal_edge
= i
;
1027 horizontal_edge_target
= target
;
1029 * The variable target is the index of the graph
1030 * column, and therefore target*2+3 is the
1031 * actual screen column of the first horizontal
1034 for (j
= (target
* 2)+3; j
< (i
- 2); j
+= 2)
1035 graph
->new_mapping
[j
] = target
;
1037 } else if (graph
->new_mapping
[i
- 1] == target
) {
1039 * There is a branch line to our left
1040 * already, and it is our target. We
1041 * combine with this line, since we share
1042 * the same parent commit.
1044 * We don't have to add anything to the
1045 * output or new_mapping, since the
1046 * existing branch line has already taken
1051 * There is a branch line to our left,
1052 * but it isn't our target. We need to
1055 * The space just to the left of this
1056 * branch should always be empty.
1058 * The branch to the left of that space
1059 * should be our eventual target.
1061 assert(graph
->new_mapping
[i
- 1] > target
);
1062 assert(graph
->new_mapping
[i
- 2] < 0);
1063 assert(graph
->new_mapping
[i
- 3] == target
);
1064 graph
->new_mapping
[i
- 2] = target
;
1066 * Mark this branch as the horizontal edge to
1067 * prevent any other edges from moving
1070 if (horizontal_edge
== -1)
1071 horizontal_edge
= i
;
1076 * The new mapping may be 1 smaller than the old mapping
1078 if (graph
->new_mapping
[graph
->mapping_size
- 1] < 0)
1079 graph
->mapping_size
--;
1082 * Output out a line based on the new mapping info
1084 for (i
= 0; i
< graph
->mapping_size
; i
++) {
1085 int target
= graph
->new_mapping
[i
];
1087 strbuf_addch(sb
, ' ');
1088 else if (target
* 2 == i
)
1089 strbuf_write_column(sb
, &graph
->new_columns
[target
], '|');
1090 else if (target
== horizontal_edge_target
&&
1091 i
!= horizontal_edge
- 1) {
1093 * Set the mappings for all but the
1094 * first segment to -1 so that they
1095 * won't continue into the next line.
1097 if (i
!= (target
* 2)+3)
1098 graph
->new_mapping
[i
] = -1;
1099 used_horizontal
= 1;
1100 strbuf_write_column(sb
, &graph
->new_columns
[target
], '_');
1102 if (used_horizontal
&& i
< horizontal_edge
)
1103 graph
->new_mapping
[i
] = -1;
1104 strbuf_write_column(sb
, &graph
->new_columns
[target
], '/');
1109 graph_pad_horizontally(graph
, sb
, graph
->mapping_size
);
1112 * Swap mapping and new_mapping
1114 tmp_mapping
= graph
->mapping
;
1115 graph
->mapping
= graph
->new_mapping
;
1116 graph
->new_mapping
= tmp_mapping
;
1119 * If graph->mapping indicates that all of the branch lines
1120 * are already in the correct positions, we are done.
1121 * Otherwise, we need to collapse some branch lines together.
1123 if (graph_is_mapping_correct(graph
))
1124 graph_update_state(graph
, GRAPH_PADDING
);
1127 static int graph_next_line(struct git_graph
*graph
, struct strbuf
*sb
)
1129 switch (graph
->state
) {
1131 graph_output_padding_line(graph
, sb
);
1134 graph_output_skip_line(graph
, sb
);
1136 case GRAPH_PRE_COMMIT
:
1137 graph_output_pre_commit_line(graph
, sb
);
1140 graph_output_commit_line(graph
, sb
);
1142 case GRAPH_POST_MERGE
:
1143 graph_output_post_merge_line(graph
, sb
);
1145 case GRAPH_COLLAPSING
:
1146 graph_output_collapsing_line(graph
, sb
);
1154 static void graph_padding_line(struct git_graph
*graph
, struct strbuf
*sb
)
1158 if (graph
->state
!= GRAPH_COMMIT
) {
1159 graph_next_line(graph
, sb
);
1164 * Output the row containing this commit
1165 * Iterate up to and including graph->num_columns,
1166 * since the current commit may not be in any of the existing
1167 * columns. (This happens when the current commit doesn't have any
1168 * children that we have already processed.)
1170 for (i
= 0; i
< graph
->num_columns
; i
++) {
1171 struct column
*col
= &graph
->columns
[i
];
1172 struct commit
*col_commit
= col
->commit
;
1173 if (col_commit
== graph
->commit
) {
1174 strbuf_write_column(sb
, col
, '|');
1176 if (graph
->num_parents
< 3)
1177 strbuf_addch(sb
, ' ');
1179 int num_spaces
= ((graph
->num_parents
- 2) * 2);
1180 for (j
= 0; j
< num_spaces
; j
++)
1181 strbuf_addch(sb
, ' ');
1184 strbuf_write_column(sb
, col
, '|');
1185 strbuf_addch(sb
, ' ');
1189 graph_pad_horizontally(graph
, sb
, graph
->num_columns
);
1192 * Update graph->prev_state since we have output a padding line
1194 graph
->prev_state
= GRAPH_PADDING
;
1197 int graph_is_commit_finished(struct git_graph
const *graph
)
1199 return (graph
->state
== GRAPH_PADDING
);
1202 void graph_show_commit(struct git_graph
*graph
)
1204 struct strbuf msgbuf
= STRBUF_INIT
;
1205 int shown_commit_line
= 0;
1210 while (!shown_commit_line
) {
1211 shown_commit_line
= graph_next_line(graph
, &msgbuf
);
1212 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
, stdout
);
1213 if (!shown_commit_line
)
1215 strbuf_setlen(&msgbuf
, 0);
1218 strbuf_release(&msgbuf
);
1221 void graph_show_oneline(struct git_graph
*graph
)
1223 struct strbuf msgbuf
= STRBUF_INIT
;
1228 graph_next_line(graph
, &msgbuf
);
1229 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
, stdout
);
1230 strbuf_release(&msgbuf
);
1233 void graph_show_padding(struct git_graph
*graph
)
1235 struct strbuf msgbuf
= STRBUF_INIT
;
1240 graph_padding_line(graph
, &msgbuf
);
1241 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
, stdout
);
1242 strbuf_release(&msgbuf
);
1245 int graph_show_remainder(struct git_graph
*graph
)
1247 struct strbuf msgbuf
= STRBUF_INIT
;
1253 if (graph_is_commit_finished(graph
))
1257 graph_next_line(graph
, &msgbuf
);
1258 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
, stdout
);
1259 strbuf_setlen(&msgbuf
, 0);
1262 if (!graph_is_commit_finished(graph
))
1267 strbuf_release(&msgbuf
);
1273 static void graph_show_strbuf(struct git_graph
*graph
, struct strbuf
const *sb
)
1278 fwrite(sb
->buf
, sizeof(char), sb
->len
, stdout
);
1283 * Print the strbuf line by line,
1284 * and display the graph info before each line but the first.
1289 char *next_p
= strchr(p
, '\n');
1294 len
= (sb
->buf
+ sb
->len
) - p
;
1296 fwrite(p
, sizeof(char), len
, stdout
);
1297 if (next_p
&& *next_p
!= '\0')
1298 graph_show_oneline(graph
);
1303 void graph_show_commit_msg(struct git_graph
*graph
,
1304 struct strbuf
const *sb
)
1306 int newline_terminated
;
1310 * If there's no graph, just print the message buffer.
1312 * The message buffer for CMIT_FMT_ONELINE and
1313 * CMIT_FMT_USERFORMAT are already missing a terminating
1314 * newline. All of the other formats should have it.
1316 fwrite(sb
->buf
, sizeof(char), sb
->len
, stdout
);
1320 newline_terminated
= (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\n');
1323 * Show the commit message
1325 graph_show_strbuf(graph
, sb
);
1328 * If there is more output needed for this commit, show it now
1330 if (!graph_is_commit_finished(graph
)) {
1332 * If sb doesn't have a terminating newline, print one now,
1333 * so we can start the remainder of the graph output on a
1336 if (!newline_terminated
)
1339 graph_show_remainder(graph
);
1342 * If sb ends with a newline, our output should too.
1344 if (newline_terminated
)