log and friends: --second-parent
[git/mjg.git] / graph.c
blobd38ee9b17fc58b56703a764d078862d9f7d12e47
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 a padding line in the graph.
12 * This is similar to graph_next_line(). However, it is guaranteed to
13 * never print the current commit line. Instead, if the commit line is
14 * next, it will simply output a line of vertical padding, extending the
15 * branch lines downwards, but leaving them otherwise unchanged.
17 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb);
20 * Print a strbuf to stdout. If the graph is non-NULL, all lines but the
21 * first will be prefixed with the graph output.
23 * If the strbuf ends with a newline, the output will end after this
24 * newline. A new graph line will not be printed after the final newline.
25 * If the strbuf is empty, no output will be printed.
27 * Since the first line will not include the graph output, the caller is
28 * responsible for printing this line's graph (perhaps via
29 * graph_show_commit() or graph_show_oneline()) before calling
30 * graph_show_strbuf().
32 static void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb);
35 * TODO:
36 * - Limit the number of columns, similar to the way gitk does.
37 * If we reach more than a specified number of columns, omit
38 * sections of some columns.
41 struct column {
43 * The parent commit of this column.
45 struct commit *commit;
47 * The color to (optionally) print this column in. This is an
48 * index into column_colors.
50 unsigned short color;
53 enum graph_state {
54 GRAPH_PADDING,
55 GRAPH_SKIP,
56 GRAPH_PRE_COMMIT,
57 GRAPH_COMMIT,
58 GRAPH_POST_MERGE,
59 GRAPH_COLLAPSING
62 static const char **column_colors;
63 static unsigned short column_colors_max;
65 void graph_set_column_colors(const char **colors, unsigned short colors_max)
67 column_colors = colors;
68 column_colors_max = colors_max;
71 static const char *column_get_color_code(unsigned short color)
73 return column_colors[color];
76 static void strbuf_write_column(struct strbuf *sb, const struct column *c,
77 char col_char)
79 if (c->color < column_colors_max)
80 strbuf_addstr(sb, column_get_color_code(c->color));
81 strbuf_addch(sb, col_char);
82 if (c->color < column_colors_max)
83 strbuf_addstr(sb, column_get_color_code(column_colors_max));
86 struct git_graph {
88 * The commit currently being processed
90 struct commit *commit;
91 /* The rev-info used for the current traversal */
92 struct rev_info *revs;
94 * The number of interesting parents that this commit has.
96 * Note that this is not the same as the actual number of parents.
97 * This count excludes parents that won't be printed in the graph
98 * output, as determined by graph_is_interesting().
100 int num_parents;
102 * The width of the graph output for this commit.
103 * All rows for this commit are padded to this width, so that
104 * messages printed after the graph output are aligned.
106 int width;
108 * The next expansion row to print
109 * when state is GRAPH_PRE_COMMIT
111 int expansion_row;
113 * The current output state.
114 * This tells us what kind of line graph_next_line() should output.
116 enum graph_state state;
118 * The output state for the previous line of output.
119 * This is primarily used to determine how the first merge line
120 * should appear, based on the last line of the previous commit.
122 enum graph_state prev_state;
124 * The index of the column that refers to this commit.
126 * If none of the incoming columns refer to this commit,
127 * this will be equal to num_columns.
129 int commit_index;
131 * The commit_index for the previously displayed commit.
133 * This is used to determine how the first line of a merge
134 * graph output should appear, based on the last line of the
135 * previous commit.
137 int prev_commit_index;
139 * The maximum number of columns that can be stored in the columns
140 * and new_columns arrays. This is also half the number of entries
141 * that can be stored in the mapping and new_mapping arrays.
143 int column_capacity;
145 * The number of columns (also called "branch lines" in some places)
147 int num_columns;
149 * The number of columns in the new_columns array
151 int num_new_columns;
153 * The number of entries in the mapping array
155 int mapping_size;
157 * The column state before we output the current commit.
159 struct column *columns;
161 * The new column state after we output the current commit.
162 * Only valid when state is GRAPH_COLLAPSING.
164 struct column *new_columns;
166 * An array that tracks the current state of each
167 * character in the output line during state GRAPH_COLLAPSING.
168 * Each entry is -1 if this character is empty, or a non-negative
169 * integer if the character contains a branch line. The value of
170 * the integer indicates the target position for this branch line.
171 * (I.e., this array maps the current column positions to their
172 * desired positions.)
174 * The maximum capacity of this array is always
175 * sizeof(int) * 2 * column_capacity.
177 int *mapping;
179 * A temporary array for computing the next mapping state
180 * while we are outputting a mapping line. This is stored as part
181 * of the git_graph simply so we don't have to allocate a new
182 * temporary array each time we have to output a collapsing line.
184 int *new_mapping;
186 * The current default column color being used. This is
187 * stored as an index into the array column_colors.
189 unsigned short default_column_color;
192 static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void *data)
194 struct git_graph *graph = data;
195 static struct strbuf msgbuf = STRBUF_INIT;
197 assert(opt);
198 assert(graph);
200 opt->output_prefix_length = graph->width;
201 strbuf_reset(&msgbuf);
202 graph_padding_line(graph, &msgbuf);
203 return &msgbuf;
206 struct git_graph *graph_init(struct rev_info *opt)
208 struct git_graph *graph = xmalloc(sizeof(struct git_graph));
210 if (!column_colors)
211 graph_set_column_colors(column_colors_ansi,
212 column_colors_ansi_max);
214 graph->commit = NULL;
215 graph->revs = opt;
216 graph->num_parents = 0;
217 graph->expansion_row = 0;
218 graph->state = GRAPH_PADDING;
219 graph->prev_state = GRAPH_PADDING;
220 graph->commit_index = 0;
221 graph->prev_commit_index = 0;
222 graph->num_columns = 0;
223 graph->num_new_columns = 0;
224 graph->mapping_size = 0;
226 * Start the column color at the maximum value, since we'll
227 * always increment it for the first commit we output.
228 * This way we start at 0 for the first commit.
230 graph->default_column_color = column_colors_max - 1;
233 * Allocate a reasonably large default number of columns
234 * We'll automatically grow columns later if we need more room.
236 graph->column_capacity = 30;
237 graph->columns = xmalloc(sizeof(struct column) *
238 graph->column_capacity);
239 graph->new_columns = xmalloc(sizeof(struct column) *
240 graph->column_capacity);
241 graph->mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
242 graph->new_mapping = xmalloc(sizeof(int) * 2 * graph->column_capacity);
245 * The diff output prefix callback, with this we can make
246 * all the diff output to align with the graph lines.
248 opt->diffopt.output_prefix = diff_output_prefix_callback;
249 opt->diffopt.output_prefix_data = graph;
250 opt->diffopt.output_prefix_length = 0;
252 return graph;
255 static void graph_update_state(struct git_graph *graph, enum graph_state s)
257 graph->prev_state = graph->state;
258 graph->state = s;
261 static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
263 if (graph->column_capacity >= num_columns)
264 return;
266 do {
267 graph->column_capacity *= 2;
268 } while (graph->column_capacity < num_columns);
270 graph->columns = xrealloc(graph->columns,
271 sizeof(struct column) *
272 graph->column_capacity);
273 graph->new_columns = xrealloc(graph->new_columns,
274 sizeof(struct column) *
275 graph->column_capacity);
276 graph->mapping = xrealloc(graph->mapping,
277 sizeof(int) * 2 * graph->column_capacity);
278 graph->new_mapping = xrealloc(graph->new_mapping,
279 sizeof(int) * 2 * graph->column_capacity);
283 * Returns 1 if the commit will be printed in the graph output,
284 * and 0 otherwise.
286 static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
289 * If revs->boundary is set, commits whose children have
290 * been shown are always interesting, even if they have the
291 * UNINTERESTING or TREESAME flags set.
293 if (graph->revs && graph->revs->boundary) {
294 if (commit->object.flags & CHILD_SHOWN)
295 return 1;
299 * Otherwise, use get_commit_action() to see if this commit is
300 * interesting
302 return get_commit_action(graph->revs, commit) == commit_show;
305 static struct commit_list *next_interesting_parent(struct git_graph *graph,
306 struct commit_list *orig)
308 struct commit_list *list;
311 * If revs->first_parent_only is set, only the first
312 * parent is interesting. None of the others are.
314 if (graph->revs->first_parent_only)
315 return NULL;
318 * Return the next interesting commit after orig
320 for (list = orig->next; list; list = list->next) {
321 if (graph_is_interesting(graph, list->item))
322 return list;
325 return NULL;
328 static struct commit_list *first_interesting_parent(struct git_graph *graph)
330 struct commit_list *parents = graph->commit->parents;
333 * If this commit has no parents, ignore it
335 if (!parents)
336 return NULL;
338 if ((graph->revs->first_parent_only == 2) && parents->next &&
339 graph_is_interesting(graph, parents->next->item))
340 return parents->next;
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 (!want_color(graph->revs->diffopt.use_color))
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 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, or the start of a new
517 * childless column, increment the current
518 * color.
520 if (graph->num_parents > 1 ||
521 !is_commit_in_columns) {
522 graph_increment_column_color(graph);
524 graph_insert_into_new_columns(graph,
525 parent->item,
526 &mapping_idx);
529 * We always need to increment mapping_idx by at
530 * least 2, even if it has no interesting parents.
531 * The current commit always takes up at least 2
532 * spaces.
534 if (mapping_idx == old_mapping_idx)
535 mapping_idx += 2;
536 } else {
537 graph_insert_into_new_columns(graph, col_commit,
538 &mapping_idx);
543 * Shrink mapping_size to be the minimum necessary
545 while (graph->mapping_size > 1 &&
546 graph->mapping[graph->mapping_size - 1] < 0)
547 graph->mapping_size--;
550 * Compute graph->width for this commit
552 graph_update_width(graph, is_commit_in_columns);
555 void graph_update(struct git_graph *graph, struct commit *commit)
557 struct commit_list *parent;
560 * Set the new commit
562 graph->commit = commit;
565 * Count how many interesting parents this commit has
567 graph->num_parents = 0;
568 for (parent = first_interesting_parent(graph);
569 parent;
570 parent = next_interesting_parent(graph, parent))
572 graph->num_parents++;
576 * Store the old commit_index in prev_commit_index.
577 * graph_update_columns() will update graph->commit_index for this
578 * commit.
580 graph->prev_commit_index = graph->commit_index;
583 * Call graph_update_columns() to update
584 * columns, new_columns, and mapping.
586 graph_update_columns(graph);
588 graph->expansion_row = 0;
591 * Update graph->state.
592 * Note that we don't call graph_update_state() here, since
593 * we don't want to update graph->prev_state. No line for
594 * graph->state was ever printed.
596 * If the previous commit didn't get to the GRAPH_PADDING state,
597 * it never finished its output. Goto GRAPH_SKIP, to print out
598 * a line to indicate that portion of the graph is missing.
600 * If there are 3 or more parents, we may need to print extra rows
601 * before the commit, to expand the branch lines around it and make
602 * room for it. We need to do this only if there is a branch row
603 * (or more) to the right of this commit.
605 * If there are less than 3 parents, we can immediately print the
606 * commit line.
608 if (graph->state != GRAPH_PADDING)
609 graph->state = GRAPH_SKIP;
610 else if (graph->num_parents >= 3 &&
611 graph->commit_index < (graph->num_columns - 1))
612 graph->state = GRAPH_PRE_COMMIT;
613 else
614 graph->state = GRAPH_COMMIT;
617 static int graph_is_mapping_correct(struct git_graph *graph)
619 int i;
622 * The mapping is up to date if each entry is at its target,
623 * or is 1 greater than its target.
624 * (If it is 1 greater than the target, '/' will be printed, so it
625 * will look correct on the next row.)
627 for (i = 0; i < graph->mapping_size; i++) {
628 int target = graph->mapping[i];
629 if (target < 0)
630 continue;
631 if (target == (i / 2))
632 continue;
633 return 0;
636 return 1;
639 static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb,
640 int chars_written)
643 * Add additional spaces to the end of the strbuf, so that all
644 * lines for a particular commit have the same width.
646 * This way, fields printed to the right of the graph will remain
647 * aligned for the entire commit.
649 int extra;
650 if (chars_written >= graph->width)
651 return;
653 extra = graph->width - chars_written;
654 strbuf_addf(sb, "%*s", (int) extra, "");
657 static void graph_output_padding_line(struct git_graph *graph,
658 struct strbuf *sb)
660 int i;
663 * We could conceivable be called with a NULL commit
664 * if our caller has a bug, and invokes graph_next_line()
665 * immediately after graph_init(), without first calling
666 * graph_update(). Return without outputting anything in this
667 * case.
669 if (!graph->commit)
670 return;
673 * Output a padding row, that leaves all branch lines unchanged
675 for (i = 0; i < graph->num_new_columns; i++) {
676 strbuf_write_column(sb, &graph->new_columns[i], '|');
677 strbuf_addch(sb, ' ');
680 graph_pad_horizontally(graph, sb, graph->num_new_columns * 2);
683 static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
686 * Output an ellipsis to indicate that a portion
687 * of the graph is missing.
689 strbuf_addstr(sb, "...");
690 graph_pad_horizontally(graph, sb, 3);
692 if (graph->num_parents >= 3 &&
693 graph->commit_index < (graph->num_columns - 1))
694 graph_update_state(graph, GRAPH_PRE_COMMIT);
695 else
696 graph_update_state(graph, GRAPH_COMMIT);
699 static void graph_output_pre_commit_line(struct git_graph *graph,
700 struct strbuf *sb)
702 int num_expansion_rows;
703 int i, seen_this;
704 int chars_written;
707 * This function formats a row that increases the space around a commit
708 * with multiple parents, to make room for it. It should only be
709 * called when there are 3 or more parents.
711 * We need 2 extra rows for every parent over 2.
713 assert(graph->num_parents >= 3);
714 num_expansion_rows = (graph->num_parents - 2) * 2;
717 * graph->expansion_row tracks the current expansion row we are on.
718 * It should be in the range [0, num_expansion_rows - 1]
720 assert(0 <= graph->expansion_row &&
721 graph->expansion_row < num_expansion_rows);
724 * Output the row
726 seen_this = 0;
727 chars_written = 0;
728 for (i = 0; i < graph->num_columns; i++) {
729 struct column *col = &graph->columns[i];
730 if (col->commit == graph->commit) {
731 seen_this = 1;
732 strbuf_write_column(sb, col, '|');
733 strbuf_addf(sb, "%*s", graph->expansion_row, "");
734 chars_written += 1 + graph->expansion_row;
735 } else if (seen_this && (graph->expansion_row == 0)) {
737 * This is the first line of the pre-commit output.
738 * If the previous commit was a merge commit and
739 * ended in the GRAPH_POST_MERGE state, all branch
740 * lines after graph->prev_commit_index were
741 * printed as "\" on the previous line. Continue
742 * to print them as "\" on this line. Otherwise,
743 * print the branch lines as "|".
745 if (graph->prev_state == GRAPH_POST_MERGE &&
746 graph->prev_commit_index < i)
747 strbuf_write_column(sb, col, '\\');
748 else
749 strbuf_write_column(sb, col, '|');
750 chars_written++;
751 } else if (seen_this && (graph->expansion_row > 0)) {
752 strbuf_write_column(sb, col, '\\');
753 chars_written++;
754 } else {
755 strbuf_write_column(sb, col, '|');
756 chars_written++;
758 strbuf_addch(sb, ' ');
759 chars_written++;
762 graph_pad_horizontally(graph, sb, chars_written);
765 * Increment graph->expansion_row,
766 * and move to state GRAPH_COMMIT if necessary
768 graph->expansion_row++;
769 if (graph->expansion_row >= num_expansion_rows)
770 graph_update_state(graph, GRAPH_COMMIT);
773 static void graph_output_commit_char(struct git_graph *graph, struct strbuf *sb)
776 * For boundary commits, print 'o'
777 * (We should only see boundary commits when revs->boundary is set.)
779 if (graph->commit->object.flags & BOUNDARY) {
780 assert(graph->revs->boundary);
781 strbuf_addch(sb, 'o');
782 return;
786 * get_revision_mark() handles all other cases without assert()
788 strbuf_addstr(sb, get_revision_mark(graph->revs, graph->commit));
792 * Draw an octopus merge and return the number of characters written.
794 static int graph_draw_octopus_merge(struct git_graph *graph,
795 struct strbuf *sb)
798 * Here dashless_commits represents the number of parents
799 * which don't need to have dashes (because their edges fit
800 * neatly under the commit).
802 const int dashless_commits = 2;
803 int col_num, i;
804 int num_dashes =
805 ((graph->num_parents - dashless_commits) * 2) - 1;
806 for (i = 0; i < num_dashes; i++) {
807 col_num = (i / 2) + dashless_commits;
808 strbuf_write_column(sb, &graph->new_columns[col_num], '-');
810 col_num = (i / 2) + dashless_commits;
811 strbuf_write_column(sb, &graph->new_columns[col_num], '.');
812 return num_dashes + 1;
815 static void graph_output_commit_line(struct git_graph *graph, struct strbuf *sb)
817 int seen_this = 0;
818 int i, chars_written;
821 * Output the row containing this commit
822 * Iterate up to and including graph->num_columns,
823 * since the current commit may not be in any of the existing
824 * columns. (This happens when the current commit doesn't have any
825 * children that we have already processed.)
827 seen_this = 0;
828 chars_written = 0;
829 for (i = 0; i <= graph->num_columns; i++) {
830 struct column *col = &graph->columns[i];
831 struct commit *col_commit;
832 if (i == graph->num_columns) {
833 if (seen_this)
834 break;
835 col_commit = graph->commit;
836 } else {
837 col_commit = graph->columns[i].commit;
840 if (col_commit == graph->commit) {
841 seen_this = 1;
842 graph_output_commit_char(graph, sb);
843 chars_written++;
845 if (graph->num_parents > 2)
846 chars_written += graph_draw_octopus_merge(graph,
847 sb);
848 } else if (seen_this && (graph->num_parents > 2)) {
849 strbuf_write_column(sb, col, '\\');
850 chars_written++;
851 } else if (seen_this && (graph->num_parents == 2)) {
853 * This is a 2-way merge commit.
854 * There is no GRAPH_PRE_COMMIT stage for 2-way
855 * merges, so this is the first line of output
856 * for this commit. Check to see what the previous
857 * line of output was.
859 * If it was GRAPH_POST_MERGE, the branch line
860 * coming into this commit may have been '\',
861 * and not '|' or '/'. If so, output the branch
862 * line as '\' on this line, instead of '|'. This
863 * makes the output look nicer.
865 if (graph->prev_state == GRAPH_POST_MERGE &&
866 graph->prev_commit_index < i)
867 strbuf_write_column(sb, col, '\\');
868 else
869 strbuf_write_column(sb, col, '|');
870 chars_written++;
871 } else {
872 strbuf_write_column(sb, col, '|');
873 chars_written++;
875 strbuf_addch(sb, ' ');
876 chars_written++;
879 graph_pad_horizontally(graph, sb, chars_written);
882 * Update graph->state
884 if (graph->num_parents > 1)
885 graph_update_state(graph, GRAPH_POST_MERGE);
886 else if (graph_is_mapping_correct(graph))
887 graph_update_state(graph, GRAPH_PADDING);
888 else
889 graph_update_state(graph, GRAPH_COLLAPSING);
892 static struct column *find_new_column_by_commit(struct git_graph *graph,
893 struct commit *commit)
895 int i;
896 for (i = 0; i < graph->num_new_columns; i++) {
897 if (graph->new_columns[i].commit == commit)
898 return &graph->new_columns[i];
900 return NULL;
903 static void graph_output_post_merge_line(struct git_graph *graph, struct strbuf *sb)
905 int seen_this = 0;
906 int i, j, chars_written;
909 * Output the post-merge row
911 chars_written = 0;
912 for (i = 0; i <= graph->num_columns; i++) {
913 struct column *col = &graph->columns[i];
914 struct commit *col_commit;
915 if (i == graph->num_columns) {
916 if (seen_this)
917 break;
918 col_commit = graph->commit;
919 } else {
920 col_commit = col->commit;
923 if (col_commit == graph->commit) {
925 * Since the current commit is a merge find
926 * the columns for the parent commits in
927 * new_columns and use those to format the
928 * edges.
930 struct commit_list *parents = NULL;
931 struct column *par_column;
932 seen_this = 1;
933 parents = first_interesting_parent(graph);
934 assert(parents);
935 par_column = find_new_column_by_commit(graph, parents->item);
936 assert(par_column);
938 strbuf_write_column(sb, par_column, '|');
939 chars_written++;
940 for (j = 0; j < graph->num_parents - 1; j++) {
941 parents = next_interesting_parent(graph, parents);
942 assert(parents);
943 par_column = find_new_column_by_commit(graph, parents->item);
944 assert(par_column);
945 strbuf_write_column(sb, par_column, '\\');
946 strbuf_addch(sb, ' ');
948 chars_written += j * 2;
949 } else if (seen_this) {
950 strbuf_write_column(sb, col, '\\');
951 strbuf_addch(sb, ' ');
952 chars_written += 2;
953 } else {
954 strbuf_write_column(sb, col, '|');
955 strbuf_addch(sb, ' ');
956 chars_written += 2;
960 graph_pad_horizontally(graph, sb, chars_written);
963 * Update graph->state
965 if (graph_is_mapping_correct(graph))
966 graph_update_state(graph, GRAPH_PADDING);
967 else
968 graph_update_state(graph, GRAPH_COLLAPSING);
971 static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
973 int i;
974 int *tmp_mapping;
975 short used_horizontal = 0;
976 int horizontal_edge = -1;
977 int horizontal_edge_target = -1;
980 * Clear out the new_mapping array
982 for (i = 0; i < graph->mapping_size; i++)
983 graph->new_mapping[i] = -1;
985 for (i = 0; i < graph->mapping_size; i++) {
986 int target = graph->mapping[i];
987 if (target < 0)
988 continue;
991 * Since update_columns() always inserts the leftmost
992 * column first, each branch's target location should
993 * always be either its current location or to the left of
994 * its current location.
996 * We never have to move branches to the right. This makes
997 * the graph much more legible, since whenever branches
998 * cross, only one is moving directions.
1000 assert(target * 2 <= i);
1002 if (target * 2 == i) {
1004 * This column is already in the
1005 * correct place
1007 assert(graph->new_mapping[i] == -1);
1008 graph->new_mapping[i] = target;
1009 } else if (graph->new_mapping[i - 1] < 0) {
1011 * Nothing is to the left.
1012 * Move to the left by one
1014 graph->new_mapping[i - 1] = target;
1016 * If there isn't already an edge moving horizontally
1017 * select this one.
1019 if (horizontal_edge == -1) {
1020 int j;
1021 horizontal_edge = i;
1022 horizontal_edge_target = target;
1024 * The variable target is the index of the graph
1025 * column, and therefore target*2+3 is the
1026 * actual screen column of the first horizontal
1027 * line.
1029 for (j = (target * 2)+3; j < (i - 2); j += 2)
1030 graph->new_mapping[j] = target;
1032 } else if (graph->new_mapping[i - 1] == target) {
1034 * There is a branch line to our left
1035 * already, and it is our target. We
1036 * combine with this line, since we share
1037 * the same parent commit.
1039 * We don't have to add anything to the
1040 * output or new_mapping, since the
1041 * existing branch line has already taken
1042 * care of it.
1044 } else {
1046 * There is a branch line to our left,
1047 * but it isn't our target. We need to
1048 * cross over it.
1050 * The space just to the left of this
1051 * branch should always be empty.
1053 * The branch to the left of that space
1054 * should be our eventual target.
1056 assert(graph->new_mapping[i - 1] > target);
1057 assert(graph->new_mapping[i - 2] < 0);
1058 assert(graph->new_mapping[i - 3] == target);
1059 graph->new_mapping[i - 2] = target;
1061 * Mark this branch as the horizontal edge to
1062 * prevent any other edges from moving
1063 * horizontally.
1065 if (horizontal_edge == -1)
1066 horizontal_edge = i;
1071 * The new mapping may be 1 smaller than the old mapping
1073 if (graph->new_mapping[graph->mapping_size - 1] < 0)
1074 graph->mapping_size--;
1077 * Output out a line based on the new mapping info
1079 for (i = 0; i < graph->mapping_size; i++) {
1080 int target = graph->new_mapping[i];
1081 if (target < 0)
1082 strbuf_addch(sb, ' ');
1083 else if (target * 2 == i)
1084 strbuf_write_column(sb, &graph->new_columns[target], '|');
1085 else if (target == horizontal_edge_target &&
1086 i != horizontal_edge - 1) {
1088 * Set the mappings for all but the
1089 * first segment to -1 so that they
1090 * won't continue into the next line.
1092 if (i != (target * 2)+3)
1093 graph->new_mapping[i] = -1;
1094 used_horizontal = 1;
1095 strbuf_write_column(sb, &graph->new_columns[target], '_');
1096 } else {
1097 if (used_horizontal && i < horizontal_edge)
1098 graph->new_mapping[i] = -1;
1099 strbuf_write_column(sb, &graph->new_columns[target], '/');
1104 graph_pad_horizontally(graph, sb, graph->mapping_size);
1107 * Swap mapping and new_mapping
1109 tmp_mapping = graph->mapping;
1110 graph->mapping = graph->new_mapping;
1111 graph->new_mapping = tmp_mapping;
1114 * If graph->mapping indicates that all of the branch lines
1115 * are already in the correct positions, we are done.
1116 * Otherwise, we need to collapse some branch lines together.
1118 if (graph_is_mapping_correct(graph))
1119 graph_update_state(graph, GRAPH_PADDING);
1122 int graph_next_line(struct git_graph *graph, struct strbuf *sb)
1124 switch (graph->state) {
1125 case GRAPH_PADDING:
1126 graph_output_padding_line(graph, sb);
1127 return 0;
1128 case GRAPH_SKIP:
1129 graph_output_skip_line(graph, sb);
1130 return 0;
1131 case GRAPH_PRE_COMMIT:
1132 graph_output_pre_commit_line(graph, sb);
1133 return 0;
1134 case GRAPH_COMMIT:
1135 graph_output_commit_line(graph, sb);
1136 return 1;
1137 case GRAPH_POST_MERGE:
1138 graph_output_post_merge_line(graph, sb);
1139 return 0;
1140 case GRAPH_COLLAPSING:
1141 graph_output_collapsing_line(graph, sb);
1142 return 0;
1145 assert(0);
1146 return 0;
1149 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
1151 int i, j;
1153 if (graph->state != GRAPH_COMMIT) {
1154 graph_next_line(graph, sb);
1155 return;
1159 * Output the row containing this commit
1160 * Iterate up to and including graph->num_columns,
1161 * since the current commit may not be in any of the existing
1162 * columns. (This happens when the current commit doesn't have any
1163 * children that we have already processed.)
1165 for (i = 0; i < graph->num_columns; i++) {
1166 struct column *col = &graph->columns[i];
1167 struct commit *col_commit = col->commit;
1168 if (col_commit == graph->commit) {
1169 strbuf_write_column(sb, col, '|');
1171 if (graph->num_parents < 3)
1172 strbuf_addch(sb, ' ');
1173 else {
1174 int num_spaces = ((graph->num_parents - 2) * 2);
1175 for (j = 0; j < num_spaces; j++)
1176 strbuf_addch(sb, ' ');
1178 } else {
1179 strbuf_write_column(sb, col, '|');
1180 strbuf_addch(sb, ' ');
1184 graph_pad_horizontally(graph, sb, graph->num_columns);
1187 * Update graph->prev_state since we have output a padding line
1189 graph->prev_state = GRAPH_PADDING;
1192 int graph_is_commit_finished(struct git_graph const *graph)
1194 return (graph->state == GRAPH_PADDING);
1197 void graph_show_commit(struct git_graph *graph)
1199 struct strbuf msgbuf = STRBUF_INIT;
1200 int shown_commit_line = 0;
1202 if (!graph)
1203 return;
1206 * When showing a diff of a merge against each of its parents, we
1207 * are called once for each parent without graph_update having been
1208 * called. In this case, simply output a single padding line.
1210 if (graph_is_commit_finished(graph)) {
1211 graph_show_padding(graph);
1212 shown_commit_line = 1;
1215 while (!shown_commit_line && !graph_is_commit_finished(graph)) {
1216 shown_commit_line = graph_next_line(graph, &msgbuf);
1217 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1218 if (!shown_commit_line)
1219 putchar('\n');
1220 strbuf_setlen(&msgbuf, 0);
1223 strbuf_release(&msgbuf);
1226 void graph_show_oneline(struct git_graph *graph)
1228 struct strbuf msgbuf = STRBUF_INIT;
1230 if (!graph)
1231 return;
1233 graph_next_line(graph, &msgbuf);
1234 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1235 strbuf_release(&msgbuf);
1238 void graph_show_padding(struct git_graph *graph)
1240 struct strbuf msgbuf = STRBUF_INIT;
1242 if (!graph)
1243 return;
1245 graph_padding_line(graph, &msgbuf);
1246 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1247 strbuf_release(&msgbuf);
1250 int graph_show_remainder(struct git_graph *graph)
1252 struct strbuf msgbuf = STRBUF_INIT;
1253 int shown = 0;
1255 if (!graph)
1256 return 0;
1258 if (graph_is_commit_finished(graph))
1259 return 0;
1261 for (;;) {
1262 graph_next_line(graph, &msgbuf);
1263 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
1264 strbuf_setlen(&msgbuf, 0);
1265 shown = 1;
1267 if (!graph_is_commit_finished(graph))
1268 putchar('\n');
1269 else
1270 break;
1272 strbuf_release(&msgbuf);
1274 return shown;
1278 static void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb)
1280 char *p;
1282 if (!graph) {
1283 fwrite(sb->buf, sizeof(char), sb->len, stdout);
1284 return;
1288 * Print the strbuf line by line,
1289 * and display the graph info before each line but the first.
1291 p = sb->buf;
1292 while (p) {
1293 size_t len;
1294 char *next_p = strchr(p, '\n');
1295 if (next_p) {
1296 next_p++;
1297 len = next_p - p;
1298 } else {
1299 len = (sb->buf + sb->len) - p;
1301 fwrite(p, sizeof(char), len, stdout);
1302 if (next_p && *next_p != '\0')
1303 graph_show_oneline(graph);
1304 p = next_p;
1308 void graph_show_commit_msg(struct git_graph *graph,
1309 struct strbuf const *sb)
1311 int newline_terminated;
1313 if (!graph) {
1315 * If there's no graph, just print the message buffer.
1317 * The message buffer for CMIT_FMT_ONELINE and
1318 * CMIT_FMT_USERFORMAT are already missing a terminating
1319 * newline. All of the other formats should have it.
1321 fwrite(sb->buf, sizeof(char), sb->len, stdout);
1322 return;
1325 newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
1328 * Show the commit message
1330 graph_show_strbuf(graph, sb);
1333 * If there is more output needed for this commit, show it now
1335 if (!graph_is_commit_finished(graph)) {
1337 * If sb doesn't have a terminating newline, print one now,
1338 * so we can start the remainder of the graph output on a
1339 * new line.
1341 if (!newline_terminated)
1342 putchar('\n');
1344 graph_show_remainder(graph);
1347 * If sb ends with a newline, our output should too.
1349 if (newline_terminated)
1350 putchar('\n');