graph: smooth appearance of collapsing edges on commit lines
[git] / graph.c
1 #include "cache.h"
2 #include "config.h"
3 #include "commit.h"
4 #include "color.h"
5 #include "graph.h"
6 #include "revision.h"
7 #include "argv-array.h"
8
9 /* Internal API */
10
11 /*
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.
17  */
18 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb);
19
20 /*
21  * Print a strbuf.  If the graph is non-NULL, all lines but the first will be
22  * prefixed with the graph output.
23  *
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.
27  *
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().
32  *
33  * Note that unlike some other graph display functions, you must pass the file
34  * handle directly. It is assumed that this is the same file handle as the
35  * file specified by the graph diff options. This is necessary so that
36  * graph_show_strbuf can be called even with a NULL graph.
37  */
38 static void graph_show_strbuf(struct git_graph *graph,
39                               FILE *file,
40                               struct strbuf const *sb);
41
42 /*
43  * TODO:
44  * - Limit the number of columns, similar to the way gitk does.
45  *   If we reach more than a specified number of columns, omit
46  *   sections of some columns.
47  */
48
49 struct column {
50         /*
51          * The parent commit of this column.
52          */
53         struct commit *commit;
54         /*
55          * The color to (optionally) print this column in.  This is an
56          * index into column_colors.
57          */
58         unsigned short color;
59 };
60
61 enum graph_state {
62         GRAPH_PADDING,
63         GRAPH_SKIP,
64         GRAPH_PRE_COMMIT,
65         GRAPH_COMMIT,
66         GRAPH_POST_MERGE,
67         GRAPH_COLLAPSING
68 };
69
70 static void graph_show_line_prefix(const struct diff_options *diffopt)
71 {
72         if (!diffopt || !diffopt->line_prefix)
73                 return;
74
75         fwrite(diffopt->line_prefix,
76                sizeof(char),
77                diffopt->line_prefix_length,
78                diffopt->file);
79 }
80
81 static const char **column_colors;
82 static unsigned short column_colors_max;
83
84 static void parse_graph_colors_config(struct argv_array *colors, const char *string)
85 {
86         const char *end, *start;
87
88         start = string;
89         end = string + strlen(string);
90         while (start < end) {
91                 const char *comma = strchrnul(start, ',');
92                 char color[COLOR_MAXLEN];
93
94                 if (!color_parse_mem(start, comma - start, color))
95                         argv_array_push(colors, color);
96                 else
97                         warning(_("ignore invalid color '%.*s' in log.graphColors"),
98                                 (int)(comma - start), start);
99                 start = comma + 1;
100         }
101         argv_array_push(colors, GIT_COLOR_RESET);
102 }
103
104 void graph_set_column_colors(const char **colors, unsigned short colors_max)
105 {
106         column_colors = colors;
107         column_colors_max = colors_max;
108 }
109
110 static const char *column_get_color_code(unsigned short color)
111 {
112         return column_colors[color];
113 }
114
115 struct graph_line {
116         struct strbuf *buf;
117         size_t width;
118 };
119
120 static inline void graph_line_addch(struct graph_line *line, int c)
121 {
122         strbuf_addch(line->buf, c);
123         line->width++;
124 }
125
126 static inline void graph_line_addchars(struct graph_line *line, int c, size_t n)
127 {
128         strbuf_addchars(line->buf, c, n);
129         line->width += n;
130 }
131
132 static inline void graph_line_addstr(struct graph_line *line, const char *s)
133 {
134         strbuf_addstr(line->buf, s);
135         line->width += strlen(s);
136 }
137
138 static inline void graph_line_addcolor(struct graph_line *line, unsigned short color)
139 {
140         strbuf_addstr(line->buf, column_get_color_code(color));
141 }
142
143 static void graph_line_write_column(struct graph_line *line, const struct column *c,
144                                     char col_char)
145 {
146         if (c->color < column_colors_max)
147                 graph_line_addcolor(line, c->color);
148         graph_line_addch(line, col_char);
149         if (c->color < column_colors_max)
150                 graph_line_addcolor(line, column_colors_max);
151 }
152
153 struct git_graph {
154         /*
155          * The commit currently being processed
156          */
157         struct commit *commit;
158         /* The rev-info used for the current traversal */
159         struct rev_info *revs;
160         /*
161          * The number of interesting parents that this commit has.
162          *
163          * Note that this is not the same as the actual number of parents.
164          * This count excludes parents that won't be printed in the graph
165          * output, as determined by graph_is_interesting().
166          */
167         int num_parents;
168         /*
169          * The width of the graph output for this commit.
170          * All rows for this commit are padded to this width, so that
171          * messages printed after the graph output are aligned.
172          */
173         int width;
174         /*
175          * The next expansion row to print
176          * when state is GRAPH_PRE_COMMIT
177          */
178         int expansion_row;
179         /*
180          * The current output state.
181          * This tells us what kind of line graph_next_line() should output.
182          */
183         enum graph_state state;
184         /*
185          * The output state for the previous line of output.
186          * This is primarily used to determine how the first merge line
187          * should appear, based on the last line of the previous commit.
188          */
189         enum graph_state prev_state;
190         /*
191          * The index of the column that refers to this commit.
192          *
193          * If none of the incoming columns refer to this commit,
194          * this will be equal to num_columns.
195          */
196         int commit_index;
197         /*
198          * The commit_index for the previously displayed commit.
199          *
200          * This is used to determine how the first line of a merge
201          * graph output should appear, based on the last line of the
202          * previous commit.
203          */
204         int prev_commit_index;
205         /*
206          * Which layout variant to use to display merge commits. If the
207          * commit's first parent is known to be in a column to the left of the
208          * merge, then this value is 0 and we use the layout on the left.
209          * Otherwise, the value is 1 and the layout on the right is used. This
210          * field tells us how many columns the first parent occupies.
211          *
212          *              0)                      1)
213          *
214          *              | | | *-.               | | *---.
215          *              | |_|/|\ \              | | |\ \ \
216          *              |/| | | | |             | | | | | *
217          */
218         int merge_layout;
219         /*
220          * The number of columns added to the graph by the current commit. For
221          * 2-way and octopus merges, this is is usually one less than the
222          * number of parents:
223          *
224          *              | | |                   | |    \
225          *              | * |                   | *---. \
226          *              | |\ \                  | |\ \ \ \
227          *              | | | |                 | | | | | |
228          *
229          *              num_parents: 2          num_parents: 4
230          *              edges_added: 1          edges_added: 3
231          *
232          * For left-skewed merges, the first parent fuses with its neighbor and
233          * so one less column is added:
234          *
235          *              | | |                   | |  \
236          *              | * |                   | *-. \
237          *              |/| |                   |/|\ \ \
238          *              | | |                   | | | | |
239          *
240          *              num_parents: 2          num_parents: 4
241          *              edges_added: 0          edges_added: 2
242          *
243          * This number determines how edges to the right of the merge are
244          * displayed in commit and post-merge lines; if no columns have been
245          * added then a vertical line should be used where a right-tracking
246          * line would otherwise be used.
247          *
248          *              | * \                   | * |
249          *              | |\ \                  |/| |
250          *              | | * \                 | * |
251          */
252         int edges_added;
253         /*
254          * The number of columns added by the previous commit, which is used to
255          * smooth edges appearing to the right of a commit in a commit line
256          * following a post-merge line.
257          */
258         int prev_edges_added;
259         /*
260          * The maximum number of columns that can be stored in the columns
261          * and new_columns arrays.  This is also half the number of entries
262          * that can be stored in the mapping and old_mapping arrays.
263          */
264         int column_capacity;
265         /*
266          * The number of columns (also called "branch lines" in some places)
267          */
268         int num_columns;
269         /*
270          * The number of columns in the new_columns array
271          */
272         int num_new_columns;
273         /*
274          * The number of entries in the mapping array
275          */
276         int mapping_size;
277         /*
278          * The column state before we output the current commit.
279          */
280         struct column *columns;
281         /*
282          * The new column state after we output the current commit.
283          * Only valid when state is GRAPH_COLLAPSING.
284          */
285         struct column *new_columns;
286         /*
287          * An array that tracks the current state of each
288          * character in the output line during state GRAPH_COLLAPSING.
289          * Each entry is -1 if this character is empty, or a non-negative
290          * integer if the character contains a branch line.  The value of
291          * the integer indicates the target position for this branch line.
292          * (I.e., this array maps the current column positions to their
293          * desired positions.)
294          *
295          * The maximum capacity of this array is always
296          * sizeof(int) * 2 * column_capacity.
297          */
298         int *mapping;
299         /*
300          * A copy of the contents of the mapping array from the last commit,
301          * which we use to improve the display of columns that are tracking
302          * from right to left through a commit line.  We also use this to
303          * avoid allocating a fresh array when we compute the next mapping.
304          */
305         int *old_mapping;
306         /*
307          * The current default column color being used.  This is
308          * stored as an index into the array column_colors.
309          */
310         unsigned short default_column_color;
311 };
312
313 static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void *data)
314 {
315         struct git_graph *graph = data;
316         static struct strbuf msgbuf = STRBUF_INIT;
317
318         assert(opt);
319
320         strbuf_reset(&msgbuf);
321         if (opt->line_prefix)
322                 strbuf_add(&msgbuf, opt->line_prefix,
323                            opt->line_prefix_length);
324         if (graph)
325                 graph_padding_line(graph, &msgbuf);
326         return &msgbuf;
327 }
328
329 static const struct diff_options *default_diffopt;
330
331 void graph_setup_line_prefix(struct diff_options *diffopt)
332 {
333         default_diffopt = diffopt;
334
335         /* setup an output prefix callback if necessary */
336         if (diffopt && !diffopt->output_prefix)
337                 diffopt->output_prefix = diff_output_prefix_callback;
338 }
339
340
341 struct git_graph *graph_init(struct rev_info *opt)
342 {
343         struct git_graph *graph = xmalloc(sizeof(struct git_graph));
344
345         if (!column_colors) {
346                 char *string;
347                 if (git_config_get_string("log.graphcolors", &string)) {
348                         /* not configured -- use default */
349                         graph_set_column_colors(column_colors_ansi,
350                                                 column_colors_ansi_max);
351                 } else {
352                         static struct argv_array custom_colors = ARGV_ARRAY_INIT;
353                         argv_array_clear(&custom_colors);
354                         parse_graph_colors_config(&custom_colors, string);
355                         free(string);
356                         /* graph_set_column_colors takes a max-index, not a count */
357                         graph_set_column_colors(custom_colors.argv,
358                                                 custom_colors.argc - 1);
359                 }
360         }
361
362         graph->commit = NULL;
363         graph->revs = opt;
364         graph->num_parents = 0;
365         graph->expansion_row = 0;
366         graph->state = GRAPH_PADDING;
367         graph->prev_state = GRAPH_PADDING;
368         graph->commit_index = 0;
369         graph->prev_commit_index = 0;
370         graph->merge_layout = 0;
371         graph->edges_added = 0;
372         graph->prev_edges_added = 0;
373         graph->num_columns = 0;
374         graph->num_new_columns = 0;
375         graph->mapping_size = 0;
376         /*
377          * Start the column color at the maximum value, since we'll
378          * always increment it for the first commit we output.
379          * This way we start at 0 for the first commit.
380          */
381         graph->default_column_color = column_colors_max - 1;
382
383         /*
384          * Allocate a reasonably large default number of columns
385          * We'll automatically grow columns later if we need more room.
386          */
387         graph->column_capacity = 30;
388         ALLOC_ARRAY(graph->columns, graph->column_capacity);
389         ALLOC_ARRAY(graph->new_columns, graph->column_capacity);
390         ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity);
391         ALLOC_ARRAY(graph->old_mapping, 2 * graph->column_capacity);
392
393         /*
394          * The diff output prefix callback, with this we can make
395          * all the diff output to align with the graph lines.
396          */
397         opt->diffopt.output_prefix = diff_output_prefix_callback;
398         opt->diffopt.output_prefix_data = graph;
399
400         return graph;
401 }
402
403 static void graph_update_state(struct git_graph *graph, enum graph_state s)
404 {
405         graph->prev_state = graph->state;
406         graph->state = s;
407 }
408
409 static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
410 {
411         if (graph->column_capacity >= num_columns)
412                 return;
413
414         do {
415                 graph->column_capacity *= 2;
416         } while (graph->column_capacity < num_columns);
417
418         REALLOC_ARRAY(graph->columns, graph->column_capacity);
419         REALLOC_ARRAY(graph->new_columns, graph->column_capacity);
420         REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2);
421         REALLOC_ARRAY(graph->old_mapping, graph->column_capacity * 2);
422 }
423
424 /*
425  * Returns 1 if the commit will be printed in the graph output,
426  * and 0 otherwise.
427  */
428 static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
429 {
430         /*
431          * If revs->boundary is set, commits whose children have
432          * been shown are always interesting, even if they have the
433          * UNINTERESTING or TREESAME flags set.
434          */
435         if (graph->revs && graph->revs->boundary) {
436                 if (commit->object.flags & CHILD_SHOWN)
437                         return 1;
438         }
439
440         /*
441          * Otherwise, use get_commit_action() to see if this commit is
442          * interesting
443          */
444         return get_commit_action(graph->revs, commit) == commit_show;
445 }
446
447 static struct commit_list *next_interesting_parent(struct git_graph *graph,
448                                                    struct commit_list *orig)
449 {
450         struct commit_list *list;
451
452         /*
453          * If revs->first_parent_only is set, only the first
454          * parent is interesting.  None of the others are.
455          */
456         if (graph->revs->first_parent_only)
457                 return NULL;
458
459         /*
460          * Return the next interesting commit after orig
461          */
462         for (list = orig->next; list; list = list->next) {
463                 if (graph_is_interesting(graph, list->item))
464                         return list;
465         }
466
467         return NULL;
468 }
469
470 static struct commit_list *first_interesting_parent(struct git_graph *graph)
471 {
472         struct commit_list *parents = graph->commit->parents;
473
474         /*
475          * If this commit has no parents, ignore it
476          */
477         if (!parents)
478                 return NULL;
479
480         /*
481          * If the first parent is interesting, return it
482          */
483         if (graph_is_interesting(graph, parents->item))
484                 return parents;
485
486         /*
487          * Otherwise, call next_interesting_parent() to get
488          * the next interesting parent
489          */
490         return next_interesting_parent(graph, parents);
491 }
492
493 static unsigned short graph_get_current_column_color(const struct git_graph *graph)
494 {
495         if (!want_color(graph->revs->diffopt.use_color))
496                 return column_colors_max;
497         return graph->default_column_color;
498 }
499
500 /*
501  * Update the graph's default column color.
502  */
503 static void graph_increment_column_color(struct git_graph *graph)
504 {
505         graph->default_column_color = (graph->default_column_color + 1) %
506                 column_colors_max;
507 }
508
509 static unsigned short graph_find_commit_color(const struct git_graph *graph,
510                                               const struct commit *commit)
511 {
512         int i;
513         for (i = 0; i < graph->num_columns; i++) {
514                 if (graph->columns[i].commit == commit)
515                         return graph->columns[i].color;
516         }
517         return graph_get_current_column_color(graph);
518 }
519
520 static int graph_find_new_column_by_commit(struct git_graph *graph,
521                                            struct commit *commit)
522 {
523         int i;
524         for (i = 0; i < graph->num_new_columns; i++) {
525                 if (graph->new_columns[i].commit == commit)
526                         return i;
527         }
528         return -1;
529 }
530
531 static void graph_insert_into_new_columns(struct git_graph *graph,
532                                           struct commit *commit,
533                                           int idx)
534 {
535         int i = graph_find_new_column_by_commit(graph, commit);
536         int mapping_idx;
537
538         /*
539          * If the commit is not already in the new_columns array, then add it
540          * and record it as being in the final column.
541          */
542         if (i < 0) {
543                 i = graph->num_new_columns++;
544                 graph->new_columns[i].commit = commit;
545                 graph->new_columns[i].color = graph_find_commit_color(graph, commit);
546         }
547
548         if (graph->num_parents > 1 && idx > -1 && graph->merge_layout == -1) {
549                 /*
550                  * If this is the first parent of a merge, choose a layout for
551                  * the merge line based on whether the parent appears in a
552                  * column to the left of the merge
553                  */
554                 int dist, shift;
555
556                 dist = idx - i;
557                 shift = (dist > 1) ? 2 * dist - 3 : 1;
558
559                 graph->merge_layout = (dist > 0) ? 0 : 1;
560                 mapping_idx = graph->width + (graph->merge_layout - 1) * shift;
561                 graph->width += 2 * graph->merge_layout;
562         } else {
563                 mapping_idx = graph->width;
564                 graph->width += 2;
565         }
566
567         graph->mapping[mapping_idx] = i;
568 }
569
570 static void graph_update_columns(struct git_graph *graph)
571 {
572         struct commit_list *parent;
573         int max_new_columns;
574         int i, seen_this, is_commit_in_columns;
575
576         /*
577          * Swap graph->columns with graph->new_columns
578          * graph->columns contains the state for the previous commit,
579          * and new_columns now contains the state for our commit.
580          *
581          * We'll re-use the old columns array as storage to compute the new
582          * columns list for the commit after this one.
583          */
584         SWAP(graph->columns, graph->new_columns);
585         graph->num_columns = graph->num_new_columns;
586         graph->num_new_columns = 0;
587
588         /*
589          * Now update new_columns and mapping with the information for the
590          * commit after this one.
591          *
592          * First, make sure we have enough room.  At most, there will
593          * be graph->num_columns + graph->num_parents columns for the next
594          * commit.
595          */
596         max_new_columns = graph->num_columns + graph->num_parents;
597         graph_ensure_capacity(graph, max_new_columns);
598
599         /*
600          * Clear out graph->mapping
601          */
602         graph->mapping_size = 2 * max_new_columns;
603         for (i = 0; i < graph->mapping_size; i++)
604                 graph->mapping[i] = -1;
605
606         graph->width = 0;
607
608         /*
609          * Populate graph->new_columns and graph->mapping
610          *
611          * Some of the parents of this commit may already be in
612          * graph->columns.  If so, graph->new_columns should only contain a
613          * single entry for each such commit.  graph->mapping should
614          * contain information about where each current branch line is
615          * supposed to end up after the collapsing is performed.
616          */
617         seen_this = 0;
618         is_commit_in_columns = 1;
619         for (i = 0; i <= graph->num_columns; i++) {
620                 struct commit *col_commit;
621                 if (i == graph->num_columns) {
622                         if (seen_this)
623                                 break;
624                         is_commit_in_columns = 0;
625                         col_commit = graph->commit;
626                 } else {
627                         col_commit = graph->columns[i].commit;
628                 }
629
630                 if (col_commit == graph->commit) {
631                         seen_this = 1;
632                         graph->commit_index = i;
633                         graph->merge_layout = -1;
634                         for (parent = first_interesting_parent(graph);
635                              parent;
636                              parent = next_interesting_parent(graph, parent)) {
637                                 /*
638                                  * If this is a merge, or the start of a new
639                                  * childless column, increment the current
640                                  * color.
641                                  */
642                                 if (graph->num_parents > 1 ||
643                                     !is_commit_in_columns) {
644                                         graph_increment_column_color(graph);
645                                 }
646                                 graph_insert_into_new_columns(graph, parent->item, i);
647                         }
648                         /*
649                          * We always need to increment graph->width by at
650                          * least 2, even if it has no interesting parents.
651                          * The current commit always takes up at least 2
652                          * spaces.
653                          */
654                         if (graph->num_parents == 0)
655                                 graph->width += 2;
656                 } else {
657                         graph_insert_into_new_columns(graph, col_commit, -1);
658                 }
659         }
660
661         /*
662          * Shrink mapping_size to be the minimum necessary
663          */
664         while (graph->mapping_size > 1 &&
665                graph->mapping[graph->mapping_size - 1] < 0)
666                 graph->mapping_size--;
667 }
668
669 static int graph_num_expansion_rows(struct git_graph *graph)
670 {
671         /*
672          * Normally, we need two expansion rows for each dashed parent line from
673          * an octopus merge:
674          *
675          *              | *
676          *              | |\
677          *              | | \
678          *              | |  \
679          *              | *-. \
680          *              | |\ \ \
681          *
682          * If the merge is skewed to the left, then its parents occupy one less
683          * column, and we don't need as many expansion rows to route around it;
684          * in some cases that means we don't need any expansion rows at all:
685          *
686          *              | *
687          *              | |\
688          *              | * \
689          *              |/|\ \
690          */
691         return (graph->num_parents + graph->merge_layout - 3) * 2;
692 }
693
694 static int graph_needs_pre_commit_line(struct git_graph *graph)
695 {
696         return graph->num_parents >= 3 &&
697                graph->commit_index < (graph->num_columns - 1) &&
698                graph->expansion_row < graph_num_expansion_rows(graph);
699 }
700
701 void graph_update(struct git_graph *graph, struct commit *commit)
702 {
703         struct commit_list *parent;
704
705         /*
706          * Set the new commit
707          */
708         graph->commit = commit;
709
710         /*
711          * Count how many interesting parents this commit has
712          */
713         graph->num_parents = 0;
714         for (parent = first_interesting_parent(graph);
715              parent;
716              parent = next_interesting_parent(graph, parent))
717         {
718                 graph->num_parents++;
719         }
720
721         /*
722          * Store the old commit_index in prev_commit_index.
723          * graph_update_columns() will update graph->commit_index for this
724          * commit.
725          */
726         graph->prev_commit_index = graph->commit_index;
727
728         /*
729          * Call graph_update_columns() to update
730          * columns, new_columns, and mapping.
731          */
732         graph_update_columns(graph);
733
734         graph->prev_edges_added = graph->edges_added;
735         graph->edges_added = graph->num_parents + graph->merge_layout - 2;
736
737         graph->expansion_row = 0;
738
739         /*
740          * Update graph->state.
741          * Note that we don't call graph_update_state() here, since
742          * we don't want to update graph->prev_state.  No line for
743          * graph->state was ever printed.
744          *
745          * If the previous commit didn't get to the GRAPH_PADDING state,
746          * it never finished its output.  Goto GRAPH_SKIP, to print out
747          * a line to indicate that portion of the graph is missing.
748          *
749          * If there are 3 or more parents, we may need to print extra rows
750          * before the commit, to expand the branch lines around it and make
751          * room for it.  We need to do this only if there is a branch row
752          * (or more) to the right of this commit.
753          *
754          * If there are less than 3 parents, we can immediately print the
755          * commit line.
756          */
757         if (graph->state != GRAPH_PADDING)
758                 graph->state = GRAPH_SKIP;
759         else if (graph_needs_pre_commit_line(graph))
760                 graph->state = GRAPH_PRE_COMMIT;
761         else
762                 graph->state = GRAPH_COMMIT;
763 }
764
765 static int graph_is_mapping_correct(struct git_graph *graph)
766 {
767         int i;
768
769         /*
770          * The mapping is up to date if each entry is at its target,
771          * or is 1 greater than its target.
772          * (If it is 1 greater than the target, '/' will be printed, so it
773          * will look correct on the next row.)
774          */
775         for (i = 0; i < graph->mapping_size; i++) {
776                 int target = graph->mapping[i];
777                 if (target < 0)
778                         continue;
779                 if (target == (i / 2))
780                         continue;
781                 return 0;
782         }
783
784         return 1;
785 }
786
787 static void graph_pad_horizontally(struct git_graph *graph, struct graph_line *line)
788 {
789         /*
790          * Add additional spaces to the end of the strbuf, so that all
791          * lines for a particular commit have the same width.
792          *
793          * This way, fields printed to the right of the graph will remain
794          * aligned for the entire commit.
795          */
796         if (line->width < graph->width)
797                 graph_line_addchars(line, ' ', graph->width - line->width);
798 }
799
800 static void graph_output_padding_line(struct git_graph *graph,
801                                       struct graph_line *line)
802 {
803         int i;
804
805         /*
806          * Output a padding row, that leaves all branch lines unchanged
807          */
808         for (i = 0; i < graph->num_new_columns; i++) {
809                 graph_line_write_column(line, &graph->new_columns[i], '|');
810                 graph_line_addch(line, ' ');
811         }
812 }
813
814
815 int graph_width(struct git_graph *graph)
816 {
817         return graph->width;
818 }
819
820
821 static void graph_output_skip_line(struct git_graph *graph, struct graph_line *line)
822 {
823         /*
824          * Output an ellipsis to indicate that a portion
825          * of the graph is missing.
826          */
827         graph_line_addstr(line, "...");
828
829         if (graph_needs_pre_commit_line(graph))
830                 graph_update_state(graph, GRAPH_PRE_COMMIT);
831         else
832                 graph_update_state(graph, GRAPH_COMMIT);
833 }
834
835 static void graph_output_pre_commit_line(struct git_graph *graph,
836                                          struct graph_line *line)
837 {
838         int i, seen_this;
839
840         /*
841          * This function formats a row that increases the space around a commit
842          * with multiple parents, to make room for it.  It should only be
843          * called when there are 3 or more parents.
844          *
845          * We need 2 extra rows for every parent over 2.
846          */
847         assert(graph->num_parents >= 3);
848
849         /*
850          * graph->expansion_row tracks the current expansion row we are on.
851          * It should be in the range [0, num_expansion_rows - 1]
852          */
853         assert(0 <= graph->expansion_row &&
854                graph->expansion_row < graph_num_expansion_rows(graph));
855
856         /*
857          * Output the row
858          */
859         seen_this = 0;
860         for (i = 0; i < graph->num_columns; i++) {
861                 struct column *col = &graph->columns[i];
862                 if (col->commit == graph->commit) {
863                         seen_this = 1;
864                         graph_line_write_column(line, col, '|');
865                         graph_line_addchars(line, ' ', graph->expansion_row);
866                 } else if (seen_this && (graph->expansion_row == 0)) {
867                         /*
868                          * This is the first line of the pre-commit output.
869                          * If the previous commit was a merge commit and
870                          * ended in the GRAPH_POST_MERGE state, all branch
871                          * lines after graph->prev_commit_index were
872                          * printed as "\" on the previous line.  Continue
873                          * to print them as "\" on this line.  Otherwise,
874                          * print the branch lines as "|".
875                          */
876                         if (graph->prev_state == GRAPH_POST_MERGE &&
877                             graph->prev_commit_index < i)
878                                 graph_line_write_column(line, col, '\\');
879                         else
880                                 graph_line_write_column(line, col, '|');
881                 } else if (seen_this && (graph->expansion_row > 0)) {
882                         graph_line_write_column(line, col, '\\');
883                 } else {
884                         graph_line_write_column(line, col, '|');
885                 }
886                 graph_line_addch(line, ' ');
887         }
888
889         /*
890          * Increment graph->expansion_row,
891          * and move to state GRAPH_COMMIT if necessary
892          */
893         graph->expansion_row++;
894         if (!graph_needs_pre_commit_line(graph))
895                 graph_update_state(graph, GRAPH_COMMIT);
896 }
897
898 static void graph_output_commit_char(struct git_graph *graph, struct graph_line *line)
899 {
900         /*
901          * For boundary commits, print 'o'
902          * (We should only see boundary commits when revs->boundary is set.)
903          */
904         if (graph->commit->object.flags & BOUNDARY) {
905                 assert(graph->revs->boundary);
906                 graph_line_addch(line, 'o');
907                 return;
908         }
909
910         /*
911          * get_revision_mark() handles all other cases without assert()
912          */
913         graph_line_addstr(line, get_revision_mark(graph->revs, graph->commit));
914 }
915
916 /*
917  * Draw the horizontal dashes of an octopus merge.
918  */
919 static void graph_draw_octopus_merge(struct git_graph *graph, struct graph_line *line)
920 {
921         /*
922          * Here dashless_parents represents the number of parents which don't
923          * need to have dashes (the edges labeled "0" and "1").  And
924          * dashful_parents are the remaining ones.
925          *
926          * | *---.
927          * | |\ \ \
928          * | | | | |
929          * x 0 1 2 3
930          *
931          */
932         const int dashless_parents = 3 - graph->merge_layout;
933         int dashful_parents = graph->num_parents - dashless_parents;
934
935         /*
936          * Usually, we add one new column for each parent (like the diagram
937          * above) but sometimes the first parent goes into an existing column,
938          * like this:
939          *
940          * | *-.
941          * |/|\ \
942          * | | | |
943          * x 0 1 2
944          *
945          * In which case the number of parents will be one greater than the
946          * number of added columns.
947          */
948         int added_cols = (graph->num_new_columns - graph->num_columns);
949         int parent_in_old_cols = graph->num_parents - added_cols;
950
951         /*
952          * In both cases, commit_index corresponds to the edge labeled "0".
953          */
954         int first_col = graph->commit_index + dashless_parents
955             - parent_in_old_cols;
956
957         int i;
958         for (i = 0; i < dashful_parents; i++) {
959                 graph_line_write_column(line, &graph->new_columns[i+first_col], '-');
960                 graph_line_write_column(line, &graph->new_columns[i+first_col],
961                                           i == dashful_parents-1 ? '.' : '-');
962         }
963 }
964
965 static void graph_output_commit_line(struct git_graph *graph, struct graph_line *line)
966 {
967         int seen_this = 0;
968         int i;
969
970         /*
971          * Output the row containing this commit
972          * Iterate up to and including graph->num_columns,
973          * since the current commit may not be in any of the existing
974          * columns.  (This happens when the current commit doesn't have any
975          * children that we have already processed.)
976          */
977         seen_this = 0;
978         for (i = 0; i <= graph->num_columns; i++) {
979                 struct column *col = &graph->columns[i];
980                 struct commit *col_commit;
981                 if (i == graph->num_columns) {
982                         if (seen_this)
983                                 break;
984                         col_commit = graph->commit;
985                 } else {
986                         col_commit = graph->columns[i].commit;
987                 }
988
989                 if (col_commit == graph->commit) {
990                         seen_this = 1;
991                         graph_output_commit_char(graph, line);
992
993                         if (graph->num_parents > 2)
994                                 graph_draw_octopus_merge(graph, line);
995                 } else if (seen_this && (graph->edges_added > 1)) {
996                         graph_line_write_column(line, col, '\\');
997                 } else if (seen_this && (graph->edges_added == 1)) {
998                         /*
999                          * This is either a right-skewed 2-way merge
1000                          * commit, or a left-skewed 3-way merge.
1001                          * There is no GRAPH_PRE_COMMIT stage for such
1002                          * merges, so this is the first line of output
1003                          * for this commit.  Check to see what the previous
1004                          * line of output was.
1005                          *
1006                          * If it was GRAPH_POST_MERGE, the branch line
1007                          * coming into this commit may have been '\',
1008                          * and not '|' or '/'.  If so, output the branch
1009                          * line as '\' on this line, instead of '|'.  This
1010                          * makes the output look nicer.
1011                          */
1012                         if (graph->prev_state == GRAPH_POST_MERGE &&
1013                             graph->prev_edges_added > 0 &&
1014                             graph->prev_commit_index < i)
1015                                 graph_line_write_column(line, col, '\\');
1016                         else
1017                                 graph_line_write_column(line, col, '|');
1018                 } else if (graph->prev_state == GRAPH_COLLAPSING &&
1019                            graph->old_mapping[2 * i + 1] == i &&
1020                            graph->mapping[2 * i] < i) {
1021                         graph_line_write_column(line, col, '/');
1022                 } else {
1023                         graph_line_write_column(line, col, '|');
1024                 }
1025                 graph_line_addch(line, ' ');
1026         }
1027
1028         /*
1029          * Update graph->state
1030          */
1031         if (graph->num_parents > 1)
1032                 graph_update_state(graph, GRAPH_POST_MERGE);
1033         else if (graph_is_mapping_correct(graph))
1034                 graph_update_state(graph, GRAPH_PADDING);
1035         else
1036                 graph_update_state(graph, GRAPH_COLLAPSING);
1037 }
1038
1039 const char merge_chars[] = {'/', '|', '\\'};
1040
1041 static void graph_output_post_merge_line(struct git_graph *graph, struct graph_line *line)
1042 {
1043         int seen_this = 0;
1044         int i;
1045
1046         struct commit_list *first_parent = first_interesting_parent(graph);
1047         int seen_parent = 0;
1048
1049         /*
1050          * Output the post-merge row
1051          */
1052         for (i = 0; i <= graph->num_columns; i++) {
1053                 struct column *col = &graph->columns[i];
1054                 struct commit *col_commit;
1055                 if (i == graph->num_columns) {
1056                         if (seen_this)
1057                                 break;
1058                         col_commit = graph->commit;
1059                 } else {
1060                         col_commit = col->commit;
1061                 }
1062
1063                 if (col_commit == graph->commit) {
1064                         /*
1065                          * Since the current commit is a merge find
1066                          * the columns for the parent commits in
1067                          * new_columns and use those to format the
1068                          * edges.
1069                          */
1070                         struct commit_list *parents = first_parent;
1071                         int par_column;
1072                         int idx = graph->merge_layout;
1073                         char c;
1074                         seen_this = 1;
1075
1076                         for (; parents; parents = next_interesting_parent(graph, parents)) {
1077                                 par_column = graph_find_new_column_by_commit(graph, parents->item);
1078                                 assert(par_column >= 0);
1079
1080                                 c = merge_chars[idx];
1081                                 graph_line_write_column(line, &graph->new_columns[par_column], c);
1082                                 if (idx == 2)
1083                                         graph_line_addch(line, ' ');
1084                                 else
1085                                         idx++;
1086                         }
1087                         if (graph->edges_added == 0)
1088                                 graph_line_addch(line, ' ');
1089
1090                 } else if (seen_this) {
1091                         if (graph->edges_added > 0)
1092                                 graph_line_write_column(line, col, '\\');
1093                         else
1094                                 graph_line_write_column(line, col, '|');
1095                         graph_line_addch(line, ' ');
1096                 } else {
1097                         graph_line_write_column(line, col, '|');
1098                         if (graph->merge_layout != 0 || i != graph->commit_index - 1)
1099                                 graph_line_addch(line, seen_parent ? '_' : ' ');
1100                 }
1101
1102                 if (col_commit == first_parent->item)
1103                         seen_parent = 1;
1104         }
1105
1106         /*
1107          * Update graph->state
1108          */
1109         if (graph_is_mapping_correct(graph))
1110                 graph_update_state(graph, GRAPH_PADDING);
1111         else
1112                 graph_update_state(graph, GRAPH_COLLAPSING);
1113 }
1114
1115 static void graph_output_collapsing_line(struct git_graph *graph, struct graph_line *line)
1116 {
1117         int i;
1118         short used_horizontal = 0;
1119         int horizontal_edge = -1;
1120         int horizontal_edge_target = -1;
1121
1122         /*
1123          * Swap the mapping and old_mapping arrays
1124          */
1125         SWAP(graph->mapping, graph->old_mapping);
1126
1127         /*
1128          * Clear out the mapping array
1129          */
1130         for (i = 0; i < graph->mapping_size; i++)
1131                 graph->mapping[i] = -1;
1132
1133         for (i = 0; i < graph->mapping_size; i++) {
1134                 int target = graph->old_mapping[i];
1135                 if (target < 0)
1136                         continue;
1137
1138                 /*
1139                  * Since update_columns() always inserts the leftmost
1140                  * column first, each branch's target location should
1141                  * always be either its current location or to the left of
1142                  * its current location.
1143                  *
1144                  * We never have to move branches to the right.  This makes
1145                  * the graph much more legible, since whenever branches
1146                  * cross, only one is moving directions.
1147                  */
1148                 assert(target * 2 <= i);
1149
1150                 if (target * 2 == i) {
1151                         /*
1152                          * This column is already in the
1153                          * correct place
1154                          */
1155                         assert(graph->mapping[i] == -1);
1156                         graph->mapping[i] = target;
1157                 } else if (graph->mapping[i - 1] < 0) {
1158                         /*
1159                          * Nothing is to the left.
1160                          * Move to the left by one
1161                          */
1162                         graph->mapping[i - 1] = target;
1163                         /*
1164                          * If there isn't already an edge moving horizontally
1165                          * select this one.
1166                          */
1167                         if (horizontal_edge == -1) {
1168                                 int j;
1169                                 horizontal_edge = i;
1170                                 horizontal_edge_target = target;
1171                                 /*
1172                                  * The variable target is the index of the graph
1173                                  * column, and therefore target*2+3 is the
1174                                  * actual screen column of the first horizontal
1175                                  * line.
1176                                  */
1177                                 for (j = (target * 2)+3; j < (i - 2); j += 2)
1178                                         graph->mapping[j] = target;
1179                         }
1180                 } else if (graph->mapping[i - 1] == target) {
1181                         /*
1182                          * There is a branch line to our left
1183                          * already, and it is our target.  We
1184                          * combine with this line, since we share
1185                          * the same parent commit.
1186                          *
1187                          * We don't have to add anything to the
1188                          * output or mapping, since the
1189                          * existing branch line has already taken
1190                          * care of it.
1191                          */
1192                 } else {
1193                         /*
1194                          * There is a branch line to our left,
1195                          * but it isn't our target.  We need to
1196                          * cross over it.
1197                          *
1198                          * The space just to the left of this
1199                          * branch should always be empty.
1200                          *
1201                          * The branch to the left of that space
1202                          * should be our eventual target.
1203                          */
1204                         assert(graph->mapping[i - 1] > target);
1205                         assert(graph->mapping[i - 2] < 0);
1206                         assert(graph->mapping[i - 3] == target);
1207                         graph->mapping[i - 2] = target;
1208                         /*
1209                          * Mark this branch as the horizontal edge to
1210                          * prevent any other edges from moving
1211                          * horizontally.
1212                          */
1213                         if (horizontal_edge == -1)
1214                                 horizontal_edge = i;
1215                 }
1216         }
1217
1218         /*
1219          * Copy the current mapping array into old_mapping
1220          */
1221         COPY_ARRAY(graph->old_mapping, graph->mapping, graph->mapping_size);
1222
1223         /*
1224          * The new mapping may be 1 smaller than the old mapping
1225          */
1226         if (graph->mapping[graph->mapping_size - 1] < 0)
1227                 graph->mapping_size--;
1228
1229         /*
1230          * Output out a line based on the new mapping info
1231          */
1232         for (i = 0; i < graph->mapping_size; i++) {
1233                 int target = graph->mapping[i];
1234                 if (target < 0)
1235                         graph_line_addch(line, ' ');
1236                 else if (target * 2 == i)
1237                         graph_line_write_column(line, &graph->new_columns[target], '|');
1238                 else if (target == horizontal_edge_target &&
1239                          i != horizontal_edge - 1) {
1240                                 /*
1241                                  * Set the mappings for all but the
1242                                  * first segment to -1 so that they
1243                                  * won't continue into the next line.
1244                                  */
1245                                 if (i != (target * 2)+3)
1246                                         graph->mapping[i] = -1;
1247                                 used_horizontal = 1;
1248                         graph_line_write_column(line, &graph->new_columns[target], '_');
1249                 } else {
1250                         if (used_horizontal && i < horizontal_edge)
1251                                 graph->mapping[i] = -1;
1252                         graph_line_write_column(line, &graph->new_columns[target], '/');
1253
1254                 }
1255         }
1256
1257         /*
1258          * If graph->mapping indicates that all of the branch lines
1259          * are already in the correct positions, we are done.
1260          * Otherwise, we need to collapse some branch lines together.
1261          */
1262         if (graph_is_mapping_correct(graph))
1263                 graph_update_state(graph, GRAPH_PADDING);
1264 }
1265
1266 int graph_next_line(struct git_graph *graph, struct strbuf *sb)
1267 {
1268         int shown_commit_line = 0;
1269         struct graph_line line = { .buf = sb, .width = 0 };
1270
1271         /*
1272          * We could conceivable be called with a NULL commit
1273          * if our caller has a bug, and invokes graph_next_line()
1274          * immediately after graph_init(), without first calling
1275          * graph_update().  Return without outputting anything in this
1276          * case.
1277          */
1278         if (!graph->commit)
1279                 return -1;
1280
1281         switch (graph->state) {
1282         case GRAPH_PADDING:
1283                 graph_output_padding_line(graph, &line);
1284                 break;
1285         case GRAPH_SKIP:
1286                 graph_output_skip_line(graph, &line);
1287                 break;
1288         case GRAPH_PRE_COMMIT:
1289                 graph_output_pre_commit_line(graph, &line);
1290                 break;
1291         case GRAPH_COMMIT:
1292                 graph_output_commit_line(graph, &line);
1293                 shown_commit_line = 1;
1294                 break;
1295         case GRAPH_POST_MERGE:
1296                 graph_output_post_merge_line(graph, &line);
1297                 break;
1298         case GRAPH_COLLAPSING:
1299                 graph_output_collapsing_line(graph, &line);
1300                 break;
1301         }
1302
1303         graph_pad_horizontally(graph, &line);
1304         return shown_commit_line;
1305 }
1306
1307 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
1308 {
1309         int i;
1310         struct graph_line line = { .buf = sb, .width = 0 };
1311
1312         if (graph->state != GRAPH_COMMIT) {
1313                 graph_next_line(graph, sb);
1314                 return;
1315         }
1316
1317         /*
1318          * Output the row containing this commit
1319          * Iterate up to and including graph->num_columns,
1320          * since the current commit may not be in any of the existing
1321          * columns.  (This happens when the current commit doesn't have any
1322          * children that we have already processed.)
1323          */
1324         for (i = 0; i < graph->num_columns; i++) {
1325                 struct column *col = &graph->columns[i];
1326
1327                 graph_line_write_column(&line, col, '|');
1328
1329                 if (col->commit == graph->commit && graph->num_parents > 2) {
1330                         int len = (graph->num_parents - 2) * 2;
1331                         graph_line_addchars(&line, ' ', len);
1332                 } else {
1333                         graph_line_addch(&line, ' ');
1334                 }
1335         }
1336
1337         graph_pad_horizontally(graph, &line);
1338
1339         /*
1340          * Update graph->prev_state since we have output a padding line
1341          */
1342         graph->prev_state = GRAPH_PADDING;
1343 }
1344
1345 int graph_is_commit_finished(struct git_graph const *graph)
1346 {
1347         return (graph->state == GRAPH_PADDING);
1348 }
1349
1350 void graph_show_commit(struct git_graph *graph)
1351 {
1352         struct strbuf msgbuf = STRBUF_INIT;
1353         int shown_commit_line = 0;
1354
1355         graph_show_line_prefix(default_diffopt);
1356
1357         if (!graph)
1358                 return;
1359
1360         /*
1361          * When showing a diff of a merge against each of its parents, we
1362          * are called once for each parent without graph_update having been
1363          * called.  In this case, simply output a single padding line.
1364          */
1365         if (graph_is_commit_finished(graph)) {
1366                 graph_show_padding(graph);
1367                 shown_commit_line = 1;
1368         }
1369
1370         while (!shown_commit_line && !graph_is_commit_finished(graph)) {
1371                 shown_commit_line = graph_next_line(graph, &msgbuf);
1372                 fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
1373                         graph->revs->diffopt.file);
1374                 if (!shown_commit_line) {
1375                         putc('\n', graph->revs->diffopt.file);
1376                         graph_show_line_prefix(&graph->revs->diffopt);
1377                 }
1378                 strbuf_setlen(&msgbuf, 0);
1379         }
1380
1381         strbuf_release(&msgbuf);
1382 }
1383
1384 void graph_show_oneline(struct git_graph *graph)
1385 {
1386         struct strbuf msgbuf = STRBUF_INIT;
1387
1388         graph_show_line_prefix(default_diffopt);
1389
1390         if (!graph)
1391                 return;
1392
1393         graph_next_line(graph, &msgbuf);
1394         fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
1395         strbuf_release(&msgbuf);
1396 }
1397
1398 void graph_show_padding(struct git_graph *graph)
1399 {
1400         struct strbuf msgbuf = STRBUF_INIT;
1401
1402         graph_show_line_prefix(default_diffopt);
1403
1404         if (!graph)
1405                 return;
1406
1407         graph_padding_line(graph, &msgbuf);
1408         fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
1409         strbuf_release(&msgbuf);
1410 }
1411
1412 int graph_show_remainder(struct git_graph *graph)
1413 {
1414         struct strbuf msgbuf = STRBUF_INIT;
1415         int shown = 0;
1416
1417         graph_show_line_prefix(default_diffopt);
1418
1419         if (!graph)
1420                 return 0;
1421
1422         if (graph_is_commit_finished(graph))
1423                 return 0;
1424
1425         for (;;) {
1426                 graph_next_line(graph, &msgbuf);
1427                 fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
1428                         graph->revs->diffopt.file);
1429                 strbuf_setlen(&msgbuf, 0);
1430                 shown = 1;
1431
1432                 if (!graph_is_commit_finished(graph)) {
1433                         putc('\n', graph->revs->diffopt.file);
1434                         graph_show_line_prefix(&graph->revs->diffopt);
1435                 } else {
1436                         break;
1437                 }
1438         }
1439         strbuf_release(&msgbuf);
1440
1441         return shown;
1442 }
1443
1444 static void graph_show_strbuf(struct git_graph *graph,
1445                               FILE *file,
1446                               struct strbuf const *sb)
1447 {
1448         char *p;
1449
1450         /*
1451          * Print the strbuf line by line,
1452          * and display the graph info before each line but the first.
1453          */
1454         p = sb->buf;
1455         while (p) {
1456                 size_t len;
1457                 char *next_p = strchr(p, '\n');
1458                 if (next_p) {
1459                         next_p++;
1460                         len = next_p - p;
1461                 } else {
1462                         len = (sb->buf + sb->len) - p;
1463                 }
1464                 fwrite(p, sizeof(char), len, file);
1465                 if (next_p && *next_p != '\0')
1466                         graph_show_oneline(graph);
1467                 p = next_p;
1468         }
1469 }
1470
1471 void graph_show_commit_msg(struct git_graph *graph,
1472                            FILE *file,
1473                            struct strbuf const *sb)
1474 {
1475         int newline_terminated;
1476
1477         /*
1478          * Show the commit message
1479          */
1480         graph_show_strbuf(graph, file, sb);
1481
1482         if (!graph)
1483                 return;
1484
1485         newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
1486
1487         /*
1488          * If there is more output needed for this commit, show it now
1489          */
1490         if (!graph_is_commit_finished(graph)) {
1491                 /*
1492                  * If sb doesn't have a terminating newline, print one now,
1493                  * so we can start the remainder of the graph output on a
1494                  * new line.
1495                  */
1496                 if (!newline_terminated)
1497                         putc('\n', file);
1498
1499                 graph_show_remainder(graph);
1500
1501                 /*
1502                  * If sb ends with a newline, our output should too.
1503                  */
1504                 if (newline_terminated)
1505                         putc('\n', file);
1506         }
1507 }