1#include"cache.h" 2#include"config.h" 3#include"commit.h" 4#include"color.h" 5#include"graph.h" 6#include"revision.h" 7#include"argv-array.h" 8 9/* Internal API */ 10 11/* 12 * Output a padding line in the graph. 13 * This is similar to graph_next_line(). However, it is guaranteed to 14 * never print the current commit line. Instead, if the commit line is 15 * next, it will simply output a line of vertical padding, extending the 16 * branch lines downwards, but leaving them otherwise unchanged. 17 */ 18static voidgraph_padding_line(struct git_graph *graph,struct strbuf *sb); 19 20/* 21 * Print a strbuf. If the graph is non-NULL, all lines but the first will be 22 * prefixed with the graph output. 23 * 24 * If the strbuf ends with a newline, the output will end after this 25 * newline. A new graph line will not be printed after the final newline. 26 * If the strbuf is empty, no output will be printed. 27 * 28 * Since the first line will not include the graph output, the caller is 29 * responsible for printing this line's graph (perhaps via 30 * graph_show_commit() or graph_show_oneline()) before calling 31 * graph_show_strbuf(). 32 * 33 * Note that unlike some other graph display functions, you must pass the file 34 * handle directly. It is assumed that this is the same file handle as the 35 * file specified by the graph diff options. This is necessary so that 36 * graph_show_strbuf can be called even with a NULL graph. 37 */ 38static voidgraph_show_strbuf(struct git_graph *graph, 39FILE*file, 40struct strbuf const*sb); 41 42/* 43 * TODO: 44 * - Limit the number of columns, similar to the way gitk does. 45 * If we reach more than a specified number of columns, omit 46 * sections of some columns. 47 */ 48 49struct column { 50/* 51 * The parent commit of this column. 52 */ 53struct commit *commit; 54/* 55 * The color to (optionally) print this column in. This is an 56 * index into column_colors. 57 */ 58unsigned short color; 59}; 60 61enum graph_state { 62 GRAPH_PADDING, 63 GRAPH_SKIP, 64 GRAPH_PRE_COMMIT, 65 GRAPH_COMMIT, 66 GRAPH_POST_MERGE, 67 GRAPH_COLLAPSING 68}; 69 70static voidgraph_show_line_prefix(const struct diff_options *diffopt) 71{ 72if(!diffopt || !diffopt->line_prefix) 73return; 74 75fwrite(diffopt->line_prefix, 76sizeof(char), 77 diffopt->line_prefix_length, 78 diffopt->file); 79} 80 81static const char**column_colors; 82static unsigned short column_colors_max; 83 84static voidparse_graph_colors_config(struct argv_array *colors,const char*string) 85{ 86const char*end, *start; 87 88 start = string; 89 end = string +strlen(string); 90while(start < end) { 91const char*comma =strchrnul(start,','); 92char color[COLOR_MAXLEN]; 93 94if(!color_parse_mem(start, comma - start, color)) 95argv_array_push(colors, color); 96else 97warning(_("ignore invalid color '%.*s' in log.graphColors"), 98(int)(comma - start), start); 99 start = comma +1; 100} 101argv_array_push(colors, GIT_COLOR_RESET); 102} 103 104voidgraph_set_column_colors(const char**colors,unsigned short colors_max) 105{ 106 column_colors = colors; 107 column_colors_max = colors_max; 108} 109 110static const char*column_get_color_code(unsigned short color) 111{ 112return column_colors[color]; 113} 114 115static voidstrbuf_write_column(struct strbuf *sb,const struct column *c, 116char col_char) 117{ 118if(c->color < column_colors_max) 119strbuf_addstr(sb,column_get_color_code(c->color)); 120strbuf_addch(sb, col_char); 121if(c->color < column_colors_max) 122strbuf_addstr(sb,column_get_color_code(column_colors_max)); 123} 124 125struct git_graph { 126/* 127 * The commit currently being processed 128 */ 129struct commit *commit; 130/* The rev-info used for the current traversal */ 131struct rev_info *revs; 132/* 133 * The number of interesting parents that this commit has. 134 * 135 * Note that this is not the same as the actual number of parents. 136 * This count excludes parents that won't be printed in the graph 137 * output, as determined by graph_is_interesting(). 138 */ 139int num_parents; 140/* 141 * The width of the graph output for this commit. 142 * All rows for this commit are padded to this width, so that 143 * messages printed after the graph output are aligned. 144 */ 145int width; 146/* 147 * The next expansion row to print 148 * when state is GRAPH_PRE_COMMIT 149 */ 150int expansion_row; 151/* 152 * The current output state. 153 * This tells us what kind of line graph_next_line() should output. 154 */ 155enum graph_state state; 156/* 157 * The output state for the previous line of output. 158 * This is primarily used to determine how the first merge line 159 * should appear, based on the last line of the previous commit. 160 */ 161enum graph_state prev_state; 162/* 163 * The index of the column that refers to this commit. 164 * 165 * If none of the incoming columns refer to this commit, 166 * this will be equal to num_columns. 167 */ 168int commit_index; 169/* 170 * The commit_index for the previously displayed commit. 171 * 172 * This is used to determine how the first line of a merge 173 * graph output should appear, based on the last line of the 174 * previous commit. 175 */ 176int prev_commit_index; 177/* 178 * The maximum number of columns that can be stored in the columns 179 * and new_columns arrays. This is also half the number of entries 180 * that can be stored in the mapping and new_mapping arrays. 181 */ 182int column_capacity; 183/* 184 * The number of columns (also called "branch lines" in some places) 185 */ 186int num_columns; 187/* 188 * The number of columns in the new_columns array 189 */ 190int num_new_columns; 191/* 192 * The number of entries in the mapping array 193 */ 194int mapping_size; 195/* 196 * The column state before we output the current commit. 197 */ 198struct column *columns; 199/* 200 * The new column state after we output the current commit. 201 * Only valid when state is GRAPH_COLLAPSING. 202 */ 203struct column *new_columns; 204/* 205 * An array that tracks the current state of each 206 * character in the output line during state GRAPH_COLLAPSING. 207 * Each entry is -1 if this character is empty, or a non-negative 208 * integer if the character contains a branch line. The value of 209 * the integer indicates the target position for this branch line. 210 * (I.e., this array maps the current column positions to their 211 * desired positions.) 212 * 213 * The maximum capacity of this array is always 214 * sizeof(int) * 2 * column_capacity. 215 */ 216int*mapping; 217/* 218 * A temporary array for computing the next mapping state 219 * while we are outputting a mapping line. This is stored as part 220 * of the git_graph simply so we don't have to allocate a new 221 * temporary array each time we have to output a collapsing line. 222 */ 223int*new_mapping; 224/* 225 * The current default column color being used. This is 226 * stored as an index into the array column_colors. 227 */ 228unsigned short default_column_color; 229}; 230 231static struct strbuf *diff_output_prefix_callback(struct diff_options *opt,void*data) 232{ 233struct git_graph *graph = data; 234static struct strbuf msgbuf = STRBUF_INIT; 235 236assert(opt); 237 238strbuf_reset(&msgbuf); 239if(opt->line_prefix) 240strbuf_add(&msgbuf, opt->line_prefix, 241 opt->line_prefix_length); 242if(graph) 243graph_padding_line(graph, &msgbuf); 244return&msgbuf; 245} 246 247static const struct diff_options *default_diffopt; 248 249voidgraph_setup_line_prefix(struct diff_options *diffopt) 250{ 251 default_diffopt = diffopt; 252 253/* setup an output prefix callback if necessary */ 254if(diffopt && !diffopt->output_prefix) 255 diffopt->output_prefix = diff_output_prefix_callback; 256} 257 258 259struct git_graph *graph_init(struct rev_info *opt) 260{ 261struct git_graph *graph =xmalloc(sizeof(struct git_graph)); 262 263if(!column_colors) { 264char*string; 265if(git_config_get_string("log.graphcolors", &string)) { 266/* not configured -- use default */ 267graph_set_column_colors(column_colors_ansi, 268 column_colors_ansi_max); 269}else{ 270static struct argv_array custom_colors = ARGV_ARRAY_INIT; 271argv_array_clear(&custom_colors); 272parse_graph_colors_config(&custom_colors, string); 273free(string); 274/* graph_set_column_colors takes a max-index, not a count */ 275graph_set_column_colors(custom_colors.argv, 276 custom_colors.argc -1); 277} 278} 279 280 graph->commit = NULL; 281 graph->revs = opt; 282 graph->num_parents =0; 283 graph->expansion_row =0; 284 graph->state = GRAPH_PADDING; 285 graph->prev_state = GRAPH_PADDING; 286 graph->commit_index =0; 287 graph->prev_commit_index =0; 288 graph->num_columns =0; 289 graph->num_new_columns =0; 290 graph->mapping_size =0; 291/* 292 * Start the column color at the maximum value, since we'll 293 * always increment it for the first commit we output. 294 * This way we start at 0 for the first commit. 295 */ 296 graph->default_column_color = column_colors_max -1; 297 298/* 299 * Allocate a reasonably large default number of columns 300 * We'll automatically grow columns later if we need more room. 301 */ 302 graph->column_capacity =30; 303ALLOC_ARRAY(graph->columns, graph->column_capacity); 304ALLOC_ARRAY(graph->new_columns, graph->column_capacity); 305ALLOC_ARRAY(graph->mapping,2* graph->column_capacity); 306ALLOC_ARRAY(graph->new_mapping,2* graph->column_capacity); 307 308/* 309 * The diff output prefix callback, with this we can make 310 * all the diff output to align with the graph lines. 311 */ 312 opt->diffopt.output_prefix = diff_output_prefix_callback; 313 opt->diffopt.output_prefix_data = graph; 314 315return graph; 316} 317 318static voidgraph_update_state(struct git_graph *graph,enum graph_state s) 319{ 320 graph->prev_state = graph->state; 321 graph->state = s; 322} 323 324static voidgraph_ensure_capacity(struct git_graph *graph,int num_columns) 325{ 326if(graph->column_capacity >= num_columns) 327return; 328 329do{ 330 graph->column_capacity *=2; 331}while(graph->column_capacity < num_columns); 332 333REALLOC_ARRAY(graph->columns, graph->column_capacity); 334REALLOC_ARRAY(graph->new_columns, graph->column_capacity); 335REALLOC_ARRAY(graph->mapping, graph->column_capacity *2); 336REALLOC_ARRAY(graph->new_mapping, graph->column_capacity *2); 337} 338 339/* 340 * Returns 1 if the commit will be printed in the graph output, 341 * and 0 otherwise. 342 */ 343static intgraph_is_interesting(struct git_graph *graph,struct commit *commit) 344{ 345/* 346 * If revs->boundary is set, commits whose children have 347 * been shown are always interesting, even if they have the 348 * UNINTERESTING or TREESAME flags set. 349 */ 350if(graph->revs && graph->revs->boundary) { 351if(commit->object.flags & CHILD_SHOWN) 352return1; 353} 354 355/* 356 * Otherwise, use get_commit_action() to see if this commit is 357 * interesting 358 */ 359returnget_commit_action(graph->revs, commit) == commit_show; 360} 361 362static struct commit_list *next_interesting_parent(struct git_graph *graph, 363struct commit_list *orig) 364{ 365struct commit_list *list; 366 367/* 368 * If revs->first_parent_only is set, only the first 369 * parent is interesting. None of the others are. 370 */ 371if(graph->revs->first_parent_only) 372return NULL; 373 374/* 375 * Return the next interesting commit after orig 376 */ 377for(list = orig->next; list; list = list->next) { 378if(graph_is_interesting(graph, list->item)) 379return list; 380} 381 382return NULL; 383} 384 385static struct commit_list *first_interesting_parent(struct git_graph *graph) 386{ 387struct commit_list *parents = graph->commit->parents; 388 389/* 390 * If this commit has no parents, ignore it 391 */ 392if(!parents) 393return NULL; 394 395/* 396 * If the first parent is interesting, return it 397 */ 398if(graph_is_interesting(graph, parents->item)) 399return parents; 400 401/* 402 * Otherwise, call next_interesting_parent() to get 403 * the next interesting parent 404 */ 405returnnext_interesting_parent(graph, parents); 406} 407 408static unsigned shortgraph_get_current_column_color(const struct git_graph *graph) 409{ 410if(!want_color(graph->revs->diffopt.use_color)) 411return column_colors_max; 412return graph->default_column_color; 413} 414 415/* 416 * Update the graph's default column color. 417 */ 418static voidgraph_increment_column_color(struct git_graph *graph) 419{ 420 graph->default_column_color = (graph->default_column_color +1) % 421 column_colors_max; 422} 423 424static unsigned shortgraph_find_commit_color(const struct git_graph *graph, 425const struct commit *commit) 426{ 427int i; 428for(i =0; i < graph->num_columns; i++) { 429if(graph->columns[i].commit == commit) 430return graph->columns[i].color; 431} 432returngraph_get_current_column_color(graph); 433} 434 435static voidgraph_insert_into_new_columns(struct git_graph *graph, 436struct commit *commit, 437int*mapping_index) 438{ 439int i; 440 441/* 442 * If the commit is already in the new_columns list, we don't need to 443 * add it. Just update the mapping correctly. 444 */ 445for(i =0; i < graph->num_new_columns; i++) { 446if(graph->new_columns[i].commit == commit) { 447 graph->mapping[*mapping_index] = i; 448*mapping_index +=2; 449return; 450} 451} 452 453/* 454 * This commit isn't already in new_columns. Add it. 455 */ 456 graph->new_columns[graph->num_new_columns].commit = commit; 457 graph->new_columns[graph->num_new_columns].color =graph_find_commit_color(graph, commit); 458 graph->mapping[*mapping_index] = graph->num_new_columns; 459*mapping_index +=2; 460 graph->num_new_columns++; 461} 462 463static voidgraph_update_width(struct git_graph *graph, 464int is_commit_in_existing_columns) 465{ 466/* 467 * Compute the width needed to display the graph for this commit. 468 * This is the maximum width needed for any row. All other rows 469 * will be padded to this width. 470 * 471 * Compute the number of columns in the widest row: 472 * Count each existing column (graph->num_columns), and each new 473 * column added by this commit. 474 */ 475int max_cols = graph->num_columns + graph->num_parents; 476 477/* 478 * Even if the current commit has no parents to be printed, it 479 * still takes up a column for itself. 480 */ 481if(graph->num_parents <1) 482 max_cols++; 483 484/* 485 * We added a column for the current commit as part of 486 * graph->num_parents. If the current commit was already in 487 * graph->columns, then we have double counted it. 488 */ 489if(is_commit_in_existing_columns) 490 max_cols--; 491 492/* 493 * Each column takes up 2 spaces 494 */ 495 graph->width = max_cols *2; 496} 497 498static voidgraph_update_columns(struct git_graph *graph) 499{ 500struct commit_list *parent; 501int max_new_columns; 502int mapping_idx; 503int i, seen_this, is_commit_in_columns; 504 505/* 506 * Swap graph->columns with graph->new_columns 507 * graph->columns contains the state for the previous commit, 508 * and new_columns now contains the state for our commit. 509 * 510 * We'll re-use the old columns array as storage to compute the new 511 * columns list for the commit after this one. 512 */ 513SWAP(graph->columns, graph->new_columns); 514 graph->num_columns = graph->num_new_columns; 515 graph->num_new_columns =0; 516 517/* 518 * Now update new_columns and mapping with the information for the 519 * commit after this one. 520 * 521 * First, make sure we have enough room. At most, there will 522 * be graph->num_columns + graph->num_parents columns for the next 523 * commit. 524 */ 525 max_new_columns = graph->num_columns + graph->num_parents; 526graph_ensure_capacity(graph, max_new_columns); 527 528/* 529 * Clear out graph->mapping 530 */ 531 graph->mapping_size =2* max_new_columns; 532for(i =0; i < graph->mapping_size; i++) 533 graph->mapping[i] = -1; 534 535/* 536 * Populate graph->new_columns and graph->mapping 537 * 538 * Some of the parents of this commit may already be in 539 * graph->columns. If so, graph->new_columns should only contain a 540 * single entry for each such commit. graph->mapping should 541 * contain information about where each current branch line is 542 * supposed to end up after the collapsing is performed. 543 */ 544 seen_this =0; 545 mapping_idx =0; 546 is_commit_in_columns =1; 547for(i =0; i <= graph->num_columns; i++) { 548struct commit *col_commit; 549if(i == graph->num_columns) { 550if(seen_this) 551break; 552 is_commit_in_columns =0; 553 col_commit = graph->commit; 554}else{ 555 col_commit = graph->columns[i].commit; 556} 557 558if(col_commit == graph->commit) { 559int old_mapping_idx = mapping_idx; 560 seen_this =1; 561 graph->commit_index = i; 562for(parent =first_interesting_parent(graph); 563 parent; 564 parent =next_interesting_parent(graph, parent)) { 565/* 566 * If this is a merge, or the start of a new 567 * childless column, increment the current 568 * color. 569 */ 570if(graph->num_parents >1|| 571!is_commit_in_columns) { 572graph_increment_column_color(graph); 573} 574graph_insert_into_new_columns(graph, 575 parent->item, 576&mapping_idx); 577} 578/* 579 * We always need to increment mapping_idx by at 580 * least 2, even if it has no interesting parents. 581 * The current commit always takes up at least 2 582 * spaces. 583 */ 584if(mapping_idx == old_mapping_idx) 585 mapping_idx +=2; 586}else{ 587graph_insert_into_new_columns(graph, col_commit, 588&mapping_idx); 589} 590} 591 592/* 593 * Shrink mapping_size to be the minimum necessary 594 */ 595while(graph->mapping_size >1&& 596 graph->mapping[graph->mapping_size -1] <0) 597 graph->mapping_size--; 598 599/* 600 * Compute graph->width for this commit 601 */ 602graph_update_width(graph, is_commit_in_columns); 603} 604 605voidgraph_update(struct git_graph *graph,struct commit *commit) 606{ 607struct commit_list *parent; 608 609/* 610 * Set the new commit 611 */ 612 graph->commit = commit; 613 614/* 615 * Count how many interesting parents this commit has 616 */ 617 graph->num_parents =0; 618for(parent =first_interesting_parent(graph); 619 parent; 620 parent =next_interesting_parent(graph, parent)) 621{ 622 graph->num_parents++; 623} 624 625/* 626 * Store the old commit_index in prev_commit_index. 627 * graph_update_columns() will update graph->commit_index for this 628 * commit. 629 */ 630 graph->prev_commit_index = graph->commit_index; 631 632/* 633 * Call graph_update_columns() to update 634 * columns, new_columns, and mapping. 635 */ 636graph_update_columns(graph); 637 638 graph->expansion_row =0; 639 640/* 641 * Update graph->state. 642 * Note that we don't call graph_update_state() here, since 643 * we don't want to update graph->prev_state. No line for 644 * graph->state was ever printed. 645 * 646 * If the previous commit didn't get to the GRAPH_PADDING state, 647 * it never finished its output. Goto GRAPH_SKIP, to print out 648 * a line to indicate that portion of the graph is missing. 649 * 650 * If there are 3 or more parents, we may need to print extra rows 651 * before the commit, to expand the branch lines around it and make 652 * room for it. We need to do this only if there is a branch row 653 * (or more) to the right of this commit. 654 * 655 * If there are less than 3 parents, we can immediately print the 656 * commit line. 657 */ 658if(graph->state != GRAPH_PADDING) 659 graph->state = GRAPH_SKIP; 660else if(graph->num_parents >=3&& 661 graph->commit_index < (graph->num_columns -1)) 662 graph->state = GRAPH_PRE_COMMIT; 663else 664 graph->state = GRAPH_COMMIT; 665} 666 667static intgraph_is_mapping_correct(struct git_graph *graph) 668{ 669int i; 670 671/* 672 * The mapping is up to date if each entry is at its target, 673 * or is 1 greater than its target. 674 * (If it is 1 greater than the target, '/' will be printed, so it 675 * will look correct on the next row.) 676 */ 677for(i =0; i < graph->mapping_size; i++) { 678int target = graph->mapping[i]; 679if(target <0) 680continue; 681if(target == (i /2)) 682continue; 683return0; 684} 685 686return1; 687} 688 689static voidgraph_pad_horizontally(struct git_graph *graph,struct strbuf *sb, 690int chars_written) 691{ 692/* 693 * Add additional spaces to the end of the strbuf, so that all 694 * lines for a particular commit have the same width. 695 * 696 * This way, fields printed to the right of the graph will remain 697 * aligned for the entire commit. 698 */ 699if(chars_written < graph->width) 700strbuf_addchars(sb,' ', graph->width - chars_written); 701} 702 703static voidgraph_output_padding_line(struct git_graph *graph, 704struct strbuf *sb) 705{ 706int i; 707 708/* 709 * We could conceivable be called with a NULL commit 710 * if our caller has a bug, and invokes graph_next_line() 711 * immediately after graph_init(), without first calling 712 * graph_update(). Return without outputting anything in this 713 * case. 714 */ 715if(!graph->commit) 716return; 717 718/* 719 * Output a padding row, that leaves all branch lines unchanged 720 */ 721for(i =0; i < graph->num_new_columns; i++) { 722strbuf_write_column(sb, &graph->new_columns[i],'|'); 723strbuf_addch(sb,' '); 724} 725 726graph_pad_horizontally(graph, sb, graph->num_new_columns *2); 727} 728 729 730intgraph_width(struct git_graph *graph) 731{ 732return graph->width; 733} 734 735 736static voidgraph_output_skip_line(struct git_graph *graph,struct strbuf *sb) 737{ 738/* 739 * Output an ellipsis to indicate that a portion 740 * of the graph is missing. 741 */ 742strbuf_addstr(sb,"..."); 743graph_pad_horizontally(graph, sb,3); 744 745if(graph->num_parents >=3&& 746 graph->commit_index < (graph->num_columns -1)) 747graph_update_state(graph, GRAPH_PRE_COMMIT); 748else 749graph_update_state(graph, GRAPH_COMMIT); 750} 751 752static voidgraph_output_pre_commit_line(struct git_graph *graph, 753struct strbuf *sb) 754{ 755int num_expansion_rows; 756int i, seen_this; 757int chars_written; 758 759/* 760 * This function formats a row that increases the space around a commit 761 * with multiple parents, to make room for it. It should only be 762 * called when there are 3 or more parents. 763 * 764 * We need 2 extra rows for every parent over 2. 765 */ 766assert(graph->num_parents >=3); 767 num_expansion_rows = (graph->num_parents -2) *2; 768 769/* 770 * graph->expansion_row tracks the current expansion row we are on. 771 * It should be in the range [0, num_expansion_rows - 1] 772 */ 773assert(0<= graph->expansion_row && 774 graph->expansion_row < num_expansion_rows); 775 776/* 777 * Output the row 778 */ 779 seen_this =0; 780 chars_written =0; 781for(i =0; i < graph->num_columns; i++) { 782struct column *col = &graph->columns[i]; 783if(col->commit == graph->commit) { 784 seen_this =1; 785strbuf_write_column(sb, col,'|'); 786strbuf_addchars(sb,' ', graph->expansion_row); 787 chars_written +=1+ graph->expansion_row; 788}else if(seen_this && (graph->expansion_row ==0)) { 789/* 790 * This is the first line of the pre-commit output. 791 * If the previous commit was a merge commit and 792 * ended in the GRAPH_POST_MERGE state, all branch 793 * lines after graph->prev_commit_index were 794 * printed as "\" on the previous line. Continue 795 * to print them as "\" on this line. Otherwise, 796 * print the branch lines as "|". 797 */ 798if(graph->prev_state == GRAPH_POST_MERGE && 799 graph->prev_commit_index < i) 800strbuf_write_column(sb, col,'\\'); 801else 802strbuf_write_column(sb, col,'|'); 803 chars_written++; 804}else if(seen_this && (graph->expansion_row >0)) { 805strbuf_write_column(sb, col,'\\'); 806 chars_written++; 807}else{ 808strbuf_write_column(sb, col,'|'); 809 chars_written++; 810} 811strbuf_addch(sb,' '); 812 chars_written++; 813} 814 815graph_pad_horizontally(graph, sb, chars_written); 816 817/* 818 * Increment graph->expansion_row, 819 * and move to state GRAPH_COMMIT if necessary 820 */ 821 graph->expansion_row++; 822if(graph->expansion_row >= num_expansion_rows) 823graph_update_state(graph, GRAPH_COMMIT); 824} 825 826static voidgraph_output_commit_char(struct git_graph *graph,struct strbuf *sb) 827{ 828/* 829 * For boundary commits, print 'o' 830 * (We should only see boundary commits when revs->boundary is set.) 831 */ 832if(graph->commit->object.flags & BOUNDARY) { 833assert(graph->revs->boundary); 834strbuf_addch(sb,'o'); 835return; 836} 837 838/* 839 * get_revision_mark() handles all other cases without assert() 840 */ 841strbuf_addstr(sb,get_revision_mark(graph->revs, graph->commit)); 842} 843 844/* 845 * Draw the horizontal dashes of an octopus merge and return the number of 846 * characters written. 847 */ 848static intgraph_draw_octopus_merge(struct git_graph *graph, 849struct strbuf *sb) 850{ 851/* 852 * Here dashless_parents represents the number of parents which don't 853 * need to have dashes (the edges labeled "0" and "1"). And 854 * dashful_parents are the remaining ones. 855 * 856 * | *---. 857 * | |\ \ \ 858 * | | | | | 859 * x 0 1 2 3 860 * 861 */ 862const int dashless_parents =2; 863int dashful_parents = graph->num_parents - dashless_parents; 864 865/* 866 * Usually, we add one new column for each parent (like the diagram 867 * above) but sometimes the first parent goes into an existing column, 868 * like this: 869 * 870 * | *---. 871 * | |\ \ \ 872 * |/ / / / 873 * x 0 1 2 874 * 875 * In which case the number of parents will be one greater than the 876 * number of added columns. 877 */ 878int added_cols = (graph->num_new_columns - graph->num_columns); 879int parent_in_old_cols = graph->num_parents - added_cols; 880 881/* 882 * In both cases, commit_index corresponds to the edge labeled "0". 883 */ 884int first_col = graph->commit_index + dashless_parents 885- parent_in_old_cols; 886 887int i; 888for(i =0; i < dashful_parents; i++) { 889strbuf_write_column(sb, &graph->new_columns[i+first_col],'-'); 890strbuf_write_column(sb, &graph->new_columns[i+first_col], 891 i == dashful_parents-1?'.':'-'); 892} 893return2* dashful_parents; 894} 895 896static voidgraph_output_commit_line(struct git_graph *graph,struct strbuf *sb) 897{ 898int seen_this =0; 899int i, chars_written; 900 901/* 902 * Output the row containing this commit 903 * Iterate up to and including graph->num_columns, 904 * since the current commit may not be in any of the existing 905 * columns. (This happens when the current commit doesn't have any 906 * children that we have already processed.) 907 */ 908 seen_this =0; 909 chars_written =0; 910for(i =0; i <= graph->num_columns; i++) { 911struct column *col = &graph->columns[i]; 912struct commit *col_commit; 913if(i == graph->num_columns) { 914if(seen_this) 915break; 916 col_commit = graph->commit; 917}else{ 918 col_commit = graph->columns[i].commit; 919} 920 921if(col_commit == graph->commit) { 922 seen_this =1; 923graph_output_commit_char(graph, sb); 924 chars_written++; 925 926if(graph->num_parents >2) 927 chars_written +=graph_draw_octopus_merge(graph, 928 sb); 929}else if(seen_this && (graph->num_parents >2)) { 930strbuf_write_column(sb, col,'\\'); 931 chars_written++; 932}else if(seen_this && (graph->num_parents ==2)) { 933/* 934 * This is a 2-way merge commit. 935 * There is no GRAPH_PRE_COMMIT stage for 2-way 936 * merges, so this is the first line of output 937 * for this commit. Check to see what the previous 938 * line of output was. 939 * 940 * If it was GRAPH_POST_MERGE, the branch line 941 * coming into this commit may have been '\', 942 * and not '|' or '/'. If so, output the branch 943 * line as '\' on this line, instead of '|'. This 944 * makes the output look nicer. 945 */ 946if(graph->prev_state == GRAPH_POST_MERGE && 947 graph->prev_commit_index < i) 948strbuf_write_column(sb, col,'\\'); 949else 950strbuf_write_column(sb, col,'|'); 951 chars_written++; 952}else{ 953strbuf_write_column(sb, col,'|'); 954 chars_written++; 955} 956strbuf_addch(sb,' '); 957 chars_written++; 958} 959 960graph_pad_horizontally(graph, sb, chars_written); 961 962/* 963 * Update graph->state 964 */ 965if(graph->num_parents >1) 966graph_update_state(graph, GRAPH_POST_MERGE); 967else if(graph_is_mapping_correct(graph)) 968graph_update_state(graph, GRAPH_PADDING); 969else 970graph_update_state(graph, GRAPH_COLLAPSING); 971} 972 973static struct column *find_new_column_by_commit(struct git_graph *graph, 974struct commit *commit) 975{ 976int i; 977for(i =0; i < graph->num_new_columns; i++) { 978if(graph->new_columns[i].commit == commit) 979return&graph->new_columns[i]; 980} 981return NULL; 982} 983 984static voidgraph_output_post_merge_line(struct git_graph *graph,struct strbuf *sb) 985{ 986int seen_this =0; 987int i, j, chars_written; 988 989/* 990 * Output the post-merge row 991 */ 992 chars_written =0; 993for(i =0; i <= graph->num_columns; i++) { 994struct column *col = &graph->columns[i]; 995struct commit *col_commit; 996if(i == graph->num_columns) { 997if(seen_this) 998break; 999 col_commit = graph->commit;1000}else{1001 col_commit = col->commit;1002}10031004if(col_commit == graph->commit) {1005/*1006 * Since the current commit is a merge find1007 * the columns for the parent commits in1008 * new_columns and use those to format the1009 * edges.1010 */1011struct commit_list *parents = NULL;1012struct column *par_column;1013 seen_this =1;1014 parents =first_interesting_parent(graph);1015assert(parents);1016 par_column =find_new_column_by_commit(graph, parents->item);1017assert(par_column);10181019strbuf_write_column(sb, par_column,'|');1020 chars_written++;1021for(j =0; j < graph->num_parents -1; j++) {1022 parents =next_interesting_parent(graph, parents);1023assert(parents);1024 par_column =find_new_column_by_commit(graph, parents->item);1025assert(par_column);1026strbuf_write_column(sb, par_column,'\\');1027strbuf_addch(sb,' ');1028}1029 chars_written += j *2;1030}else if(seen_this) {1031strbuf_write_column(sb, col,'\\');1032strbuf_addch(sb,' ');1033 chars_written +=2;1034}else{1035strbuf_write_column(sb, col,'|');1036strbuf_addch(sb,' ');1037 chars_written +=2;1038}1039}10401041graph_pad_horizontally(graph, sb, chars_written);10421043/*1044 * Update graph->state1045 */1046if(graph_is_mapping_correct(graph))1047graph_update_state(graph, GRAPH_PADDING);1048else1049graph_update_state(graph, GRAPH_COLLAPSING);1050}10511052static voidgraph_output_collapsing_line(struct git_graph *graph,struct strbuf *sb)1053{1054int i;1055short used_horizontal =0;1056int horizontal_edge = -1;1057int horizontal_edge_target = -1;10581059/*1060 * Clear out the new_mapping array1061 */1062for(i =0; i < graph->mapping_size; i++)1063 graph->new_mapping[i] = -1;10641065for(i =0; i < graph->mapping_size; i++) {1066int target = graph->mapping[i];1067if(target <0)1068continue;10691070/*1071 * Since update_columns() always inserts the leftmost1072 * column first, each branch's target location should1073 * always be either its current location or to the left of1074 * its current location.1075 *1076 * We never have to move branches to the right. This makes1077 * the graph much more legible, since whenever branches1078 * cross, only one is moving directions.1079 */1080assert(target *2<= i);10811082if(target *2== i) {1083/*1084 * This column is already in the1085 * correct place1086 */1087assert(graph->new_mapping[i] == -1);1088 graph->new_mapping[i] = target;1089}else if(graph->new_mapping[i -1] <0) {1090/*1091 * Nothing is to the left.1092 * Move to the left by one1093 */1094 graph->new_mapping[i -1] = target;1095/*1096 * If there isn't already an edge moving horizontally1097 * select this one.1098 */1099if(horizontal_edge == -1) {1100int j;1101 horizontal_edge = i;1102 horizontal_edge_target = target;1103/*1104 * The variable target is the index of the graph1105 * column, and therefore target*2+3 is the1106 * actual screen column of the first horizontal1107 * line.1108 */1109for(j = (target *2)+3; j < (i -2); j +=2)1110 graph->new_mapping[j] = target;1111}1112}else if(graph->new_mapping[i -1] == target) {1113/*1114 * There is a branch line to our left1115 * already, and it is our target. We1116 * combine with this line, since we share1117 * the same parent commit.1118 *1119 * We don't have to add anything to the1120 * output or new_mapping, since the1121 * existing branch line has already taken1122 * care of it.1123 */1124}else{1125/*1126 * There is a branch line to our left,1127 * but it isn't our target. We need to1128 * cross over it.1129 *1130 * The space just to the left of this1131 * branch should always be empty.1132 *1133 * The branch to the left of that space1134 * should be our eventual target.1135 */1136assert(graph->new_mapping[i -1] > target);1137assert(graph->new_mapping[i -2] <0);1138assert(graph->new_mapping[i -3] == target);1139 graph->new_mapping[i -2] = target;1140/*1141 * Mark this branch as the horizontal edge to1142 * prevent any other edges from moving1143 * horizontally.1144 */1145if(horizontal_edge == -1)1146 horizontal_edge = i;1147}1148}11491150/*1151 * The new mapping may be 1 smaller than the old mapping1152 */1153if(graph->new_mapping[graph->mapping_size -1] <0)1154 graph->mapping_size--;11551156/*1157 * Output out a line based on the new mapping info1158 */1159for(i =0; i < graph->mapping_size; i++) {1160int target = graph->new_mapping[i];1161if(target <0)1162strbuf_addch(sb,' ');1163else if(target *2== i)1164strbuf_write_column(sb, &graph->new_columns[target],'|');1165else if(target == horizontal_edge_target &&1166 i != horizontal_edge -1) {1167/*1168 * Set the mappings for all but the1169 * first segment to -1 so that they1170 * won't continue into the next line.1171 */1172if(i != (target *2)+3)1173 graph->new_mapping[i] = -1;1174 used_horizontal =1;1175strbuf_write_column(sb, &graph->new_columns[target],'_');1176}else{1177if(used_horizontal && i < horizontal_edge)1178 graph->new_mapping[i] = -1;1179strbuf_write_column(sb, &graph->new_columns[target],'/');11801181}1182}11831184graph_pad_horizontally(graph, sb, graph->mapping_size);11851186/*1187 * Swap mapping and new_mapping1188 */1189SWAP(graph->mapping, graph->new_mapping);11901191/*1192 * If graph->mapping indicates that all of the branch lines1193 * are already in the correct positions, we are done.1194 * Otherwise, we need to collapse some branch lines together.1195 */1196if(graph_is_mapping_correct(graph))1197graph_update_state(graph, GRAPH_PADDING);1198}11991200intgraph_next_line(struct git_graph *graph,struct strbuf *sb)1201{1202switch(graph->state) {1203case GRAPH_PADDING:1204graph_output_padding_line(graph, sb);1205return0;1206case GRAPH_SKIP:1207graph_output_skip_line(graph, sb);1208return0;1209case GRAPH_PRE_COMMIT:1210graph_output_pre_commit_line(graph, sb);1211return0;1212case GRAPH_COMMIT:1213graph_output_commit_line(graph, sb);1214return1;1215case GRAPH_POST_MERGE:1216graph_output_post_merge_line(graph, sb);1217return0;1218case GRAPH_COLLAPSING:1219graph_output_collapsing_line(graph, sb);1220return0;1221}12221223assert(0);1224return0;1225}12261227static voidgraph_padding_line(struct git_graph *graph,struct strbuf *sb)1228{1229int i;1230int chars_written =0;12311232if(graph->state != GRAPH_COMMIT) {1233graph_next_line(graph, sb);1234return;1235}12361237/*1238 * Output the row containing this commit1239 * Iterate up to and including graph->num_columns,1240 * since the current commit may not be in any of the existing1241 * columns. (This happens when the current commit doesn't have any1242 * children that we have already processed.)1243 */1244for(i =0; i < graph->num_columns; i++) {1245struct column *col = &graph->columns[i];12461247strbuf_write_column(sb, col,'|');1248 chars_written++;12491250if(col->commit == graph->commit && graph->num_parents >2) {1251int len = (graph->num_parents -2) *2;1252strbuf_addchars(sb,' ', len);1253 chars_written += len;1254}else{1255strbuf_addch(sb,' ');1256 chars_written++;1257}1258}12591260graph_pad_horizontally(graph, sb, chars_written);12611262/*1263 * Update graph->prev_state since we have output a padding line1264 */1265 graph->prev_state = GRAPH_PADDING;1266}12671268intgraph_is_commit_finished(struct git_graph const*graph)1269{1270return(graph->state == GRAPH_PADDING);1271}12721273voidgraph_show_commit(struct git_graph *graph)1274{1275struct strbuf msgbuf = STRBUF_INIT;1276int shown_commit_line =0;12771278graph_show_line_prefix(default_diffopt);12791280if(!graph)1281return;12821283/*1284 * When showing a diff of a merge against each of its parents, we1285 * are called once for each parent without graph_update having been1286 * called. In this case, simply output a single padding line.1287 */1288if(graph_is_commit_finished(graph)) {1289graph_show_padding(graph);1290 shown_commit_line =1;1291}12921293while(!shown_commit_line && !graph_is_commit_finished(graph)) {1294 shown_commit_line =graph_next_line(graph, &msgbuf);1295fwrite(msgbuf.buf,sizeof(char), msgbuf.len,1296 graph->revs->diffopt.file);1297if(!shown_commit_line) {1298putc('\n', graph->revs->diffopt.file);1299graph_show_line_prefix(&graph->revs->diffopt);1300}1301strbuf_setlen(&msgbuf,0);1302}13031304strbuf_release(&msgbuf);1305}13061307voidgraph_show_oneline(struct git_graph *graph)1308{1309struct strbuf msgbuf = STRBUF_INIT;13101311graph_show_line_prefix(default_diffopt);13121313if(!graph)1314return;13151316graph_next_line(graph, &msgbuf);1317fwrite(msgbuf.buf,sizeof(char), msgbuf.len, graph->revs->diffopt.file);1318strbuf_release(&msgbuf);1319}13201321voidgraph_show_padding(struct git_graph *graph)1322{1323struct strbuf msgbuf = STRBUF_INIT;13241325graph_show_line_prefix(default_diffopt);13261327if(!graph)1328return;13291330graph_padding_line(graph, &msgbuf);1331fwrite(msgbuf.buf,sizeof(char), msgbuf.len, graph->revs->diffopt.file);1332strbuf_release(&msgbuf);1333}13341335intgraph_show_remainder(struct git_graph *graph)1336{1337struct strbuf msgbuf = STRBUF_INIT;1338int shown =0;13391340graph_show_line_prefix(default_diffopt);13411342if(!graph)1343return0;13441345if(graph_is_commit_finished(graph))1346return0;13471348for(;;) {1349graph_next_line(graph, &msgbuf);1350fwrite(msgbuf.buf,sizeof(char), msgbuf.len,1351 graph->revs->diffopt.file);1352strbuf_setlen(&msgbuf,0);1353 shown =1;13541355if(!graph_is_commit_finished(graph)) {1356putc('\n', graph->revs->diffopt.file);1357graph_show_line_prefix(&graph->revs->diffopt);1358}else{1359break;1360}1361}1362strbuf_release(&msgbuf);13631364return shown;1365}13661367static voidgraph_show_strbuf(struct git_graph *graph,1368FILE*file,1369struct strbuf const*sb)1370{1371char*p;13721373/*1374 * Print the strbuf line by line,1375 * and display the graph info before each line but the first.1376 */1377 p = sb->buf;1378while(p) {1379size_t len;1380char*next_p =strchr(p,'\n');1381if(next_p) {1382 next_p++;1383 len = next_p - p;1384}else{1385 len = (sb->buf + sb->len) - p;1386}1387fwrite(p,sizeof(char), len, file);1388if(next_p && *next_p !='\0')1389graph_show_oneline(graph);1390 p = next_p;1391}1392}13931394voidgraph_show_commit_msg(struct git_graph *graph,1395FILE*file,1396struct strbuf const*sb)1397{1398int newline_terminated;13991400/*1401 * Show the commit message1402 */1403graph_show_strbuf(graph, file, sb);14041405if(!graph)1406return;14071408 newline_terminated = (sb->len && sb->buf[sb->len -1] =='\n');14091410/*1411 * If there is more output needed for this commit, show it now1412 */1413if(!graph_is_commit_finished(graph)) {1414/*1415 * If sb doesn't have a terminating newline, print one now,1416 * so we can start the remainder of the graph output on a1417 * new line.1418 */1419if(!newline_terminated)1420putc('\n', file);14211422graph_show_remainder(graph);14231424/*1425 * If sb ends with a newline, our output should too.1426 */1427if(newline_terminated)1428putc('\n', file);1429}1430}