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