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 to stdout. If the graph is non-NULL, all lines but the 21 * first will be 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(graph); 198 199strbuf_reset(&msgbuf); 200graph_padding_line(graph, &msgbuf); 201return&msgbuf; 202} 203 204struct git_graph *graph_init(struct rev_info *opt) 205{ 206struct git_graph *graph =xmalloc(sizeof(struct git_graph)); 207 208if(!column_colors) 209graph_set_column_colors(column_colors_ansi, 210 column_colors_ansi_max); 211 212 graph->commit = NULL; 213 graph->revs = opt; 214 graph->num_parents =0; 215 graph->expansion_row =0; 216 graph->state = GRAPH_PADDING; 217 graph->prev_state = GRAPH_PADDING; 218 graph->commit_index =0; 219 graph->prev_commit_index =0; 220 graph->num_columns =0; 221 graph->num_new_columns =0; 222 graph->mapping_size =0; 223/* 224 * Start the column color at the maximum value, since we'll 225 * always increment it for the first commit we output. 226 * This way we start at 0 for the first commit. 227 */ 228 graph->default_column_color = column_colors_max -1; 229 230/* 231 * Allocate a reasonably large default number of columns 232 * We'll automatically grow columns later if we need more room. 233 */ 234 graph->column_capacity =30; 235 graph->columns =xmalloc(sizeof(struct column) * 236 graph->column_capacity); 237 graph->new_columns =xmalloc(sizeof(struct column) * 238 graph->column_capacity); 239 graph->mapping =xmalloc(sizeof(int) *2* graph->column_capacity); 240 graph->new_mapping =xmalloc(sizeof(int) *2* graph->column_capacity); 241 242/* 243 * The diff output prefix callback, with this we can make 244 * all the diff output to align with the graph lines. 245 */ 246 opt->diffopt.output_prefix = diff_output_prefix_callback; 247 opt->diffopt.output_prefix_data = graph; 248 249return graph; 250} 251 252static voidgraph_update_state(struct git_graph *graph,enum graph_state s) 253{ 254 graph->prev_state = graph->state; 255 graph->state = s; 256} 257 258static voidgraph_ensure_capacity(struct git_graph *graph,int num_columns) 259{ 260if(graph->column_capacity >= num_columns) 261return; 262 263do{ 264 graph->column_capacity *=2; 265}while(graph->column_capacity < num_columns); 266 267 graph->columns =xrealloc(graph->columns, 268sizeof(struct column) * 269 graph->column_capacity); 270 graph->new_columns =xrealloc(graph->new_columns, 271sizeof(struct column) * 272 graph->column_capacity); 273 graph->mapping =xrealloc(graph->mapping, 274sizeof(int) *2* graph->column_capacity); 275 graph->new_mapping =xrealloc(graph->new_mapping, 276sizeof(int) *2* graph->column_capacity); 277} 278 279/* 280 * Returns 1 if the commit will be printed in the graph output, 281 * and 0 otherwise. 282 */ 283static intgraph_is_interesting(struct git_graph *graph,struct commit *commit) 284{ 285/* 286 * If revs->boundary is set, commits whose children have 287 * been shown are always interesting, even if they have the 288 * UNINTERESTING or TREESAME flags set. 289 */ 290if(graph->revs && graph->revs->boundary) { 291if(commit->object.flags & CHILD_SHOWN) 292return1; 293} 294 295/* 296 * Otherwise, use get_commit_action() to see if this commit is 297 * interesting 298 */ 299returnget_commit_action(graph->revs, commit) == commit_show; 300} 301 302static struct commit_list *next_interesting_parent(struct git_graph *graph, 303struct commit_list *orig) 304{ 305struct commit_list *list; 306 307/* 308 * If revs->first_parent_only is set, only the first 309 * parent is interesting. None of the others are. 310 */ 311if(graph->revs->first_parent_only) 312return NULL; 313 314/* 315 * Return the next interesting commit after orig 316 */ 317for(list = orig->next; list; list = list->next) { 318if(graph_is_interesting(graph, list->item)) 319return list; 320} 321 322return NULL; 323} 324 325static struct commit_list *first_interesting_parent(struct git_graph *graph) 326{ 327struct commit_list *parents = graph->commit->parents; 328 329/* 330 * If this commit has no parents, ignore it 331 */ 332if(!parents) 333return NULL; 334 335/* 336 * If the first parent is interesting, return it 337 */ 338if(graph_is_interesting(graph, parents->item)) 339return parents; 340 341/* 342 * Otherwise, call next_interesting_parent() to get 343 * the next interesting parent 344 */ 345returnnext_interesting_parent(graph, parents); 346} 347 348static unsigned shortgraph_get_current_column_color(const struct git_graph *graph) 349{ 350if(!DIFF_OPT_TST(&graph->revs->diffopt, COLOR_DIFF)) 351return column_colors_max; 352return graph->default_column_color; 353} 354 355/* 356 * Update the graph's default column color. 357 */ 358static voidgraph_increment_column_color(struct git_graph *graph) 359{ 360 graph->default_column_color = (graph->default_column_color +1) % 361 column_colors_max; 362} 363 364static unsigned shortgraph_find_commit_color(const struct git_graph *graph, 365const struct commit *commit) 366{ 367int i; 368for(i =0; i < graph->num_columns; i++) { 369if(graph->columns[i].commit == commit) 370return graph->columns[i].color; 371} 372returngraph_get_current_column_color(graph); 373} 374 375static voidgraph_insert_into_new_columns(struct git_graph *graph, 376struct commit *commit, 377int*mapping_index) 378{ 379int i; 380 381/* 382 * If the commit is already in the new_columns list, we don't need to 383 * add it. Just update the mapping correctly. 384 */ 385for(i =0; i < graph->num_new_columns; i++) { 386if(graph->new_columns[i].commit == commit) { 387 graph->mapping[*mapping_index] = i; 388*mapping_index +=2; 389return; 390} 391} 392 393/* 394 * This commit isn't already in new_columns. Add it. 395 */ 396 graph->new_columns[graph->num_new_columns].commit = commit; 397 graph->new_columns[graph->num_new_columns].color =graph_find_commit_color(graph, commit); 398 graph->mapping[*mapping_index] = graph->num_new_columns; 399*mapping_index +=2; 400 graph->num_new_columns++; 401} 402 403static voidgraph_update_width(struct git_graph *graph, 404int is_commit_in_existing_columns) 405{ 406/* 407 * Compute the width needed to display the graph for this commit. 408 * This is the maximum width needed for any row. All other rows 409 * will be padded to this width. 410 * 411 * Compute the number of columns in the widest row: 412 * Count each existing column (graph->num_columns), and each new 413 * column added by this commit. 414 */ 415int max_cols = graph->num_columns + graph->num_parents; 416 417/* 418 * Even if the current commit has no parents to be printed, it 419 * still takes up a column for itself. 420 */ 421if(graph->num_parents <1) 422 max_cols++; 423 424/* 425 * We added a column for the current commit as part of 426 * graph->num_parents. If the current commit was already in 427 * graph->columns, then we have double counted it. 428 */ 429if(is_commit_in_existing_columns) 430 max_cols--; 431 432/* 433 * Each column takes up 2 spaces 434 */ 435 graph->width = max_cols *2; 436} 437 438static voidgraph_update_columns(struct git_graph *graph) 439{ 440struct commit_list *parent; 441struct column *tmp_columns; 442int max_new_columns; 443int mapping_idx; 444int i, seen_this, is_commit_in_columns; 445 446/* 447 * Swap graph->columns with graph->new_columns 448 * graph->columns contains the state for the previous commit, 449 * and new_columns now contains the state for our commit. 450 * 451 * We'll re-use the old columns array as storage to compute the new 452 * columns list for the commit after this one. 453 */ 454 tmp_columns = graph->columns; 455 graph->columns = graph->new_columns; 456 graph->num_columns = graph->num_new_columns; 457 458 graph->new_columns = tmp_columns; 459 graph->num_new_columns =0; 460 461/* 462 * Now update new_columns and mapping with the information for the 463 * commit after this one. 464 * 465 * First, make sure we have enough room. At most, there will 466 * be graph->num_columns + graph->num_parents columns for the next 467 * commit. 468 */ 469 max_new_columns = graph->num_columns + graph->num_parents; 470graph_ensure_capacity(graph, max_new_columns); 471 472/* 473 * Clear out graph->mapping 474 */ 475 graph->mapping_size =2* max_new_columns; 476for(i =0; i < graph->mapping_size; i++) 477 graph->mapping[i] = -1; 478 479/* 480 * Populate graph->new_columns and graph->mapping 481 * 482 * Some of the parents of this commit may already be in 483 * graph->columns. If so, graph->new_columns should only contain a 484 * single entry for each such commit. graph->mapping should 485 * contain information about where each current branch line is 486 * supposed to end up after the collapsing is performed. 487 */ 488 seen_this =0; 489 mapping_idx =0; 490 is_commit_in_columns =1; 491for(i =0; i <= graph->num_columns; i++) { 492struct commit *col_commit; 493if(i == graph->num_columns) { 494if(seen_this) 495break; 496 is_commit_in_columns =0; 497 col_commit = graph->commit; 498}else{ 499 col_commit = graph->columns[i].commit; 500} 501 502if(col_commit == graph->commit) { 503int old_mapping_idx = mapping_idx; 504 seen_this =1; 505 graph->commit_index = i; 506for(parent =first_interesting_parent(graph); 507 parent; 508 parent =next_interesting_parent(graph, parent)) { 509/* 510 * If this is a merge, or the start of a new 511 * childless column, increment the current 512 * color. 513 */ 514if(graph->num_parents >1|| 515!is_commit_in_columns) { 516graph_increment_column_color(graph); 517} 518graph_insert_into_new_columns(graph, 519 parent->item, 520&mapping_idx); 521} 522/* 523 * We always need to increment mapping_idx by at 524 * least 2, even if it has no interesting parents. 525 * The current commit always takes up at least 2 526 * spaces. 527 */ 528if(mapping_idx == old_mapping_idx) 529 mapping_idx +=2; 530}else{ 531graph_insert_into_new_columns(graph, col_commit, 532&mapping_idx); 533} 534} 535 536/* 537 * Shrink mapping_size to be the minimum necessary 538 */ 539while(graph->mapping_size >1&& 540 graph->mapping[graph->mapping_size -1] <0) 541 graph->mapping_size--; 542 543/* 544 * Compute graph->width for this commit 545 */ 546graph_update_width(graph, is_commit_in_columns); 547} 548 549voidgraph_update(struct git_graph *graph,struct commit *commit) 550{ 551struct commit_list *parent; 552 553/* 554 * Set the new commit 555 */ 556 graph->commit = commit; 557 558/* 559 * Count how many interesting parents this commit has 560 */ 561 graph->num_parents =0; 562for(parent =first_interesting_parent(graph); 563 parent; 564 parent =next_interesting_parent(graph, parent)) 565{ 566 graph->num_parents++; 567} 568 569/* 570 * Store the old commit_index in prev_commit_index. 571 * graph_update_columns() will update graph->commit_index for this 572 * commit. 573 */ 574 graph->prev_commit_index = graph->commit_index; 575 576/* 577 * Call graph_update_columns() to update 578 * columns, new_columns, and mapping. 579 */ 580graph_update_columns(graph); 581 582 graph->expansion_row =0; 583 584/* 585 * Update graph->state. 586 * Note that we don't call graph_update_state() here, since 587 * we don't want to update graph->prev_state. No line for 588 * graph->state was ever printed. 589 * 590 * If the previous commit didn't get to the GRAPH_PADDING state, 591 * it never finished its output. Goto GRAPH_SKIP, to print out 592 * a line to indicate that portion of the graph is missing. 593 * 594 * If there are 3 or more parents, we may need to print extra rows 595 * before the commit, to expand the branch lines around it and make 596 * room for it. We need to do this only if there is a branch row 597 * (or more) to the right of this commit. 598 * 599 * If there are less than 3 parents, we can immediately print the 600 * commit line. 601 */ 602if(graph->state != GRAPH_PADDING) 603 graph->state = GRAPH_SKIP; 604else if(graph->num_parents >=3&& 605 graph->commit_index < (graph->num_columns -1)) 606 graph->state = GRAPH_PRE_COMMIT; 607else 608 graph->state = GRAPH_COMMIT; 609} 610 611static intgraph_is_mapping_correct(struct git_graph *graph) 612{ 613int i; 614 615/* 616 * The mapping is up to date if each entry is at its target, 617 * or is 1 greater than its target. 618 * (If it is 1 greater than the target, '/' will be printed, so it 619 * will look correct on the next row.) 620 */ 621for(i =0; i < graph->mapping_size; i++) { 622int target = graph->mapping[i]; 623if(target <0) 624continue; 625if(target == (i /2)) 626continue; 627return0; 628} 629 630return1; 631} 632 633static voidgraph_pad_horizontally(struct git_graph *graph,struct strbuf *sb, 634int chars_written) 635{ 636/* 637 * Add additional spaces to the end of the strbuf, so that all 638 * lines for a particular commit have the same width. 639 * 640 * This way, fields printed to the right of the graph will remain 641 * aligned for the entire commit. 642 */ 643int extra; 644if(chars_written >= graph->width) 645return; 646 647 extra = graph->width - chars_written; 648strbuf_addf(sb,"%*s", (int) extra,""); 649} 650 651static voidgraph_output_padding_line(struct git_graph *graph, 652struct strbuf *sb) 653{ 654int i; 655 656/* 657 * We could conceivable be called with a NULL commit 658 * if our caller has a bug, and invokes graph_next_line() 659 * immediately after graph_init(), without first calling 660 * graph_update(). Return without outputting anything in this 661 * case. 662 */ 663if(!graph->commit) 664return; 665 666/* 667 * Output a padding row, that leaves all branch lines unchanged 668 */ 669for(i =0; i < graph->num_new_columns; i++) { 670strbuf_write_column(sb, &graph->new_columns[i],'|'); 671strbuf_addch(sb,' '); 672} 673 674graph_pad_horizontally(graph, sb, graph->num_new_columns *2); 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; 802strbuf_write_column(sb, &graph->new_columns[col_num],'-'); 803} 804 col_num = (i /2) + dashless_commits; 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, j;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];1161struct commit *col_commit = col->commit;1162if(col_commit == graph->commit) {1163strbuf_write_column(sb, col,'|');11641165if(graph->num_parents <3)1166strbuf_addch(sb,' ');1167else{1168int num_spaces = ((graph->num_parents -2) *2);1169for(j =0; j < num_spaces; j++)1170strbuf_addch(sb,' ');1171}1172}else{1173strbuf_write_column(sb, col,'|');1174strbuf_addch(sb,' ');1175}1176}11771178graph_pad_horizontally(graph, sb, graph->num_columns);11791180/*1181 * Update graph->prev_state since we have output a padding line1182 */1183 graph->prev_state = GRAPH_PADDING;1184}11851186intgraph_is_commit_finished(struct git_graph const*graph)1187{1188return(graph->state == GRAPH_PADDING);1189}11901191voidgraph_show_commit(struct git_graph *graph)1192{1193struct strbuf msgbuf = STRBUF_INIT;1194int shown_commit_line =0;11951196if(!graph)1197return;11981199while(!shown_commit_line) {1200 shown_commit_line =graph_next_line(graph, &msgbuf);1201fwrite(msgbuf.buf,sizeof(char), msgbuf.len, stdout);1202if(!shown_commit_line)1203putchar('\n');1204strbuf_setlen(&msgbuf,0);1205}12061207strbuf_release(&msgbuf);1208}12091210voidgraph_show_oneline(struct git_graph *graph)1211{1212struct strbuf msgbuf = STRBUF_INIT;12131214if(!graph)1215return;12161217graph_next_line(graph, &msgbuf);1218fwrite(msgbuf.buf,sizeof(char), msgbuf.len, stdout);1219strbuf_release(&msgbuf);1220}12211222voidgraph_show_padding(struct git_graph *graph)1223{1224struct strbuf msgbuf = STRBUF_INIT;12251226if(!graph)1227return;12281229graph_padding_line(graph, &msgbuf);1230fwrite(msgbuf.buf,sizeof(char), msgbuf.len, stdout);1231strbuf_release(&msgbuf);1232}12331234intgraph_show_remainder(struct git_graph *graph)1235{1236struct strbuf msgbuf = STRBUF_INIT;1237int shown =0;12381239if(!graph)1240return0;12411242if(graph_is_commit_finished(graph))1243return0;12441245for(;;) {1246graph_next_line(graph, &msgbuf);1247fwrite(msgbuf.buf,sizeof(char), msgbuf.len, stdout);1248strbuf_setlen(&msgbuf,0);1249 shown =1;12501251if(!graph_is_commit_finished(graph))1252putchar('\n');1253else1254break;1255}1256strbuf_release(&msgbuf);12571258return shown;1259}126012611262static voidgraph_show_strbuf(struct git_graph *graph,struct strbuf const*sb)1263{1264char*p;12651266if(!graph) {1267fwrite(sb->buf,sizeof(char), sb->len, stdout);1268return;1269}12701271/*1272 * Print the strbuf line by line,1273 * and display the graph info before each line but the first.1274 */1275 p = sb->buf;1276while(p) {1277size_t len;1278char*next_p =strchr(p,'\n');1279if(next_p) {1280 next_p++;1281 len = next_p - p;1282}else{1283 len = (sb->buf + sb->len) - p;1284}1285fwrite(p,sizeof(char), len, stdout);1286if(next_p && *next_p !='\0')1287graph_show_oneline(graph);1288 p = next_p;1289}1290}12911292voidgraph_show_commit_msg(struct git_graph *graph,1293struct strbuf const*sb)1294{1295int newline_terminated;12961297if(!graph) {1298/*1299 * If there's no graph, just print the message buffer.1300 *1301 * The message buffer for CMIT_FMT_ONELINE and1302 * CMIT_FMT_USERFORMAT are already missing a terminating1303 * newline. All of the other formats should have it.1304 */1305fwrite(sb->buf,sizeof(char), sb->len, stdout);1306return;1307}13081309 newline_terminated = (sb->len && sb->buf[sb->len -1] =='\n');13101311/*1312 * Show the commit message1313 */1314graph_show_strbuf(graph, sb);13151316/*1317 * If there is more output needed for this commit, show it now1318 */1319if(!graph_is_commit_finished(graph)) {1320/*1321 * If sb doesn't have a terminating newline, print one now,1322 * so we can start the remainder of the graph output on a1323 * new line.1324 */1325if(!newline_terminated)1326putchar('\n');13271328graph_show_remainder(graph);13291330/*1331 * If sb ends with a newline, our output should too.1332 */1333if(newline_terminated)1334putchar('\n');1335}1336}