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