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