b5dbb9f4ec77cd5567e2f4ef90ab6a90ec83ab4d
   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 "epoch.h"
   8#include "diff.h"
   9
  10#define SEEN            (1u << 0)
  11#define INTERESTING     (1u << 1)
  12#define COUNTED         (1u << 2)
  13#define SHOWN           (1u << 3)
  14
  15static const char rev_list_usage[] =
  16        "git-rev-list [OPTION] commit-id <commit-id>\n"
  17                      "  --max-count=nr\n"
  18                      "  --max-age=epoch\n"
  19                      "  --min-age=epoch\n"
  20                      "  --parents\n"
  21                      "  --bisect\n"
  22                      "  --objects\n"
  23                      "  --unpacked\n"
  24                      "  --header\n"
  25                      "  --pretty\n"
  26                      "  --no-merges\n"
  27                      "  --merge-order [ --show-breaks ]\n"
  28                      "  --topo-order";
  29
  30static int unpacked = 0;
  31static int bisect_list = 0;
  32static int tag_objects = 0;
  33static int tree_objects = 0;
  34static int blob_objects = 0;
  35static int verbose_header = 0;
  36static int show_parents = 0;
  37static int hdr_termination = 0;
  38static const char *commit_prefix = "";
  39static unsigned long max_age = -1;
  40static unsigned long min_age = -1;
  41static int max_count = -1;
  42static enum cmit_fmt commit_format = CMIT_FMT_RAW;
  43static int merge_order = 0;
  44static int show_breaks = 0;
  45static int stop_traversal = 0;
  46static int topo_order = 0;
  47static int no_merges = 0;
  48static const char **paths = NULL;
  49
  50static void show_commit(struct commit *commit)
  51{
  52        commit->object.flags |= SHOWN;
  53        if (show_breaks) {
  54                commit_prefix = "| ";
  55                if (commit->object.flags & DISCONTINUITY) {
  56                        commit_prefix = "^ ";     
  57                } else if (commit->object.flags & BOUNDARY) {
  58                        commit_prefix = "= ";
  59                } 
  60        }                       
  61        printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
  62        if (show_parents) {
  63                struct commit_list *parents = commit->parents;
  64                while (parents) {
  65                        printf(" %s", sha1_to_hex(parents->item->object.sha1));
  66                        parents = parents->next;
  67                }
  68        }
  69        if (commit_format == CMIT_FMT_ONELINE)
  70                putchar(' ');
  71        else
  72                putchar('\n');
  73
  74        if (verbose_header) {
  75                static char pretty_header[16384];
  76                pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
  77                printf("%s%c", pretty_header, hdr_termination);
  78        }
  79        fflush(stdout);
  80}
  81
  82static int filter_commit(struct commit * commit)
  83{
  84        if (stop_traversal && (commit->object.flags & BOUNDARY))
  85                return STOP;
  86        if (commit->object.flags & (UNINTERESTING|SHOWN))
  87                return CONTINUE;
  88        if (min_age != -1 && (commit->date > min_age))
  89                return CONTINUE;
  90        if (max_age != -1 && (commit->date < max_age)) {
  91                stop_traversal=1;
  92                return CONTINUE;
  93        }
  94        if (max_count != -1 && !max_count--)
  95                return STOP;
  96        if (no_merges && (commit->parents && commit->parents->next))
  97                return CONTINUE;
  98        return DO;
  99}
 100
 101static int process_commit(struct commit * commit)
 102{
 103        int action=filter_commit(commit);
 104
 105        if (action == STOP) {
 106                return STOP;
 107        }
 108
 109        if (action == CONTINUE) {
 110                return CONTINUE;
 111        }
 112
 113        show_commit(commit);
 114
 115        return CONTINUE;
 116}
 117
 118static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
 119{
 120        struct object_list *entry = xmalloc(sizeof(*entry));
 121        entry->item = obj;
 122        entry->next = *p;
 123        entry->name = name;
 124        *p = entry;
 125        return &entry->next;
 126}
 127
 128static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
 129{
 130        struct object *obj = &blob->object;
 131
 132        if (!blob_objects)
 133                return p;
 134        if (obj->flags & (UNINTERESTING | SEEN))
 135                return p;
 136        obj->flags |= SEEN;
 137        return add_object(obj, p, name);
 138}
 139
 140static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
 141{
 142        struct object *obj = &tree->object;
 143        struct tree_entry_list *entry;
 144
 145        if (!tree_objects)
 146                return p;
 147        if (obj->flags & (UNINTERESTING | SEEN))
 148                return p;
 149        if (parse_tree(tree) < 0)
 150                die("bad tree object %s", sha1_to_hex(obj->sha1));
 151        obj->flags |= SEEN;
 152        p = add_object(obj, p, name);
 153        entry = tree->entries;
 154        tree->entries = NULL;
 155        while (entry) {
 156                struct tree_entry_list *next = entry->next;
 157                if (entry->directory)
 158                        p = process_tree(entry->item.tree, p, entry->name);
 159                else
 160                        p = process_blob(entry->item.blob, p, entry->name);
 161                free(entry);
 162                entry = next;
 163        }
 164        return p;
 165}
 166
 167static struct object_list *pending_objects = NULL;
 168
 169static void show_commit_list(struct commit_list *list)
 170{
 171        struct object_list *objects = NULL, **p = &objects, *pending;
 172        while (list) {
 173                struct commit *commit = pop_most_recent_commit(&list, SEEN);
 174
 175                p = process_tree(commit->tree, p, "");
 176                if (process_commit(commit) == STOP)
 177                        break;
 178        }
 179        for (pending = pending_objects; pending; pending = pending->next) {
 180                struct object *obj = pending->item;
 181                const char *name = pending->name;
 182                if (obj->flags & (UNINTERESTING | SEEN))
 183                        continue;
 184                if (obj->type == tag_type) {
 185                        obj->flags |= SEEN;
 186                        p = add_object(obj, p, name);
 187                        continue;
 188                }
 189                if (obj->type == tree_type) {
 190                        p = process_tree((struct tree *)obj, p, name);
 191                        continue;
 192                }
 193                if (obj->type == blob_type) {
 194                        p = process_blob((struct blob *)obj, p, name);
 195                        continue;
 196                }
 197                die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
 198        }
 199        while (objects) {
 200                /* An object with name "foo\n0000000000000000000000000000000000000000"
 201                 * can be used confuse downstream git-pack-objects very badly.
 202                 */
 203                const char *ep = strchr(objects->name, '\n');
 204                if (ep) {
 205                        printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
 206                               (int) (ep - objects->name),
 207                               objects->name);
 208                }
 209                else
 210                        printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
 211                objects = objects->next;
 212        }
 213}
 214
 215static void mark_blob_uninteresting(struct blob *blob)
 216{
 217        if (!blob_objects)
 218                return;
 219        if (blob->object.flags & UNINTERESTING)
 220                return;
 221        blob->object.flags |= UNINTERESTING;
 222}
 223
 224static void mark_tree_uninteresting(struct tree *tree)
 225{
 226        struct object *obj = &tree->object;
 227        struct tree_entry_list *entry;
 228
 229        if (!tree_objects)
 230                return;
 231        if (obj->flags & UNINTERESTING)
 232                return;
 233        obj->flags |= UNINTERESTING;
 234        if (!has_sha1_file(obj->sha1))
 235                return;
 236        if (parse_tree(tree) < 0)
 237                die("bad tree %s", sha1_to_hex(obj->sha1));
 238        entry = tree->entries;
 239        tree->entries = NULL;
 240        while (entry) {
 241                struct tree_entry_list *next = entry->next;
 242                if (entry->directory)
 243                        mark_tree_uninteresting(entry->item.tree);
 244                else
 245                        mark_blob_uninteresting(entry->item.blob);
 246                free(entry);
 247                entry = next;
 248        }
 249}
 250
 251static void mark_parents_uninteresting(struct commit *commit)
 252{
 253        struct commit_list *parents = commit->parents;
 254
 255        while (parents) {
 256                struct commit *commit = parents->item;
 257                commit->object.flags |= UNINTERESTING;
 258
 259                /*
 260                 * Normally we haven't parsed the parent
 261                 * yet, so we won't have a parent of a parent
 262                 * here. However, it may turn out that we've
 263                 * reached this commit some other way (where it
 264                 * wasn't uninteresting), in which case we need
 265                 * to mark its parents recursively too..
 266                 */
 267                if (commit->parents)
 268                        mark_parents_uninteresting(commit);
 269
 270                /*
 271                 * A missing commit is ok iff its parent is marked 
 272                 * uninteresting.
 273                 *
 274                 * We just mark such a thing parsed, so that when
 275                 * it is popped next time around, we won't be trying
 276                 * to parse it and get an error.
 277                 */
 278                if (!has_sha1_file(commit->object.sha1))
 279                        commit->object.parsed = 1;
 280                parents = parents->next;
 281        }
 282}
 283
 284static int everybody_uninteresting(struct commit_list *orig)
 285{
 286        struct commit_list *list = orig;
 287        while (list) {
 288                struct commit *commit = list->item;
 289                list = list->next;
 290                if (commit->object.flags & UNINTERESTING)
 291                        continue;
 292                return 0;
 293        }
 294        return 1;
 295}
 296
 297/*
 298 * This is a truly stupid algorithm, but it's only
 299 * used for bisection, and we just don't care enough.
 300 *
 301 * We care just barely enough to avoid recursing for
 302 * non-merge entries.
 303 */
 304static int count_distance(struct commit_list *entry)
 305{
 306        int nr = 0;
 307
 308        while (entry) {
 309                struct commit *commit = entry->item;
 310                struct commit_list *p;
 311
 312                if (commit->object.flags & (UNINTERESTING | COUNTED))
 313                        break;
 314                nr++;
 315                commit->object.flags |= COUNTED;
 316                p = commit->parents;
 317                entry = p;
 318                if (p) {
 319                        p = p->next;
 320                        while (p) {
 321                                nr += count_distance(p);
 322                                p = p->next;
 323                        }
 324                }
 325        }
 326        return nr;
 327}
 328
 329static void clear_distance(struct commit_list *list)
 330{
 331        while (list) {
 332                struct commit *commit = list->item;
 333                commit->object.flags &= ~COUNTED;
 334                list = list->next;
 335        }
 336}
 337
 338static struct commit_list *find_bisection(struct commit_list *list)
 339{
 340        int nr, closest;
 341        struct commit_list *p, *best;
 342
 343        nr = 0;
 344        p = list;
 345        while (p) {
 346                nr++;
 347                p = p->next;
 348        }
 349        closest = 0;
 350        best = list;
 351
 352        p = list;
 353        while (p) {
 354                int distance = count_distance(p);
 355                clear_distance(list);
 356                if (nr - distance < distance)
 357                        distance = nr - distance;
 358                if (distance > closest) {
 359                        best = p;
 360                        closest = distance;
 361                }
 362                p = p->next;
 363        }
 364        if (best)
 365                best->next = NULL;
 366        return best;
 367}
 368
 369static void mark_edges_uninteresting(struct commit_list *list)
 370{
 371        for ( ; list; list = list->next) {
 372                struct commit_list *parents = list->item->parents;
 373
 374                for ( ; parents; parents = parents->next) {
 375                        struct commit *commit = parents->item;
 376                        if (commit->object.flags & UNINTERESTING)
 377                                mark_tree_uninteresting(commit->tree);
 378                }
 379        }
 380}
 381
 382static int is_different = 0;
 383
 384static void file_add_remove(struct diff_options *options,
 385                    int addremove, unsigned mode,
 386                    const unsigned char *sha1,
 387                    const char *base, const char *path)
 388{
 389        is_different = 1;
 390}
 391
 392static void file_change(struct diff_options *options,
 393                 unsigned old_mode, unsigned new_mode,
 394                 const unsigned char *old_sha1,
 395                 const unsigned char *new_sha1,
 396                 const char *base, const char *path)
 397{
 398        is_different = 1;
 399}
 400
 401static struct diff_options diff_opt = {
 402        .recursive = 1,
 403        .add_remove = file_add_remove,
 404        .change = file_change,
 405};
 406
 407static struct commit *try_to_simplify_merge(struct commit *commit, struct commit_list *parent)
 408{
 409        if (!commit->tree)
 410                return NULL;
 411
 412        while (parent) {
 413                struct commit *p = parent->item;
 414                parent = parent->next;
 415                parse_commit(p);
 416                if (!p->tree)
 417                        continue;
 418                is_different = 0;
 419                if (diff_tree_sha1(commit->tree->object.sha1,
 420                                   p->tree->object.sha1, "", &diff_opt) < 0)
 421                        continue;
 422                if (!is_different)
 423                        return p;
 424        }
 425        return NULL;
 426}
 427
 428static void add_parents_to_list(struct commit *commit, struct commit_list **list)
 429{
 430        struct commit_list *parent = commit->parents;
 431
 432        /*
 433         * If the commit is uninteresting, don't try to
 434         * prune parents - we want the maximal uninteresting
 435         * set.
 436         *
 437         * Normally we haven't parsed the parent
 438         * yet, so we won't have a parent of a parent
 439         * here. However, it may turn out that we've
 440         * reached this commit some other way (where it
 441         * wasn't uninteresting), in which case we need
 442         * to mark its parents recursively too..
 443         */
 444        if (commit->object.flags & UNINTERESTING) {
 445                while (parent) {
 446                        struct commit *p = parent->item;
 447                        parent = parent->next;
 448                        parse_commit(p);
 449                        p->object.flags |= UNINTERESTING;
 450                        if (p->parents)
 451                                mark_parents_uninteresting(p);
 452                        if (p->object.flags & SEEN)
 453                                continue;
 454                        p->object.flags |= SEEN;
 455                        insert_by_date(p, list);
 456                }
 457                return;
 458        }
 459
 460        /*
 461         * Ok, the commit wasn't uninteresting. If it
 462         * is a merge, try to find the parent that has
 463         * no differences in the path set if one exists.
 464         */
 465        if (paths && parent && parent->next) {
 466                struct commit *preferred;
 467
 468                preferred = try_to_simplify_merge(commit, parent);
 469                if (preferred) {
 470                        parent->item = preferred;
 471                        parent->next = NULL;
 472                }
 473        }
 474
 475        while (parent) {
 476                struct commit *p = parent->item;
 477
 478                parent = parent->next;
 479
 480                parse_commit(p);
 481                if (p->object.flags & SEEN)
 482                        continue;
 483                p->object.flags |= SEEN;
 484                insert_by_date(p, list);
 485        }
 486}
 487
 488static struct commit_list *limit_list(struct commit_list *list)
 489{
 490        struct commit_list *newlist = NULL;
 491        struct commit_list **p = &newlist;
 492        while (list) {
 493                struct commit_list *entry = list;
 494                struct commit *commit = list->item;
 495                struct object *obj = &commit->object;
 496
 497                list = list->next;
 498                free(entry);
 499
 500                if (max_age != -1 && (commit->date < max_age))
 501                        obj->flags |= UNINTERESTING;
 502                if (unpacked && has_sha1_pack(obj->sha1))
 503                        obj->flags |= UNINTERESTING;
 504                add_parents_to_list(commit, &list);
 505                if (obj->flags & UNINTERESTING) {
 506                        mark_parents_uninteresting(commit);
 507                        if (everybody_uninteresting(list))
 508                                break;
 509                        continue;
 510                }
 511                if (min_age != -1 && (commit->date > min_age))
 512                        continue;
 513                p = &commit_list_insert(commit, p)->next;
 514        }
 515        if (tree_objects)
 516                mark_edges_uninteresting(newlist);
 517        if (bisect_list)
 518                newlist = find_bisection(newlist);
 519        return newlist;
 520}
 521
 522static void add_pending_object(struct object *obj, const char *name)
 523{
 524        add_object(obj, &pending_objects, name);
 525}
 526
 527static struct commit *get_commit_reference(const char *name, unsigned int flags)
 528{
 529        unsigned char sha1[20];
 530        struct object *object;
 531
 532        if (get_sha1(name, sha1))
 533                usage(rev_list_usage);
 534        object = parse_object(sha1);
 535        if (!object)
 536                die("bad object %s", name);
 537
 538        /*
 539         * Tag object? Look what it points to..
 540         */
 541        while (object->type == tag_type) {
 542                struct tag *tag = (struct tag *) object;
 543                object->flags |= flags;
 544                if (tag_objects && !(object->flags & UNINTERESTING))
 545                        add_pending_object(object, tag->tag);
 546                object = parse_object(tag->tagged->sha1);
 547                if (!object)
 548                        die("bad object %s", sha1_to_hex(tag->tagged->sha1));
 549        }
 550
 551        /*
 552         * Commit object? Just return it, we'll do all the complex
 553         * reachability crud.
 554         */
 555        if (object->type == commit_type) {
 556                struct commit *commit = (struct commit *)object;
 557                object->flags |= flags;
 558                if (parse_commit(commit) < 0)
 559                        die("unable to parse commit %s", name);
 560                if (flags & UNINTERESTING)
 561                        mark_parents_uninteresting(commit);
 562                return commit;
 563        }
 564
 565        /*
 566         * Tree object? Either mark it uniniteresting, or add it
 567         * to the list of objects to look at later..
 568         */
 569        if (object->type == tree_type) {
 570                struct tree *tree = (struct tree *)object;
 571                if (!tree_objects)
 572                        return NULL;
 573                if (flags & UNINTERESTING) {
 574                        mark_tree_uninteresting(tree);
 575                        return NULL;
 576                }
 577                add_pending_object(object, "");
 578                return NULL;
 579        }
 580
 581        /*
 582         * Blob object? You know the drill by now..
 583         */
 584        if (object->type == blob_type) {
 585                struct blob *blob = (struct blob *)object;
 586                if (!blob_objects)
 587                        return NULL;
 588                if (flags & UNINTERESTING) {
 589                        mark_blob_uninteresting(blob);
 590                        return NULL;
 591                }
 592                add_pending_object(object, "");
 593                return NULL;
 594        }
 595        die("%s is unknown object", name);
 596}
 597
 598static void handle_one_commit(struct commit *com, struct commit_list **lst)
 599{
 600        if (!com || com->object.flags & SEEN)
 601                return;
 602        com->object.flags |= SEEN;
 603        commit_list_insert(com, lst);
 604}
 605
 606/* for_each_ref() callback does not allow user data -- Yuck. */
 607static struct commit_list **global_lst;
 608
 609static int include_one_commit(const char *path, const unsigned char *sha1)
 610{
 611        struct commit *com = get_commit_reference(path, 0);
 612        handle_one_commit(com, global_lst);
 613        return 0;
 614}
 615
 616static void handle_all(struct commit_list **lst)
 617{
 618        global_lst = lst;
 619        for_each_ref(include_one_commit);
 620        global_lst = NULL;
 621}
 622
 623int main(int argc, const char **argv)
 624{
 625        const char *prefix = setup_git_directory();
 626        struct commit_list *list = NULL;
 627        int i, limited = 0;
 628
 629        for (i = 1 ; i < argc; i++) {
 630                int flags;
 631                const char *arg = argv[i];
 632                char *dotdot;
 633                struct commit *commit;
 634
 635                if (!strncmp(arg, "--max-count=", 12)) {
 636                        max_count = atoi(arg + 12);
 637                        continue;
 638                }
 639                if (!strncmp(arg, "--max-age=", 10)) {
 640                        max_age = atoi(arg + 10);
 641                        limited = 1;
 642                        continue;
 643                }
 644                if (!strncmp(arg, "--min-age=", 10)) {
 645                        min_age = atoi(arg + 10);
 646                        limited = 1;
 647                        continue;
 648                }
 649                if (!strcmp(arg, "--header")) {
 650                        verbose_header = 1;
 651                        continue;
 652                }
 653                if (!strncmp(arg, "--pretty", 8)) {
 654                        commit_format = get_commit_format(arg+8);
 655                        verbose_header = 1;
 656                        hdr_termination = '\n';
 657                        if (commit_format == CMIT_FMT_ONELINE)
 658                                commit_prefix = "";
 659                        else
 660                                commit_prefix = "commit ";
 661                        continue;
 662                }
 663                if (!strncmp(arg, "--no-merges", 11)) {
 664                        no_merges = 1;
 665                        continue;
 666                }
 667                if (!strcmp(arg, "--parents")) {
 668                        show_parents = 1;
 669                        continue;
 670                }
 671                if (!strcmp(arg, "--bisect")) {
 672                        bisect_list = 1;
 673                        continue;
 674                }
 675                if (!strcmp(arg, "--all")) {
 676                        handle_all(&list);
 677                        continue;
 678                }
 679                if (!strcmp(arg, "--objects")) {
 680                        tag_objects = 1;
 681                        tree_objects = 1;
 682                        blob_objects = 1;
 683                        continue;
 684                }
 685                if (!strcmp(arg, "--unpacked")) {
 686                        unpacked = 1;
 687                        limited = 1;
 688                        continue;
 689                }
 690                if (!strcmp(arg, "--merge-order")) {
 691                        merge_order = 1;
 692                        continue;
 693                }
 694                if (!strcmp(arg, "--show-breaks")) {
 695                        show_breaks = 1;
 696                        continue;
 697                }
 698                if (!strcmp(arg, "--topo-order")) {
 699                        topo_order = 1;
 700                        limited = 1;
 701                        continue;
 702                }
 703                if (!strcmp(arg, "--")) {
 704                        paths = get_pathspec(prefix, argv + i + 1);
 705                        if (paths) {
 706                                limited = 1;
 707                                diff_tree_setup_paths(paths);
 708                        }
 709                        break;
 710                }
 711
 712                if (show_breaks && !merge_order)
 713                        usage(rev_list_usage);
 714
 715                flags = 0;
 716                dotdot = strstr(arg, "..");
 717                if (dotdot) {
 718                        char *next = dotdot + 2;
 719                        struct commit *exclude = NULL;
 720                        struct commit *include = NULL;
 721                        *dotdot = 0;
 722                        if (!*next)
 723                                next = "HEAD";
 724                        exclude = get_commit_reference(arg, UNINTERESTING);
 725                        include = get_commit_reference(next, 0);
 726                        if (exclude && include) {
 727                                limited = 1;
 728                                handle_one_commit(exclude, &list);
 729                                handle_one_commit(include, &list);
 730                                continue;
 731                        }
 732                        *dotdot = '.';
 733                }
 734                if (*arg == '^') {
 735                        flags = UNINTERESTING;
 736                        arg++;
 737                        limited = 1;
 738                }
 739                commit = get_commit_reference(arg, flags);
 740                handle_one_commit(commit, &list);
 741        }
 742
 743        save_commit_buffer = verbose_header;
 744        track_object_refs = 0;
 745
 746        if (!merge_order) {             
 747                sort_by_date(&list);
 748                if (list && !limited && max_count == 1 &&
 749                    !tag_objects && !tree_objects && !blob_objects) {
 750                        show_commit(list->item);
 751                        return 0;
 752                }
 753                if (limited)
 754                        list = limit_list(list);
 755                if (topo_order)
 756                        sort_in_topological_order(&list);
 757                show_commit_list(list);
 758        } else {
 759#ifndef NO_OPENSSL
 760                if (sort_list_in_merge_order(list, &process_commit)) {
 761                        die("merge order sort failed\n");
 762                }
 763#else
 764                die("merge order sort unsupported, OpenSSL not linked");
 765#endif
 766        }
 767
 768        return 0;
 769}