fix cat-file usage message and documentation
[git/kirr.git] / graph.c
blob06fbeb6c2416d0c1c5fd7df1a12924656592cdc3
1 #include "cache.h"
2 #include "commit.h"
3 #include "color.h"
4 #include "graph.h"
5 #include "diff.h"
6 #include "revision.h"
8 /* Internal API */
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);
46 * TODO:
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.
51 * - The output during the GRAPH_PRE_COMMIT and GRAPH_COLLAPSING states
52 * could be made more compact by printing horizontal lines, instead of
53 * long diagonal lines. For example, during collapsing, something like
54 * this: instead of this:
55 * | | | | | | | | | |
56 * | |_|_|/ | | | |/
57 * |/| | | | | |/|
58 * | | | | | |/| |
59 * |/| | |
60 * | | | |
62 * If there are several parallel diagonal lines, they will need to be
63 * replaced with horizontal lines on subsequent rows.
66 struct column {
68 * The parent commit of this column.
70 struct commit *commit;
72 * The color to (optionally) print this column in. This is an
73 * index into column_colors.
75 unsigned short color;
78 enum graph_state {
79 GRAPH_PADDING,
80 GRAPH_SKIP,
81 GRAPH_PRE_COMMIT,
82 GRAPH_COMMIT,
83 GRAPH_POST_MERGE,
84 GRAPH_COLLAPSING
88 * The list of available column colors.
90 static char column_colors[][COLOR_MAXLEN] = {
91 GIT_COLOR_RED,
92 GIT_COLOR_GREEN,
93 GIT_COLOR_YELLOW,
94 GIT_COLOR_BLUE,
95 GIT_COLOR_MAGENTA,
96 GIT_COLOR_CYAN,
97 GIT_COLOR_BOLD GIT_COLOR_RED,
98 GIT_COLOR_BOLD GIT_COLOR_GREEN,
99 GIT_COLOR_BOLD GIT_COLOR_YELLOW,
100 GIT_COLOR_BOLD GIT_COLOR_BLUE,
101 GIT_COLOR_BOLD GIT_COLOR_MAGENTA,
102 GIT_COLOR_BOLD GIT_COLOR_CYAN,
105 #define COLUMN_COLORS_MAX (ARRAY_SIZE(column_colors))
107 static const char *column_get_color_code(const struct column *c)
109 return column_colors[c->color];
112 static void strbuf_write_column(struct strbuf *sb, const struct column *c,
113 char col_char)
115 if (c->color < COLUMN_COLORS_MAX)
116 strbuf_addstr(sb, column_get_color_code(c));
117 strbuf_addch(sb, col_char);
118 if (c->color < COLUMN_COLORS_MAX)
119 strbuf_addstr(sb, GIT_COLOR_RESET);
122 struct git_graph {
124 * The commit currently being processed
126 struct commit *commit;
127 /* The rev-info used for the current traversal */
128 struct rev_info *revs;
130 * The number of interesting parents that this commit has.
132 * Note that this is not the same as the actual number of parents.
133 * This count excludes parents that won't be printed in the graph
134 * output, as determined by graph_is_interesting().
136 int num_parents;
138 * The width of the graph output for this commit.
139 * All rows for this commit are padded to this width, so that
140 * messages printed after the graph output are aligned.
142 int width;
144 * The next expansion row to print
145 * when state is GRAPH_PRE_COMMIT
147 int expansion_row;
149 * The current output state.
150 * This tells us what kind of line graph_next_line() should output.
152 enum graph_state state;
154 * The output state for the previous line of output.
155 * This is primarily used to determine how the first merge line
156 * should appear, based on the last line of the previous commit.
158 enum graph_state prev_state;
160 * The index of the column that refers to this commit.
162 * If none of the incoming columns refer to this commit,
163 * this will be equal to num_columns.
165 int commit_index;
167 * The commit_index for the previously displayed commit.
169 * This is used to determine how the first line of a merge
170 * graph output should appear, based on the last line of the
171 * previous commit.
173 int prev_commit_index;
175 * The maximum number of columns that can be stored in the columns
176 * and new_columns arrays. This is also half the number of entries
177 * that can be stored in the mapping and new_mapping arrays.
179 int column_capacity;
181 * The number of columns (also called "branch lines" in some places)
183 int num_columns;
185 * The number of columns in the new_columns array
187 int num_new_columns;
189 * The number of entries in the mapping array
191 int mapping_size;
193 * The column state before we output the current commit.
195 struct column *columns;
197 * The new column state after we output the current commit.
198 * Only valid when state is GRAPH_COLLAPSING.
200 struct column *new_columns;
202 * An array that tracks the current state of each
203 * character in the output line during state GRAPH_COLLAPSING.
204 * Each entry is -1 if this character is empty, or a non-negative
205 * integer if the character contains a branch line. The value of
206 * the integer indicates the target position for this branch line.
207 * (I.e., this array maps the current column positions to their
208 * desired positions.)
210 * The maximum capacity of this array is always
211 * sizeof(int) * 2 * column_capacity.
213 int *mapping;
215 * A temporary array for computing the next mapping state
216 * while we are outputting a mapping line. This is stored as part
217 * of the git_graph simply so we don't have to allocate a new
218 * temporary array each time we have to output a collapsing line.
220 int *new_mapping;
222 * The current default column color being used. This is
223 * stored as an index into the array column_colors.
225 unsigned short default_column_color;
228 struct git_graph *graph_init(struct rev_info *opt)
230 struct git_graph *graph = xmalloc(sizeof(struct git_graph));
231 graph->commit = NULL;
232 graph->revs = opt;
233 graph->num_parents = 0;
234 graph->expansion_row = 0;
235 graph->state = GRAPH_PADDING;
236 graph->prev_state = GRAPH_PADDING;
237 graph->commit_index = 0;
238 graph->prev_commit_index = 0;
239 graph->num_columns = 0;
240 graph->num_new_columns = 0;
241 graph->mapping_size = 0;
242 graph->default_column_color = 0;
245 * Allocate a reasonably large default number of columns
246 * We'll automatically grow columns later if we need more room.
248 graph->column_capacity = 30;
249 graph->columns = xmalloc(sizeof(struct column) *
250 graph->column_capacity);
251 graph->new_columns = xmalloc(sizeof(struct column) *
252 graph->column_capacity);
253 graph->mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
254 graph->new_mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
256 return graph;
259 static void graph_update_state(struct git_graph *graph, enum graph_state s)
261 graph->prev_state = graph->state;
262 graph->state = s;
265 static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
267 if (graph->column_capacity >= num_columns)
268 return;
270 do {
271 graph->column_capacity *= 2;
272 } while (graph->column_capacity < num_columns);
274 graph->columns = xrealloc(graph->columns,
275 sizeof(struct column) *
276 graph->column_capacity);
277 graph->new_columns = xrealloc(graph->new_columns,
278 sizeof(struct column) *
279 graph->column_capacity);
280 graph->mapping = xrealloc(graph->mapping,
281 sizeof(int) * 2 * graph->column_capacity);
282 graph->new_mapping = xrealloc(graph->new_mapping,
283 sizeof(int) * 2 * graph->column_capacity);
287 * Returns 1 if the commit will be printed in the graph output,
288 * and 0 otherwise.
290 static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
293 * If revs->boundary is set, commits whose children have
294 * been shown are always interesting, even if they have the
295 * UNINTERESTING or TREESAME flags set.
297 if (graph->revs && graph->revs->boundary) {
298 if (commit->object.flags & CHILD_SHOWN)
299 return 1;
303 * Uninteresting and pruned commits won't be printed
305 return (commit->object.flags & (UNINTERESTING | TREESAME)) ? 0 : 1;
308 static struct commit_list *next_interesting_parent(struct git_graph *graph,
309 struct commit_list *orig)
311 struct commit_list *list;
314 * If revs->first_parent_only is set, only the first
315 * parent is interesting. None of the others are.
317 if (graph->revs->first_parent_only)
318 return NULL;
321 * Return the next interesting commit after orig
323 for (list = orig->next; list; list = list->next) {
324 if (graph_is_interesting(graph, list->item))
325 return list;
328 return NULL;
331 static struct commit_list *first_interesting_parent(struct git_graph *graph)
333 struct commit_list *parents = graph->commit->parents;
336 * If this commit has no parents, ignore it
338 if (!parents)
339 return NULL;
342 * If the first parent is interesting, return it
344 if (graph_is_interesting(graph, parents->item))
345 return parents;
348 * Otherwise, call next_interesting_parent() to get
349 * the next interesting parent
351 return next_interesting_parent(graph, parents);
354 static unsigned short graph_get_current_column_color(const struct git_graph *graph)
356 if (!DIFF_OPT_TST(&graph->revs->diffopt, COLOR_DIFF))
357 return COLUMN_COLORS_MAX;
358 return graph->default_column_color;
362 * Update the graph's default column color.
364 static void graph_increment_column_color(struct git_graph *graph)
366 graph->default_column_color = (graph->default_column_color + 1) %
367 COLUMN_COLORS_MAX;
370 static unsigned short graph_find_commit_color(const struct git_graph *graph,
371 const struct commit *commit)
373 int i;
374 for (i = 0; i < graph->num_columns; i++) {
375 if (graph->columns[i].commit == commit)
376 return graph->columns[i].color;
378 return graph_get_current_column_color(graph);
381 static void graph_insert_into_new_columns(struct git_graph *graph,
382 struct commit *commit,
383 int *mapping_index)
385 int i;
388 * If the commit is already in the new_columns list, we don't need to
389 * add it. Just update the mapping correctly.
391 for (i = 0; i < graph->num_new_columns; i++) {
392 if (graph->new_columns[i].commit == commit) {
393 graph->mapping[*mapping_index] = i;
394 *mapping_index += 2;
395 return;
400 * This commit isn't already in new_columns. Add it.
402 graph->new_columns[graph->num_new_columns].commit = commit;
403 graph->new_columns[graph->num_new_columns].color = graph_find_commit_color(graph, commit);
404 graph->mapping[*mapping_index] = graph->num_new_columns;
405 *mapping_index += 2;
406 graph->num_new_columns++;
409 static void graph_update_width(struct git_graph *graph,
410 int is_commit_in_existing_columns)
413 * Compute the width needed to display the graph for this commit.
414 * This is the maximum width needed for any row. All other rows
415 * will be padded to this width.
417 * Compute the number of columns in the widest row:
418 * Count each existing column (graph->num_columns), and each new
419 * column added by this commit.
421 int max_cols = graph->num_columns + graph->num_parents;
424 * Even if the current commit has no parents to be printed, it
425 * still takes up a column for itself.
427 if (graph->num_parents < 1)
428 max_cols++;
431 * We added a column for the the current commit as part of
432 * graph->num_parents. If the current commit was already in
433 * graph->columns, then we have double counted it.
435 if (is_commit_in_existing_columns)
436 max_cols--;
439 * Each column takes up 2 spaces
441 graph->width = max_cols * 2;
444 static void graph_update_columns(struct git_graph *graph)
446 struct commit_list *parent;
447 struct column *tmp_columns;
448 int max_new_columns;
449 int mapping_idx;
450 int i, seen_this, is_commit_in_columns;
453 * Swap graph->columns with graph->new_columns
454 * graph->columns contains the state for the previous commit,
455 * and new_columns now contains the state for our commit.
457 * We'll re-use the old columns array as storage to compute the new
458 * columns list for the commit after this one.
460 tmp_columns = graph->columns;
461 graph->columns = graph->new_columns;
462 graph->num_columns = graph->num_new_columns;
464 graph->new_columns = tmp_columns;
465 graph->num_new_columns = 0;
468 * Now update new_columns and mapping with the information for the
469 * commit after this one.
471 * First, make sure we have enough room. At most, there will
472 * be graph->num_columns + graph->num_parents columns for the next
473 * commit.
475 max_new_columns = graph->num_columns + graph->num_parents;
476 graph_ensure_capacity(graph, max_new_columns);
479 * Clear out graph->mapping
481 graph->mapping_size = 2 * max_new_columns;
482 for (i = 0; i < graph->mapping_size; i++)
483 graph->mapping[i] = -1;
486 * Populate graph->new_columns and graph->mapping
488 * Some of the parents of this commit may already be in
489 * graph->columns. If so, graph->new_columns should only contain a
490 * single entry for each such commit. graph->mapping should
491 * contain information about where each current branch line is
492 * supposed to end up after the collapsing is performed.
494 seen_this = 0;
495 mapping_idx = 0;
496 is_commit_in_columns = 1;
497 for (i = 0; i <= graph->num_columns; i++) {
498 struct commit *col_commit;
499 if (i == graph->num_columns) {
500 if (seen_this)
501 break;
502 is_commit_in_columns = 0;
503 col_commit = graph->commit;
504 } else {
505 col_commit = graph->columns[i].commit;
508 if (col_commit == graph->commit) {
509 int old_mapping_idx = mapping_idx;
510 seen_this = 1;
511 graph->commit_index = i;
512 for (parent = first_interesting_parent(graph);
513 parent;
514 parent = next_interesting_parent(graph, parent)) {
516 * If this is a merge increment the current
517 * color.
519 if (graph->num_parents > 1)
520 graph_increment_column_color(graph);
521 graph_insert_into_new_columns(graph,
522 parent->item,
523 &mapping_idx);
526 * We always need to increment mapping_idx by at
527 * least 2, even if it has no interesting parents.
528 * The current commit always takes up at least 2
529 * spaces.
531 if (mapping_idx == old_mapping_idx)
532 mapping_idx += 2;
533 } else {
534 graph_insert_into_new_columns(graph, col_commit,
535 &mapping_idx);
540 * Shrink mapping_size to be the minimum necessary
542 while (graph->mapping_size > 1 &&
543 graph->mapping[graph->mapping_size - 1] < 0)
544 graph->mapping_size--;
547 * Compute graph->width for this commit
549 graph_update_width(graph, is_commit_in_columns);
552 void graph_update(struct git_graph *graph, struct commit *commit)
554 struct commit_list *parent;
557 * Set the new commit
559 graph->commit = commit;
562 * Count how many interesting parents this commit has
564 graph->num_parents = 0;
565 for (parent = first_interesting_parent(graph);
566 parent;
567 parent = next_interesting_parent(graph, parent))
569 graph->num_parents++;
573 * Store the old commit_index in prev_commit_index.
574 * graph_update_columns() will update graph->commit_index for this
575 * commit.
577 graph->prev_commit_index = graph->commit_index;
580 * Call graph_update_columns() to update
581 * columns, new_columns, and mapping.
583 graph_update_columns(graph);
585 graph->expansion_row = 0;
588 * Update graph->state.
589 * Note that we don't call graph_update_state() here, since
590 * we don't want to update graph->prev_state. No line for
591 * graph->state was ever printed.
593 * If the previous commit didn't get to the GRAPH_PADDING state,
594 * it never finished its output. Goto GRAPH_SKIP, to print out
595 * a line to indicate that portion of the graph is missing.
597 * If there are 3 or more parents, we may need to print extra rows
598 * before the commit, to expand the branch lines around it and make
599 * room for it. We need to do this only if there is a branch row
600 * (or more) to the right of this commit.
602 * If there are less than 3 parents, we can immediately print the
603 * commit line.
605 if (graph->state != GRAPH_PADDING)
606 graph->state = GRAPH_SKIP;
607 else if (graph->num_parents >= 3 &&
608 graph->commit_index < (graph->num_columns - 1))
609 graph->state = GRAPH_PRE_COMMIT;
610 else
611 graph->state = GRAPH_COMMIT;
614 static int graph_is_mapping_correct(struct git_graph *graph)
616 int i;
619 * The mapping is up to date if each entry is at its target,
620 * or is 1 greater than its target.
621 * (If it is 1 greater than the target, '/' will be printed, so it
622 * will look correct on the next row.)
624 for (i = 0; i < graph->mapping_size; i++) {
625 int target = graph->mapping[i];
626 if (target < 0)
627 continue;
628 if (target == (i / 2))
629 continue;
630 return 0;
633 return 1;
636 static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb,
637 int chars_written)
640 * Add additional spaces to the end of the strbuf, so that all
641 * lines for a particular commit have the same width.
643 * This way, fields printed to the right of the graph will remain
644 * aligned for the entire commit.
646 int extra;
647 if (chars_written >= graph->width)
648 return;
650 extra = graph->width - chars_written;
651 strbuf_addf(sb, "%*s", (int) extra, "");
654 static void graph_output_padding_line(struct git_graph *graph,
655 struct strbuf *sb)
657 int i;
660 * We could conceivable be called with a NULL commit
661 * if our caller has a bug, and invokes graph_next_line()
662 * immediately after graph_init(), without first calling
663 * graph_update(). Return without outputting anything in this
664 * case.
666 if (!graph->commit)
667 return;
670 * Output a padding row, that leaves all branch lines unchanged
672 for (i = 0; i < graph->num_new_columns; i++) {
673 strbuf_write_column(sb, &graph->new_columns[i], '|');
674 strbuf_addch(sb, ' ');
677 graph_pad_horizontally(graph, sb, graph->num_new_columns * 2);
680 static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
683 * Output an ellipsis to indicate that a portion
684 * of the graph is missing.
686 strbuf_addstr(sb, "...");
687 graph_pad_horizontally(graph, sb, 3);
689 if (graph->num_parents >= 3 &&
690 graph->commit_index < (graph->num_columns - 1))
691 graph_update_state(graph, GRAPH_PRE_COMMIT);
692 else
693 graph_update_state(graph, GRAPH_COMMIT);
696 static void graph_output_pre_commit_line(struct git_graph *graph,
697 struct strbuf *sb)
699 int num_expansion_rows;
700 int i, seen_this;
701 int chars_written;
704 * This function formats a row that increases the space around a commit
705 * with multiple parents, to make room for it. It should only be
706 * called when there are 3 or more parents.
708 * We need 2 extra rows for every parent over 2.
710 assert(graph->num_parents >= 3);
711 num_expansion_rows = (graph->num_parents - 2) * 2;
714 * graph->expansion_row tracks the current expansion row we are on.
715 * It should be in the range [0, num_expansion_rows - 1]
717 assert(0 <= graph->expansion_row &&
718 graph->expansion_row < num_expansion_rows);
721 * Output the row
723 seen_this = 0;
724 chars_written = 0;
725 for (i = 0; i < graph->num_columns; i++) {
726 struct column *col = &graph->columns[i];
727 if (col->commit == graph->commit) {
728 seen_this = 1;
729 strbuf_write_column(sb, col, '|');
730 strbuf_addf(sb, "%*s", graph->expansion_row, "");
731 chars_written += 1 + graph->expansion_row;
732 } else if (seen_this && (graph->expansion_row == 0)) {
734 * This is the first line of the pre-commit output.
735 * If the previous commit was a merge commit and
736 * ended in the GRAPH_POST_MERGE state, all branch
737 * lines after graph->prev_commit_index were
738 * printed as "\" on the previous line. Continue
739 * to print them as "\" on this line. Otherwise,
740 * print the branch lines as "|".
742 if (graph->prev_state == GRAPH_POST_MERGE &&
743 graph->prev_commit_index < i)
744 strbuf_write_column(sb, col, '\\');
745 else
746 strbuf_write_column(sb, col, '|');
747 chars_written++;
748 } else if (seen_this && (graph->expansion_row > 0)) {
749 strbuf_write_column(sb, col, '\\');
750 chars_written++;
751 } else {
752 strbuf_write_column(sb, col, '|');
753 chars_written++;
755 strbuf_addch(sb, ' ');
756 chars_written++;
759 graph_pad_horizontally(graph, sb, chars_written);
762 * Increment graph->expansion_row,
763 * and move to state GRAPH_COMMIT if necessary
765 graph->expansion_row++;
766 if (graph->expansion_row >= num_expansion_rows)
767 graph_update_state(graph, GRAPH_COMMIT);
770 static void graph_output_commit_char(struct git_graph *graph, struct strbuf *sb)
773 * For boundary commits, print 'o'
774 * (We should only see boundary commits when revs->boundary is set.)
776 if (graph->commit->object.flags & BOUNDARY) {
777 assert(graph->revs->boundary);
778 strbuf_addch(sb, 'o');
779 return;
783 * If revs->left_right is set, print '<' for commits that
784 * come from the left side, and '>' for commits from the right
785 * side.
787 if (graph->revs && graph->revs->left_right) {
788 if (graph->commit->object.flags & SYMMETRIC_LEFT)
789 strbuf_addch(sb, '<');
790 else
791 strbuf_addch(sb, '>');
792 return;
796 * Print '*' in all other cases
798 strbuf_addch(sb, '*');
802 * Draw an octopus merge and return the number of characters written.
804 static int graph_draw_octopus_merge(struct git_graph *graph,
805 struct strbuf *sb)
808 * Here dashless_commits represents the number of parents
809 * which don't need to have dashes (because their edges fit
810 * neatly under the commit).
812 const int dashless_commits = 2;
813 int col_num, i;
814 int num_dashes =
815 ((graph->num_parents - dashless_commits) * 2) - 1;
816 for (i = 0; i < num_dashes; i++) {
817 col_num = (i / 2) + dashless_commits;
818 strbuf_write_column(sb, &graph->new_columns[col_num], '-');
820 col_num = (i / 2) + dashless_commits;
821 strbuf_write_column(sb, &graph->new_columns[col_num], '.');
822 return num_dashes + 1;
825 static void graph_output_commit_line(struct git_graph *graph, struct strbuf *sb)
827 int seen_this = 0;
828 int i, chars_written;
831 * Output the row containing this commit
832 * Iterate up to and including graph->num_columns,
833 * since the current commit may not be in any of the existing
834 * columns. (This happens when the current commit doesn't have any
835 * children that we have already processed.)
837 seen_this = 0;
838 chars_written = 0;
839 for (i = 0; i <= graph->num_columns; i++) {
840 struct column *col = &graph->columns[i];
841 struct commit *col_commit;
842 if (i == graph->num_columns) {
843 if (seen_this)
844 break;
845 col_commit = graph->commit;
846 } else {
847 col_commit = graph->columns[i].commit;
850 if (col_commit == graph->commit) {
851 seen_this = 1;
852 graph_output_commit_char(graph, sb);
853 chars_written++;
855 if (graph->num_parents > 2)
856 chars_written += graph_draw_octopus_merge(graph,
857 sb);
858 } else if (seen_this && (graph->num_parents > 2)) {
859 strbuf_write_column(sb, col, '\\');
860 chars_written++;
861 } else if (seen_this && (graph->num_parents == 2)) {
863 * This is a 2-way merge commit.
864 * There is no GRAPH_PRE_COMMIT stage for 2-way
865 * merges, so this is the first line of output
866 * for this commit. Check to see what the previous
867 * line of output was.
869 * If it was GRAPH_POST_MERGE, the branch line
870 * coming into this commit may have been '\',
871 * and not '|' or '/'. If so, output the branch
872 * line as '\' on this line, instead of '|'. This
873 * makes the output look nicer.
875 if (graph->prev_state == GRAPH_POST_MERGE &&
876 graph->prev_commit_index < i)
877 strbuf_write_column(sb, col, '\\');
878 else
879 strbuf_write_column(sb, col, '|');
880 chars_written++;
881 } else {
882 strbuf_write_column(sb, col, '|');
883 chars_written++;
885 strbuf_addch(sb, ' ');
886 chars_written++;
889 graph_pad_horizontally(graph, sb, chars_written);
892 * Update graph->state
894 if (graph->num_parents > 1)
895 graph_update_state(graph, GRAPH_POST_MERGE);
896 else if (graph_is_mapping_correct(graph))
897 graph_update_state(graph, GRAPH_PADDING);
898 else
899 graph_update_state(graph, GRAPH_COLLAPSING);
902 static struct column *find_new_column_by_commit(struct git_graph *graph,
903 struct commit *commit)
905 int i;
906 for (i = 0; i < graph->num_new_columns; i++) {
907 if (graph->new_columns[i].commit == commit)
908 return &graph->new_columns[i];
910 return 0;
913 static void graph_output_post_merge_line(struct git_graph *graph, struct strbuf *sb)
915 int seen_this = 0;
916 int i, j, chars_written;
919 * Output the post-merge row
921 chars_written = 0;
922 for (i = 0; i <= graph->num_columns; i++) {
923 struct column *col = &graph->columns[i];
924 struct commit *col_commit;
925 if (i == graph->num_columns) {
926 if (seen_this)
927 break;
928 col_commit = graph->commit;
929 } else {
930 col_commit = col->commit;
933 if (col_commit == graph->commit) {
935 * Since the current commit is a merge find
936 * the columns for the parent commits in
937 * new_columns and use those to format the
938 * edges.
940 struct commit_list *parents = NULL;
941 struct column *par_column;
942 seen_this = 1;
943 parents = first_interesting_parent(graph);
944 assert(parents);
945 par_column = find_new_column_by_commit(graph, parents->item);
946 assert(par_column);
948 strbuf_write_column(sb, par_column, '|');
949 chars_written++;
950 for (j = 0; j < graph->num_parents - 1; j++) {
951 parents = next_interesting_parent(graph, parents);
952 assert(parents);
953 par_column = find_new_column_by_commit(graph, parents->item);
954 assert(par_column);
955 strbuf_write_column(sb, par_column, '\\');
956 strbuf_addch(sb, ' ');
958 chars_written += j * 2;
959 } else if (seen_this) {
960 strbuf_write_column(sb, col, '\\');
961 strbuf_addch(sb, ' ');
962 chars_written += 2;
963 } else {
964 strbuf_write_column(sb, col, '|');
965 strbuf_addch(sb, ' ');
966 chars_written += 2;
970 graph_pad_horizontally(graph, sb, chars_written);
973 * Update graph->state
975 if (graph_is_mapping_correct(graph))
976 graph_update_state(graph, GRAPH_PADDING);
977 else
978 graph_update_state(graph, GRAPH_COLLAPSING);
981 static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
983 int i;
984 int *tmp_mapping;
987 * Clear out the new_mapping array
989 for (i = 0; i < graph->mapping_size; i++)
990 graph->new_mapping[i] = -1;
992 for (i = 0; i < graph->mapping_size; i++) {
993 int target = graph->mapping[i];
994 if (target < 0)
995 continue;
998 * Since update_columns() always inserts the leftmost
999 * column first, each branch's target location should
1000 * always be either its current location or to the left of
1001 * its current location.
1003 * We never have to move branches to the right. This makes
1004 * the graph much more legible, since whenever branches
1005 * cross, only one is moving directions.
1007 assert(target * 2 <= i);
1009 if (target * 2 == i) {
1011 * This column is already in the
1012 * correct place
1014 assert(graph->new_mapping[i] == -1);
1015 graph->new_mapping[i] = target;
1016 } else if (graph->new_mapping[i - 1] < 0) {
1018 * Nothing is to the left.
1019 * Move to the left by one
1021 graph->new_mapping[i - 1] = target;
1022 } else if (graph->new_mapping[i - 1] == target) {
1024 * There is a branch line to our left
1025 * already, and it is our target. We
1026 * combine with this line, since we share
1027 * the same parent commit.
1029 * We don't have to add anything to the
1030 * output or new_mapping, since the
1031 * existing branch line has already taken
1032 * care of it.
1034 } else {
1036 * There is a branch line to our left,
1037 * but it isn't our target. We need to
1038 * cross over it.
1040 * The space just to the left of this
1041 * branch should always be empty.
1043 assert(graph->new_mapping[i - 1] > target);
1044 assert(graph->new_mapping[i - 2] < 0);
1045 graph->new_mapping[i - 2] = target;
1050 * The new mapping may be 1 smaller than the old mapping
1052 if (graph->new_mapping[graph->mapping_size - 1] < 0)
1053 graph->mapping_size--;
1056 * Output out a line based on the new mapping info
1058 for (i = 0; i < graph->mapping_size; i++) {
1059 int target = graph->new_mapping[i];
1060 if (target < 0)
1061 strbuf_addch(sb, ' ');
1062 else if (target * 2 == i)
1063 strbuf_write_column(sb, &graph->new_columns[target], '|');
1064 else
1065 strbuf_write_column(sb, &graph->new_columns[target], '/');
1068 graph_pad_horizontally(graph, sb, graph->mapping_size);
1071 * Swap mapping and new_mapping
1073 tmp_mapping = graph->mapping;
1074 graph->mapping = graph->new_mapping;
1075 graph->new_mapping = tmp_mapping;
1078 * If graph->mapping indicates that all of the branch lines
1079 * are already in the correct positions, we are done.
1080 * Otherwise, we need to collapse some branch lines together.
1082 if (graph_is_mapping_correct(graph))
1083 graph_update_state(graph, GRAPH_PADDING);
1086 static int graph_next_line(struct git_graph *graph, struct strbuf *sb)
1088 switch (graph->state) {
1089 case GRAPH_PADDING:
1090 graph_output_padding_line(graph, sb);
1091 return 0;
1092 case GRAPH_SKIP:
1093 graph_output_skip_line(graph, sb);
1094 return 0;
1095 case GRAPH_PRE_COMMIT:
1096 graph_output_pre_commit_line(graph, sb);
1097 return 0;
1098 case GRAPH_COMMIT:
1099 graph_output_commit_line(graph, sb);
1100 return 1;
1101 case GRAPH_POST_MERGE:
1102 graph_output_post_merge_line(graph, sb);
1103 return 0;
1104 case GRAPH_COLLAPSING:
1105 graph_output_collapsing_line(graph, sb);
1106 return 0;
1109 assert(0);
1110 return 0;
1113 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
1115 int i, j;
1117 if (graph->state != GRAPH_COMMIT) {
1118 graph_next_line(graph, sb);
1119 return;
1123 * Output the row containing this commit
1124 * Iterate up to and including graph->num_columns,
1125 * since the current commit may not be in any of the existing
1126 * columns. (This happens when the current commit doesn't have any
1127 * children that we have already processed.)
1129 for (i = 0; i < graph->num_columns; i++) {
1130 struct column *col = &graph->columns[i];
1131 struct commit *col_commit = col->commit;
1132 if (col_commit == graph->commit) {
1133 strbuf_write_column(sb, col, '|');
1135 if (graph->num_parents < 3)
1136 strbuf_addch(sb, ' ');
1137 else {
1138 int num_spaces = ((graph->num_parents - 2) * 2);
1139 for (j = 0; j < num_spaces; j++)
1140 strbuf_addch(sb, ' ');
1142 } else {
1143 strbuf_write_column(sb, col, '|');
1144 strbuf_addch(sb, ' ');
1148 graph_pad_horizontally(graph, sb, graph->num_columns);
1151 * Update graph->prev_state since we have output a padding line
1153 graph->prev_state = GRAPH_PADDING;
1156 int graph_is_commit_finished(struct git_graph const *graph)
1158 return (graph->state == GRAPH_PADDING);
1161 void graph_show_commit(struct git_graph *graph)
1163 struct strbuf msgbuf = STRBUF_INIT;
1164 int shown_commit_line = 0;
1166 if (!graph)
1167 return;
1169 while (!shown_commit_line) {
1170 shown_commit_line = graph_next_line(graph, &msgbuf);
1171 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1172 if (!shown_commit_line)
1173 putchar('\n');
1174 strbuf_setlen(&msgbuf, 0);
1177 strbuf_release(&msgbuf);
1180 void graph_show_oneline(struct git_graph *graph)
1182 struct strbuf msgbuf = STRBUF_INIT;
1184 if (!graph)
1185 return;
1187 graph_next_line(graph, &msgbuf);
1188 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1189 strbuf_release(&msgbuf);
1192 void graph_show_padding(struct git_graph *graph)
1194 struct strbuf msgbuf = STRBUF_INIT;
1196 if (!graph)
1197 return;
1199 graph_padding_line(graph, &msgbuf);
1200 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1201 strbuf_release(&msgbuf);
1204 int graph_show_remainder(struct git_graph *graph)
1206 struct strbuf msgbuf = STRBUF_INIT;
1207 int shown = 0;
1209 if (!graph)
1210 return 0;
1212 if (graph_is_commit_finished(graph))
1213 return 0;
1215 for (;;) {
1216 graph_next_line(graph, &msgbuf);
1217 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1218 strbuf_setlen(&msgbuf, 0);
1219 shown = 1;
1221 if (!graph_is_commit_finished(graph))
1222 putchar('\n');
1223 else
1224 break;
1226 strbuf_release(&msgbuf);
1228 return shown;
1232 static void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb)
1234 char *p;
1236 if (!graph) {
1237 fwrite(sb->buf, sizeof(char), sb->len, stdout);
1238 return;
1242 * Print the strbuf line by line,
1243 * and display the graph info before each line but the first.
1245 p = sb->buf;
1246 while (p) {
1247 size_t len;
1248 char *next_p = strchr(p, '\n');
1249 if (next_p) {
1250 next_p++;
1251 len = next_p - p;
1252 } else {
1253 len = (sb->buf + sb->len) - p;
1255 fwrite(p, sizeof(char), len, stdout);
1256 if (next_p && *next_p != '\0')
1257 graph_show_oneline(graph);
1258 p = next_p;
1262 void graph_show_commit_msg(struct git_graph *graph,
1263 struct strbuf const *sb)
1265 int newline_terminated;
1267 if (!graph) {
1269 * If there's no graph, just print the message buffer.
1271 * The message buffer for CMIT_FMT_ONELINE and
1272 * CMIT_FMT_USERFORMAT are already missing a terminating
1273 * newline. All of the other formats should have it.
1275 fwrite(sb->buf, sizeof(char), sb->len, stdout);
1276 return;
1279 newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
1282 * Show the commit message
1284 graph_show_strbuf(graph, sb);
1287 * If there is more output needed for this commit, show it now
1289 if (!graph_is_commit_finished(graph)) {
1291 * If sb doesn't have a terminating newline, print one now,
1292 * so we can start the remainder of the graph output on a
1293 * new line.
1295 if (!newline_terminated)
1296 putchar('\n');
1298 graph_show_remainder(graph);
1301 * If sb ends with a newline, our output should too.
1303 if (newline_terminated)
1304 putchar('\n');