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