Elijah Newren | fc7bd51 | 2023-02-24 00:09:34 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| 2 | #include "gettext.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 3 | #include "config.h" |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 4 | #include "commit.h" |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 5 | #include "color.h" |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 6 | #include "graph.h" |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 7 | #include "revision.h" |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 8 | #include "strvec.h" |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 9 | |
Nanako Shiraishi | 064bfbd | 2008-09-25 18:41:08 +0900 | [diff] [blame] | 10 | /* Internal API */ |
| 11 | |
| 12 | /* |
Nanako Shiraishi | 064bfbd | 2008-09-25 18:41:08 +0900 | [diff] [blame] | 13 | * 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 | */ |
| 19 | static void graph_padding_line(struct git_graph *graph, struct strbuf *sb); |
| 20 | |
| 21 | /* |
Johannes Schindelin | c61008f | 2016-06-22 17:01:44 +0200 | [diff] [blame] | 22 | * Print a strbuf. If the graph is non-NULL, all lines but the first will be |
| 23 | * prefixed with the graph output. |
Nanako Shiraishi | 064bfbd | 2008-09-25 18:41:08 +0900 | [diff] [blame] | 24 | * |
| 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 Ralphson | 3ea3c21 | 2009-04-17 19:13:30 +0100 | [diff] [blame] | 29 | * Since the first line will not include the graph output, the caller is |
Nanako Shiraishi | 064bfbd | 2008-09-25 18:41:08 +0900 | [diff] [blame] | 30 | * responsible for printing this line's graph (perhaps via |
| 31 | * graph_show_commit() or graph_show_oneline()) before calling |
| 32 | * graph_show_strbuf(). |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 33 | * |
| 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 Waly | 3f1480b | 2019-11-17 21:04:42 +0000 | [diff] [blame] | 38 | * If a NULL graph is supplied, the strbuf is printed as-is. |
Nanako Shiraishi | 064bfbd | 2008-09-25 18:41:08 +0900 | [diff] [blame] | 39 | */ |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 40 | static void graph_show_strbuf(struct git_graph *graph, |
| 41 | FILE *file, |
| 42 | struct strbuf const *sb); |
Nanako Shiraishi | 064bfbd | 2008-09-25 18:41:08 +0900 | [diff] [blame] | 43 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 44 | /* |
| 45 | * TODO: |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 46 | * - 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 Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 49 | */ |
| 50 | |
| 51 | struct column { |
| 52 | /* |
| 53 | * The parent commit of this column. |
| 54 | */ |
| 55 | struct commit *commit; |
| 56 | /* |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 57 | * The color to (optionally) print this column in. This is an |
| 58 | * index into column_colors. |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 59 | */ |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 60 | unsigned short color; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | enum 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 Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 72 | static 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 Herland | 1e3d411 | 2010-07-13 23:23:39 +0200 | [diff] [blame] | 83 | static const char **column_colors; |
| 84 | static unsigned short column_colors_max; |
| 85 | |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 86 | static void parse_graph_colors_config(struct strvec *colors, const char *string) |
Nguyễn Thái Ngọc Duy | 73c727d | 2017-01-19 18:41:23 +0700 | [diff] [blame] | 87 | { |
| 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 King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 97 | strvec_push(colors, color); |
Nguyễn Thái Ngọc Duy | 73c727d | 2017-01-19 18:41:23 +0700 | [diff] [blame] | 98 | else |
Alex Henrie | 9853830 | 2021-06-12 12:41:44 -0600 | [diff] [blame] | 99 | warning(_("ignored invalid color '%.*s' in log.graphColors"), |
Nguyễn Thái Ngọc Duy | 73c727d | 2017-01-19 18:41:23 +0700 | [diff] [blame] | 100 | (int)(comma - start), start); |
| 101 | start = comma + 1; |
| 102 | } |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 103 | strvec_push(colors, GIT_COLOR_RESET); |
Nguyễn Thái Ngọc Duy | 73c727d | 2017-01-19 18:41:23 +0700 | [diff] [blame] | 104 | } |
| 105 | |
John Keeping | ac751a0 | 2013-03-04 00:03:37 +0000 | [diff] [blame] | 106 | void graph_set_column_colors(const char **colors, unsigned short colors_max) |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 107 | { |
Johan Herland | 1e3d411 | 2010-07-13 23:23:39 +0200 | [diff] [blame] | 108 | column_colors = colors; |
| 109 | column_colors_max = colors_max; |
| 110 | } |
| 111 | |
| 112 | static const char *column_get_color_code(unsigned short color) |
| 113 | { |
| 114 | return column_colors[color]; |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 115 | } |
| 116 | |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 117 | struct graph_line { |
| 118 | struct strbuf *buf; |
| 119 | size_t width; |
| 120 | }; |
| 121 | |
| 122 | static inline void graph_line_addch(struct graph_line *line, int c) |
| 123 | { |
| 124 | strbuf_addch(line->buf, c); |
| 125 | line->width++; |
| 126 | } |
| 127 | |
| 128 | static 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 | |
| 134 | static 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 | |
| 140 | static 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 | |
| 145 | static void graph_line_write_column(struct graph_line *line, const struct column *c, |
| 146 | char col_char) |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 147 | { |
Johan Herland | 1e3d411 | 2010-07-13 23:23:39 +0200 | [diff] [blame] | 148 | if (c->color < column_colors_max) |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 149 | graph_line_addcolor(line, c->color); |
| 150 | graph_line_addch(line, col_char); |
Johan Herland | 1e3d411 | 2010-07-13 23:23:39 +0200 | [diff] [blame] | 151 | if (c->color < column_colors_max) |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 152 | graph_line_addcolor(line, column_colors_max); |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 155 | struct git_graph { |
| 156 | /* |
| 157 | * The commit currently being processed |
| 158 | */ |
| 159 | struct commit *commit; |
Adam Simpkins | 7528f27 | 2008-05-25 00:07:21 -0700 | [diff] [blame] | 160 | /* The rev-info used for the current traversal */ |
| 161 | struct rev_info *revs; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 162 | /* |
Adam Simpkins | 37a75ab | 2008-05-23 19:24:11 -0700 | [diff] [blame] | 163 | * 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 Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 168 | */ |
| 169 | int num_parents; |
| 170 | /* |
Adam Simpkins | 0724cb8 | 2008-05-05 00:57:03 -0700 | [diff] [blame] | 171 | * 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 Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 177 | * 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 Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 187 | * 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 Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 208 | * 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 Coglan | d62893e | 2019-10-15 23:47:55 +0000 | [diff] [blame] | 222 | * The number of columns added to the graph by the current commit. For |
ryenus | 571fb96 | 2019-12-15 15:12:24 +0000 | [diff] [blame] | 223 | * 2-way and octopus merges, this is usually one less than the |
James Coglan | d62893e | 2019-10-15 23:47:55 +0000 | [diff] [blame] | 224 | * 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 Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 262 | * 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 Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 264 | * that can be stored in the mapping and old_mapping arrays. |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 265 | */ |
| 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 Coglan | 479db18 | 2019-10-15 23:47:57 +0000 | [diff] [blame] | 302 | * 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 Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 306 | */ |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 307 | int *old_mapping; |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 308 | /* |
| 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 Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 313 | }; |
| 314 | |
Bo Yang | b5a4de9 | 2010-05-26 15:23:56 +0800 | [diff] [blame] | 315 | static 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 Poston | 5e71a84 | 2012-04-16 03:44:50 -0700 | [diff] [blame] | 320 | assert(opt); |
Bo Yang | b5a4de9 | 2010-05-26 15:23:56 +0800 | [diff] [blame] | 321 | |
| 322 | strbuf_reset(&msgbuf); |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 323 | 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 Yang | b5a4de9 | 2010-05-26 15:23:56 +0800 | [diff] [blame] | 328 | return &msgbuf; |
| 329 | } |
| 330 | |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 331 | static const struct diff_options *default_diffopt; |
| 332 | |
| 333 | void 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 | |
| 342 | |
Adam Simpkins | 7528f27 | 2008-05-25 00:07:21 -0700 | [diff] [blame] | 343 | struct git_graph *graph_init(struct rev_info *opt) |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 344 | { |
| 345 | struct git_graph *graph = xmalloc(sizeof(struct git_graph)); |
Johan Herland | 1e3d411 | 2010-07-13 23:23:39 +0200 | [diff] [blame] | 346 | |
Nguyễn Thái Ngọc Duy | 73c727d | 2017-01-19 18:41:23 +0700 | [diff] [blame] | 347 | if (!column_colors) { |
| 348 | char *string; |
| 349 | if (git_config_get_string("log.graphcolors", &string)) { |
| 350 | /* not configured -- use default */ |
| 351 | graph_set_column_colors(column_colors_ansi, |
| 352 | column_colors_ansi_max); |
| 353 | } else { |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 354 | static struct strvec custom_colors = STRVEC_INIT; |
| 355 | strvec_clear(&custom_colors); |
Nguyễn Thái Ngọc Duy | 73c727d | 2017-01-19 18:41:23 +0700 | [diff] [blame] | 356 | parse_graph_colors_config(&custom_colors, string); |
| 357 | free(string); |
| 358 | /* graph_set_column_colors takes a max-index, not a count */ |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 359 | graph_set_column_colors(custom_colors.v, |
| 360 | custom_colors.nr - 1); |
Nguyễn Thái Ngọc Duy | 73c727d | 2017-01-19 18:41:23 +0700 | [diff] [blame] | 361 | } |
| 362 | } |
Johan Herland | 1e3d411 | 2010-07-13 23:23:39 +0200 | [diff] [blame] | 363 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 364 | graph->commit = NULL; |
Adam Simpkins | 7528f27 | 2008-05-25 00:07:21 -0700 | [diff] [blame] | 365 | graph->revs = opt; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 366 | graph->num_parents = 0; |
| 367 | graph->expansion_row = 0; |
| 368 | graph->state = GRAPH_PADDING; |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 369 | graph->prev_state = GRAPH_PADDING; |
| 370 | graph->commit_index = 0; |
| 371 | graph->prev_commit_index = 0; |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 372 | graph->merge_layout = 0; |
James Coglan | d62893e | 2019-10-15 23:47:55 +0000 | [diff] [blame] | 373 | graph->edges_added = 0; |
| 374 | graph->prev_edges_added = 0; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 375 | graph->num_columns = 0; |
| 376 | graph->num_new_columns = 0; |
| 377 | graph->mapping_size = 0; |
Adam Simpkins | 91e50b2 | 2009-08-18 14:41:12 -0700 | [diff] [blame] | 378 | /* |
| 379 | * Start the column color at the maximum value, since we'll |
| 380 | * always increment it for the first commit we output. |
| 381 | * This way we start at 0 for the first commit. |
| 382 | */ |
Johan Herland | 1e3d411 | 2010-07-13 23:23:39 +0200 | [diff] [blame] | 383 | graph->default_column_color = column_colors_max - 1; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 384 | |
| 385 | /* |
| 386 | * Allocate a reasonably large default number of columns |
| 387 | * We'll automatically grow columns later if we need more room. |
| 388 | */ |
| 389 | graph->column_capacity = 30; |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 390 | ALLOC_ARRAY(graph->columns, graph->column_capacity); |
| 391 | ALLOC_ARRAY(graph->new_columns, graph->column_capacity); |
| 392 | ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity); |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 393 | ALLOC_ARRAY(graph->old_mapping, 2 * graph->column_capacity); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 394 | |
Bo Yang | b5a4de9 | 2010-05-26 15:23:56 +0800 | [diff] [blame] | 395 | /* |
| 396 | * The diff output prefix callback, with this we can make |
| 397 | * all the diff output to align with the graph lines. |
| 398 | */ |
| 399 | opt->diffopt.output_prefix = diff_output_prefix_callback; |
| 400 | opt->diffopt.output_prefix_data = graph; |
| 401 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 402 | return graph; |
| 403 | } |
| 404 | |
Alex Henrie | dccf6c1 | 2022-02-11 09:36:24 -0700 | [diff] [blame] | 405 | void graph_clear(struct git_graph *graph) |
| 406 | { |
| 407 | if (!graph) |
| 408 | return; |
| 409 | |
| 410 | free(graph->columns); |
| 411 | free(graph->new_columns); |
| 412 | free(graph->mapping); |
| 413 | free(graph->old_mapping); |
| 414 | free(graph); |
| 415 | } |
| 416 | |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 417 | static void graph_update_state(struct git_graph *graph, enum graph_state s) |
| 418 | { |
| 419 | graph->prev_state = graph->state; |
| 420 | graph->state = s; |
| 421 | } |
| 422 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 423 | static void graph_ensure_capacity(struct git_graph *graph, int num_columns) |
| 424 | { |
| 425 | if (graph->column_capacity >= num_columns) |
| 426 | return; |
| 427 | |
| 428 | do { |
| 429 | graph->column_capacity *= 2; |
| 430 | } while (graph->column_capacity < num_columns); |
| 431 | |
René Scharfe | 2756ca4 | 2014-09-16 20:56:57 +0200 | [diff] [blame] | 432 | REALLOC_ARRAY(graph->columns, graph->column_capacity); |
| 433 | REALLOC_ARRAY(graph->new_columns, graph->column_capacity); |
| 434 | REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2); |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 435 | REALLOC_ARRAY(graph->old_mapping, graph->column_capacity * 2); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Adam Simpkins | 37a75ab | 2008-05-23 19:24:11 -0700 | [diff] [blame] | 438 | /* |
| 439 | * Returns 1 if the commit will be printed in the graph output, |
| 440 | * and 0 otherwise. |
| 441 | */ |
Adam Simpkins | 3c68d67 | 2008-05-24 16:02:04 -0700 | [diff] [blame] | 442 | static int graph_is_interesting(struct git_graph *graph, struct commit *commit) |
Adam Simpkins | 37a75ab | 2008-05-23 19:24:11 -0700 | [diff] [blame] | 443 | { |
| 444 | /* |
Adam Simpkins | 3c68d67 | 2008-05-24 16:02:04 -0700 | [diff] [blame] | 445 | * If revs->boundary is set, commits whose children have |
| 446 | * been shown are always interesting, even if they have the |
| 447 | * UNINTERESTING or TREESAME flags set. |
Adam Simpkins | 3c68d67 | 2008-05-24 16:02:04 -0700 | [diff] [blame] | 448 | */ |
| 449 | if (graph->revs && graph->revs->boundary) { |
Adam Simpkins | 4603ec0 | 2008-05-24 16:02:05 -0700 | [diff] [blame] | 450 | if (commit->object.flags & CHILD_SHOWN) |
Adam Simpkins | 3c68d67 | 2008-05-24 16:02:04 -0700 | [diff] [blame] | 451 | return 1; |
| 452 | } |
| 453 | |
| 454 | /* |
Adam Simpkins | beb5af4 | 2009-08-18 19:34:33 -0700 | [diff] [blame] | 455 | * Otherwise, use get_commit_action() to see if this commit is |
| 456 | * interesting |
Adam Simpkins | 37a75ab | 2008-05-23 19:24:11 -0700 | [diff] [blame] | 457 | */ |
Adam Simpkins | beb5af4 | 2009-08-18 19:34:33 -0700 | [diff] [blame] | 458 | return get_commit_action(graph->revs, commit) == commit_show; |
Adam Simpkins | 37a75ab | 2008-05-23 19:24:11 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Adam Simpkins | a0ebe57 | 2008-06-05 01:56:19 -0700 | [diff] [blame] | 461 | static struct commit_list *next_interesting_parent(struct git_graph *graph, |
| 462 | struct commit_list *orig) |
| 463 | { |
| 464 | struct commit_list *list; |
| 465 | |
| 466 | /* |
| 467 | * If revs->first_parent_only is set, only the first |
| 468 | * parent is interesting. None of the others are. |
| 469 | */ |
| 470 | if (graph->revs->first_parent_only) |
| 471 | return NULL; |
| 472 | |
| 473 | /* |
| 474 | * Return the next interesting commit after orig |
| 475 | */ |
| 476 | for (list = orig->next; list; list = list->next) { |
| 477 | if (graph_is_interesting(graph, list->item)) |
| 478 | return list; |
| 479 | } |
| 480 | |
| 481 | return NULL; |
| 482 | } |
| 483 | |
| 484 | static struct commit_list *first_interesting_parent(struct git_graph *graph) |
| 485 | { |
| 486 | struct commit_list *parents = graph->commit->parents; |
| 487 | |
| 488 | /* |
| 489 | * If this commit has no parents, ignore it |
| 490 | */ |
| 491 | if (!parents) |
| 492 | return NULL; |
| 493 | |
| 494 | /* |
| 495 | * If the first parent is interesting, return it |
| 496 | */ |
| 497 | if (graph_is_interesting(graph, parents->item)) |
| 498 | return parents; |
| 499 | |
| 500 | /* |
| 501 | * Otherwise, call next_interesting_parent() to get |
| 502 | * the next interesting parent |
| 503 | */ |
| 504 | return next_interesting_parent(graph, parents); |
| 505 | } |
| 506 | |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 507 | static unsigned short graph_get_current_column_color(const struct git_graph *graph) |
| 508 | { |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 509 | if (!want_color(graph->revs->diffopt.use_color)) |
Johan Herland | 1e3d411 | 2010-07-13 23:23:39 +0200 | [diff] [blame] | 510 | return column_colors_max; |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 511 | return graph->default_column_color; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Update the graph's default column color. |
| 516 | */ |
| 517 | static void graph_increment_column_color(struct git_graph *graph) |
| 518 | { |
| 519 | graph->default_column_color = (graph->default_column_color + 1) % |
Johan Herland | 1e3d411 | 2010-07-13 23:23:39 +0200 | [diff] [blame] | 520 | column_colors_max; |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static unsigned short graph_find_commit_color(const struct git_graph *graph, |
| 524 | const struct commit *commit) |
| 525 | { |
| 526 | int i; |
| 527 | for (i = 0; i < graph->num_columns; i++) { |
| 528 | if (graph->columns[i].commit == commit) |
| 529 | return graph->columns[i].color; |
| 530 | } |
| 531 | return graph_get_current_column_color(graph); |
| 532 | } |
| 533 | |
James Coglan | 9157a2a | 2019-10-15 23:47:49 +0000 | [diff] [blame] | 534 | static int graph_find_new_column_by_commit(struct git_graph *graph, |
| 535 | struct commit *commit) |
| 536 | { |
| 537 | int i; |
| 538 | for (i = 0; i < graph->num_new_columns; i++) { |
| 539 | if (graph->new_columns[i].commit == commit) |
| 540 | return i; |
| 541 | } |
| 542 | return -1; |
| 543 | } |
| 544 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 545 | static void graph_insert_into_new_columns(struct git_graph *graph, |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 546 | struct commit *commit, |
| 547 | int idx) |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 548 | { |
James Coglan | 9157a2a | 2019-10-15 23:47:49 +0000 | [diff] [blame] | 549 | int i = graph_find_new_column_by_commit(graph, commit); |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 550 | int mapping_idx; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 551 | |
| 552 | /* |
James Coglan | a551fd5 | 2019-10-15 23:47:50 +0000 | [diff] [blame] | 553 | * If the commit is not already in the new_columns array, then add it |
| 554 | * and record it as being in the final column. |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 555 | */ |
James Coglan | a551fd5 | 2019-10-15 23:47:50 +0000 | [diff] [blame] | 556 | if (i < 0) { |
| 557 | i = graph->num_new_columns++; |
| 558 | graph->new_columns[i].commit = commit; |
| 559 | graph->new_columns[i].color = graph_find_commit_color(graph, commit); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 560 | } |
| 561 | |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 562 | if (graph->num_parents > 1 && idx > -1 && graph->merge_layout == -1) { |
| 563 | /* |
| 564 | * If this is the first parent of a merge, choose a layout for |
| 565 | * the merge line based on whether the parent appears in a |
| 566 | * column to the left of the merge |
| 567 | */ |
| 568 | int dist, shift; |
| 569 | |
| 570 | dist = idx - i; |
| 571 | shift = (dist > 1) ? 2 * dist - 3 : 1; |
| 572 | |
| 573 | graph->merge_layout = (dist > 0) ? 0 : 1; |
James Coglan | 92beecc | 2019-10-15 23:47:58 +0000 | [diff] [blame] | 574 | graph->edges_added = graph->num_parents + graph->merge_layout - 2; |
| 575 | |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 576 | mapping_idx = graph->width + (graph->merge_layout - 1) * shift; |
| 577 | graph->width += 2 * graph->merge_layout; |
James Coglan | 92beecc | 2019-10-15 23:47:58 +0000 | [diff] [blame] | 578 | |
| 579 | } else if (graph->edges_added > 0 && i == graph->mapping[graph->width - 2]) { |
| 580 | /* |
| 581 | * If some columns have been added by a merge, but this commit |
| 582 | * was found in the last existing column, then adjust the |
| 583 | * numbers so that the two edges immediately join, i.e.: |
| 584 | * |
| 585 | * * | * | |
| 586 | * |\ \ => |\| |
| 587 | * | |/ | * |
| 588 | * | * |
| 589 | */ |
| 590 | mapping_idx = graph->width - 2; |
| 591 | graph->edges_added = -1; |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 592 | } else { |
| 593 | mapping_idx = graph->width; |
| 594 | graph->width += 2; |
| 595 | } |
| 596 | |
| 597 | graph->mapping[mapping_idx] = i; |
Adam Simpkins | 0724cb8 | 2008-05-05 00:57:03 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 600 | static void graph_update_columns(struct git_graph *graph) |
| 601 | { |
| 602 | struct commit_list *parent; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 603 | int max_new_columns; |
Adam Simpkins | 0724cb8 | 2008-05-05 00:57:03 -0700 | [diff] [blame] | 604 | int i, seen_this, is_commit_in_columns; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 605 | |
| 606 | /* |
| 607 | * Swap graph->columns with graph->new_columns |
| 608 | * graph->columns contains the state for the previous commit, |
| 609 | * and new_columns now contains the state for our commit. |
| 610 | * |
| 611 | * We'll re-use the old columns array as storage to compute the new |
| 612 | * columns list for the commit after this one. |
| 613 | */ |
René Scharfe | 9e2edd6 | 2017-01-28 22:42:15 +0100 | [diff] [blame] | 614 | SWAP(graph->columns, graph->new_columns); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 615 | graph->num_columns = graph->num_new_columns; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 616 | graph->num_new_columns = 0; |
| 617 | |
| 618 | /* |
| 619 | * Now update new_columns and mapping with the information for the |
| 620 | * commit after this one. |
| 621 | * |
| 622 | * First, make sure we have enough room. At most, there will |
| 623 | * be graph->num_columns + graph->num_parents columns for the next |
| 624 | * commit. |
| 625 | */ |
| 626 | max_new_columns = graph->num_columns + graph->num_parents; |
| 627 | graph_ensure_capacity(graph, max_new_columns); |
| 628 | |
| 629 | /* |
| 630 | * Clear out graph->mapping |
| 631 | */ |
| 632 | graph->mapping_size = 2 * max_new_columns; |
| 633 | for (i = 0; i < graph->mapping_size; i++) |
| 634 | graph->mapping[i] = -1; |
| 635 | |
James Coglan | 46ba2ab | 2019-10-15 23:47:51 +0000 | [diff] [blame] | 636 | graph->width = 0; |
James Coglan | 92beecc | 2019-10-15 23:47:58 +0000 | [diff] [blame] | 637 | graph->prev_edges_added = graph->edges_added; |
| 638 | graph->edges_added = 0; |
James Coglan | 46ba2ab | 2019-10-15 23:47:51 +0000 | [diff] [blame] | 639 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 640 | /* |
| 641 | * Populate graph->new_columns and graph->mapping |
| 642 | * |
| 643 | * Some of the parents of this commit may already be in |
| 644 | * graph->columns. If so, graph->new_columns should only contain a |
| 645 | * single entry for each such commit. graph->mapping should |
| 646 | * contain information about where each current branch line is |
| 647 | * supposed to end up after the collapsing is performed. |
| 648 | */ |
| 649 | seen_this = 0; |
Adam Simpkins | 0724cb8 | 2008-05-05 00:57:03 -0700 | [diff] [blame] | 650 | is_commit_in_columns = 1; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 651 | for (i = 0; i <= graph->num_columns; i++) { |
| 652 | struct commit *col_commit; |
| 653 | if (i == graph->num_columns) { |
| 654 | if (seen_this) |
| 655 | break; |
Adam Simpkins | 0724cb8 | 2008-05-05 00:57:03 -0700 | [diff] [blame] | 656 | is_commit_in_columns = 0; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 657 | col_commit = graph->commit; |
| 658 | } else { |
| 659 | col_commit = graph->columns[i].commit; |
| 660 | } |
| 661 | |
| 662 | if (col_commit == graph->commit) { |
| 663 | seen_this = 1; |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 664 | graph->commit_index = i; |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 665 | graph->merge_layout = -1; |
Adam Simpkins | a0ebe57 | 2008-06-05 01:56:19 -0700 | [diff] [blame] | 666 | for (parent = first_interesting_parent(graph); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 667 | parent; |
Adam Simpkins | a0ebe57 | 2008-06-05 01:56:19 -0700 | [diff] [blame] | 668 | parent = next_interesting_parent(graph, parent)) { |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 669 | /* |
Adam Simpkins | 91e50b2 | 2009-08-18 14:41:12 -0700 | [diff] [blame] | 670 | * If this is a merge, or the start of a new |
| 671 | * childless column, increment the current |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 672 | * color. |
| 673 | */ |
Adam Simpkins | 91e50b2 | 2009-08-18 14:41:12 -0700 | [diff] [blame] | 674 | if (graph->num_parents > 1 || |
| 675 | !is_commit_in_columns) { |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 676 | graph_increment_column_color(graph); |
Adam Simpkins | 91e50b2 | 2009-08-18 14:41:12 -0700 | [diff] [blame] | 677 | } |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 678 | graph_insert_into_new_columns(graph, parent->item, i); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 679 | } |
Adam Simpkins | 37a75ab | 2008-05-23 19:24:11 -0700 | [diff] [blame] | 680 | /* |
James Coglan | 46ba2ab | 2019-10-15 23:47:51 +0000 | [diff] [blame] | 681 | * We always need to increment graph->width by at |
Adam Simpkins | 37a75ab | 2008-05-23 19:24:11 -0700 | [diff] [blame] | 682 | * least 2, even if it has no interesting parents. |
| 683 | * The current commit always takes up at least 2 |
| 684 | * spaces. |
| 685 | */ |
James Coglan | 46ba2ab | 2019-10-15 23:47:51 +0000 | [diff] [blame] | 686 | if (graph->num_parents == 0) |
| 687 | graph->width += 2; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 688 | } else { |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 689 | graph_insert_into_new_columns(graph, col_commit, -1); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * Shrink mapping_size to be the minimum necessary |
| 695 | */ |
| 696 | while (graph->mapping_size > 1 && |
| 697 | graph->mapping[graph->mapping_size - 1] < 0) |
| 698 | graph->mapping_size--; |
| 699 | } |
| 700 | |
James Coglan | bbb13e8 | 2019-10-15 23:47:59 +0000 | [diff] [blame] | 701 | static int graph_num_dashed_parents(struct git_graph *graph) |
| 702 | { |
| 703 | return graph->num_parents + graph->merge_layout - 3; |
| 704 | } |
| 705 | |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 706 | static int graph_num_expansion_rows(struct git_graph *graph) |
| 707 | { |
| 708 | /* |
| 709 | * Normally, we need two expansion rows for each dashed parent line from |
| 710 | * an octopus merge: |
| 711 | * |
| 712 | * | * |
| 713 | * | |\ |
| 714 | * | | \ |
| 715 | * | | \ |
| 716 | * | *-. \ |
| 717 | * | |\ \ \ |
| 718 | * |
| 719 | * If the merge is skewed to the left, then its parents occupy one less |
| 720 | * column, and we don't need as many expansion rows to route around it; |
| 721 | * in some cases that means we don't need any expansion rows at all: |
| 722 | * |
| 723 | * | * |
| 724 | * | |\ |
| 725 | * | * \ |
| 726 | * |/|\ \ |
| 727 | */ |
James Coglan | bbb13e8 | 2019-10-15 23:47:59 +0000 | [diff] [blame] | 728 | return graph_num_dashed_parents(graph) * 2; |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 729 | } |
| 730 | |
James Coglan | ee7abb5 | 2019-10-15 23:47:52 +0000 | [diff] [blame] | 731 | static int graph_needs_pre_commit_line(struct git_graph *graph) |
| 732 | { |
| 733 | return graph->num_parents >= 3 && |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 734 | graph->commit_index < (graph->num_columns - 1) && |
| 735 | graph->expansion_row < graph_num_expansion_rows(graph); |
James Coglan | ee7abb5 | 2019-10-15 23:47:52 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 738 | void graph_update(struct git_graph *graph, struct commit *commit) |
| 739 | { |
| 740 | struct commit_list *parent; |
| 741 | |
| 742 | /* |
| 743 | * Set the new commit |
| 744 | */ |
| 745 | graph->commit = commit; |
| 746 | |
| 747 | /* |
Adam Simpkins | 37a75ab | 2008-05-23 19:24:11 -0700 | [diff] [blame] | 748 | * Count how many interesting parents this commit has |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 749 | */ |
| 750 | graph->num_parents = 0; |
Adam Simpkins | a0ebe57 | 2008-06-05 01:56:19 -0700 | [diff] [blame] | 751 | for (parent = first_interesting_parent(graph); |
| 752 | parent; |
| 753 | parent = next_interesting_parent(graph, parent)) |
| 754 | { |
| 755 | graph->num_parents++; |
Adam Simpkins | 37a75ab | 2008-05-23 19:24:11 -0700 | [diff] [blame] | 756 | } |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 757 | |
| 758 | /* |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 759 | * Store the old commit_index in prev_commit_index. |
| 760 | * graph_update_columns() will update graph->commit_index for this |
| 761 | * commit. |
| 762 | */ |
| 763 | graph->prev_commit_index = graph->commit_index; |
| 764 | |
| 765 | /* |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 766 | * Call graph_update_columns() to update |
| 767 | * columns, new_columns, and mapping. |
| 768 | */ |
| 769 | graph_update_columns(graph); |
| 770 | |
| 771 | graph->expansion_row = 0; |
| 772 | |
| 773 | /* |
| 774 | * Update graph->state. |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 775 | * Note that we don't call graph_update_state() here, since |
| 776 | * we don't want to update graph->prev_state. No line for |
| 777 | * graph->state was ever printed. |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 778 | * |
| 779 | * If the previous commit didn't get to the GRAPH_PADDING state, |
| 780 | * it never finished its output. Goto GRAPH_SKIP, to print out |
| 781 | * a line to indicate that portion of the graph is missing. |
| 782 | * |
Adam Simpkins | f1979d6 | 2008-06-01 13:56:58 -0700 | [diff] [blame] | 783 | * If there are 3 or more parents, we may need to print extra rows |
| 784 | * before the commit, to expand the branch lines around it and make |
| 785 | * room for it. We need to do this only if there is a branch row |
| 786 | * (or more) to the right of this commit. |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 787 | * |
| 788 | * If there are less than 3 parents, we can immediately print the |
| 789 | * commit line. |
| 790 | */ |
| 791 | if (graph->state != GRAPH_PADDING) |
| 792 | graph->state = GRAPH_SKIP; |
James Coglan | ee7abb5 | 2019-10-15 23:47:52 +0000 | [diff] [blame] | 793 | else if (graph_needs_pre_commit_line(graph)) |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 794 | graph->state = GRAPH_PRE_COMMIT; |
| 795 | else |
| 796 | graph->state = GRAPH_COMMIT; |
| 797 | } |
| 798 | |
| 799 | static int graph_is_mapping_correct(struct git_graph *graph) |
| 800 | { |
| 801 | int i; |
| 802 | |
| 803 | /* |
| 804 | * The mapping is up to date if each entry is at its target, |
| 805 | * or is 1 greater than its target. |
| 806 | * (If it is 1 greater than the target, '/' will be printed, so it |
| 807 | * will look correct on the next row.) |
| 808 | */ |
| 809 | for (i = 0; i < graph->mapping_size; i++) { |
| 810 | int target = graph->mapping[i]; |
| 811 | if (target < 0) |
| 812 | continue; |
| 813 | if (target == (i / 2)) |
| 814 | continue; |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | return 1; |
| 819 | } |
| 820 | |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 821 | static void graph_pad_horizontally(struct git_graph *graph, struct graph_line *line) |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 822 | { |
| 823 | /* |
| 824 | * Add additional spaces to the end of the strbuf, so that all |
| 825 | * lines for a particular commit have the same width. |
| 826 | * |
| 827 | * This way, fields printed to the right of the graph will remain |
| 828 | * aligned for the entire commit. |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 829 | */ |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 830 | if (line->width < graph->width) |
| 831 | graph_line_addchars(line, ' ', graph->width - line->width); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | static void graph_output_padding_line(struct git_graph *graph, |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 835 | struct graph_line *line) |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 836 | { |
| 837 | int i; |
| 838 | |
| 839 | /* |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 840 | * Output a padding row, that leaves all branch lines unchanged |
| 841 | */ |
| 842 | for (i = 0; i < graph->num_new_columns; i++) { |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 843 | graph_line_write_column(line, &graph->new_columns[i], '|'); |
| 844 | graph_line_addch(line, ' '); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 845 | } |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Josef Kufner | 3ad87c8 | 2016-06-16 20:18:37 +0700 | [diff] [blame] | 848 | |
| 849 | int graph_width(struct git_graph *graph) |
| 850 | { |
| 851 | return graph->width; |
| 852 | } |
| 853 | |
| 854 | |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 855 | static void graph_output_skip_line(struct git_graph *graph, struct graph_line *line) |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 856 | { |
| 857 | /* |
| 858 | * Output an ellipsis to indicate that a portion |
| 859 | * of the graph is missing. |
| 860 | */ |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 861 | graph_line_addstr(line, "..."); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 862 | |
James Coglan | ee7abb5 | 2019-10-15 23:47:52 +0000 | [diff] [blame] | 863 | if (graph_needs_pre_commit_line(graph)) |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 864 | graph_update_state(graph, GRAPH_PRE_COMMIT); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 865 | else |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 866 | graph_update_state(graph, GRAPH_COMMIT); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | static void graph_output_pre_commit_line(struct git_graph *graph, |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 870 | struct graph_line *line) |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 871 | { |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 872 | int i, seen_this; |
| 873 | |
| 874 | /* |
| 875 | * This function formats a row that increases the space around a commit |
| 876 | * with multiple parents, to make room for it. It should only be |
| 877 | * called when there are 3 or more parents. |
| 878 | * |
| 879 | * We need 2 extra rows for every parent over 2. |
| 880 | */ |
| 881 | assert(graph->num_parents >= 3); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 882 | |
| 883 | /* |
| 884 | * graph->expansion_row tracks the current expansion row we are on. |
| 885 | * It should be in the range [0, num_expansion_rows - 1] |
| 886 | */ |
| 887 | assert(0 <= graph->expansion_row && |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 888 | graph->expansion_row < graph_num_expansion_rows(graph)); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 889 | |
| 890 | /* |
| 891 | * Output the row |
| 892 | */ |
| 893 | seen_this = 0; |
| 894 | for (i = 0; i < graph->num_columns; i++) { |
| 895 | struct column *col = &graph->columns[i]; |
| 896 | if (col->commit == graph->commit) { |
| 897 | seen_this = 1; |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 898 | graph_line_write_column(line, col, '|'); |
| 899 | graph_line_addchars(line, ' ', graph->expansion_row); |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 900 | } else if (seen_this && (graph->expansion_row == 0)) { |
| 901 | /* |
| 902 | * This is the first line of the pre-commit output. |
| 903 | * If the previous commit was a merge commit and |
| 904 | * ended in the GRAPH_POST_MERGE state, all branch |
| 905 | * lines after graph->prev_commit_index were |
| 906 | * printed as "\" on the previous line. Continue |
| 907 | * to print them as "\" on this line. Otherwise, |
| 908 | * print the branch lines as "|". |
| 909 | */ |
| 910 | if (graph->prev_state == GRAPH_POST_MERGE && |
| 911 | graph->prev_commit_index < i) |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 912 | graph_line_write_column(line, col, '\\'); |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 913 | else |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 914 | graph_line_write_column(line, col, '|'); |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 915 | } else if (seen_this && (graph->expansion_row > 0)) { |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 916 | graph_line_write_column(line, col, '\\'); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 917 | } else { |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 918 | graph_line_write_column(line, col, '|'); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 919 | } |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 920 | graph_line_addch(line, ' '); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 923 | /* |
| 924 | * Increment graph->expansion_row, |
| 925 | * and move to state GRAPH_COMMIT if necessary |
| 926 | */ |
| 927 | graph->expansion_row++; |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 928 | if (!graph_needs_pre_commit_line(graph)) |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 929 | graph_update_state(graph, GRAPH_COMMIT); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 930 | } |
| 931 | |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 932 | static void graph_output_commit_char(struct git_graph *graph, struct graph_line *line) |
Adam Simpkins | 3c68d67 | 2008-05-24 16:02:04 -0700 | [diff] [blame] | 933 | { |
| 934 | /* |
| 935 | * For boundary commits, print 'o' |
| 936 | * (We should only see boundary commits when revs->boundary is set.) |
| 937 | */ |
| 938 | if (graph->commit->object.flags & BOUNDARY) { |
| 939 | assert(graph->revs->boundary); |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 940 | graph_line_addch(line, 'o'); |
Adam Simpkins | 3c68d67 | 2008-05-24 16:02:04 -0700 | [diff] [blame] | 941 | return; |
| 942 | } |
| 943 | |
| 944 | /* |
Michael J Gruber | 1df2d65 | 2011-03-07 13:31:39 +0100 | [diff] [blame] | 945 | * get_revision_mark() handles all other cases without assert() |
Adam Simpkins | 3c68d67 | 2008-05-24 16:02:04 -0700 | [diff] [blame] | 946 | */ |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 947 | graph_line_addstr(line, get_revision_mark(graph->revs, graph->commit)); |
Adam Simpkins | 3c68d67 | 2008-05-24 16:02:04 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 950 | /* |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 951 | * Draw the horizontal dashes of an octopus merge. |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 952 | */ |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 953 | static void graph_draw_octopus_merge(struct git_graph *graph, struct graph_line *line) |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 954 | { |
| 955 | /* |
James Coglan | bbb13e8 | 2019-10-15 23:47:59 +0000 | [diff] [blame] | 956 | * The parents of a merge commit can be arbitrarily reordered as they |
| 957 | * are mapped onto display columns, for example this is a valid merge: |
Noam Postavsky | 0400583 | 2018-09-01 20:07:16 -0400 | [diff] [blame] | 958 | * |
James Coglan | bbb13e8 | 2019-10-15 23:47:59 +0000 | [diff] [blame] | 959 | * | | *---. |
| 960 | * | | |\ \ \ |
| 961 | * | | |/ / / |
| 962 | * | |/| | / |
| 963 | * | |_|_|/ |
| 964 | * |/| | | |
| 965 | * 3 1 0 2 |
Noam Postavsky | 0400583 | 2018-09-01 20:07:16 -0400 | [diff] [blame] | 966 | * |
James Coglan | bbb13e8 | 2019-10-15 23:47:59 +0000 | [diff] [blame] | 967 | * The numbers denote which parent of the merge each visual column |
| 968 | * corresponds to; we can't assume that the parents will initially |
| 969 | * display in the order given by new_columns. |
| 970 | * |
| 971 | * To find the right color for each dash, we need to consult the |
| 972 | * mapping array, starting from the column 2 places to the right of the |
| 973 | * merge commit, and use that to find out which logical column each |
| 974 | * edge will collapse to. |
| 975 | * |
| 976 | * Commits are rendered once all edges have collapsed to their correct |
| 977 | * logcial column, so commit_index gives us the right visual offset for |
| 978 | * the merge commit. |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 979 | */ |
Noam Postavsky | 0400583 | 2018-09-01 20:07:16 -0400 | [diff] [blame] | 980 | |
James Coglan | bbb13e8 | 2019-10-15 23:47:59 +0000 | [diff] [blame] | 981 | int i, j; |
| 982 | struct column *col; |
Noam Postavsky | 0400583 | 2018-09-01 20:07:16 -0400 | [diff] [blame] | 983 | |
James Coglan | bbb13e8 | 2019-10-15 23:47:59 +0000 | [diff] [blame] | 984 | int dashed_parents = graph_num_dashed_parents(graph); |
Noam Postavsky | 0400583 | 2018-09-01 20:07:16 -0400 | [diff] [blame] | 985 | |
James Coglan | bbb13e8 | 2019-10-15 23:47:59 +0000 | [diff] [blame] | 986 | for (i = 0; i < dashed_parents; i++) { |
| 987 | j = graph->mapping[(graph->commit_index + i + 2) * 2]; |
| 988 | col = &graph->new_columns[j]; |
| 989 | |
| 990 | graph_line_write_column(line, col, '-'); |
| 991 | graph_line_write_column(line, col, (i == dashed_parents - 1) ? '.' : '-'); |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 992 | } |
James Coglan | bbb13e8 | 2019-10-15 23:47:59 +0000 | [diff] [blame] | 993 | |
| 994 | return; |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 995 | } |
| 996 | |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 997 | static void graph_output_commit_line(struct git_graph *graph, struct graph_line *line) |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 998 | { |
| 999 | int seen_this = 0; |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1000 | int i; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1001 | |
| 1002 | /* |
| 1003 | * Output the row containing this commit |
| 1004 | * Iterate up to and including graph->num_columns, |
| 1005 | * since the current commit may not be in any of the existing |
| 1006 | * columns. (This happens when the current commit doesn't have any |
| 1007 | * children that we have already processed.) |
| 1008 | */ |
| 1009 | seen_this = 0; |
| 1010 | for (i = 0; i <= graph->num_columns; i++) { |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 1011 | struct column *col = &graph->columns[i]; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1012 | struct commit *col_commit; |
| 1013 | if (i == graph->num_columns) { |
| 1014 | if (seen_this) |
| 1015 | break; |
| 1016 | col_commit = graph->commit; |
| 1017 | } else { |
| 1018 | col_commit = graph->columns[i].commit; |
| 1019 | } |
| 1020 | |
| 1021 | if (col_commit == graph->commit) { |
| 1022 | seen_this = 1; |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1023 | graph_output_commit_char(graph, line); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1024 | |
Allan Caffee | a6c1a38 | 2009-04-22 17:27:59 -0400 | [diff] [blame] | 1025 | if (graph->num_parents > 2) |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1026 | graph_draw_octopus_merge(graph, line); |
James Coglan | d62893e | 2019-10-15 23:47:55 +0000 | [diff] [blame] | 1027 | } else if (seen_this && (graph->edges_added > 1)) { |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1028 | graph_line_write_column(line, col, '\\'); |
James Coglan | d62893e | 2019-10-15 23:47:55 +0000 | [diff] [blame] | 1029 | } else if (seen_this && (graph->edges_added == 1)) { |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 1030 | /* |
James Coglan | d62893e | 2019-10-15 23:47:55 +0000 | [diff] [blame] | 1031 | * This is either a right-skewed 2-way merge |
| 1032 | * commit, or a left-skewed 3-way merge. |
| 1033 | * There is no GRAPH_PRE_COMMIT stage for such |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 1034 | * merges, so this is the first line of output |
| 1035 | * for this commit. Check to see what the previous |
| 1036 | * line of output was. |
| 1037 | * |
| 1038 | * If it was GRAPH_POST_MERGE, the branch line |
| 1039 | * coming into this commit may have been '\', |
| 1040 | * and not '|' or '/'. If so, output the branch |
| 1041 | * line as '\' on this line, instead of '|'. This |
| 1042 | * makes the output look nicer. |
| 1043 | */ |
| 1044 | if (graph->prev_state == GRAPH_POST_MERGE && |
James Coglan | d62893e | 2019-10-15 23:47:55 +0000 | [diff] [blame] | 1045 | graph->prev_edges_added > 0 && |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 1046 | graph->prev_commit_index < i) |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1047 | graph_line_write_column(line, col, '\\'); |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 1048 | else |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1049 | graph_line_write_column(line, col, '|'); |
James Coglan | 479db18 | 2019-10-15 23:47:57 +0000 | [diff] [blame] | 1050 | } else if (graph->prev_state == GRAPH_COLLAPSING && |
| 1051 | graph->old_mapping[2 * i + 1] == i && |
| 1052 | graph->mapping[2 * i] < i) { |
| 1053 | graph_line_write_column(line, col, '/'); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1054 | } else { |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1055 | graph_line_write_column(line, col, '|'); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1056 | } |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1057 | graph_line_addch(line, ' '); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1060 | /* |
| 1061 | * Update graph->state |
| 1062 | */ |
| 1063 | if (graph->num_parents > 1) |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 1064 | graph_update_state(graph, GRAPH_POST_MERGE); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1065 | else if (graph_is_mapping_correct(graph)) |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 1066 | graph_update_state(graph, GRAPH_PADDING); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1067 | else |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 1068 | graph_update_state(graph, GRAPH_COLLAPSING); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Đoàn Trần Công Danh | 9d2152d | 2020-04-27 21:22:36 +0700 | [diff] [blame] | 1071 | static const char merge_chars[] = {'/', '|', '\\'}; |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 1072 | |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1073 | static void graph_output_post_merge_line(struct git_graph *graph, struct graph_line *line) |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1074 | { |
| 1075 | int seen_this = 0; |
James Coglan | 92beecc | 2019-10-15 23:47:58 +0000 | [diff] [blame] | 1076 | int i, j; |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 1077 | |
| 1078 | struct commit_list *first_parent = first_interesting_parent(graph); |
Derrick Stolee | a1087c9 | 2020-01-07 21:27:02 +0000 | [diff] [blame] | 1079 | struct column *parent_col = NULL; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1080 | |
| 1081 | /* |
| 1082 | * Output the post-merge row |
| 1083 | */ |
| 1084 | for (i = 0; i <= graph->num_columns; i++) { |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 1085 | struct column *col = &graph->columns[i]; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1086 | struct commit *col_commit; |
| 1087 | if (i == graph->num_columns) { |
| 1088 | if (seen_this) |
| 1089 | break; |
| 1090 | col_commit = graph->commit; |
| 1091 | } else { |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 1092 | col_commit = col->commit; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | if (col_commit == graph->commit) { |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 1096 | /* |
| 1097 | * Since the current commit is a merge find |
| 1098 | * the columns for the parent commits in |
| 1099 | * new_columns and use those to format the |
| 1100 | * edges. |
| 1101 | */ |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 1102 | struct commit_list *parents = first_parent; |
James Coglan | 9157a2a | 2019-10-15 23:47:49 +0000 | [diff] [blame] | 1103 | int par_column; |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 1104 | int idx = graph->merge_layout; |
| 1105 | char c; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1106 | seen_this = 1; |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 1107 | |
James Coglan | 92beecc | 2019-10-15 23:47:58 +0000 | [diff] [blame] | 1108 | for (j = 0; j < graph->num_parents; j++) { |
James Coglan | 9157a2a | 2019-10-15 23:47:49 +0000 | [diff] [blame] | 1109 | par_column = graph_find_new_column_by_commit(graph, parents->item); |
| 1110 | assert(par_column >= 0); |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 1111 | |
| 1112 | c = merge_chars[idx]; |
| 1113 | graph_line_write_column(line, &graph->new_columns[par_column], c); |
James Coglan | 92beecc | 2019-10-15 23:47:58 +0000 | [diff] [blame] | 1114 | if (idx == 2) { |
| 1115 | if (graph->edges_added > 0 || j < graph->num_parents - 1) |
| 1116 | graph_line_addch(line, ' '); |
| 1117 | } else { |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 1118 | idx++; |
James Coglan | 92beecc | 2019-10-15 23:47:58 +0000 | [diff] [blame] | 1119 | } |
| 1120 | parents = next_interesting_parent(graph, parents); |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 1121 | } |
James Coglan | d62893e | 2019-10-15 23:47:55 +0000 | [diff] [blame] | 1122 | if (graph->edges_added == 0) |
| 1123 | graph_line_addch(line, ' '); |
| 1124 | |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 1125 | } else if (seen_this) { |
James Coglan | d62893e | 2019-10-15 23:47:55 +0000 | [diff] [blame] | 1126 | if (graph->edges_added > 0) |
| 1127 | graph_line_write_column(line, col, '\\'); |
| 1128 | else |
| 1129 | graph_line_write_column(line, col, '|'); |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1130 | graph_line_addch(line, ' '); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1131 | } else { |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1132 | graph_line_write_column(line, col, '|'); |
Derrick Stolee | a1087c9 | 2020-01-07 21:27:02 +0000 | [diff] [blame] | 1133 | if (graph->merge_layout != 0 || i != graph->commit_index - 1) { |
| 1134 | if (parent_col) |
| 1135 | graph_line_write_column( |
| 1136 | line, parent_col, '_'); |
| 1137 | else |
| 1138 | graph_line_addch(line, ' '); |
| 1139 | } |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1140 | } |
James Coglan | 0f0f389 | 2019-10-15 23:47:54 +0000 | [diff] [blame] | 1141 | |
| 1142 | if (col_commit == first_parent->item) |
Derrick Stolee | a1087c9 | 2020-01-07 21:27:02 +0000 | [diff] [blame] | 1143 | parent_col = col; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1146 | /* |
| 1147 | * Update graph->state |
| 1148 | */ |
| 1149 | if (graph_is_mapping_correct(graph)) |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 1150 | graph_update_state(graph, GRAPH_PADDING); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1151 | else |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 1152 | graph_update_state(graph, GRAPH_COLLAPSING); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1155 | static void graph_output_collapsing_line(struct git_graph *graph, struct graph_line *line) |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1156 | { |
| 1157 | int i; |
Allan Caffee | eaf158f | 2009-04-21 08:47:01 -0400 | [diff] [blame] | 1158 | short used_horizontal = 0; |
| 1159 | int horizontal_edge = -1; |
| 1160 | int horizontal_edge_target = -1; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1161 | |
| 1162 | /* |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 1163 | * Swap the mapping and old_mapping arrays |
| 1164 | */ |
| 1165 | SWAP(graph->mapping, graph->old_mapping); |
| 1166 | |
| 1167 | /* |
| 1168 | * Clear out the mapping array |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1169 | */ |
| 1170 | for (i = 0; i < graph->mapping_size; i++) |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 1171 | graph->mapping[i] = -1; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1172 | |
| 1173 | for (i = 0; i < graph->mapping_size; i++) { |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 1174 | int target = graph->old_mapping[i]; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1175 | if (target < 0) |
| 1176 | continue; |
| 1177 | |
| 1178 | /* |
| 1179 | * Since update_columns() always inserts the leftmost |
| 1180 | * column first, each branch's target location should |
| 1181 | * always be either its current location or to the left of |
| 1182 | * its current location. |
| 1183 | * |
| 1184 | * We never have to move branches to the right. This makes |
| 1185 | * the graph much more legible, since whenever branches |
| 1186 | * cross, only one is moving directions. |
| 1187 | */ |
| 1188 | assert(target * 2 <= i); |
| 1189 | |
| 1190 | if (target * 2 == i) { |
| 1191 | /* |
| 1192 | * This column is already in the |
| 1193 | * correct place |
| 1194 | */ |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 1195 | assert(graph->mapping[i] == -1); |
| 1196 | graph->mapping[i] = target; |
| 1197 | } else if (graph->mapping[i - 1] < 0) { |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1198 | /* |
| 1199 | * Nothing is to the left. |
| 1200 | * Move to the left by one |
| 1201 | */ |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 1202 | graph->mapping[i - 1] = target; |
Allan Caffee | eaf158f | 2009-04-21 08:47:01 -0400 | [diff] [blame] | 1203 | /* |
| 1204 | * If there isn't already an edge moving horizontally |
| 1205 | * select this one. |
| 1206 | */ |
| 1207 | if (horizontal_edge == -1) { |
| 1208 | int j; |
| 1209 | horizontal_edge = i; |
| 1210 | horizontal_edge_target = target; |
| 1211 | /* |
| 1212 | * The variable target is the index of the graph |
| 1213 | * column, and therefore target*2+3 is the |
| 1214 | * actual screen column of the first horizontal |
| 1215 | * line. |
| 1216 | */ |
| 1217 | for (j = (target * 2)+3; j < (i - 2); j += 2) |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 1218 | graph->mapping[j] = target; |
Allan Caffee | eaf158f | 2009-04-21 08:47:01 -0400 | [diff] [blame] | 1219 | } |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 1220 | } else if (graph->mapping[i - 1] == target) { |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1221 | /* |
| 1222 | * There is a branch line to our left |
| 1223 | * already, and it is our target. We |
| 1224 | * combine with this line, since we share |
| 1225 | * the same parent commit. |
| 1226 | * |
| 1227 | * We don't have to add anything to the |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 1228 | * output or mapping, since the |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1229 | * existing branch line has already taken |
| 1230 | * care of it. |
| 1231 | */ |
| 1232 | } else { |
| 1233 | /* |
| 1234 | * There is a branch line to our left, |
| 1235 | * but it isn't our target. We need to |
| 1236 | * cross over it. |
| 1237 | * |
| 1238 | * The space just to the left of this |
| 1239 | * branch should always be empty. |
| 1240 | */ |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 1241 | assert(graph->mapping[i - 1] > target); |
| 1242 | assert(graph->mapping[i - 2] < 0); |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 1243 | graph->mapping[i - 2] = target; |
Allan Caffee | eaf158f | 2009-04-21 08:47:01 -0400 | [diff] [blame] | 1244 | /* |
| 1245 | * Mark this branch as the horizontal edge to |
| 1246 | * prevent any other edges from moving |
| 1247 | * horizontally. |
| 1248 | */ |
Derrick Stolee | c958d3b | 2020-01-08 04:27:55 +0000 | [diff] [blame] | 1249 | if (horizontal_edge == -1) { |
| 1250 | int j; |
| 1251 | horizontal_edge_target = target; |
| 1252 | horizontal_edge = i - 1; |
| 1253 | |
| 1254 | for (j = (target * 2) + 3; j < (i - 2); j += 2) |
| 1255 | graph->mapping[j] = target; |
| 1256 | } |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | /* |
James Coglan | 479db18 | 2019-10-15 23:47:57 +0000 | [diff] [blame] | 1261 | * Copy the current mapping array into old_mapping |
| 1262 | */ |
| 1263 | COPY_ARRAY(graph->old_mapping, graph->mapping, graph->mapping_size); |
| 1264 | |
| 1265 | /* |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1266 | * The new mapping may be 1 smaller than the old mapping |
| 1267 | */ |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 1268 | if (graph->mapping[graph->mapping_size - 1] < 0) |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1269 | graph->mapping_size--; |
| 1270 | |
| 1271 | /* |
| 1272 | * Output out a line based on the new mapping info |
| 1273 | */ |
| 1274 | for (i = 0; i < graph->mapping_size; i++) { |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 1275 | int target = graph->mapping[i]; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1276 | if (target < 0) |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1277 | graph_line_addch(line, ' '); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1278 | else if (target * 2 == i) |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1279 | graph_line_write_column(line, &graph->new_columns[target], '|'); |
Allan Caffee | eaf158f | 2009-04-21 08:47:01 -0400 | [diff] [blame] | 1280 | else if (target == horizontal_edge_target && |
| 1281 | i != horizontal_edge - 1) { |
| 1282 | /* |
| 1283 | * Set the mappings for all but the |
| 1284 | * first segment to -1 so that they |
| 1285 | * won't continue into the next line. |
| 1286 | */ |
| 1287 | if (i != (target * 2)+3) |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 1288 | graph->mapping[i] = -1; |
Allan Caffee | eaf158f | 2009-04-21 08:47:01 -0400 | [diff] [blame] | 1289 | used_horizontal = 1; |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1290 | graph_line_write_column(line, &graph->new_columns[target], '_'); |
Allan Caffee | eaf158f | 2009-04-21 08:47:01 -0400 | [diff] [blame] | 1291 | } else { |
| 1292 | if (used_horizontal && i < horizontal_edge) |
James Coglan | 0195285 | 2019-10-15 23:47:56 +0000 | [diff] [blame] | 1293 | graph->mapping[i] = -1; |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1294 | graph_line_write_column(line, &graph->new_columns[target], '/'); |
Allan Caffee | eaf158f | 2009-04-21 08:47:01 -0400 | [diff] [blame] | 1295 | |
| 1296 | } |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1299 | /* |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1300 | * If graph->mapping indicates that all of the branch lines |
| 1301 | * are already in the correct positions, we are done. |
| 1302 | * Otherwise, we need to collapse some branch lines together. |
| 1303 | */ |
| 1304 | if (graph_is_mapping_correct(graph)) |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 1305 | graph_update_state(graph, GRAPH_PADDING); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
John Keeping | ac751a0 | 2013-03-04 00:03:37 +0000 | [diff] [blame] | 1308 | int graph_next_line(struct git_graph *graph, struct strbuf *sb) |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1309 | { |
James Coglan | 210179a | 2019-10-15 23:47:48 +0000 | [diff] [blame] | 1310 | int shown_commit_line = 0; |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1311 | struct graph_line line = { .buf = sb, .width = 0 }; |
| 1312 | |
James Coglan | 210179a | 2019-10-15 23:47:48 +0000 | [diff] [blame] | 1313 | /* |
| 1314 | * We could conceivable be called with a NULL commit |
| 1315 | * if our caller has a bug, and invokes graph_next_line() |
| 1316 | * immediately after graph_init(), without first calling |
| 1317 | * graph_update(). Return without outputting anything in this |
| 1318 | * case. |
| 1319 | */ |
| 1320 | if (!graph->commit) |
| 1321 | return -1; |
| 1322 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1323 | switch (graph->state) { |
| 1324 | case GRAPH_PADDING: |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1325 | graph_output_padding_line(graph, &line); |
James Coglan | 210179a | 2019-10-15 23:47:48 +0000 | [diff] [blame] | 1326 | break; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1327 | case GRAPH_SKIP: |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1328 | graph_output_skip_line(graph, &line); |
James Coglan | 210179a | 2019-10-15 23:47:48 +0000 | [diff] [blame] | 1329 | break; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1330 | case GRAPH_PRE_COMMIT: |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1331 | graph_output_pre_commit_line(graph, &line); |
James Coglan | 210179a | 2019-10-15 23:47:48 +0000 | [diff] [blame] | 1332 | break; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1333 | case GRAPH_COMMIT: |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1334 | graph_output_commit_line(graph, &line); |
James Coglan | 210179a | 2019-10-15 23:47:48 +0000 | [diff] [blame] | 1335 | shown_commit_line = 1; |
| 1336 | break; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1337 | case GRAPH_POST_MERGE: |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1338 | graph_output_post_merge_line(graph, &line); |
James Coglan | 210179a | 2019-10-15 23:47:48 +0000 | [diff] [blame] | 1339 | break; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1340 | case GRAPH_COLLAPSING: |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1341 | graph_output_collapsing_line(graph, &line); |
James Coglan | 210179a | 2019-10-15 23:47:48 +0000 | [diff] [blame] | 1342 | break; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
James Coglan | 210179a | 2019-10-15 23:47:48 +0000 | [diff] [blame] | 1345 | graph_pad_horizontally(graph, &line); |
| 1346 | return shown_commit_line; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
Nanako Shiraishi | 064bfbd | 2008-09-25 18:41:08 +0900 | [diff] [blame] | 1349 | static void graph_padding_line(struct git_graph *graph, struct strbuf *sb) |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1350 | { |
René Scharfe | 415792e | 2014-09-07 09:06:42 +0200 | [diff] [blame] | 1351 | int i; |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1352 | struct graph_line line = { .buf = sb, .width = 0 }; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1353 | |
| 1354 | if (graph->state != GRAPH_COMMIT) { |
| 1355 | graph_next_line(graph, sb); |
| 1356 | return; |
| 1357 | } |
| 1358 | |
| 1359 | /* |
| 1360 | * Output the row containing this commit |
| 1361 | * Iterate up to and including graph->num_columns, |
| 1362 | * since the current commit may not be in any of the existing |
| 1363 | * columns. (This happens when the current commit doesn't have any |
| 1364 | * children that we have already processed.) |
| 1365 | */ |
| 1366 | for (i = 0; i < graph->num_columns; i++) { |
Allan Caffee | 427fc5b | 2009-04-13 15:53:41 -0400 | [diff] [blame] | 1367 | struct column *col = &graph->columns[i]; |
Jeff King | 1647793 | 2016-09-29 04:37:51 -0400 | [diff] [blame] | 1368 | |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1369 | graph_line_write_column(&line, col, '|'); |
Jeff King | 1647793 | 2016-09-29 04:37:51 -0400 | [diff] [blame] | 1370 | |
| 1371 | if (col->commit == graph->commit && graph->num_parents > 2) { |
| 1372 | int len = (graph->num_parents - 2) * 2; |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1373 | graph_line_addchars(&line, ' ', len); |
Jeff King | 1647793 | 2016-09-29 04:37:51 -0400 | [diff] [blame] | 1374 | } else { |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1375 | graph_line_addch(&line, ' '); |
Jeff King | 1647793 | 2016-09-29 04:37:51 -0400 | [diff] [blame] | 1376 | } |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
James Coglan | fbccf25 | 2019-10-15 23:47:47 +0000 | [diff] [blame] | 1379 | graph_pad_horizontally(graph, &line); |
Adam Simpkins | 3395908 | 2008-06-01 13:56:57 -0700 | [diff] [blame] | 1380 | |
| 1381 | /* |
| 1382 | * Update graph->prev_state since we have output a padding line |
| 1383 | */ |
| 1384 | graph->prev_state = GRAPH_PADDING; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | int graph_is_commit_finished(struct git_graph const *graph) |
| 1388 | { |
| 1389 | return (graph->state == GRAPH_PADDING); |
| 1390 | } |
| 1391 | |
| 1392 | void graph_show_commit(struct git_graph *graph) |
| 1393 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 1394 | struct strbuf msgbuf = STRBUF_INIT; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1395 | int shown_commit_line = 0; |
| 1396 | |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1397 | graph_show_line_prefix(default_diffopt); |
| 1398 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1399 | if (!graph) |
| 1400 | return; |
| 1401 | |
John Keeping | a48ec24 | 2013-02-07 20:15:23 +0000 | [diff] [blame] | 1402 | /* |
| 1403 | * When showing a diff of a merge against each of its parents, we |
| 1404 | * are called once for each parent without graph_update having been |
| 1405 | * called. In this case, simply output a single padding line. |
| 1406 | */ |
| 1407 | if (graph_is_commit_finished(graph)) { |
| 1408 | graph_show_padding(graph); |
| 1409 | shown_commit_line = 1; |
| 1410 | } |
| 1411 | |
Michał Kiedrowicz | 656197a | 2009-07-25 01:45:00 +0200 | [diff] [blame] | 1412 | while (!shown_commit_line && !graph_is_commit_finished(graph)) { |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1413 | shown_commit_line = graph_next_line(graph, &msgbuf); |
Johannes Schindelin | c61008f | 2016-06-22 17:01:44 +0200 | [diff] [blame] | 1414 | fwrite(msgbuf.buf, sizeof(char), msgbuf.len, |
| 1415 | graph->revs->diffopt.file); |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1416 | if (!shown_commit_line) { |
Johannes Schindelin | c61008f | 2016-06-22 17:01:44 +0200 | [diff] [blame] | 1417 | putc('\n', graph->revs->diffopt.file); |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1418 | graph_show_line_prefix(&graph->revs->diffopt); |
| 1419 | } |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1420 | strbuf_setlen(&msgbuf, 0); |
| 1421 | } |
| 1422 | |
| 1423 | strbuf_release(&msgbuf); |
| 1424 | } |
| 1425 | |
| 1426 | void graph_show_oneline(struct git_graph *graph) |
| 1427 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 1428 | struct strbuf msgbuf = STRBUF_INIT; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1429 | |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1430 | graph_show_line_prefix(default_diffopt); |
| 1431 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1432 | if (!graph) |
| 1433 | return; |
| 1434 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1435 | graph_next_line(graph, &msgbuf); |
Johannes Schindelin | c61008f | 2016-06-22 17:01:44 +0200 | [diff] [blame] | 1436 | fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1437 | strbuf_release(&msgbuf); |
| 1438 | } |
| 1439 | |
| 1440 | void graph_show_padding(struct git_graph *graph) |
| 1441 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 1442 | struct strbuf msgbuf = STRBUF_INIT; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1443 | |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1444 | graph_show_line_prefix(default_diffopt); |
| 1445 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1446 | if (!graph) |
| 1447 | return; |
| 1448 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1449 | graph_padding_line(graph, &msgbuf); |
Johannes Schindelin | c61008f | 2016-06-22 17:01:44 +0200 | [diff] [blame] | 1450 | fwrite(msgbuf.buf, sizeof(char), msgbuf.len, graph->revs->diffopt.file); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1451 | strbuf_release(&msgbuf); |
| 1452 | } |
| 1453 | |
| 1454 | int graph_show_remainder(struct git_graph *graph) |
| 1455 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 1456 | struct strbuf msgbuf = STRBUF_INIT; |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1457 | int shown = 0; |
| 1458 | |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1459 | graph_show_line_prefix(default_diffopt); |
| 1460 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1461 | if (!graph) |
| 1462 | return 0; |
| 1463 | |
| 1464 | if (graph_is_commit_finished(graph)) |
| 1465 | return 0; |
| 1466 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1467 | for (;;) { |
| 1468 | graph_next_line(graph, &msgbuf); |
Johannes Schindelin | c61008f | 2016-06-22 17:01:44 +0200 | [diff] [blame] | 1469 | fwrite(msgbuf.buf, sizeof(char), msgbuf.len, |
| 1470 | graph->revs->diffopt.file); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1471 | strbuf_setlen(&msgbuf, 0); |
| 1472 | shown = 1; |
| 1473 | |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1474 | if (!graph_is_commit_finished(graph)) { |
Johannes Schindelin | c61008f | 2016-06-22 17:01:44 +0200 | [diff] [blame] | 1475 | putc('\n', graph->revs->diffopt.file); |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1476 | graph_show_line_prefix(&graph->revs->diffopt); |
| 1477 | } else { |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1478 | break; |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1479 | } |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1480 | } |
| 1481 | strbuf_release(&msgbuf); |
| 1482 | |
| 1483 | return shown; |
| 1484 | } |
| 1485 | |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1486 | static void graph_show_strbuf(struct git_graph *graph, |
| 1487 | FILE *file, |
| 1488 | struct strbuf const *sb) |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1489 | { |
| 1490 | char *p; |
| 1491 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1492 | /* |
| 1493 | * Print the strbuf line by line, |
| 1494 | * and display the graph info before each line but the first. |
| 1495 | */ |
| 1496 | p = sb->buf; |
| 1497 | while (p) { |
| 1498 | size_t len; |
| 1499 | char *next_p = strchr(p, '\n'); |
| 1500 | if (next_p) { |
| 1501 | next_p++; |
| 1502 | len = next_p - p; |
| 1503 | } else { |
| 1504 | len = (sb->buf + sb->len) - p; |
| 1505 | } |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1506 | fwrite(p, sizeof(char), len, file); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1507 | if (next_p && *next_p != '\0') |
| 1508 | graph_show_oneline(graph); |
| 1509 | p = next_p; |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | void graph_show_commit_msg(struct git_graph *graph, |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1514 | FILE *file, |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1515 | struct strbuf const *sb) |
| 1516 | { |
| 1517 | int newline_terminated; |
| 1518 | |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1519 | /* |
| 1520 | * Show the commit message |
| 1521 | */ |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1522 | graph_show_strbuf(graph, file, sb); |
| 1523 | |
| 1524 | if (!graph) |
| 1525 | return; |
| 1526 | |
| 1527 | newline_terminated = (sb->len && sb->buf[sb->len - 1] == '\n'); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1528 | |
| 1529 | /* |
| 1530 | * If there is more output needed for this commit, show it now |
| 1531 | */ |
| 1532 | if (!graph_is_commit_finished(graph)) { |
| 1533 | /* |
| 1534 | * If sb doesn't have a terminating newline, print one now, |
| 1535 | * so we can start the remainder of the graph output on a |
| 1536 | * new line. |
| 1537 | */ |
| 1538 | if (!newline_terminated) |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1539 | putc('\n', file); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1540 | |
| 1541 | graph_show_remainder(graph); |
| 1542 | |
| 1543 | /* |
| 1544 | * If sb ends with a newline, our output should too. |
| 1545 | */ |
| 1546 | if (newline_terminated) |
Jacob Keller | 660e113 | 2016-08-31 16:27:20 -0700 | [diff] [blame] | 1547 | putc('\n', file); |
Adam Simpkins | c12172d | 2008-05-04 03:36:53 -0700 | [diff] [blame] | 1548 | } |
| 1549 | } |