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