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