9 * - Add colors to the graph.
10 * Pick a color for each column, and print all characters
11 * in that column with the specified color.
13 * - Limit the number of columns, similar to the way gitk does.
14 * If we reach more than a specified number of columns, omit
15 * sections of some columns.
17 * - The output during the GRAPH_PRE_COMMIT and GRAPH_COLLAPSING states
18 * could be made more compact by printing horizontal lines, instead of
19 * long diagonal lines. For example, during collapsing, something like
20 * this: instead of this:
28 * If there are several parallel diagonal lines, they will need to be
29 * replaced with horizontal lines on subsequent rows.
34 * The parent commit of this column.
36 struct commit
*commit
;
38 * XXX: Once we add support for colors, struct column could also
39 * contain the color of its branch line.
54 * The commit currently being processed
56 struct commit
*commit
;
57 /* The rev-info used for the current traversal */
58 struct rev_info
*revs
;
60 * The number of interesting parents that this commit has.
62 * Note that this is not the same as the actual number of parents.
63 * This count excludes parents that won't be printed in the graph
64 * output, as determined by graph_is_interesting().
68 * The width of the graph output for this commit.
69 * All rows for this commit are padded to this width, so that
70 * messages printed after the graph output are aligned.
74 * The next expansion row to print
75 * when state is GRAPH_PRE_COMMIT
79 * The current output state.
80 * This tells us what kind of line graph_next_line() should output.
82 enum graph_state state
;
84 * The output state for the previous line of output.
85 * This is primarily used to determine how the first merge line
86 * should appear, based on the last line of the previous commit.
88 enum graph_state prev_state
;
90 * The index of the column that refers to this commit.
92 * If none of the incoming columns refer to this commit,
93 * this will be equal to num_columns.
97 * The commit_index for the previously displayed commit.
99 * This is used to determine how the first line of a merge
100 * graph output should appear, based on the last line of the
103 int prev_commit_index
;
105 * The maximum number of columns that can be stored in the columns
106 * and new_columns arrays. This is also half the number of entries
107 * that can be stored in the mapping and new_mapping arrays.
111 * The number of columns (also called "branch lines" in some places)
115 * The number of columns in the new_columns array
119 * The number of entries in the mapping array
123 * The column state before we output the current commit.
125 struct column
*columns
;
127 * The new column state after we output the current commit.
128 * Only valid when state is GRAPH_COLLAPSING.
130 struct column
*new_columns
;
132 * An array that tracks the current state of each
133 * character in the output line during state GRAPH_COLLAPSING.
134 * Each entry is -1 if this character is empty, or a non-negative
135 * integer if the character contains a branch line. The value of
136 * the integer indicates the target position for this branch line.
137 * (I.e., this array maps the current column positions to their
138 * desired positions.)
140 * The maximum capacity of this array is always
141 * sizeof(int) * 2 * column_capacity.
145 * A temporary array for computing the next mapping state
146 * while we are outputting a mapping line. This is stored as part
147 * of the git_graph simply so we don't have to allocate a new
148 * temporary array each time we have to output a collapsing line.
153 struct git_graph
*graph_init(struct rev_info
*opt
)
155 struct git_graph
*graph
= xmalloc(sizeof(struct git_graph
));
156 graph
->commit
= NULL
;
158 graph
->num_parents
= 0;
159 graph
->expansion_row
= 0;
160 graph
->state
= GRAPH_PADDING
;
161 graph
->prev_state
= GRAPH_PADDING
;
162 graph
->commit_index
= 0;
163 graph
->prev_commit_index
= 0;
164 graph
->num_columns
= 0;
165 graph
->num_new_columns
= 0;
166 graph
->mapping_size
= 0;
169 * Allocate a reasonably large default number of columns
170 * We'll automatically grow columns later if we need more room.
172 graph
->column_capacity
= 30;
173 graph
->columns
= xmalloc(sizeof(struct column
) *
174 graph
->column_capacity
);
175 graph
->new_columns
= xmalloc(sizeof(struct column
) *
176 graph
->column_capacity
);
177 graph
->mapping
= xmalloc(sizeof(int) * 2 * graph
->column_capacity
);
178 graph
->new_mapping
= xmalloc(sizeof(int) * 2 * graph
->column_capacity
);
183 void graph_release(struct git_graph
*graph
)
185 free(graph
->columns
);
186 free(graph
->new_columns
);
187 free(graph
->mapping
);
191 static void graph_update_state(struct git_graph
*graph
, enum graph_state s
)
193 graph
->prev_state
= graph
->state
;
197 static void graph_ensure_capacity(struct git_graph
*graph
, int num_columns
)
199 if (graph
->column_capacity
>= num_columns
)
203 graph
->column_capacity
*= 2;
204 } while (graph
->column_capacity
< num_columns
);
206 graph
->columns
= xrealloc(graph
->columns
,
207 sizeof(struct column
) *
208 graph
->column_capacity
);
209 graph
->new_columns
= xrealloc(graph
->new_columns
,
210 sizeof(struct column
) *
211 graph
->column_capacity
);
212 graph
->mapping
= xrealloc(graph
->mapping
,
213 sizeof(int) * 2 * graph
->column_capacity
);
214 graph
->new_mapping
= xrealloc(graph
->new_mapping
,
215 sizeof(int) * 2 * graph
->column_capacity
);
219 * Returns 1 if the commit will be printed in the graph output,
222 static int graph_is_interesting(struct git_graph
*graph
, struct commit
*commit
)
225 * If revs->boundary is set, commits whose children have
226 * been shown are always interesting, even if they have the
227 * UNINTERESTING or TREESAME flags set.
229 if (graph
->revs
&& graph
->revs
->boundary
) {
230 if (commit
->object
.flags
& CHILD_SHOWN
)
235 * Uninteresting and pruned commits won't be printed
237 return (commit
->object
.flags
& (UNINTERESTING
| TREESAME
)) ? 0 : 1;
240 static struct commit_list
*next_interesting_parent(struct git_graph
*graph
,
241 struct commit_list
*orig
)
243 struct commit_list
*list
;
246 * If revs->first_parent_only is set, only the first
247 * parent is interesting. None of the others are.
249 if (graph
->revs
->first_parent_only
)
253 * Return the next interesting commit after orig
255 for (list
= orig
->next
; list
; list
= list
->next
) {
256 if (graph_is_interesting(graph
, list
->item
))
263 static struct commit_list
*first_interesting_parent(struct git_graph
*graph
)
265 struct commit_list
*parents
= graph
->commit
->parents
;
268 * If this commit has no parents, ignore it
274 * If the first parent is interesting, return it
276 if (graph_is_interesting(graph
, parents
->item
))
280 * Otherwise, call next_interesting_parent() to get
281 * the next interesting parent
283 return next_interesting_parent(graph
, parents
);
286 static void graph_insert_into_new_columns(struct git_graph
*graph
,
287 struct commit
*commit
,
293 * If the commit is already in the new_columns list, we don't need to
294 * add it. Just update the mapping correctly.
296 for (i
= 0; i
< graph
->num_new_columns
; i
++) {
297 if (graph
->new_columns
[i
].commit
== commit
) {
298 graph
->mapping
[*mapping_index
] = i
;
305 * This commit isn't already in new_columns. Add it.
307 graph
->new_columns
[graph
->num_new_columns
].commit
= commit
;
308 graph
->mapping
[*mapping_index
] = graph
->num_new_columns
;
310 graph
->num_new_columns
++;
313 static void graph_update_width(struct git_graph
*graph
,
314 int is_commit_in_existing_columns
)
317 * Compute the width needed to display the graph for this commit.
318 * This is the maximum width needed for any row. All other rows
319 * will be padded to this width.
321 * Compute the number of columns in the widest row:
322 * Count each existing column (graph->num_columns), and each new
323 * column added by this commit.
325 int max_cols
= graph
->num_columns
+ graph
->num_parents
;
328 * Even if the current commit has no parents to be printed, it
329 * still takes up a column for itself.
331 if (graph
->num_parents
< 1)
335 * We added a column for the the current commit as part of
336 * graph->num_parents. If the current commit was already in
337 * graph->columns, then we have double counted it.
339 if (is_commit_in_existing_columns
)
343 * Each column takes up 2 spaces
345 graph
->width
= max_cols
* 2;
348 static void graph_update_columns(struct git_graph
*graph
)
350 struct commit_list
*parent
;
351 struct column
*tmp_columns
;
354 int i
, seen_this
, is_commit_in_columns
;
357 * Swap graph->columns with graph->new_columns
358 * graph->columns contains the state for the previous commit,
359 * and new_columns now contains the state for our commit.
361 * We'll re-use the old columns array as storage to compute the new
362 * columns list for the commit after this one.
364 tmp_columns
= graph
->columns
;
365 graph
->columns
= graph
->new_columns
;
366 graph
->num_columns
= graph
->num_new_columns
;
368 graph
->new_columns
= tmp_columns
;
369 graph
->num_new_columns
= 0;
372 * Now update new_columns and mapping with the information for the
373 * commit after this one.
375 * First, make sure we have enough room. At most, there will
376 * be graph->num_columns + graph->num_parents columns for the next
379 max_new_columns
= graph
->num_columns
+ graph
->num_parents
;
380 graph_ensure_capacity(graph
, max_new_columns
);
383 * Clear out graph->mapping
385 graph
->mapping_size
= 2 * max_new_columns
;
386 for (i
= 0; i
< graph
->mapping_size
; i
++)
387 graph
->mapping
[i
] = -1;
390 * Populate graph->new_columns and graph->mapping
392 * Some of the parents of this commit may already be in
393 * graph->columns. If so, graph->new_columns should only contain a
394 * single entry for each such commit. graph->mapping should
395 * contain information about where each current branch line is
396 * supposed to end up after the collapsing is performed.
400 is_commit_in_columns
= 1;
401 for (i
= 0; i
<= graph
->num_columns
; i
++) {
402 struct commit
*col_commit
;
403 if (i
== graph
->num_columns
) {
406 is_commit_in_columns
= 0;
407 col_commit
= graph
->commit
;
409 col_commit
= graph
->columns
[i
].commit
;
412 if (col_commit
== graph
->commit
) {
413 int old_mapping_idx
= mapping_idx
;
415 graph
->commit_index
= i
;
416 for (parent
= first_interesting_parent(graph
);
418 parent
= next_interesting_parent(graph
, parent
)) {
419 graph_insert_into_new_columns(graph
,
424 * We always need to increment mapping_idx by at
425 * least 2, even if it has no interesting parents.
426 * The current commit always takes up at least 2
429 if (mapping_idx
== old_mapping_idx
)
432 graph_insert_into_new_columns(graph
, col_commit
,
438 * Shrink mapping_size to be the minimum necessary
440 while (graph
->mapping_size
> 1 &&
441 graph
->mapping
[graph
->mapping_size
- 1] < 0)
442 graph
->mapping_size
--;
445 * Compute graph->width for this commit
447 graph_update_width(graph
, is_commit_in_columns
);
450 void graph_update(struct git_graph
*graph
, struct commit
*commit
)
452 struct commit_list
*parent
;
457 graph
->commit
= commit
;
460 * Count how many interesting parents this commit has
462 graph
->num_parents
= 0;
463 for (parent
= first_interesting_parent(graph
);
465 parent
= next_interesting_parent(graph
, parent
))
467 graph
->num_parents
++;
471 * Store the old commit_index in prev_commit_index.
472 * graph_update_columns() will update graph->commit_index for this
475 graph
->prev_commit_index
= graph
->commit_index
;
478 * Call graph_update_columns() to update
479 * columns, new_columns, and mapping.
481 graph_update_columns(graph
);
483 graph
->expansion_row
= 0;
486 * Update graph->state.
487 * Note that we don't call graph_update_state() here, since
488 * we don't want to update graph->prev_state. No line for
489 * graph->state was ever printed.
491 * If the previous commit didn't get to the GRAPH_PADDING state,
492 * it never finished its output. Goto GRAPH_SKIP, to print out
493 * a line to indicate that portion of the graph is missing.
495 * If there are 3 or more parents, we may need to print extra rows
496 * before the commit, to expand the branch lines around it and make
497 * room for it. We need to do this only if there is a branch row
498 * (or more) to the right of this commit.
500 * If there are less than 3 parents, we can immediately print the
503 if (graph
->state
!= GRAPH_PADDING
)
504 graph
->state
= GRAPH_SKIP
;
505 else if (graph
->num_parents
>= 3 &&
506 graph
->commit_index
< (graph
->num_columns
- 1))
507 graph
->state
= GRAPH_PRE_COMMIT
;
509 graph
->state
= GRAPH_COMMIT
;
512 static int graph_is_mapping_correct(struct git_graph
*graph
)
517 * The mapping is up to date if each entry is at its target,
518 * or is 1 greater than its target.
519 * (If it is 1 greater than the target, '/' will be printed, so it
520 * will look correct on the next row.)
522 for (i
= 0; i
< graph
->mapping_size
; i
++) {
523 int target
= graph
->mapping
[i
];
526 if (target
== (i
/ 2))
534 static void graph_pad_horizontally(struct git_graph
*graph
, struct strbuf
*sb
)
537 * Add additional spaces to the end of the strbuf, so that all
538 * lines for a particular commit have the same width.
540 * This way, fields printed to the right of the graph will remain
541 * aligned for the entire commit.
544 if (sb
->len
>= graph
->width
)
547 extra
= graph
->width
- sb
->len
;
548 strbuf_addf(sb
, "%*s", (int) extra
, "");
551 static void graph_output_padding_line(struct git_graph
*graph
,
557 * We could conceivable be called with a NULL commit
558 * if our caller has a bug, and invokes graph_next_line()
559 * immediately after graph_init(), without first calling
560 * graph_update(). Return without outputting anything in this
567 * Output a padding row, that leaves all branch lines unchanged
569 for (i
= 0; i
< graph
->num_new_columns
; i
++) {
570 strbuf_addstr(sb
, "| ");
573 graph_pad_horizontally(graph
, sb
);
576 static void graph_output_skip_line(struct git_graph
*graph
, struct strbuf
*sb
)
579 * Output an ellipsis to indicate that a portion
580 * of the graph is missing.
582 strbuf_addstr(sb
, "...");
583 graph_pad_horizontally(graph
, sb
);
585 if (graph
->num_parents
>= 3 &&
586 graph
->commit_index
< (graph
->num_columns
- 1))
587 graph_update_state(graph
, GRAPH_PRE_COMMIT
);
589 graph_update_state(graph
, GRAPH_COMMIT
);
592 static void graph_output_pre_commit_line(struct git_graph
*graph
,
595 int num_expansion_rows
;
599 * This function formats a row that increases the space around a commit
600 * with multiple parents, to make room for it. It should only be
601 * called when there are 3 or more parents.
603 * We need 2 extra rows for every parent over 2.
605 assert(graph
->num_parents
>= 3);
606 num_expansion_rows
= (graph
->num_parents
- 2) * 2;
609 * graph->expansion_row tracks the current expansion row we are on.
610 * It should be in the range [0, num_expansion_rows - 1]
612 assert(0 <= graph
->expansion_row
&&
613 graph
->expansion_row
< num_expansion_rows
);
619 for (i
= 0; i
< graph
->num_columns
; i
++) {
620 struct column
*col
= &graph
->columns
[i
];
621 if (col
->commit
== graph
->commit
) {
623 strbuf_addf(sb
, "| %*s", graph
->expansion_row
, "");
624 } else if (seen_this
&& (graph
->expansion_row
== 0)) {
626 * This is the first line of the pre-commit output.
627 * If the previous commit was a merge commit and
628 * ended in the GRAPH_POST_MERGE state, all branch
629 * lines after graph->prev_commit_index were
630 * printed as "\" on the previous line. Continue
631 * to print them as "\" on this line. Otherwise,
632 * print the branch lines as "|".
634 if (graph
->prev_state
== GRAPH_POST_MERGE
&&
635 graph
->prev_commit_index
< i
)
636 strbuf_addstr(sb
, "\\ ");
638 strbuf_addstr(sb
, "| ");
639 } else if (seen_this
&& (graph
->expansion_row
> 0)) {
640 strbuf_addstr(sb
, "\\ ");
642 strbuf_addstr(sb
, "| ");
646 graph_pad_horizontally(graph
, sb
);
649 * Increment graph->expansion_row,
650 * and move to state GRAPH_COMMIT if necessary
652 graph
->expansion_row
++;
653 if (graph
->expansion_row
>= num_expansion_rows
)
654 graph_update_state(graph
, GRAPH_COMMIT
);
657 static void graph_output_commit_char(struct git_graph
*graph
, struct strbuf
*sb
)
660 * For boundary commits, print 'o'
661 * (We should only see boundary commits when revs->boundary is set.)
663 if (graph
->commit
->object
.flags
& BOUNDARY
) {
664 assert(graph
->revs
->boundary
);
665 strbuf_addch(sb
, 'o');
670 * If revs->left_right is set, print '<' for commits that
671 * come from the left side, and '>' for commits from the right
674 if (graph
->revs
&& graph
->revs
->left_right
) {
675 if (graph
->commit
->object
.flags
& SYMMETRIC_LEFT
)
676 strbuf_addch(sb
, '<');
678 strbuf_addch(sb
, '>');
683 * Print '*' in all other cases
685 strbuf_addch(sb
, '*');
688 void graph_output_commit_line(struct git_graph
*graph
, struct strbuf
*sb
)
694 * Output the row containing this commit
695 * Iterate up to and including graph->num_columns,
696 * since the current commit may not be in any of the existing
697 * columns. (This happens when the current commit doesn't have any
698 * children that we have already processed.)
701 for (i
= 0; i
<= graph
->num_columns
; i
++) {
702 struct commit
*col_commit
;
703 if (i
== graph
->num_columns
) {
706 col_commit
= graph
->commit
;
708 col_commit
= graph
->columns
[i
].commit
;
711 if (col_commit
== graph
->commit
) {
713 graph_output_commit_char(graph
, sb
);
715 if (graph
->num_parents
< 3)
716 strbuf_addch(sb
, ' ');
719 ((graph
->num_parents
- 2) * 2) - 1;
720 for (j
= 0; j
< num_dashes
; j
++)
721 strbuf_addch(sb
, '-');
722 strbuf_addstr(sb
, ". ");
724 } else if (seen_this
&& (graph
->num_parents
> 2)) {
725 strbuf_addstr(sb
, "\\ ");
726 } else if (seen_this
&& (graph
->num_parents
== 2)) {
728 * This is a 2-way merge commit.
729 * There is no GRAPH_PRE_COMMIT stage for 2-way
730 * merges, so this is the first line of output
731 * for this commit. Check to see what the previous
732 * line of output was.
734 * If it was GRAPH_POST_MERGE, the branch line
735 * coming into this commit may have been '\',
736 * and not '|' or '/'. If so, output the branch
737 * line as '\' on this line, instead of '|'. This
738 * makes the output look nicer.
740 if (graph
->prev_state
== GRAPH_POST_MERGE
&&
741 graph
->prev_commit_index
< i
)
742 strbuf_addstr(sb
, "\\ ");
744 strbuf_addstr(sb
, "| ");
746 strbuf_addstr(sb
, "| ");
750 graph_pad_horizontally(graph
, sb
);
753 * Update graph->state
755 if (graph
->num_parents
> 1)
756 graph_update_state(graph
, GRAPH_POST_MERGE
);
757 else if (graph_is_mapping_correct(graph
))
758 graph_update_state(graph
, GRAPH_PADDING
);
760 graph_update_state(graph
, GRAPH_COLLAPSING
);
763 void graph_output_post_merge_line(struct git_graph
*graph
, struct strbuf
*sb
)
769 * Output the post-merge row
771 for (i
= 0; i
<= graph
->num_columns
; i
++) {
772 struct commit
*col_commit
;
773 if (i
== graph
->num_columns
) {
776 col_commit
= graph
->commit
;
778 col_commit
= graph
->columns
[i
].commit
;
781 if (col_commit
== graph
->commit
) {
783 strbuf_addch(sb
, '|');
784 for (j
= 0; j
< graph
->num_parents
- 1; j
++)
785 strbuf_addstr(sb
, "\\ ");
786 } else if (seen_this
) {
787 strbuf_addstr(sb
, "\\ ");
789 strbuf_addstr(sb
, "| ");
793 graph_pad_horizontally(graph
, sb
);
796 * Update graph->state
798 if (graph_is_mapping_correct(graph
))
799 graph_update_state(graph
, GRAPH_PADDING
);
801 graph_update_state(graph
, GRAPH_COLLAPSING
);
804 void graph_output_collapsing_line(struct git_graph
*graph
, struct strbuf
*sb
)
810 * Clear out the new_mapping array
812 for (i
= 0; i
< graph
->mapping_size
; i
++)
813 graph
->new_mapping
[i
] = -1;
815 for (i
= 0; i
< graph
->mapping_size
; i
++) {
816 int target
= graph
->mapping
[i
];
821 * Since update_columns() always inserts the leftmost
822 * column first, each branch's target location should
823 * always be either its current location or to the left of
824 * its current location.
826 * We never have to move branches to the right. This makes
827 * the graph much more legible, since whenever branches
828 * cross, only one is moving directions.
830 assert(target
* 2 <= i
);
832 if (target
* 2 == i
) {
834 * This column is already in the
837 assert(graph
->new_mapping
[i
] == -1);
838 graph
->new_mapping
[i
] = target
;
839 } else if (graph
->new_mapping
[i
- 1] < 0) {
841 * Nothing is to the left.
842 * Move to the left by one
844 graph
->new_mapping
[i
- 1] = target
;
845 } else if (graph
->new_mapping
[i
- 1] == target
) {
847 * There is a branch line to our left
848 * already, and it is our target. We
849 * combine with this line, since we share
850 * the same parent commit.
852 * We don't have to add anything to the
853 * output or new_mapping, since the
854 * existing branch line has already taken
859 * There is a branch line to our left,
860 * but it isn't our target. We need to
863 * The space just to the left of this
864 * branch should always be empty.
866 assert(graph
->new_mapping
[i
- 1] > target
);
867 assert(graph
->new_mapping
[i
- 2] < 0);
868 graph
->new_mapping
[i
- 2] = target
;
873 * The new mapping may be 1 smaller than the old mapping
875 if (graph
->new_mapping
[graph
->mapping_size
- 1] < 0)
876 graph
->mapping_size
--;
879 * Output out a line based on the new mapping info
881 for (i
= 0; i
< graph
->mapping_size
; i
++) {
882 int target
= graph
->new_mapping
[i
];
884 strbuf_addch(sb
, ' ');
885 else if (target
* 2 == i
)
886 strbuf_addch(sb
, '|');
888 strbuf_addch(sb
, '/');
891 graph_pad_horizontally(graph
, sb
);
894 * Swap mapping and new_mapping
896 tmp_mapping
= graph
->mapping
;
897 graph
->mapping
= graph
->new_mapping
;
898 graph
->new_mapping
= tmp_mapping
;
901 * If graph->mapping indicates that all of the branch lines
902 * are already in the correct positions, we are done.
903 * Otherwise, we need to collapse some branch lines together.
905 if (graph_is_mapping_correct(graph
))
906 graph_update_state(graph
, GRAPH_PADDING
);
909 int graph_next_line(struct git_graph
*graph
, struct strbuf
*sb
)
911 switch (graph
->state
) {
913 graph_output_padding_line(graph
, sb
);
916 graph_output_skip_line(graph
, sb
);
918 case GRAPH_PRE_COMMIT
:
919 graph_output_pre_commit_line(graph
, sb
);
922 graph_output_commit_line(graph
, sb
);
924 case GRAPH_POST_MERGE
:
925 graph_output_post_merge_line(graph
, sb
);
927 case GRAPH_COLLAPSING
:
928 graph_output_collapsing_line(graph
, sb
);
936 void graph_padding_line(struct git_graph
*graph
, struct strbuf
*sb
)
940 if (graph
->state
!= GRAPH_COMMIT
) {
941 graph_next_line(graph
, sb
);
946 * Output the row containing this commit
947 * Iterate up to and including graph->num_columns,
948 * since the current commit may not be in any of the existing
949 * columns. (This happens when the current commit doesn't have any
950 * children that we have already processed.)
952 for (i
= 0; i
< graph
->num_columns
; i
++) {
953 struct commit
*col_commit
= graph
->columns
[i
].commit
;
954 if (col_commit
== graph
->commit
) {
955 strbuf_addch(sb
, '|');
957 if (graph
->num_parents
< 3)
958 strbuf_addch(sb
, ' ');
960 int num_spaces
= ((graph
->num_parents
- 2) * 2);
961 for (j
= 0; j
< num_spaces
; j
++)
962 strbuf_addch(sb
, ' ');
965 strbuf_addstr(sb
, "| ");
969 graph_pad_horizontally(graph
, sb
);
972 * Update graph->prev_state since we have output a padding line
974 graph
->prev_state
= GRAPH_PADDING
;
977 int graph_is_commit_finished(struct git_graph
const *graph
)
979 return (graph
->state
== GRAPH_PADDING
);
982 void graph_show_commit(struct git_graph
*graph
)
984 struct strbuf msgbuf
;
985 int shown_commit_line
= 0;
990 strbuf_init(&msgbuf
, 0);
992 while (!shown_commit_line
) {
993 shown_commit_line
= graph_next_line(graph
, &msgbuf
);
994 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
, stdout
);
995 if (!shown_commit_line
)
997 strbuf_setlen(&msgbuf
, 0);
1000 strbuf_release(&msgbuf
);
1003 void graph_show_oneline(struct git_graph
*graph
)
1005 struct strbuf msgbuf
;
1010 strbuf_init(&msgbuf
, 0);
1011 graph_next_line(graph
, &msgbuf
);
1012 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
, stdout
);
1013 strbuf_release(&msgbuf
);
1016 void graph_show_padding(struct git_graph
*graph
)
1018 struct strbuf msgbuf
;
1023 strbuf_init(&msgbuf
, 0);
1024 graph_padding_line(graph
, &msgbuf
);
1025 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
, stdout
);
1026 strbuf_release(&msgbuf
);
1029 int graph_show_remainder(struct git_graph
*graph
)
1031 struct strbuf msgbuf
;
1037 if (graph_is_commit_finished(graph
))
1040 strbuf_init(&msgbuf
, 0);
1042 graph_next_line(graph
, &msgbuf
);
1043 fwrite(msgbuf
.buf
, sizeof(char), msgbuf
.len
, stdout
);
1044 strbuf_setlen(&msgbuf
, 0);
1047 if (!graph_is_commit_finished(graph
))
1052 strbuf_release(&msgbuf
);
1058 void graph_show_strbuf(struct git_graph
*graph
, struct strbuf
const *sb
)
1063 fwrite(sb
->buf
, sizeof(char), sb
->len
, stdout
);
1068 * Print the strbuf line by line,
1069 * and display the graph info before each line but the first.
1074 char *next_p
= strchr(p
, '\n');
1079 len
= (sb
->buf
+ sb
->len
) - p
;
1081 fwrite(p
, sizeof(char), len
, stdout
);
1082 if (next_p
&& *next_p
!= '\0')
1083 graph_show_oneline(graph
);
1088 void graph_show_commit_msg(struct git_graph
*graph
,
1089 struct strbuf
const *sb
)
1091 int newline_terminated
;
1095 * If there's no graph, just print the message buffer.
1097 * The message buffer for CMIT_FMT_ONELINE and
1098 * CMIT_FMT_USERFORMAT are already missing a terminating
1099 * newline. All of the other formats should have it.
1101 fwrite(sb
->buf
, sizeof(char), sb
->len
, stdout
);
1105 newline_terminated
= (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\n');
1108 * Show the commit message
1110 graph_show_strbuf(graph
, sb
);
1113 * If there is more output needed for this commit, show it now
1115 if (!graph_is_commit_finished(graph
)) {
1117 * If sb doesn't have a terminating newline, print one now,
1118 * so we can start the remainder of the graph output on a
1121 if (!newline_terminated
)
1124 graph_show_remainder(graph
);
1127 * If sb ends with a newline, our output should too.
1129 if (newline_terminated
)