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