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