12df855cfa970971ff2340a9cd0ae7a1de044e7d
   1#include "cache.h"
   2#include "commit.h"
   3#include "diff.h"
   4#include "revision.h"
   5#include "refs.h"
   6#include "list-objects.h"
   7#include "quote.h"
   8#include "sha1-lookup.h"
   9#include "run-command.h"
  10#include "bisect.h"
  11
  12struct sha1_array {
  13        unsigned char (*sha1)[20];
  14        int sha1_nr;
  15        int sha1_alloc;
  16};
  17
  18static struct sha1_array skipped_revs;
  19
  20static const char **rev_argv;
  21static int rev_argv_nr;
  22static int rev_argv_alloc;
  23
  24static const unsigned char *current_bad_sha1;
  25
  26static const char *argv_diff_tree[] = {"diff-tree", "--pretty", NULL, NULL};
  27static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL};
  28static const char *argv_show_branch[] = {"show-branch", NULL, NULL};
  29
  30/* bits #0-15 in revision.h */
  31
  32#define COUNTED         (1u<<16)
  33
  34/*
  35 * This is a truly stupid algorithm, but it's only
  36 * used for bisection, and we just don't care enough.
  37 *
  38 * We care just barely enough to avoid recursing for
  39 * non-merge entries.
  40 */
  41static int count_distance(struct commit_list *entry)
  42{
  43        int nr = 0;
  44
  45        while (entry) {
  46                struct commit *commit = entry->item;
  47                struct commit_list *p;
  48
  49                if (commit->object.flags & (UNINTERESTING | COUNTED))
  50                        break;
  51                if (!(commit->object.flags & TREESAME))
  52                        nr++;
  53                commit->object.flags |= COUNTED;
  54                p = commit->parents;
  55                entry = p;
  56                if (p) {
  57                        p = p->next;
  58                        while (p) {
  59                                nr += count_distance(p);
  60                                p = p->next;
  61                        }
  62                }
  63        }
  64
  65        return nr;
  66}
  67
  68static void clear_distance(struct commit_list *list)
  69{
  70        while (list) {
  71                struct commit *commit = list->item;
  72                commit->object.flags &= ~COUNTED;
  73                list = list->next;
  74        }
  75}
  76
  77#define DEBUG_BISECT 0
  78
  79static inline int weight(struct commit_list *elem)
  80{
  81        return *((int*)(elem->item->util));
  82}
  83
  84static inline void weight_set(struct commit_list *elem, int weight)
  85{
  86        *((int*)(elem->item->util)) = weight;
  87}
  88
  89static int count_interesting_parents(struct commit *commit)
  90{
  91        struct commit_list *p;
  92        int count;
  93
  94        for (count = 0, p = commit->parents; p; p = p->next) {
  95                if (p->item->object.flags & UNINTERESTING)
  96                        continue;
  97                count++;
  98        }
  99        return count;
 100}
 101
 102static inline int halfway(struct commit_list *p, int nr)
 103{
 104        /*
 105         * Don't short-cut something we are not going to return!
 106         */
 107        if (p->item->object.flags & TREESAME)
 108                return 0;
 109        if (DEBUG_BISECT)
 110                return 0;
 111        /*
 112         * 2 and 3 are halfway of 5.
 113         * 3 is halfway of 6 but 2 and 4 are not.
 114         */
 115        switch (2 * weight(p) - nr) {
 116        case -1: case 0: case 1:
 117                return 1;
 118        default:
 119                return 0;
 120        }
 121}
 122
 123#if !DEBUG_BISECT
 124#define show_list(a,b,c,d) do { ; } while (0)
 125#else
 126static void show_list(const char *debug, int counted, int nr,
 127                      struct commit_list *list)
 128{
 129        struct commit_list *p;
 130
 131        fprintf(stderr, "%s (%d/%d)\n", debug, counted, nr);
 132
 133        for (p = list; p; p = p->next) {
 134                struct commit_list *pp;
 135                struct commit *commit = p->item;
 136                unsigned flags = commit->object.flags;
 137                enum object_type type;
 138                unsigned long size;
 139                char *buf = read_sha1_file(commit->object.sha1, &type, &size);
 140                char *ep, *sp;
 141
 142                fprintf(stderr, "%c%c%c ",
 143                        (flags & TREESAME) ? ' ' : 'T',
 144                        (flags & UNINTERESTING) ? 'U' : ' ',
 145                        (flags & COUNTED) ? 'C' : ' ');
 146                if (commit->util)
 147                        fprintf(stderr, "%3d", weight(p));
 148                else
 149                        fprintf(stderr, "---");
 150                fprintf(stderr, " %.*s", 8, sha1_to_hex(commit->object.sha1));
 151                for (pp = commit->parents; pp; pp = pp->next)
 152                        fprintf(stderr, " %.*s", 8,
 153                                sha1_to_hex(pp->item->object.sha1));
 154
 155                sp = strstr(buf, "\n\n");
 156                if (sp) {
 157                        sp += 2;
 158                        for (ep = sp; *ep && *ep != '\n'; ep++)
 159                                ;
 160                        fprintf(stderr, " %.*s", (int)(ep - sp), sp);
 161                }
 162                fprintf(stderr, "\n");
 163        }
 164}
 165#endif /* DEBUG_BISECT */
 166
 167static struct commit_list *best_bisection(struct commit_list *list, int nr)
 168{
 169        struct commit_list *p, *best;
 170        int best_distance = -1;
 171
 172        best = list;
 173        for (p = list; p; p = p->next) {
 174                int distance;
 175                unsigned flags = p->item->object.flags;
 176
 177                if (flags & TREESAME)
 178                        continue;
 179                distance = weight(p);
 180                if (nr - distance < distance)
 181                        distance = nr - distance;
 182                if (distance > best_distance) {
 183                        best = p;
 184                        best_distance = distance;
 185                }
 186        }
 187
 188        return best;
 189}
 190
 191struct commit_dist {
 192        struct commit *commit;
 193        int distance;
 194};
 195
 196static int compare_commit_dist(const void *a_, const void *b_)
 197{
 198        struct commit_dist *a, *b;
 199
 200        a = (struct commit_dist *)a_;
 201        b = (struct commit_dist *)b_;
 202        if (a->distance != b->distance)
 203                return b->distance - a->distance; /* desc sort */
 204        return hashcmp(a->commit->object.sha1, b->commit->object.sha1);
 205}
 206
 207static struct commit_list *best_bisection_sorted(struct commit_list *list, int nr)
 208{
 209        struct commit_list *p;
 210        struct commit_dist *array = xcalloc(nr, sizeof(*array));
 211        int cnt, i;
 212
 213        for (p = list, cnt = 0; p; p = p->next) {
 214                int distance;
 215                unsigned flags = p->item->object.flags;
 216
 217                if (flags & TREESAME)
 218                        continue;
 219                distance = weight(p);
 220                if (nr - distance < distance)
 221                        distance = nr - distance;
 222                array[cnt].commit = p->item;
 223                array[cnt].distance = distance;
 224                cnt++;
 225        }
 226        qsort(array, cnt, sizeof(*array), compare_commit_dist);
 227        for (p = list, i = 0; i < cnt; i++) {
 228                struct name_decoration *r = xmalloc(sizeof(*r) + 100);
 229                struct object *obj = &(array[i].commit->object);
 230
 231                sprintf(r->name, "dist=%d", array[i].distance);
 232                r->next = add_decoration(&name_decoration, obj, r);
 233                p->item = array[i].commit;
 234                p = p->next;
 235        }
 236        if (p)
 237                p->next = NULL;
 238        free(array);
 239        return list;
 240}
 241
 242/*
 243 * zero or positive weight is the number of interesting commits it can
 244 * reach, including itself.  Especially, weight = 0 means it does not
 245 * reach any tree-changing commits (e.g. just above uninteresting one
 246 * but traversal is with pathspec).
 247 *
 248 * weight = -1 means it has one parent and its distance is yet to
 249 * be computed.
 250 *
 251 * weight = -2 means it has more than one parent and its distance is
 252 * unknown.  After running count_distance() first, they will get zero
 253 * or positive distance.
 254 */
 255static struct commit_list *do_find_bisection(struct commit_list *list,
 256                                             int nr, int *weights,
 257                                             int find_all)
 258{
 259        int n, counted;
 260        struct commit_list *p;
 261
 262        counted = 0;
 263
 264        for (n = 0, p = list; p; p = p->next) {
 265                struct commit *commit = p->item;
 266                unsigned flags = commit->object.flags;
 267
 268                p->item->util = &weights[n++];
 269                switch (count_interesting_parents(commit)) {
 270                case 0:
 271                        if (!(flags & TREESAME)) {
 272                                weight_set(p, 1);
 273                                counted++;
 274                                show_list("bisection 2 count one",
 275                                          counted, nr, list);
 276                        }
 277                        /*
 278                         * otherwise, it is known not to reach any
 279                         * tree-changing commit and gets weight 0.
 280                         */
 281                        break;
 282                case 1:
 283                        weight_set(p, -1);
 284                        break;
 285                default:
 286                        weight_set(p, -2);
 287                        break;
 288                }
 289        }
 290
 291        show_list("bisection 2 initialize", counted, nr, list);
 292
 293        /*
 294         * If you have only one parent in the resulting set
 295         * then you can reach one commit more than that parent
 296         * can reach.  So we do not have to run the expensive
 297         * count_distance() for single strand of pearls.
 298         *
 299         * However, if you have more than one parents, you cannot
 300         * just add their distance and one for yourself, since
 301         * they usually reach the same ancestor and you would
 302         * end up counting them twice that way.
 303         *
 304         * So we will first count distance of merges the usual
 305         * way, and then fill the blanks using cheaper algorithm.
 306         */
 307        for (p = list; p; p = p->next) {
 308                if (p->item->object.flags & UNINTERESTING)
 309                        continue;
 310                if (weight(p) != -2)
 311                        continue;
 312                weight_set(p, count_distance(p));
 313                clear_distance(list);
 314
 315                /* Does it happen to be at exactly half-way? */
 316                if (!find_all && halfway(p, nr))
 317                        return p;
 318                counted++;
 319        }
 320
 321        show_list("bisection 2 count_distance", counted, nr, list);
 322
 323        while (counted < nr) {
 324                for (p = list; p; p = p->next) {
 325                        struct commit_list *q;
 326                        unsigned flags = p->item->object.flags;
 327
 328                        if (0 <= weight(p))
 329                                continue;
 330                        for (q = p->item->parents; q; q = q->next) {
 331                                if (q->item->object.flags & UNINTERESTING)
 332                                        continue;
 333                                if (0 <= weight(q))
 334                                        break;
 335                        }
 336                        if (!q)
 337                                continue;
 338
 339                        /*
 340                         * weight for p is unknown but q is known.
 341                         * add one for p itself if p is to be counted,
 342                         * otherwise inherit it from q directly.
 343                         */
 344                        if (!(flags & TREESAME)) {
 345                                weight_set(p, weight(q)+1);
 346                                counted++;
 347                                show_list("bisection 2 count one",
 348                                          counted, nr, list);
 349                        }
 350                        else
 351                                weight_set(p, weight(q));
 352
 353                        /* Does it happen to be at exactly half-way? */
 354                        if (!find_all && halfway(p, nr))
 355                                return p;
 356                }
 357        }
 358
 359        show_list("bisection 2 counted all", counted, nr, list);
 360
 361        if (!find_all)
 362                return best_bisection(list, nr);
 363        else
 364                return best_bisection_sorted(list, nr);
 365}
 366
 367struct commit_list *find_bisection(struct commit_list *list,
 368                                          int *reaches, int *all,
 369                                          int find_all)
 370{
 371        int nr, on_list;
 372        struct commit_list *p, *best, *next, *last;
 373        int *weights;
 374
 375        show_list("bisection 2 entry", 0, 0, list);
 376
 377        /*
 378         * Count the number of total and tree-changing items on the
 379         * list, while reversing the list.
 380         */
 381        for (nr = on_list = 0, last = NULL, p = list;
 382             p;
 383             p = next) {
 384                unsigned flags = p->item->object.flags;
 385
 386                next = p->next;
 387                if (flags & UNINTERESTING)
 388                        continue;
 389                p->next = last;
 390                last = p;
 391                if (!(flags & TREESAME))
 392                        nr++;
 393                on_list++;
 394        }
 395        list = last;
 396        show_list("bisection 2 sorted", 0, nr, list);
 397
 398        *all = nr;
 399        weights = xcalloc(on_list, sizeof(*weights));
 400
 401        /* Do the real work of finding bisection commit. */
 402        best = do_find_bisection(list, nr, weights, find_all);
 403        if (best) {
 404                if (!find_all)
 405                        best->next = NULL;
 406                *reaches = weight(best);
 407        }
 408        free(weights);
 409        return best;
 410}
 411
 412static int register_ref(const char *refname, const unsigned char *sha1,
 413                        int flags, void *cb_data)
 414{
 415        if (!strcmp(refname, "bad")) {
 416                ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
 417                current_bad_sha1 = sha1;
 418                rev_argv[rev_argv_nr++] = xstrdup(sha1_to_hex(sha1));
 419        } else if (!prefixcmp(refname, "good-")) {
 420                const char *hex = sha1_to_hex(sha1);
 421                char *good = xmalloc(strlen(hex) + 2);
 422                *good = '^';
 423                memcpy(good + 1, hex, strlen(hex) + 1);
 424                ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
 425                rev_argv[rev_argv_nr++] = good;
 426        } else if (!prefixcmp(refname, "skip-")) {
 427                ALLOC_GROW(skipped_revs.sha1, skipped_revs.sha1_nr + 1,
 428                           skipped_revs.sha1_alloc);
 429                hashcpy(skipped_revs.sha1[skipped_revs.sha1_nr++], sha1);
 430        }
 431
 432        return 0;
 433}
 434
 435static int read_bisect_refs(void)
 436{
 437        return for_each_ref_in("refs/bisect/", register_ref, NULL);
 438}
 439
 440void read_bisect_paths(void)
 441{
 442        struct strbuf str = STRBUF_INIT;
 443        const char *filename = git_path("BISECT_NAMES");
 444        FILE *fp = fopen(filename, "r");
 445
 446        if (!fp)
 447                die("Could not open file '%s': %s", filename, strerror(errno));
 448
 449        while (strbuf_getline(&str, fp, '\n') != EOF) {
 450                char *quoted;
 451                int res;
 452
 453                strbuf_trim(&str);
 454                quoted = strbuf_detach(&str, NULL);
 455                res = sq_dequote_to_argv(quoted, &rev_argv,
 456                                         &rev_argv_nr, &rev_argv_alloc);
 457                if (res)
 458                        die("Badly quoted content in file '%s': %s",
 459                            filename, quoted);
 460        }
 461
 462        strbuf_release(&str);
 463        fclose(fp);
 464}
 465
 466static int skipcmp(const void *a, const void *b)
 467{
 468        return hashcmp(a, b);
 469}
 470
 471static void prepare_skipped(void)
 472{
 473        qsort(skipped_revs.sha1, skipped_revs.sha1_nr,
 474              sizeof(*skipped_revs.sha1), skipcmp);
 475}
 476
 477static const unsigned char *skipped_sha1_access(size_t index, void *table)
 478{
 479        unsigned char (*skipped)[20] = table;
 480        return skipped[index];
 481}
 482
 483static int lookup_skipped(unsigned char *sha1)
 484{
 485        return sha1_pos(sha1, skipped_revs.sha1, skipped_revs.sha1_nr,
 486                        skipped_sha1_access);
 487}
 488
 489struct commit_list *filter_skipped(struct commit_list *list,
 490                                   struct commit_list **tried,
 491                                   int show_all)
 492{
 493        struct commit_list *filtered = NULL, **f = &filtered;
 494
 495        *tried = NULL;
 496
 497        if (!skipped_revs.sha1_nr)
 498                return list;
 499
 500        prepare_skipped();
 501
 502        while (list) {
 503                struct commit_list *next = list->next;
 504                list->next = NULL;
 505                if (0 <= lookup_skipped(list->item->object.sha1)) {
 506                        /* Move current to tried list */
 507                        *tried = list;
 508                        tried = &list->next;
 509                } else {
 510                        if (!show_all)
 511                                return list;
 512                        /* Move current to filtered list */
 513                        *f = list;
 514                        f = &list->next;
 515                }
 516                list = next;
 517        }
 518
 519        return filtered;
 520}
 521
 522static void bisect_rev_setup(struct rev_info *revs, const char *prefix)
 523{
 524        init_revisions(revs, prefix);
 525        revs->abbrev = 0;
 526        revs->commit_format = CMIT_FMT_UNSPECIFIED;
 527
 528        /* argv[0] will be ignored by setup_revisions */
 529        ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
 530        rev_argv[rev_argv_nr++] = xstrdup("bisect_rev_setup");
 531
 532        if (read_bisect_refs())
 533                die("reading bisect refs failed");
 534
 535        ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
 536        rev_argv[rev_argv_nr++] = xstrdup("--");
 537
 538        read_bisect_paths();
 539
 540        ALLOC_GROW(rev_argv, rev_argv_nr + 1, rev_argv_alloc);
 541        rev_argv[rev_argv_nr++] = NULL;
 542
 543        setup_revisions(rev_argv_nr, rev_argv, revs, NULL);
 544
 545        revs->limited = 1;
 546}
 547
 548static void bisect_common(struct rev_info *revs, const char *prefix,
 549                          int *reaches, int *all)
 550{
 551        bisect_rev_setup(revs, prefix);
 552
 553        if (prepare_revision_walk(revs))
 554                die("revision walk setup failed");
 555        if (revs->tree_objects)
 556                mark_edges_uninteresting(revs->commits, revs, NULL);
 557
 558        revs->commits = find_bisection(revs->commits, reaches, all,
 559                                       !!skipped_revs.sha1_nr);
 560}
 561
 562static void exit_if_skipped_commits(struct commit_list *tried,
 563                                    const unsigned char *bad)
 564{
 565        if (!tried)
 566                return;
 567
 568        printf("There are only 'skip'ped commits left to test.\n"
 569               "The first bad commit could be any of:\n");
 570        print_commit_list(tried, "%s\n", "%s\n");
 571        if (bad)
 572                printf("%s\n", sha1_to_hex(bad));
 573        printf("We cannot bisect more!\n");
 574        exit(2);
 575}
 576
 577static void mark_expected_rev(char *bisect_rev_hex)
 578{
 579        int len = strlen(bisect_rev_hex);
 580        const char *filename = git_path("BISECT_EXPECTED_REV");
 581        int fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
 582
 583        if (fd < 0)
 584                die("could not create file '%s': %s",
 585                    filename, strerror(errno));
 586
 587        bisect_rev_hex[len] = '\n';
 588        write_or_die(fd, bisect_rev_hex, len + 1);
 589        bisect_rev_hex[len] = '\0';
 590
 591        if (close(fd) < 0)
 592                die("closing file %s: %s", filename, strerror(errno));
 593}
 594
 595static int bisect_checkout(char *bisect_rev_hex)
 596{
 597        int res;
 598
 599        mark_expected_rev(bisect_rev_hex);
 600
 601        argv_checkout[2] = bisect_rev_hex;
 602        res = run_command_v_opt(argv_checkout, RUN_GIT_CMD);
 603        if (res)
 604                exit(res);
 605
 606        argv_show_branch[1] = bisect_rev_hex;
 607        return run_command_v_opt(argv_show_branch, RUN_GIT_CMD);
 608}
 609
 610/*
 611 * We use the convention that exiting with an exit code 10 means that
 612 * the bisection process finished successfully.
 613 * In this case the calling shell script should exit 0.
 614 */
 615int bisect_next_exit(const char *prefix)
 616{
 617        struct rev_info revs;
 618        struct commit_list *tried;
 619        int reaches = 0, all = 0, nr;
 620        const unsigned char *bisect_rev;
 621        char bisect_rev_hex[41];
 622
 623        bisect_common(&revs, prefix, &reaches, &all);
 624
 625        revs.commits = filter_skipped(revs.commits, &tried, 0);
 626
 627        if (!revs.commits) {
 628                /*
 629                 * We should exit here only if the "bad"
 630                 * commit is also a "skip" commit.
 631                 */
 632                exit_if_skipped_commits(tried, NULL);
 633
 634                printf("%s was both good and bad\n",
 635                       sha1_to_hex(current_bad_sha1));
 636                exit(1);
 637        }
 638
 639        bisect_rev = revs.commits->item->object.sha1;
 640        memcpy(bisect_rev_hex, sha1_to_hex(bisect_rev), 41);
 641
 642        if (!hashcmp(bisect_rev, current_bad_sha1)) {
 643                exit_if_skipped_commits(tried, current_bad_sha1);
 644                printf("%s is first bad commit\n", bisect_rev_hex);
 645                argv_diff_tree[2] = bisect_rev_hex;
 646                run_command_v_opt(argv_diff_tree, RUN_GIT_CMD);
 647                /* This means the bisection process succeeded. */
 648                exit(10);
 649        }
 650
 651        nr = all - reaches - 1;
 652        printf("Bisecting: %d revisions left to test after this "
 653               "(roughly %d steps)\n", nr, estimate_bisect_steps(all));
 654
 655        return bisect_checkout(bisect_rev_hex);
 656}
 657