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