Merge branch 'ic/maint-rebase-i-abort'
[git/jnareb-git/bp-gitweb.git] / graph.c
blobac7c60540645761d3c99fb405a69cb2d06aaddc2
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.
52 struct column {
54 * The parent commit of this column.
56 struct commit *commit;
58 * The color to (optionally) print this column in. This is an
59 * index into column_colors.
61 unsigned short color;
64 enum graph_state {
65 GRAPH_PADDING,
66 GRAPH_SKIP,
67 GRAPH_PRE_COMMIT,
68 GRAPH_COMMIT,
69 GRAPH_POST_MERGE,
70 GRAPH_COLLAPSING
74 * The list of available column colors.
76 static char column_colors[][COLOR_MAXLEN] = {
77 GIT_COLOR_RED,
78 GIT_COLOR_GREEN,
79 GIT_COLOR_YELLOW,
80 GIT_COLOR_BLUE,
81 GIT_COLOR_MAGENTA,
82 GIT_COLOR_CYAN,
83 GIT_COLOR_BOLD_RED,
84 GIT_COLOR_BOLD_GREEN,
85 GIT_COLOR_BOLD_YELLOW,
86 GIT_COLOR_BOLD_BLUE,
87 GIT_COLOR_BOLD_MAGENTA,
88 GIT_COLOR_BOLD_CYAN,
91 #define COLUMN_COLORS_MAX (ARRAY_SIZE(column_colors))
93 static const char *column_get_color_code(const struct column *c)
95 return column_colors[c->color];
98 static void strbuf_write_column(struct strbuf *sb, const struct column *c,
99 char col_char)
101 if (c->color < COLUMN_COLORS_MAX)
102 strbuf_addstr(sb, column_get_color_code(c));
103 strbuf_addch(sb, col_char);
104 if (c->color < COLUMN_COLORS_MAX)
105 strbuf_addstr(sb, GIT_COLOR_RESET);
108 struct git_graph {
110 * The commit currently being processed
112 struct commit *commit;
113 /* The rev-info used for the current traversal */
114 struct rev_info *revs;
116 * The number of interesting parents that this commit has.
118 * Note that this is not the same as the actual number of parents.
119 * This count excludes parents that won't be printed in the graph
120 * output, as determined by graph_is_interesting().
122 int num_parents;
124 * The width of the graph output for this commit.
125 * All rows for this commit are padded to this width, so that
126 * messages printed after the graph output are aligned.
128 int width;
130 * The next expansion row to print
131 * when state is GRAPH_PRE_COMMIT
133 int expansion_row;
135 * The current output state.
136 * This tells us what kind of line graph_next_line() should output.
138 enum graph_state state;
140 * The output state for the previous line of output.
141 * This is primarily used to determine how the first merge line
142 * should appear, based on the last line of the previous commit.
144 enum graph_state prev_state;
146 * The index of the column that refers to this commit.
148 * If none of the incoming columns refer to this commit,
149 * this will be equal to num_columns.
151 int commit_index;
153 * The commit_index for the previously displayed commit.
155 * This is used to determine how the first line of a merge
156 * graph output should appear, based on the last line of the
157 * previous commit.
159 int prev_commit_index;
161 * The maximum number of columns that can be stored in the columns
162 * and new_columns arrays. This is also half the number of entries
163 * that can be stored in the mapping and new_mapping arrays.
165 int column_capacity;
167 * The number of columns (also called "branch lines" in some places)
169 int num_columns;
171 * The number of columns in the new_columns array
173 int num_new_columns;
175 * The number of entries in the mapping array
177 int mapping_size;
179 * The column state before we output the current commit.
181 struct column *columns;
183 * The new column state after we output the current commit.
184 * Only valid when state is GRAPH_COLLAPSING.
186 struct column *new_columns;
188 * An array that tracks the current state of each
189 * character in the output line during state GRAPH_COLLAPSING.
190 * Each entry is -1 if this character is empty, or a non-negative
191 * integer if the character contains a branch line. The value of
192 * the integer indicates the target position for this branch line.
193 * (I.e., this array maps the current column positions to their
194 * desired positions.)
196 * The maximum capacity of this array is always
197 * sizeof(int) * 2 * column_capacity.
199 int *mapping;
201 * A temporary array for computing the next mapping state
202 * while we are outputting a mapping line. This is stored as part
203 * of the git_graph simply so we don't have to allocate a new
204 * temporary array each time we have to output a collapsing line.
206 int *new_mapping;
208 * The current default column color being used. This is
209 * stored as an index into the array column_colors.
211 unsigned short default_column_color;
214 static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void *data)
216 struct git_graph *graph = data;
217 static struct strbuf msgbuf = STRBUF_INIT;
219 assert(graph);
221 strbuf_reset(&msgbuf);
222 graph_padding_line(graph, &msgbuf);
223 return &msgbuf;
226 struct git_graph *graph_init(struct rev_info *opt)
228 struct git_graph *graph = xmalloc(sizeof(struct git_graph));
229 graph->commit = NULL;
230 graph->revs = opt;
231 graph->num_parents = 0;
232 graph->expansion_row = 0;
233 graph->state = GRAPH_PADDING;
234 graph->prev_state = GRAPH_PADDING;
235 graph->commit_index = 0;
236 graph->prev_commit_index = 0;
237 graph->num_columns = 0;
238 graph->num_new_columns = 0;
239 graph->mapping_size = 0;
241 * Start the column color at the maximum value, since we'll
242 * always increment it for the first commit we output.
243 * This way we start at 0 for the first commit.
245 graph->default_column_color = COLUMN_COLORS_MAX - 1;
248 * Allocate a reasonably large default number of columns
249 * We'll automatically grow columns later if we need more room.
251 graph->column_capacity = 30;
252 graph->columns = xmalloc(sizeof(struct column) *
253 graph->column_capacity);
254 graph->new_columns = xmalloc(sizeof(struct column) *
255 graph->column_capacity);
256 graph->mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
257 graph->new_mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
260 * The diff output prefix callback, with this we can make
261 * all the diff output to align with the graph lines.
263 opt->diffopt.output_prefix = diff_output_prefix_callback;
264 opt->diffopt.output_prefix_data = graph;
266 return graph;
269 static void graph_update_state(struct git_graph *graph, enum graph_state s)
271 graph->prev_state = graph->state;
272 graph->state = s;
275 static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
277 if (graph->column_capacity >= num_columns)
278 return;
280 do {
281 graph->column_capacity *= 2;
282 } while (graph->column_capacity < num_columns);
284 graph->columns = xrealloc(graph->columns,
285 sizeof(struct column) *
286 graph->column_capacity);
287 graph->new_columns = xrealloc(graph->new_columns,
288 sizeof(struct column) *
289 graph->column_capacity);
290 graph->mapping = xrealloc(graph->mapping,
291 sizeof(int) * 2 * graph->column_capacity);
292 graph->new_mapping = xrealloc(graph->new_mapping,
293 sizeof(int) * 2 * graph->column_capacity);
297 * Returns 1 if the commit will be printed in the graph output,
298 * and 0 otherwise.
300 static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
303 * If revs->boundary is set, commits whose children have
304 * been shown are always interesting, even if they have the
305 * UNINTERESTING or TREESAME flags set.
307 if (graph->revs && graph->revs->boundary) {
308 if (commit->object.flags & CHILD_SHOWN)
309 return 1;
313 * Otherwise, use get_commit_action() to see if this commit is
314 * interesting
316 return get_commit_action(graph->revs, commit) == commit_show;
319 static struct commit_list *next_interesting_parent(struct git_graph *graph,
320 struct commit_list *orig)
322 struct commit_list *list;
325 * If revs->first_parent_only is set, only the first
326 * parent is interesting. None of the others are.
328 if (graph->revs->first_parent_only)
329 return NULL;
332 * Return the next interesting commit after orig
334 for (list = orig->next; list; list = list->next) {
335 if (graph_is_interesting(graph, list->item))
336 return list;
339 return NULL;
342 static struct commit_list *first_interesting_parent(struct git_graph *graph)
344 struct commit_list *parents = graph->commit->parents;
347 * If this commit has no parents, ignore it
349 if (!parents)
350 return NULL;
353 * If the first parent is interesting, return it
355 if (graph_is_interesting(graph, parents->item))
356 return parents;
359 * Otherwise, call next_interesting_parent() to get
360 * the next interesting parent
362 return next_interesting_parent(graph, parents);
365 static unsigned short graph_get_current_column_color(const struct git_graph *graph)
367 if (!DIFF_OPT_TST(&graph->revs->diffopt, COLOR_DIFF))
368 return COLUMN_COLORS_MAX;
369 return graph->default_column_color;
373 * Update the graph's default column color.
375 static void graph_increment_column_color(struct git_graph *graph)
377 graph->default_column_color = (graph->default_column_color + 1) %
378 COLUMN_COLORS_MAX;
381 static unsigned short graph_find_commit_color(const struct git_graph *graph,
382 const struct commit *commit)
384 int i;
385 for (i = 0; i < graph->num_columns; i++) {
386 if (graph->columns[i].commit == commit)
387 return graph->columns[i].color;
389 return graph_get_current_column_color(graph);
392 static void graph_insert_into_new_columns(struct git_graph *graph,
393 struct commit *commit,
394 int *mapping_index)
396 int i;
399 * If the commit is already in the new_columns list, we don't need to
400 * add it. Just update the mapping correctly.
402 for (i = 0; i < graph->num_new_columns; i++) {
403 if (graph->new_columns[i].commit == commit) {
404 graph->mapping[*mapping_index] = i;
405 *mapping_index += 2;
406 return;
411 * This commit isn't already in new_columns. Add it.
413 graph->new_columns[graph->num_new_columns].commit = commit;
414 graph->new_columns[graph->num_new_columns].color = graph_find_commit_color(graph, commit);
415 graph->mapping[*mapping_index] = graph->num_new_columns;
416 *mapping_index += 2;
417 graph->num_new_columns++;
420 static void graph_update_width(struct git_graph *graph,
421 int is_commit_in_existing_columns)
424 * Compute the width needed to display the graph for this commit.
425 * This is the maximum width needed for any row. All other rows
426 * will be padded to this width.
428 * Compute the number of columns in the widest row:
429 * Count each existing column (graph->num_columns), and each new
430 * column added by this commit.
432 int max_cols = graph->num_columns + graph->num_parents;
435 * Even if the current commit has no parents to be printed, it
436 * still takes up a column for itself.
438 if (graph->num_parents < 1)
439 max_cols++;
442 * We added a column for the the current commit as part of
443 * graph->num_parents. If the current commit was already in
444 * graph->columns, then we have double counted it.
446 if (is_commit_in_existing_columns)
447 max_cols--;
450 * Each column takes up 2 spaces
452 graph->width = max_cols * 2;
455 static void graph_update_columns(struct git_graph *graph)
457 struct commit_list *parent;
458 struct column *tmp_columns;
459 int max_new_columns;
460 int mapping_idx;
461 int i, seen_this, is_commit_in_columns;
464 * Swap graph->columns with graph->new_columns
465 * graph->columns contains the state for the previous commit,
466 * and new_columns now contains the state for our commit.
468 * We'll re-use the old columns array as storage to compute the new
469 * columns list for the commit after this one.
471 tmp_columns = graph->columns;
472 graph->columns = graph->new_columns;
473 graph->num_columns = graph->num_new_columns;
475 graph->new_columns = tmp_columns;
476 graph->num_new_columns = 0;
479 * Now update new_columns and mapping with the information for the
480 * commit after this one.
482 * First, make sure we have enough room. At most, there will
483 * be graph->num_columns + graph->num_parents columns for the next
484 * commit.
486 max_new_columns = graph->num_columns + graph->num_parents;
487 graph_ensure_capacity(graph, max_new_columns);
490 * Clear out graph->mapping
492 graph->mapping_size = 2 * max_new_columns;
493 for (i = 0; i < graph->mapping_size; i++)
494 graph->mapping[i] = -1;
497 * Populate graph->new_columns and graph->mapping
499 * Some of the parents of this commit may already be in
500 * graph->columns. If so, graph->new_columns should only contain a
501 * single entry for each such commit. graph->mapping should
502 * contain information about where each current branch line is
503 * supposed to end up after the collapsing is performed.
505 seen_this = 0;
506 mapping_idx = 0;
507 is_commit_in_columns = 1;
508 for (i = 0; i <= graph->num_columns; i++) {
509 struct commit *col_commit;
510 if (i == graph->num_columns) {
511 if (seen_this)
512 break;
513 is_commit_in_columns = 0;
514 col_commit = graph->commit;
515 } else {
516 col_commit = graph->columns[i].commit;
519 if (col_commit == graph->commit) {
520 int old_mapping_idx = mapping_idx;
521 seen_this = 1;
522 graph->commit_index = i;
523 for (parent = first_interesting_parent(graph);
524 parent;
525 parent = next_interesting_parent(graph, parent)) {
527 * If this is a merge, or the start of a new
528 * childless column, increment the current
529 * color.
531 if (graph->num_parents > 1 ||
532 !is_commit_in_columns) {
533 graph_increment_column_color(graph);
535 graph_insert_into_new_columns(graph,
536 parent->item,
537 &mapping_idx);
540 * We always need to increment mapping_idx by at
541 * least 2, even if it has no interesting parents.
542 * The current commit always takes up at least 2
543 * spaces.
545 if (mapping_idx == old_mapping_idx)
546 mapping_idx += 2;
547 } else {
548 graph_insert_into_new_columns(graph, col_commit,
549 &mapping_idx);
554 * Shrink mapping_size to be the minimum necessary
556 while (graph->mapping_size > 1 &&
557 graph->mapping[graph->mapping_size - 1] < 0)
558 graph->mapping_size--;
561 * Compute graph->width for this commit
563 graph_update_width(graph, is_commit_in_columns);
566 void graph_update(struct git_graph *graph, struct commit *commit)
568 struct commit_list *parent;
571 * Set the new commit
573 graph->commit = commit;
576 * Count how many interesting parents this commit has
578 graph->num_parents = 0;
579 for (parent = first_interesting_parent(graph);
580 parent;
581 parent = next_interesting_parent(graph, parent))
583 graph->num_parents++;
587 * Store the old commit_index in prev_commit_index.
588 * graph_update_columns() will update graph->commit_index for this
589 * commit.
591 graph->prev_commit_index = graph->commit_index;
594 * Call graph_update_columns() to update
595 * columns, new_columns, and mapping.
597 graph_update_columns(graph);
599 graph->expansion_row = 0;
602 * Update graph->state.
603 * Note that we don't call graph_update_state() here, since
604 * we don't want to update graph->prev_state. No line for
605 * graph->state was ever printed.
607 * If the previous commit didn't get to the GRAPH_PADDING state,
608 * it never finished its output. Goto GRAPH_SKIP, to print out
609 * a line to indicate that portion of the graph is missing.
611 * If there are 3 or more parents, we may need to print extra rows
612 * before the commit, to expand the branch lines around it and make
613 * room for it. We need to do this only if there is a branch row
614 * (or more) to the right of this commit.
616 * If there are less than 3 parents, we can immediately print the
617 * commit line.
619 if (graph->state != GRAPH_PADDING)
620 graph->state = GRAPH_SKIP;
621 else if (graph->num_parents >= 3 &&
622 graph->commit_index < (graph->num_columns - 1))
623 graph->state = GRAPH_PRE_COMMIT;
624 else
625 graph->state = GRAPH_COMMIT;
628 static int graph_is_mapping_correct(struct git_graph *graph)
630 int i;
633 * The mapping is up to date if each entry is at its target,
634 * or is 1 greater than its target.
635 * (If it is 1 greater than the target, '/' will be printed, so it
636 * will look correct on the next row.)
638 for (i = 0; i < graph->mapping_size; i++) {
639 int target = graph->mapping[i];
640 if (target < 0)
641 continue;
642 if (target == (i / 2))
643 continue;
644 return 0;
647 return 1;
650 static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb,
651 int chars_written)
654 * Add additional spaces to the end of the strbuf, so that all
655 * lines for a particular commit have the same width.
657 * This way, fields printed to the right of the graph will remain
658 * aligned for the entire commit.
660 int extra;
661 if (chars_written >= graph->width)
662 return;
664 extra = graph->width - chars_written;
665 strbuf_addf(sb, "%*s", (int) extra, "");
668 static void graph_output_padding_line(struct git_graph *graph,
669 struct strbuf *sb)
671 int i;
674 * We could conceivable be called with a NULL commit
675 * if our caller has a bug, and invokes graph_next_line()
676 * immediately after graph_init(), without first calling
677 * graph_update(). Return without outputting anything in this
678 * case.
680 if (!graph->commit)
681 return;
684 * Output a padding row, that leaves all branch lines unchanged
686 for (i = 0; i < graph->num_new_columns; i++) {
687 strbuf_write_column(sb, &graph->new_columns[i], '|');
688 strbuf_addch(sb, ' ');
691 graph_pad_horizontally(graph, sb, graph->num_new_columns * 2);
694 static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
697 * Output an ellipsis to indicate that a portion
698 * of the graph is missing.
700 strbuf_addstr(sb, "...");
701 graph_pad_horizontally(graph, sb, 3);
703 if (graph->num_parents >= 3 &&
704 graph->commit_index < (graph->num_columns - 1))
705 graph_update_state(graph, GRAPH_PRE_COMMIT);
706 else
707 graph_update_state(graph, GRAPH_COMMIT);
710 static void graph_output_pre_commit_line(struct git_graph *graph,
711 struct strbuf *sb)
713 int num_expansion_rows;
714 int i, seen_this;
715 int chars_written;
718 * This function formats a row that increases the space around a commit
719 * with multiple parents, to make room for it. It should only be
720 * called when there are 3 or more parents.
722 * We need 2 extra rows for every parent over 2.
724 assert(graph->num_parents >= 3);
725 num_expansion_rows = (graph->num_parents - 2) * 2;
728 * graph->expansion_row tracks the current expansion row we are on.
729 * It should be in the range [0, num_expansion_rows - 1]
731 assert(0 <= graph->expansion_row &&
732 graph->expansion_row < num_expansion_rows);
735 * Output the row
737 seen_this = 0;
738 chars_written = 0;
739 for (i = 0; i < graph->num_columns; i++) {
740 struct column *col = &graph->columns[i];
741 if (col->commit == graph->commit) {
742 seen_this = 1;
743 strbuf_write_column(sb, col, '|');
744 strbuf_addf(sb, "%*s", graph->expansion_row, "");
745 chars_written += 1 + graph->expansion_row;
746 } else if (seen_this && (graph->expansion_row == 0)) {
748 * This is the first line of the pre-commit output.
749 * If the previous commit was a merge commit and
750 * ended in the GRAPH_POST_MERGE state, all branch
751 * lines after graph->prev_commit_index were
752 * printed as "\" on the previous line. Continue
753 * to print them as "\" on this line. Otherwise,
754 * print the branch lines as "|".
756 if (graph->prev_state == GRAPH_POST_MERGE &&
757 graph->prev_commit_index < i)
758 strbuf_write_column(sb, col, '\\');
759 else
760 strbuf_write_column(sb, col, '|');
761 chars_written++;
762 } else if (seen_this && (graph->expansion_row > 0)) {
763 strbuf_write_column(sb, col, '\\');
764 chars_written++;
765 } else {
766 strbuf_write_column(sb, col, '|');
767 chars_written++;
769 strbuf_addch(sb, ' ');
770 chars_written++;
773 graph_pad_horizontally(graph, sb, chars_written);
776 * Increment graph->expansion_row,
777 * and move to state GRAPH_COMMIT if necessary
779 graph->expansion_row++;
780 if (graph->expansion_row >= num_expansion_rows)
781 graph_update_state(graph, GRAPH_COMMIT);
784 static void graph_output_commit_char(struct git_graph *graph, struct strbuf *sb)
787 * For boundary commits, print 'o'
788 * (We should only see boundary commits when revs->boundary is set.)
790 if (graph->commit->object.flags & BOUNDARY) {
791 assert(graph->revs->boundary);
792 strbuf_addch(sb, 'o');
793 return;
797 * If revs->left_right is set, print '<' for commits that
798 * come from the left side, and '>' for commits from the right
799 * side.
801 if (graph->revs && graph->revs->left_right) {
802 if (graph->commit->object.flags & SYMMETRIC_LEFT)
803 strbuf_addch(sb, '<');
804 else
805 strbuf_addch(sb, '>');
806 return;
810 * Print '*' in all other cases
812 strbuf_addch(sb, '*');
816 * Draw an octopus merge and return the number of characters written.
818 static int graph_draw_octopus_merge(struct git_graph *graph,
819 struct strbuf *sb)
822 * Here dashless_commits represents the number of parents
823 * which don't need to have dashes (because their edges fit
824 * neatly under the commit).
826 const int dashless_commits = 2;
827 int col_num, i;
828 int num_dashes =
829 ((graph->num_parents - dashless_commits) * 2) - 1;
830 for (i = 0; i < num_dashes; i++) {
831 col_num = (i / 2) + dashless_commits;
832 strbuf_write_column(sb, &graph->new_columns[col_num], '-');
834 col_num = (i / 2) + dashless_commits;
835 strbuf_write_column(sb, &graph->new_columns[col_num], '.');
836 return num_dashes + 1;
839 static void graph_output_commit_line(struct git_graph *graph, struct strbuf *sb)
841 int seen_this = 0;
842 int i, chars_written;
845 * Output the row containing this commit
846 * Iterate up to and including graph->num_columns,
847 * since the current commit may not be in any of the existing
848 * columns. (This happens when the current commit doesn't have any
849 * children that we have already processed.)
851 seen_this = 0;
852 chars_written = 0;
853 for (i = 0; i <= graph->num_columns; i++) {
854 struct column *col = &graph->columns[i];
855 struct commit *col_commit;
856 if (i == graph->num_columns) {
857 if (seen_this)
858 break;
859 col_commit = graph->commit;
860 } else {
861 col_commit = graph->columns[i].commit;
864 if (col_commit == graph->commit) {
865 seen_this = 1;
866 graph_output_commit_char(graph, sb);
867 chars_written++;
869 if (graph->num_parents > 2)
870 chars_written += graph_draw_octopus_merge(graph,
871 sb);
872 } else if (seen_this && (graph->num_parents > 2)) {
873 strbuf_write_column(sb, col, '\\');
874 chars_written++;
875 } else if (seen_this && (graph->num_parents == 2)) {
877 * This is a 2-way merge commit.
878 * There is no GRAPH_PRE_COMMIT stage for 2-way
879 * merges, so this is the first line of output
880 * for this commit. Check to see what the previous
881 * line of output was.
883 * If it was GRAPH_POST_MERGE, the branch line
884 * coming into this commit may have been '\',
885 * and not '|' or '/'. If so, output the branch
886 * line as '\' on this line, instead of '|'. This
887 * makes the output look nicer.
889 if (graph->prev_state == GRAPH_POST_MERGE &&
890 graph->prev_commit_index < i)
891 strbuf_write_column(sb, col, '\\');
892 else
893 strbuf_write_column(sb, col, '|');
894 chars_written++;
895 } else {
896 strbuf_write_column(sb, col, '|');
897 chars_written++;
899 strbuf_addch(sb, ' ');
900 chars_written++;
903 graph_pad_horizontally(graph, sb, chars_written);
906 * Update graph->state
908 if (graph->num_parents > 1)
909 graph_update_state(graph, GRAPH_POST_MERGE);
910 else if (graph_is_mapping_correct(graph))
911 graph_update_state(graph, GRAPH_PADDING);
912 else
913 graph_update_state(graph, GRAPH_COLLAPSING);
916 static struct column *find_new_column_by_commit(struct git_graph *graph,
917 struct commit *commit)
919 int i;
920 for (i = 0; i < graph->num_new_columns; i++) {
921 if (graph->new_columns[i].commit == commit)
922 return &graph->new_columns[i];
924 return NULL;
927 static void graph_output_post_merge_line(struct git_graph *graph, struct strbuf *sb)
929 int seen_this = 0;
930 int i, j, chars_written;
933 * Output the post-merge row
935 chars_written = 0;
936 for (i = 0; i <= graph->num_columns; i++) {
937 struct column *col = &graph->columns[i];
938 struct commit *col_commit;
939 if (i == graph->num_columns) {
940 if (seen_this)
941 break;
942 col_commit = graph->commit;
943 } else {
944 col_commit = col->commit;
947 if (col_commit == graph->commit) {
949 * Since the current commit is a merge find
950 * the columns for the parent commits in
951 * new_columns and use those to format the
952 * edges.
954 struct commit_list *parents = NULL;
955 struct column *par_column;
956 seen_this = 1;
957 parents = first_interesting_parent(graph);
958 assert(parents);
959 par_column = find_new_column_by_commit(graph, parents->item);
960 assert(par_column);
962 strbuf_write_column(sb, par_column, '|');
963 chars_written++;
964 for (j = 0; j < graph->num_parents - 1; j++) {
965 parents = next_interesting_parent(graph, parents);
966 assert(parents);
967 par_column = find_new_column_by_commit(graph, parents->item);
968 assert(par_column);
969 strbuf_write_column(sb, par_column, '\\');
970 strbuf_addch(sb, ' ');
972 chars_written += j * 2;
973 } else if (seen_this) {
974 strbuf_write_column(sb, col, '\\');
975 strbuf_addch(sb, ' ');
976 chars_written += 2;
977 } else {
978 strbuf_write_column(sb, col, '|');
979 strbuf_addch(sb, ' ');
980 chars_written += 2;
984 graph_pad_horizontally(graph, sb, chars_written);
987 * Update graph->state
989 if (graph_is_mapping_correct(graph))
990 graph_update_state(graph, GRAPH_PADDING);
991 else
992 graph_update_state(graph, GRAPH_COLLAPSING);
995 static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
997 int i;
998 int *tmp_mapping;
999 short used_horizontal = 0;
1000 int horizontal_edge = -1;
1001 int horizontal_edge_target = -1;
1004 * Clear out the new_mapping array
1006 for (i = 0; i < graph->mapping_size; i++)
1007 graph->new_mapping[i] = -1;
1009 for (i = 0; i < graph->mapping_size; i++) {
1010 int target = graph->mapping[i];
1011 if (target < 0)
1012 continue;
1015 * Since update_columns() always inserts the leftmost
1016 * column first, each branch's target location should
1017 * always be either its current location or to the left of
1018 * its current location.
1020 * We never have to move branches to the right. This makes
1021 * the graph much more legible, since whenever branches
1022 * cross, only one is moving directions.
1024 assert(target * 2 <= i);
1026 if (target * 2 == i) {
1028 * This column is already in the
1029 * correct place
1031 assert(graph->new_mapping[i] == -1);
1032 graph->new_mapping[i] = target;
1033 } else if (graph->new_mapping[i - 1] < 0) {
1035 * Nothing is to the left.
1036 * Move to the left by one
1038 graph->new_mapping[i - 1] = target;
1040 * If there isn't already an edge moving horizontally
1041 * select this one.
1043 if (horizontal_edge == -1) {
1044 int j;
1045 horizontal_edge = i;
1046 horizontal_edge_target = target;
1048 * The variable target is the index of the graph
1049 * column, and therefore target*2+3 is the
1050 * actual screen column of the first horizontal
1051 * line.
1053 for (j = (target * 2)+3; j < (i - 2); j += 2)
1054 graph->new_mapping[j] = target;
1056 } else if (graph->new_mapping[i - 1] == target) {
1058 * There is a branch line to our left
1059 * already, and it is our target. We
1060 * combine with this line, since we share
1061 * the same parent commit.
1063 * We don't have to add anything to the
1064 * output or new_mapping, since the
1065 * existing branch line has already taken
1066 * care of it.
1068 } else {
1070 * There is a branch line to our left,
1071 * but it isn't our target. We need to
1072 * cross over it.
1074 * The space just to the left of this
1075 * branch should always be empty.
1077 * The branch to the left of that space
1078 * should be our eventual target.
1080 assert(graph->new_mapping[i - 1] > target);
1081 assert(graph->new_mapping[i - 2] < 0);
1082 assert(graph->new_mapping[i - 3] == target);
1083 graph->new_mapping[i - 2] = target;
1085 * Mark this branch as the horizontal edge to
1086 * prevent any other edges from moving
1087 * horizontally.
1089 if (horizontal_edge == -1)
1090 horizontal_edge = i;
1095 * The new mapping may be 1 smaller than the old mapping
1097 if (graph->new_mapping[graph->mapping_size - 1] < 0)
1098 graph->mapping_size--;
1101 * Output out a line based on the new mapping info
1103 for (i = 0; i < graph->mapping_size; i++) {
1104 int target = graph->new_mapping[i];
1105 if (target < 0)
1106 strbuf_addch(sb, ' ');
1107 else if (target * 2 == i)
1108 strbuf_write_column(sb, &graph->new_columns[target], '|');
1109 else if (target == horizontal_edge_target &&
1110 i != horizontal_edge - 1) {
1112 * Set the mappings for all but the
1113 * first segment to -1 so that they
1114 * won't continue into the next line.
1116 if (i != (target * 2)+3)
1117 graph->new_mapping[i] = -1;
1118 used_horizontal = 1;
1119 strbuf_write_column(sb, &graph->new_columns[target], '_');
1120 } else {
1121 if (used_horizontal && i < horizontal_edge)
1122 graph->new_mapping[i] = -1;
1123 strbuf_write_column(sb, &graph->new_columns[target], '/');
1128 graph_pad_horizontally(graph, sb, graph->mapping_size);
1131 * Swap mapping and new_mapping
1133 tmp_mapping = graph->mapping;
1134 graph->mapping = graph->new_mapping;
1135 graph->new_mapping = tmp_mapping;
1138 * If graph->mapping indicates that all of the branch lines
1139 * are already in the correct positions, we are done.
1140 * Otherwise, we need to collapse some branch lines together.
1142 if (graph_is_mapping_correct(graph))
1143 graph_update_state(graph, GRAPH_PADDING);
1146 static int graph_next_line(struct git_graph *graph, struct strbuf *sb)
1148 switch (graph->state) {
1149 case GRAPH_PADDING:
1150 graph_output_padding_line(graph, sb);
1151 return 0;
1152 case GRAPH_SKIP:
1153 graph_output_skip_line(graph, sb);
1154 return 0;
1155 case GRAPH_PRE_COMMIT:
1156 graph_output_pre_commit_line(graph, sb);
1157 return 0;
1158 case GRAPH_COMMIT:
1159 graph_output_commit_line(graph, sb);
1160 return 1;
1161 case GRAPH_POST_MERGE:
1162 graph_output_post_merge_line(graph, sb);
1163 return 0;
1164 case GRAPH_COLLAPSING:
1165 graph_output_collapsing_line(graph, sb);
1166 return 0;
1169 assert(0);
1170 return 0;
1173 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
1175 int i, j;
1177 if (graph->state != GRAPH_COMMIT) {
1178 graph_next_line(graph, sb);
1179 return;
1183 * Output the row containing this commit
1184 * Iterate up to and including graph->num_columns,
1185 * since the current commit may not be in any of the existing
1186 * columns. (This happens when the current commit doesn't have any
1187 * children that we have already processed.)
1189 for (i = 0; i < graph->num_columns; i++) {
1190 struct column *col = &graph->columns[i];
1191 struct commit *col_commit = col->commit;
1192 if (col_commit == graph->commit) {
1193 strbuf_write_column(sb, col, '|');
1195 if (graph->num_parents < 3)
1196 strbuf_addch(sb, ' ');
1197 else {
1198 int num_spaces = ((graph->num_parents - 2) * 2);
1199 for (j = 0; j < num_spaces; j++)
1200 strbuf_addch(sb, ' ');
1202 } else {
1203 strbuf_write_column(sb, col, '|');
1204 strbuf_addch(sb, ' ');
1208 graph_pad_horizontally(graph, sb, graph->num_columns);
1211 * Update graph->prev_state since we have output a padding line
1213 graph->prev_state = GRAPH_PADDING;
1216 int graph_is_commit_finished(struct git_graph const *graph)
1218 return (graph->state == GRAPH_PADDING);
1221 void graph_show_commit(struct git_graph *graph)
1223 struct strbuf msgbuf = STRBUF_INIT;
1224 int shown_commit_line = 0;
1226 if (!graph)
1227 return;
1229 while (!shown_commit_line) {
1230 shown_commit_line = graph_next_line(graph, &msgbuf);
1231 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1232 if (!shown_commit_line)
1233 putchar('\n');
1234 strbuf_setlen(&msgbuf, 0);
1237 strbuf_release(&msgbuf);
1240 void graph_show_oneline(struct git_graph *graph)
1242 struct strbuf msgbuf = STRBUF_INIT;
1244 if (!graph)
1245 return;
1247 graph_next_line(graph, &msgbuf);
1248 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1249 strbuf_release(&msgbuf);
1252 void graph_show_padding(struct git_graph *graph)
1254 struct strbuf msgbuf = STRBUF_INIT;
1256 if (!graph)
1257 return;
1259 graph_padding_line(graph, &msgbuf);
1260 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1261 strbuf_release(&msgbuf);
1264 int graph_show_remainder(struct git_graph *graph)
1266 struct strbuf msgbuf = STRBUF_INIT;
1267 int shown = 0;
1269 if (!graph)
1270 return 0;
1272 if (graph_is_commit_finished(graph))
1273 return 0;
1275 for (;;) {
1276 graph_next_line(graph, &msgbuf);
1277 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1278 strbuf_setlen(&msgbuf, 0);
1279 shown = 1;
1281 if (!graph_is_commit_finished(graph))
1282 putchar('\n');
1283 else
1284 break;
1286 strbuf_release(&msgbuf);
1288 return shown;
1292 static void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb)
1294 char *p;
1296 if (!graph) {
1297 fwrite(sb->buf, sizeof(char), sb->len, stdout);
1298 return;
1302 * Print the strbuf line by line,
1303 * and display the graph info before each line but the first.
1305 p = sb->buf;
1306 while (p) {
1307 size_t len;
1308 char *next_p = strchr(p, '\n');
1309 if (next_p) {
1310 next_p++;
1311 len = next_p - p;
1312 } else {
1313 len = (sb->buf + sb->len) - p;
1315 fwrite(p, sizeof(char), len, stdout);
1316 if (next_p && *next_p != '\0')
1317 graph_show_oneline(graph);
1318 p = next_p;
1322 void graph_show_commit_msg(struct git_graph *graph,
1323 struct strbuf const *sb)
1325 int newline_terminated;
1327 if (!graph) {
1329 * If there's no graph, just print the message buffer.
1331 * The message buffer for CMIT_FMT_ONELINE and
1332 * CMIT_FMT_USERFORMAT are already missing a terminating
1333 * newline. All of the other formats should have it.
1335 fwrite(sb->buf, sizeof(char), sb->len, stdout);
1336 return;
1339 newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
1342 * Show the commit message
1344 graph_show_strbuf(graph, sb);
1347 * If there is more output needed for this commit, show it now
1349 if (!graph_is_commit_finished(graph)) {
1351 * If sb doesn't have a terminating newline, print one now,
1352 * so we can start the remainder of the graph output on a
1353 * new line.
1355 if (!newline_terminated)
1356 putchar('\n');
1358 graph_show_remainder(graph);
1361 * If sb ends with a newline, our output should too.
1363 if (newline_terminated)
1364 putchar('\n');