git-init: accept --bare option
[git/gitweb.git] / graph.c
blob26b8c5209e280697cc35ffd5313fe3e79681fc43
1 #include "cache.h"
2 #include "commit.h"
3 #include "graph.h"
4 #include "diff.h"
5 #include "revision.h"
7 /*
8 * TODO:
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:
21 * | | | | | | | | | |
22 * | |_|_|/ | | | |/
23 * |/| | | | | |/|
24 * | | | | | |/| |
25 * |/| | |
26 * | | | |
28 * If there are several parallel diagonal lines, they will need to be
29 * replaced with horizontal lines on subsequent rows.
32 struct column {
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.
43 enum graph_state {
44 GRAPH_PADDING,
45 GRAPH_SKIP,
46 GRAPH_PRE_COMMIT,
47 GRAPH_COMMIT,
48 GRAPH_POST_MERGE,
49 GRAPH_COLLAPSING
52 struct git_graph {
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().
66 int num_parents;
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.
72 int width;
74 * The next expansion row to print
75 * when state is GRAPH_PRE_COMMIT
77 int expansion_row;
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 maximum number of columns that can be stored in the columns
85 * and new_columns arrays. This is also half the number of entries
86 * that can be stored in the mapping and new_mapping arrays.
88 int column_capacity;
90 * The number of columns (also called "branch lines" in some places)
92 int num_columns;
94 * The number of columns in the new_columns array
96 int num_new_columns;
98 * The number of entries in the mapping array
100 int mapping_size;
102 * The column state before we output the current commit.
104 struct column *columns;
106 * The new column state after we output the current commit.
107 * Only valid when state is GRAPH_COLLAPSING.
109 struct column *new_columns;
111 * An array that tracks the current state of each
112 * character in the output line during state GRAPH_COLLAPSING.
113 * Each entry is -1 if this character is empty, or a non-negative
114 * integer if the character contains a branch line. The value of
115 * the integer indicates the target position for this branch line.
116 * (I.e., this array maps the current column positions to their
117 * desired positions.)
119 * The maximum capacity of this array is always
120 * sizeof(int) * 2 * column_capacity.
122 int *mapping;
124 * A temporary array for computing the next mapping state
125 * while we are outputting a mapping line. This is stored as part
126 * of the git_graph simply so we don't have to allocate a new
127 * temporary array each time we have to output a collapsing line.
129 int *new_mapping;
132 struct git_graph *graph_init(struct rev_info *opt)
134 struct git_graph *graph = xmalloc(sizeof(struct git_graph));
135 graph->commit = NULL;
136 graph->revs = opt;
137 graph->num_parents = 0;
138 graph->expansion_row = 0;
139 graph->state = GRAPH_PADDING;
140 graph->num_columns = 0;
141 graph->num_new_columns = 0;
142 graph->mapping_size = 0;
145 * Allocate a reasonably large default number of columns
146 * We'll automatically grow columns later if we need more room.
148 graph->column_capacity = 30;
149 graph->columns = xmalloc(sizeof(struct column) *
150 graph->column_capacity);
151 graph->new_columns = xmalloc(sizeof(struct column) *
152 graph->column_capacity);
153 graph->mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
154 graph->new_mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
156 return graph;
159 void graph_release(struct git_graph *graph)
161 free(graph->columns);
162 free(graph->new_columns);
163 free(graph->mapping);
164 free(graph);
167 static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
169 if (graph->column_capacity >= num_columns)
170 return;
172 do {
173 graph->column_capacity *= 2;
174 } while (graph->column_capacity < num_columns);
176 graph->columns = xrealloc(graph->columns,
177 sizeof(struct column) *
178 graph->column_capacity);
179 graph->new_columns = xrealloc(graph->new_columns,
180 sizeof(struct column) *
181 graph->column_capacity);
182 graph->mapping = xrealloc(graph->mapping,
183 sizeof(int) * 2 * graph->column_capacity);
184 graph->new_mapping = xrealloc(graph->new_mapping,
185 sizeof(int) * 2 * graph->column_capacity);
189 * Returns 1 if the commit will be printed in the graph output,
190 * and 0 otherwise.
192 static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
195 * If revs->boundary is set, commits whose children have
196 * been shown are always interesting, even if they have the
197 * UNINTERESTING or TREESAME flags set.
199 if (graph->revs && graph->revs->boundary) {
200 if (commit->object.flags & CHILD_SHOWN)
201 return 1;
205 * Uninteresting and pruned commits won't be printed
207 return (commit->object.flags & (UNINTERESTING | TREESAME)) ? 0 : 1;
210 static void graph_insert_into_new_columns(struct git_graph *graph,
211 struct commit *commit,
212 int *mapping_index)
214 int i;
217 * Ignore uinteresting commits
219 if (!graph_is_interesting(graph, commit))
220 return;
223 * If the commit is already in the new_columns list, we don't need to
224 * add it. Just update the mapping correctly.
226 for (i = 0; i < graph->num_new_columns; i++) {
227 if (graph->new_columns[i].commit == commit) {
228 graph->mapping[*mapping_index] = i;
229 *mapping_index += 2;
230 return;
235 * This commit isn't already in new_columns. Add it.
237 graph->new_columns[graph->num_new_columns].commit = commit;
238 graph->mapping[*mapping_index] = graph->num_new_columns;
239 *mapping_index += 2;
240 graph->num_new_columns++;
243 static void graph_update_width(struct git_graph *graph,
244 int is_commit_in_existing_columns)
247 * Compute the width needed to display the graph for this commit.
248 * This is the maximum width needed for any row. All other rows
249 * will be padded to this width.
251 * Compute the number of columns in the widest row:
252 * Count each existing column (graph->num_columns), and each new
253 * column added by this commit.
255 int max_cols = graph->num_columns + graph->num_parents;
258 * Even if the current commit has no parents to be printed, it
259 * still takes up a column for itself.
261 if (graph->num_parents < 1)
262 max_cols++;
265 * We added a column for the the current commit as part of
266 * graph->num_parents. If the current commit was already in
267 * graph->columns, then we have double counted it.
269 if (is_commit_in_existing_columns)
270 max_cols--;
273 * Each column takes up 2 spaces
275 graph->width = max_cols * 2;
278 static void graph_update_columns(struct git_graph *graph)
280 struct commit_list *parent;
281 struct column *tmp_columns;
282 int max_new_columns;
283 int mapping_idx;
284 int i, seen_this, is_commit_in_columns;
287 * Swap graph->columns with graph->new_columns
288 * graph->columns contains the state for the previous commit,
289 * and new_columns now contains the state for our commit.
291 * We'll re-use the old columns array as storage to compute the new
292 * columns list for the commit after this one.
294 tmp_columns = graph->columns;
295 graph->columns = graph->new_columns;
296 graph->num_columns = graph->num_new_columns;
298 graph->new_columns = tmp_columns;
299 graph->num_new_columns = 0;
302 * Now update new_columns and mapping with the information for the
303 * commit after this one.
305 * First, make sure we have enough room. At most, there will
306 * be graph->num_columns + graph->num_parents columns for the next
307 * commit.
309 max_new_columns = graph->num_columns + graph->num_parents;
310 graph_ensure_capacity(graph, max_new_columns);
313 * Clear out graph->mapping
315 graph->mapping_size = 2 * max_new_columns;
316 for (i = 0; i < graph->mapping_size; i++)
317 graph->mapping[i] = -1;
320 * Populate graph->new_columns and graph->mapping
322 * Some of the parents of this commit may already be in
323 * graph->columns. If so, graph->new_columns should only contain a
324 * single entry for each such commit. graph->mapping should
325 * contain information about where each current branch line is
326 * supposed to end up after the collapsing is performed.
328 seen_this = 0;
329 mapping_idx = 0;
330 is_commit_in_columns = 1;
331 for (i = 0; i <= graph->num_columns; i++) {
332 struct commit *col_commit;
333 if (i == graph->num_columns) {
334 if (seen_this)
335 break;
336 is_commit_in_columns = 0;
337 col_commit = graph->commit;
338 } else {
339 col_commit = graph->columns[i].commit;
342 if (col_commit == graph->commit) {
343 int old_mapping_idx = mapping_idx;
344 seen_this = 1;
345 for (parent = graph->commit->parents;
346 parent;
347 parent = parent->next) {
348 graph_insert_into_new_columns(graph,
349 parent->item,
350 &mapping_idx);
353 * We always need to increment mapping_idx by at
354 * least 2, even if it has no interesting parents.
355 * The current commit always takes up at least 2
356 * spaces.
358 if (mapping_idx == old_mapping_idx)
359 mapping_idx += 2;
360 } else {
361 graph_insert_into_new_columns(graph, col_commit,
362 &mapping_idx);
367 * Shrink mapping_size to be the minimum necessary
369 while (graph->mapping_size > 1 &&
370 graph->mapping[graph->mapping_size - 1] < 0)
371 graph->mapping_size--;
374 * Compute graph->width for this commit
376 graph_update_width(graph, is_commit_in_columns);
379 void graph_update(struct git_graph *graph, struct commit *commit)
381 struct commit_list *parent;
384 * Set the new commit
386 graph->commit = commit;
389 * Count how many interesting parents this commit has
391 graph->num_parents = 0;
392 for (parent = commit->parents; parent; parent = parent->next) {
393 if (graph_is_interesting(graph, parent->item))
394 graph->num_parents++;
398 * Call graph_update_columns() to update
399 * columns, new_columns, and mapping.
401 graph_update_columns(graph);
403 graph->expansion_row = 0;
406 * Update graph->state.
408 * If the previous commit didn't get to the GRAPH_PADDING state,
409 * it never finished its output. Goto GRAPH_SKIP, to print out
410 * a line to indicate that portion of the graph is missing.
412 * Otherwise, if there are 3 or more parents, we need to print
413 * extra rows before the commit, to expand the branch lines around
414 * it and make room for it.
416 * If there are less than 3 parents, we can immediately print the
417 * commit line.
419 if (graph->state != GRAPH_PADDING)
420 graph->state = GRAPH_SKIP;
421 else if (graph->num_parents >= 3)
422 graph->state = GRAPH_PRE_COMMIT;
423 else
424 graph->state = GRAPH_COMMIT;
427 static int graph_is_mapping_correct(struct git_graph *graph)
429 int i;
432 * The mapping is up to date if each entry is at its target,
433 * or is 1 greater than its target.
434 * (If it is 1 greater than the target, '/' will be printed, so it
435 * will look correct on the next row.)
437 for (i = 0; i < graph->mapping_size; i++) {
438 int target = graph->mapping[i];
439 if (target < 0)
440 continue;
441 if (target == (i / 2))
442 continue;
443 return 0;
446 return 1;
449 static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb)
452 * Add additional spaces to the end of the strbuf, so that all
453 * lines for a particular commit have the same width.
455 * This way, fields printed to the right of the graph will remain
456 * aligned for the entire commit.
458 int extra;
459 if (sb->len >= graph->width)
460 return;
462 extra = graph->width - sb->len;
463 strbuf_addf(sb, "%*s", (int) extra, "");
466 static void graph_output_padding_line(struct git_graph *graph,
467 struct strbuf *sb)
469 int i;
472 * We could conceivable be called with a NULL commit
473 * if our caller has a bug, and invokes graph_next_line()
474 * immediately after graph_init(), without first calling
475 * graph_update(). Return without outputting anything in this
476 * case.
478 if (!graph->commit)
479 return;
482 * Output a padding row, that leaves all branch lines unchanged
484 for (i = 0; i < graph->num_new_columns; i++) {
485 strbuf_addstr(sb, "| ");
488 graph_pad_horizontally(graph, sb);
491 static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
494 * Output an ellipsis to indicate that a portion
495 * of the graph is missing.
497 strbuf_addstr(sb, "...");
498 graph_pad_horizontally(graph, sb);
500 if (graph->num_parents >= 3)
501 graph->state = GRAPH_PRE_COMMIT;
502 else
503 graph->state = GRAPH_COMMIT;
506 static void graph_output_pre_commit_line(struct git_graph *graph,
507 struct strbuf *sb)
509 int num_expansion_rows;
510 int i, seen_this;
513 * This function formats a row that increases the space around a commit
514 * with multiple parents, to make room for it. It should only be
515 * called when there are 3 or more parents.
517 * We need 2 extra rows for every parent over 2.
519 assert(graph->num_parents >= 3);
520 num_expansion_rows = (graph->num_parents - 2) * 2;
523 * graph->expansion_row tracks the current expansion row we are on.
524 * It should be in the range [0, num_expansion_rows - 1]
526 assert(0 <= graph->expansion_row &&
527 graph->expansion_row < num_expansion_rows);
530 * Output the row
532 seen_this = 0;
533 for (i = 0; i < graph->num_columns; i++) {
534 struct column *col = &graph->columns[i];
535 if (col->commit == graph->commit) {
536 seen_this = 1;
537 strbuf_addf(sb, "| %*s", graph->expansion_row, "");
538 } else if (seen_this) {
539 strbuf_addstr(sb, "\\ ");
540 } else {
541 strbuf_addstr(sb, "| ");
545 graph_pad_horizontally(graph, sb);
548 * Increment graph->expansion_row,
549 * and move to state GRAPH_COMMIT if necessary
551 graph->expansion_row++;
552 if (graph->expansion_row >= num_expansion_rows)
553 graph->state = GRAPH_COMMIT;
556 static void graph_output_commit_char(struct git_graph *graph, struct strbuf *sb)
559 * For boundary commits, print 'o'
560 * (We should only see boundary commits when revs->boundary is set.)
562 if (graph->commit->object.flags & BOUNDARY) {
563 assert(graph->revs->boundary);
564 strbuf_addch(sb, 'o');
565 return;
569 * If revs->left_right is set, print '<' for commits that
570 * come from the left side, and '>' for commits from the right
571 * side.
573 if (graph->revs && graph->revs->left_right) {
574 if (graph->commit->object.flags & SYMMETRIC_LEFT)
575 strbuf_addch(sb, '<');
576 else
577 strbuf_addch(sb, '>');
578 return;
582 * Print 'M' for merge commits
584 * Note that we don't check graph->num_parents to determine if the
585 * commit is a merge, since that only tracks the number of
586 * "interesting" parents. We want to print 'M' for merge commits
587 * even if they have less than 2 interesting parents.
589 if (graph->commit->parents != NULL &&
590 graph->commit->parents->next != NULL) {
591 strbuf_addch(sb, 'M');
592 return;
596 * Print '*' in all other cases
598 strbuf_addch(sb, '*');
601 void graph_output_commit_line(struct git_graph *graph, struct strbuf *sb)
603 int seen_this = 0;
604 int i, j;
607 * Output the row containing this commit
608 * Iterate up to and including graph->num_columns,
609 * since the current commit may not be in any of the existing
610 * columns. (This happens when the current commit doesn't have any
611 * children that we have already processed.)
613 seen_this = 0;
614 for (i = 0; i <= graph->num_columns; i++) {
615 struct commit *col_commit;
616 if (i == graph->num_columns) {
617 if (seen_this)
618 break;
619 col_commit = graph->commit;
620 } else {
621 col_commit = graph->columns[i].commit;
624 if (col_commit == graph->commit) {
625 seen_this = 1;
626 graph_output_commit_char(graph, sb);
628 if (graph->num_parents < 2)
629 strbuf_addch(sb, ' ');
630 else if (graph->num_parents == 2)
631 strbuf_addstr(sb, " ");
632 else {
633 int num_dashes =
634 ((graph->num_parents - 2) * 2) - 1;
635 for (j = 0; j < num_dashes; j++)
636 strbuf_addch(sb, '-');
637 strbuf_addstr(sb, ". ");
639 } else if (seen_this && (graph->num_parents > 1)) {
640 strbuf_addstr(sb, "\\ ");
641 } else {
642 strbuf_addstr(sb, "| ");
646 graph_pad_horizontally(graph, sb);
649 * Update graph->state
651 if (graph->num_parents > 1)
652 graph->state = GRAPH_POST_MERGE;
653 else if (graph_is_mapping_correct(graph))
654 graph->state = GRAPH_PADDING;
655 else
656 graph->state = GRAPH_COLLAPSING;
659 void graph_output_post_merge_line(struct git_graph *graph, struct strbuf *sb)
661 int seen_this = 0;
662 int i, j;
665 * Output the post-merge row
667 for (i = 0; i <= graph->num_columns; i++) {
668 struct commit *col_commit;
669 if (i == graph->num_columns) {
670 if (seen_this)
671 break;
672 col_commit = graph->commit;
673 } else {
674 col_commit = graph->columns[i].commit;
677 if (col_commit == graph->commit) {
678 seen_this = 1;
679 strbuf_addch(sb, '|');
680 for (j = 0; j < graph->num_parents - 1; j++)
681 strbuf_addstr(sb, "\\ ");
682 if (graph->num_parents == 2)
683 strbuf_addch(sb, ' ');
684 } else if (seen_this && (graph->num_parents > 2)) {
685 strbuf_addstr(sb, "\\ ");
686 } else {
687 strbuf_addstr(sb, "| ");
691 graph_pad_horizontally(graph, sb);
694 * Update graph->state
696 if (graph_is_mapping_correct(graph))
697 graph->state = GRAPH_PADDING;
698 else
699 graph->state = GRAPH_COLLAPSING;
702 void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
704 int i;
705 int *tmp_mapping;
708 * Clear out the new_mapping array
710 for (i = 0; i < graph->mapping_size; i++)
711 graph->new_mapping[i] = -1;
713 for (i = 0; i < graph->mapping_size; i++) {
714 int target = graph->mapping[i];
715 if (target < 0)
716 continue;
719 * Since update_columns() always inserts the leftmost
720 * column first, each branch's target location should
721 * always be either its current location or to the left of
722 * its current location.
724 * We never have to move branches to the right. This makes
725 * the graph much more legible, since whenever branches
726 * cross, only one is moving directions.
728 assert(target * 2 <= i);
730 if (target * 2 == i) {
732 * This column is already in the
733 * correct place
735 assert(graph->new_mapping[i] == -1);
736 graph->new_mapping[i] = target;
737 } else if (graph->new_mapping[i - 1] < 0) {
739 * Nothing is to the left.
740 * Move to the left by one
742 graph->new_mapping[i - 1] = target;
743 } else if (graph->new_mapping[i - 1] == target) {
745 * There is a branch line to our left
746 * already, and it is our target. We
747 * combine with this line, since we share
748 * the same parent commit.
750 * We don't have to add anything to the
751 * output or new_mapping, since the
752 * existing branch line has already taken
753 * care of it.
755 } else {
757 * There is a branch line to our left,
758 * but it isn't our target. We need to
759 * cross over it.
761 * The space just to the left of this
762 * branch should always be empty.
764 assert(graph->new_mapping[i - 1] > target);
765 assert(graph->new_mapping[i - 2] < 0);
766 graph->new_mapping[i - 2] = target;
771 * The new mapping may be 1 smaller than the old mapping
773 if (graph->new_mapping[graph->mapping_size - 1] < 0)
774 graph->mapping_size--;
777 * Output out a line based on the new mapping info
779 for (i = 0; i < graph->mapping_size; i++) {
780 int target = graph->new_mapping[i];
781 if (target < 0)
782 strbuf_addch(sb, ' ');
783 else if (target * 2 == i)
784 strbuf_addch(sb, '|');
785 else
786 strbuf_addch(sb, '/');
789 graph_pad_horizontally(graph, sb);
792 * Swap mapping and new_mapping
794 tmp_mapping = graph->mapping;
795 graph->mapping = graph->new_mapping;
796 graph->new_mapping = tmp_mapping;
799 * If graph->mapping indicates that all of the branch lines
800 * are already in the correct positions, we are done.
801 * Otherwise, we need to collapse some branch lines together.
803 if (graph_is_mapping_correct(graph))
804 graph->state = GRAPH_PADDING;
807 int graph_next_line(struct git_graph *graph, struct strbuf *sb)
809 switch (graph->state) {
810 case GRAPH_PADDING:
811 graph_output_padding_line(graph, sb);
812 return 0;
813 case GRAPH_SKIP:
814 graph_output_skip_line(graph, sb);
815 return 0;
816 case GRAPH_PRE_COMMIT:
817 graph_output_pre_commit_line(graph, sb);
818 return 0;
819 case GRAPH_COMMIT:
820 graph_output_commit_line(graph, sb);
821 return 1;
822 case GRAPH_POST_MERGE:
823 graph_output_post_merge_line(graph, sb);
824 return 0;
825 case GRAPH_COLLAPSING:
826 graph_output_collapsing_line(graph, sb);
827 return 0;
830 assert(0);
831 return 0;
834 void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
836 int i, j;
838 if (graph->state != GRAPH_COMMIT) {
839 graph_next_line(graph, sb);
840 return;
844 * Output the row containing this commit
845 * Iterate up to and including graph->num_columns,
846 * since the current commit may not be in any of the existing
847 * columns. (This happens when the current commit doesn't have any
848 * children that we have already processed.)
850 for (i = 0; i < graph->num_columns; i++) {
851 struct commit *col_commit = graph->columns[i].commit;
852 if (col_commit == graph->commit) {
853 strbuf_addch(sb, '|');
855 if (graph->num_parents < 3)
856 strbuf_addch(sb, ' ');
857 else {
858 int num_spaces = ((graph->num_parents - 2) * 2);
859 for (j = 0; j < num_spaces; j++)
860 strbuf_addch(sb, ' ');
862 } else {
863 strbuf_addstr(sb, "| ");
867 graph_pad_horizontally(graph, sb);
870 int graph_is_commit_finished(struct git_graph const *graph)
872 return (graph->state == GRAPH_PADDING);
875 void graph_show_commit(struct git_graph *graph)
877 struct strbuf msgbuf;
878 int shown_commit_line = 0;
880 if (!graph)
881 return;
883 strbuf_init(&msgbuf, 0);
885 while (!shown_commit_line) {
886 shown_commit_line = graph_next_line(graph, &msgbuf);
887 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
888 if (!shown_commit_line)
889 putchar('\n');
890 strbuf_setlen(&msgbuf, 0);
893 strbuf_release(&msgbuf);
896 void graph_show_oneline(struct git_graph *graph)
898 struct strbuf msgbuf;
900 if (!graph)
901 return;
903 strbuf_init(&msgbuf, 0);
904 graph_next_line(graph, &msgbuf);
905 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
906 strbuf_release(&msgbuf);
909 void graph_show_padding(struct git_graph *graph)
911 struct strbuf msgbuf;
913 if (!graph)
914 return;
916 strbuf_init(&msgbuf, 0);
917 graph_padding_line(graph, &msgbuf);
918 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
919 strbuf_release(&msgbuf);
922 int graph_show_remainder(struct git_graph *graph)
924 struct strbuf msgbuf;
925 int shown = 0;
927 if (!graph)
928 return 0;
930 if (graph_is_commit_finished(graph))
931 return 0;
933 strbuf_init(&msgbuf, 0);
934 for (;;) {
935 graph_next_line(graph, &msgbuf);
936 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
937 strbuf_setlen(&msgbuf, 0);
938 shown = 1;
940 if (!graph_is_commit_finished(graph))
941 putchar('\n');
942 else
943 break;
945 strbuf_release(&msgbuf);
947 return shown;
951 void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb)
953 char *p;
955 if (!graph) {
956 fwrite(sb->buf, sizeof(char), sb->len, stdout);
957 return;
961 * Print the strbuf line by line,
962 * and display the graph info before each line but the first.
964 p = sb->buf;
965 while (p) {
966 size_t len;
967 char *next_p = strchr(p, '\n');
968 if (next_p) {
969 next_p++;
970 len = next_p - p;
971 } else {
972 len = (sb->buf + sb->len) - p;
974 fwrite(p, sizeof(char), len, stdout);
975 if (next_p && *next_p != '\0')
976 graph_show_oneline(graph);
977 p = next_p;
981 void graph_show_commit_msg(struct git_graph *graph,
982 struct strbuf const *sb)
984 int newline_terminated;
986 if (!graph) {
988 * If there's no graph, just print the message buffer.
990 * The message buffer for CMIT_FMT_ONELINE and
991 * CMIT_FMT_USERFORMAT are already missing a terminating
992 * newline. All of the other formats should have it.
994 fwrite(sb->buf, sizeof(char), sb->len, stdout);
995 return;
998 newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
1001 * Show the commit message
1003 graph_show_strbuf(graph, sb);
1006 * If there is more output needed for this commit, show it now
1008 if (!graph_is_commit_finished(graph)) {
1010 * If sb doesn't have a terminating newline, print one now,
1011 * so we can start the remainder of the graph output on a
1012 * new line.
1014 if (!newline_terminated)
1015 putchar('\n');
1017 graph_show_remainder(graph);
1020 * If sb ends with a newline, our output should too.
1022 if (newline_terminated)
1023 putchar('\n');