log --graph: customize the graph lines with config log.graphColors
[alt-git.git] / graph.c
blob00aeee36d8a048d56ab686b136cc68bc111f9929
1 #include "cache.h"
2 #include "commit.h"
3 #include "color.h"
4 #include "graph.h"
5 #include "diff.h"
6 #include "revision.h"
7 #include "argv-array.h"
9 /* Internal API */
12 * Output a padding line in the graph.
13 * This is similar to graph_next_line(). However, it is guaranteed to
14 * never print the current commit line. Instead, if the commit line is
15 * next, it will simply output a line of vertical padding, extending the
16 * branch lines downwards, but leaving them otherwise unchanged.
18 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb);
21 * Print a strbuf. If the graph is non-NULL, all lines but the first will be
22 * prefixed with the graph output.
24 * If the strbuf ends with a newline, the output will end after this
25 * newline. A new graph line will not be printed after the final newline.
26 * If the strbuf is empty, no output will be printed.
28 * Since the first line will not include the graph output, the caller is
29 * responsible for printing this line's graph (perhaps via
30 * graph_show_commit() or graph_show_oneline()) before calling
31 * graph_show_strbuf().
33 static void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb);
36 * TODO:
37 * - Limit the number of columns, similar to the way gitk does.
38 * If we reach more than a specified number of columns, omit
39 * sections of some columns.
42 struct column {
44 * The parent commit of this column.
46 struct commit *commit;
48 * The color to (optionally) print this column in. This is an
49 * index into column_colors.
51 unsigned short color;
54 enum graph_state {
55 GRAPH_PADDING,
56 GRAPH_SKIP,
57 GRAPH_PRE_COMMIT,
58 GRAPH_COMMIT,
59 GRAPH_POST_MERGE,
60 GRAPH_COLLAPSING
63 static const char **column_colors;
64 static unsigned short column_colors_max;
66 static void parse_graph_colors_config(struct argv_array *colors, const char *string)
68 const char *end, *start;
70 start = string;
71 end = string + strlen(string);
72 while (start < end) {
73 const char *comma = strchrnul(start, ',');
74 char color[COLOR_MAXLEN];
76 if (!color_parse_mem(start, comma - start, color))
77 argv_array_push(colors, color);
78 else
79 warning(_("ignore invalid color '%.*s' in log.graphColors"),
80 (int)(comma - start), start);
81 start = comma + 1;
83 argv_array_push(colors, GIT_COLOR_RESET);
86 void graph_set_column_colors(const char **colors, unsigned short colors_max)
88 column_colors = colors;
89 column_colors_max = colors_max;
92 static const char *column_get_color_code(unsigned short color)
94 return column_colors[color];
97 static void strbuf_write_column(struct strbuf *sb, const struct column *c,
98 char col_char)
100 if (c->color < column_colors_max)
101 strbuf_addstr(sb, column_get_color_code(c->color));
102 strbuf_addch(sb, col_char);
103 if (c->color < column_colors_max)
104 strbuf_addstr(sb, column_get_color_code(column_colors_max));
107 struct git_graph {
109 * The commit currently being processed
111 struct commit *commit;
112 /* The rev-info used for the current traversal */
113 struct rev_info *revs;
115 * The number of interesting parents that this commit has.
117 * Note that this is not the same as the actual number of parents.
118 * This count excludes parents that won't be printed in the graph
119 * output, as determined by graph_is_interesting().
121 int num_parents;
123 * The width of the graph output for this commit.
124 * All rows for this commit are padded to this width, so that
125 * messages printed after the graph output are aligned.
127 int width;
129 * The next expansion row to print
130 * when state is GRAPH_PRE_COMMIT
132 int expansion_row;
134 * The current output state.
135 * This tells us what kind of line graph_next_line() should output.
137 enum graph_state state;
139 * The output state for the previous line of output.
140 * This is primarily used to determine how the first merge line
141 * should appear, based on the last line of the previous commit.
143 enum graph_state prev_state;
145 * The index of the column that refers to this commit.
147 * If none of the incoming columns refer to this commit,
148 * this will be equal to num_columns.
150 int commit_index;
152 * The commit_index for the previously displayed commit.
154 * This is used to determine how the first line of a merge
155 * graph output should appear, based on the last line of the
156 * previous commit.
158 int prev_commit_index;
160 * The maximum number of columns that can be stored in the columns
161 * and new_columns arrays. This is also half the number of entries
162 * that can be stored in the mapping and new_mapping arrays.
164 int column_capacity;
166 * The number of columns (also called "branch lines" in some places)
168 int num_columns;
170 * The number of columns in the new_columns array
172 int num_new_columns;
174 * The number of entries in the mapping array
176 int mapping_size;
178 * The column state before we output the current commit.
180 struct column *columns;
182 * The new column state after we output the current commit.
183 * Only valid when state is GRAPH_COLLAPSING.
185 struct column *new_columns;
187 * An array that tracks the current state of each
188 * character in the output line during state GRAPH_COLLAPSING.
189 * Each entry is -1 if this character is empty, or a non-negative
190 * integer if the character contains a branch line. The value of
191 * the integer indicates the target position for this branch line.
192 * (I.e., this array maps the current column positions to their
193 * desired positions.)
195 * The maximum capacity of this array is always
196 * sizeof(int) * 2 * column_capacity.
198 int *mapping;
200 * A temporary array for computing the next mapping state
201 * while we are outputting a mapping line. This is stored as part
202 * of the git_graph simply so we don't have to allocate a new
203 * temporary array each time we have to output a collapsing line.
205 int *new_mapping;
207 * The current default column color being used. This is
208 * stored as an index into the array column_colors.
210 unsigned short default_column_color;
213 static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void *data)
215 struct git_graph *graph = data;
216 static struct strbuf msgbuf = STRBUF_INIT;
218 assert(opt);
219 assert(graph);
221 opt->output_prefix_length = graph->width;
222 strbuf_reset(&msgbuf);
223 graph_padding_line(graph, &msgbuf);
224 return &msgbuf;
227 struct git_graph *graph_init(struct rev_info *opt)
229 struct git_graph *graph = xmalloc(sizeof(struct git_graph));
231 if (!column_colors) {
232 char *string;
233 if (git_config_get_string("log.graphcolors", &string)) {
234 /* not configured -- use default */
235 graph_set_column_colors(column_colors_ansi,
236 column_colors_ansi_max);
237 } else {
238 static struct argv_array custom_colors = ARGV_ARRAY_INIT;
239 argv_array_clear(&custom_colors);
240 parse_graph_colors_config(&custom_colors, string);
241 free(string);
242 /* graph_set_column_colors takes a max-index, not a count */
243 graph_set_column_colors(custom_colors.argv,
244 custom_colors.argc - 1);
248 graph->commit = NULL;
249 graph->revs = opt;
250 graph->num_parents = 0;
251 graph->expansion_row = 0;
252 graph->state = GRAPH_PADDING;
253 graph->prev_state = GRAPH_PADDING;
254 graph->commit_index = 0;
255 graph->prev_commit_index = 0;
256 graph->num_columns = 0;
257 graph->num_new_columns = 0;
258 graph->mapping_size = 0;
260 * Start the column color at the maximum value, since we'll
261 * always increment it for the first commit we output.
262 * This way we start at 0 for the first commit.
264 graph->default_column_color = column_colors_max - 1;
267 * Allocate a reasonably large default number of columns
268 * We'll automatically grow columns later if we need more room.
270 graph->column_capacity = 30;
271 ALLOC_ARRAY(graph->columns, graph->column_capacity);
272 ALLOC_ARRAY(graph->new_columns, graph->column_capacity);
273 ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity);
274 ALLOC_ARRAY(graph->new_mapping, 2 * graph->column_capacity);
277 * The diff output prefix callback, with this we can make
278 * all the diff output to align with the graph lines.
280 opt->diffopt.output_prefix = diff_output_prefix_callback;
281 opt->diffopt.output_prefix_data = graph;
282 opt->diffopt.output_prefix_length = 0;
284 return graph;
287 static void graph_update_state(struct git_graph *graph, enum graph_state s)
289 graph->prev_state = graph->state;
290 graph->state = s;
293 static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
295 if (graph->column_capacity >= num_columns)
296 return;
298 do {
299 graph->column_capacity *= 2;
300 } while (graph->column_capacity < num_columns);
302 REALLOC_ARRAY(graph->columns, graph->column_capacity);
303 REALLOC_ARRAY(graph->new_columns, graph->column_capacity);
304 REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2);
305 REALLOC_ARRAY(graph->new_mapping, graph->column_capacity * 2);
309 * Returns 1 if the commit will be printed in the graph output,
310 * and 0 otherwise.
312 static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
315 * If revs->boundary is set, commits whose children have
316 * been shown are always interesting, even if they have the
317 * UNINTERESTING or TREESAME flags set.
319 if (graph->revs && graph->revs->boundary) {
320 if (commit->object.flags & CHILD_SHOWN)
321 return 1;
325 * Otherwise, use get_commit_action() to see if this commit is
326 * interesting
328 return get_commit_action(graph->revs, commit) == commit_show;
331 static struct commit_list *next_interesting_parent(struct git_graph *graph,
332 struct commit_list *orig)
334 struct commit_list *list;
337 * If revs->first_parent_only is set, only the first
338 * parent is interesting. None of the others are.
340 if (graph->revs->first_parent_only)
341 return NULL;
344 * Return the next interesting commit after orig
346 for (list = orig->next; list; list = list->next) {
347 if (graph_is_interesting(graph, list->item))
348 return list;
351 return NULL;
354 static struct commit_list *first_interesting_parent(struct git_graph *graph)
356 struct commit_list *parents = graph->commit->parents;
359 * If this commit has no parents, ignore it
361 if (!parents)
362 return NULL;
365 * If the first parent is interesting, return it
367 if (graph_is_interesting(graph, parents->item))
368 return parents;
371 * Otherwise, call next_interesting_parent() to get
372 * the next interesting parent
374 return next_interesting_parent(graph, parents);
377 static unsigned short graph_get_current_column_color(const struct git_graph *graph)
379 if (!want_color(graph->revs->diffopt.use_color))
380 return column_colors_max;
381 return graph->default_column_color;
385 * Update the graph's default column color.
387 static void graph_increment_column_color(struct git_graph *graph)
389 graph->default_column_color = (graph->default_column_color + 1) %
390 column_colors_max;
393 static unsigned short graph_find_commit_color(const struct git_graph *graph,
394 const struct commit *commit)
396 int i;
397 for (i = 0; i < graph->num_columns; i++) {
398 if (graph->columns[i].commit == commit)
399 return graph->columns[i].color;
401 return graph_get_current_column_color(graph);
404 static void graph_insert_into_new_columns(struct git_graph *graph,
405 struct commit *commit,
406 int *mapping_index)
408 int i;
411 * If the commit is already in the new_columns list, we don't need to
412 * add it. Just update the mapping correctly.
414 for (i = 0; i < graph->num_new_columns; i++) {
415 if (graph->new_columns[i].commit == commit) {
416 graph->mapping[*mapping_index] = i;
417 *mapping_index += 2;
418 return;
423 * This commit isn't already in new_columns. Add it.
425 graph->new_columns[graph->num_new_columns].commit = commit;
426 graph->new_columns[graph->num_new_columns].color = graph_find_commit_color(graph, commit);
427 graph->mapping[*mapping_index] = graph->num_new_columns;
428 *mapping_index += 2;
429 graph->num_new_columns++;
432 static void graph_update_width(struct git_graph *graph,
433 int is_commit_in_existing_columns)
436 * Compute the width needed to display the graph for this commit.
437 * This is the maximum width needed for any row. All other rows
438 * will be padded to this width.
440 * Compute the number of columns in the widest row:
441 * Count each existing column (graph->num_columns), and each new
442 * column added by this commit.
444 int max_cols = graph->num_columns + graph->num_parents;
447 * Even if the current commit has no parents to be printed, it
448 * still takes up a column for itself.
450 if (graph->num_parents < 1)
451 max_cols++;
454 * We added a column for the current commit as part of
455 * graph->num_parents. If the current commit was already in
456 * graph->columns, then we have double counted it.
458 if (is_commit_in_existing_columns)
459 max_cols--;
462 * Each column takes up 2 spaces
464 graph->width = max_cols * 2;
467 static void graph_update_columns(struct git_graph *graph)
469 struct commit_list *parent;
470 struct column *tmp_columns;
471 int max_new_columns;
472 int mapping_idx;
473 int i, seen_this, is_commit_in_columns;
476 * Swap graph->columns with graph->new_columns
477 * graph->columns contains the state for the previous commit,
478 * and new_columns now contains the state for our commit.
480 * We'll re-use the old columns array as storage to compute the new
481 * columns list for the commit after this one.
483 tmp_columns = graph->columns;
484 graph->columns = graph->new_columns;
485 graph->num_columns = graph->num_new_columns;
487 graph->new_columns = tmp_columns;
488 graph->num_new_columns = 0;
491 * Now update new_columns and mapping with the information for the
492 * commit after this one.
494 * First, make sure we have enough room. At most, there will
495 * be graph->num_columns + graph->num_parents columns for the next
496 * commit.
498 max_new_columns = graph->num_columns + graph->num_parents;
499 graph_ensure_capacity(graph, max_new_columns);
502 * Clear out graph->mapping
504 graph->mapping_size = 2 * max_new_columns;
505 for (i = 0; i < graph->mapping_size; i++)
506 graph->mapping[i] = -1;
509 * Populate graph->new_columns and graph->mapping
511 * Some of the parents of this commit may already be in
512 * graph->columns. If so, graph->new_columns should only contain a
513 * single entry for each such commit. graph->mapping should
514 * contain information about where each current branch line is
515 * supposed to end up after the collapsing is performed.
517 seen_this = 0;
518 mapping_idx = 0;
519 is_commit_in_columns = 1;
520 for (i = 0; i <= graph->num_columns; i++) {
521 struct commit *col_commit;
522 if (i == graph->num_columns) {
523 if (seen_this)
524 break;
525 is_commit_in_columns = 0;
526 col_commit = graph->commit;
527 } else {
528 col_commit = graph->columns[i].commit;
531 if (col_commit == graph->commit) {
532 int old_mapping_idx = mapping_idx;
533 seen_this = 1;
534 graph->commit_index = i;
535 for (parent = first_interesting_parent(graph);
536 parent;
537 parent = next_interesting_parent(graph, parent)) {
539 * If this is a merge, or the start of a new
540 * childless column, increment the current
541 * color.
543 if (graph->num_parents > 1 ||
544 !is_commit_in_columns) {
545 graph_increment_column_color(graph);
547 graph_insert_into_new_columns(graph,
548 parent->item,
549 &mapping_idx);
552 * We always need to increment mapping_idx by at
553 * least 2, even if it has no interesting parents.
554 * The current commit always takes up at least 2
555 * spaces.
557 if (mapping_idx == old_mapping_idx)
558 mapping_idx += 2;
559 } else {
560 graph_insert_into_new_columns(graph, col_commit,
561 &mapping_idx);
566 * Shrink mapping_size to be the minimum necessary
568 while (graph->mapping_size > 1 &&
569 graph->mapping[graph->mapping_size - 1] < 0)
570 graph->mapping_size--;
573 * Compute graph->width for this commit
575 graph_update_width(graph, is_commit_in_columns);
578 void graph_update(struct git_graph *graph, struct commit *commit)
580 struct commit_list *parent;
583 * Set the new commit
585 graph->commit = commit;
588 * Count how many interesting parents this commit has
590 graph->num_parents = 0;
591 for (parent = first_interesting_parent(graph);
592 parent;
593 parent = next_interesting_parent(graph, parent))
595 graph->num_parents++;
599 * Store the old commit_index in prev_commit_index.
600 * graph_update_columns() will update graph->commit_index for this
601 * commit.
603 graph->prev_commit_index = graph->commit_index;
606 * Call graph_update_columns() to update
607 * columns, new_columns, and mapping.
609 graph_update_columns(graph);
611 graph->expansion_row = 0;
614 * Update graph->state.
615 * Note that we don't call graph_update_state() here, since
616 * we don't want to update graph->prev_state. No line for
617 * graph->state was ever printed.
619 * If the previous commit didn't get to the GRAPH_PADDING state,
620 * it never finished its output. Goto GRAPH_SKIP, to print out
621 * a line to indicate that portion of the graph is missing.
623 * If there are 3 or more parents, we may need to print extra rows
624 * before the commit, to expand the branch lines around it and make
625 * room for it. We need to do this only if there is a branch row
626 * (or more) to the right of this commit.
628 * If there are less than 3 parents, we can immediately print the
629 * commit line.
631 if (graph->state != GRAPH_PADDING)
632 graph->state = GRAPH_SKIP;
633 else if (graph->num_parents >= 3 &&
634 graph->commit_index < (graph->num_columns - 1))
635 graph->state = GRAPH_PRE_COMMIT;
636 else
637 graph->state = GRAPH_COMMIT;
640 static int graph_is_mapping_correct(struct git_graph *graph)
642 int i;
645 * The mapping is up to date if each entry is at its target,
646 * or is 1 greater than its target.
647 * (If it is 1 greater than the target, '/' will be printed, so it
648 * will look correct on the next row.)
650 for (i = 0; i < graph->mapping_size; i++) {
651 int target = graph->mapping[i];
652 if (target < 0)
653 continue;
654 if (target == (i / 2))
655 continue;
656 return 0;
659 return 1;
662 static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb,
663 int chars_written)
666 * Add additional spaces to the end of the strbuf, so that all
667 * lines for a particular commit have the same width.
669 * This way, fields printed to the right of the graph will remain
670 * aligned for the entire commit.
672 int extra;
673 if (chars_written >= graph->width)
674 return;
676 extra = graph->width - chars_written;
677 strbuf_addf(sb, "%*s", (int) extra, "");
680 static void graph_output_padding_line(struct git_graph *graph,
681 struct strbuf *sb)
683 int i;
686 * We could conceivable be called with a NULL commit
687 * if our caller has a bug, and invokes graph_next_line()
688 * immediately after graph_init(), without first calling
689 * graph_update(). Return without outputting anything in this
690 * case.
692 if (!graph->commit)
693 return;
696 * Output a padding row, that leaves all branch lines unchanged
698 for (i = 0; i < graph->num_new_columns; i++) {
699 strbuf_write_column(sb, &graph->new_columns[i], '|');
700 strbuf_addch(sb, ' ');
703 graph_pad_horizontally(graph, sb, graph->num_new_columns * 2);
707 int graph_width(struct git_graph *graph)
709 return graph->width;
713 static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
716 * Output an ellipsis to indicate that a portion
717 * of the graph is missing.
719 strbuf_addstr(sb, "...");
720 graph_pad_horizontally(graph, sb, 3);
722 if (graph->num_parents >= 3 &&
723 graph->commit_index < (graph->num_columns - 1))
724 graph_update_state(graph, GRAPH_PRE_COMMIT);
725 else
726 graph_update_state(graph, GRAPH_COMMIT);
729 static void graph_output_pre_commit_line(struct git_graph *graph,
730 struct strbuf *sb)
732 int num_expansion_rows;
733 int i, seen_this;
734 int chars_written;
737 * This function formats a row that increases the space around a commit
738 * with multiple parents, to make room for it. It should only be
739 * called when there are 3 or more parents.
741 * We need 2 extra rows for every parent over 2.
743 assert(graph->num_parents >= 3);
744 num_expansion_rows = (graph->num_parents - 2) * 2;
747 * graph->expansion_row tracks the current expansion row we are on.
748 * It should be in the range [0, num_expansion_rows - 1]
750 assert(0 <= graph->expansion_row &&
751 graph->expansion_row < num_expansion_rows);
754 * Output the row
756 seen_this = 0;
757 chars_written = 0;
758 for (i = 0; i < graph->num_columns; i++) {
759 struct column *col = &graph->columns[i];
760 if (col->commit == graph->commit) {
761 seen_this = 1;
762 strbuf_write_column(sb, col, '|');
763 strbuf_addf(sb, "%*s", graph->expansion_row, "");
764 chars_written += 1 + graph->expansion_row;
765 } else if (seen_this && (graph->expansion_row == 0)) {
767 * This is the first line of the pre-commit output.
768 * If the previous commit was a merge commit and
769 * ended in the GRAPH_POST_MERGE state, all branch
770 * lines after graph->prev_commit_index were
771 * printed as "\" on the previous line. Continue
772 * to print them as "\" on this line. Otherwise,
773 * print the branch lines as "|".
775 if (graph->prev_state == GRAPH_POST_MERGE &&
776 graph->prev_commit_index < i)
777 strbuf_write_column(sb, col, '\\');
778 else
779 strbuf_write_column(sb, col, '|');
780 chars_written++;
781 } else if (seen_this && (graph->expansion_row > 0)) {
782 strbuf_write_column(sb, col, '\\');
783 chars_written++;
784 } else {
785 strbuf_write_column(sb, col, '|');
786 chars_written++;
788 strbuf_addch(sb, ' ');
789 chars_written++;
792 graph_pad_horizontally(graph, sb, chars_written);
795 * Increment graph->expansion_row,
796 * and move to state GRAPH_COMMIT if necessary
798 graph->expansion_row++;
799 if (graph->expansion_row >= num_expansion_rows)
800 graph_update_state(graph, GRAPH_COMMIT);
803 static void graph_output_commit_char(struct git_graph *graph, struct strbuf *sb)
806 * For boundary commits, print 'o'
807 * (We should only see boundary commits when revs->boundary is set.)
809 if (graph->commit->object.flags & BOUNDARY) {
810 assert(graph->revs->boundary);
811 strbuf_addch(sb, 'o');
812 return;
816 * get_revision_mark() handles all other cases without assert()
818 strbuf_addstr(sb, get_revision_mark(graph->revs, graph->commit));
822 * Draw an octopus merge and return the number of characters written.
824 static int graph_draw_octopus_merge(struct git_graph *graph,
825 struct strbuf *sb)
828 * Here dashless_commits represents the number of parents
829 * which don't need to have dashes (because their edges fit
830 * neatly under the commit).
832 const int dashless_commits = 2;
833 int col_num, i;
834 int num_dashes =
835 ((graph->num_parents - dashless_commits) * 2) - 1;
836 for (i = 0; i < num_dashes; i++) {
837 col_num = (i / 2) + dashless_commits + graph->commit_index;
838 strbuf_write_column(sb, &graph->new_columns[col_num], '-');
840 col_num = (i / 2) + dashless_commits + graph->commit_index;
841 strbuf_write_column(sb, &graph->new_columns[col_num], '.');
842 return num_dashes + 1;
845 static void graph_output_commit_line(struct git_graph *graph, struct strbuf *sb)
847 int seen_this = 0;
848 int i, chars_written;
851 * Output the row containing this commit
852 * Iterate up to and including graph->num_columns,
853 * since the current commit may not be in any of the existing
854 * columns. (This happens when the current commit doesn't have any
855 * children that we have already processed.)
857 seen_this = 0;
858 chars_written = 0;
859 for (i = 0; i <= graph->num_columns; i++) {
860 struct column *col = &graph->columns[i];
861 struct commit *col_commit;
862 if (i == graph->num_columns) {
863 if (seen_this)
864 break;
865 col_commit = graph->commit;
866 } else {
867 col_commit = graph->columns[i].commit;
870 if (col_commit == graph->commit) {
871 seen_this = 1;
872 graph_output_commit_char(graph, sb);
873 chars_written++;
875 if (graph->num_parents > 2)
876 chars_written += graph_draw_octopus_merge(graph,
877 sb);
878 } else if (seen_this && (graph->num_parents > 2)) {
879 strbuf_write_column(sb, col, '\\');
880 chars_written++;
881 } else if (seen_this && (graph->num_parents == 2)) {
883 * This is a 2-way merge commit.
884 * There is no GRAPH_PRE_COMMIT stage for 2-way
885 * merges, so this is the first line of output
886 * for this commit. Check to see what the previous
887 * line of output was.
889 * If it was GRAPH_POST_MERGE, the branch line
890 * coming into this commit may have been '\',
891 * and not '|' or '/'. If so, output the branch
892 * line as '\' on this line, instead of '|'. This
893 * makes the output look nicer.
895 if (graph->prev_state == GRAPH_POST_MERGE &&
896 graph->prev_commit_index < i)
897 strbuf_write_column(sb, col, '\\');
898 else
899 strbuf_write_column(sb, col, '|');
900 chars_written++;
901 } else {
902 strbuf_write_column(sb, col, '|');
903 chars_written++;
905 strbuf_addch(sb, ' ');
906 chars_written++;
909 graph_pad_horizontally(graph, sb, chars_written);
912 * Update graph->state
914 if (graph->num_parents > 1)
915 graph_update_state(graph, GRAPH_POST_MERGE);
916 else if (graph_is_mapping_correct(graph))
917 graph_update_state(graph, GRAPH_PADDING);
918 else
919 graph_update_state(graph, GRAPH_COLLAPSING);
922 static struct column *find_new_column_by_commit(struct git_graph *graph,
923 struct commit *commit)
925 int i;
926 for (i = 0; i < graph->num_new_columns; i++) {
927 if (graph->new_columns[i].commit == commit)
928 return &graph->new_columns[i];
930 return NULL;
933 static void graph_output_post_merge_line(struct git_graph *graph, struct strbuf *sb)
935 int seen_this = 0;
936 int i, j, chars_written;
939 * Output the post-merge row
941 chars_written = 0;
942 for (i = 0; i <= graph->num_columns; i++) {
943 struct column *col = &graph->columns[i];
944 struct commit *col_commit;
945 if (i == graph->num_columns) {
946 if (seen_this)
947 break;
948 col_commit = graph->commit;
949 } else {
950 col_commit = col->commit;
953 if (col_commit == graph->commit) {
955 * Since the current commit is a merge find
956 * the columns for the parent commits in
957 * new_columns and use those to format the
958 * edges.
960 struct commit_list *parents = NULL;
961 struct column *par_column;
962 seen_this = 1;
963 parents = first_interesting_parent(graph);
964 assert(parents);
965 par_column = find_new_column_by_commit(graph, parents->item);
966 assert(par_column);
968 strbuf_write_column(sb, par_column, '|');
969 chars_written++;
970 for (j = 0; j < graph->num_parents - 1; j++) {
971 parents = next_interesting_parent(graph, parents);
972 assert(parents);
973 par_column = find_new_column_by_commit(graph, parents->item);
974 assert(par_column);
975 strbuf_write_column(sb, par_column, '\\');
976 strbuf_addch(sb, ' ');
978 chars_written += j * 2;
979 } else if (seen_this) {
980 strbuf_write_column(sb, col, '\\');
981 strbuf_addch(sb, ' ');
982 chars_written += 2;
983 } else {
984 strbuf_write_column(sb, col, '|');
985 strbuf_addch(sb, ' ');
986 chars_written += 2;
990 graph_pad_horizontally(graph, sb, chars_written);
993 * Update graph->state
995 if (graph_is_mapping_correct(graph))
996 graph_update_state(graph, GRAPH_PADDING);
997 else
998 graph_update_state(graph, GRAPH_COLLAPSING);
1001 static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
1003 int i;
1004 int *tmp_mapping;
1005 short used_horizontal = 0;
1006 int horizontal_edge = -1;
1007 int horizontal_edge_target = -1;
1010 * Clear out the new_mapping array
1012 for (i = 0; i < graph->mapping_size; i++)
1013 graph->new_mapping[i] = -1;
1015 for (i = 0; i < graph->mapping_size; i++) {
1016 int target = graph->mapping[i];
1017 if (target < 0)
1018 continue;
1021 * Since update_columns() always inserts the leftmost
1022 * column first, each branch's target location should
1023 * always be either its current location or to the left of
1024 * its current location.
1026 * We never have to move branches to the right. This makes
1027 * the graph much more legible, since whenever branches
1028 * cross, only one is moving directions.
1030 assert(target * 2 <= i);
1032 if (target * 2 == i) {
1034 * This column is already in the
1035 * correct place
1037 assert(graph->new_mapping[i] == -1);
1038 graph->new_mapping[i] = target;
1039 } else if (graph->new_mapping[i - 1] < 0) {
1041 * Nothing is to the left.
1042 * Move to the left by one
1044 graph->new_mapping[i - 1] = target;
1046 * If there isn't already an edge moving horizontally
1047 * select this one.
1049 if (horizontal_edge == -1) {
1050 int j;
1051 horizontal_edge = i;
1052 horizontal_edge_target = target;
1054 * The variable target is the index of the graph
1055 * column, and therefore target*2+3 is the
1056 * actual screen column of the first horizontal
1057 * line.
1059 for (j = (target * 2)+3; j < (i - 2); j += 2)
1060 graph->new_mapping[j] = target;
1062 } else if (graph->new_mapping[i - 1] == target) {
1064 * There is a branch line to our left
1065 * already, and it is our target. We
1066 * combine with this line, since we share
1067 * the same parent commit.
1069 * We don't have to add anything to the
1070 * output or new_mapping, since the
1071 * existing branch line has already taken
1072 * care of it.
1074 } else {
1076 * There is a branch line to our left,
1077 * but it isn't our target. We need to
1078 * cross over it.
1080 * The space just to the left of this
1081 * branch should always be empty.
1083 * The branch to the left of that space
1084 * should be our eventual target.
1086 assert(graph->new_mapping[i - 1] > target);
1087 assert(graph->new_mapping[i - 2] < 0);
1088 assert(graph->new_mapping[i - 3] == target);
1089 graph->new_mapping[i - 2] = target;
1091 * Mark this branch as the horizontal edge to
1092 * prevent any other edges from moving
1093 * horizontally.
1095 if (horizontal_edge == -1)
1096 horizontal_edge = i;
1101 * The new mapping may be 1 smaller than the old mapping
1103 if (graph->new_mapping[graph->mapping_size - 1] < 0)
1104 graph->mapping_size--;
1107 * Output out a line based on the new mapping info
1109 for (i = 0; i < graph->mapping_size; i++) {
1110 int target = graph->new_mapping[i];
1111 if (target < 0)
1112 strbuf_addch(sb, ' ');
1113 else if (target * 2 == i)
1114 strbuf_write_column(sb, &graph->new_columns[target], '|');
1115 else if (target == horizontal_edge_target &&
1116 i != horizontal_edge - 1) {
1118 * Set the mappings for all but the
1119 * first segment to -1 so that they
1120 * won't continue into the next line.
1122 if (i != (target * 2)+3)
1123 graph->new_mapping[i] = -1;
1124 used_horizontal = 1;
1125 strbuf_write_column(sb, &graph->new_columns[target], '_');
1126 } else {
1127 if (used_horizontal && i < horizontal_edge)
1128 graph->new_mapping[i] = -1;
1129 strbuf_write_column(sb, &graph->new_columns[target], '/');
1134 graph_pad_horizontally(graph, sb, graph->mapping_size);
1137 * Swap mapping and new_mapping
1139 tmp_mapping = graph->mapping;
1140 graph->mapping = graph->new_mapping;
1141 graph->new_mapping = tmp_mapping;
1144 * If graph->mapping indicates that all of the branch lines
1145 * are already in the correct positions, we are done.
1146 * Otherwise, we need to collapse some branch lines together.
1148 if (graph_is_mapping_correct(graph))
1149 graph_update_state(graph, GRAPH_PADDING);
1152 int graph_next_line(struct git_graph *graph, struct strbuf *sb)
1154 switch (graph->state) {
1155 case GRAPH_PADDING:
1156 graph_output_padding_line(graph, sb);
1157 return 0;
1158 case GRAPH_SKIP:
1159 graph_output_skip_line(graph, sb);
1160 return 0;
1161 case GRAPH_PRE_COMMIT:
1162 graph_output_pre_commit_line(graph, sb);
1163 return 0;
1164 case GRAPH_COMMIT:
1165 graph_output_commit_line(graph, sb);
1166 return 1;
1167 case GRAPH_POST_MERGE:
1168 graph_output_post_merge_line(graph, sb);
1169 return 0;
1170 case GRAPH_COLLAPSING:
1171 graph_output_collapsing_line(graph, sb);
1172 return 0;
1175 assert(0);
1176 return 0;
1179 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
1181 int i;
1183 if (graph->state != GRAPH_COMMIT) {
1184 graph_next_line(graph, sb);
1185 return;
1189 * Output the row containing this commit
1190 * Iterate up to and including graph->num_columns,
1191 * since the current commit may not be in any of the existing
1192 * columns. (This happens when the current commit doesn't have any
1193 * children that we have already processed.)
1195 for (i = 0; i < graph->num_columns; i++) {
1196 struct column *col = &graph->columns[i];
1197 strbuf_write_column(sb, col, '|');
1198 if (col->commit == graph->commit && graph->num_parents > 2)
1199 strbuf_addchars(sb, ' ', (graph->num_parents - 2) * 2);
1200 else
1201 strbuf_addch(sb, ' ');
1204 graph_pad_horizontally(graph, sb, graph->num_columns);
1207 * Update graph->prev_state since we have output a padding line
1209 graph->prev_state = GRAPH_PADDING;
1212 int graph_is_commit_finished(struct git_graph const *graph)
1214 return (graph->state == GRAPH_PADDING);
1217 void graph_show_commit(struct git_graph *graph)
1219 struct strbuf msgbuf = STRBUF_INIT;
1220 int shown_commit_line = 0;
1222 if (!graph)
1223 return;
1226 * When showing a diff of a merge against each of its parents, we
1227 * are called once for each parent without graph_update having been
1228 * called. In this case, simply output a single padding line.
1230 if (graph_is_commit_finished(graph)) {
1231 graph_show_padding(graph);
1232 shown_commit_line = 1;
1235 while (!shown_commit_line && !graph_is_commit_finished(graph)) {
1236 shown_commit_line = graph_next_line(graph, &msgbuf);
1237 fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
1238 graph->revs->diffopt.file);
1239 if (!shown_commit_line)
1240 putc('\n', graph->revs->diffopt.file);
1241 strbuf_setlen(&msgbuf, 0);
1244 strbuf_release(&msgbuf);
1247 void graph_show_oneline(struct git_graph *graph)
1249 struct strbuf msgbuf = STRBUF_INIT;
1251 if (!graph)
1252 return;
1254 graph_next_line(graph, &msgbuf);
1255 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
1256 strbuf_release(&msgbuf);
1259 void graph_show_padding(struct git_graph *graph)
1261 struct strbuf msgbuf = STRBUF_INIT;
1263 if (!graph)
1264 return;
1266 graph_padding_line(graph, &msgbuf);
1267 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
1268 strbuf_release(&msgbuf);
1271 int graph_show_remainder(struct git_graph *graph)
1273 struct strbuf msgbuf = STRBUF_INIT;
1274 int shown = 0;
1276 if (!graph)
1277 return 0;
1279 if (graph_is_commit_finished(graph))
1280 return 0;
1282 for (;;) {
1283 graph_next_line(graph, &msgbuf);
1284 fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
1285 graph->revs->diffopt.file);
1286 strbuf_setlen(&msgbuf, 0);
1287 shown = 1;
1289 if (!graph_is_commit_finished(graph))
1290 putc('\n', graph->revs->diffopt.file);
1291 else
1292 break;
1294 strbuf_release(&msgbuf);
1296 return shown;
1300 static void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb)
1302 char *p;
1304 if (!graph) {
1305 fwrite(sb->buf, sizeof(char), sb->len,
1306 graph->revs->diffopt.file);
1307 return;
1311 * Print the strbuf line by line,
1312 * and display the graph info before each line but the first.
1314 p = sb->buf;
1315 while (p) {
1316 size_t len;
1317 char *next_p = strchr(p, '\n');
1318 if (next_p) {
1319 next_p++;
1320 len = next_p - p;
1321 } else {
1322 len = (sb->buf + sb->len) - p;
1324 fwrite(p, sizeof(char), len, graph->revs->diffopt.file);
1325 if (next_p && *next_p != '\0')
1326 graph_show_oneline(graph);
1327 p = next_p;
1331 void graph_show_commit_msg(struct git_graph *graph,
1332 struct strbuf const *sb)
1334 int newline_terminated;
1336 if (!graph) {
1338 * If there's no graph, just print the message buffer.
1340 * The message buffer for CMIT_FMT_ONELINE and
1341 * CMIT_FMT_USERFORMAT are already missing a terminating
1342 * newline. All of the other formats should have it.
1344 fwrite(sb->buf, sizeof(char), sb->len,
1345 graph->revs->diffopt.file);
1346 return;
1349 newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
1352 * Show the commit message
1354 graph_show_strbuf(graph, sb);
1357 * If there is more output needed for this commit, show it now
1359 if (!graph_is_commit_finished(graph)) {
1361 * If sb doesn't have a terminating newline, print one now,
1362 * so we can start the remainder of the graph output on a
1363 * new line.
1365 if (!newline_terminated)
1366 putc('\n', graph->revs->diffopt.file);
1368 graph_show_remainder(graph);
1371 * If sb ends with a newline, our output should too.
1373 if (newline_terminated)
1374 putc('\n', graph->revs->diffopt.file);