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