1#include"cache.h" 2#include"commit.h" 3#include"color.h" 4#include"graph.h" 5#include"diff.h" 6#include"revision.h" 7 8/* Internal API */ 9 10/* 11 * Output the next line for a graph. 12 * This formats the next graph line into the specified strbuf. It is not 13 * terminated with a newline. 14 * 15 * Returns 1 if the line includes the current commit, and 0 otherwise. 16 * graph_next_line() will return 1 exactly once for each time 17 * graph_update() is called. 18 */ 19static intgraph_next_line(struct git_graph *graph,struct strbuf *sb); 20 21/* 22 * Output a padding line in the graph. 23 * This is similar to graph_next_line(). However, it is guaranteed to 24 * never print the current commit line. Instead, if the commit line is 25 * next, it will simply output a line of vertical padding, extending the 26 * branch lines downwards, but leaving them otherwise unchanged. 27 */ 28static voidgraph_padding_line(struct git_graph *graph,struct strbuf *sb); 29 30/* 31 * Print a strbuf to stdout. If the graph is non-NULL, all lines but the 32 * first will be prefixed with the graph output. 33 * 34 * If the strbuf ends with a newline, the output will end after this 35 * newline. A new graph line will not be printed after the final newline. 36 * If the strbuf is empty, no output will be printed. 37 * 38 * Since the first line will not include the graph output, the caller is 39 * responsible for printing this line's graph (perhaps via 40 * graph_show_commit() or graph_show_oneline()) before calling 41 * graph_show_strbuf(). 42 */ 43static voidgraph_show_strbuf(struct git_graph *graph,struct strbuf const*sb); 44 45/* 46 * TODO: 47 * - Limit the number of columns, similar to the way gitk does. 48 * If we reach more than a specified number of columns, omit 49 * sections of some columns. 50 */ 51 52struct column { 53/* 54 * The parent commit of this column. 55 */ 56struct commit *commit; 57/* 58 * The color to (optionally) print this column in. This is an 59 * index into column_colors. 60 */ 61unsigned short color; 62}; 63 64enum graph_state { 65 GRAPH_PADDING, 66 GRAPH_SKIP, 67 GRAPH_PRE_COMMIT, 68 GRAPH_COMMIT, 69 GRAPH_POST_MERGE, 70 GRAPH_COLLAPSING 71}; 72 73/* 74 * The list of available column colors. 75 */ 76static char column_colors[][COLOR_MAXLEN] = { 77 GIT_COLOR_RED, 78 GIT_COLOR_GREEN, 79 GIT_COLOR_YELLOW, 80 GIT_COLOR_BLUE, 81 GIT_COLOR_MAGENTA, 82 GIT_COLOR_CYAN, 83 GIT_COLOR_BOLD GIT_COLOR_RED, 84 GIT_COLOR_BOLD GIT_COLOR_GREEN, 85 GIT_COLOR_BOLD GIT_COLOR_YELLOW, 86 GIT_COLOR_BOLD GIT_COLOR_BLUE, 87 GIT_COLOR_BOLD GIT_COLOR_MAGENTA, 88 GIT_COLOR_BOLD GIT_COLOR_CYAN, 89}; 90 91#define COLUMN_COLORS_MAX (ARRAY_SIZE(column_colors)) 92 93static const char*column_get_color_code(const struct column *c) 94{ 95return column_colors[c->color]; 96} 97 98static voidstrbuf_write_column(struct strbuf *sb,const struct column *c, 99char col_char) 100{ 101if(c->color < COLUMN_COLORS_MAX) 102strbuf_addstr(sb,column_get_color_code(c)); 103strbuf_addch(sb, col_char); 104if(c->color < COLUMN_COLORS_MAX) 105strbuf_addstr(sb, GIT_COLOR_RESET); 106} 107 108struct git_graph { 109/* 110 * The commit currently being processed 111 */ 112struct commit *commit; 113/* The rev-info used for the current traversal */ 114struct rev_info *revs; 115/* 116 * The number of interesting parents that this commit has. 117 * 118 * Note that this is not the same as the actual number of parents. 119 * This count excludes parents that won't be printed in the graph 120 * output, as determined by graph_is_interesting(). 121 */ 122int num_parents; 123/* 124 * The width of the graph output for this commit. 125 * All rows for this commit are padded to this width, so that 126 * messages printed after the graph output are aligned. 127 */ 128int width; 129/* 130 * The next expansion row to print 131 * when state is GRAPH_PRE_COMMIT 132 */ 133int expansion_row; 134/* 135 * The current output state. 136 * This tells us what kind of line graph_next_line() should output. 137 */ 138enum graph_state state; 139/* 140 * The output state for the previous line of output. 141 * This is primarily used to determine how the first merge line 142 * should appear, based on the last line of the previous commit. 143 */ 144enum graph_state prev_state; 145/* 146 * The index of the column that refers to this commit. 147 * 148 * If none of the incoming columns refer to this commit, 149 * this will be equal to num_columns. 150 */ 151int commit_index; 152/* 153 * The commit_index for the previously displayed commit. 154 * 155 * This is used to determine how the first line of a merge 156 * graph output should appear, based on the last line of the 157 * previous commit. 158 */ 159int prev_commit_index; 160/* 161 * The maximum number of columns that can be stored in the columns 162 * and new_columns arrays. This is also half the number of entries 163 * that can be stored in the mapping and new_mapping arrays. 164 */ 165int column_capacity; 166/* 167 * The number of columns (also called "branch lines" in some places) 168 */ 169int num_columns; 170/* 171 * The number of columns in the new_columns array 172 */ 173int num_new_columns; 174/* 175 * The number of entries in the mapping array 176 */ 177int mapping_size; 178/* 179 * The column state before we output the current commit. 180 */ 181struct column *columns; 182/* 183 * The new column state after we output the current commit. 184 * Only valid when state is GRAPH_COLLAPSING. 185 */ 186struct column *new_columns; 187/* 188 * An array that tracks the current state of each 189 * character in the output line during state GRAPH_COLLAPSING. 190 * Each entry is -1 if this character is empty, or a non-negative 191 * integer if the character contains a branch line. The value of 192 * the integer indicates the target position for this branch line. 193 * (I.e., this array maps the current column positions to their 194 * desired positions.) 195 * 196 * The maximum capacity of this array is always 197 * sizeof(int) * 2 * column_capacity. 198 */ 199int*mapping; 200/* 201 * A temporary array for computing the next mapping state 202 * while we are outputting a mapping line. This is stored as part 203 * of the git_graph simply so we don't have to allocate a new 204 * temporary array each time we have to output a collapsing line. 205 */ 206int*new_mapping; 207/* 208 * The current default column color being used. This is 209 * stored as an index into the array column_colors. 210 */ 211unsigned short default_column_color; 212}; 213 214struct git_graph *graph_init(struct rev_info *opt) 215{ 216struct git_graph *graph =xmalloc(sizeof(struct git_graph)); 217 graph->commit = NULL; 218 graph->revs = opt; 219 graph->num_parents =0; 220 graph->expansion_row =0; 221 graph->state = GRAPH_PADDING; 222 graph->prev_state = GRAPH_PADDING; 223 graph->commit_index =0; 224 graph->prev_commit_index =0; 225 graph->num_columns =0; 226 graph->num_new_columns =0; 227 graph->mapping_size =0; 228/* 229 * Start the column color at the maximum value, since we'll 230 * always increment it for the first commit we output. 231 * This way we start at 0 for the first commit. 232 */ 233 graph->default_column_color = COLUMN_COLORS_MAX -1; 234 235/* 236 * Allocate a reasonably large default number of columns 237 * We'll automatically grow columns later if we need more room. 238 */ 239 graph->column_capacity =30; 240 graph->columns =xmalloc(sizeof(struct column) * 241 graph->column_capacity); 242 graph->new_columns =xmalloc(sizeof(struct column) * 243 graph->column_capacity); 244 graph->mapping =xmalloc(sizeof(int) *2* graph->column_capacity); 245 graph->new_mapping =xmalloc(sizeof(int) *2* graph->column_capacity); 246 247return graph; 248} 249 250static voidgraph_update_state(struct git_graph *graph,enum graph_state s) 251{ 252 graph->prev_state = graph->state; 253 graph->state = s; 254} 255 256static voidgraph_ensure_capacity(struct git_graph *graph,int num_columns) 257{ 258if(graph->column_capacity >= num_columns) 259return; 260 261do{ 262 graph->column_capacity *=2; 263}while(graph->column_capacity < num_columns); 264 265 graph->columns =xrealloc(graph->columns, 266sizeof(struct column) * 267 graph->column_capacity); 268 graph->new_columns =xrealloc(graph->new_columns, 269sizeof(struct column) * 270 graph->column_capacity); 271 graph->mapping =xrealloc(graph->mapping, 272sizeof(int) *2* graph->column_capacity); 273 graph->new_mapping =xrealloc(graph->new_mapping, 274sizeof(int) *2* graph->column_capacity); 275} 276 277/* 278 * Returns 1 if the commit will be printed in the graph output, 279 * and 0 otherwise. 280 */ 281static intgraph_is_interesting(struct git_graph *graph,struct commit *commit) 282{ 283/* 284 * If revs->boundary is set, commits whose children have 285 * been shown are always interesting, even if they have the 286 * UNINTERESTING or TREESAME flags set. 287 */ 288if(graph->revs && graph->revs->boundary) { 289if(commit->object.flags & CHILD_SHOWN) 290return1; 291} 292 293/* 294 * Uninteresting and pruned commits won't be printed 295 */ 296return(commit->object.flags & (UNINTERESTING | TREESAME)) ?0:1; 297} 298 299static struct commit_list *next_interesting_parent(struct git_graph *graph, 300struct commit_list *orig) 301{ 302struct commit_list *list; 303 304/* 305 * If revs->first_parent_only is set, only the first 306 * parent is interesting. None of the others are. 307 */ 308if(graph->revs->first_parent_only) 309return NULL; 310 311/* 312 * Return the next interesting commit after orig 313 */ 314for(list = orig->next; list; list = list->next) { 315if(graph_is_interesting(graph, list->item)) 316return list; 317} 318 319return NULL; 320} 321 322static struct commit_list *first_interesting_parent(struct git_graph *graph) 323{ 324struct commit_list *parents = graph->commit->parents; 325 326/* 327 * If this commit has no parents, ignore it 328 */ 329if(!parents) 330return NULL; 331 332/* 333 * If the first parent is interesting, return it 334 */ 335if(graph_is_interesting(graph, parents->item)) 336return parents; 337 338/* 339 * Otherwise, call next_interesting_parent() to get 340 * the next interesting parent 341 */ 342returnnext_interesting_parent(graph, parents); 343} 344 345static unsigned shortgraph_get_current_column_color(const struct git_graph *graph) 346{ 347if(!DIFF_OPT_TST(&graph->revs->diffopt, COLOR_DIFF)) 348return COLUMN_COLORS_MAX; 349return graph->default_column_color; 350} 351 352/* 353 * Update the graph's default column color. 354 */ 355static voidgraph_increment_column_color(struct git_graph *graph) 356{ 357 graph->default_column_color = (graph->default_column_color +1) % 358 COLUMN_COLORS_MAX; 359} 360 361static unsigned shortgraph_find_commit_color(const struct git_graph *graph, 362const struct commit *commit) 363{ 364int i; 365for(i =0; i < graph->num_columns; i++) { 366if(graph->columns[i].commit == commit) 367return graph->columns[i].color; 368} 369returngraph_get_current_column_color(graph); 370} 371 372static voidgraph_insert_into_new_columns(struct git_graph *graph, 373struct commit *commit, 374int*mapping_index) 375{ 376int i; 377 378/* 379 * If the commit is already in the new_columns list, we don't need to 380 * add it. Just update the mapping correctly. 381 */ 382for(i =0; i < graph->num_new_columns; i++) { 383if(graph->new_columns[i].commit == commit) { 384 graph->mapping[*mapping_index] = i; 385*mapping_index +=2; 386return; 387} 388} 389 390/* 391 * This commit isn't already in new_columns. Add it. 392 */ 393 graph->new_columns[graph->num_new_columns].commit = commit; 394 graph->new_columns[graph->num_new_columns].color =graph_find_commit_color(graph, commit); 395 graph->mapping[*mapping_index] = graph->num_new_columns; 396*mapping_index +=2; 397 graph->num_new_columns++; 398} 399 400static voidgraph_update_width(struct git_graph *graph, 401int is_commit_in_existing_columns) 402{ 403/* 404 * Compute the width needed to display the graph for this commit. 405 * This is the maximum width needed for any row. All other rows 406 * will be padded to this width. 407 * 408 * Compute the number of columns in the widest row: 409 * Count each existing column (graph->num_columns), and each new 410 * column added by this commit. 411 */ 412int max_cols = graph->num_columns + graph->num_parents; 413 414/* 415 * Even if the current commit has no parents to be printed, it 416 * still takes up a column for itself. 417 */ 418if(graph->num_parents <1) 419 max_cols++; 420 421/* 422 * We added a column for the the current commit as part of 423 * graph->num_parents. If the current commit was already in 424 * graph->columns, then we have double counted it. 425 */ 426if(is_commit_in_existing_columns) 427 max_cols--; 428 429/* 430 * Each column takes up 2 spaces 431 */ 432 graph->width = max_cols *2; 433} 434 435static voidgraph_update_columns(struct git_graph *graph) 436{ 437struct commit_list *parent; 438struct column *tmp_columns; 439int max_new_columns; 440int mapping_idx; 441int i, seen_this, is_commit_in_columns; 442 443/* 444 * Swap graph->columns with graph->new_columns 445 * graph->columns contains the state for the previous commit, 446 * and new_columns now contains the state for our commit. 447 * 448 * We'll re-use the old columns array as storage to compute the new 449 * columns list for the commit after this one. 450 */ 451 tmp_columns = graph->columns; 452 graph->columns = graph->new_columns; 453 graph->num_columns = graph->num_new_columns; 454 455 graph->new_columns = tmp_columns; 456 graph->num_new_columns =0; 457 458/* 459 * Now update new_columns and mapping with the information for the 460 * commit after this one. 461 * 462 * First, make sure we have enough room. At most, there will 463 * be graph->num_columns + graph->num_parents columns for the next 464 * commit. 465 */ 466 max_new_columns = graph->num_columns + graph->num_parents; 467graph_ensure_capacity(graph, max_new_columns); 468 469/* 470 * Clear out graph->mapping 471 */ 472 graph->mapping_size =2* max_new_columns; 473for(i =0; i < graph->mapping_size; i++) 474 graph->mapping[i] = -1; 475 476/* 477 * Populate graph->new_columns and graph->mapping 478 * 479 * Some of the parents of this commit may already be in 480 * graph->columns. If so, graph->new_columns should only contain a 481 * single entry for each such commit. graph->mapping should 482 * contain information about where each current branch line is 483 * supposed to end up after the collapsing is performed. 484 */ 485 seen_this =0; 486 mapping_idx =0; 487 is_commit_in_columns =1; 488for(i =0; i <= graph->num_columns; i++) { 489struct commit *col_commit; 490if(i == graph->num_columns) { 491if(seen_this) 492break; 493 is_commit_in_columns =0; 494 col_commit = graph->commit; 495}else{ 496 col_commit = graph->columns[i].commit; 497} 498 499if(col_commit == graph->commit) { 500int old_mapping_idx = mapping_idx; 501 seen_this =1; 502 graph->commit_index = i; 503for(parent =first_interesting_parent(graph); 504 parent; 505 parent =next_interesting_parent(graph, parent)) { 506/* 507 * If this is a merge, or the start of a new 508 * childless column, increment the current 509 * color. 510 */ 511if(graph->num_parents >1|| 512!is_commit_in_columns) { 513graph_increment_column_color(graph); 514} 515graph_insert_into_new_columns(graph, 516 parent->item, 517&mapping_idx); 518} 519/* 520 * We always need to increment mapping_idx by at 521 * least 2, even if it has no interesting parents. 522 * The current commit always takes up at least 2 523 * spaces. 524 */ 525if(mapping_idx == old_mapping_idx) 526 mapping_idx +=2; 527}else{ 528graph_insert_into_new_columns(graph, col_commit, 529&mapping_idx); 530} 531} 532 533/* 534 * Shrink mapping_size to be the minimum necessary 535 */ 536while(graph->mapping_size >1&& 537 graph->mapping[graph->mapping_size -1] <0) 538 graph->mapping_size--; 539 540/* 541 * Compute graph->width for this commit 542 */ 543graph_update_width(graph, is_commit_in_columns); 544} 545 546voidgraph_update(struct git_graph *graph,struct commit *commit) 547{ 548struct commit_list *parent; 549 550/* 551 * Set the new commit 552 */ 553 graph->commit = commit; 554 555/* 556 * Count how many interesting parents this commit has 557 */ 558 graph->num_parents =0; 559for(parent =first_interesting_parent(graph); 560 parent; 561 parent =next_interesting_parent(graph, parent)) 562{ 563 graph->num_parents++; 564} 565 566/* 567 * Store the old commit_index in prev_commit_index. 568 * graph_update_columns() will update graph->commit_index for this 569 * commit. 570 */ 571 graph->prev_commit_index = graph->commit_index; 572 573/* 574 * Call graph_update_columns() to update 575 * columns, new_columns, and mapping. 576 */ 577graph_update_columns(graph); 578 579 graph->expansion_row =0; 580 581/* 582 * Update graph->state. 583 * Note that we don't call graph_update_state() here, since 584 * we don't want to update graph->prev_state. No line for 585 * graph->state was ever printed. 586 * 587 * If the previous commit didn't get to the GRAPH_PADDING state, 588 * it never finished its output. Goto GRAPH_SKIP, to print out 589 * a line to indicate that portion of the graph is missing. 590 * 591 * If there are 3 or more parents, we may need to print extra rows 592 * before the commit, to expand the branch lines around it and make 593 * room for it. We need to do this only if there is a branch row 594 * (or more) to the right of this commit. 595 * 596 * If there are less than 3 parents, we can immediately print the 597 * commit line. 598 */ 599if(graph->state != GRAPH_PADDING) 600 graph->state = GRAPH_SKIP; 601else if(graph->num_parents >=3&& 602 graph->commit_index < (graph->num_columns -1)) 603 graph->state = GRAPH_PRE_COMMIT; 604else 605 graph->state = GRAPH_COMMIT; 606} 607 608static intgraph_is_mapping_correct(struct git_graph *graph) 609{ 610int i; 611 612/* 613 * The mapping is up to date if each entry is at its target, 614 * or is 1 greater than its target. 615 * (If it is 1 greater than the target, '/' will be printed, so it 616 * will look correct on the next row.) 617 */ 618for(i =0; i < graph->mapping_size; i++) { 619int target = graph->mapping[i]; 620if(target <0) 621continue; 622if(target == (i /2)) 623continue; 624return0; 625} 626 627return1; 628} 629 630static voidgraph_pad_horizontally(struct git_graph *graph,struct strbuf *sb, 631int chars_written) 632{ 633/* 634 * Add additional spaces to the end of the strbuf, so that all 635 * lines for a particular commit have the same width. 636 * 637 * This way, fields printed to the right of the graph will remain 638 * aligned for the entire commit. 639 */ 640int extra; 641if(chars_written >= graph->width) 642return; 643 644 extra = graph->width - chars_written; 645strbuf_addf(sb,"%*s", (int) extra,""); 646} 647 648static voidgraph_output_padding_line(struct git_graph *graph, 649struct strbuf *sb) 650{ 651int i; 652 653/* 654 * We could conceivable be called with a NULL commit 655 * if our caller has a bug, and invokes graph_next_line() 656 * immediately after graph_init(), without first calling 657 * graph_update(). Return without outputting anything in this 658 * case. 659 */ 660if(!graph->commit) 661return; 662 663/* 664 * Output a padding row, that leaves all branch lines unchanged 665 */ 666for(i =0; i < graph->num_new_columns; i++) { 667strbuf_write_column(sb, &graph->new_columns[i],'|'); 668strbuf_addch(sb,' '); 669} 670 671graph_pad_horizontally(graph, sb, graph->num_new_columns *2); 672} 673 674static voidgraph_output_skip_line(struct git_graph *graph,struct strbuf *sb) 675{ 676/* 677 * Output an ellipsis to indicate that a portion 678 * of the graph is missing. 679 */ 680strbuf_addstr(sb,"..."); 681graph_pad_horizontally(graph, sb,3); 682 683if(graph->num_parents >=3&& 684 graph->commit_index < (graph->num_columns -1)) 685graph_update_state(graph, GRAPH_PRE_COMMIT); 686else 687graph_update_state(graph, GRAPH_COMMIT); 688} 689 690static voidgraph_output_pre_commit_line(struct git_graph *graph, 691struct strbuf *sb) 692{ 693int num_expansion_rows; 694int i, seen_this; 695int chars_written; 696 697/* 698 * This function formats a row that increases the space around a commit 699 * with multiple parents, to make room for it. It should only be 700 * called when there are 3 or more parents. 701 * 702 * We need 2 extra rows for every parent over 2. 703 */ 704assert(graph->num_parents >=3); 705 num_expansion_rows = (graph->num_parents -2) *2; 706 707/* 708 * graph->expansion_row tracks the current expansion row we are on. 709 * It should be in the range [0, num_expansion_rows - 1] 710 */ 711assert(0<= graph->expansion_row && 712 graph->expansion_row < num_expansion_rows); 713 714/* 715 * Output the row 716 */ 717 seen_this =0; 718 chars_written =0; 719for(i =0; i < graph->num_columns; i++) { 720struct column *col = &graph->columns[i]; 721if(col->commit == graph->commit) { 722 seen_this =1; 723strbuf_write_column(sb, col,'|'); 724strbuf_addf(sb,"%*s", graph->expansion_row,""); 725 chars_written +=1+ graph->expansion_row; 726}else if(seen_this && (graph->expansion_row ==0)) { 727/* 728 * This is the first line of the pre-commit output. 729 * If the previous commit was a merge commit and 730 * ended in the GRAPH_POST_MERGE state, all branch 731 * lines after graph->prev_commit_index were 732 * printed as "\" on the previous line. Continue 733 * to print them as "\" on this line. Otherwise, 734 * print the branch lines as "|". 735 */ 736if(graph->prev_state == GRAPH_POST_MERGE && 737 graph->prev_commit_index < i) 738strbuf_write_column(sb, col,'\\'); 739else 740strbuf_write_column(sb, col,'|'); 741 chars_written++; 742}else if(seen_this && (graph->expansion_row >0)) { 743strbuf_write_column(sb, col,'\\'); 744 chars_written++; 745}else{ 746strbuf_write_column(sb, col,'|'); 747 chars_written++; 748} 749strbuf_addch(sb,' '); 750 chars_written++; 751} 752 753graph_pad_horizontally(graph, sb, chars_written); 754 755/* 756 * Increment graph->expansion_row, 757 * and move to state GRAPH_COMMIT if necessary 758 */ 759 graph->expansion_row++; 760if(graph->expansion_row >= num_expansion_rows) 761graph_update_state(graph, GRAPH_COMMIT); 762} 763 764static voidgraph_output_commit_char(struct git_graph *graph,struct strbuf *sb) 765{ 766/* 767 * For boundary commits, print 'o' 768 * (We should only see boundary commits when revs->boundary is set.) 769 */ 770if(graph->commit->object.flags & BOUNDARY) { 771assert(graph->revs->boundary); 772strbuf_addch(sb,'o'); 773return; 774} 775 776/* 777 * If revs->left_right is set, print '<' for commits that 778 * come from the left side, and '>' for commits from the right 779 * side. 780 */ 781if(graph->revs && graph->revs->left_right) { 782if(graph->commit->object.flags & SYMMETRIC_LEFT) 783strbuf_addch(sb,'<'); 784else 785strbuf_addch(sb,'>'); 786return; 787} 788 789/* 790 * Print '*' in all other cases 791 */ 792strbuf_addch(sb,'*'); 793} 794 795/* 796 * Draw an octopus merge and return the number of characters written. 797 */ 798static intgraph_draw_octopus_merge(struct git_graph *graph, 799struct strbuf *sb) 800{ 801/* 802 * Here dashless_commits represents the number of parents 803 * which don't need to have dashes (because their edges fit 804 * neatly under the commit). 805 */ 806const int dashless_commits =2; 807int col_num, i; 808int num_dashes = 809((graph->num_parents - dashless_commits) *2) -1; 810for(i =0; i < num_dashes; i++) { 811 col_num = (i /2) + dashless_commits; 812strbuf_write_column(sb, &graph->new_columns[col_num],'-'); 813} 814 col_num = (i /2) + dashless_commits; 815strbuf_write_column(sb, &graph->new_columns[col_num],'.'); 816return num_dashes +1; 817} 818 819static voidgraph_output_commit_line(struct git_graph *graph,struct strbuf *sb) 820{ 821int seen_this =0; 822int i, chars_written; 823 824/* 825 * Output the row containing this commit 826 * Iterate up to and including graph->num_columns, 827 * since the current commit may not be in any of the existing 828 * columns. (This happens when the current commit doesn't have any 829 * children that we have already processed.) 830 */ 831 seen_this =0; 832 chars_written =0; 833for(i =0; i <= graph->num_columns; i++) { 834struct column *col = &graph->columns[i]; 835struct commit *col_commit; 836if(i == graph->num_columns) { 837if(seen_this) 838break; 839 col_commit = graph->commit; 840}else{ 841 col_commit = graph->columns[i].commit; 842} 843 844if(col_commit == graph->commit) { 845 seen_this =1; 846graph_output_commit_char(graph, sb); 847 chars_written++; 848 849if(graph->num_parents >2) 850 chars_written +=graph_draw_octopus_merge(graph, 851 sb); 852}else if(seen_this && (graph->num_parents >2)) { 853strbuf_write_column(sb, col,'\\'); 854 chars_written++; 855}else if(seen_this && (graph->num_parents ==2)) { 856/* 857 * This is a 2-way merge commit. 858 * There is no GRAPH_PRE_COMMIT stage for 2-way 859 * merges, so this is the first line of output 860 * for this commit. Check to see what the previous 861 * line of output was. 862 * 863 * If it was GRAPH_POST_MERGE, the branch line 864 * coming into this commit may have been '\', 865 * and not '|' or '/'. If so, output the branch 866 * line as '\' on this line, instead of '|'. This 867 * makes the output look nicer. 868 */ 869if(graph->prev_state == GRAPH_POST_MERGE && 870 graph->prev_commit_index < i) 871strbuf_write_column(sb, col,'\\'); 872else 873strbuf_write_column(sb, col,'|'); 874 chars_written++; 875}else{ 876strbuf_write_column(sb, col,'|'); 877 chars_written++; 878} 879strbuf_addch(sb,' '); 880 chars_written++; 881} 882 883graph_pad_horizontally(graph, sb, chars_written); 884 885/* 886 * Update graph->state 887 */ 888if(graph->num_parents >1) 889graph_update_state(graph, GRAPH_POST_MERGE); 890else if(graph_is_mapping_correct(graph)) 891graph_update_state(graph, GRAPH_PADDING); 892else 893graph_update_state(graph, GRAPH_COLLAPSING); 894} 895 896static struct column *find_new_column_by_commit(struct git_graph *graph, 897struct commit *commit) 898{ 899int i; 900for(i =0; i < graph->num_new_columns; i++) { 901if(graph->new_columns[i].commit == commit) 902return&graph->new_columns[i]; 903} 904return NULL; 905} 906 907static voidgraph_output_post_merge_line(struct git_graph *graph,struct strbuf *sb) 908{ 909int seen_this =0; 910int i, j, chars_written; 911 912/* 913 * Output the post-merge row 914 */ 915 chars_written =0; 916for(i =0; i <= graph->num_columns; i++) { 917struct column *col = &graph->columns[i]; 918struct commit *col_commit; 919if(i == graph->num_columns) { 920if(seen_this) 921break; 922 col_commit = graph->commit; 923}else{ 924 col_commit = col->commit; 925} 926 927if(col_commit == graph->commit) { 928/* 929 * Since the current commit is a merge find 930 * the columns for the parent commits in 931 * new_columns and use those to format the 932 * edges. 933 */ 934struct commit_list *parents = NULL; 935struct column *par_column; 936 seen_this =1; 937 parents =first_interesting_parent(graph); 938assert(parents); 939 par_column =find_new_column_by_commit(graph, parents->item); 940assert(par_column); 941 942strbuf_write_column(sb, par_column,'|'); 943 chars_written++; 944for(j =0; j < graph->num_parents -1; j++) { 945 parents =next_interesting_parent(graph, parents); 946assert(parents); 947 par_column =find_new_column_by_commit(graph, parents->item); 948assert(par_column); 949strbuf_write_column(sb, par_column,'\\'); 950strbuf_addch(sb,' '); 951} 952 chars_written += j *2; 953}else if(seen_this) { 954strbuf_write_column(sb, col,'\\'); 955strbuf_addch(sb,' '); 956 chars_written +=2; 957}else{ 958strbuf_write_column(sb, col,'|'); 959strbuf_addch(sb,' '); 960 chars_written +=2; 961} 962} 963 964graph_pad_horizontally(graph, sb, chars_written); 965 966/* 967 * Update graph->state 968 */ 969if(graph_is_mapping_correct(graph)) 970graph_update_state(graph, GRAPH_PADDING); 971else 972graph_update_state(graph, GRAPH_COLLAPSING); 973} 974 975static voidgraph_output_collapsing_line(struct git_graph *graph,struct strbuf *sb) 976{ 977int i; 978int*tmp_mapping; 979short used_horizontal =0; 980int horizontal_edge = -1; 981int horizontal_edge_target = -1; 982 983/* 984 * Clear out the new_mapping array 985 */ 986for(i =0; i < graph->mapping_size; i++) 987 graph->new_mapping[i] = -1; 988 989for(i =0; i < graph->mapping_size; i++) { 990int target = graph->mapping[i]; 991if(target <0) 992continue; 993 994/* 995 * Since update_columns() always inserts the leftmost 996 * column first, each branch's target location should 997 * always be either its current location or to the left of 998 * its current location. 999 *1000 * We never have to move branches to the right. This makes1001 * the graph much more legible, since whenever branches1002 * cross, only one is moving directions.1003 */1004assert(target *2<= i);10051006if(target *2== i) {1007/*1008 * This column is already in the1009 * correct place1010 */1011assert(graph->new_mapping[i] == -1);1012 graph->new_mapping[i] = target;1013}else if(graph->new_mapping[i -1] <0) {1014/*1015 * Nothing is to the left.1016 * Move to the left by one1017 */1018 graph->new_mapping[i -1] = target;1019/*1020 * If there isn't already an edge moving horizontally1021 * select this one.1022 */1023if(horizontal_edge == -1) {1024int j;1025 horizontal_edge = i;1026 horizontal_edge_target = target;1027/*1028 * The variable target is the index of the graph1029 * column, and therefore target*2+3 is the1030 * actual screen column of the first horizontal1031 * line.1032 */1033for(j = (target *2)+3; j < (i -2); j +=2)1034 graph->new_mapping[j] = target;1035}1036}else if(graph->new_mapping[i -1] == target) {1037/*1038 * There is a branch line to our left1039 * already, and it is our target. We1040 * combine with this line, since we share1041 * the same parent commit.1042 *1043 * We don't have to add anything to the1044 * output or new_mapping, since the1045 * existing branch line has already taken1046 * care of it.1047 */1048}else{1049/*1050 * There is a branch line to our left,1051 * but it isn't our target. We need to1052 * cross over it.1053 *1054 * The space just to the left of this1055 * branch should always be empty.1056 *1057 * The branch to the left of that space1058 * should be our eventual target.1059 */1060assert(graph->new_mapping[i -1] > target);1061assert(graph->new_mapping[i -2] <0);1062assert(graph->new_mapping[i -3] == target);1063 graph->new_mapping[i -2] = target;1064/*1065 * Mark this branch as the horizontal edge to1066 * prevent any other edges from moving1067 * horizontally.1068 */1069if(horizontal_edge == -1)1070 horizontal_edge = i;1071}1072}10731074/*1075 * The new mapping may be 1 smaller than the old mapping1076 */1077if(graph->new_mapping[graph->mapping_size -1] <0)1078 graph->mapping_size--;10791080/*1081 * Output out a line based on the new mapping info1082 */1083for(i =0; i < graph->mapping_size; i++) {1084int target = graph->new_mapping[i];1085if(target <0)1086strbuf_addch(sb,' ');1087else if(target *2== i)1088strbuf_write_column(sb, &graph->new_columns[target],'|');1089else if(target == horizontal_edge_target &&1090 i != horizontal_edge -1) {1091/*1092 * Set the mappings for all but the1093 * first segment to -1 so that they1094 * won't continue into the next line.1095 */1096if(i != (target *2)+3)1097 graph->new_mapping[i] = -1;1098 used_horizontal =1;1099strbuf_write_column(sb, &graph->new_columns[target],'_');1100}else{1101if(used_horizontal && i < horizontal_edge)1102 graph->new_mapping[i] = -1;1103strbuf_write_column(sb, &graph->new_columns[target],'/');11041105}1106}11071108graph_pad_horizontally(graph, sb, graph->mapping_size);11091110/*1111 * Swap mapping and new_mapping1112 */1113 tmp_mapping = graph->mapping;1114 graph->mapping = graph->new_mapping;1115 graph->new_mapping = tmp_mapping;11161117/*1118 * If graph->mapping indicates that all of the branch lines1119 * are already in the correct positions, we are done.1120 * Otherwise, we need to collapse some branch lines together.1121 */1122if(graph_is_mapping_correct(graph))1123graph_update_state(graph, GRAPH_PADDING);1124}11251126static intgraph_next_line(struct git_graph *graph,struct strbuf *sb)1127{1128switch(graph->state) {1129case GRAPH_PADDING:1130graph_output_padding_line(graph, sb);1131return0;1132case GRAPH_SKIP:1133graph_output_skip_line(graph, sb);1134return0;1135case GRAPH_PRE_COMMIT:1136graph_output_pre_commit_line(graph, sb);1137return0;1138case GRAPH_COMMIT:1139graph_output_commit_line(graph, sb);1140return1;1141case GRAPH_POST_MERGE:1142graph_output_post_merge_line(graph, sb);1143return0;1144case GRAPH_COLLAPSING:1145graph_output_collapsing_line(graph, sb);1146return0;1147}11481149assert(0);1150return0;1151}11521153static voidgraph_padding_line(struct git_graph *graph,struct strbuf *sb)1154{1155int i, j;11561157if(graph->state != GRAPH_COMMIT) {1158graph_next_line(graph, sb);1159return;1160}11611162/*1163 * Output the row containing this commit1164 * Iterate up to and including graph->num_columns,1165 * since the current commit may not be in any of the existing1166 * columns. (This happens when the current commit doesn't have any1167 * children that we have already processed.)1168 */1169for(i =0; i < graph->num_columns; i++) {1170struct column *col = &graph->columns[i];1171struct commit *col_commit = col->commit;1172if(col_commit == graph->commit) {1173strbuf_write_column(sb, col,'|');11741175if(graph->num_parents <3)1176strbuf_addch(sb,' ');1177else{1178int num_spaces = ((graph->num_parents -2) *2);1179for(j =0; j < num_spaces; j++)1180strbuf_addch(sb,' ');1181}1182}else{1183strbuf_write_column(sb, col,'|');1184strbuf_addch(sb,' ');1185}1186}11871188graph_pad_horizontally(graph, sb, graph->num_columns);11891190/*1191 * Update graph->prev_state since we have output a padding line1192 */1193 graph->prev_state = GRAPH_PADDING;1194}11951196intgraph_is_commit_finished(struct git_graph const*graph)1197{1198return(graph->state == GRAPH_PADDING);1199}12001201voidgraph_show_commit(struct git_graph *graph)1202{1203struct strbuf msgbuf = STRBUF_INIT;1204int shown_commit_line =0;12051206if(!graph)1207return;12081209while(!shown_commit_line) {1210 shown_commit_line =graph_next_line(graph, &msgbuf);1211fwrite(msgbuf.buf,sizeof(char), msgbuf.len, stdout);1212if(!shown_commit_line)1213putchar('\n');1214strbuf_setlen(&msgbuf,0);1215}12161217strbuf_release(&msgbuf);1218}12191220voidgraph_show_oneline(struct git_graph *graph)1221{1222struct strbuf msgbuf = STRBUF_INIT;12231224if(!graph)1225return;12261227graph_next_line(graph, &msgbuf);1228fwrite(msgbuf.buf,sizeof(char), msgbuf.len, stdout);1229strbuf_release(&msgbuf);1230}12311232voidgraph_show_padding(struct git_graph *graph)1233{1234struct strbuf msgbuf = STRBUF_INIT;12351236if(!graph)1237return;12381239graph_padding_line(graph, &msgbuf);1240fwrite(msgbuf.buf,sizeof(char), msgbuf.len, stdout);1241strbuf_release(&msgbuf);1242}12431244intgraph_show_remainder(struct git_graph *graph)1245{1246struct strbuf msgbuf = STRBUF_INIT;1247int shown =0;12481249if(!graph)1250return0;12511252if(graph_is_commit_finished(graph))1253return0;12541255for(;;) {1256graph_next_line(graph, &msgbuf);1257fwrite(msgbuf.buf,sizeof(char), msgbuf.len, stdout);1258strbuf_setlen(&msgbuf,0);1259 shown =1;12601261if(!graph_is_commit_finished(graph))1262putchar('\n');1263else1264break;1265}1266strbuf_release(&msgbuf);12671268return shown;1269}127012711272static voidgraph_show_strbuf(struct git_graph *graph,struct strbuf const*sb)1273{1274char*p;12751276if(!graph) {1277fwrite(sb->buf,sizeof(char), sb->len, stdout);1278return;1279}12801281/*1282 * Print the strbuf line by line,1283 * and display the graph info before each line but the first.1284 */1285 p = sb->buf;1286while(p) {1287size_t len;1288char*next_p =strchr(p,'\n');1289if(next_p) {1290 next_p++;1291 len = next_p - p;1292}else{1293 len = (sb->buf + sb->len) - p;1294}1295fwrite(p,sizeof(char), len, stdout);1296if(next_p && *next_p !='\0')1297graph_show_oneline(graph);1298 p = next_p;1299}1300}13011302voidgraph_show_commit_msg(struct git_graph *graph,1303struct strbuf const*sb)1304{1305int newline_terminated;13061307if(!graph) {1308/*1309 * If there's no graph, just print the message buffer.1310 *1311 * The message buffer for CMIT_FMT_ONELINE and1312 * CMIT_FMT_USERFORMAT are already missing a terminating1313 * newline. All of the other formats should have it.1314 */1315fwrite(sb->buf,sizeof(char), sb->len, stdout);1316return;1317}13181319 newline_terminated = (sb->len && sb->buf[sb->len -1] =='\n');13201321/*1322 * Show the commit message1323 */1324graph_show_strbuf(graph, sb);13251326/*1327 * If there is more output needed for this commit, show it now1328 */1329if(!graph_is_commit_finished(graph)) {1330/*1331 * If sb doesn't have a terminating newline, print one now,1332 * so we can start the remainder of the graph output on a1333 * new line.1334 */1335if(!newline_terminated)1336putchar('\n');13371338graph_show_remainder(graph);13391340/*1341 * If sb ends with a newline, our output should too.1342 */1343if(newline_terminated)1344putchar('\n');1345}1346}