6a383f853a1c747226b18c014be801d7b24d853b
   1#include "cache.h"
   2#include "commit.h"
   3#include "tree.h"
   4#include "blob.h"
   5#include "epoch.h"
   6
   7#define SEEN            (1u << 0)
   8#define INTERESTING     (1u << 1)
   9#define COUNTED         (1u << 2)
  10#define SHOWN           (LAST_EPOCH_FLAG << 2)
  11
  12static const char rev_list_usage[] =
  13        "usage: git-rev-list [OPTION] commit-id <commit-id>\n"
  14                      "  --max-count=nr\n"
  15                      "  --max-age=epoch\n"
  16                      "  --min-age=epoch\n"
  17                      "  --header\n"
  18                      "  --pretty\n"
  19                      "  --merge-order [ --show-breaks ]";
  20
  21static int bisect_list = 0;
  22static int tree_objects = 0;
  23static int blob_objects = 0;
  24static int verbose_header = 0;
  25static int show_parents = 0;
  26static int hdr_termination = 0;
  27static const char *prefix = "";
  28static unsigned long max_age = -1;
  29static unsigned long min_age = -1;
  30static int max_count = -1;
  31static enum cmit_fmt commit_format = CMIT_FMT_RAW;
  32static int merge_order = 0;
  33static int show_breaks = 0;
  34static int stop_traversal = 0;
  35
  36static void show_commit(struct commit *commit)
  37{
  38        commit->object.flags |= SHOWN;
  39        if (show_breaks) {
  40                prefix = "| ";
  41                if (commit->object.flags & DISCONTINUITY) {
  42                        prefix = "^ ";     
  43                } else if (commit->object.flags & BOUNDARY) {
  44                        prefix = "= ";
  45                } 
  46        }                       
  47        printf("%s%s", prefix, sha1_to_hex(commit->object.sha1));
  48        if (show_parents) {
  49                struct commit_list *parents = commit->parents;
  50                while (parents) {
  51                        printf(" %s", sha1_to_hex(parents->item->object.sha1));
  52                        parents = parents->next;
  53                }
  54        }
  55        putchar('\n');
  56        if (verbose_header) {
  57                static char pretty_header[16384];
  58                pretty_print_commit(commit_format, commit->buffer, ~0, pretty_header, sizeof(pretty_header));
  59                printf("%s%c", pretty_header, hdr_termination);
  60        }       
  61}
  62
  63static int filter_commit(struct commit * commit)
  64{
  65        if (merge_order && stop_traversal && commit->object.flags & BOUNDARY)
  66                return STOP;
  67        if (commit->object.flags & (UNINTERESTING|SHOWN))
  68                return CONTINUE;
  69        if (min_age != -1 && (commit->date > min_age))
  70                return CONTINUE;
  71        if (max_age != -1 && (commit->date < max_age)) {
  72                if (!merge_order)
  73                        return STOP;
  74                else {
  75                        stop_traversal = 1;
  76                        return CONTINUE;
  77                }
  78        }
  79        if (max_count != -1 && !max_count--)
  80                return STOP;
  81        return DO;
  82}
  83
  84static int process_commit(struct commit * commit)
  85{
  86        int action=filter_commit(commit);
  87
  88        if (action == STOP) {
  89                return STOP;
  90        }
  91
  92        if (action == CONTINUE) {
  93                return CONTINUE;
  94        }
  95
  96        show_commit(commit);
  97
  98        return CONTINUE;
  99}
 100
 101static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
 102{
 103        struct object_list *entry = xmalloc(sizeof(*entry));
 104        entry->item = obj;
 105        entry->next = NULL;
 106        entry->name = name;
 107        *p = entry;
 108        return &entry->next;
 109}
 110
 111static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
 112{
 113        struct object *obj = &blob->object;
 114
 115        if (!blob_objects)
 116                return p;
 117        if (obj->flags & (UNINTERESTING | SEEN))
 118                return p;
 119        obj->flags |= SEEN;
 120        return add_object(obj, p, name);
 121}
 122
 123static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
 124{
 125        struct object *obj = &tree->object;
 126        struct tree_entry_list *entry;
 127
 128        if (!tree_objects)
 129                return p;
 130        if (obj->flags & (UNINTERESTING | SEEN))
 131                return p;
 132        if (parse_tree(tree) < 0)
 133                die("bad tree object %s", sha1_to_hex(obj->sha1));
 134        obj->flags |= SEEN;
 135        p = add_object(obj, p, name);
 136        for (entry = tree->entries ; entry ; entry = entry->next) {
 137                if (entry->directory)
 138                        p = process_tree(entry->item.tree, p, entry->name);
 139                else
 140                        p = process_blob(entry->item.blob, p, entry->name);
 141        }
 142        return p;
 143}
 144
 145static void show_commit_list(struct commit_list *list)
 146{
 147        struct object_list *objects = NULL, **p = &objects;
 148        while (list) {
 149                struct commit *commit = pop_most_recent_commit(&list, SEEN);
 150
 151                p = process_tree(commit->tree, p, "");
 152                if (process_commit(commit) == STOP)
 153                        break;
 154        }
 155        while (objects) {
 156                printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
 157                objects = objects->next;
 158        }
 159}
 160
 161static void mark_blob_uninteresting(struct blob *blob)
 162{
 163        if (!blob_objects)
 164                return;
 165        if (blob->object.flags & UNINTERESTING)
 166                return;
 167        blob->object.flags |= UNINTERESTING;
 168}
 169
 170static void mark_tree_uninteresting(struct tree *tree)
 171{
 172        struct object *obj = &tree->object;
 173        struct tree_entry_list *entry;
 174
 175        if (!tree_objects)
 176                return;
 177        if (obj->flags & UNINTERESTING)
 178                return;
 179        obj->flags |= UNINTERESTING;
 180        if (parse_tree(tree) < 0)
 181                die("bad tree %s", sha1_to_hex(obj->sha1));
 182        entry = tree->entries;
 183        while (entry) {
 184                if (entry->directory)
 185                        mark_tree_uninteresting(entry->item.tree);
 186                else
 187                        mark_blob_uninteresting(entry->item.blob);
 188                entry = entry->next;
 189        }
 190}
 191
 192static void mark_parents_uninteresting(struct commit *commit)
 193{
 194        struct commit_list *parents = commit->parents;
 195
 196        if (tree_objects)
 197                mark_tree_uninteresting(commit->tree);
 198        while (parents) {
 199                struct commit *commit = parents->item;
 200                commit->object.flags |= UNINTERESTING;
 201                parents = parents->next;
 202        }
 203}
 204
 205static int everybody_uninteresting(struct commit_list *list)
 206{
 207        while (list) {
 208                struct commit *commit = list->item;
 209                list = list->next;
 210                if (commit->object.flags & UNINTERESTING)
 211                        continue;
 212                return 0;
 213        }
 214        return 1;
 215}
 216
 217/*
 218 * This is a truly stupid algorithm, but it's only
 219 * used for bisection, and we just don't care enough.
 220 *
 221 * We care just barely enough to avoid recursing for
 222 * non-merge entries.
 223 */
 224static int count_distance(struct commit_list *entry)
 225{
 226        int nr = 0;
 227
 228        while (entry) {
 229                struct commit *commit = entry->item;
 230                struct commit_list *p;
 231
 232                if (commit->object.flags & (UNINTERESTING | COUNTED))
 233                        break;
 234                nr++;
 235                commit->object.flags |= COUNTED;
 236                p = commit->parents;
 237                entry = p;
 238                if (p) {
 239                        p = p->next;
 240                        while (p) {
 241                                nr += count_distance(p);
 242                                p = p->next;
 243                        }
 244                }
 245        }
 246        return nr;
 247}
 248
 249static void clear_distance(struct commit_list *list)
 250{
 251        while (list) {
 252                struct commit *commit = list->item;
 253                commit->object.flags &= ~COUNTED;
 254                list = list->next;
 255        }
 256}
 257
 258static struct commit_list *find_bisection(struct commit_list *list)
 259{
 260        int nr, closest;
 261        struct commit_list *p, *best;
 262
 263        nr = 0;
 264        p = list;
 265        while (p) {
 266                nr++;
 267                p = p->next;
 268        }
 269        closest = 0;
 270        best = list;
 271
 272        p = list;
 273        while (p) {
 274                int distance = count_distance(p);
 275                clear_distance(list);
 276                if (nr - distance < distance)
 277                        distance = nr - distance;
 278                if (distance > closest) {
 279                        best = p;
 280                        closest = distance;
 281                }
 282                p = p->next;
 283        }
 284        if (best)
 285                best->next = NULL;
 286        return best;
 287}
 288
 289struct commit_list *limit_list(struct commit_list *list)
 290{
 291        struct commit_list *newlist = NULL;
 292        struct commit_list **p = &newlist;
 293        do {
 294                struct commit *commit = pop_most_recent_commit(&list, SEEN);
 295                struct object *obj = &commit->object;
 296
 297                if (obj->flags & UNINTERESTING) {
 298                        mark_parents_uninteresting(commit);
 299                        if (everybody_uninteresting(list))
 300                                break;
 301                        continue;
 302                }
 303                p = &commit_list_insert(commit, p)->next;
 304        } while (list);
 305        if (bisect_list)
 306                newlist = find_bisection(newlist);
 307        return newlist;
 308}
 309
 310static enum cmit_fmt get_commit_format(const char *arg)
 311{
 312        if (!*arg)
 313                return CMIT_FMT_DEFAULT;
 314        if (!strcmp(arg, "=raw"))
 315                return CMIT_FMT_RAW;
 316        if (!strcmp(arg, "=medium"))
 317                return CMIT_FMT_MEDIUM;
 318        if (!strcmp(arg, "=short"))
 319                return CMIT_FMT_SHORT;
 320        usage(rev_list_usage);  
 321}                       
 322
 323
 324int main(int argc, char **argv)
 325{
 326        struct commit_list *list = NULL;
 327        int i, limited = 0;
 328
 329        for (i = 1 ; i < argc; i++) {
 330                int flags;
 331                char *arg = argv[i];
 332                unsigned char sha1[20];
 333                struct commit *commit;
 334
 335                if (!strncmp(arg, "--max-count=", 12)) {
 336                        max_count = atoi(arg + 12);
 337                        continue;
 338                }
 339                if (!strncmp(arg, "--max-age=", 10)) {
 340                        max_age = atoi(arg + 10);
 341                        continue;
 342                }
 343                if (!strncmp(arg, "--min-age=", 10)) {
 344                        min_age = atoi(arg + 10);
 345                        continue;
 346                }
 347                if (!strcmp(arg, "--header")) {
 348                        verbose_header = 1;
 349                        continue;
 350                }
 351                if (!strncmp(arg, "--pretty", 8)) {
 352                        commit_format = get_commit_format(arg+8);
 353                        verbose_header = 1;
 354                        hdr_termination = '\n';
 355                        prefix = "commit ";
 356                        continue;
 357                }
 358                if (!strcmp(arg, "--parents")) {
 359                        show_parents = 1;
 360                        continue;
 361                }
 362                if (!strcmp(arg, "--bisect")) {
 363                        bisect_list = 1;
 364                        continue;
 365                }
 366                if (!strcmp(arg, "--objects")) {
 367                        tree_objects = 1;
 368                        blob_objects = 1;
 369                        continue;
 370                }
 371                if (!strncmp(arg, "--merge-order", 13)) {
 372                        merge_order = 1;
 373                        continue;
 374                }
 375                if (!strncmp(arg, "--show-breaks", 13)) {
 376                        show_breaks = 1;
 377                        continue;
 378                }
 379
 380                flags = 0;
 381                if (*arg == '^') {
 382                        flags = UNINTERESTING;
 383                        arg++;
 384                        limited = 1;
 385                }
 386                if (get_sha1(arg, sha1) || (show_breaks && !merge_order))
 387                        usage(rev_list_usage);
 388                commit = lookup_commit_reference(sha1);
 389                if (!commit || parse_commit(commit) < 0)
 390                        die("bad commit object %s", arg);
 391                commit->object.flags |= flags;
 392                commit_list_insert(commit, &list);
 393        }
 394
 395        if (!list)
 396                usage(rev_list_usage);
 397
 398        if (!merge_order) {             
 399                if (limited)
 400                        list = limit_list(list);
 401                show_commit_list(list);
 402        } else {
 403                if (sort_list_in_merge_order(list, &process_commit)) {
 404                          die("merge order sort failed\n");
 405                }
 406        }
 407
 408        return 0;
 409}