Merge branch 'lw/test-fix'
[git/gitweb.git] / graph.c
blobedfab2d5b461f5bfd39d32fa843ee59841b4ce0a
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 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.
95 int commit_index;
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
101 * previous commit.
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.
109 int column_capacity;
111 * The number of columns (also called "branch lines" in some places)
113 int num_columns;
115 * The number of columns in the new_columns array
117 int num_new_columns;
119 * The number of entries in the mapping array
121 int mapping_size;
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.
143 int *mapping;
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.
150 int *new_mapping;
153 struct git_graph *graph_init(struct rev_info *opt)
155 struct git_graph *graph = xmalloc(sizeof(struct git_graph));
156 graph->commit = NULL;
157 graph->revs = opt;
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);
180 return graph;
183 void graph_release(struct git_graph *graph)
185 free(graph->columns);
186 free(graph->new_columns);
187 free(graph->mapping);
188 free(graph);
191 static void graph_update_state(struct git_graph *graph, enum graph_state s)
193 graph->prev_state = graph->state;
194 graph->state = s;
197 static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
199 if (graph->column_capacity >= num_columns)
200 return;
202 do {
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,
220 * and 0 otherwise.
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)
231 return 1;
235 * Uninteresting and pruned commits won't be printed
237 return (commit->object.flags & (UNINTERESTING | TREESAME)) ? 0 : 1;
240 static void graph_insert_into_new_columns(struct git_graph *graph,
241 struct commit *commit,
242 int *mapping_index)
244 int i;
247 * Ignore uinteresting commits
249 if (!graph_is_interesting(graph, commit))
250 return;
253 * If the commit is already in the new_columns list, we don't need to
254 * add it. Just update the mapping correctly.
256 for (i = 0; i < graph->num_new_columns; i++) {
257 if (graph->new_columns[i].commit == commit) {
258 graph->mapping[*mapping_index] = i;
259 *mapping_index += 2;
260 return;
265 * This commit isn't already in new_columns. Add it.
267 graph->new_columns[graph->num_new_columns].commit = commit;
268 graph->mapping[*mapping_index] = graph->num_new_columns;
269 *mapping_index += 2;
270 graph->num_new_columns++;
273 static void graph_update_width(struct git_graph *graph,
274 int is_commit_in_existing_columns)
277 * Compute the width needed to display the graph for this commit.
278 * This is the maximum width needed for any row. All other rows
279 * will be padded to this width.
281 * Compute the number of columns in the widest row:
282 * Count each existing column (graph->num_columns), and each new
283 * column added by this commit.
285 int max_cols = graph->num_columns + graph->num_parents;
288 * Even if the current commit has no parents to be printed, it
289 * still takes up a column for itself.
291 if (graph->num_parents < 1)
292 max_cols++;
295 * We added a column for the the current commit as part of
296 * graph->num_parents. If the current commit was already in
297 * graph->columns, then we have double counted it.
299 if (is_commit_in_existing_columns)
300 max_cols--;
303 * Each column takes up 2 spaces
305 graph->width = max_cols * 2;
308 static void graph_update_columns(struct git_graph *graph)
310 struct commit_list *parent;
311 struct column *tmp_columns;
312 int max_new_columns;
313 int mapping_idx;
314 int i, seen_this, is_commit_in_columns;
317 * Swap graph->columns with graph->new_columns
318 * graph->columns contains the state for the previous commit,
319 * and new_columns now contains the state for our commit.
321 * We'll re-use the old columns array as storage to compute the new
322 * columns list for the commit after this one.
324 tmp_columns = graph->columns;
325 graph->columns = graph->new_columns;
326 graph->num_columns = graph->num_new_columns;
328 graph->new_columns = tmp_columns;
329 graph->num_new_columns = 0;
332 * Now update new_columns and mapping with the information for the
333 * commit after this one.
335 * First, make sure we have enough room. At most, there will
336 * be graph->num_columns + graph->num_parents columns for the next
337 * commit.
339 max_new_columns = graph->num_columns + graph->num_parents;
340 graph_ensure_capacity(graph, max_new_columns);
343 * Clear out graph->mapping
345 graph->mapping_size = 2 * max_new_columns;
346 for (i = 0; i < graph->mapping_size; i++)
347 graph->mapping[i] = -1;
350 * Populate graph->new_columns and graph->mapping
352 * Some of the parents of this commit may already be in
353 * graph->columns. If so, graph->new_columns should only contain a
354 * single entry for each such commit. graph->mapping should
355 * contain information about where each current branch line is
356 * supposed to end up after the collapsing is performed.
358 seen_this = 0;
359 mapping_idx = 0;
360 is_commit_in_columns = 1;
361 for (i = 0; i <= graph->num_columns; i++) {
362 struct commit *col_commit;
363 if (i == graph->num_columns) {
364 if (seen_this)
365 break;
366 is_commit_in_columns = 0;
367 col_commit = graph->commit;
368 } else {
369 col_commit = graph->columns[i].commit;
372 if (col_commit == graph->commit) {
373 int old_mapping_idx = mapping_idx;
374 seen_this = 1;
375 graph->commit_index = i;
376 for (parent = graph->commit->parents;
377 parent;
378 parent = parent->next) {
379 graph_insert_into_new_columns(graph,
380 parent->item,
381 &mapping_idx);
384 * We always need to increment mapping_idx by at
385 * least 2, even if it has no interesting parents.
386 * The current commit always takes up at least 2
387 * spaces.
389 if (mapping_idx == old_mapping_idx)
390 mapping_idx += 2;
391 } else {
392 graph_insert_into_new_columns(graph, col_commit,
393 &mapping_idx);
398 * Shrink mapping_size to be the minimum necessary
400 while (graph->mapping_size > 1 &&
401 graph->mapping[graph->mapping_size - 1] < 0)
402 graph->mapping_size--;
405 * Compute graph->width for this commit
407 graph_update_width(graph, is_commit_in_columns);
410 void graph_update(struct git_graph *graph, struct commit *commit)
412 struct commit_list *parent;
415 * Set the new commit
417 graph->commit = commit;
420 * Count how many interesting parents this commit has
422 graph->num_parents = 0;
423 for (parent = commit->parents; parent; parent = parent->next) {
424 if (graph_is_interesting(graph, parent->item))
425 graph->num_parents++;
429 * Store the old commit_index in prev_commit_index.
430 * graph_update_columns() will update graph->commit_index for this
431 * commit.
433 graph->prev_commit_index = graph->commit_index;
436 * Call graph_update_columns() to update
437 * columns, new_columns, and mapping.
439 graph_update_columns(graph);
441 graph->expansion_row = 0;
444 * Update graph->state.
445 * Note that we don't call graph_update_state() here, since
446 * we don't want to update graph->prev_state. No line for
447 * graph->state was ever printed.
449 * If the previous commit didn't get to the GRAPH_PADDING state,
450 * it never finished its output. Goto GRAPH_SKIP, to print out
451 * a line to indicate that portion of the graph is missing.
453 * If there are 3 or more parents, we may need to print extra rows
454 * before the commit, to expand the branch lines around it and make
455 * room for it. We need to do this only if there is a branch row
456 * (or more) to the right of this commit.
458 * If there are less than 3 parents, we can immediately print the
459 * commit line.
461 if (graph->state != GRAPH_PADDING)
462 graph->state = GRAPH_SKIP;
463 else if (graph->num_parents >= 3 &&
464 graph->commit_index < (graph->num_columns - 1))
465 graph->state = GRAPH_PRE_COMMIT;
466 else
467 graph->state = GRAPH_COMMIT;
470 static int graph_is_mapping_correct(struct git_graph *graph)
472 int i;
475 * The mapping is up to date if each entry is at its target,
476 * or is 1 greater than its target.
477 * (If it is 1 greater than the target, '/' will be printed, so it
478 * will look correct on the next row.)
480 for (i = 0; i < graph->mapping_size; i++) {
481 int target = graph->mapping[i];
482 if (target < 0)
483 continue;
484 if (target == (i / 2))
485 continue;
486 return 0;
489 return 1;
492 static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb)
495 * Add additional spaces to the end of the strbuf, so that all
496 * lines for a particular commit have the same width.
498 * This way, fields printed to the right of the graph will remain
499 * aligned for the entire commit.
501 int extra;
502 if (sb->len >= graph->width)
503 return;
505 extra = graph->width - sb->len;
506 strbuf_addf(sb, "%*s", (int) extra, "");
509 static void graph_output_padding_line(struct git_graph *graph,
510 struct strbuf *sb)
512 int i;
515 * We could conceivable be called with a NULL commit
516 * if our caller has a bug, and invokes graph_next_line()
517 * immediately after graph_init(), without first calling
518 * graph_update(). Return without outputting anything in this
519 * case.
521 if (!graph->commit)
522 return;
525 * Output a padding row, that leaves all branch lines unchanged
527 for (i = 0; i < graph->num_new_columns; i++) {
528 strbuf_addstr(sb, "| ");
531 graph_pad_horizontally(graph, sb);
534 static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
537 * Output an ellipsis to indicate that a portion
538 * of the graph is missing.
540 strbuf_addstr(sb, "...");
541 graph_pad_horizontally(graph, sb);
543 if (graph->num_parents >= 3 &&
544 graph->commit_index < (graph->num_columns - 1))
545 graph_update_state(graph, GRAPH_PRE_COMMIT);
546 else
547 graph_update_state(graph, GRAPH_COMMIT);
550 static void graph_output_pre_commit_line(struct git_graph *graph,
551 struct strbuf *sb)
553 int num_expansion_rows;
554 int i, seen_this;
557 * This function formats a row that increases the space around a commit
558 * with multiple parents, to make room for it. It should only be
559 * called when there are 3 or more parents.
561 * We need 2 extra rows for every parent over 2.
563 assert(graph->num_parents >= 3);
564 num_expansion_rows = (graph->num_parents - 2) * 2;
567 * graph->expansion_row tracks the current expansion row we are on.
568 * It should be in the range [0, num_expansion_rows - 1]
570 assert(0 <= graph->expansion_row &&
571 graph->expansion_row < num_expansion_rows);
574 * Output the row
576 seen_this = 0;
577 for (i = 0; i < graph->num_columns; i++) {
578 struct column *col = &graph->columns[i];
579 if (col->commit == graph->commit) {
580 seen_this = 1;
581 strbuf_addf(sb, "| %*s", graph->expansion_row, "");
582 } else if (seen_this && (graph->expansion_row == 0)) {
584 * This is the first line of the pre-commit output.
585 * If the previous commit was a merge commit and
586 * ended in the GRAPH_POST_MERGE state, all branch
587 * lines after graph->prev_commit_index were
588 * printed as "\" on the previous line. Continue
589 * to print them as "\" on this line. Otherwise,
590 * print the branch lines as "|".
592 if (graph->prev_state == GRAPH_POST_MERGE &&
593 graph->prev_commit_index < i)
594 strbuf_addstr(sb, "\\ ");
595 else
596 strbuf_addstr(sb, "| ");
597 } else if (seen_this && (graph->expansion_row > 0)) {
598 strbuf_addstr(sb, "\\ ");
599 } else {
600 strbuf_addstr(sb, "| ");
604 graph_pad_horizontally(graph, sb);
607 * Increment graph->expansion_row,
608 * and move to state GRAPH_COMMIT if necessary
610 graph->expansion_row++;
611 if (graph->expansion_row >= num_expansion_rows)
612 graph_update_state(graph, GRAPH_COMMIT);
615 static void graph_output_commit_char(struct git_graph *graph, struct strbuf *sb)
618 * For boundary commits, print 'o'
619 * (We should only see boundary commits when revs->boundary is set.)
621 if (graph->commit->object.flags & BOUNDARY) {
622 assert(graph->revs->boundary);
623 strbuf_addch(sb, 'o');
624 return;
628 * If revs->left_right is set, print '<' for commits that
629 * come from the left side, and '>' for commits from the right
630 * side.
632 if (graph->revs && graph->revs->left_right) {
633 if (graph->commit->object.flags & SYMMETRIC_LEFT)
634 strbuf_addch(sb, '<');
635 else
636 strbuf_addch(sb, '>');
637 return;
641 * Print 'M' for merge commits
643 * Note that we don't check graph->num_parents to determine if the
644 * commit is a merge, since that only tracks the number of
645 * "interesting" parents. We want to print 'M' for merge commits
646 * even if they have less than 2 interesting parents.
648 if (graph->commit->parents != NULL &&
649 graph->commit->parents->next != NULL) {
650 strbuf_addch(sb, 'M');
651 return;
655 * Print '*' in all other cases
657 strbuf_addch(sb, '*');
660 void graph_output_commit_line(struct git_graph *graph, struct strbuf *sb)
662 int seen_this = 0;
663 int i, j;
666 * Output the row containing this commit
667 * Iterate up to and including graph->num_columns,
668 * since the current commit may not be in any of the existing
669 * columns. (This happens when the current commit doesn't have any
670 * children that we have already processed.)
672 seen_this = 0;
673 for (i = 0; i <= graph->num_columns; i++) {
674 struct commit *col_commit;
675 if (i == graph->num_columns) {
676 if (seen_this)
677 break;
678 col_commit = graph->commit;
679 } else {
680 col_commit = graph->columns[i].commit;
683 if (col_commit == graph->commit) {
684 seen_this = 1;
685 graph_output_commit_char(graph, sb);
687 if (graph->num_parents < 3)
688 strbuf_addch(sb, ' ');
689 else {
690 int num_dashes =
691 ((graph->num_parents - 2) * 2) - 1;
692 for (j = 0; j < num_dashes; j++)
693 strbuf_addch(sb, '-');
694 strbuf_addstr(sb, ". ");
696 } else if (seen_this && (graph->num_parents > 2)) {
697 strbuf_addstr(sb, "\\ ");
698 } else if (seen_this && (graph->num_parents == 2)) {
700 * This is a 2-way merge commit.
701 * There is no GRAPH_PRE_COMMIT stage for 2-way
702 * merges, so this is the first line of output
703 * for this commit. Check to see what the previous
704 * line of output was.
706 * If it was GRAPH_POST_MERGE, the branch line
707 * coming into this commit may have been '\',
708 * and not '|' or '/'. If so, output the branch
709 * line as '\' on this line, instead of '|'. This
710 * makes the output look nicer.
712 if (graph->prev_state == GRAPH_POST_MERGE &&
713 graph->prev_commit_index < i)
714 strbuf_addstr(sb, "\\ ");
715 else
716 strbuf_addstr(sb, "| ");
717 } else {
718 strbuf_addstr(sb, "| ");
722 graph_pad_horizontally(graph, sb);
725 * Update graph->state
727 if (graph->num_parents > 1)
728 graph_update_state(graph, GRAPH_POST_MERGE);
729 else if (graph_is_mapping_correct(graph))
730 graph_update_state(graph, GRAPH_PADDING);
731 else
732 graph_update_state(graph, GRAPH_COLLAPSING);
735 void graph_output_post_merge_line(struct git_graph *graph, struct strbuf *sb)
737 int seen_this = 0;
738 int i, j;
741 * Output the post-merge row
743 for (i = 0; i <= graph->num_columns; i++) {
744 struct commit *col_commit;
745 if (i == graph->num_columns) {
746 if (seen_this)
747 break;
748 col_commit = graph->commit;
749 } else {
750 col_commit = graph->columns[i].commit;
753 if (col_commit == graph->commit) {
754 seen_this = 1;
755 strbuf_addch(sb, '|');
756 for (j = 0; j < graph->num_parents - 1; j++)
757 strbuf_addstr(sb, "\\ ");
758 } else if (seen_this) {
759 strbuf_addstr(sb, "\\ ");
760 } else {
761 strbuf_addstr(sb, "| ");
765 graph_pad_horizontally(graph, sb);
768 * Update graph->state
770 if (graph_is_mapping_correct(graph))
771 graph_update_state(graph, GRAPH_PADDING);
772 else
773 graph_update_state(graph, GRAPH_COLLAPSING);
776 void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
778 int i;
779 int *tmp_mapping;
782 * Clear out the new_mapping array
784 for (i = 0; i < graph->mapping_size; i++)
785 graph->new_mapping[i] = -1;
787 for (i = 0; i < graph->mapping_size; i++) {
788 int target = graph->mapping[i];
789 if (target < 0)
790 continue;
793 * Since update_columns() always inserts the leftmost
794 * column first, each branch's target location should
795 * always be either its current location or to the left of
796 * its current location.
798 * We never have to move branches to the right. This makes
799 * the graph much more legible, since whenever branches
800 * cross, only one is moving directions.
802 assert(target * 2 <= i);
804 if (target * 2 == i) {
806 * This column is already in the
807 * correct place
809 assert(graph->new_mapping[i] == -1);
810 graph->new_mapping[i] = target;
811 } else if (graph->new_mapping[i - 1] < 0) {
813 * Nothing is to the left.
814 * Move to the left by one
816 graph->new_mapping[i - 1] = target;
817 } else if (graph->new_mapping[i - 1] == target) {
819 * There is a branch line to our left
820 * already, and it is our target. We
821 * combine with this line, since we share
822 * the same parent commit.
824 * We don't have to add anything to the
825 * output or new_mapping, since the
826 * existing branch line has already taken
827 * care of it.
829 } else {
831 * There is a branch line to our left,
832 * but it isn't our target. We need to
833 * cross over it.
835 * The space just to the left of this
836 * branch should always be empty.
838 assert(graph->new_mapping[i - 1] > target);
839 assert(graph->new_mapping[i - 2] < 0);
840 graph->new_mapping[i - 2] = target;
845 * The new mapping may be 1 smaller than the old mapping
847 if (graph->new_mapping[graph->mapping_size - 1] < 0)
848 graph->mapping_size--;
851 * Output out a line based on the new mapping info
853 for (i = 0; i < graph->mapping_size; i++) {
854 int target = graph->new_mapping[i];
855 if (target < 0)
856 strbuf_addch(sb, ' ');
857 else if (target * 2 == i)
858 strbuf_addch(sb, '|');
859 else
860 strbuf_addch(sb, '/');
863 graph_pad_horizontally(graph, sb);
866 * Swap mapping and new_mapping
868 tmp_mapping = graph->mapping;
869 graph->mapping = graph->new_mapping;
870 graph->new_mapping = tmp_mapping;
873 * If graph->mapping indicates that all of the branch lines
874 * are already in the correct positions, we are done.
875 * Otherwise, we need to collapse some branch lines together.
877 if (graph_is_mapping_correct(graph))
878 graph_update_state(graph, GRAPH_PADDING);
881 int graph_next_line(struct git_graph *graph, struct strbuf *sb)
883 switch (graph->state) {
884 case GRAPH_PADDING:
885 graph_output_padding_line(graph, sb);
886 return 0;
887 case GRAPH_SKIP:
888 graph_output_skip_line(graph, sb);
889 return 0;
890 case GRAPH_PRE_COMMIT:
891 graph_output_pre_commit_line(graph, sb);
892 return 0;
893 case GRAPH_COMMIT:
894 graph_output_commit_line(graph, sb);
895 return 1;
896 case GRAPH_POST_MERGE:
897 graph_output_post_merge_line(graph, sb);
898 return 0;
899 case GRAPH_COLLAPSING:
900 graph_output_collapsing_line(graph, sb);
901 return 0;
904 assert(0);
905 return 0;
908 void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
910 int i, j;
912 if (graph->state != GRAPH_COMMIT) {
913 graph_next_line(graph, sb);
914 return;
918 * Output the row containing this commit
919 * Iterate up to and including graph->num_columns,
920 * since the current commit may not be in any of the existing
921 * columns. (This happens when the current commit doesn't have any
922 * children that we have already processed.)
924 for (i = 0; i < graph->num_columns; i++) {
925 struct commit *col_commit = graph->columns[i].commit;
926 if (col_commit == graph->commit) {
927 strbuf_addch(sb, '|');
929 if (graph->num_parents < 3)
930 strbuf_addch(sb, ' ');
931 else {
932 int num_spaces = ((graph->num_parents - 2) * 2);
933 for (j = 0; j < num_spaces; j++)
934 strbuf_addch(sb, ' ');
936 } else {
937 strbuf_addstr(sb, "| ");
941 graph_pad_horizontally(graph, sb);
944 * Update graph->prev_state since we have output a padding line
946 graph->prev_state = GRAPH_PADDING;
949 int graph_is_commit_finished(struct git_graph const *graph)
951 return (graph->state == GRAPH_PADDING);
954 void graph_show_commit(struct git_graph *graph)
956 struct strbuf msgbuf;
957 int shown_commit_line = 0;
959 if (!graph)
960 return;
962 strbuf_init(&msgbuf, 0);
964 while (!shown_commit_line) {
965 shown_commit_line = graph_next_line(graph, &msgbuf);
966 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
967 if (!shown_commit_line)
968 putchar('\n');
969 strbuf_setlen(&msgbuf, 0);
972 strbuf_release(&msgbuf);
975 void graph_show_oneline(struct git_graph *graph)
977 struct strbuf msgbuf;
979 if (!graph)
980 return;
982 strbuf_init(&msgbuf, 0);
983 graph_next_line(graph, &msgbuf);
984 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
985 strbuf_release(&msgbuf);
988 void graph_show_padding(struct git_graph *graph)
990 struct strbuf msgbuf;
992 if (!graph)
993 return;
995 strbuf_init(&msgbuf, 0);
996 graph_padding_line(graph, &msgbuf);
997 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
998 strbuf_release(&msgbuf);
1001 int graph_show_remainder(struct git_graph *graph)
1003 struct strbuf msgbuf;
1004 int shown = 0;
1006 if (!graph)
1007 return 0;
1009 if (graph_is_commit_finished(graph))
1010 return 0;
1012 strbuf_init(&msgbuf, 0);
1013 for (;;) {
1014 graph_next_line(graph, &msgbuf);
1015 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1016 strbuf_setlen(&msgbuf, 0);
1017 shown = 1;
1019 if (!graph_is_commit_finished(graph))
1020 putchar('\n');
1021 else
1022 break;
1024 strbuf_release(&msgbuf);
1026 return shown;
1030 void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb)
1032 char *p;
1034 if (!graph) {
1035 fwrite(sb->buf, sizeof(char), sb->len, stdout);
1036 return;
1040 * Print the strbuf line by line,
1041 * and display the graph info before each line but the first.
1043 p = sb->buf;
1044 while (p) {
1045 size_t len;
1046 char *next_p = strchr(p, '\n');
1047 if (next_p) {
1048 next_p++;
1049 len = next_p - p;
1050 } else {
1051 len = (sb->buf + sb->len) - p;
1053 fwrite(p, sizeof(char), len, stdout);
1054 if (next_p && *next_p != '\0')
1055 graph_show_oneline(graph);
1056 p = next_p;
1060 void graph_show_commit_msg(struct git_graph *graph,
1061 struct strbuf const *sb)
1063 int newline_terminated;
1065 if (!graph) {
1067 * If there's no graph, just print the message buffer.
1069 * The message buffer for CMIT_FMT_ONELINE and
1070 * CMIT_FMT_USERFORMAT are already missing a terminating
1071 * newline. All of the other formats should have it.
1073 fwrite(sb->buf, sizeof(char), sb->len, stdout);
1074 return;
1077 newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
1080 * Show the commit message
1082 graph_show_strbuf(graph, sb);
1085 * If there is more output needed for this commit, show it now
1087 if (!graph_is_commit_finished(graph)) {
1089 * If sb doesn't have a terminating newline, print one now,
1090 * so we can start the remainder of the graph output on a
1091 * new line.
1093 if (!newline_terminated)
1094 putchar('\n');
1096 graph_show_remainder(graph);
1099 * If sb ends with a newline, our output should too.
1101 if (newline_terminated)
1102 putchar('\n');