blob: e3828eb8f2309048353524e4132d33dd6cda3555 [file] [log] [blame]
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Adam Simpkinsc12172d2008-05-04 03:36:53 -07003#include "commit.h"
Allan Caffee427fc5b2009-04-13 15:53:41 -04004#include "color.h"
Adam Simpkinsc12172d2008-05-04 03:36:53 -07005#include "graph.h"
Adam Simpkinsc12172d2008-05-04 03:36:53 -07006#include "revision.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -04007#include "strvec.h"
Adam Simpkinsc12172d2008-05-04 03:36:53 -07008
Nanako Shiraishi064bfbd2008-09-25 18:41:08 +09009/* Internal API */
10
11/*
Nanako Shiraishi064bfbd2008-09-25 18:41:08 +090012 * 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 */
18static void graph_padding_line(struct git_graph *graph, struct strbuf *sb);
19
20/*
Johannes Schindelinc61008f2016-06-22 17:01:44 +020021 * Print a strbuf. If the graph is non-NULL, all lines but the first will be
22 * prefixed with the graph output.
Nanako Shiraishi064bfbd2008-09-25 18:41:08 +090023 *
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 *
Mike Ralphson3ea3c212009-04-17 19:13:30 +010028 * Since the first line will not include the graph output, the caller is
Nanako Shiraishi064bfbd2008-09-25 18:41:08 +090029 * responsible for printing this line's graph (perhaps via
30 * graph_show_commit() or graph_show_oneline()) before calling
31 * graph_show_strbuf().
Jacob Keller660e1132016-08-31 16:27:20 -070032 *
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.
Heba Waly3f1480b2019-11-17 21:04:42 +000037 * If a NULL graph is supplied, the strbuf is printed as-is.
Nanako Shiraishi064bfbd2008-09-25 18:41:08 +090038 */
Jacob Keller660e1132016-08-31 16:27:20 -070039static void graph_show_strbuf(struct git_graph *graph,
40 FILE *file,
41 struct strbuf const *sb);
Nanako Shiraishi064bfbd2008-09-25 18:41:08 +090042
Adam Simpkinsc12172d2008-05-04 03:36:53 -070043/*
44 * TODO:
Adam Simpkinsc12172d2008-05-04 03:36:53 -070045 * - Limit the number of columns, similar to the way gitk does.
46 * If we reach more than a specified number of columns, omit
47 * sections of some columns.
Adam Simpkinsc12172d2008-05-04 03:36:53 -070048 */
49
50struct column {
51 /*
52 * The parent commit of this column.
53 */
54 struct commit *commit;
55 /*
Allan Caffee427fc5b2009-04-13 15:53:41 -040056 * The color to (optionally) print this column in. This is an
57 * index into column_colors.
Adam Simpkinsc12172d2008-05-04 03:36:53 -070058 */
Allan Caffee427fc5b2009-04-13 15:53:41 -040059 unsigned short color;
Adam Simpkinsc12172d2008-05-04 03:36:53 -070060};
61
62enum graph_state {
63 GRAPH_PADDING,
64 GRAPH_SKIP,
65 GRAPH_PRE_COMMIT,
66 GRAPH_COMMIT,
67 GRAPH_POST_MERGE,
68 GRAPH_COLLAPSING
69};
70
Jacob Keller660e1132016-08-31 16:27:20 -070071static void graph_show_line_prefix(const struct diff_options *diffopt)
72{
73 if (!diffopt || !diffopt->line_prefix)
74 return;
75
76 fwrite(diffopt->line_prefix,
77 sizeof(char),
78 diffopt->line_prefix_length,
79 diffopt->file);
80}
81
Johan Herland1e3d4112010-07-13 23:23:39 +020082static const char **column_colors;
83static unsigned short column_colors_max;
84
Jeff Kingef8d7ac2020-07-28 16:24:53 -040085static void parse_graph_colors_config(struct strvec *colors, const char *string)
Nguyễn Thái Ngọc Duy73c727d2017-01-19 18:41:23 +070086{
87 const char *end, *start;
88
89 start = string;
90 end = string + strlen(string);
91 while (start < end) {
92 const char *comma = strchrnul(start, ',');
93 char color[COLOR_MAXLEN];
94
95 if (!color_parse_mem(start, comma - start, color))
Jeff Kingef8d7ac2020-07-28 16:24:53 -040096 strvec_push(colors, color);
Nguyễn Thái Ngọc Duy73c727d2017-01-19 18:41:23 +070097 else
Alex Henrie98538302021-06-12 12:41:44 -060098 warning(_("ignored invalid color '%.*s' in log.graphColors"),
Nguyễn Thái Ngọc Duy73c727d2017-01-19 18:41:23 +070099 (int)(comma - start), start);
100 start = comma + 1;
101 }
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400102 strvec_push(colors, GIT_COLOR_RESET);
Nguyễn Thái Ngọc Duy73c727d2017-01-19 18:41:23 +0700103}
104
John Keepingac751a02013-03-04 00:03:37 +0000105void graph_set_column_colors(const char **colors, unsigned short colors_max)
Allan Caffee427fc5b2009-04-13 15:53:41 -0400106{
Johan Herland1e3d4112010-07-13 23:23:39 +0200107 column_colors = colors;
108 column_colors_max = colors_max;
109}
110
111static const char *column_get_color_code(unsigned short color)
112{
113 return column_colors[color];
Allan Caffee427fc5b2009-04-13 15:53:41 -0400114}
115
James Coglanfbccf252019-10-15 23:47:47 +0000116struct graph_line {
117 struct strbuf *buf;
118 size_t width;
119};
120
121static inline void graph_line_addch(struct graph_line *line, int c)
122{
123 strbuf_addch(line->buf, c);
124 line->width++;
125}
126
127static inline void graph_line_addchars(struct graph_line *line, int c, size_t n)
128{
129 strbuf_addchars(line->buf, c, n);
130 line->width += n;
131}
132
133static inline void graph_line_addstr(struct graph_line *line, const char *s)
134{
135 strbuf_addstr(line->buf, s);
136 line->width += strlen(s);
137}
138
139static inline void graph_line_addcolor(struct graph_line *line, unsigned short color)
140{
141 strbuf_addstr(line->buf, column_get_color_code(color));
142}
143
144static void graph_line_write_column(struct graph_line *line, const struct column *c,
145 char col_char)
Allan Caffee427fc5b2009-04-13 15:53:41 -0400146{
Johan Herland1e3d4112010-07-13 23:23:39 +0200147 if (c->color < column_colors_max)
James Coglanfbccf252019-10-15 23:47:47 +0000148 graph_line_addcolor(line, c->color);
149 graph_line_addch(line, col_char);
Johan Herland1e3d4112010-07-13 23:23:39 +0200150 if (c->color < column_colors_max)
James Coglanfbccf252019-10-15 23:47:47 +0000151 graph_line_addcolor(line, column_colors_max);
Allan Caffee427fc5b2009-04-13 15:53:41 -0400152}
153
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700154struct git_graph {
155 /*
156 * The commit currently being processed
157 */
158 struct commit *commit;
Adam Simpkins7528f272008-05-25 00:07:21 -0700159 /* The rev-info used for the current traversal */
160 struct rev_info *revs;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700161 /*
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700162 * The number of interesting parents that this commit has.
163 *
164 * Note that this is not the same as the actual number of parents.
165 * This count excludes parents that won't be printed in the graph
166 * output, as determined by graph_is_interesting().
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700167 */
168 int num_parents;
169 /*
Adam Simpkins0724cb82008-05-05 00:57:03 -0700170 * The width of the graph output for this commit.
171 * All rows for this commit are padded to this width, so that
172 * messages printed after the graph output are aligned.
173 */
174 int width;
175 /*
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700176 * The next expansion row to print
177 * when state is GRAPH_PRE_COMMIT
178 */
179 int expansion_row;
180 /*
181 * The current output state.
182 * This tells us what kind of line graph_next_line() should output.
183 */
184 enum graph_state state;
185 /*
Adam Simpkins33959082008-06-01 13:56:57 -0700186 * The output state for the previous line of output.
187 * This is primarily used to determine how the first merge line
188 * should appear, based on the last line of the previous commit.
189 */
190 enum graph_state prev_state;
191 /*
192 * The index of the column that refers to this commit.
193 *
194 * If none of the incoming columns refer to this commit,
195 * this will be equal to num_columns.
196 */
197 int commit_index;
198 /*
199 * The commit_index for the previously displayed commit.
200 *
201 * This is used to determine how the first line of a merge
202 * graph output should appear, based on the last line of the
203 * previous commit.
204 */
205 int prev_commit_index;
206 /*
James Coglan0f0f3892019-10-15 23:47:54 +0000207 * Which layout variant to use to display merge commits. If the
208 * commit's first parent is known to be in a column to the left of the
209 * merge, then this value is 0 and we use the layout on the left.
210 * Otherwise, the value is 1 and the layout on the right is used. This
211 * field tells us how many columns the first parent occupies.
212 *
213 * 0) 1)
214 *
215 * | | | *-. | | *---.
216 * | |_|/|\ \ | | |\ \ \
217 * |/| | | | | | | | | | *
218 */
219 int merge_layout;
220 /*
James Cogland62893e2019-10-15 23:47:55 +0000221 * The number of columns added to the graph by the current commit. For
ryenus571fb962019-12-15 15:12:24 +0000222 * 2-way and octopus merges, this is usually one less than the
James Cogland62893e2019-10-15 23:47:55 +0000223 * number of parents:
224 *
225 * | | | | | \
226 * | * | | *---. \
227 * | |\ \ | |\ \ \ \
228 * | | | | | | | | | |
229 *
230 * num_parents: 2 num_parents: 4
231 * edges_added: 1 edges_added: 3
232 *
233 * For left-skewed merges, the first parent fuses with its neighbor and
234 * so one less column is added:
235 *
236 * | | | | | \
237 * | * | | *-. \
238 * |/| | |/|\ \ \
239 * | | | | | | | |
240 *
241 * num_parents: 2 num_parents: 4
242 * edges_added: 0 edges_added: 2
243 *
244 * This number determines how edges to the right of the merge are
245 * displayed in commit and post-merge lines; if no columns have been
246 * added then a vertical line should be used where a right-tracking
247 * line would otherwise be used.
248 *
249 * | * \ | * |
250 * | |\ \ |/| |
251 * | | * \ | * |
252 */
253 int edges_added;
254 /*
255 * The number of columns added by the previous commit, which is used to
256 * smooth edges appearing to the right of a commit in a commit line
257 * following a post-merge line.
258 */
259 int prev_edges_added;
260 /*
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700261 * The maximum number of columns that can be stored in the columns
262 * and new_columns arrays. This is also half the number of entries
James Coglan01952852019-10-15 23:47:56 +0000263 * that can be stored in the mapping and old_mapping arrays.
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700264 */
265 int column_capacity;
266 /*
267 * The number of columns (also called "branch lines" in some places)
268 */
269 int num_columns;
270 /*
271 * The number of columns in the new_columns array
272 */
273 int num_new_columns;
274 /*
275 * The number of entries in the mapping array
276 */
277 int mapping_size;
278 /*
279 * The column state before we output the current commit.
280 */
281 struct column *columns;
282 /*
283 * The new column state after we output the current commit.
284 * Only valid when state is GRAPH_COLLAPSING.
285 */
286 struct column *new_columns;
287 /*
288 * An array that tracks the current state of each
289 * character in the output line during state GRAPH_COLLAPSING.
290 * Each entry is -1 if this character is empty, or a non-negative
291 * integer if the character contains a branch line. The value of
292 * the integer indicates the target position for this branch line.
293 * (I.e., this array maps the current column positions to their
294 * desired positions.)
295 *
296 * The maximum capacity of this array is always
297 * sizeof(int) * 2 * column_capacity.
298 */
299 int *mapping;
300 /*
James Coglan479db182019-10-15 23:47:57 +0000301 * A copy of the contents of the mapping array from the last commit,
302 * which we use to improve the display of columns that are tracking
303 * from right to left through a commit line. We also use this to
304 * avoid allocating a fresh array when we compute the next mapping.
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700305 */
James Coglan01952852019-10-15 23:47:56 +0000306 int *old_mapping;
Allan Caffee427fc5b2009-04-13 15:53:41 -0400307 /*
308 * The current default column color being used. This is
309 * stored as an index into the array column_colors.
310 */
311 unsigned short default_column_color;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700312};
313
Bo Yangb5a4de92010-05-26 15:23:56 +0800314static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void *data)
315{
316 struct git_graph *graph = data;
317 static struct strbuf msgbuf = STRBUF_INIT;
318
Lucian Poston5e71a842012-04-16 03:44:50 -0700319 assert(opt);
Bo Yangb5a4de92010-05-26 15:23:56 +0800320
321 strbuf_reset(&msgbuf);
Jacob Keller660e1132016-08-31 16:27:20 -0700322 if (opt->line_prefix)
323 strbuf_add(&msgbuf, opt->line_prefix,
324 opt->line_prefix_length);
325 if (graph)
326 graph_padding_line(graph, &msgbuf);
Bo Yangb5a4de92010-05-26 15:23:56 +0800327 return &msgbuf;
328}
329
Jacob Keller660e1132016-08-31 16:27:20 -0700330static const struct diff_options *default_diffopt;
331
332void graph_setup_line_prefix(struct diff_options *diffopt)
333{
334 default_diffopt = diffopt;
335
336 /* setup an output prefix callback if necessary */
337 if (diffopt && !diffopt->output_prefix)
338 diffopt->output_prefix = diff_output_prefix_callback;
339}
340
341
Adam Simpkins7528f272008-05-25 00:07:21 -0700342struct git_graph *graph_init(struct rev_info *opt)
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700343{
344 struct git_graph *graph = xmalloc(sizeof(struct git_graph));
Johan Herland1e3d4112010-07-13 23:23:39 +0200345
Nguyễn Thái Ngọc Duy73c727d2017-01-19 18:41:23 +0700346 if (!column_colors) {
347 char *string;
348 if (git_config_get_string("log.graphcolors", &string)) {
349 /* not configured -- use default */
350 graph_set_column_colors(column_colors_ansi,
351 column_colors_ansi_max);
352 } else {
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400353 static struct strvec custom_colors = STRVEC_INIT;
354 strvec_clear(&custom_colors);
Nguyễn Thái Ngọc Duy73c727d2017-01-19 18:41:23 +0700355 parse_graph_colors_config(&custom_colors, string);
356 free(string);
357 /* graph_set_column_colors takes a max-index, not a count */
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400358 graph_set_column_colors(custom_colors.v,
359 custom_colors.nr - 1);
Nguyễn Thái Ngọc Duy73c727d2017-01-19 18:41:23 +0700360 }
361 }
Johan Herland1e3d4112010-07-13 23:23:39 +0200362
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700363 graph->commit = NULL;
Adam Simpkins7528f272008-05-25 00:07:21 -0700364 graph->revs = opt;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700365 graph->num_parents = 0;
366 graph->expansion_row = 0;
367 graph->state = GRAPH_PADDING;
Adam Simpkins33959082008-06-01 13:56:57 -0700368 graph->prev_state = GRAPH_PADDING;
369 graph->commit_index = 0;
370 graph->prev_commit_index = 0;
James Coglan0f0f3892019-10-15 23:47:54 +0000371 graph->merge_layout = 0;
James Cogland62893e2019-10-15 23:47:55 +0000372 graph->edges_added = 0;
373 graph->prev_edges_added = 0;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700374 graph->num_columns = 0;
375 graph->num_new_columns = 0;
376 graph->mapping_size = 0;
Adam Simpkins91e50b22009-08-18 14:41:12 -0700377 /*
378 * Start the column color at the maximum value, since we'll
379 * always increment it for the first commit we output.
380 * This way we start at 0 for the first commit.
381 */
Johan Herland1e3d4112010-07-13 23:23:39 +0200382 graph->default_column_color = column_colors_max - 1;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700383
384 /*
385 * Allocate a reasonably large default number of columns
386 * We'll automatically grow columns later if we need more room.
387 */
388 graph->column_capacity = 30;
Jeff Kingb32fa952016-02-22 17:44:25 -0500389 ALLOC_ARRAY(graph->columns, graph->column_capacity);
390 ALLOC_ARRAY(graph->new_columns, graph->column_capacity);
391 ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity);
James Coglan01952852019-10-15 23:47:56 +0000392 ALLOC_ARRAY(graph->old_mapping, 2 * graph->column_capacity);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700393
Bo Yangb5a4de92010-05-26 15:23:56 +0800394 /*
395 * The diff output prefix callback, with this we can make
396 * all the diff output to align with the graph lines.
397 */
398 opt->diffopt.output_prefix = diff_output_prefix_callback;
399 opt->diffopt.output_prefix_data = graph;
400
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700401 return graph;
402}
403
Adam Simpkins33959082008-06-01 13:56:57 -0700404static void graph_update_state(struct git_graph *graph, enum graph_state s)
405{
406 graph->prev_state = graph->state;
407 graph->state = s;
408}
409
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700410static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
411{
412 if (graph->column_capacity >= num_columns)
413 return;
414
415 do {
416 graph->column_capacity *= 2;
417 } while (graph->column_capacity < num_columns);
418
René Scharfe2756ca42014-09-16 20:56:57 +0200419 REALLOC_ARRAY(graph->columns, graph->column_capacity);
420 REALLOC_ARRAY(graph->new_columns, graph->column_capacity);
421 REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2);
James Coglan01952852019-10-15 23:47:56 +0000422 REALLOC_ARRAY(graph->old_mapping, graph->column_capacity * 2);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700423}
424
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700425/*
426 * Returns 1 if the commit will be printed in the graph output,
427 * and 0 otherwise.
428 */
Adam Simpkins3c68d672008-05-24 16:02:04 -0700429static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700430{
431 /*
Adam Simpkins3c68d672008-05-24 16:02:04 -0700432 * If revs->boundary is set, commits whose children have
433 * been shown are always interesting, even if they have the
434 * UNINTERESTING or TREESAME flags set.
Adam Simpkins3c68d672008-05-24 16:02:04 -0700435 */
436 if (graph->revs && graph->revs->boundary) {
Adam Simpkins4603ec02008-05-24 16:02:05 -0700437 if (commit->object.flags & CHILD_SHOWN)
Adam Simpkins3c68d672008-05-24 16:02:04 -0700438 return 1;
439 }
440
441 /*
Adam Simpkinsbeb5af42009-08-18 19:34:33 -0700442 * Otherwise, use get_commit_action() to see if this commit is
443 * interesting
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700444 */
Adam Simpkinsbeb5af42009-08-18 19:34:33 -0700445 return get_commit_action(graph->revs, commit) == commit_show;
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700446}
447
Adam Simpkinsa0ebe572008-06-05 01:56:19 -0700448static struct commit_list *next_interesting_parent(struct git_graph *graph,
449 struct commit_list *orig)
450{
451 struct commit_list *list;
452
453 /*
454 * If revs->first_parent_only is set, only the first
455 * parent is interesting. None of the others are.
456 */
457 if (graph->revs->first_parent_only)
458 return NULL;
459
460 /*
461 * Return the next interesting commit after orig
462 */
463 for (list = orig->next; list; list = list->next) {
464 if (graph_is_interesting(graph, list->item))
465 return list;
466 }
467
468 return NULL;
469}
470
471static struct commit_list *first_interesting_parent(struct git_graph *graph)
472{
473 struct commit_list *parents = graph->commit->parents;
474
475 /*
476 * If this commit has no parents, ignore it
477 */
478 if (!parents)
479 return NULL;
480
481 /*
482 * If the first parent is interesting, return it
483 */
484 if (graph_is_interesting(graph, parents->item))
485 return parents;
486
487 /*
488 * Otherwise, call next_interesting_parent() to get
489 * the next interesting parent
490 */
491 return next_interesting_parent(graph, parents);
492}
493
Allan Caffee427fc5b2009-04-13 15:53:41 -0400494static unsigned short graph_get_current_column_color(const struct git_graph *graph)
495{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700496 if (!want_color(graph->revs->diffopt.use_color))
Johan Herland1e3d4112010-07-13 23:23:39 +0200497 return column_colors_max;
Allan Caffee427fc5b2009-04-13 15:53:41 -0400498 return graph->default_column_color;
499}
500
501/*
502 * Update the graph's default column color.
503 */
504static void graph_increment_column_color(struct git_graph *graph)
505{
506 graph->default_column_color = (graph->default_column_color + 1) %
Johan Herland1e3d4112010-07-13 23:23:39 +0200507 column_colors_max;
Allan Caffee427fc5b2009-04-13 15:53:41 -0400508}
509
510static unsigned short graph_find_commit_color(const struct git_graph *graph,
511 const struct commit *commit)
512{
513 int i;
514 for (i = 0; i < graph->num_columns; i++) {
515 if (graph->columns[i].commit == commit)
516 return graph->columns[i].color;
517 }
518 return graph_get_current_column_color(graph);
519}
520
James Coglan9157a2a2019-10-15 23:47:49 +0000521static int graph_find_new_column_by_commit(struct git_graph *graph,
522 struct commit *commit)
523{
524 int i;
525 for (i = 0; i < graph->num_new_columns; i++) {
526 if (graph->new_columns[i].commit == commit)
527 return i;
528 }
529 return -1;
530}
531
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700532static void graph_insert_into_new_columns(struct git_graph *graph,
James Coglan0f0f3892019-10-15 23:47:54 +0000533 struct commit *commit,
534 int idx)
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700535{
James Coglan9157a2a2019-10-15 23:47:49 +0000536 int i = graph_find_new_column_by_commit(graph, commit);
James Coglan0f0f3892019-10-15 23:47:54 +0000537 int mapping_idx;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700538
539 /*
James Coglana551fd52019-10-15 23:47:50 +0000540 * If the commit is not already in the new_columns array, then add it
541 * and record it as being in the final column.
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700542 */
James Coglana551fd52019-10-15 23:47:50 +0000543 if (i < 0) {
544 i = graph->num_new_columns++;
545 graph->new_columns[i].commit = commit;
546 graph->new_columns[i].color = graph_find_commit_color(graph, commit);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700547 }
548
James Coglan0f0f3892019-10-15 23:47:54 +0000549 if (graph->num_parents > 1 && idx > -1 && graph->merge_layout == -1) {
550 /*
551 * If this is the first parent of a merge, choose a layout for
552 * the merge line based on whether the parent appears in a
553 * column to the left of the merge
554 */
555 int dist, shift;
556
557 dist = idx - i;
558 shift = (dist > 1) ? 2 * dist - 3 : 1;
559
560 graph->merge_layout = (dist > 0) ? 0 : 1;
James Coglan92beecc2019-10-15 23:47:58 +0000561 graph->edges_added = graph->num_parents + graph->merge_layout - 2;
562
James Coglan0f0f3892019-10-15 23:47:54 +0000563 mapping_idx = graph->width + (graph->merge_layout - 1) * shift;
564 graph->width += 2 * graph->merge_layout;
James Coglan92beecc2019-10-15 23:47:58 +0000565
566 } else if (graph->edges_added > 0 && i == graph->mapping[graph->width - 2]) {
567 /*
568 * If some columns have been added by a merge, but this commit
569 * was found in the last existing column, then adjust the
570 * numbers so that the two edges immediately join, i.e.:
571 *
572 * * | * |
573 * |\ \ => |\|
574 * | |/ | *
575 * | *
576 */
577 mapping_idx = graph->width - 2;
578 graph->edges_added = -1;
James Coglan0f0f3892019-10-15 23:47:54 +0000579 } else {
580 mapping_idx = graph->width;
581 graph->width += 2;
582 }
583
584 graph->mapping[mapping_idx] = i;
Adam Simpkins0724cb82008-05-05 00:57:03 -0700585}
586
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700587static void graph_update_columns(struct git_graph *graph)
588{
589 struct commit_list *parent;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700590 int max_new_columns;
Adam Simpkins0724cb82008-05-05 00:57:03 -0700591 int i, seen_this, is_commit_in_columns;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700592
593 /*
594 * Swap graph->columns with graph->new_columns
595 * graph->columns contains the state for the previous commit,
596 * and new_columns now contains the state for our commit.
597 *
598 * We'll re-use the old columns array as storage to compute the new
599 * columns list for the commit after this one.
600 */
René Scharfe9e2edd62017-01-28 22:42:15 +0100601 SWAP(graph->columns, graph->new_columns);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700602 graph->num_columns = graph->num_new_columns;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700603 graph->num_new_columns = 0;
604
605 /*
606 * Now update new_columns and mapping with the information for the
607 * commit after this one.
608 *
609 * First, make sure we have enough room. At most, there will
610 * be graph->num_columns + graph->num_parents columns for the next
611 * commit.
612 */
613 max_new_columns = graph->num_columns + graph->num_parents;
614 graph_ensure_capacity(graph, max_new_columns);
615
616 /*
617 * Clear out graph->mapping
618 */
619 graph->mapping_size = 2 * max_new_columns;
620 for (i = 0; i < graph->mapping_size; i++)
621 graph->mapping[i] = -1;
622
James Coglan46ba2ab2019-10-15 23:47:51 +0000623 graph->width = 0;
James Coglan92beecc2019-10-15 23:47:58 +0000624 graph->prev_edges_added = graph->edges_added;
625 graph->edges_added = 0;
James Coglan46ba2ab2019-10-15 23:47:51 +0000626
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700627 /*
628 * Populate graph->new_columns and graph->mapping
629 *
630 * Some of the parents of this commit may already be in
631 * graph->columns. If so, graph->new_columns should only contain a
632 * single entry for each such commit. graph->mapping should
633 * contain information about where each current branch line is
634 * supposed to end up after the collapsing is performed.
635 */
636 seen_this = 0;
Adam Simpkins0724cb82008-05-05 00:57:03 -0700637 is_commit_in_columns = 1;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700638 for (i = 0; i <= graph->num_columns; i++) {
639 struct commit *col_commit;
640 if (i == graph->num_columns) {
641 if (seen_this)
642 break;
Adam Simpkins0724cb82008-05-05 00:57:03 -0700643 is_commit_in_columns = 0;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700644 col_commit = graph->commit;
645 } else {
646 col_commit = graph->columns[i].commit;
647 }
648
649 if (col_commit == graph->commit) {
650 seen_this = 1;
Adam Simpkins33959082008-06-01 13:56:57 -0700651 graph->commit_index = i;
James Coglan0f0f3892019-10-15 23:47:54 +0000652 graph->merge_layout = -1;
Adam Simpkinsa0ebe572008-06-05 01:56:19 -0700653 for (parent = first_interesting_parent(graph);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700654 parent;
Adam Simpkinsa0ebe572008-06-05 01:56:19 -0700655 parent = next_interesting_parent(graph, parent)) {
Allan Caffee427fc5b2009-04-13 15:53:41 -0400656 /*
Adam Simpkins91e50b22009-08-18 14:41:12 -0700657 * If this is a merge, or the start of a new
658 * childless column, increment the current
Allan Caffee427fc5b2009-04-13 15:53:41 -0400659 * color.
660 */
Adam Simpkins91e50b22009-08-18 14:41:12 -0700661 if (graph->num_parents > 1 ||
662 !is_commit_in_columns) {
Allan Caffee427fc5b2009-04-13 15:53:41 -0400663 graph_increment_column_color(graph);
Adam Simpkins91e50b22009-08-18 14:41:12 -0700664 }
James Coglan0f0f3892019-10-15 23:47:54 +0000665 graph_insert_into_new_columns(graph, parent->item, i);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700666 }
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700667 /*
James Coglan46ba2ab2019-10-15 23:47:51 +0000668 * We always need to increment graph->width by at
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700669 * least 2, even if it has no interesting parents.
670 * The current commit always takes up at least 2
671 * spaces.
672 */
James Coglan46ba2ab2019-10-15 23:47:51 +0000673 if (graph->num_parents == 0)
674 graph->width += 2;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700675 } else {
James Coglan0f0f3892019-10-15 23:47:54 +0000676 graph_insert_into_new_columns(graph, col_commit, -1);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700677 }
678 }
679
680 /*
681 * Shrink mapping_size to be the minimum necessary
682 */
683 while (graph->mapping_size > 1 &&
684 graph->mapping[graph->mapping_size - 1] < 0)
685 graph->mapping_size--;
686}
687
James Coglanbbb13e82019-10-15 23:47:59 +0000688static int graph_num_dashed_parents(struct git_graph *graph)
689{
690 return graph->num_parents + graph->merge_layout - 3;
691}
692
James Coglan0f0f3892019-10-15 23:47:54 +0000693static int graph_num_expansion_rows(struct git_graph *graph)
694{
695 /*
696 * Normally, we need two expansion rows for each dashed parent line from
697 * an octopus merge:
698 *
699 * | *
700 * | |\
701 * | | \
702 * | | \
703 * | *-. \
704 * | |\ \ \
705 *
706 * If the merge is skewed to the left, then its parents occupy one less
707 * column, and we don't need as many expansion rows to route around it;
708 * in some cases that means we don't need any expansion rows at all:
709 *
710 * | *
711 * | |\
712 * | * \
713 * |/|\ \
714 */
James Coglanbbb13e82019-10-15 23:47:59 +0000715 return graph_num_dashed_parents(graph) * 2;
James Coglan0f0f3892019-10-15 23:47:54 +0000716}
717
James Coglanee7abb52019-10-15 23:47:52 +0000718static int graph_needs_pre_commit_line(struct git_graph *graph)
719{
720 return graph->num_parents >= 3 &&
James Coglan0f0f3892019-10-15 23:47:54 +0000721 graph->commit_index < (graph->num_columns - 1) &&
722 graph->expansion_row < graph_num_expansion_rows(graph);
James Coglanee7abb52019-10-15 23:47:52 +0000723}
724
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700725void graph_update(struct git_graph *graph, struct commit *commit)
726{
727 struct commit_list *parent;
728
729 /*
730 * Set the new commit
731 */
732 graph->commit = commit;
733
734 /*
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700735 * Count how many interesting parents this commit has
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700736 */
737 graph->num_parents = 0;
Adam Simpkinsa0ebe572008-06-05 01:56:19 -0700738 for (parent = first_interesting_parent(graph);
739 parent;
740 parent = next_interesting_parent(graph, parent))
741 {
742 graph->num_parents++;
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700743 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700744
745 /*
Adam Simpkins33959082008-06-01 13:56:57 -0700746 * Store the old commit_index in prev_commit_index.
747 * graph_update_columns() will update graph->commit_index for this
748 * commit.
749 */
750 graph->prev_commit_index = graph->commit_index;
751
752 /*
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700753 * Call graph_update_columns() to update
754 * columns, new_columns, and mapping.
755 */
756 graph_update_columns(graph);
757
758 graph->expansion_row = 0;
759
760 /*
761 * Update graph->state.
Adam Simpkins33959082008-06-01 13:56:57 -0700762 * Note that we don't call graph_update_state() here, since
763 * we don't want to update graph->prev_state. No line for
764 * graph->state was ever printed.
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700765 *
766 * If the previous commit didn't get to the GRAPH_PADDING state,
767 * it never finished its output. Goto GRAPH_SKIP, to print out
768 * a line to indicate that portion of the graph is missing.
769 *
Adam Simpkinsf1979d62008-06-01 13:56:58 -0700770 * If there are 3 or more parents, we may need to print extra rows
771 * before the commit, to expand the branch lines around it and make
772 * room for it. We need to do this only if there is a branch row
773 * (or more) to the right of this commit.
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700774 *
775 * If there are less than 3 parents, we can immediately print the
776 * commit line.
777 */
778 if (graph->state != GRAPH_PADDING)
779 graph->state = GRAPH_SKIP;
James Coglanee7abb52019-10-15 23:47:52 +0000780 else if (graph_needs_pre_commit_line(graph))
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700781 graph->state = GRAPH_PRE_COMMIT;
782 else
783 graph->state = GRAPH_COMMIT;
784}
785
786static int graph_is_mapping_correct(struct git_graph *graph)
787{
788 int i;
789
790 /*
791 * The mapping is up to date if each entry is at its target,
792 * or is 1 greater than its target.
793 * (If it is 1 greater than the target, '/' will be printed, so it
794 * will look correct on the next row.)
795 */
796 for (i = 0; i < graph->mapping_size; i++) {
797 int target = graph->mapping[i];
798 if (target < 0)
799 continue;
800 if (target == (i / 2))
801 continue;
802 return 0;
803 }
804
805 return 1;
806}
807
James Coglanfbccf252019-10-15 23:47:47 +0000808static void graph_pad_horizontally(struct git_graph *graph, struct graph_line *line)
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700809{
810 /*
811 * Add additional spaces to the end of the strbuf, so that all
812 * lines for a particular commit have the same width.
813 *
814 * This way, fields printed to the right of the graph will remain
815 * aligned for the entire commit.
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700816 */
James Coglanfbccf252019-10-15 23:47:47 +0000817 if (line->width < graph->width)
818 graph_line_addchars(line, ' ', graph->width - line->width);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700819}
820
821static void graph_output_padding_line(struct git_graph *graph,
James Coglanfbccf252019-10-15 23:47:47 +0000822 struct graph_line *line)
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700823{
824 int i;
825
826 /*
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700827 * Output a padding row, that leaves all branch lines unchanged
828 */
829 for (i = 0; i < graph->num_new_columns; i++) {
James Coglanfbccf252019-10-15 23:47:47 +0000830 graph_line_write_column(line, &graph->new_columns[i], '|');
831 graph_line_addch(line, ' ');
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700832 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700833}
834
Josef Kufner3ad87c82016-06-16 20:18:37 +0700835
836int graph_width(struct git_graph *graph)
837{
838 return graph->width;
839}
840
841
James Coglanfbccf252019-10-15 23:47:47 +0000842static void graph_output_skip_line(struct git_graph *graph, struct graph_line *line)
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700843{
844 /*
845 * Output an ellipsis to indicate that a portion
846 * of the graph is missing.
847 */
James Coglanfbccf252019-10-15 23:47:47 +0000848 graph_line_addstr(line, "...");
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700849
James Coglanee7abb52019-10-15 23:47:52 +0000850 if (graph_needs_pre_commit_line(graph))
Adam Simpkins33959082008-06-01 13:56:57 -0700851 graph_update_state(graph, GRAPH_PRE_COMMIT);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700852 else
Adam Simpkins33959082008-06-01 13:56:57 -0700853 graph_update_state(graph, GRAPH_COMMIT);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700854}
855
856static void graph_output_pre_commit_line(struct git_graph *graph,
James Coglanfbccf252019-10-15 23:47:47 +0000857 struct graph_line *line)
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700858{
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700859 int i, seen_this;
860
861 /*
862 * This function formats a row that increases the space around a commit
863 * with multiple parents, to make room for it. It should only be
864 * called when there are 3 or more parents.
865 *
866 * We need 2 extra rows for every parent over 2.
867 */
868 assert(graph->num_parents >= 3);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700869
870 /*
871 * graph->expansion_row tracks the current expansion row we are on.
872 * It should be in the range [0, num_expansion_rows - 1]
873 */
874 assert(0 <= graph->expansion_row &&
James Coglan0f0f3892019-10-15 23:47:54 +0000875 graph->expansion_row < graph_num_expansion_rows(graph));
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700876
877 /*
878 * Output the row
879 */
880 seen_this = 0;
881 for (i = 0; i < graph->num_columns; i++) {
882 struct column *col = &graph->columns[i];
883 if (col->commit == graph->commit) {
884 seen_this = 1;
James Coglanfbccf252019-10-15 23:47:47 +0000885 graph_line_write_column(line, col, '|');
886 graph_line_addchars(line, ' ', graph->expansion_row);
Adam Simpkins33959082008-06-01 13:56:57 -0700887 } else if (seen_this && (graph->expansion_row == 0)) {
888 /*
889 * This is the first line of the pre-commit output.
890 * If the previous commit was a merge commit and
891 * ended in the GRAPH_POST_MERGE state, all branch
892 * lines after graph->prev_commit_index were
893 * printed as "\" on the previous line. Continue
894 * to print them as "\" on this line. Otherwise,
895 * print the branch lines as "|".
896 */
897 if (graph->prev_state == GRAPH_POST_MERGE &&
898 graph->prev_commit_index < i)
James Coglanfbccf252019-10-15 23:47:47 +0000899 graph_line_write_column(line, col, '\\');
Adam Simpkins33959082008-06-01 13:56:57 -0700900 else
James Coglanfbccf252019-10-15 23:47:47 +0000901 graph_line_write_column(line, col, '|');
Adam Simpkins33959082008-06-01 13:56:57 -0700902 } else if (seen_this && (graph->expansion_row > 0)) {
James Coglanfbccf252019-10-15 23:47:47 +0000903 graph_line_write_column(line, col, '\\');
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700904 } else {
James Coglanfbccf252019-10-15 23:47:47 +0000905 graph_line_write_column(line, col, '|');
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700906 }
James Coglanfbccf252019-10-15 23:47:47 +0000907 graph_line_addch(line, ' ');
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700908 }
909
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700910 /*
911 * Increment graph->expansion_row,
912 * and move to state GRAPH_COMMIT if necessary
913 */
914 graph->expansion_row++;
James Coglan0f0f3892019-10-15 23:47:54 +0000915 if (!graph_needs_pre_commit_line(graph))
Adam Simpkins33959082008-06-01 13:56:57 -0700916 graph_update_state(graph, GRAPH_COMMIT);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700917}
918
James Coglanfbccf252019-10-15 23:47:47 +0000919static void graph_output_commit_char(struct git_graph *graph, struct graph_line *line)
Adam Simpkins3c68d672008-05-24 16:02:04 -0700920{
921 /*
922 * For boundary commits, print 'o'
923 * (We should only see boundary commits when revs->boundary is set.)
924 */
925 if (graph->commit->object.flags & BOUNDARY) {
926 assert(graph->revs->boundary);
James Coglanfbccf252019-10-15 23:47:47 +0000927 graph_line_addch(line, 'o');
Adam Simpkins3c68d672008-05-24 16:02:04 -0700928 return;
929 }
930
931 /*
Michael J Gruber1df2d652011-03-07 13:31:39 +0100932 * get_revision_mark() handles all other cases without assert()
Adam Simpkins3c68d672008-05-24 16:02:04 -0700933 */
James Coglanfbccf252019-10-15 23:47:47 +0000934 graph_line_addstr(line, get_revision_mark(graph->revs, graph->commit));
Adam Simpkins3c68d672008-05-24 16:02:04 -0700935}
936
Allan Caffee427fc5b2009-04-13 15:53:41 -0400937/*
James Coglanfbccf252019-10-15 23:47:47 +0000938 * Draw the horizontal dashes of an octopus merge.
Allan Caffee427fc5b2009-04-13 15:53:41 -0400939 */
James Coglanfbccf252019-10-15 23:47:47 +0000940static void graph_draw_octopus_merge(struct git_graph *graph, struct graph_line *line)
Allan Caffee427fc5b2009-04-13 15:53:41 -0400941{
942 /*
James Coglanbbb13e82019-10-15 23:47:59 +0000943 * The parents of a merge commit can be arbitrarily reordered as they
944 * are mapped onto display columns, for example this is a valid merge:
Noam Postavsky04005832018-09-01 20:07:16 -0400945 *
James Coglanbbb13e82019-10-15 23:47:59 +0000946 * | | *---.
947 * | | |\ \ \
948 * | | |/ / /
949 * | |/| | /
950 * | |_|_|/
951 * |/| | |
952 * 3 1 0 2
Noam Postavsky04005832018-09-01 20:07:16 -0400953 *
James Coglanbbb13e82019-10-15 23:47:59 +0000954 * The numbers denote which parent of the merge each visual column
955 * corresponds to; we can't assume that the parents will initially
956 * display in the order given by new_columns.
957 *
958 * To find the right color for each dash, we need to consult the
959 * mapping array, starting from the column 2 places to the right of the
960 * merge commit, and use that to find out which logical column each
961 * edge will collapse to.
962 *
963 * Commits are rendered once all edges have collapsed to their correct
964 * logcial column, so commit_index gives us the right visual offset for
965 * the merge commit.
Allan Caffee427fc5b2009-04-13 15:53:41 -0400966 */
Noam Postavsky04005832018-09-01 20:07:16 -0400967
James Coglanbbb13e82019-10-15 23:47:59 +0000968 int i, j;
969 struct column *col;
Noam Postavsky04005832018-09-01 20:07:16 -0400970
James Coglanbbb13e82019-10-15 23:47:59 +0000971 int dashed_parents = graph_num_dashed_parents(graph);
Noam Postavsky04005832018-09-01 20:07:16 -0400972
James Coglanbbb13e82019-10-15 23:47:59 +0000973 for (i = 0; i < dashed_parents; i++) {
974 j = graph->mapping[(graph->commit_index + i + 2) * 2];
975 col = &graph->new_columns[j];
976
977 graph_line_write_column(line, col, '-');
978 graph_line_write_column(line, col, (i == dashed_parents - 1) ? '.' : '-');
Allan Caffee427fc5b2009-04-13 15:53:41 -0400979 }
James Coglanbbb13e82019-10-15 23:47:59 +0000980
981 return;
Allan Caffee427fc5b2009-04-13 15:53:41 -0400982}
983
James Coglanfbccf252019-10-15 23:47:47 +0000984static void graph_output_commit_line(struct git_graph *graph, struct graph_line *line)
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700985{
986 int seen_this = 0;
James Coglanfbccf252019-10-15 23:47:47 +0000987 int i;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700988
989 /*
990 * Output the row containing this commit
991 * Iterate up to and including graph->num_columns,
992 * since the current commit may not be in any of the existing
993 * columns. (This happens when the current commit doesn't have any
994 * children that we have already processed.)
995 */
996 seen_this = 0;
997 for (i = 0; i <= graph->num_columns; i++) {
Allan Caffee427fc5b2009-04-13 15:53:41 -0400998 struct column *col = &graph->columns[i];
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700999 struct commit *col_commit;
1000 if (i == graph->num_columns) {
1001 if (seen_this)
1002 break;
1003 col_commit = graph->commit;
1004 } else {
1005 col_commit = graph->columns[i].commit;
1006 }
1007
1008 if (col_commit == graph->commit) {
1009 seen_this = 1;
James Coglanfbccf252019-10-15 23:47:47 +00001010 graph_output_commit_char(graph, line);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001011
Allan Caffeea6c1a382009-04-22 17:27:59 -04001012 if (graph->num_parents > 2)
James Coglanfbccf252019-10-15 23:47:47 +00001013 graph_draw_octopus_merge(graph, line);
James Cogland62893e2019-10-15 23:47:55 +00001014 } else if (seen_this && (graph->edges_added > 1)) {
James Coglanfbccf252019-10-15 23:47:47 +00001015 graph_line_write_column(line, col, '\\');
James Cogland62893e2019-10-15 23:47:55 +00001016 } else if (seen_this && (graph->edges_added == 1)) {
Adam Simpkins33959082008-06-01 13:56:57 -07001017 /*
James Cogland62893e2019-10-15 23:47:55 +00001018 * This is either a right-skewed 2-way merge
1019 * commit, or a left-skewed 3-way merge.
1020 * There is no GRAPH_PRE_COMMIT stage for such
Adam Simpkins33959082008-06-01 13:56:57 -07001021 * merges, so this is the first line of output
1022 * for this commit. Check to see what the previous
1023 * line of output was.
1024 *
1025 * If it was GRAPH_POST_MERGE, the branch line
1026 * coming into this commit may have been '\',
1027 * and not '|' or '/'. If so, output the branch
1028 * line as '\' on this line, instead of '|'. This
1029 * makes the output look nicer.
1030 */
1031 if (graph->prev_state == GRAPH_POST_MERGE &&
James Cogland62893e2019-10-15 23:47:55 +00001032 graph->prev_edges_added > 0 &&
Adam Simpkins33959082008-06-01 13:56:57 -07001033 graph->prev_commit_index < i)
James Coglanfbccf252019-10-15 23:47:47 +00001034 graph_line_write_column(line, col, '\\');
Adam Simpkins33959082008-06-01 13:56:57 -07001035 else
James Coglanfbccf252019-10-15 23:47:47 +00001036 graph_line_write_column(line, col, '|');
James Coglan479db182019-10-15 23:47:57 +00001037 } else if (graph->prev_state == GRAPH_COLLAPSING &&
1038 graph->old_mapping[2 * i + 1] == i &&
1039 graph->mapping[2 * i] < i) {
1040 graph_line_write_column(line, col, '/');
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001041 } else {
James Coglanfbccf252019-10-15 23:47:47 +00001042 graph_line_write_column(line, col, '|');
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001043 }
James Coglanfbccf252019-10-15 23:47:47 +00001044 graph_line_addch(line, ' ');
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001045 }
1046
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001047 /*
1048 * Update graph->state
1049 */
1050 if (graph->num_parents > 1)
Adam Simpkins33959082008-06-01 13:56:57 -07001051 graph_update_state(graph, GRAPH_POST_MERGE);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001052 else if (graph_is_mapping_correct(graph))
Adam Simpkins33959082008-06-01 13:56:57 -07001053 graph_update_state(graph, GRAPH_PADDING);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001054 else
Adam Simpkins33959082008-06-01 13:56:57 -07001055 graph_update_state(graph, GRAPH_COLLAPSING);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001056}
1057
Đoàn Trần Công Danh9d2152d2020-04-27 21:22:36 +07001058static const char merge_chars[] = {'/', '|', '\\'};
James Coglan0f0f3892019-10-15 23:47:54 +00001059
James Coglanfbccf252019-10-15 23:47:47 +00001060static void graph_output_post_merge_line(struct git_graph *graph, struct graph_line *line)
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001061{
1062 int seen_this = 0;
James Coglan92beecc2019-10-15 23:47:58 +00001063 int i, j;
James Coglan0f0f3892019-10-15 23:47:54 +00001064
1065 struct commit_list *first_parent = first_interesting_parent(graph);
Derrick Stoleea1087c92020-01-07 21:27:02 +00001066 struct column *parent_col = NULL;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001067
1068 /*
1069 * Output the post-merge row
1070 */
1071 for (i = 0; i <= graph->num_columns; i++) {
Allan Caffee427fc5b2009-04-13 15:53:41 -04001072 struct column *col = &graph->columns[i];
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001073 struct commit *col_commit;
1074 if (i == graph->num_columns) {
1075 if (seen_this)
1076 break;
1077 col_commit = graph->commit;
1078 } else {
Allan Caffee427fc5b2009-04-13 15:53:41 -04001079 col_commit = col->commit;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001080 }
1081
1082 if (col_commit == graph->commit) {
Allan Caffee427fc5b2009-04-13 15:53:41 -04001083 /*
1084 * Since the current commit is a merge find
1085 * the columns for the parent commits in
1086 * new_columns and use those to format the
1087 * edges.
1088 */
James Coglan0f0f3892019-10-15 23:47:54 +00001089 struct commit_list *parents = first_parent;
James Coglan9157a2a2019-10-15 23:47:49 +00001090 int par_column;
James Coglan0f0f3892019-10-15 23:47:54 +00001091 int idx = graph->merge_layout;
1092 char c;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001093 seen_this = 1;
Allan Caffee427fc5b2009-04-13 15:53:41 -04001094
James Coglan92beecc2019-10-15 23:47:58 +00001095 for (j = 0; j < graph->num_parents; j++) {
James Coglan9157a2a2019-10-15 23:47:49 +00001096 par_column = graph_find_new_column_by_commit(graph, parents->item);
1097 assert(par_column >= 0);
James Coglan0f0f3892019-10-15 23:47:54 +00001098
1099 c = merge_chars[idx];
1100 graph_line_write_column(line, &graph->new_columns[par_column], c);
James Coglan92beecc2019-10-15 23:47:58 +00001101 if (idx == 2) {
1102 if (graph->edges_added > 0 || j < graph->num_parents - 1)
1103 graph_line_addch(line, ' ');
1104 } else {
James Coglan0f0f3892019-10-15 23:47:54 +00001105 idx++;
James Coglan92beecc2019-10-15 23:47:58 +00001106 }
1107 parents = next_interesting_parent(graph, parents);
Allan Caffee427fc5b2009-04-13 15:53:41 -04001108 }
James Cogland62893e2019-10-15 23:47:55 +00001109 if (graph->edges_added == 0)
1110 graph_line_addch(line, ' ');
1111
Adam Simpkins33959082008-06-01 13:56:57 -07001112 } else if (seen_this) {
James Cogland62893e2019-10-15 23:47:55 +00001113 if (graph->edges_added > 0)
1114 graph_line_write_column(line, col, '\\');
1115 else
1116 graph_line_write_column(line, col, '|');
James Coglanfbccf252019-10-15 23:47:47 +00001117 graph_line_addch(line, ' ');
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001118 } else {
James Coglanfbccf252019-10-15 23:47:47 +00001119 graph_line_write_column(line, col, '|');
Derrick Stoleea1087c92020-01-07 21:27:02 +00001120 if (graph->merge_layout != 0 || i != graph->commit_index - 1) {
1121 if (parent_col)
1122 graph_line_write_column(
1123 line, parent_col, '_');
1124 else
1125 graph_line_addch(line, ' ');
1126 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001127 }
James Coglan0f0f3892019-10-15 23:47:54 +00001128
1129 if (col_commit == first_parent->item)
Derrick Stoleea1087c92020-01-07 21:27:02 +00001130 parent_col = col;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001131 }
1132
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001133 /*
1134 * Update graph->state
1135 */
1136 if (graph_is_mapping_correct(graph))
Adam Simpkins33959082008-06-01 13:56:57 -07001137 graph_update_state(graph, GRAPH_PADDING);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001138 else
Adam Simpkins33959082008-06-01 13:56:57 -07001139 graph_update_state(graph, GRAPH_COLLAPSING);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001140}
1141
James Coglanfbccf252019-10-15 23:47:47 +00001142static void graph_output_collapsing_line(struct git_graph *graph, struct graph_line *line)
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001143{
1144 int i;
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001145 short used_horizontal = 0;
1146 int horizontal_edge = -1;
1147 int horizontal_edge_target = -1;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001148
1149 /*
James Coglan01952852019-10-15 23:47:56 +00001150 * Swap the mapping and old_mapping arrays
1151 */
1152 SWAP(graph->mapping, graph->old_mapping);
1153
1154 /*
1155 * Clear out the mapping array
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001156 */
1157 for (i = 0; i < graph->mapping_size; i++)
James Coglan01952852019-10-15 23:47:56 +00001158 graph->mapping[i] = -1;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001159
1160 for (i = 0; i < graph->mapping_size; i++) {
James Coglan01952852019-10-15 23:47:56 +00001161 int target = graph->old_mapping[i];
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001162 if (target < 0)
1163 continue;
1164
1165 /*
1166 * Since update_columns() always inserts the leftmost
1167 * column first, each branch's target location should
1168 * always be either its current location or to the left of
1169 * its current location.
1170 *
1171 * We never have to move branches to the right. This makes
1172 * the graph much more legible, since whenever branches
1173 * cross, only one is moving directions.
1174 */
1175 assert(target * 2 <= i);
1176
1177 if (target * 2 == i) {
1178 /*
1179 * This column is already in the
1180 * correct place
1181 */
James Coglan01952852019-10-15 23:47:56 +00001182 assert(graph->mapping[i] == -1);
1183 graph->mapping[i] = target;
1184 } else if (graph->mapping[i - 1] < 0) {
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001185 /*
1186 * Nothing is to the left.
1187 * Move to the left by one
1188 */
James Coglan01952852019-10-15 23:47:56 +00001189 graph->mapping[i - 1] = target;
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001190 /*
1191 * If there isn't already an edge moving horizontally
1192 * select this one.
1193 */
1194 if (horizontal_edge == -1) {
1195 int j;
1196 horizontal_edge = i;
1197 horizontal_edge_target = target;
1198 /*
1199 * The variable target is the index of the graph
1200 * column, and therefore target*2+3 is the
1201 * actual screen column of the first horizontal
1202 * line.
1203 */
1204 for (j = (target * 2)+3; j < (i - 2); j += 2)
James Coglan01952852019-10-15 23:47:56 +00001205 graph->mapping[j] = target;
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001206 }
James Coglan01952852019-10-15 23:47:56 +00001207 } else if (graph->mapping[i - 1] == target) {
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001208 /*
1209 * There is a branch line to our left
1210 * already, and it is our target. We
1211 * combine with this line, since we share
1212 * the same parent commit.
1213 *
1214 * We don't have to add anything to the
James Coglan01952852019-10-15 23:47:56 +00001215 * output or mapping, since the
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001216 * existing branch line has already taken
1217 * care of it.
1218 */
1219 } else {
1220 /*
1221 * There is a branch line to our left,
1222 * but it isn't our target. We need to
1223 * cross over it.
1224 *
1225 * The space just to the left of this
1226 * branch should always be empty.
1227 */
James Coglan01952852019-10-15 23:47:56 +00001228 assert(graph->mapping[i - 1] > target);
1229 assert(graph->mapping[i - 2] < 0);
James Coglan01952852019-10-15 23:47:56 +00001230 graph->mapping[i - 2] = target;
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001231 /*
1232 * Mark this branch as the horizontal edge to
1233 * prevent any other edges from moving
1234 * horizontally.
1235 */
Derrick Stoleec958d3b2020-01-08 04:27:55 +00001236 if (horizontal_edge == -1) {
1237 int j;
1238 horizontal_edge_target = target;
1239 horizontal_edge = i - 1;
1240
1241 for (j = (target * 2) + 3; j < (i - 2); j += 2)
1242 graph->mapping[j] = target;
1243 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001244 }
1245 }
1246
1247 /*
James Coglan479db182019-10-15 23:47:57 +00001248 * Copy the current mapping array into old_mapping
1249 */
1250 COPY_ARRAY(graph->old_mapping, graph->mapping, graph->mapping_size);
1251
1252 /*
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001253 * The new mapping may be 1 smaller than the old mapping
1254 */
James Coglan01952852019-10-15 23:47:56 +00001255 if (graph->mapping[graph->mapping_size - 1] < 0)
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001256 graph->mapping_size--;
1257
1258 /*
1259 * Output out a line based on the new mapping info
1260 */
1261 for (i = 0; i < graph->mapping_size; i++) {
James Coglan01952852019-10-15 23:47:56 +00001262 int target = graph->mapping[i];
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001263 if (target < 0)
James Coglanfbccf252019-10-15 23:47:47 +00001264 graph_line_addch(line, ' ');
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001265 else if (target * 2 == i)
James Coglanfbccf252019-10-15 23:47:47 +00001266 graph_line_write_column(line, &graph->new_columns[target], '|');
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001267 else if (target == horizontal_edge_target &&
1268 i != horizontal_edge - 1) {
1269 /*
1270 * Set the mappings for all but the
1271 * first segment to -1 so that they
1272 * won't continue into the next line.
1273 */
1274 if (i != (target * 2)+3)
James Coglan01952852019-10-15 23:47:56 +00001275 graph->mapping[i] = -1;
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001276 used_horizontal = 1;
James Coglanfbccf252019-10-15 23:47:47 +00001277 graph_line_write_column(line, &graph->new_columns[target], '_');
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001278 } else {
1279 if (used_horizontal && i < horizontal_edge)
James Coglan01952852019-10-15 23:47:56 +00001280 graph->mapping[i] = -1;
James Coglanfbccf252019-10-15 23:47:47 +00001281 graph_line_write_column(line, &graph->new_columns[target], '/');
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001282
1283 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001284 }
1285
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001286 /*
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001287 * If graph->mapping indicates that all of the branch lines
1288 * are already in the correct positions, we are done.
1289 * Otherwise, we need to collapse some branch lines together.
1290 */
1291 if (graph_is_mapping_correct(graph))
Adam Simpkins33959082008-06-01 13:56:57 -07001292 graph_update_state(graph, GRAPH_PADDING);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001293}
1294
John Keepingac751a02013-03-04 00:03:37 +00001295int graph_next_line(struct git_graph *graph, struct strbuf *sb)
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001296{
James Coglan210179a2019-10-15 23:47:48 +00001297 int shown_commit_line = 0;
James Coglanfbccf252019-10-15 23:47:47 +00001298 struct graph_line line = { .buf = sb, .width = 0 };
1299
James Coglan210179a2019-10-15 23:47:48 +00001300 /*
1301 * We could conceivable be called with a NULL commit
1302 * if our caller has a bug, and invokes graph_next_line()
1303 * immediately after graph_init(), without first calling
1304 * graph_update(). Return without outputting anything in this
1305 * case.
1306 */
1307 if (!graph->commit)
1308 return -1;
1309
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001310 switch (graph->state) {
1311 case GRAPH_PADDING:
James Coglanfbccf252019-10-15 23:47:47 +00001312 graph_output_padding_line(graph, &line);
James Coglan210179a2019-10-15 23:47:48 +00001313 break;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001314 case GRAPH_SKIP:
James Coglanfbccf252019-10-15 23:47:47 +00001315 graph_output_skip_line(graph, &line);
James Coglan210179a2019-10-15 23:47:48 +00001316 break;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001317 case GRAPH_PRE_COMMIT:
James Coglanfbccf252019-10-15 23:47:47 +00001318 graph_output_pre_commit_line(graph, &line);
James Coglan210179a2019-10-15 23:47:48 +00001319 break;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001320 case GRAPH_COMMIT:
James Coglanfbccf252019-10-15 23:47:47 +00001321 graph_output_commit_line(graph, &line);
James Coglan210179a2019-10-15 23:47:48 +00001322 shown_commit_line = 1;
1323 break;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001324 case GRAPH_POST_MERGE:
James Coglanfbccf252019-10-15 23:47:47 +00001325 graph_output_post_merge_line(graph, &line);
James Coglan210179a2019-10-15 23:47:48 +00001326 break;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001327 case GRAPH_COLLAPSING:
James Coglanfbccf252019-10-15 23:47:47 +00001328 graph_output_collapsing_line(graph, &line);
James Coglan210179a2019-10-15 23:47:48 +00001329 break;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001330 }
1331
James Coglan210179a2019-10-15 23:47:48 +00001332 graph_pad_horizontally(graph, &line);
1333 return shown_commit_line;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001334}
1335
Nanako Shiraishi064bfbd2008-09-25 18:41:08 +09001336static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001337{
René Scharfe415792e2014-09-07 09:06:42 +02001338 int i;
James Coglanfbccf252019-10-15 23:47:47 +00001339 struct graph_line line = { .buf = sb, .width = 0 };
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001340
1341 if (graph->state != GRAPH_COMMIT) {
1342 graph_next_line(graph, sb);
1343 return;
1344 }
1345
1346 /*
1347 * Output the row containing this commit
1348 * Iterate up to and including graph->num_columns,
1349 * since the current commit may not be in any of the existing
1350 * columns. (This happens when the current commit doesn't have any
1351 * children that we have already processed.)
1352 */
1353 for (i = 0; i < graph->num_columns; i++) {
Allan Caffee427fc5b2009-04-13 15:53:41 -04001354 struct column *col = &graph->columns[i];
Jeff King16477932016-09-29 04:37:51 -04001355
James Coglanfbccf252019-10-15 23:47:47 +00001356 graph_line_write_column(&line, col, '|');
Jeff King16477932016-09-29 04:37:51 -04001357
1358 if (col->commit == graph->commit && graph->num_parents > 2) {
1359 int len = (graph->num_parents - 2) * 2;
James Coglanfbccf252019-10-15 23:47:47 +00001360 graph_line_addchars(&line, ' ', len);
Jeff King16477932016-09-29 04:37:51 -04001361 } else {
James Coglanfbccf252019-10-15 23:47:47 +00001362 graph_line_addch(&line, ' ');
Jeff King16477932016-09-29 04:37:51 -04001363 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001364 }
1365
James Coglanfbccf252019-10-15 23:47:47 +00001366 graph_pad_horizontally(graph, &line);
Adam Simpkins33959082008-06-01 13:56:57 -07001367
1368 /*
1369 * Update graph->prev_state since we have output a padding line
1370 */
1371 graph->prev_state = GRAPH_PADDING;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001372}
1373
1374int graph_is_commit_finished(struct git_graph const *graph)
1375{
1376 return (graph->state == GRAPH_PADDING);
1377}
1378
1379void graph_show_commit(struct git_graph *graph)
1380{
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001381 struct strbuf msgbuf = STRBUF_INIT;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001382 int shown_commit_line = 0;
1383
Jacob Keller660e1132016-08-31 16:27:20 -07001384 graph_show_line_prefix(default_diffopt);
1385
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001386 if (!graph)
1387 return;
1388
John Keepinga48ec242013-02-07 20:15:23 +00001389 /*
1390 * When showing a diff of a merge against each of its parents, we
1391 * are called once for each parent without graph_update having been
1392 * called. In this case, simply output a single padding line.
1393 */
1394 if (graph_is_commit_finished(graph)) {
1395 graph_show_padding(graph);
1396 shown_commit_line = 1;
1397 }
1398
Michał Kiedrowicz656197a2009-07-25 01:45:00 +02001399 while (!shown_commit_line && !graph_is_commit_finished(graph)) {
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001400 shown_commit_line = graph_next_line(graph, &msgbuf);
Johannes Schindelinc61008f2016-06-22 17:01:44 +02001401 fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
1402 graph->revs->diffopt.file);
Jacob Keller660e1132016-08-31 16:27:20 -07001403 if (!shown_commit_line) {
Johannes Schindelinc61008f2016-06-22 17:01:44 +02001404 putc('\n', graph->revs->diffopt.file);
Jacob Keller660e1132016-08-31 16:27:20 -07001405 graph_show_line_prefix(&graph->revs->diffopt);
1406 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001407 strbuf_setlen(&msgbuf, 0);
1408 }
1409
1410 strbuf_release(&msgbuf);
1411}
1412
1413void graph_show_oneline(struct git_graph *graph)
1414{
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001415 struct strbuf msgbuf = STRBUF_INIT;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001416
Jacob Keller660e1132016-08-31 16:27:20 -07001417 graph_show_line_prefix(default_diffopt);
1418
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001419 if (!graph)
1420 return;
1421
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001422 graph_next_line(graph, &msgbuf);
Johannes Schindelinc61008f2016-06-22 17:01:44 +02001423 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001424 strbuf_release(&msgbuf);
1425}
1426
1427void graph_show_padding(struct git_graph *graph)
1428{
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001429 struct strbuf msgbuf = STRBUF_INIT;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001430
Jacob Keller660e1132016-08-31 16:27:20 -07001431 graph_show_line_prefix(default_diffopt);
1432
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001433 if (!graph)
1434 return;
1435
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001436 graph_padding_line(graph, &msgbuf);
Johannes Schindelinc61008f2016-06-22 17:01:44 +02001437 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001438 strbuf_release(&msgbuf);
1439}
1440
1441int graph_show_remainder(struct git_graph *graph)
1442{
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001443 struct strbuf msgbuf = STRBUF_INIT;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001444 int shown = 0;
1445
Jacob Keller660e1132016-08-31 16:27:20 -07001446 graph_show_line_prefix(default_diffopt);
1447
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001448 if (!graph)
1449 return 0;
1450
1451 if (graph_is_commit_finished(graph))
1452 return 0;
1453
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001454 for (;;) {
1455 graph_next_line(graph, &msgbuf);
Johannes Schindelinc61008f2016-06-22 17:01:44 +02001456 fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
1457 graph->revs->diffopt.file);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001458 strbuf_setlen(&msgbuf, 0);
1459 shown = 1;
1460
Jacob Keller660e1132016-08-31 16:27:20 -07001461 if (!graph_is_commit_finished(graph)) {
Johannes Schindelinc61008f2016-06-22 17:01:44 +02001462 putc('\n', graph->revs->diffopt.file);
Jacob Keller660e1132016-08-31 16:27:20 -07001463 graph_show_line_prefix(&graph->revs->diffopt);
1464 } else {
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001465 break;
Jacob Keller660e1132016-08-31 16:27:20 -07001466 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001467 }
1468 strbuf_release(&msgbuf);
1469
1470 return shown;
1471}
1472
Jacob Keller660e1132016-08-31 16:27:20 -07001473static void graph_show_strbuf(struct git_graph *graph,
1474 FILE *file,
1475 struct strbuf const *sb)
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001476{
1477 char *p;
1478
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001479 /*
1480 * Print the strbuf line by line,
1481 * and display the graph info before each line but the first.
1482 */
1483 p = sb->buf;
1484 while (p) {
1485 size_t len;
1486 char *next_p = strchr(p, '\n');
1487 if (next_p) {
1488 next_p++;
1489 len = next_p - p;
1490 } else {
1491 len = (sb->buf + sb->len) - p;
1492 }
Jacob Keller660e1132016-08-31 16:27:20 -07001493 fwrite(p, sizeof(char), len, file);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001494 if (next_p && *next_p != '\0')
1495 graph_show_oneline(graph);
1496 p = next_p;
1497 }
1498}
1499
1500void graph_show_commit_msg(struct git_graph *graph,
Jacob Keller660e1132016-08-31 16:27:20 -07001501 FILE *file,
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001502 struct strbuf const *sb)
1503{
1504 int newline_terminated;
1505
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001506 /*
1507 * Show the commit message
1508 */
Jacob Keller660e1132016-08-31 16:27:20 -07001509 graph_show_strbuf(graph, file, sb);
1510
1511 if (!graph)
1512 return;
1513
1514 newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001515
1516 /*
1517 * If there is more output needed for this commit, show it now
1518 */
1519 if (!graph_is_commit_finished(graph)) {
1520 /*
1521 * If sb doesn't have a terminating newline, print one now,
1522 * so we can start the remainder of the graph output on a
1523 * new line.
1524 */
1525 if (!newline_terminated)
Jacob Keller660e1132016-08-31 16:27:20 -07001526 putc('\n', file);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001527
1528 graph_show_remainder(graph);
1529
1530 /*
1531 * If sb ends with a newline, our output should too.
1532 */
1533 if (newline_terminated)
Jacob Keller660e1132016-08-31 16:27:20 -07001534 putc('\n', file);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001535 }
1536}