blob: 1ca34770ee8139f25e0a2476c5aa3cd8b06fb89c [file] [log] [blame]
Elijah Newrenfc7bd512023-02-24 00:09:34 +00001#include "git-compat-util.h"
2#include "gettext.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Adam Simpkinsc12172d2008-05-04 03:36:53 -07004#include "commit.h"
Allan Caffee427fc5b2009-04-13 15:53:41 -04005#include "color.h"
Adam Simpkinsc12172d2008-05-04 03:36:53 -07006#include "graph.h"
Adam Simpkinsc12172d2008-05-04 03:36:53 -07007#include "revision.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -04008#include "strvec.h"
Adam Simpkinsc12172d2008-05-04 03:36:53 -07009
Nanako Shiraishi064bfbd2008-09-25 18:41:08 +090010/* Internal API */
11
12/*
Nanako Shiraishi064bfbd2008-09-25 18:41:08 +090013 * Output a padding line in the graph.
14 * This is similar to graph_next_line(). However, it is guaranteed to
15 * never print the current commit line. Instead, if the commit line is
16 * next, it will simply output a line of vertical padding, extending the
17 * branch lines downwards, but leaving them otherwise unchanged.
18 */
19static void graph_padding_line(struct git_graph *graph, struct strbuf *sb);
20
21/*
Johannes Schindelinc61008f2016-06-22 17:01:44 +020022 * Print a strbuf. If the graph is non-NULL, all lines but the first will be
23 * prefixed with the graph output.
Nanako Shiraishi064bfbd2008-09-25 18:41:08 +090024 *
25 * If the strbuf ends with a newline, the output will end after this
26 * newline. A new graph line will not be printed after the final newline.
27 * If the strbuf is empty, no output will be printed.
28 *
Mike Ralphson3ea3c212009-04-17 19:13:30 +010029 * Since the first line will not include the graph output, the caller is
Nanako Shiraishi064bfbd2008-09-25 18:41:08 +090030 * responsible for printing this line's graph (perhaps via
31 * graph_show_commit() or graph_show_oneline()) before calling
32 * graph_show_strbuf().
Jacob Keller660e1132016-08-31 16:27:20 -070033 *
34 * Note that unlike some other graph display functions, you must pass the file
35 * handle directly. It is assumed that this is the same file handle as the
36 * file specified by the graph diff options. This is necessary so that
37 * graph_show_strbuf can be called even with a NULL graph.
Heba Waly3f1480b2019-11-17 21:04:42 +000038 * If a NULL graph is supplied, the strbuf is printed as-is.
Nanako Shiraishi064bfbd2008-09-25 18:41:08 +090039 */
Jacob Keller660e1132016-08-31 16:27:20 -070040static void graph_show_strbuf(struct git_graph *graph,
41 FILE *file,
42 struct strbuf const *sb);
Nanako Shiraishi064bfbd2008-09-25 18:41:08 +090043
Adam Simpkinsc12172d2008-05-04 03:36:53 -070044/*
45 * TODO:
Adam Simpkinsc12172d2008-05-04 03:36:53 -070046 * - Limit the number of columns, similar to the way gitk does.
47 * If we reach more than a specified number of columns, omit
48 * sections of some columns.
Adam Simpkinsc12172d2008-05-04 03:36:53 -070049 */
50
51struct column {
52 /*
53 * The parent commit of this column.
54 */
55 struct commit *commit;
56 /*
Allan Caffee427fc5b2009-04-13 15:53:41 -040057 * The color to (optionally) print this column in. This is an
58 * index into column_colors.
Adam Simpkinsc12172d2008-05-04 03:36:53 -070059 */
Allan Caffee427fc5b2009-04-13 15:53:41 -040060 unsigned short color;
Adam Simpkinsc12172d2008-05-04 03:36:53 -070061};
62
63enum graph_state {
64 GRAPH_PADDING,
65 GRAPH_SKIP,
66 GRAPH_PRE_COMMIT,
67 GRAPH_COMMIT,
68 GRAPH_POST_MERGE,
69 GRAPH_COLLAPSING
70};
71
Jacob Keller660e1132016-08-31 16:27:20 -070072static void graph_show_line_prefix(const struct diff_options *diffopt)
73{
74 if (!diffopt || !diffopt->line_prefix)
75 return;
76
77 fwrite(diffopt->line_prefix,
78 sizeof(char),
79 diffopt->line_prefix_length,
80 diffopt->file);
81}
82
Johan Herland1e3d4112010-07-13 23:23:39 +020083static const char **column_colors;
84static unsigned short column_colors_max;
85
Jeff Kingef8d7ac2020-07-28 16:24:53 -040086static void parse_graph_colors_config(struct strvec *colors, const char *string)
Nguyễn Thái Ngọc Duy73c727d2017-01-19 18:41:23 +070087{
88 const char *end, *start;
89
90 start = string;
91 end = string + strlen(string);
92 while (start < end) {
93 const char *comma = strchrnul(start, ',');
94 char color[COLOR_MAXLEN];
95
96 if (!color_parse_mem(start, comma - start, color))
Jeff Kingef8d7ac2020-07-28 16:24:53 -040097 strvec_push(colors, color);
Nguyễn Thái Ngọc Duy73c727d2017-01-19 18:41:23 +070098 else
Alex Henrie98538302021-06-12 12:41:44 -060099 warning(_("ignored invalid color '%.*s' in log.graphColors"),
Nguyễn Thái Ngọc Duy73c727d2017-01-19 18:41:23 +0700100 (int)(comma - start), start);
101 start = comma + 1;
102 }
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400103 strvec_push(colors, GIT_COLOR_RESET);
Nguyễn Thái Ngọc Duy73c727d2017-01-19 18:41:23 +0700104}
105
John Keepingac751a02013-03-04 00:03:37 +0000106void graph_set_column_colors(const char **colors, unsigned short colors_max)
Allan Caffee427fc5b2009-04-13 15:53:41 -0400107{
Johan Herland1e3d4112010-07-13 23:23:39 +0200108 column_colors = colors;
109 column_colors_max = colors_max;
110}
111
112static const char *column_get_color_code(unsigned short color)
113{
114 return column_colors[color];
Allan Caffee427fc5b2009-04-13 15:53:41 -0400115}
116
James Coglanfbccf252019-10-15 23:47:47 +0000117struct graph_line {
118 struct strbuf *buf;
119 size_t width;
120};
121
122static inline void graph_line_addch(struct graph_line *line, int c)
123{
124 strbuf_addch(line->buf, c);
125 line->width++;
126}
127
128static inline void graph_line_addchars(struct graph_line *line, int c, size_t n)
129{
130 strbuf_addchars(line->buf, c, n);
131 line->width += n;
132}
133
134static inline void graph_line_addstr(struct graph_line *line, const char *s)
135{
136 strbuf_addstr(line->buf, s);
137 line->width += strlen(s);
138}
139
140static inline void graph_line_addcolor(struct graph_line *line, unsigned short color)
141{
142 strbuf_addstr(line->buf, column_get_color_code(color));
143}
144
145static void graph_line_write_column(struct graph_line *line, const struct column *c,
146 char col_char)
Allan Caffee427fc5b2009-04-13 15:53:41 -0400147{
Johan Herland1e3d4112010-07-13 23:23:39 +0200148 if (c->color < column_colors_max)
James Coglanfbccf252019-10-15 23:47:47 +0000149 graph_line_addcolor(line, c->color);
150 graph_line_addch(line, col_char);
Johan Herland1e3d4112010-07-13 23:23:39 +0200151 if (c->color < column_colors_max)
James Coglanfbccf252019-10-15 23:47:47 +0000152 graph_line_addcolor(line, column_colors_max);
Allan Caffee427fc5b2009-04-13 15:53:41 -0400153}
154
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700155struct git_graph {
156 /*
157 * The commit currently being processed
158 */
159 struct commit *commit;
Adam Simpkins7528f272008-05-25 00:07:21 -0700160 /* The rev-info used for the current traversal */
161 struct rev_info *revs;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700162 /*
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700163 * The number of interesting parents that this commit has.
164 *
165 * Note that this is not the same as the actual number of parents.
166 * This count excludes parents that won't be printed in the graph
167 * output, as determined by graph_is_interesting().
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700168 */
169 int num_parents;
170 /*
Adam Simpkins0724cb82008-05-05 00:57:03 -0700171 * The width of the graph output for this commit.
172 * All rows for this commit are padded to this width, so that
173 * messages printed after the graph output are aligned.
174 */
175 int width;
176 /*
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700177 * The next expansion row to print
178 * when state is GRAPH_PRE_COMMIT
179 */
180 int expansion_row;
181 /*
182 * The current output state.
183 * This tells us what kind of line graph_next_line() should output.
184 */
185 enum graph_state state;
186 /*
Adam Simpkins33959082008-06-01 13:56:57 -0700187 * The output state for the previous line of output.
188 * This is primarily used to determine how the first merge line
189 * should appear, based on the last line of the previous commit.
190 */
191 enum graph_state prev_state;
192 /*
193 * The index of the column that refers to this commit.
194 *
195 * If none of the incoming columns refer to this commit,
196 * this will be equal to num_columns.
197 */
198 int commit_index;
199 /*
200 * The commit_index for the previously displayed commit.
201 *
202 * This is used to determine how the first line of a merge
203 * graph output should appear, based on the last line of the
204 * previous commit.
205 */
206 int prev_commit_index;
207 /*
James Coglan0f0f3892019-10-15 23:47:54 +0000208 * Which layout variant to use to display merge commits. If the
209 * commit's first parent is known to be in a column to the left of the
210 * merge, then this value is 0 and we use the layout on the left.
211 * Otherwise, the value is 1 and the layout on the right is used. This
212 * field tells us how many columns the first parent occupies.
213 *
214 * 0) 1)
215 *
216 * | | | *-. | | *---.
217 * | |_|/|\ \ | | |\ \ \
218 * |/| | | | | | | | | | *
219 */
220 int merge_layout;
221 /*
James Cogland62893e2019-10-15 23:47:55 +0000222 * The number of columns added to the graph by the current commit. For
ryenus571fb962019-12-15 15:12:24 +0000223 * 2-way and octopus merges, this is usually one less than the
James Cogland62893e2019-10-15 23:47:55 +0000224 * number of parents:
225 *
226 * | | | | | \
227 * | * | | *---. \
228 * | |\ \ | |\ \ \ \
229 * | | | | | | | | | |
230 *
231 * num_parents: 2 num_parents: 4
232 * edges_added: 1 edges_added: 3
233 *
234 * For left-skewed merges, the first parent fuses with its neighbor and
235 * so one less column is added:
236 *
237 * | | | | | \
238 * | * | | *-. \
239 * |/| | |/|\ \ \
240 * | | | | | | | |
241 *
242 * num_parents: 2 num_parents: 4
243 * edges_added: 0 edges_added: 2
244 *
245 * This number determines how edges to the right of the merge are
246 * displayed in commit and post-merge lines; if no columns have been
247 * added then a vertical line should be used where a right-tracking
248 * line would otherwise be used.
249 *
250 * | * \ | * |
251 * | |\ \ |/| |
252 * | | * \ | * |
253 */
254 int edges_added;
255 /*
256 * The number of columns added by the previous commit, which is used to
257 * smooth edges appearing to the right of a commit in a commit line
258 * following a post-merge line.
259 */
260 int prev_edges_added;
261 /*
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700262 * The maximum number of columns that can be stored in the columns
263 * and new_columns arrays. This is also half the number of entries
James Coglan01952852019-10-15 23:47:56 +0000264 * that can be stored in the mapping and old_mapping arrays.
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700265 */
266 int column_capacity;
267 /*
268 * The number of columns (also called "branch lines" in some places)
269 */
270 int num_columns;
271 /*
272 * The number of columns in the new_columns array
273 */
274 int num_new_columns;
275 /*
276 * The number of entries in the mapping array
277 */
278 int mapping_size;
279 /*
280 * The column state before we output the current commit.
281 */
282 struct column *columns;
283 /*
284 * The new column state after we output the current commit.
285 * Only valid when state is GRAPH_COLLAPSING.
286 */
287 struct column *new_columns;
288 /*
289 * An array that tracks the current state of each
290 * character in the output line during state GRAPH_COLLAPSING.
291 * Each entry is -1 if this character is empty, or a non-negative
292 * integer if the character contains a branch line. The value of
293 * the integer indicates the target position for this branch line.
294 * (I.e., this array maps the current column positions to their
295 * desired positions.)
296 *
297 * The maximum capacity of this array is always
298 * sizeof(int) * 2 * column_capacity.
299 */
300 int *mapping;
301 /*
James Coglan479db182019-10-15 23:47:57 +0000302 * A copy of the contents of the mapping array from the last commit,
303 * which we use to improve the display of columns that are tracking
304 * from right to left through a commit line. We also use this to
305 * avoid allocating a fresh array when we compute the next mapping.
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700306 */
James Coglan01952852019-10-15 23:47:56 +0000307 int *old_mapping;
Allan Caffee427fc5b2009-04-13 15:53:41 -0400308 /*
309 * The current default column color being used. This is
310 * stored as an index into the array column_colors.
311 */
312 unsigned short default_column_color;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700313};
314
Bo Yangb5a4de92010-05-26 15:23:56 +0800315static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void *data)
316{
317 struct git_graph *graph = data;
318 static struct strbuf msgbuf = STRBUF_INIT;
319
Lucian Poston5e71a842012-04-16 03:44:50 -0700320 assert(opt);
Bo Yangb5a4de92010-05-26 15:23:56 +0800321
322 strbuf_reset(&msgbuf);
Jacob Keller660e1132016-08-31 16:27:20 -0700323 if (opt->line_prefix)
324 strbuf_add(&msgbuf, opt->line_prefix,
325 opt->line_prefix_length);
326 if (graph)
327 graph_padding_line(graph, &msgbuf);
Bo Yangb5a4de92010-05-26 15:23:56 +0800328 return &msgbuf;
329}
330
Jacob Keller660e1132016-08-31 16:27:20 -0700331static const struct diff_options *default_diffopt;
332
333void graph_setup_line_prefix(struct diff_options *diffopt)
334{
335 default_diffopt = diffopt;
336
337 /* setup an output prefix callback if necessary */
338 if (diffopt && !diffopt->output_prefix)
339 diffopt->output_prefix = diff_output_prefix_callback;
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
Alex Henriedccf6c12022-02-11 09:36:24 -0700404void graph_clear(struct git_graph *graph)
405{
406 if (!graph)
407 return;
408
409 free(graph->columns);
410 free(graph->new_columns);
411 free(graph->mapping);
412 free(graph->old_mapping);
413 free(graph);
414}
415
Adam Simpkins33959082008-06-01 13:56:57 -0700416static void graph_update_state(struct git_graph *graph, enum graph_state s)
417{
418 graph->prev_state = graph->state;
419 graph->state = s;
420}
421
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700422static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
423{
424 if (graph->column_capacity >= num_columns)
425 return;
426
427 do {
428 graph->column_capacity *= 2;
429 } while (graph->column_capacity < num_columns);
430
René Scharfe2756ca42014-09-16 20:56:57 +0200431 REALLOC_ARRAY(graph->columns, graph->column_capacity);
432 REALLOC_ARRAY(graph->new_columns, graph->column_capacity);
433 REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2);
James Coglan01952852019-10-15 23:47:56 +0000434 REALLOC_ARRAY(graph->old_mapping, graph->column_capacity * 2);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700435}
436
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700437/*
438 * Returns 1 if the commit will be printed in the graph output,
439 * and 0 otherwise.
440 */
Adam Simpkins3c68d672008-05-24 16:02:04 -0700441static int graph_is_interesting(struct git_graph *graph, struct commit *commit)
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700442{
443 /*
Adam Simpkins3c68d672008-05-24 16:02:04 -0700444 * If revs->boundary is set, commits whose children have
445 * been shown are always interesting, even if they have the
446 * UNINTERESTING or TREESAME flags set.
Adam Simpkins3c68d672008-05-24 16:02:04 -0700447 */
448 if (graph->revs && graph->revs->boundary) {
Adam Simpkins4603ec02008-05-24 16:02:05 -0700449 if (commit->object.flags & CHILD_SHOWN)
Adam Simpkins3c68d672008-05-24 16:02:04 -0700450 return 1;
451 }
452
453 /*
Adam Simpkinsbeb5af42009-08-18 19:34:33 -0700454 * Otherwise, use get_commit_action() to see if this commit is
455 * interesting
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700456 */
Adam Simpkinsbeb5af42009-08-18 19:34:33 -0700457 return get_commit_action(graph->revs, commit) == commit_show;
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700458}
459
Adam Simpkinsa0ebe572008-06-05 01:56:19 -0700460static struct commit_list *next_interesting_parent(struct git_graph *graph,
461 struct commit_list *orig)
462{
463 struct commit_list *list;
464
465 /*
466 * If revs->first_parent_only is set, only the first
467 * parent is interesting. None of the others are.
468 */
469 if (graph->revs->first_parent_only)
470 return NULL;
471
472 /*
473 * Return the next interesting commit after orig
474 */
475 for (list = orig->next; list; list = list->next) {
476 if (graph_is_interesting(graph, list->item))
477 return list;
478 }
479
480 return NULL;
481}
482
483static struct commit_list *first_interesting_parent(struct git_graph *graph)
484{
485 struct commit_list *parents = graph->commit->parents;
486
487 /*
488 * If this commit has no parents, ignore it
489 */
490 if (!parents)
491 return NULL;
492
493 /*
494 * If the first parent is interesting, return it
495 */
496 if (graph_is_interesting(graph, parents->item))
497 return parents;
498
499 /*
500 * Otherwise, call next_interesting_parent() to get
501 * the next interesting parent
502 */
503 return next_interesting_parent(graph, parents);
504}
505
Allan Caffee427fc5b2009-04-13 15:53:41 -0400506static unsigned short graph_get_current_column_color(const struct git_graph *graph)
507{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700508 if (!want_color(graph->revs->diffopt.use_color))
Johan Herland1e3d4112010-07-13 23:23:39 +0200509 return column_colors_max;
Allan Caffee427fc5b2009-04-13 15:53:41 -0400510 return graph->default_column_color;
511}
512
513/*
514 * Update the graph's default column color.
515 */
516static void graph_increment_column_color(struct git_graph *graph)
517{
518 graph->default_column_color = (graph->default_column_color + 1) %
Johan Herland1e3d4112010-07-13 23:23:39 +0200519 column_colors_max;
Allan Caffee427fc5b2009-04-13 15:53:41 -0400520}
521
522static unsigned short graph_find_commit_color(const struct git_graph *graph,
523 const struct commit *commit)
524{
525 int i;
526 for (i = 0; i < graph->num_columns; i++) {
527 if (graph->columns[i].commit == commit)
528 return graph->columns[i].color;
529 }
530 return graph_get_current_column_color(graph);
531}
532
James Coglan9157a2a2019-10-15 23:47:49 +0000533static int graph_find_new_column_by_commit(struct git_graph *graph,
534 struct commit *commit)
535{
536 int i;
537 for (i = 0; i < graph->num_new_columns; i++) {
538 if (graph->new_columns[i].commit == commit)
539 return i;
540 }
541 return -1;
542}
543
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700544static void graph_insert_into_new_columns(struct git_graph *graph,
James Coglan0f0f3892019-10-15 23:47:54 +0000545 struct commit *commit,
546 int idx)
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700547{
James Coglan9157a2a2019-10-15 23:47:49 +0000548 int i = graph_find_new_column_by_commit(graph, commit);
James Coglan0f0f3892019-10-15 23:47:54 +0000549 int mapping_idx;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700550
551 /*
James Coglana551fd52019-10-15 23:47:50 +0000552 * If the commit is not already in the new_columns array, then add it
553 * and record it as being in the final column.
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700554 */
James Coglana551fd52019-10-15 23:47:50 +0000555 if (i < 0) {
556 i = graph->num_new_columns++;
557 graph->new_columns[i].commit = commit;
558 graph->new_columns[i].color = graph_find_commit_color(graph, commit);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700559 }
560
James Coglan0f0f3892019-10-15 23:47:54 +0000561 if (graph->num_parents > 1 && idx > -1 && graph->merge_layout == -1) {
562 /*
563 * If this is the first parent of a merge, choose a layout for
564 * the merge line based on whether the parent appears in a
565 * column to the left of the merge
566 */
567 int dist, shift;
568
569 dist = idx - i;
570 shift = (dist > 1) ? 2 * dist - 3 : 1;
571
572 graph->merge_layout = (dist > 0) ? 0 : 1;
James Coglan92beecc2019-10-15 23:47:58 +0000573 graph->edges_added = graph->num_parents + graph->merge_layout - 2;
574
James Coglan0f0f3892019-10-15 23:47:54 +0000575 mapping_idx = graph->width + (graph->merge_layout - 1) * shift;
576 graph->width += 2 * graph->merge_layout;
James Coglan92beecc2019-10-15 23:47:58 +0000577
578 } else if (graph->edges_added > 0 && i == graph->mapping[graph->width - 2]) {
579 /*
580 * If some columns have been added by a merge, but this commit
581 * was found in the last existing column, then adjust the
582 * numbers so that the two edges immediately join, i.e.:
583 *
584 * * | * |
585 * |\ \ => |\|
586 * | |/ | *
587 * | *
588 */
589 mapping_idx = graph->width - 2;
590 graph->edges_added = -1;
James Coglan0f0f3892019-10-15 23:47:54 +0000591 } else {
592 mapping_idx = graph->width;
593 graph->width += 2;
594 }
595
596 graph->mapping[mapping_idx] = i;
Adam Simpkins0724cb82008-05-05 00:57:03 -0700597}
598
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700599static void graph_update_columns(struct git_graph *graph)
600{
601 struct commit_list *parent;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700602 int max_new_columns;
Adam Simpkins0724cb82008-05-05 00:57:03 -0700603 int i, seen_this, is_commit_in_columns;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700604
605 /*
606 * Swap graph->columns with graph->new_columns
607 * graph->columns contains the state for the previous commit,
608 * and new_columns now contains the state for our commit.
609 *
610 * We'll re-use the old columns array as storage to compute the new
611 * columns list for the commit after this one.
612 */
René Scharfe9e2edd62017-01-28 22:42:15 +0100613 SWAP(graph->columns, graph->new_columns);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700614 graph->num_columns = graph->num_new_columns;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700615 graph->num_new_columns = 0;
616
617 /*
618 * Now update new_columns and mapping with the information for the
619 * commit after this one.
620 *
621 * First, make sure we have enough room. At most, there will
622 * be graph->num_columns + graph->num_parents columns for the next
623 * commit.
624 */
625 max_new_columns = graph->num_columns + graph->num_parents;
626 graph_ensure_capacity(graph, max_new_columns);
627
628 /*
629 * Clear out graph->mapping
630 */
631 graph->mapping_size = 2 * max_new_columns;
632 for (i = 0; i < graph->mapping_size; i++)
633 graph->mapping[i] = -1;
634
James Coglan46ba2ab2019-10-15 23:47:51 +0000635 graph->width = 0;
James Coglan92beecc2019-10-15 23:47:58 +0000636 graph->prev_edges_added = graph->edges_added;
637 graph->edges_added = 0;
James Coglan46ba2ab2019-10-15 23:47:51 +0000638
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700639 /*
640 * Populate graph->new_columns and graph->mapping
641 *
642 * Some of the parents of this commit may already be in
643 * graph->columns. If so, graph->new_columns should only contain a
644 * single entry for each such commit. graph->mapping should
645 * contain information about where each current branch line is
646 * supposed to end up after the collapsing is performed.
647 */
648 seen_this = 0;
Adam Simpkins0724cb82008-05-05 00:57:03 -0700649 is_commit_in_columns = 1;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700650 for (i = 0; i <= graph->num_columns; i++) {
651 struct commit *col_commit;
652 if (i == graph->num_columns) {
653 if (seen_this)
654 break;
Adam Simpkins0724cb82008-05-05 00:57:03 -0700655 is_commit_in_columns = 0;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700656 col_commit = graph->commit;
657 } else {
658 col_commit = graph->columns[i].commit;
659 }
660
661 if (col_commit == graph->commit) {
662 seen_this = 1;
Adam Simpkins33959082008-06-01 13:56:57 -0700663 graph->commit_index = i;
James Coglan0f0f3892019-10-15 23:47:54 +0000664 graph->merge_layout = -1;
Adam Simpkinsa0ebe572008-06-05 01:56:19 -0700665 for (parent = first_interesting_parent(graph);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700666 parent;
Adam Simpkinsa0ebe572008-06-05 01:56:19 -0700667 parent = next_interesting_parent(graph, parent)) {
Allan Caffee427fc5b2009-04-13 15:53:41 -0400668 /*
Adam Simpkins91e50b22009-08-18 14:41:12 -0700669 * If this is a merge, or the start of a new
670 * childless column, increment the current
Allan Caffee427fc5b2009-04-13 15:53:41 -0400671 * color.
672 */
Adam Simpkins91e50b22009-08-18 14:41:12 -0700673 if (graph->num_parents > 1 ||
674 !is_commit_in_columns) {
Allan Caffee427fc5b2009-04-13 15:53:41 -0400675 graph_increment_column_color(graph);
Adam Simpkins91e50b22009-08-18 14:41:12 -0700676 }
James Coglan0f0f3892019-10-15 23:47:54 +0000677 graph_insert_into_new_columns(graph, parent->item, i);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700678 }
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700679 /*
James Coglan46ba2ab2019-10-15 23:47:51 +0000680 * We always need to increment graph->width by at
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700681 * least 2, even if it has no interesting parents.
682 * The current commit always takes up at least 2
683 * spaces.
684 */
James Coglan46ba2ab2019-10-15 23:47:51 +0000685 if (graph->num_parents == 0)
686 graph->width += 2;
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700687 } else {
James Coglan0f0f3892019-10-15 23:47:54 +0000688 graph_insert_into_new_columns(graph, col_commit, -1);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700689 }
690 }
691
692 /*
693 * Shrink mapping_size to be the minimum necessary
694 */
695 while (graph->mapping_size > 1 &&
696 graph->mapping[graph->mapping_size - 1] < 0)
697 graph->mapping_size--;
698}
699
James Coglanbbb13e82019-10-15 23:47:59 +0000700static int graph_num_dashed_parents(struct git_graph *graph)
701{
702 return graph->num_parents + graph->merge_layout - 3;
703}
704
James Coglan0f0f3892019-10-15 23:47:54 +0000705static int graph_num_expansion_rows(struct git_graph *graph)
706{
707 /*
708 * Normally, we need two expansion rows for each dashed parent line from
709 * an octopus merge:
710 *
711 * | *
712 * | |\
713 * | | \
714 * | | \
715 * | *-. \
716 * | |\ \ \
717 *
718 * If the merge is skewed to the left, then its parents occupy one less
719 * column, and we don't need as many expansion rows to route around it;
720 * in some cases that means we don't need any expansion rows at all:
721 *
722 * | *
723 * | |\
724 * | * \
725 * |/|\ \
726 */
James Coglanbbb13e82019-10-15 23:47:59 +0000727 return graph_num_dashed_parents(graph) * 2;
James Coglan0f0f3892019-10-15 23:47:54 +0000728}
729
James Coglanee7abb52019-10-15 23:47:52 +0000730static int graph_needs_pre_commit_line(struct git_graph *graph)
731{
732 return graph->num_parents >= 3 &&
James Coglan0f0f3892019-10-15 23:47:54 +0000733 graph->commit_index < (graph->num_columns - 1) &&
734 graph->expansion_row < graph_num_expansion_rows(graph);
James Coglanee7abb52019-10-15 23:47:52 +0000735}
736
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700737void graph_update(struct git_graph *graph, struct commit *commit)
738{
739 struct commit_list *parent;
740
741 /*
742 * Set the new commit
743 */
744 graph->commit = commit;
745
746 /*
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700747 * Count how many interesting parents this commit has
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700748 */
749 graph->num_parents = 0;
Adam Simpkinsa0ebe572008-06-05 01:56:19 -0700750 for (parent = first_interesting_parent(graph);
751 parent;
752 parent = next_interesting_parent(graph, parent))
753 {
754 graph->num_parents++;
Adam Simpkins37a75ab2008-05-23 19:24:11 -0700755 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700756
757 /*
Adam Simpkins33959082008-06-01 13:56:57 -0700758 * Store the old commit_index in prev_commit_index.
759 * graph_update_columns() will update graph->commit_index for this
760 * commit.
761 */
762 graph->prev_commit_index = graph->commit_index;
763
764 /*
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700765 * Call graph_update_columns() to update
766 * columns, new_columns, and mapping.
767 */
768 graph_update_columns(graph);
769
770 graph->expansion_row = 0;
771
772 /*
773 * Update graph->state.
Adam Simpkins33959082008-06-01 13:56:57 -0700774 * Note that we don't call graph_update_state() here, since
775 * we don't want to update graph->prev_state. No line for
776 * graph->state was ever printed.
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700777 *
778 * If the previous commit didn't get to the GRAPH_PADDING state,
779 * it never finished its output. Goto GRAPH_SKIP, to print out
780 * a line to indicate that portion of the graph is missing.
781 *
Adam Simpkinsf1979d62008-06-01 13:56:58 -0700782 * If there are 3 or more parents, we may need to print extra rows
783 * before the commit, to expand the branch lines around it and make
784 * room for it. We need to do this only if there is a branch row
785 * (or more) to the right of this commit.
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700786 *
787 * If there are less than 3 parents, we can immediately print the
788 * commit line.
789 */
790 if (graph->state != GRAPH_PADDING)
791 graph->state = GRAPH_SKIP;
James Coglanee7abb52019-10-15 23:47:52 +0000792 else if (graph_needs_pre_commit_line(graph))
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700793 graph->state = GRAPH_PRE_COMMIT;
794 else
795 graph->state = GRAPH_COMMIT;
796}
797
798static int graph_is_mapping_correct(struct git_graph *graph)
799{
800 int i;
801
802 /*
803 * The mapping is up to date if each entry is at its target,
804 * or is 1 greater than its target.
805 * (If it is 1 greater than the target, '/' will be printed, so it
806 * will look correct on the next row.)
807 */
808 for (i = 0; i < graph->mapping_size; i++) {
809 int target = graph->mapping[i];
810 if (target < 0)
811 continue;
812 if (target == (i / 2))
813 continue;
814 return 0;
815 }
816
817 return 1;
818}
819
James Coglanfbccf252019-10-15 23:47:47 +0000820static void graph_pad_horizontally(struct git_graph *graph, struct graph_line *line)
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700821{
822 /*
823 * Add additional spaces to the end of the strbuf, so that all
824 * lines for a particular commit have the same width.
825 *
826 * This way, fields printed to the right of the graph will remain
827 * aligned for the entire commit.
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700828 */
James Coglanfbccf252019-10-15 23:47:47 +0000829 if (line->width < graph->width)
830 graph_line_addchars(line, ' ', graph->width - line->width);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700831}
832
833static void graph_output_padding_line(struct git_graph *graph,
James Coglanfbccf252019-10-15 23:47:47 +0000834 struct graph_line *line)
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700835{
836 int i;
837
838 /*
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700839 * Output a padding row, that leaves all branch lines unchanged
840 */
841 for (i = 0; i < graph->num_new_columns; i++) {
James Coglanfbccf252019-10-15 23:47:47 +0000842 graph_line_write_column(line, &graph->new_columns[i], '|');
843 graph_line_addch(line, ' ');
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700844 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700845}
846
Josef Kufner3ad87c82016-06-16 20:18:37 +0700847
848int graph_width(struct git_graph *graph)
849{
850 return graph->width;
851}
852
853
James Coglanfbccf252019-10-15 23:47:47 +0000854static void graph_output_skip_line(struct git_graph *graph, struct graph_line *line)
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700855{
856 /*
857 * Output an ellipsis to indicate that a portion
858 * of the graph is missing.
859 */
James Coglanfbccf252019-10-15 23:47:47 +0000860 graph_line_addstr(line, "...");
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700861
James Coglanee7abb52019-10-15 23:47:52 +0000862 if (graph_needs_pre_commit_line(graph))
Adam Simpkins33959082008-06-01 13:56:57 -0700863 graph_update_state(graph, GRAPH_PRE_COMMIT);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700864 else
Adam Simpkins33959082008-06-01 13:56:57 -0700865 graph_update_state(graph, GRAPH_COMMIT);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700866}
867
868static void graph_output_pre_commit_line(struct git_graph *graph,
James Coglanfbccf252019-10-15 23:47:47 +0000869 struct graph_line *line)
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700870{
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700871 int i, seen_this;
872
873 /*
874 * This function formats a row that increases the space around a commit
875 * with multiple parents, to make room for it. It should only be
876 * called when there are 3 or more parents.
877 *
878 * We need 2 extra rows for every parent over 2.
879 */
880 assert(graph->num_parents >= 3);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700881
882 /*
883 * graph->expansion_row tracks the current expansion row we are on.
884 * It should be in the range [0, num_expansion_rows - 1]
885 */
886 assert(0 <= graph->expansion_row &&
James Coglan0f0f3892019-10-15 23:47:54 +0000887 graph->expansion_row < graph_num_expansion_rows(graph));
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700888
889 /*
890 * Output the row
891 */
892 seen_this = 0;
893 for (i = 0; i < graph->num_columns; i++) {
894 struct column *col = &graph->columns[i];
895 if (col->commit == graph->commit) {
896 seen_this = 1;
James Coglanfbccf252019-10-15 23:47:47 +0000897 graph_line_write_column(line, col, '|');
898 graph_line_addchars(line, ' ', graph->expansion_row);
Adam Simpkins33959082008-06-01 13:56:57 -0700899 } else if (seen_this && (graph->expansion_row == 0)) {
900 /*
901 * This is the first line of the pre-commit output.
902 * If the previous commit was a merge commit and
903 * ended in the GRAPH_POST_MERGE state, all branch
904 * lines after graph->prev_commit_index were
905 * printed as "\" on the previous line. Continue
906 * to print them as "\" on this line. Otherwise,
907 * print the branch lines as "|".
908 */
909 if (graph->prev_state == GRAPH_POST_MERGE &&
910 graph->prev_commit_index < i)
James Coglanfbccf252019-10-15 23:47:47 +0000911 graph_line_write_column(line, col, '\\');
Adam Simpkins33959082008-06-01 13:56:57 -0700912 else
James Coglanfbccf252019-10-15 23:47:47 +0000913 graph_line_write_column(line, col, '|');
Adam Simpkins33959082008-06-01 13:56:57 -0700914 } else if (seen_this && (graph->expansion_row > 0)) {
James Coglanfbccf252019-10-15 23:47:47 +0000915 graph_line_write_column(line, col, '\\');
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700916 } else {
James Coglanfbccf252019-10-15 23:47:47 +0000917 graph_line_write_column(line, col, '|');
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700918 }
James Coglanfbccf252019-10-15 23:47:47 +0000919 graph_line_addch(line, ' ');
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700920 }
921
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700922 /*
923 * Increment graph->expansion_row,
924 * and move to state GRAPH_COMMIT if necessary
925 */
926 graph->expansion_row++;
James Coglan0f0f3892019-10-15 23:47:54 +0000927 if (!graph_needs_pre_commit_line(graph))
Adam Simpkins33959082008-06-01 13:56:57 -0700928 graph_update_state(graph, GRAPH_COMMIT);
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700929}
930
James Coglanfbccf252019-10-15 23:47:47 +0000931static void graph_output_commit_char(struct git_graph *graph, struct graph_line *line)
Adam Simpkins3c68d672008-05-24 16:02:04 -0700932{
933 /*
934 * For boundary commits, print 'o'
935 * (We should only see boundary commits when revs->boundary is set.)
936 */
937 if (graph->commit->object.flags & BOUNDARY) {
938 assert(graph->revs->boundary);
James Coglanfbccf252019-10-15 23:47:47 +0000939 graph_line_addch(line, 'o');
Adam Simpkins3c68d672008-05-24 16:02:04 -0700940 return;
941 }
942
943 /*
Michael J Gruber1df2d652011-03-07 13:31:39 +0100944 * get_revision_mark() handles all other cases without assert()
Adam Simpkins3c68d672008-05-24 16:02:04 -0700945 */
James Coglanfbccf252019-10-15 23:47:47 +0000946 graph_line_addstr(line, get_revision_mark(graph->revs, graph->commit));
Adam Simpkins3c68d672008-05-24 16:02:04 -0700947}
948
Allan Caffee427fc5b2009-04-13 15:53:41 -0400949/*
James Coglanfbccf252019-10-15 23:47:47 +0000950 * Draw the horizontal dashes of an octopus merge.
Allan Caffee427fc5b2009-04-13 15:53:41 -0400951 */
James Coglanfbccf252019-10-15 23:47:47 +0000952static void graph_draw_octopus_merge(struct git_graph *graph, struct graph_line *line)
Allan Caffee427fc5b2009-04-13 15:53:41 -0400953{
954 /*
James Coglanbbb13e82019-10-15 23:47:59 +0000955 * The parents of a merge commit can be arbitrarily reordered as they
956 * are mapped onto display columns, for example this is a valid merge:
Noam Postavsky04005832018-09-01 20:07:16 -0400957 *
James Coglanbbb13e82019-10-15 23:47:59 +0000958 * | | *---.
959 * | | |\ \ \
960 * | | |/ / /
961 * | |/| | /
962 * | |_|_|/
963 * |/| | |
964 * 3 1 0 2
Noam Postavsky04005832018-09-01 20:07:16 -0400965 *
James Coglanbbb13e82019-10-15 23:47:59 +0000966 * The numbers denote which parent of the merge each visual column
967 * corresponds to; we can't assume that the parents will initially
968 * display in the order given by new_columns.
969 *
970 * To find the right color for each dash, we need to consult the
971 * mapping array, starting from the column 2 places to the right of the
972 * merge commit, and use that to find out which logical column each
973 * edge will collapse to.
974 *
975 * Commits are rendered once all edges have collapsed to their correct
976 * logcial column, so commit_index gives us the right visual offset for
977 * the merge commit.
Allan Caffee427fc5b2009-04-13 15:53:41 -0400978 */
Noam Postavsky04005832018-09-01 20:07:16 -0400979
James Coglanbbb13e82019-10-15 23:47:59 +0000980 int i, j;
981 struct column *col;
Noam Postavsky04005832018-09-01 20:07:16 -0400982
James Coglanbbb13e82019-10-15 23:47:59 +0000983 int dashed_parents = graph_num_dashed_parents(graph);
Noam Postavsky04005832018-09-01 20:07:16 -0400984
James Coglanbbb13e82019-10-15 23:47:59 +0000985 for (i = 0; i < dashed_parents; i++) {
986 j = graph->mapping[(graph->commit_index + i + 2) * 2];
987 col = &graph->new_columns[j];
988
989 graph_line_write_column(line, col, '-');
990 graph_line_write_column(line, col, (i == dashed_parents - 1) ? '.' : '-');
Allan Caffee427fc5b2009-04-13 15:53:41 -0400991 }
James Coglanbbb13e82019-10-15 23:47:59 +0000992
993 return;
Allan Caffee427fc5b2009-04-13 15:53:41 -0400994}
995
James Coglanfbccf252019-10-15 23:47:47 +0000996static void graph_output_commit_line(struct git_graph *graph, struct graph_line *line)
Adam Simpkinsc12172d2008-05-04 03:36:53 -0700997{
998 int seen_this = 0;
James Coglanfbccf252019-10-15 23:47:47 +0000999 int i;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001000
1001 /*
1002 * Output the row containing this commit
1003 * Iterate up to and including graph->num_columns,
1004 * since the current commit may not be in any of the existing
1005 * columns. (This happens when the current commit doesn't have any
1006 * children that we have already processed.)
1007 */
1008 seen_this = 0;
1009 for (i = 0; i <= graph->num_columns; i++) {
Allan Caffee427fc5b2009-04-13 15:53:41 -04001010 struct column *col = &graph->columns[i];
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001011 struct commit *col_commit;
1012 if (i == graph->num_columns) {
1013 if (seen_this)
1014 break;
1015 col_commit = graph->commit;
1016 } else {
1017 col_commit = graph->columns[i].commit;
1018 }
1019
1020 if (col_commit == graph->commit) {
1021 seen_this = 1;
James Coglanfbccf252019-10-15 23:47:47 +00001022 graph_output_commit_char(graph, line);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001023
Allan Caffeea6c1a382009-04-22 17:27:59 -04001024 if (graph->num_parents > 2)
James Coglanfbccf252019-10-15 23:47:47 +00001025 graph_draw_octopus_merge(graph, line);
James Cogland62893e2019-10-15 23:47:55 +00001026 } else if (seen_this && (graph->edges_added > 1)) {
James Coglanfbccf252019-10-15 23:47:47 +00001027 graph_line_write_column(line, col, '\\');
James Cogland62893e2019-10-15 23:47:55 +00001028 } else if (seen_this && (graph->edges_added == 1)) {
Adam Simpkins33959082008-06-01 13:56:57 -07001029 /*
James Cogland62893e2019-10-15 23:47:55 +00001030 * This is either a right-skewed 2-way merge
1031 * commit, or a left-skewed 3-way merge.
1032 * There is no GRAPH_PRE_COMMIT stage for such
Adam Simpkins33959082008-06-01 13:56:57 -07001033 * merges, so this is the first line of output
1034 * for this commit. Check to see what the previous
1035 * line of output was.
1036 *
1037 * If it was GRAPH_POST_MERGE, the branch line
1038 * coming into this commit may have been '\',
1039 * and not '|' or '/'. If so, output the branch
1040 * line as '\' on this line, instead of '|'. This
1041 * makes the output look nicer.
1042 */
1043 if (graph->prev_state == GRAPH_POST_MERGE &&
James Cogland62893e2019-10-15 23:47:55 +00001044 graph->prev_edges_added > 0 &&
Adam Simpkins33959082008-06-01 13:56:57 -07001045 graph->prev_commit_index < i)
James Coglanfbccf252019-10-15 23:47:47 +00001046 graph_line_write_column(line, col, '\\');
Adam Simpkins33959082008-06-01 13:56:57 -07001047 else
James Coglanfbccf252019-10-15 23:47:47 +00001048 graph_line_write_column(line, col, '|');
James Coglan479db182019-10-15 23:47:57 +00001049 } else if (graph->prev_state == GRAPH_COLLAPSING &&
1050 graph->old_mapping[2 * i + 1] == i &&
1051 graph->mapping[2 * i] < i) {
1052 graph_line_write_column(line, col, '/');
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001053 } else {
James Coglanfbccf252019-10-15 23:47:47 +00001054 graph_line_write_column(line, col, '|');
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001055 }
James Coglanfbccf252019-10-15 23:47:47 +00001056 graph_line_addch(line, ' ');
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001057 }
1058
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001059 /*
1060 * Update graph->state
1061 */
1062 if (graph->num_parents > 1)
Adam Simpkins33959082008-06-01 13:56:57 -07001063 graph_update_state(graph, GRAPH_POST_MERGE);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001064 else if (graph_is_mapping_correct(graph))
Adam Simpkins33959082008-06-01 13:56:57 -07001065 graph_update_state(graph, GRAPH_PADDING);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001066 else
Adam Simpkins33959082008-06-01 13:56:57 -07001067 graph_update_state(graph, GRAPH_COLLAPSING);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001068}
1069
Đoàn Trần Công Danh9d2152d2020-04-27 21:22:36 +07001070static const char merge_chars[] = {'/', '|', '\\'};
James Coglan0f0f3892019-10-15 23:47:54 +00001071
James Coglanfbccf252019-10-15 23:47:47 +00001072static void graph_output_post_merge_line(struct git_graph *graph, struct graph_line *line)
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001073{
1074 int seen_this = 0;
James Coglan92beecc2019-10-15 23:47:58 +00001075 int i, j;
James Coglan0f0f3892019-10-15 23:47:54 +00001076
1077 struct commit_list *first_parent = first_interesting_parent(graph);
Derrick Stoleea1087c92020-01-07 21:27:02 +00001078 struct column *parent_col = NULL;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001079
1080 /*
1081 * Output the post-merge row
1082 */
1083 for (i = 0; i <= graph->num_columns; i++) {
Allan Caffee427fc5b2009-04-13 15:53:41 -04001084 struct column *col = &graph->columns[i];
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001085 struct commit *col_commit;
1086 if (i == graph->num_columns) {
1087 if (seen_this)
1088 break;
1089 col_commit = graph->commit;
1090 } else {
Allan Caffee427fc5b2009-04-13 15:53:41 -04001091 col_commit = col->commit;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001092 }
1093
1094 if (col_commit == graph->commit) {
Allan Caffee427fc5b2009-04-13 15:53:41 -04001095 /*
1096 * Since the current commit is a merge find
1097 * the columns for the parent commits in
1098 * new_columns and use those to format the
1099 * edges.
1100 */
James Coglan0f0f3892019-10-15 23:47:54 +00001101 struct commit_list *parents = first_parent;
James Coglan9157a2a2019-10-15 23:47:49 +00001102 int par_column;
James Coglan0f0f3892019-10-15 23:47:54 +00001103 int idx = graph->merge_layout;
1104 char c;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001105 seen_this = 1;
Allan Caffee427fc5b2009-04-13 15:53:41 -04001106
James Coglan92beecc2019-10-15 23:47:58 +00001107 for (j = 0; j < graph->num_parents; j++) {
James Coglan9157a2a2019-10-15 23:47:49 +00001108 par_column = graph_find_new_column_by_commit(graph, parents->item);
1109 assert(par_column >= 0);
James Coglan0f0f3892019-10-15 23:47:54 +00001110
1111 c = merge_chars[idx];
1112 graph_line_write_column(line, &graph->new_columns[par_column], c);
James Coglan92beecc2019-10-15 23:47:58 +00001113 if (idx == 2) {
1114 if (graph->edges_added > 0 || j < graph->num_parents - 1)
1115 graph_line_addch(line, ' ');
1116 } else {
James Coglan0f0f3892019-10-15 23:47:54 +00001117 idx++;
James Coglan92beecc2019-10-15 23:47:58 +00001118 }
1119 parents = next_interesting_parent(graph, parents);
Allan Caffee427fc5b2009-04-13 15:53:41 -04001120 }
James Cogland62893e2019-10-15 23:47:55 +00001121 if (graph->edges_added == 0)
1122 graph_line_addch(line, ' ');
1123
Adam Simpkins33959082008-06-01 13:56:57 -07001124 } else if (seen_this) {
James Cogland62893e2019-10-15 23:47:55 +00001125 if (graph->edges_added > 0)
1126 graph_line_write_column(line, col, '\\');
1127 else
1128 graph_line_write_column(line, col, '|');
James Coglanfbccf252019-10-15 23:47:47 +00001129 graph_line_addch(line, ' ');
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001130 } else {
James Coglanfbccf252019-10-15 23:47:47 +00001131 graph_line_write_column(line, col, '|');
Derrick Stoleea1087c92020-01-07 21:27:02 +00001132 if (graph->merge_layout != 0 || i != graph->commit_index - 1) {
1133 if (parent_col)
1134 graph_line_write_column(
1135 line, parent_col, '_');
1136 else
1137 graph_line_addch(line, ' ');
1138 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001139 }
James Coglan0f0f3892019-10-15 23:47:54 +00001140
1141 if (col_commit == first_parent->item)
Derrick Stoleea1087c92020-01-07 21:27:02 +00001142 parent_col = col;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001143 }
1144
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001145 /*
1146 * Update graph->state
1147 */
1148 if (graph_is_mapping_correct(graph))
Adam Simpkins33959082008-06-01 13:56:57 -07001149 graph_update_state(graph, GRAPH_PADDING);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001150 else
Adam Simpkins33959082008-06-01 13:56:57 -07001151 graph_update_state(graph, GRAPH_COLLAPSING);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001152}
1153
James Coglanfbccf252019-10-15 23:47:47 +00001154static void graph_output_collapsing_line(struct git_graph *graph, struct graph_line *line)
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001155{
1156 int i;
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001157 short used_horizontal = 0;
1158 int horizontal_edge = -1;
1159 int horizontal_edge_target = -1;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001160
1161 /*
James Coglan01952852019-10-15 23:47:56 +00001162 * Swap the mapping and old_mapping arrays
1163 */
1164 SWAP(graph->mapping, graph->old_mapping);
1165
1166 /*
1167 * Clear out the mapping array
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001168 */
1169 for (i = 0; i < graph->mapping_size; i++)
James Coglan01952852019-10-15 23:47:56 +00001170 graph->mapping[i] = -1;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001171
1172 for (i = 0; i < graph->mapping_size; i++) {
James Coglan01952852019-10-15 23:47:56 +00001173 int target = graph->old_mapping[i];
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001174 if (target < 0)
1175 continue;
1176
1177 /*
1178 * Since update_columns() always inserts the leftmost
1179 * column first, each branch's target location should
1180 * always be either its current location or to the left of
1181 * its current location.
1182 *
1183 * We never have to move branches to the right. This makes
1184 * the graph much more legible, since whenever branches
1185 * cross, only one is moving directions.
1186 */
1187 assert(target * 2 <= i);
1188
1189 if (target * 2 == i) {
1190 /*
1191 * This column is already in the
1192 * correct place
1193 */
James Coglan01952852019-10-15 23:47:56 +00001194 assert(graph->mapping[i] == -1);
1195 graph->mapping[i] = target;
1196 } else if (graph->mapping[i - 1] < 0) {
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001197 /*
1198 * Nothing is to the left.
1199 * Move to the left by one
1200 */
James Coglan01952852019-10-15 23:47:56 +00001201 graph->mapping[i - 1] = target;
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001202 /*
1203 * If there isn't already an edge moving horizontally
1204 * select this one.
1205 */
1206 if (horizontal_edge == -1) {
1207 int j;
1208 horizontal_edge = i;
1209 horizontal_edge_target = target;
1210 /*
1211 * The variable target is the index of the graph
1212 * column, and therefore target*2+3 is the
1213 * actual screen column of the first horizontal
1214 * line.
1215 */
1216 for (j = (target * 2)+3; j < (i - 2); j += 2)
James Coglan01952852019-10-15 23:47:56 +00001217 graph->mapping[j] = target;
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001218 }
James Coglan01952852019-10-15 23:47:56 +00001219 } else if (graph->mapping[i - 1] == target) {
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001220 /*
1221 * There is a branch line to our left
1222 * already, and it is our target. We
1223 * combine with this line, since we share
1224 * the same parent commit.
1225 *
1226 * We don't have to add anything to the
James Coglan01952852019-10-15 23:47:56 +00001227 * output or mapping, since the
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001228 * existing branch line has already taken
1229 * care of it.
1230 */
1231 } else {
1232 /*
1233 * There is a branch line to our left,
1234 * but it isn't our target. We need to
1235 * cross over it.
1236 *
1237 * The space just to the left of this
1238 * branch should always be empty.
1239 */
James Coglan01952852019-10-15 23:47:56 +00001240 assert(graph->mapping[i - 1] > target);
1241 assert(graph->mapping[i - 2] < 0);
James Coglan01952852019-10-15 23:47:56 +00001242 graph->mapping[i - 2] = target;
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001243 /*
1244 * Mark this branch as the horizontal edge to
1245 * prevent any other edges from moving
1246 * horizontally.
1247 */
Derrick Stoleec958d3b2020-01-08 04:27:55 +00001248 if (horizontal_edge == -1) {
1249 int j;
1250 horizontal_edge_target = target;
1251 horizontal_edge = i - 1;
1252
1253 for (j = (target * 2) + 3; j < (i - 2); j += 2)
1254 graph->mapping[j] = target;
1255 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001256 }
1257 }
1258
1259 /*
James Coglan479db182019-10-15 23:47:57 +00001260 * Copy the current mapping array into old_mapping
1261 */
1262 COPY_ARRAY(graph->old_mapping, graph->mapping, graph->mapping_size);
1263
1264 /*
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001265 * The new mapping may be 1 smaller than the old mapping
1266 */
James Coglan01952852019-10-15 23:47:56 +00001267 if (graph->mapping[graph->mapping_size - 1] < 0)
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001268 graph->mapping_size--;
1269
1270 /*
1271 * Output out a line based on the new mapping info
1272 */
1273 for (i = 0; i < graph->mapping_size; i++) {
James Coglan01952852019-10-15 23:47:56 +00001274 int target = graph->mapping[i];
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001275 if (target < 0)
James Coglanfbccf252019-10-15 23:47:47 +00001276 graph_line_addch(line, ' ');
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001277 else if (target * 2 == i)
James Coglanfbccf252019-10-15 23:47:47 +00001278 graph_line_write_column(line, &graph->new_columns[target], '|');
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001279 else if (target == horizontal_edge_target &&
1280 i != horizontal_edge - 1) {
1281 /*
1282 * Set the mappings for all but the
1283 * first segment to -1 so that they
1284 * won't continue into the next line.
1285 */
1286 if (i != (target * 2)+3)
James Coglan01952852019-10-15 23:47:56 +00001287 graph->mapping[i] = -1;
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001288 used_horizontal = 1;
James Coglanfbccf252019-10-15 23:47:47 +00001289 graph_line_write_column(line, &graph->new_columns[target], '_');
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001290 } else {
1291 if (used_horizontal && i < horizontal_edge)
James Coglan01952852019-10-15 23:47:56 +00001292 graph->mapping[i] = -1;
James Coglanfbccf252019-10-15 23:47:47 +00001293 graph_line_write_column(line, &graph->new_columns[target], '/');
Allan Caffeeeaf158f2009-04-21 08:47:01 -04001294
1295 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001296 }
1297
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001298 /*
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001299 * If graph->mapping indicates that all of the branch lines
1300 * are already in the correct positions, we are done.
1301 * Otherwise, we need to collapse some branch lines together.
1302 */
1303 if (graph_is_mapping_correct(graph))
Adam Simpkins33959082008-06-01 13:56:57 -07001304 graph_update_state(graph, GRAPH_PADDING);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001305}
1306
John Keepingac751a02013-03-04 00:03:37 +00001307int graph_next_line(struct git_graph *graph, struct strbuf *sb)
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001308{
James Coglan210179a2019-10-15 23:47:48 +00001309 int shown_commit_line = 0;
James Coglanfbccf252019-10-15 23:47:47 +00001310 struct graph_line line = { .buf = sb, .width = 0 };
1311
James Coglan210179a2019-10-15 23:47:48 +00001312 /*
1313 * We could conceivable be called with a NULL commit
1314 * if our caller has a bug, and invokes graph_next_line()
1315 * immediately after graph_init(), without first calling
1316 * graph_update(). Return without outputting anything in this
1317 * case.
1318 */
1319 if (!graph->commit)
1320 return -1;
1321
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001322 switch (graph->state) {
1323 case GRAPH_PADDING:
James Coglanfbccf252019-10-15 23:47:47 +00001324 graph_output_padding_line(graph, &line);
James Coglan210179a2019-10-15 23:47:48 +00001325 break;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001326 case GRAPH_SKIP:
James Coglanfbccf252019-10-15 23:47:47 +00001327 graph_output_skip_line(graph, &line);
James Coglan210179a2019-10-15 23:47:48 +00001328 break;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001329 case GRAPH_PRE_COMMIT:
James Coglanfbccf252019-10-15 23:47:47 +00001330 graph_output_pre_commit_line(graph, &line);
James Coglan210179a2019-10-15 23:47:48 +00001331 break;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001332 case GRAPH_COMMIT:
James Coglanfbccf252019-10-15 23:47:47 +00001333 graph_output_commit_line(graph, &line);
James Coglan210179a2019-10-15 23:47:48 +00001334 shown_commit_line = 1;
1335 break;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001336 case GRAPH_POST_MERGE:
James Coglanfbccf252019-10-15 23:47:47 +00001337 graph_output_post_merge_line(graph, &line);
James Coglan210179a2019-10-15 23:47:48 +00001338 break;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001339 case GRAPH_COLLAPSING:
James Coglanfbccf252019-10-15 23:47:47 +00001340 graph_output_collapsing_line(graph, &line);
James Coglan210179a2019-10-15 23:47:48 +00001341 break;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001342 }
1343
James Coglan210179a2019-10-15 23:47:48 +00001344 graph_pad_horizontally(graph, &line);
1345 return shown_commit_line;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001346}
1347
Nanako Shiraishi064bfbd2008-09-25 18:41:08 +09001348static void graph_padding_line(struct git_graph *graph, struct strbuf *sb)
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001349{
René Scharfe415792e2014-09-07 09:06:42 +02001350 int i;
James Coglanfbccf252019-10-15 23:47:47 +00001351 struct graph_line line = { .buf = sb, .width = 0 };
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001352
1353 if (graph->state != GRAPH_COMMIT) {
1354 graph_next_line(graph, sb);
1355 return;
1356 }
1357
1358 /*
1359 * Output the row containing this commit
1360 * Iterate up to and including graph->num_columns,
1361 * since the current commit may not be in any of the existing
1362 * columns. (This happens when the current commit doesn't have any
1363 * children that we have already processed.)
1364 */
1365 for (i = 0; i < graph->num_columns; i++) {
Allan Caffee427fc5b2009-04-13 15:53:41 -04001366 struct column *col = &graph->columns[i];
Jeff King16477932016-09-29 04:37:51 -04001367
James Coglanfbccf252019-10-15 23:47:47 +00001368 graph_line_write_column(&line, col, '|');
Jeff King16477932016-09-29 04:37:51 -04001369
1370 if (col->commit == graph->commit && graph->num_parents > 2) {
1371 int len = (graph->num_parents - 2) * 2;
James Coglanfbccf252019-10-15 23:47:47 +00001372 graph_line_addchars(&line, ' ', len);
Jeff King16477932016-09-29 04:37:51 -04001373 } else {
James Coglanfbccf252019-10-15 23:47:47 +00001374 graph_line_addch(&line, ' ');
Jeff King16477932016-09-29 04:37:51 -04001375 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001376 }
1377
James Coglanfbccf252019-10-15 23:47:47 +00001378 graph_pad_horizontally(graph, &line);
Adam Simpkins33959082008-06-01 13:56:57 -07001379
1380 /*
1381 * Update graph->prev_state since we have output a padding line
1382 */
1383 graph->prev_state = GRAPH_PADDING;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001384}
1385
1386int graph_is_commit_finished(struct git_graph const *graph)
1387{
1388 return (graph->state == GRAPH_PADDING);
1389}
1390
1391void graph_show_commit(struct git_graph *graph)
1392{
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001393 struct strbuf msgbuf = STRBUF_INIT;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001394 int shown_commit_line = 0;
1395
Jacob Keller660e1132016-08-31 16:27:20 -07001396 graph_show_line_prefix(default_diffopt);
1397
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001398 if (!graph)
1399 return;
1400
John Keepinga48ec242013-02-07 20:15:23 +00001401 /*
1402 * When showing a diff of a merge against each of its parents, we
1403 * are called once for each parent without graph_update having been
1404 * called. In this case, simply output a single padding line.
1405 */
1406 if (graph_is_commit_finished(graph)) {
1407 graph_show_padding(graph);
1408 shown_commit_line = 1;
1409 }
1410
Michał Kiedrowicz656197a2009-07-25 01:45:00 +02001411 while (!shown_commit_line && !graph_is_commit_finished(graph)) {
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001412 shown_commit_line = graph_next_line(graph, &msgbuf);
Johannes Schindelinc61008f2016-06-22 17:01:44 +02001413 fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
1414 graph->revs->diffopt.file);
Jacob Keller660e1132016-08-31 16:27:20 -07001415 if (!shown_commit_line) {
Johannes Schindelinc61008f2016-06-22 17:01:44 +02001416 putc('\n', graph->revs->diffopt.file);
Jacob Keller660e1132016-08-31 16:27:20 -07001417 graph_show_line_prefix(&graph->revs->diffopt);
1418 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001419 strbuf_setlen(&msgbuf, 0);
1420 }
1421
1422 strbuf_release(&msgbuf);
1423}
1424
1425void graph_show_oneline(struct git_graph *graph)
1426{
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001427 struct strbuf msgbuf = STRBUF_INIT;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001428
Jacob Keller660e1132016-08-31 16:27:20 -07001429 graph_show_line_prefix(default_diffopt);
1430
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001431 if (!graph)
1432 return;
1433
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001434 graph_next_line(graph, &msgbuf);
Johannes Schindelinc61008f2016-06-22 17:01:44 +02001435 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001436 strbuf_release(&msgbuf);
1437}
1438
1439void graph_show_padding(struct git_graph *graph)
1440{
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001441 struct strbuf msgbuf = STRBUF_INIT;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001442
Jacob Keller660e1132016-08-31 16:27:20 -07001443 graph_show_line_prefix(default_diffopt);
1444
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001445 if (!graph)
1446 return;
1447
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001448 graph_padding_line(graph, &msgbuf);
Johannes Schindelinc61008f2016-06-22 17:01:44 +02001449 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001450 strbuf_release(&msgbuf);
1451}
1452
1453int graph_show_remainder(struct git_graph *graph)
1454{
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001455 struct strbuf msgbuf = STRBUF_INIT;
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001456 int shown = 0;
1457
Jacob Keller660e1132016-08-31 16:27:20 -07001458 graph_show_line_prefix(default_diffopt);
1459
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001460 if (!graph)
1461 return 0;
1462
1463 if (graph_is_commit_finished(graph))
1464 return 0;
1465
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001466 for (;;) {
1467 graph_next_line(graph, &msgbuf);
Johannes Schindelinc61008f2016-06-22 17:01:44 +02001468 fwrite(msgbuf.buf, sizeof(char), msgbuf.len,
1469 graph->revs->diffopt.file);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001470 strbuf_setlen(&msgbuf, 0);
1471 shown = 1;
1472
Jacob Keller660e1132016-08-31 16:27:20 -07001473 if (!graph_is_commit_finished(graph)) {
Johannes Schindelinc61008f2016-06-22 17:01:44 +02001474 putc('\n', graph->revs->diffopt.file);
Jacob Keller660e1132016-08-31 16:27:20 -07001475 graph_show_line_prefix(&graph->revs->diffopt);
1476 } else {
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001477 break;
Jacob Keller660e1132016-08-31 16:27:20 -07001478 }
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001479 }
1480 strbuf_release(&msgbuf);
1481
1482 return shown;
1483}
1484
Jacob Keller660e1132016-08-31 16:27:20 -07001485static void graph_show_strbuf(struct git_graph *graph,
1486 FILE *file,
1487 struct strbuf const *sb)
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001488{
1489 char *p;
1490
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001491 /*
1492 * Print the strbuf line by line,
1493 * and display the graph info before each line but the first.
1494 */
1495 p = sb->buf;
1496 while (p) {
1497 size_t len;
1498 char *next_p = strchr(p, '\n');
1499 if (next_p) {
1500 next_p++;
1501 len = next_p - p;
1502 } else {
1503 len = (sb->buf + sb->len) - p;
1504 }
Jacob Keller660e1132016-08-31 16:27:20 -07001505 fwrite(p, sizeof(char), len, file);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001506 if (next_p && *next_p != '\0')
1507 graph_show_oneline(graph);
1508 p = next_p;
1509 }
1510}
1511
1512void graph_show_commit_msg(struct git_graph *graph,
Jacob Keller660e1132016-08-31 16:27:20 -07001513 FILE *file,
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001514 struct strbuf const *sb)
1515{
1516 int newline_terminated;
1517
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001518 /*
1519 * Show the commit message
1520 */
Jacob Keller660e1132016-08-31 16:27:20 -07001521 graph_show_strbuf(graph, file, sb);
1522
1523 if (!graph)
1524 return;
1525
1526 newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n');
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001527
1528 /*
1529 * If there is more output needed for this commit, show it now
1530 */
1531 if (!graph_is_commit_finished(graph)) {
1532 /*
1533 * If sb doesn't have a terminating newline, print one now,
1534 * so we can start the remainder of the graph output on a
1535 * new line.
1536 */
1537 if (!newline_terminated)
Jacob Keller660e1132016-08-31 16:27:20 -07001538 putc('\n', file);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001539
1540 graph_show_remainder(graph);
1541
1542 /*
1543 * If sb ends with a newline, our output should too.
1544 */
1545 if (newline_terminated)
Jacob Keller660e1132016-08-31 16:27:20 -07001546 putc('\n', file);
Adam Simpkinsc12172d2008-05-04 03:36:53 -07001547 }
1548}