log --graph: customize the graph lines with config log.graphColors
[git] / graph.c
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"
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 static void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb);
34
35 /*
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.
40  */
41
42 struct column {
43         /*
44          * The parent commit of this column.
45          */
46         struct commit *commit;
47         /*
48          * The color to (optionally) print this column in.  This is an
49          * index into column_colors.
50          */
51         unsigned short color;
52 };
53
54 enum graph_state {
55         GRAPH_PADDING,
56         GRAPH_SKIP,
57         GRAPH_PRE_COMMIT,
58         GRAPH_COMMIT,
59         GRAPH_POST_MERGE,
60         GRAPH_COLLAPSING
61 };
62
63 static const char **column_colors;
64 static unsigned short column_colors_max;
65
66 static void parse_graph_colors_config(struct argv_array *colors, const char *string)
67 {
68         const char *end, *start;
69
70         start = string;
71         end = string + strlen(string);
72         while (start < end) {
73                 const char *comma = strchrnul(start, ',');
74                 char color[COLOR_MAXLEN];
75
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;
82         }
83         argv_array_push(colors, GIT_COLOR_RESET);
84 }
85
86 void graph_set_column_colors(const char **colors, unsigned short colors_max)
87 {
88         column_colors = colors;
89         column_colors_max = colors_max;
90 }
91
92 static const char *column_get_color_code(unsigned short color)
93 {
94         return column_colors[color];
95 }
96
97 static void strbuf_write_column(struct strbuf *sb, const struct column *c,
98                                 char col_char)
99 {
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));
105 }
106
107 struct git_graph {
108         /*
109          * The commit currently being processed
110          */
111         struct commit *commit;
112         /* The rev-info used for the current traversal */
113         struct rev_info *revs;
114         /*
115          * The number of interesting parents that this commit has.
116          *
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().
120          */
121         int num_parents;
122         /*
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.
126          */
127         int width;
128         /*
129          * The next expansion row to print
130          * when state is GRAPH_PRE_COMMIT
131          */
132         int expansion_row;
133         /*
134          * The current output state.
135          * This tells us what kind of line graph_next_line() should output.
136          */
137         enum graph_state state;
138         /*
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.
142          */
143         enum graph_state prev_state;
144         /*
145          * The index of the column that refers to this commit.
146          *
147          * If none of the incoming columns refer to this commit,
148          * this will be equal to num_columns.
149          */
150         int commit_index;
151         /*
152          * The commit_index for the previously displayed commit.
153          *
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.
157          */
158         int prev_commit_index;
159         /*
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.
163          */
164         int column_capacity;
165         /*
166          * The number of columns (also called "branch lines" in some places)
167          */
168         int num_columns;
169         /*
170          * The number of columns in the new_columns array
171          */
172         int num_new_columns;
173         /*
174          * The number of entries in the mapping array
175          */
176         int mapping_size;
177         /*
178          * The column state before we output the current commit.
179          */
180         struct column *columns;
181         /*
182          * The new column state after we output the current commit.
183          * Only valid when state is GRAPH_COLLAPSING.
184          */
185         struct column *new_columns;
186         /*
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.)
194          *
195          * The maximum capacity of this array is always
196          * sizeof(int) * 2 * column_capacity.
197          */
198         int *mapping;
199         /*
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.
204          */
205         int *new_mapping;
206         /*
207          * The current default column color being used.  This is
208          * stored as an index into the array column_colors.
209          */
210         unsigned short default_column_color;
211 };
212
213 static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void *data)
214 {
215         struct git_graph *graph = data;
216         static struct strbuf msgbuf = STRBUF_INIT;
217
218         assert(opt);
219         assert(graph);
220
221         opt->output_prefix_length = graph->width;
222         strbuf_reset(&msgbuf);
223         graph_padding_line(graph, &msgbuf);
224         return &msgbuf;
225 }
226
227 struct git_graph *graph_init(struct rev_info *opt)
228 {
229         struct git_graph *graph = xmalloc(sizeof(struct git_graph));
230
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);
245                 }
246         }
247
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;
259         /*
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.
263          */
264         graph->default_column_color = column_colors_max - 1;
265
266         /*
267          * Allocate a reasonably large default number of columns
268          * We'll automatically grow columns later if we need more room.
269          */
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);
275
276         /*
277          * The diff output prefix callback, with this we can make
278          * all the diff output to align with the graph lines.
279          */
280         opt->diffopt.output_prefix = diff_output_prefix_callback;
281         opt->diffopt.output_prefix_data = graph;
282         opt->diffopt.output_prefix_length = 0;
283
284         return graph;
285 }
286
287 static void graph_update_state(struct git_graph *graph, enum graph_state s)
288 {
289         graph->prev_state = graph->state;
290         graph->state = s;
291 }
292
293 static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
294 {
295         if (graph->column_capacity >= num_columns)
296                 return;
297
298         do {
299                 graph->column_capacity *= 2;
300         } while (graph->column_capacity < num_columns);
301
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);
306 }
307
308 /*
309  * Returns 1 if the commit will be printed in the graph output,
310  * and 0 otherwise.
311  */
312 static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
313 {
314         /*
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.
318          */
319         if (graph->revs && graph->revs->boundary) {
320                 if (commit->object.flags & CHILD_SHOWN)
321                         return 1;
322         }
323
324         /*
325          * Otherwise, use get_commit_action() to see if this commit is
326          * interesting
327          */
328         return get_commit_action(graph->revs, commit) == commit_show;
329 }
330
331 static struct commit_list *next_interesting_parent(struct git_graph *graph,
332                                                    struct commit_list *orig)
333 {
334         struct commit_list *list;
335
336         /*
337          * If revs->first_parent_only is set, only the first
338          * parent is interesting.  None of the others are.
339          */
340         if (graph->revs->first_parent_only)
341                 return NULL;
342
343         /*
344          * Return the next interesting commit after orig
345          */
346         for (list = orig->next; list; list = list->next) {
347                 if (graph_is_interesting(graph, list->item))
348                         return list;
349         }
350
351         return NULL;
352 }
353
354 static struct commit_list *first_interesting_parent(struct git_graph *graph)
355 {
356         struct commit_list *parents = graph->commit->parents;
357
358         /*
359          * If this commit has no parents, ignore it
360          */
361         if (!parents)
362                 return NULL;
363
364         /*
365          * If the first parent is interesting, return it
366          */
367         if (graph_is_interesting(graph, parents->item))
368                 return parents;
369
370         /*
371          * Otherwise, call next_interesting_parent() to get
372          * the next interesting parent
373          */
374         return next_interesting_parent(graph, parents);
375 }
376
377 static unsigned short graph_get_current_column_color(const struct git_graph *graph)
378 {
379         if (!want_color(graph->revs->diffopt.use_color))
380                 return column_colors_max;
381         return graph->default_column_color;
382 }
383
384 /*
385  * Update the graph's default column color.
386  */
387 static void graph_increment_column_color(struct git_graph *graph)
388 {
389         graph->default_column_color = (graph->default_column_color + 1) %
390                 column_colors_max;
391 }
392
393 static unsigned short graph_find_commit_color(const struct git_graph *graph,
394                                               const struct commit *commit)
395 {
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;
400         }
401         return graph_get_current_column_color(graph);
402 }
403
404 static void graph_insert_into_new_columns(struct git_graph *graph,
405                                           struct commit *commit,
406                                           int *mapping_index)
407 {
408         int i;
409
410         /*
411          * If the commit is already in the new_columns list, we don't need to
412          * add it.  Just update the mapping correctly.
413          */
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;
419                 }
420         }
421
422         /*
423          * This commit isn't already in new_columns.  Add it.
424          */
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++;
430 }
431
432 static void graph_update_width(struct git_graph *graph,
433                                int is_commit_in_existing_columns)
434 {
435         /*
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.
439          *
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.
443          */
444         int max_cols = graph->num_columns + graph->num_parents;
445
446         /*
447          * Even if the current commit has no parents to be printed, it
448          * still takes up a column for itself.
449          */
450         if (graph->num_parents < 1)
451                 max_cols++;
452
453         /*
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.
457          */
458         if (is_commit_in_existing_columns)
459                 max_cols--;
460
461         /*
462          * Each column takes up 2 spaces
463          */
464         graph->width = max_cols * 2;
465 }
466
467 static void graph_update_columns(struct git_graph *graph)
468 {
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;
474
475         /*
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.
479          *
480          * We'll re-use the old columns array as storage to compute the new
481          * columns list for the commit after this one.
482          */
483         tmp_columns = graph->columns;
484         graph->columns = graph->new_columns;
485         graph->num_columns = graph->num_new_columns;
486
487         graph->new_columns = tmp_columns;
488         graph->num_new_columns = 0;
489
490         /*
491          * Now update new_columns and mapping with the information for the
492          * commit after this one.
493          *
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.
497          */
498         max_new_columns = graph->num_columns + graph->num_parents;
499         graph_ensure_capacity(graph, max_new_columns);
500
501         /*
502          * Clear out graph->mapping
503          */
504         graph->mapping_size = 2 * max_new_columns;
505         for (i = 0; i < graph->mapping_size; i++)
506                 graph->mapping[i] = -1;
507
508         /*
509          * Populate graph->new_columns and graph->mapping
510          *
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.
516          */
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;
529                 }
530
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)) {
538                                 /*
539                                  * If this is a merge, or the start of a new
540                                  * childless column, increment the current
541                                  * color.
542                                  */
543                                 if (graph->num_parents > 1 ||
544                                     !is_commit_in_columns) {
545                                         graph_increment_column_color(graph);
546                                 }
547                                 graph_insert_into_new_columns(graph,
548                                                               parent->item,
549                                                               &mapping_idx);
550                         }
551                         /*
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.
556                          */
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);
562                 }
563         }
564
565         /*
566          * Shrink mapping_size to be the minimum necessary
567          */
568         while (graph->mapping_size > 1 &&
569                graph->mapping[graph->mapping_size - 1] < 0)
570                 graph->mapping_size--;
571
572         /*
573          * Compute graph->width for this commit
574          */
575         graph_update_width(graph, is_commit_in_columns);
576 }
577
578 void graph_update(struct git_graph *graph, struct commit *commit)
579 {
580         struct commit_list *parent;
581
582         /*
583          * Set the new commit
584          */
585         graph->commit = commit;
586
587         /*
588          * Count how many interesting parents this commit has
589          */
590         graph->num_parents = 0;
591         for (parent = first_interesting_parent(graph);
592              parent;
593              parent = next_interesting_parent(graph, parent))
594         {
595                 graph->num_parents++;
596         }
597
598         /*
599          * Store the old commit_index in prev_commit_index.
600          * graph_update_columns() will update graph->commit_index for this
601          * commit.
602          */
603         graph->prev_commit_index = graph->commit_index;
604
605         /*
606          * Call graph_update_columns() to update
607          * columns, new_columns, and mapping.
608          */
609         graph_update_columns(graph);
610
611         graph->expansion_row = 0;
612
613         /*
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.
618          *
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.
622          *
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.
627          *
628          * If there are less than 3 parents, we can immediately print the
629          * commit line.
630          */
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;
638 }
639
640 static int graph_is_mapping_correct(struct git_graph *graph)
641 {
642         int i;
643
644         /*
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.)
649          */
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;
657         }
658
659         return 1;
660 }
661
662 static void graph_pad_horizontally(struct git_graph *graph, struct strbuf *sb,
663                                    int chars_written)
664 {
665         /*
666          * Add additional spaces to the end of the strbuf, so that all
667          * lines for a particular commit have the same width.
668          *
669          * This way, fields printed to the right of the graph will remain
670          * aligned for the entire commit.
671          */
672         int extra;
673         if (chars_written >= graph->width)
674                 return;
675
676         extra = graph->width - chars_written;
677         strbuf_addf(sb, "%*s", (int) extra, "");
678 }
679
680 static void graph_output_padding_line(struct git_graph *graph,
681                                       struct strbuf *sb)
682 {
683         int i;
684
685         /*
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.
691          */
692         if (!graph->commit)
693                 return;
694
695         /*
696          * Output a padding row, that leaves all branch lines unchanged
697          */
698         for (i = 0; i < graph->num_new_columns; i++) {
699                 strbuf_write_column(sb, &graph->new_columns[i], '|');
700                 strbuf_addch(sb, ' ');
701         }
702
703         graph_pad_horizontally(graph, sb, graph->num_new_columns * 2);
704 }
705
706
707 int graph_width(struct git_graph *graph)
708 {
709         return graph->width;
710 }
711
712
713 static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb)
714 {
715         /*
716          * Output an ellipsis to indicate that a portion
717          * of the graph is missing.
718          */
719         strbuf_addstr(sb, "...");
720         graph_pad_horizontally(graph, sb, 3);
721
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);
727 }
728
729 static void graph_output_pre_commit_line(struct git_graph *graph,
730                                          struct strbuf *sb)
731 {
732         int num_expansion_rows;
733         int i, seen_this;
734         int chars_written;
735
736         /*
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.
740          *
741          * We need 2 extra rows for every parent over 2.
742          */
743         assert(graph->num_parents >= 3);
744         num_expansion_rows = (graph->num_parents - 2) * 2;
745
746         /*
747          * graph->expansion_row tracks the current expansion row we are on.
748          * It should be in the range [0, num_expansion_rows - 1]
749          */
750         assert(0 <= graph->expansion_row &&
751                graph->expansion_row < num_expansion_rows);
752
753         /*
754          * Output the row
755          */
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)) {
766                         /*
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 "|".
774                          */
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++;
787                 }
788                 strbuf_addch(sb, ' ');
789                 chars_written++;
790         }
791
792         graph_pad_horizontally(graph, sb, chars_written);
793
794         /*
795          * Increment graph->expansion_row,
796          * and move to state GRAPH_COMMIT if necessary
797          */
798         graph->expansion_row++;
799         if (graph->expansion_row >= num_expansion_rows)
800                 graph_update_state(graph, GRAPH_COMMIT);
801 }
802
803 static void graph_output_commit_char(struct git_graph *graph, struct strbuf *sb)
804 {
805         /*
806          * For boundary commits, print 'o'
807          * (We should only see boundary commits when revs->boundary is set.)
808          */
809         if (graph->commit->object.flags & BOUNDARY) {
810                 assert(graph->revs->boundary);
811                 strbuf_addch(sb, 'o');
812                 return;
813         }
814
815         /*
816          * get_revision_mark() handles all other cases without assert()
817          */
818         strbuf_addstr(sb, get_revision_mark(graph->revs, graph->commit));
819 }
820
821 /*
822  * Draw an octopus merge and return the number of characters written.
823  */
824 static int graph_draw_octopus_merge(struct git_graph *graph,
825                                     struct strbuf *sb)
826 {
827         /*
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).
831          */
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], '-');
839         }
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;
843 }
844
845 static void graph_output_commit_line(struct git_graph *graph, struct strbuf *sb)
846 {
847         int seen_this = 0;
848         int i, chars_written;
849
850         /*
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.)
856          */
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;
868                 }
869
870                 if (col_commit == graph->commit) {
871                         seen_this = 1;
872                         graph_output_commit_char(graph, sb);
873                         chars_written++;
874
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)) {
882                         /*
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.
888                          *
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.
894                          */
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++;
904                 }
905                 strbuf_addch(sb, ' ');
906                 chars_written++;
907         }
908
909         graph_pad_horizontally(graph, sb, chars_written);
910
911         /*
912          * Update graph->state
913          */
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);
920 }
921
922 static struct column *find_new_column_by_commit(struct git_graph *graph,
923                                                 struct commit *commit)
924 {
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];
929         }
930         return NULL;
931 }
932
933 static void graph_output_post_merge_line(struct git_graph *graph, struct strbuf *sb)
934 {
935         int seen_this = 0;
936         int i, j, chars_written;
937
938         /*
939          * Output the post-merge row
940          */
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;
951                 }
952
953                 if (col_commit == graph->commit) {
954                         /*
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.
959                          */
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);
967
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, ' ');
977                         }
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;
987                 }
988         }
989
990         graph_pad_horizontally(graph, sb, chars_written);
991
992         /*
993          * Update graph->state
994          */
995         if (graph_is_mapping_correct(graph))
996                 graph_update_state(graph, GRAPH_PADDING);
997         else
998                 graph_update_state(graph, GRAPH_COLLAPSING);
999 }
1000
1001 static void graph_output_collapsing_line(struct git_graph *graph, struct strbuf *sb)
1002 {
1003         int i;
1004         int *tmp_mapping;
1005         short used_horizontal = 0;
1006         int horizontal_edge = -1;
1007         int horizontal_edge_target = -1;
1008
1009         /*
1010          * Clear out the new_mapping array
1011          */
1012         for (i = 0; i < graph->mapping_size; i++)
1013                 graph->new_mapping[i] = -1;
1014
1015         for (i = 0; i < graph->mapping_size; i++) {
1016                 int target = graph->mapping[i];
1017                 if (target < 0)
1018                         continue;
1019
1020                 /*
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.
1025                  *
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.
1029                  */
1030                 assert(target * 2 <= i);
1031
1032                 if (target * 2 == i) {
1033                         /*
1034                          * This column is already in the
1035                          * correct place
1036                          */
1037                         assert(graph->new_mapping[i] == -1);
1038                         graph->new_mapping[i] = target;
1039                 } else if (graph->new_mapping[i - 1] < 0) {
1040                         /*
1041                          * Nothing is to the left.
1042                          * Move to the left by one
1043                          */
1044                         graph->new_mapping[i - 1] = target;
1045                         /*
1046                          * If there isn't already an edge moving horizontally
1047                          * select this one.
1048                          */
1049                         if (horizontal_edge == -1) {
1050                                 int j;
1051                                 horizontal_edge = i;
1052                                 horizontal_edge_target = target;
1053                                 /*
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.
1058                                  */
1059                                 for (j = (target * 2)+3; j < (i - 2); j += 2)
1060                                         graph->new_mapping[j] = target;
1061                         }
1062                 } else if (graph->new_mapping[i - 1] == target) {
1063                         /*
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.
1068                          *
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.
1073                          */
1074                 } else {
1075                         /*
1076                          * There is a branch line to our left,
1077                          * but it isn't our target.  We need to
1078                          * cross over it.
1079                          *
1080                          * The space just to the left of this
1081                          * branch should always be empty.
1082                          *
1083                          * The branch to the left of that space
1084                          * should be our eventual target.
1085                          */
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;
1090                         /*
1091                          * Mark this branch as the horizontal edge to
1092                          * prevent any other edges from moving
1093                          * horizontally.
1094                          */
1095                         if (horizontal_edge == -1)
1096                                 horizontal_edge = i;
1097                 }
1098         }
1099
1100         /*
1101          * The new mapping may be 1 smaller than the old mapping
1102          */
1103         if (graph->new_mapping[graph->mapping_size - 1] < 0)
1104                 graph->mapping_size--;
1105
1106         /*
1107          * Output out a line based on the new mapping info
1108          */
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) {
1117                                 /*
1118                                  * Set the mappings for all but the
1119                                  * first segment to -1 so that they
1120                                  * won't continue into the next line.
1121                                  */
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], '/');
1130
1131                 }
1132         }
1133
1134         graph_pad_horizontally(graph, sb, graph->mapping_size);
1135
1136         /*
1137          * Swap mapping and new_mapping
1138          */
1139         tmp_mapping = graph->mapping;
1140         graph->mapping = graph->new_mapping;
1141         graph->new_mapping = tmp_mapping;
1142
1143         /*
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.
1147          */
1148         if (graph_is_mapping_correct(graph))
1149                 graph_update_state(graph, GRAPH_PADDING);
1150 }
1151
1152 int graph_next_line(struct git_graph *graph, struct strbuf *sb)
1153 {
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;
1173         }
1174
1175         assert(0);
1176         return 0;
1177 }
1178
1179 static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
1180 {
1181         int i;
1182
1183         if (graph->state != GRAPH_COMMIT) {
1184                 graph_next_line(graph, sb);
1185                 return;
1186         }
1187
1188         /*
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.)
1194          */
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, ' ');
1202         }
1203
1204         graph_pad_horizontally(graph, sb, graph->num_columns);
1205
1206         /*
1207          * Update graph->prev_state since we have output a padding line
1208          */
1209         graph->prev_state = GRAPH_PADDING;
1210 }
1211
1212 int graph_is_commit_finished(struct git_graph const *graph)
1213 {
1214         return (graph->state == GRAPH_PADDING);
1215 }
1216
1217 void graph_show_commit(struct git_graph *graph)
1218 {
1219         struct strbuf msgbuf = STRBUF_INIT;
1220         int shown_commit_line = 0;
1221
1222         if (!graph)
1223                 return;
1224
1225         /*
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.
1229          */
1230         if (graph_is_commit_finished(graph)) {
1231                 graph_show_padding(graph);
1232                 shown_commit_line = 1;
1233         }
1234
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);
1242         }
1243
1244         strbuf_release(&msgbuf);
1245 }
1246
1247 void graph_show_oneline(struct git_graph *graph)
1248 {
1249         struct strbuf msgbuf = STRBUF_INIT;
1250
1251         if (!graph)
1252                 return;
1253
1254         graph_next_line(graph, &msgbuf);
1255         fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
1256         strbuf_release(&msgbuf);
1257 }
1258
1259 void graph_show_padding(struct git_graph *graph)
1260 {
1261         struct strbuf msgbuf = STRBUF_INIT;
1262
1263         if (!graph)
1264                 return;
1265
1266         graph_padding_line(graph, &msgbuf);
1267         fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
1268         strbuf_release(&msgbuf);
1269 }
1270
1271 int graph_show_remainder(struct git_graph *graph)
1272 {
1273         struct strbuf msgbuf = STRBUF_INIT;
1274         int shown = 0;
1275
1276         if (!graph)
1277                 return 0;
1278
1279         if (graph_is_commit_finished(graph))
1280                 return 0;
1281
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;
1288
1289                 if (!graph_is_commit_finished(graph))
1290                         putc('\n', graph->revs->diffopt.file);
1291                 else
1292                         break;
1293         }
1294         strbuf_release(&msgbuf);
1295
1296         return shown;
1297 }
1298
1299
1300 static void graph_show_strbuf(struct git_graph *graph, struct strbuf const *sb)
1301 {
1302         char *p;
1303
1304         if (!graph) {
1305                 fwrite(sb->buf, sizeof(char), sb->len,
1306                         graph->revs->diffopt.file);
1307                 return;
1308         }
1309
1310         /*
1311          * Print the strbuf line by line,
1312          * and display the graph info before each line but the first.
1313          */
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;
1323                 }
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;
1328         }
1329 }
1330
1331 void graph_show_commit_msg(struct git_graph *graph,
1332                            struct strbuf const *sb)
1333 {
1334         int newline_terminated;
1335
1336         if (!graph) {
1337                 /*
1338                  * If there's no graph, just print the message buffer.
1339                  *
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.
1343                  */
1344                 fwrite(sb->buf, sizeof(char), sb->len,
1345                         graph->revs->diffopt.file);
1346                 return;
1347         }
1348
1349         newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
1350
1351         /*
1352          * Show the commit message
1353          */
1354         graph_show_strbuf(graph, sb);
1355
1356         /*
1357          * If there is more output needed for this commit, show it now
1358          */
1359         if (!graph_is_commit_finished(graph)) {
1360                 /*
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.
1364                  */
1365                 if (!newline_terminated)
1366                         putc('\n', graph->revs->diffopt.file);
1367
1368                 graph_show_remainder(graph);
1369
1370                 /*
1371                  * If sb ends with a newline, our output should too.
1372                  */
1373                 if (newline_terminated)
1374                         putc('\n', graph->revs->diffopt.file);
1375         }
1376 }