builtin-rev-list.con commit pull --rebase: exit early when the working directory is dirty (f9189cf)
   1#include "cache.h"
   2#include "refs.h"
   3#include "tag.h"
   4#include "commit.h"
   5#include "tree.h"
   6#include "blob.h"
   7#include "tree-walk.h"
   8#include "diff.h"
   9#include "revision.h"
  10#include "list-objects.h"
  11#include "builtin.h"
  12#include "log-tree.h"
  13#include "graph.h"
  14
  15/* bits #0-15 in revision.h */
  16
  17#define COUNTED         (1u<<16)
  18
  19static const char rev_list_usage[] =
  20"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
  21"  limiting output:\n"
  22"    --max-count=nr\n"
  23"    --max-age=epoch\n"
  24"    --min-age=epoch\n"
  25"    --sparse\n"
  26"    --no-merges\n"
  27"    --remove-empty\n"
  28"    --all\n"
  29"    --branches\n"
  30"    --tags\n"
  31"    --remotes\n"
  32"    --stdin\n"
  33"    --quiet\n"
  34"  ordering output:\n"
  35"    --topo-order\n"
  36"    --date-order\n"
  37"    --reverse\n"
  38"  formatting output:\n"
  39"    --parents\n"
  40"    --objects | --objects-edge\n"
  41"    --unpacked\n"
  42"    --header | --pretty\n"
  43"    --abbrev=nr | --no-abbrev\n"
  44"    --abbrev-commit\n"
  45"    --left-right\n"
  46"  special purpose:\n"
  47"    --bisect\n"
  48"    --bisect-vars\n"
  49"    --bisect-all"
  50;
  51
  52static struct rev_info revs;
  53
  54static int bisect_list;
  55static int show_timestamp;
  56static int hdr_termination;
  57static const char *header_prefix;
  58
  59static void finish_commit(struct commit *commit);
  60static void show_commit(struct commit *commit)
  61{
  62        graph_show_commit(revs.graph);
  63
  64        if (show_timestamp)
  65                printf("%lu ", commit->date);
  66        if (header_prefix)
  67                fputs(header_prefix, stdout);
  68        if (commit->object.flags & BOUNDARY)
  69                putchar('-');
  70        else if (commit->object.flags & UNINTERESTING)
  71                putchar('^');
  72        else if (revs.left_right) {
  73                if (commit->object.flags & SYMMETRIC_LEFT)
  74                        putchar('<');
  75                else
  76                        putchar('>');
  77        }
  78        if (revs.abbrev_commit && revs.abbrev)
  79                fputs(find_unique_abbrev(commit->object.sha1, revs.abbrev),
  80                      stdout);
  81        else
  82                fputs(sha1_to_hex(commit->object.sha1), stdout);
  83        if (revs.print_parents) {
  84                struct commit_list *parents = commit->parents;
  85                while (parents) {
  86                        printf(" %s", sha1_to_hex(parents->item->object.sha1));
  87                        parents = parents->next;
  88                }
  89        }
  90        show_decorations(commit);
  91        if (revs.commit_format == CMIT_FMT_ONELINE)
  92                putchar(' ');
  93        else
  94                putchar('\n');
  95
  96        if (revs.verbose_header && commit->buffer) {
  97                struct strbuf buf;
  98                strbuf_init(&buf, 0);
  99                pretty_print_commit(revs.commit_format, commit,
 100                                    &buf, revs.abbrev, NULL, NULL,
 101                                    revs.date_mode, 0);
 102                if (revs.graph) {
 103                        if (buf.len) {
 104                                if (revs.commit_format != CMIT_FMT_ONELINE)
 105                                        graph_show_oneline(revs.graph);
 106
 107                                graph_show_commit_msg(revs.graph, &buf);
 108
 109                                /*
 110                                 * Add a newline after the commit message.
 111                                 *
 112                                 * Usually, this newline produces a blank
 113                                 * padding line between entries, in which case
 114                                 * we need to add graph padding on this line.
 115                                 *
 116                                 * However, the commit message may not end in a
 117                                 * newline.  In this case the newline simply
 118                                 * ends the last line of the commit message,
 119                                 * and we don't need any graph output.  (This
 120                                 * always happens with CMIT_FMT_ONELINE, and it
 121                                 * happens with CMIT_FMT_USERFORMAT when the
 122                                 * format doesn't explicitly end in a newline.)
 123                                 */
 124                                if (buf.len && buf.buf[buf.len - 1] == '\n')
 125                                        graph_show_padding(revs.graph);
 126                                putchar('\n');
 127                        } else {
 128                                /*
 129                                 * If the message buffer is empty, just show
 130                                 * the rest of the graph output for this
 131                                 * commit.
 132                                 */
 133                                if (graph_show_remainder(revs.graph))
 134                                        putchar('\n');
 135                        }
 136                } else {
 137                        if (buf.len)
 138                                printf("%s%c", buf.buf, hdr_termination);
 139                }
 140                strbuf_release(&buf);
 141        } else {
 142                if (graph_show_remainder(revs.graph))
 143                        putchar('\n');
 144        }
 145        maybe_flush_or_die(stdout, "stdout");
 146        finish_commit(commit);
 147}
 148
 149static void finish_commit(struct commit *commit)
 150{
 151        if (commit->parents) {
 152                free_commit_list(commit->parents);
 153                commit->parents = NULL;
 154        }
 155        free(commit->buffer);
 156        commit->buffer = NULL;
 157}
 158
 159static void finish_object(struct object_array_entry *p)
 160{
 161        if (p->item->type == OBJ_BLOB && !has_sha1_file(p->item->sha1))
 162                die("missing blob object '%s'", sha1_to_hex(p->item->sha1));
 163}
 164
 165static void show_object(struct object_array_entry *p)
 166{
 167        /* An object with name "foo\n0000000..." can be used to
 168         * confuse downstream git-pack-objects very badly.
 169         */
 170        const char *ep = strchr(p->name, '\n');
 171
 172        finish_object(p);
 173        if (ep) {
 174                printf("%s %.*s\n", sha1_to_hex(p->item->sha1),
 175                       (int) (ep - p->name),
 176                       p->name);
 177        }
 178        else
 179                printf("%s %s\n", sha1_to_hex(p->item->sha1), p->name);
 180}
 181
 182static void show_edge(struct commit *commit)
 183{
 184        printf("-%s\n", sha1_to_hex(commit->object.sha1));
 185}
 186
 187/*
 188 * This is a truly stupid algorithm, but it's only
 189 * used for bisection, and we just don't care enough.
 190 *
 191 * We care just barely enough to avoid recursing for
 192 * non-merge entries.
 193 */
 194static int count_distance(struct commit_list *entry)
 195{
 196        int nr = 0;
 197
 198        while (entry) {
 199                struct commit *commit = entry->item;
 200                struct commit_list *p;
 201
 202                if (commit->object.flags & (UNINTERESTING | COUNTED))
 203                        break;
 204                if (!(commit->object.flags & TREESAME))
 205                        nr++;
 206                commit->object.flags |= COUNTED;
 207                p = commit->parents;
 208                entry = p;
 209                if (p) {
 210                        p = p->next;
 211                        while (p) {
 212                                nr += count_distance(p);
 213                                p = p->next;
 214                        }
 215                }
 216        }
 217
 218        return nr;
 219}
 220
 221static void clear_distance(struct commit_list *list)
 222{
 223        while (list) {
 224                struct commit *commit = list->item;
 225                commit->object.flags &= ~COUNTED;
 226                list = list->next;
 227        }
 228}
 229
 230#define DEBUG_BISECT 0
 231
 232static inline int weight(struct commit_list *elem)
 233{
 234        return *((int*)(elem->item->util));
 235}
 236
 237static inline void weight_set(struct commit_list *elem, int weight)
 238{
 239        *((int*)(elem->item->util)) = weight;
 240}
 241
 242static int count_interesting_parents(struct commit *commit)
 243{
 244        struct commit_list *p;
 245        int count;
 246
 247        for (count = 0, p = commit->parents; p; p = p->next) {
 248                if (p->item->object.flags & UNINTERESTING)
 249                        continue;
 250                count++;
 251        }
 252        return count;
 253}
 254
 255static inline int halfway(struct commit_list *p, int nr)
 256{
 257        /*
 258         * Don't short-cut something we are not going to return!
 259         */
 260        if (p->item->object.flags & TREESAME)
 261                return 0;
 262        if (DEBUG_BISECT)
 263                return 0;
 264        /*
 265         * 2 and 3 are halfway of 5.
 266         * 3 is halfway of 6 but 2 and 4 are not.
 267         */
 268        switch (2 * weight(p) - nr) {
 269        case -1: case 0: case 1:
 270                return 1;
 271        default:
 272                return 0;
 273        }
 274}
 275
 276#if !DEBUG_BISECT
 277#define show_list(a,b,c,d) do { ; } while (0)
 278#else
 279static void show_list(const char *debug, int counted, int nr,
 280                      struct commit_list *list)
 281{
 282        struct commit_list *p;
 283
 284        fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
 285
 286        for (p = list; p; p = p->next) {
 287                struct commit_list *pp;
 288                struct commit *commit = p->item;
 289                unsigned flags = commit->object.flags;
 290                enum object_type type;
 291                unsigned long size;
 292                char *buf = read_sha1_file(commit->object.sha1, &type, &size);
 293                char *ep, *sp;
 294
 295                fprintf(stderr, "%c%c%c ",
 296                        (flags & TREESAME) ? ' ' : 'T',
 297                        (flags & UNINTERESTING) ? 'U' : ' ',
 298                        (flags & COUNTED) ? 'C' : ' ');
 299                if (commit->util)
 300                        fprintf(stderr, "%3d", weight(p));
 301                else
 302                        fprintf(stderr, "---");
 303                fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
 304                for (pp = commit->parents; pp; pp = pp->next)
 305                        fprintf(stderr, " %.*s", 8,
 306                                sha1_to_hex(pp->item->object.sha1));
 307
 308                sp = strstr(buf, "\n\n");
 309                if (sp) {
 310                        sp += 2;
 311                        for (ep = sp; *ep && *ep != '\n'; ep++)
 312                                ;
 313                        fprintf(stderr, " %.*s", (int)(ep - sp), sp);
 314                }
 315                fprintf(stderr, "\n");
 316        }
 317}
 318#endif /* DEBUG_BISECT */
 319
 320static struct commit_list *best_bisection(struct commit_list *list, int nr)
 321{
 322        struct commit_list *p, *best;
 323        int best_distance = -1;
 324
 325        best = list;
 326        for (p = list; p; p = p->next) {
 327                int distance;
 328                unsigned flags = p->item->object.flags;
 329
 330                if (flags & TREESAME)
 331                        continue;
 332                distance = weight(p);
 333                if (nr - distance < distance)
 334                        distance = nr - distance;
 335                if (distance > best_distance) {
 336                        best = p;
 337                        best_distance = distance;
 338                }
 339        }
 340
 341        return best;
 342}
 343
 344struct commit_dist {
 345        struct commit *commit;
 346        int distance;
 347};
 348
 349static int compare_commit_dist(const void *a_, const void *b_)
 350{
 351        struct commit_dist *a, *b;
 352
 353        a = (struct commit_dist *)a_;
 354        b = (struct commit_dist *)b_;
 355        if (a->distance != b->distance)
 356                return b->distance - a->distance; /* desc sort */
 357        return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
 358}
 359
 360static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
 361{
 362        struct commit_list *p;
 363        struct commit_dist *array = xcalloc(nr, sizeof(*array));
 364        int cnt, i;
 365
 366        for (p = list, cnt = 0; p; p = p->next) {
 367                int distance;
 368                unsigned flags = p->item->object.flags;
 369
 370                if (flags & TREESAME)
 371                        continue;
 372                distance = weight(p);
 373                if (nr - distance < distance)
 374                        distance = nr - distance;
 375                array[cnt].commit = p->item;
 376                array[cnt].distance = distance;
 377                cnt++;
 378        }
 379        qsort(array, cnt, sizeof(*array), compare_commit_dist);
 380        for (p = list, i = 0; i < cnt; i++) {
 381                struct name_decoration *r = xmalloc(sizeof(*r) + 100);
 382                struct object *obj = &(array[i].commit->object);
 383
 384                sprintf(r->name, "dist=%d", array[i].distance);
 385                r->next = add_decoration(&name_decoration, obj, r);
 386                p->item = array[i].commit;
 387                p = p->next;
 388        }
 389        if (p)
 390                p->next = NULL;
 391        free(array);
 392        return list;
 393}
 394
 395/*
 396 * zero or positive weight is the number of interesting commits it can
 397 * reach, including itself.  Especially, weight = 0 means it does not
 398 * reach any tree-changing commits (e.g. just above uninteresting one
 399 * but traversal is with pathspec).
 400 *
 401 * weight = -1 means it has one parent and its distance is yet to
 402 * be computed.
 403 *
 404 * weight = -2 means it has more than one parent and its distance is
 405 * unknown.  After running count_distance() first, they will get zero
 406 * or positive distance.
 407 */
 408static struct commit_list *do_find_bisection(struct commit_list *list,
 409                                             int nr, int *weights,
 410                                             int find_all)
 411{
 412        int n, counted;
 413        struct commit_list *p;
 414
 415        counted = 0;
 416
 417        for (n = 0, p = list; p; p = p->next) {
 418                struct commit *commit = p->item;
 419                unsigned flags = commit->object.flags;
 420
 421                p->item->util = &weights[n++];
 422                switch (count_interesting_parents(commit)) {
 423                case 0:
 424                        if (!(flags & TREESAME)) {
 425                                weight_set(p, 1);
 426                                counted++;
 427                                show_list("bisection 2 count one",
 428                                          counted, nr, list);
 429                        }
 430                        /*
 431                         * otherwise, it is known not to reach any
 432                         * tree-changing commit and gets weight 0.
 433                         */
 434                        break;
 435                case 1:
 436                        weight_set(p, -1);
 437                        break;
 438                default:
 439                        weight_set(p, -2);
 440                        break;
 441                }
 442        }
 443
 444        show_list("bisection 2 initialize", counted, nr, list);
 445
 446        /*
 447         * If you have only one parent in the resulting set
 448         * then you can reach one commit more than that parent
 449         * can reach.  So we do not have to run the expensive
 450         * count_distance() for single strand of pearls.
 451         *
 452         * However, if you have more than one parents, you cannot
 453         * just add their distance and one for yourself, since
 454         * they usually reach the same ancestor and you would
 455         * end up counting them twice that way.
 456         *
 457         * So we will first count distance of merges the usual
 458         * way, and then fill the blanks using cheaper algorithm.
 459         */
 460        for (p = list; p; p = p->next) {
 461                if (p->item->object.flags & UNINTERESTING)
 462                        continue;
 463                if (weight(p) != -2)
 464                        continue;
 465                weight_set(p, count_distance(p));
 466                clear_distance(list);
 467
 468                /* Does it happen to be at exactly half-way? */
 469                if (!find_all && halfway(p, nr))
 470                        return p;
 471                counted++;
 472        }
 473
 474        show_list("bisection 2 count_distance", counted, nr, list);
 475
 476        while (counted < nr) {
 477                for (p = list; p; p = p->next) {
 478                        struct commit_list *q;
 479                        unsigned flags = p->item->object.flags;
 480
 481                        if (0 <= weight(p))
 482                                continue;
 483                        for (q = p->item->parents; q; q = q->next) {
 484                                if (q->item->object.flags & UNINTERESTING)
 485                                        continue;
 486                                if (0 <= weight(q))
 487                                        break;
 488                        }
 489                        if (!q)
 490                                continue;
 491
 492                        /*
 493                         * weight for p is unknown but q is known.
 494                         * add one for p itself if p is to be counted,
 495                         * otherwise inherit it from q directly.
 496                         */
 497                        if (!(flags & TREESAME)) {
 498                                weight_set(p, weight(q)+1);
 499                                counted++;
 500                                show_list("bisection 2 count one",
 501                                          counted, nr, list);
 502                        }
 503                        else
 504                                weight_set(p, weight(q));
 505
 506                        /* Does it happen to be at exactly half-way? */
 507                        if (!find_all && halfway(p, nr))
 508                                return p;
 509                }
 510        }
 511
 512        show_list("bisection 2 counted all", counted, nr, list);
 513
 514        if (!find_all)
 515                return best_bisection(list, nr);
 516        else
 517                return best_bisection_sorted(list, nr);
 518}
 519
 520static struct commit_list *find_bisection(struct commit_list *list,
 521                                          int *reaches, int *all,
 522                                          int find_all)
 523{
 524        int nr, on_list;
 525        struct commit_list *p, *best, *next, *last;
 526        int *weights;
 527
 528        show_list("bisection 2 entry", 0, 0, list);
 529
 530        /*
 531         * Count the number of total and tree-changing items on the
 532         * list, while reversing the list.
 533         */
 534        for (nr = on_list = 0, last = NULL, p = list;
 535             p;
 536             p = next) {
 537                unsigned flags = p->item->object.flags;
 538
 539                next = p->next;
 540                if (flags & UNINTERESTING)
 541                        continue;
 542                p->next = last;
 543                last = p;
 544                if (!(flags & TREESAME))
 545                        nr++;
 546                on_list++;
 547        }
 548        list = last;
 549        show_list("bisection 2 sorted", 0, nr, list);
 550
 551        *all = nr;
 552        weights = xcalloc(on_list, sizeof(*weights));
 553
 554        /* Do the real work of finding bisection commit. */
 555        best = do_find_bisection(list, nr, weights, find_all);
 556        if (best) {
 557                if (!find_all)
 558                        best->next = NULL;
 559                *reaches = weight(best);
 560        }
 561        free(weights);
 562        return best;
 563}
 564
 565static void read_revisions_from_stdin(struct rev_info *revs)
 566{
 567        char line[1000];
 568
 569        while (fgets(line, sizeof(line), stdin) != NULL) {
 570                int len = strlen(line);
 571                if (len && line[len - 1] == '\n')
 572                        line[--len] = 0;
 573                if (!len)
 574                        break;
 575                if (line[0] == '-')
 576                        die("options not supported in --stdin mode");
 577                if (handle_revision_arg(line, revs, 0, 1))
 578                        die("bad revision '%s'", line);
 579        }
 580}
 581
 582int cmd_rev_list(int argc, const char **argv, const char *prefix)
 583{
 584        struct commit_list *list;
 585        int i;
 586        int read_from_stdin = 0;
 587        int bisect_show_vars = 0;
 588        int bisect_find_all = 0;
 589        int quiet = 0;
 590
 591        git_config(git_default_config);
 592        init_revisions(&revs, prefix);
 593        revs.abbrev = 0;
 594        revs.commit_format = CMIT_FMT_UNSPECIFIED;
 595        argc = setup_revisions(argc, argv, &revs, NULL);
 596
 597        for (i = 1 ; i < argc; i++) {
 598                const char *arg = argv[i];
 599
 600                if (!strcmp(arg, "--header")) {
 601                        revs.verbose_header = 1;
 602                        continue;
 603                }
 604                if (!strcmp(arg, "--timestamp")) {
 605                        show_timestamp = 1;
 606                        continue;
 607                }
 608                if (!strcmp(arg, "--bisect")) {
 609                        bisect_list = 1;
 610                        continue;
 611                }
 612                if (!strcmp(arg, "--bisect-all")) {
 613                        bisect_list = 1;
 614                        bisect_find_all = 1;
 615                        continue;
 616                }
 617                if (!strcmp(arg, "--bisect-vars")) {
 618                        bisect_list = 1;
 619                        bisect_show_vars = 1;
 620                        continue;
 621                }
 622                if (!strcmp(arg, "--stdin")) {
 623                        if (read_from_stdin++)
 624                                die("--stdin given twice?");
 625                        read_revisions_from_stdin(&revs);
 626                        continue;
 627                }
 628                if (!strcmp(arg, "--quiet")) {
 629                        quiet = 1;
 630                        continue;
 631                }
 632                usage(rev_list_usage);
 633
 634        }
 635        if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
 636                /* The command line has a --pretty  */
 637                hdr_termination = '\n';
 638                if (revs.commit_format == CMIT_FMT_ONELINE)
 639                        header_prefix = "";
 640                else
 641                        header_prefix = "commit ";
 642        }
 643        else if (revs.verbose_header)
 644                /* Only --header was specified */
 645                revs.commit_format = CMIT_FMT_RAW;
 646
 647        list = revs.commits;
 648
 649        if ((!list &&
 650             (!(revs.tag_objects||revs.tree_objects||revs.blob_objects) &&
 651              !revs.pending.nr)) ||
 652            revs.diff)
 653                usage(rev_list_usage);
 654
 655        save_commit_buffer = revs.verbose_header || revs.grep_filter;
 656        if (bisect_list)
 657                revs.limited = 1;
 658
 659        if (prepare_revision_walk(&revs))
 660                die("revision walk setup failed");
 661        if (revs.tree_objects)
 662                mark_edges_uninteresting(revs.commits, &revs, show_edge);
 663
 664        if (bisect_list) {
 665                int reaches = reaches, all = all;
 666
 667                revs.commits = find_bisection(revs.commits, &reaches, &all,
 668                                              bisect_find_all);
 669                if (bisect_show_vars) {
 670                        int cnt;
 671                        char hex[41];
 672                        if (!revs.commits)
 673                                return 1;
 674                        /*
 675                         * revs.commits can reach "reaches" commits among
 676                         * "all" commits.  If it is good, then there are
 677                         * (all-reaches) commits left to be bisected.
 678                         * On the other hand, if it is bad, then the set
 679                         * to bisect is "reaches".
 680                         * A bisect set of size N has (N-1) commits further
 681                         * to test, as we already know one bad one.
 682                         */
 683                        cnt = all - reaches;
 684                        if (cnt < reaches)
 685                                cnt = reaches;
 686                        strcpy(hex, sha1_to_hex(revs.commits->item->object.sha1));
 687
 688                        if (bisect_find_all) {
 689                                traverse_commit_list(&revs, show_commit, show_object);
 690                                printf("------\n");
 691                        }
 692
 693                        printf("bisect_rev=%s\n"
 694                               "bisect_nr=%d\n"
 695                               "bisect_good=%d\n"
 696                               "bisect_bad=%d\n"
 697                               "bisect_all=%d\n",
 698                               hex,
 699                               cnt - 1,
 700                               all - reaches - 1,
 701                               reaches - 1,
 702                               all);
 703                        return 0;
 704                }
 705        }
 706
 707        traverse_commit_list(&revs,
 708                quiet ? finish_commit : show_commit,
 709                quiet ? finish_object : show_object);
 710
 711        return 0;
 712}