rev-list.con commit git-rev-list: remove the DUPCHECK logic, use SEEN instead (960cea2)
   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        "usage: 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                      "  --bisect\n"
  19                      "  --objects\n"
  20                      "  --unpacked\n"
  21                      "  --header\n"
  22                      "  --pretty\n"
  23                      "  --merge-order [ --show-breaks ]";
  24
  25static int unpacked = 0;
  26static int bisect_list = 0;
  27static int tag_objects = 0;
  28static int tree_objects = 0;
  29static int blob_objects = 0;
  30static int verbose_header = 0;
  31static int show_parents = 0;
  32static int hdr_termination = 0;
  33static const char *prefix = "";
  34static unsigned long max_age = -1;
  35static unsigned long min_age = -1;
  36static int max_count = -1;
  37static enum cmit_fmt commit_format = CMIT_FMT_RAW;
  38static int merge_order = 0;
  39static int show_breaks = 0;
  40static int stop_traversal = 0;
  41static int topo_order = 0;
  42
  43static void show_commit(struct commit *commit)
  44{
  45        commit->object.flags |= SHOWN;
  46        if (show_breaks) {
  47                prefix = "| ";
  48                if (commit->object.flags & DISCONTINUITY) {
  49                        prefix = "^ ";     
  50                } else if (commit->object.flags & BOUNDARY) {
  51                        prefix = "= ";
  52                } 
  53        }                       
  54        printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
  55        if (show_parents) {
  56                struct commit_list *parents = commit->parents;
  57                while (parents) {
  58                        printf(" %s", sha1_to_hex(parents->item->object.sha1));
  59                        parents = parents->next;
  60                }
  61        }
  62        putchar('\n');
  63        if (verbose_header) {
  64                static char pretty_header[16384];
  65                pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
  66                printf("%s%c", pretty_header, hdr_termination);
  67        }
  68        fflush(stdout);
  69}
  70
  71static int filter_commit(struct commit * commit)
  72{
  73        if (stop_traversal && (commit->object.flags & BOUNDARY))
  74                return STOP;
  75        if (commit->object.flags & (UNINTERESTING|SHOWN))
  76                return CONTINUE;
  77        if (min_age != -1 && (commit->date > min_age))
  78                return CONTINUE;
  79        if (max_age != -1 && (commit->date < max_age)) {
  80                stop_traversal=1;
  81                return merge_order?CONTINUE:STOP;
  82        }
  83        if (max_count != -1 && !max_count--)
  84                return STOP;
  85        return DO;
  86}
  87
  88static int process_commit(struct commit * commit)
  89{
  90        int action=filter_commit(commit);
  91
  92        if (action == STOP) {
  93                return STOP;
  94        }
  95
  96        if (action == CONTINUE) {
  97                return CONTINUE;
  98        }
  99
 100        show_commit(commit);
 101
 102        return CONTINUE;
 103}
 104
 105static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
 106{
 107        struct object_list *entry = xmalloc(sizeof(*entry));
 108        entry->item = obj;
 109        entry->next = *p;
 110        entry->name = name;
 111        *p = entry;
 112        return &entry->next;
 113}
 114
 115static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
 116{
 117        struct object *obj = &blob->object;
 118
 119        if (!blob_objects)
 120                return p;
 121        if (obj->flags & (UNINTERESTING | SEEN))
 122                return p;
 123        obj->flags |= SEEN;
 124        return add_object(obj, p, name);
 125}
 126
 127static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
 128{
 129        struct object *obj = &tree->object;
 130        struct tree_entry_list *entry;
 131
 132        if (!tree_objects)
 133                return p;
 134        if (obj->flags & (UNINTERESTING | SEEN))
 135                return p;
 136        if (parse_tree(tree) < 0)
 137                die("bad tree object %s", sha1_to_hex(obj->sha1));
 138        obj->flags |= SEEN;
 139        p = add_object(obj, p, name);
 140        for (entry = tree->entries ; entry ; entry = entry->next) {
 141                if (entry->directory)
 142                        p = process_tree(entry->item.tree, p, entry->name);
 143                else
 144                        p = process_blob(entry->item.blob, p, entry->name);
 145        }
 146        return p;
 147}
 148
 149static struct object_list *pending_objects = NULL;
 150
 151static void show_commit_list(struct commit_list *list)
 152{
 153        struct object_list *objects = NULL, **p = &objects, *pending;
 154        while (list) {
 155                struct commit *commit = pop_most_recent_commit(&list, SEEN);
 156
 157                p = process_tree(commit->tree, p, "");
 158                if (process_commit(commit) == STOP)
 159                        break;
 160        }
 161        for (pending = pending_objects; pending; pending = pending->next) {
 162                struct object *obj = pending->item;
 163                const char *name = pending->name;
 164                if (obj->flags & (UNINTERESTING | SEEN))
 165                        continue;
 166                if (obj->type == tag_type) {
 167                        obj->flags |= SEEN;
 168                        p = add_object(obj, p, name);
 169                        continue;
 170                }
 171                if (obj->type == tree_type) {
 172                        p = process_tree((struct tree *)obj, p, name);
 173                        continue;
 174                }
 175                if (obj->type == blob_type) {
 176                        p = process_blob((struct blob *)obj, p, name);
 177                        continue;
 178                }
 179                die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
 180        }
 181        while (objects) {
 182                printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
 183                objects = objects->next;
 184        }
 185}
 186
 187static void mark_blob_uninteresting(struct blob *blob)
 188{
 189        if (!blob_objects)
 190                return;
 191        if (blob->object.flags & UNINTERESTING)
 192                return;
 193        blob->object.flags |= UNINTERESTING;
 194}
 195
 196static void mark_tree_uninteresting(struct tree *tree)
 197{
 198        struct object *obj = &tree->object;
 199        struct tree_entry_list *entry;
 200
 201        if (!tree_objects)
 202                return;
 203        if (obj->flags & UNINTERESTING)
 204                return;
 205        obj->flags |= UNINTERESTING;
 206        if (parse_tree(tree) < 0)
 207                die("bad tree %s", sha1_to_hex(obj->sha1));
 208        entry = tree->entries;
 209        while (entry) {
 210                if (entry->directory)
 211                        mark_tree_uninteresting(entry->item.tree);
 212                else
 213                        mark_blob_uninteresting(entry->item.blob);
 214                entry = entry->next;
 215        }
 216}
 217
 218static void mark_parents_uninteresting(struct commit *commit)
 219{
 220        struct commit_list *parents = commit->parents;
 221
 222        if (tree_objects)
 223                mark_tree_uninteresting(commit->tree);
 224        while (parents) {
 225                struct commit *commit = parents->item;
 226                commit->object.flags |= UNINTERESTING;
 227                parents = parents->next;
 228        }
 229}
 230
 231static int everybody_uninteresting(struct commit_list *list)
 232{
 233        while (list) {
 234                struct commit *commit = list->item;
 235                list = list->next;
 236                if (commit->object.flags & UNINTERESTING)
 237                        continue;
 238                return 0;
 239        }
 240        return 1;
 241}
 242
 243/*
 244 * This is a truly stupid algorithm, but it's only
 245 * used for bisection, and we just don't care enough.
 246 *
 247 * We care just barely enough to avoid recursing for
 248 * non-merge entries.
 249 */
 250static int count_distance(struct commit_list *entry)
 251{
 252        int nr = 0;
 253
 254        while (entry) {
 255                struct commit *commit = entry->item;
 256                struct commit_list *p;
 257
 258                if (commit->object.flags & (UNINTERESTING | COUNTED))
 259                        break;
 260                nr++;
 261                commit->object.flags |= COUNTED;
 262                p = commit->parents;
 263                entry = p;
 264                if (p) {
 265                        p = p->next;
 266                        while (p) {
 267                                nr += count_distance(p);
 268                                p = p->next;
 269                        }
 270                }
 271        }
 272        return nr;
 273}
 274
 275static void clear_distance(struct commit_list *list)
 276{
 277        while (list) {
 278                struct commit *commit = list->item;
 279                commit->object.flags &= ~COUNTED;
 280                list = list->next;
 281        }
 282}
 283
 284static struct commit_list *find_bisection(struct commit_list *list)
 285{
 286        int nr, closest;
 287        struct commit_list *p, *best;
 288
 289        nr = 0;
 290        p = list;
 291        while (p) {
 292                nr++;
 293                p = p->next;
 294        }
 295        closest = 0;
 296        best = list;
 297
 298        p = list;
 299        while (p) {
 300                int distance = count_distance(p);
 301                clear_distance(list);
 302                if (nr - distance < distance)
 303                        distance = nr - distance;
 304                if (distance > closest) {
 305                        best = p;
 306                        closest = distance;
 307                }
 308                p = p->next;
 309        }
 310        if (best)
 311                best->next = NULL;
 312        return best;
 313}
 314
 315static struct commit_list *limit_list(struct commit_list *list)
 316{
 317        struct commit_list *newlist = NULL;
 318        struct commit_list **p = &newlist;
 319        while (list) {
 320                struct commit *commit = pop_most_recent_commit(&list, SEEN);
 321                struct object *obj = &commit->object;
 322
 323                if (unpacked && has_sha1_pack(obj->sha1))
 324                        obj->flags |= UNINTERESTING;
 325                if (obj->flags & UNINTERESTING) {
 326                        mark_parents_uninteresting(commit);
 327                        if (everybody_uninteresting(list))
 328                                break;
 329                        continue;
 330                }
 331                p = &commit_list_insert(commit, p)->next;
 332        }
 333        if (bisect_list)
 334                newlist = find_bisection(newlist);
 335        return newlist;
 336}
 337
 338static void add_pending_object(struct object *obj, const char *name)
 339{
 340        add_object(obj, &pending_objects, name);
 341}
 342
 343static struct commit *get_commit_reference(const char *name, unsigned int flags)
 344{
 345        unsigned char sha1[20];
 346        struct object *object;
 347
 348        if (get_sha1(name, sha1))
 349                usage(rev_list_usage);
 350        object = parse_object(sha1);
 351        if (!object)
 352                die("bad object %s", name);
 353
 354        /*
 355         * Tag object? Look what it points to..
 356         */
 357        if (object->type == tag_type) {
 358                struct tag *tag = (struct tag *) object;
 359                object->flags |= flags;
 360                if (tag_objects && !(object->flags & UNINTERESTING))
 361                        add_pending_object(object, tag->tag);
 362                object = tag->tagged;
 363        }
 364
 365        /*
 366         * Commit object? Just return it, we'll do all the complex
 367         * reachability crud.
 368         */
 369        if (object->type == commit_type) {
 370                struct commit *commit = (struct commit *)object;
 371                object->flags |= flags;
 372                if (parse_commit(commit) < 0)
 373                        die("unable to parse commit %s", name);
 374                return commit;
 375        }
 376
 377        /*
 378         * Tree object? Either mark it uniniteresting, or add it
 379         * to the list of objects to look at later..
 380         */
 381        if (object->type == tree_type) {
 382                struct tree *tree = (struct tree *)object;
 383                if (!tree_objects)
 384                        return NULL;
 385                if (flags & UNINTERESTING) {
 386                        mark_tree_uninteresting(tree);
 387                        return NULL;
 388                }
 389                add_pending_object(object, "");
 390                return NULL;
 391        }
 392
 393        /*
 394         * Blob object? You know the drill by now..
 395         */
 396        if (object->type == blob_type) {
 397                struct blob *blob = (struct blob *)object;
 398                if (!blob_objects)
 399                        return NULL;
 400                if (flags & UNINTERESTING) {
 401                        mark_blob_uninteresting(blob);
 402                        return NULL;
 403                }
 404                add_pending_object(object, "");
 405                return NULL;
 406        }
 407        die("%s is unknown object", name);
 408}
 409
 410int main(int argc, char **argv)
 411{
 412        struct commit_list *list = NULL;
 413        struct commit_list *(*insert)(struct commit *, struct commit_list **);
 414        int i, limited = 0;
 415
 416        insert = insert_by_date;
 417        for (i = 1 ; i < argc; i++) {
 418                int flags;
 419                char *arg = argv[i];
 420                struct commit *commit;
 421
 422                if (!strncmp(arg, "--max-count=", 12)) {
 423                        max_count = atoi(arg + 12);
 424                        continue;
 425                }
 426                if (!strncmp(arg, "--max-age=", 10)) {
 427                        max_age = atoi(arg + 10);
 428                        continue;
 429                }
 430                if (!strncmp(arg, "--min-age=", 10)) {
 431                        min_age = atoi(arg + 10);
 432                        continue;
 433                }
 434                if (!strcmp(arg, "--header")) {
 435                        verbose_header = 1;
 436                        continue;
 437                }
 438                if (!strncmp(arg, "--pretty", 8)) {
 439                        commit_format = get_commit_format(arg+8);
 440                        verbose_header = 1;
 441                        hdr_termination = '\n';
 442                        prefix = "commit ";
 443                        continue;
 444                }
 445                if (!strcmp(arg, "--parents")) {
 446                        show_parents = 1;
 447                        continue;
 448                }
 449                if (!strcmp(arg, "--bisect")) {
 450                        bisect_list = 1;
 451                        continue;
 452                }
 453                if (!strcmp(arg, "--objects")) {
 454                        tag_objects = 1;
 455                        tree_objects = 1;
 456                        blob_objects = 1;
 457                        continue;
 458                }
 459                if (!strcmp(arg, "--unpacked")) {
 460                        unpacked = 1;
 461                        limited = 1;
 462                        continue;
 463                }
 464                if (!strcmp(arg, "--merge-order")) {
 465                        merge_order = 1;
 466                        insert = commit_list_insert;
 467                        continue;
 468                }
 469                if (!strcmp(arg, "--show-breaks")) {
 470                        show_breaks = 1;
 471                        continue;
 472                }
 473                if (!strcmp(arg, "--topo-order")) {
 474                        topo_order = 1;
 475                        limited = 1;
 476                        continue;
 477                }
 478
 479                flags = 0;
 480                if (*arg == '^') {
 481                        flags = UNINTERESTING;
 482                        arg++;
 483                        limited = 1;
 484                }
 485                if (show_breaks && !merge_order)
 486                        usage(rev_list_usage);
 487                commit = get_commit_reference(arg, flags);
 488                if (!commit)
 489                        continue;
 490                if (commit->object.flags & SEEN)
 491                        continue;
 492                commit->object.flags |= SEEN;
 493                insert(commit, &list);
 494        }
 495
 496        if (!merge_order) {             
 497                if (limited)
 498                        list = limit_list(list);
 499                if (topo_order)
 500                        sort_in_topological_order(&list);
 501                show_commit_list(list);
 502        } else {
 503                if (sort_list_in_merge_order(list, &process_commit)) {
 504                          die("merge order sort failed\n");
 505                }
 506        }
 507
 508        return 0;
 509}