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