Git 1.7.1
[git/kirr.git] / graph.c
blobe6bbcaa8c4655add3ecaca578e948355795e36ca
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 struct git_graph *graph_init(struct rev_info *opt)
216 struct git_graph *graph = xmalloc(sizeof(struct git_graph));
217 graph->commit = NULL;
218 graph->revs = opt;
219 graph->num_parents = 0;
220 graph->expansion_row = 0;
221 graph->state = GRAPH_PADDING;
222 graph->prev_state = GRAPH_PADDING;
223 graph->commit_index = 0;
224 graph->prev_commit_index = 0;
225 graph->num_columns = 0;
226 graph->num_new_columns = 0;
227 graph->mapping_size = 0;
229 * Start the column color at the maximum value, since we'll
230 * always increment it for the first commit we output.
231 * This way we start at 0 for the first commit.
233 graph->default_column_color = COLUMN_COLORS_MAX - 1;
236 * Allocate a reasonably large default number of columns
237 * We'll automatically grow columns later if we need more room.
239 graph->column_capacity = 30;
240 graph->columns = xmalloc(sizeof(struct column) *
241 graph->column_capacity);
242 graph->new_columns = xmalloc(sizeof(struct column) *
243 graph->column_capacity);
244 graph->mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
245 graph->new_mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
247 return graph;
250 static void graph_update_state(struct git_graph *graph, enum graph_state s)
252 graph->prev_state = graph->state;
253 graph->state = s;
256 static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
258 if (graph->column_capacity >= num_columns)
259 return;
261 do {
262 graph->column_capacity *= 2;
263 } while (graph->column_capacity < num_columns);
265 graph->columns = xrealloc(graph->columns,
266 sizeof(struct column) *
267 graph->column_capacity);
268 graph->new_columns = xrealloc(graph->new_columns,
269 sizeof(struct column) *
270 graph->column_capacity);
271 graph->mapping = xrealloc(graph->mapping,
272 sizeof(int) * 2 * graph->column_capacity);
273 graph->new_mapping = xrealloc(graph->new_mapping,
274 sizeof(int) * 2 * graph->column_capacity);
278 * Returns 1 if the commit will be printed in the graph output,
279 * and 0 otherwise.
281 static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
284 * If revs->boundary is set, commits whose children have
285 * been shown are always interesting, even if they have the
286 * UNINTERESTING or TREESAME flags set.
288 if (graph->revs && graph->revs->boundary) {
289 if (commit->object.flags & CHILD_SHOWN)
290 return 1;
294 * Otherwise, use get_commit_action() to see if this commit is
295 * interesting
297 return get_commit_action(graph->revs, commit) == commit_show;
300 static struct commit_list *next_interesting_parent(struct git_graph *graph,
301 struct commit_list *orig)
303 struct commit_list *list;
306 * If revs->first_parent_only is set, only the first
307 * parent is interesting. None of the others are.
309 if (graph->revs->first_parent_only)
310 return NULL;
313 * Return the next interesting commit after orig
315 for (list = orig->next; list; list = list->next) {
316 if (graph_is_interesting(graph, list->item))
317 return list;
320 return NULL;
323 static struct commit_list *first_interesting_parent(struct git_graph *graph)
325 struct commit_list *parents = graph->commit->parents;
328 * If this commit has no parents, ignore it
330 if (!parents)
331 return NULL;
334 * If the first parent is interesting, return it
336 if (graph_is_interesting(graph, parents->item))
337 return parents;
340 * Otherwise, call next_interesting_parent() to get
341 * the next interesting parent
343 return next_interesting_parent(graph, parents);
346 static unsigned short graph_get_current_column_color(const struct git_graph *graph)
348 if (!DIFF_OPT_TST(&graph->revs->diffopt, COLOR_DIFF))
349 return COLUMN_COLORS_MAX;
350 return graph->default_column_color;
354 * Update the graph's default column color.
356 static void graph_increment_column_color(struct git_graph *graph)
358 graph->default_column_color = (graph->default_column_color + 1) %
359 COLUMN_COLORS_MAX;
362 static unsigned short graph_find_commit_color(const struct git_graph *graph,
363 const struct commit *commit)
365 int i;
366 for (i = 0; i < graph->num_columns; i++) {
367 if (graph->columns[i].commit == commit)
368 return graph->columns[i].color;
370 return graph_get_current_column_color(graph);
373 static void graph_insert_into_new_columns(struct git_graph *graph,
374 struct commit *commit,
375 int *mapping_index)
377 int i;
380 * If the commit is already in the new_columns list, we don't need to
381 * add it. Just update the mapping correctly.
383 for (i = 0; i < graph->num_new_columns; i++) {
384 if (graph->new_columns[i].commit == commit) {
385 graph->mapping[*mapping_index] = i;
386 *mapping_index += 2;
387 return;
392 * This commit isn't already in new_columns. Add it.
394 graph->new_columns[graph->num_new_columns].commit = commit;
395 graph->new_columns[graph->num_new_columns].color = graph_find_commit_color(graph, commit);
396 graph->mapping[*mapping_index] = graph->num_new_columns;
397 *mapping_index += 2;
398 graph->num_new_columns++;
401 static void graph_update_width(struct git_graph *graph,
402 int is_commit_in_existing_columns)
405 * Compute the width needed to display the graph for this commit.
406 * This is the maximum width needed for any row. All other rows
407 * will be padded to this width.
409 * Compute the number of columns in the widest row:
410 * Count each existing column (graph->num_columns), and each new
411 * column added by this commit.
413 int max_cols = graph->num_columns + graph->num_parents;
416 * Even if the current commit has no parents to be printed, it
417 * still takes up a column for itself.
419 if (graph->num_parents < 1)
420 max_cols++;
423 * We added a column for the the current commit as part of
424 * graph->num_parents. If the current commit was already in
425 * graph->columns, then we have double counted it.
427 if (is_commit_in_existing_columns)
428 max_cols--;
431 * Each column takes up 2 spaces
433 graph->width = max_cols * 2;
436 static void graph_update_columns(struct git_graph *graph)
438 struct commit_list *parent;
439 struct column *tmp_columns;
440 int max_new_columns;
441 int mapping_idx;
442 int i, seen_this, is_commit_in_columns;
445 * Swap graph->columns with graph->new_columns
446 * graph->columns contains the state for the previous commit,
447 * and new_columns now contains the state for our commit.
449 * We'll re-use the old columns array as storage to compute the new
450 * columns list for the commit after this one.
452 tmp_columns = graph->columns;
453 graph->columns = graph->new_columns;
454 graph->num_columns = graph->num_new_columns;
456 graph->new_columns = tmp_columns;
457 graph->num_new_columns = 0;
460 * Now update new_columns and mapping with the information for the
461 * commit after this one.
463 * First, make sure we have enough room. At most, there will
464 * be graph->num_columns + graph->num_parents columns for the next
465 * commit.
467 max_new_columns = graph->num_columns + graph->num_parents;
468 graph_ensure_capacity(graph, max_new_columns);
471 * Clear out graph->mapping
473 graph->mapping_size = 2 * max_new_columns;
474 for (i = 0; i < graph->mapping_size; i++)
475 graph->mapping[i] = -1;
478 * Populate graph->new_columns and graph->mapping
480 * Some of the parents of this commit may already be in
481 * graph->columns. If so, graph->new_columns should only contain a
482 * single entry for each such commit. graph->mapping should
483 * contain information about where each current branch line is
484 * supposed to end up after the collapsing is performed.
486 seen_this = 0;
487 mapping_idx = 0;
488 is_commit_in_columns = 1;
489 for (i = 0; i <= graph->num_columns; i++) {
490 struct commit *col_commit;
491 if (i == graph->num_columns) {
492 if (seen_this)
493 break;
494 is_commit_in_columns = 0;
495 col_commit = graph->commit;
496 } else {
497 col_commit = graph->columns[i].commit;
500 if (col_commit == graph->commit) {
501 int old_mapping_idx = mapping_idx;
502 seen_this = 1;
503 graph->commit_index = i;
504 for (parent = first_interesting_parent(graph);
505 parent;
506 parent = next_interesting_parent(graph, parent)) {
508 * If this is a merge, or the start of a new
509 * childless column, increment the current
510 * color.
512 if (graph->num_parents > 1 ||
513 !is_commit_in_columns) {
514 graph_increment_column_color(graph);
516 graph_insert_into_new_columns(graph,
517 parent->item,
518 &mapping_idx);
521 * We always need to increment mapping_idx by at
522 * least 2, even if it has no interesting parents.
523 * The current commit always takes up at least 2
524 * spaces.
526 if (mapping_idx == old_mapping_idx)
527 mapping_idx += 2;
528 } else {
529 graph_insert_into_new_columns(graph, col_commit,
530 &mapping_idx);
535 * Shrink mapping_size to be the minimum necessary
537 while (graph->mapping_size > 1 &&
538 graph->mapping[graph->mapping_size - 1] < 0)
539 graph->mapping_size--;
542 * Compute graph->width for this commit
544 graph_update_width(graph, is_commit_in_columns);
547 void graph_update(struct git_graph *graph, struct commit *commit)
549 struct commit_list *parent;
552 * Set the new commit
554 graph->commit = commit;
557 * Count how many interesting parents this commit has
559 graph->num_parents = 0;
560 for (parent = first_interesting_parent(graph);
561 parent;
562 parent = next_interesting_parent(graph, parent))
564 graph->num_parents++;
568 * Store the old commit_index in prev_commit_index.
569 * graph_update_columns() will update graph->commit_index for this
570 * commit.
572 graph->prev_commit_index = graph->commit_index;
575 * Call graph_update_columns() to update
576 * columns, new_columns, and mapping.
578 graph_update_columns(graph);
580 graph->expansion_row = 0;
583 * Update graph->state.
584 * Note that we don't call graph_update_state() here, since
585 * we don't want to update graph->prev_state. No line for
586 * graph->state was ever printed.
588 * If the previous commit didn't get to the GRAPH_PADDING state,
589 * it never finished its output. Goto GRAPH_SKIP, to print out
590 * a line to indicate that portion of the graph is missing.
592 * If there are 3 or more parents, we may need to print extra rows
593 * before the commit, to expand the branch lines around it and make
594 * room for it. We need to do this only if there is a branch row
595 * (or more) to the right of this commit.
597 * If there are less than 3 parents, we can immediately print the
598 * commit line.
600 if (graph->state != GRAPH_PADDING)
601 graph->state = GRAPH_SKIP;
602 else if (graph->num_parents >= 3 &&
603 graph->commit_index < (graph->num_columns - 1))
604 graph->state = GRAPH_PRE_COMMIT;
605 else
606 graph->state = GRAPH_COMMIT;
609 static int graph_is_mapping_correct(struct git_graph *graph)
611 int i;
614 * The mapping is up to date if each entry is at its target,
615 * or is 1 greater than its target.
616 * (If it is 1 greater than the target, '/' will be printed, so it
617 * will look correct on the next row.)
619 for (i = 0; i < graph->mapping_size; i++) {
620 int target = graph->mapping[i];
621 if (target < 0)
622 continue;
623 if (target == (i / 2))
624 continue;
625 return 0;
628 return 1;
631 static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb,
632 int chars_written)
635 * Add additional spaces to the end of the strbuf, so that all
636 * lines for a particular commit have the same width.
638 * This way, fields printed to the right of the graph will remain
639 * aligned for the entire commit.
641 int extra;
642 if (chars_written >= graph->width)
643 return;
645 extra = graph->width - chars_written;
646 strbuf_addf(sb, "%*s", (int) extra, "");
649 static void graph_output_padding_line(struct git_graph *graph,
650 struct strbuf *sb)
652 int i;
655 * We could conceivable be called with a NULL commit
656 * if our caller has a bug, and invokes graph_next_line()
657 * immediately after graph_init(), without first calling
658 * graph_update(). Return without outputting anything in this
659 * case.
661 if (!graph->commit)
662 return;
665 * Output a padding row, that leaves all branch lines unchanged
667 for (i = 0; i < graph->num_new_columns; i++) {
668 strbuf_write_column(sb, &graph->new_columns[i], '|');
669 strbuf_addch(sb, ' ');
672 graph_pad_horizontally(graph, sb, graph->num_new_columns * 2);
675 static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
678 * Output an ellipsis to indicate that a portion
679 * of the graph is missing.
681 strbuf_addstr(sb, "...");
682 graph_pad_horizontally(graph, sb, 3);
684 if (graph->num_parents >= 3 &&
685 graph->commit_index < (graph->num_columns - 1))
686 graph_update_state(graph, GRAPH_PRE_COMMIT);
687 else
688 graph_update_state(graph, GRAPH_COMMIT);
691 static void graph_output_pre_commit_line(struct git_graph *graph,
692 struct strbuf *sb)
694 int num_expansion_rows;
695 int i, seen_this;
696 int chars_written;
699 * This function formats a row that increases the space around a commit
700 * with multiple parents, to make room for it. It should only be
701 * called when there are 3 or more parents.
703 * We need 2 extra rows for every parent over 2.
705 assert(graph->num_parents >= 3);
706 num_expansion_rows = (graph->num_parents - 2) * 2;
709 * graph->expansion_row tracks the current expansion row we are on.
710 * It should be in the range [0, num_expansion_rows - 1]
712 assert(0 <= graph->expansion_row &&
713 graph->expansion_row < num_expansion_rows);
716 * Output the row
718 seen_this = 0;
719 chars_written = 0;
720 for (i = 0; i < graph->num_columns; i++) {
721 struct column *col = &graph->columns[i];
722 if (col->commit == graph->commit) {
723 seen_this = 1;
724 strbuf_write_column(sb, col, '|');
725 strbuf_addf(sb, "%*s", graph->expansion_row, "");
726 chars_written += 1 + graph->expansion_row;
727 } else if (seen_this && (graph->expansion_row == 0)) {
729 * This is the first line of the pre-commit output.
730 * If the previous commit was a merge commit and
731 * ended in the GRAPH_POST_MERGE state, all branch
732 * lines after graph->prev_commit_index were
733 * printed as "\" on the previous line. Continue
734 * to print them as "\" on this line. Otherwise,
735 * print the branch lines as "|".
737 if (graph->prev_state == GRAPH_POST_MERGE &&
738 graph->prev_commit_index < i)
739 strbuf_write_column(sb, col, '\\');
740 else
741 strbuf_write_column(sb, col, '|');
742 chars_written++;
743 } else if (seen_this && (graph->expansion_row > 0)) {
744 strbuf_write_column(sb, col, '\\');
745 chars_written++;
746 } else {
747 strbuf_write_column(sb, col, '|');
748 chars_written++;
750 strbuf_addch(sb, ' ');
751 chars_written++;
754 graph_pad_horizontally(graph, sb, chars_written);
757 * Increment graph->expansion_row,
758 * and move to state GRAPH_COMMIT if necessary
760 graph->expansion_row++;
761 if (graph->expansion_row >= num_expansion_rows)
762 graph_update_state(graph, GRAPH_COMMIT);
765 static void graph_output_commit_char(struct git_graph *graph, struct strbuf *sb)
768 * For boundary commits, print 'o'
769 * (We should only see boundary commits when revs->boundary is set.)
771 if (graph->commit->object.flags & BOUNDARY) {
772 assert(graph->revs->boundary);
773 strbuf_addch(sb, 'o');
774 return;
778 * If revs->left_right is set, print '<' for commits that
779 * come from the left side, and '>' for commits from the right
780 * side.
782 if (graph->revs && graph->revs->left_right) {
783 if (graph->commit->object.flags & SYMMETRIC_LEFT)
784 strbuf_addch(sb, '<');
785 else
786 strbuf_addch(sb, '>');
787 return;
791 * Print '*' in all other cases
793 strbuf_addch(sb, '*');
797 * Draw an octopus merge and return the number of characters written.
799 static int graph_draw_octopus_merge(struct git_graph *graph,
800 struct strbuf *sb)
803 * Here dashless_commits represents the number of parents
804 * which don't need to have dashes (because their edges fit
805 * neatly under the commit).
807 const int dashless_commits = 2;
808 int col_num, i;
809 int num_dashes =
810 ((graph->num_parents - dashless_commits) * 2) - 1;
811 for (i = 0; i < num_dashes; i++) {
812 col_num = (i / 2) + dashless_commits;
813 strbuf_write_column(sb, &graph->new_columns[col_num], '-');
815 col_num = (i / 2) + dashless_commits;
816 strbuf_write_column(sb, &graph->new_columns[col_num], '.');
817 return num_dashes + 1;
820 static void graph_output_commit_line(struct git_graph *graph, struct strbuf *sb)
822 int seen_this = 0;
823 int i, chars_written;
826 * Output the row containing this commit
827 * Iterate up to and including graph->num_columns,
828 * since the current commit may not be in any of the existing
829 * columns. (This happens when the current commit doesn't have any
830 * children that we have already processed.)
832 seen_this = 0;
833 chars_written = 0;
834 for (i = 0; i <= graph->num_columns; i++) {
835 struct column *col = &graph->columns[i];
836 struct commit *col_commit;
837 if (i == graph->num_columns) {
838 if (seen_this)
839 break;
840 col_commit = graph->commit;
841 } else {
842 col_commit = graph->columns[i].commit;
845 if (col_commit == graph->commit) {
846 seen_this = 1;
847 graph_output_commit_char(graph, sb);
848 chars_written++;
850 if (graph->num_parents > 2)
851 chars_written += graph_draw_octopus_merge(graph,
852 sb);
853 } else if (seen_this && (graph->num_parents > 2)) {
854 strbuf_write_column(sb, col, '\\');
855 chars_written++;
856 } else if (seen_this && (graph->num_parents == 2)) {
858 * This is a 2-way merge commit.
859 * There is no GRAPH_PRE_COMMIT stage for 2-way
860 * merges, so this is the first line of output
861 * for this commit. Check to see what the previous
862 * line of output was.
864 * If it was GRAPH_POST_MERGE, the branch line
865 * coming into this commit may have been '\',
866 * and not '|' or '/'. If so, output the branch
867 * line as '\' on this line, instead of '|'. This
868 * makes the output look nicer.
870 if (graph->prev_state == GRAPH_POST_MERGE &&
871 graph->prev_commit_index < i)
872 strbuf_write_column(sb, col, '\\');
873 else
874 strbuf_write_column(sb, col, '|');
875 chars_written++;
876 } else {
877 strbuf_write_column(sb, col, '|');
878 chars_written++;
880 strbuf_addch(sb, ' ');
881 chars_written++;
884 graph_pad_horizontally(graph, sb, chars_written);
887 * Update graph->state
889 if (graph->num_parents > 1)
890 graph_update_state(graph, GRAPH_POST_MERGE);
891 else if (graph_is_mapping_correct(graph))
892 graph_update_state(graph, GRAPH_PADDING);
893 else
894 graph_update_state(graph, GRAPH_COLLAPSING);
897 static struct column *find_new_column_by_commit(struct git_graph *graph,
898 struct commit *commit)
900 int i;
901 for (i = 0; i < graph->num_new_columns; i++) {
902 if (graph->new_columns[i].commit == commit)
903 return &graph->new_columns[i];
905 return NULL;
908 static void graph_output_post_merge_line(struct git_graph *graph, struct strbuf *sb)
910 int seen_this = 0;
911 int i, j, chars_written;
914 * Output the post-merge row
916 chars_written = 0;
917 for (i = 0; i <= graph->num_columns; i++) {
918 struct column *col = &graph->columns[i];
919 struct commit *col_commit;
920 if (i == graph->num_columns) {
921 if (seen_this)
922 break;
923 col_commit = graph->commit;
924 } else {
925 col_commit = col->commit;
928 if (col_commit == graph->commit) {
930 * Since the current commit is a merge find
931 * the columns for the parent commits in
932 * new_columns and use those to format the
933 * edges.
935 struct commit_list *parents = NULL;
936 struct column *par_column;
937 seen_this = 1;
938 parents = first_interesting_parent(graph);
939 assert(parents);
940 par_column = find_new_column_by_commit(graph, parents->item);
941 assert(par_column);
943 strbuf_write_column(sb, par_column, '|');
944 chars_written++;
945 for (j = 0; j < graph->num_parents - 1; j++) {
946 parents = next_interesting_parent(graph, parents);
947 assert(parents);
948 par_column = find_new_column_by_commit(graph, parents->item);
949 assert(par_column);
950 strbuf_write_column(sb, par_column, '\\');
951 strbuf_addch(sb, ' ');
953 chars_written += j * 2;
954 } else if (seen_this) {
955 strbuf_write_column(sb, col, '\\');
956 strbuf_addch(sb, ' ');
957 chars_written += 2;
958 } else {
959 strbuf_write_column(sb, col, '|');
960 strbuf_addch(sb, ' ');
961 chars_written += 2;
965 graph_pad_horizontally(graph, sb, chars_written);
968 * Update graph->state
970 if (graph_is_mapping_correct(graph))
971 graph_update_state(graph, GRAPH_PADDING);
972 else
973 graph_update_state(graph, GRAPH_COLLAPSING);
976 static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
978 int i;
979 int *tmp_mapping;
980 short used_horizontal = 0;
981 int horizontal_edge = -1;
982 int horizontal_edge_target = -1;
985 * Clear out the new_mapping array
987 for (i = 0; i < graph->mapping_size; i++)
988 graph->new_mapping[i] = -1;
990 for (i = 0; i < graph->mapping_size; i++) {
991 int target = graph->mapping[i];
992 if (target < 0)
993 continue;
996 * Since update_columns() always inserts the leftmost
997 * column first, each branch's target location should
998 * always be either its current location or to the left of
999 * its current location.
1001 * We never have to move branches to the right. This makes
1002 * the graph much more legible, since whenever branches
1003 * cross, only one is moving directions.
1005 assert(target * 2 <= i);
1007 if (target * 2 == i) {
1009 * This column is already in the
1010 * correct place
1012 assert(graph->new_mapping[i] == -1);
1013 graph->new_mapping[i] = target;
1014 } else if (graph->new_mapping[i - 1] < 0) {
1016 * Nothing is to the left.
1017 * Move to the left by one
1019 graph->new_mapping[i - 1] = target;
1021 * If there isn't already an edge moving horizontally
1022 * select this one.
1024 if (horizontal_edge == -1) {
1025 int j;
1026 horizontal_edge = i;
1027 horizontal_edge_target = target;
1029 * The variable target is the index of the graph
1030 * column, and therefore target*2+3 is the
1031 * actual screen column of the first horizontal
1032 * line.
1034 for (j = (target * 2)+3; j < (i - 2); j += 2)
1035 graph->new_mapping[j] = target;
1037 } else if (graph->new_mapping[i - 1] == target) {
1039 * There is a branch line to our left
1040 * already, and it is our target. We
1041 * combine with this line, since we share
1042 * the same parent commit.
1044 * We don't have to add anything to the
1045 * output or new_mapping, since the
1046 * existing branch line has already taken
1047 * care of it.
1049 } else {
1051 * There is a branch line to our left,
1052 * but it isn't our target. We need to
1053 * cross over it.
1055 * The space just to the left of this
1056 * branch should always be empty.
1058 * The branch to the left of that space
1059 * should be our eventual target.
1061 assert(graph->new_mapping[i - 1] > target);
1062 assert(graph->new_mapping[i - 2] < 0);
1063 assert(graph->new_mapping[i - 3] == target);
1064 graph->new_mapping[i - 2] = target;
1066 * Mark this branch as the horizontal edge to
1067 * prevent any other edges from moving
1068 * horizontally.
1070 if (horizontal_edge == -1)
1071 horizontal_edge = i;
1076 * The new mapping may be 1 smaller than the old mapping
1078 if (graph->new_mapping[graph->mapping_size - 1] < 0)
1079 graph->mapping_size--;
1082 * Output out a line based on the new mapping info
1084 for (i = 0; i < graph->mapping_size; i++) {
1085 int target = graph->new_mapping[i];
1086 if (target < 0)
1087 strbuf_addch(sb, ' ');
1088 else if (target * 2 == i)
1089 strbuf_write_column(sb, &graph->new_columns[target], '|');
1090 else if (target == horizontal_edge_target &&
1091 i != horizontal_edge - 1) {
1093 * Set the mappings for all but the
1094 * first segment to -1 so that they
1095 * won't continue into the next line.
1097 if (i != (target * 2)+3)
1098 graph->new_mapping[i] = -1;
1099 used_horizontal = 1;
1100 strbuf_write_column(sb, &graph->new_columns[target], '_');
1101 } else {
1102 if (used_horizontal && i < horizontal_edge)
1103 graph->new_mapping[i] = -1;
1104 strbuf_write_column(sb, &graph->new_columns[target], '/');
1109 graph_pad_horizontally(graph, sb, graph->mapping_size);
1112 * Swap mapping and new_mapping
1114 tmp_mapping = graph->mapping;
1115 graph->mapping = graph->new_mapping;
1116 graph->new_mapping = tmp_mapping;
1119 * If graph->mapping indicates that all of the branch lines
1120 * are already in the correct positions, we are done.
1121 * Otherwise, we need to collapse some branch lines together.
1123 if (graph_is_mapping_correct(graph))
1124 graph_update_state(graph, GRAPH_PADDING);
1127 static int graph_next_line(struct git_graph *graph, struct strbuf *sb)
1129 switch (graph->state) {
1130 case GRAPH_PADDING:
1131 graph_output_padding_line(graph, sb);
1132 return 0;
1133 case GRAPH_SKIP:
1134 graph_output_skip_line(graph, sb);
1135 return 0;
1136 case GRAPH_PRE_COMMIT:
1137 graph_output_pre_commit_line(graph, sb);
1138 return 0;
1139 case GRAPH_COMMIT:
1140 graph_output_commit_line(graph, sb);
1141 return 1;
1142 case GRAPH_POST_MERGE:
1143 graph_output_post_merge_line(graph, sb);
1144 return 0;
1145 case GRAPH_COLLAPSING:
1146 graph_output_collapsing_line(graph, sb);
1147 return 0;
1150 assert(0);
1151 return 0;
1154 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
1156 int i, j;
1158 if (graph->state != GRAPH_COMMIT) {
1159 graph_next_line(graph, sb);
1160 return;
1164 * Output the row containing this commit
1165 * Iterate up to and including graph->num_columns,
1166 * since the current commit may not be in any of the existing
1167 * columns. (This happens when the current commit doesn't have any
1168 * children that we have already processed.)
1170 for (i = 0; i < graph->num_columns; i++) {
1171 struct column *col = &graph->columns[i];
1172 struct commit *col_commit = col->commit;
1173 if (col_commit == graph->commit) {
1174 strbuf_write_column(sb, col, '|');
1176 if (graph->num_parents < 3)
1177 strbuf_addch(sb, ' ');
1178 else {
1179 int num_spaces = ((graph->num_parents - 2) * 2);
1180 for (j = 0; j < num_spaces; j++)
1181 strbuf_addch(sb, ' ');
1183 } else {
1184 strbuf_write_column(sb, col, '|');
1185 strbuf_addch(sb, ' ');
1189 graph_pad_horizontally(graph, sb, graph->num_columns);
1192 * Update graph->prev_state since we have output a padding line
1194 graph->prev_state = GRAPH_PADDING;
1197 int graph_is_commit_finished(struct git_graph const *graph)
1199 return (graph->state == GRAPH_PADDING);
1202 void graph_show_commit(struct git_graph *graph)
1204 struct strbuf msgbuf = STRBUF_INIT;
1205 int shown_commit_line = 0;
1207 if (!graph)
1208 return;
1210 while (!shown_commit_line) {
1211 shown_commit_line = graph_next_line(graph, &msgbuf);
1212 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1213 if (!shown_commit_line)
1214 putchar('\n');
1215 strbuf_setlen(&msgbuf, 0);
1218 strbuf_release(&msgbuf);
1221 void graph_show_oneline(struct git_graph *graph)
1223 struct strbuf msgbuf = STRBUF_INIT;
1225 if (!graph)
1226 return;
1228 graph_next_line(graph, &msgbuf);
1229 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1230 strbuf_release(&msgbuf);
1233 void graph_show_padding(struct git_graph *graph)
1235 struct strbuf msgbuf = STRBUF_INIT;
1237 if (!graph)
1238 return;
1240 graph_padding_line(graph, &msgbuf);
1241 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1242 strbuf_release(&msgbuf);
1245 int graph_show_remainder(struct git_graph *graph)
1247 struct strbuf msgbuf = STRBUF_INIT;
1248 int shown = 0;
1250 if (!graph)
1251 return 0;
1253 if (graph_is_commit_finished(graph))
1254 return 0;
1256 for (;;) {
1257 graph_next_line(graph, &msgbuf);
1258 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1259 strbuf_setlen(&msgbuf, 0);
1260 shown = 1;
1262 if (!graph_is_commit_finished(graph))
1263 putchar('\n');
1264 else
1265 break;
1267 strbuf_release(&msgbuf);
1269 return shown;
1273 static void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb)
1275 char *p;
1277 if (!graph) {
1278 fwrite(sb->buf, sizeof(char), sb->len, stdout);
1279 return;
1283 * Print the strbuf line by line,
1284 * and display the graph info before each line but the first.
1286 p = sb->buf;
1287 while (p) {
1288 size_t len;
1289 char *next_p = strchr(p, '\n');
1290 if (next_p) {
1291 next_p++;
1292 len = next_p - p;
1293 } else {
1294 len = (sb->buf + sb->len) - p;
1296 fwrite(p, sizeof(char), len, stdout);
1297 if (next_p && *next_p != '\0')
1298 graph_show_oneline(graph);
1299 p = next_p;
1303 void graph_show_commit_msg(struct git_graph *graph,
1304 struct strbuf const *sb)
1306 int newline_terminated;
1308 if (!graph) {
1310 * If there's no graph, just print the message buffer.
1312 * The message buffer for CMIT_FMT_ONELINE and
1313 * CMIT_FMT_USERFORMAT are already missing a terminating
1314 * newline. All of the other formats should have it.
1316 fwrite(sb->buf, sizeof(char), sb->len, stdout);
1317 return;
1320 newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
1323 * Show the commit message
1325 graph_show_strbuf(graph, sb);
1328 * If there is more output needed for this commit, show it now
1330 if (!graph_is_commit_finished(graph)) {
1332 * If sb doesn't have a terminating newline, print one now,
1333 * so we can start the remainder of the graph output on a
1334 * new line.
1336 if (!newline_terminated)
1337 putchar('\n');
1339 graph_show_remainder(graph);
1342 * If sb ends with a newline, our output should too.
1344 if (newline_terminated)
1345 putchar('\n');