]> git.ipfire.org Git - thirdparty/git.git/blame - rev-list.c
delta micro optimization
[thirdparty/git.git] / rev-list.c
CommitLineData
64745109 1#include "cache.h"
e091eb93 2#include "refs.h"
36f8d174 3#include "tag.h"
64745109 4#include "commit.h"
9de48752
LT
5#include "tree.h"
6#include "blob.h"
a3437b8c 7#include "epoch.h"
cf484544 8#include "diff.h"
64745109 9
8906300f
LT
10#define SEEN (1u << 0)
11#define INTERESTING (1u << 1)
8b3a1e05 12#define COUNTED (1u << 2)
bce62866 13#define SHOWN (1u << 3)
1b9e059d 14#define TREECHANGE (1u << 4)
88494423 15#define TMP_MARK (1u << 5) /* for isolated cases; clean after use */
8906300f 16
a6f68d47 17static const char rev_list_usage[] =
69e0c256
JH
18"git-rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
19" limiting output:\n"
20" --max-count=nr\n"
21" --max-age=epoch\n"
22" --min-age=epoch\n"
23" --sparse\n"
24" --no-merges\n"
93b74bca 25" --remove-empty\n"
69e0c256
JH
26" --all\n"
27" ordering output:\n"
28" --merge-order [ --show-breaks ]\n"
29" --topo-order\n"
30" formatting output:\n"
31" --parents\n"
32" --objects\n"
33" --unpacked\n"
34" --header | --pretty\n"
35" special purpose:\n"
36" --bisect"
37;
a6f68d47 38
7b34c2fa 39static int dense = 1;
12d2a187 40static int unpacked = 0;
8b3a1e05 41static int bisect_list = 0;
3c90f03d 42static int tag_objects = 0;
9de48752
LT
43static int tree_objects = 0;
44static int blob_objects = 0;
81f2bb1f
LT
45static int verbose_header = 0;
46static int show_parents = 0;
81f2bb1f 47static int hdr_termination = 0;
d998a089 48static const char *commit_prefix = "";
81f2bb1f
LT
49static unsigned long max_age = -1;
50static unsigned long min_age = -1;
51static int max_count = -1;
000182ea 52static enum cmit_fmt commit_format = CMIT_FMT_RAW;
a3437b8c
JS
53static int merge_order = 0;
54static int show_breaks = 0;
5e749e25 55static int stop_traversal = 0;
d2d02a49 56static int topo_order = 0;
76cd8eb6 57static int no_merges = 0;
cf484544 58static const char **paths = NULL;
461cf59f 59static int remove_empty_trees = 0;
81f2bb1f
LT
60
61static void show_commit(struct commit *commit)
62{
51b1e171 63 commit->object.flags |= SHOWN;
a3437b8c 64 if (show_breaks) {
d998a089 65 commit_prefix = "| ";
a3437b8c 66 if (commit->object.flags & DISCONTINUITY) {
d998a089 67 commit_prefix = "^ ";
a3437b8c 68 } else if (commit->object.flags & BOUNDARY) {
d998a089 69 commit_prefix = "= ";
a3437b8c
JS
70 }
71 }
d998a089 72 printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
81f2bb1f
LT
73 if (show_parents) {
74 struct commit_list *parents = commit->parents;
75 while (parents) {
88494423 76 struct object *o = &(parents->item->object);
81f2bb1f 77 parents = parents->next;
88494423
JH
78 if (o->flags & TMP_MARK)
79 continue;
80 printf(" %s", sha1_to_hex(o->sha1));
81 o->flags |= TMP_MARK;
81f2bb1f 82 }
88494423
JH
83 /* TMP_MARK is a general purpose flag that can
84 * be used locally, but the user should clean
85 * things up after it is done with them.
86 */
87 for (parents = commit->parents;
88 parents;
89 parents = parents->next)
90 parents->item->object.flags &= ~TMP_MARK;
81f2bb1f 91 }
d87449c5
JH
92 if (commit_format == CMIT_FMT_ONELINE)
93 putchar(' ');
94 else
95 putchar('\n');
96
81f2bb1f 97 if (verbose_header) {
000182ea 98 static char pretty_header[16384];
3815f423 99 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), 0);
000182ea 100 printf("%s%c", pretty_header, hdr_termination);
7620d39f
LT
101 }
102 fflush(stdout);
a3437b8c
JS
103}
104
129adf4d 105static int rewrite_one(struct commit **pp)
1b9e059d
LT
106{
107 for (;;) {
108 struct commit *p = *pp;
109 if (p->object.flags & (TREECHANGE | UNINTERESTING))
129adf4d
LT
110 return 0;
111 if (!p->parents)
112 return -1;
1b9e059d
LT
113 *pp = p->parents->item;
114 }
115}
116
117static void rewrite_parents(struct commit *commit)
118{
129adf4d
LT
119 struct commit_list **pp = &commit->parents;
120 while (*pp) {
121 struct commit_list *parent = *pp;
122 if (rewrite_one(&parent->item) < 0) {
123 *pp = parent->next;
124 continue;
125 }
126 pp = &parent->next;
1b9e059d
LT
127 }
128}
129
a3437b8c
JS
130static int filter_commit(struct commit * commit)
131{
d2775a81 132 if (stop_traversal && (commit->object.flags & BOUNDARY))
5e749e25 133 return STOP;
51b1e171 134 if (commit->object.flags & (UNINTERESTING|SHOWN))
a3437b8c
JS
135 return CONTINUE;
136 if (min_age != -1 && (commit->date > min_age))
137 return CONTINUE;
5e749e25 138 if (max_age != -1 && (commit->date < max_age)) {
d2775a81 139 stop_traversal=1;
27cfe2e2 140 return CONTINUE;
5e749e25 141 }
76cd8eb6
JS
142 if (no_merges && (commit->parents && commit->parents->next))
143 return CONTINUE;
1b9e059d
LT
144 if (paths && dense) {
145 if (!(commit->object.flags & TREECHANGE))
146 return CONTINUE;
147 rewrite_parents(commit);
148 }
a3437b8c
JS
149 return DO;
150}
151
152static int process_commit(struct commit * commit)
153{
154 int action=filter_commit(commit);
155
156 if (action == STOP) {
157 return STOP;
158 }
159
160 if (action == CONTINUE) {
161 return CONTINUE;
81f2bb1f 162 }
a3437b8c 163
07f92477
LT
164 if (max_count != -1 && !max_count--)
165 return STOP;
166
a3437b8c
JS
167 show_commit(commit);
168
169 return CONTINUE;
81f2bb1f
LT
170}
171
9ce43d1c 172static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
9de48752
LT
173{
174 struct object_list *entry = xmalloc(sizeof(*entry));
175 entry->item = obj;
36f8d174 176 entry->next = *p;
9ce43d1c 177 entry->name = name;
9de48752
LT
178 *p = entry;
179 return &entry->next;
180}
181
9ce43d1c 182static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
9de48752
LT
183{
184 struct object *obj = &blob->object;
185
186 if (!blob_objects)
187 return p;
188 if (obj->flags & (UNINTERESTING | SEEN))
189 return p;
190 obj->flags |= SEEN;
9ce43d1c 191 return add_object(obj, p, name);
9de48752
LT
192}
193
9ce43d1c 194static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
9de48752
LT
195{
196 struct object *obj = &tree->object;
197 struct tree_entry_list *entry;
198
199 if (!tree_objects)
200 return p;
201 if (obj->flags & (UNINTERESTING | SEEN))
202 return p;
203 if (parse_tree(tree) < 0)
204 die("bad tree object %s", sha1_to_hex(obj->sha1));
205 obj->flags |= SEEN;
9ce43d1c 206 p = add_object(obj, p, name);
b0d8923e
LT
207 entry = tree->entries;
208 tree->entries = NULL;
209 while (entry) {
210 struct tree_entry_list *next = entry->next;
9de48752 211 if (entry->directory)
9ce43d1c 212 p = process_tree(entry->item.tree, p, entry->name);
9de48752 213 else
9ce43d1c 214 p = process_blob(entry->item.blob, p, entry->name);
b0d8923e
LT
215 free(entry);
216 entry = next;
9de48752
LT
217 }
218 return p;
219}
220
36f8d174
LT
221static struct object_list *pending_objects = NULL;
222
81f2bb1f
LT
223static void show_commit_list(struct commit_list *list)
224{
36f8d174 225 struct object_list *objects = NULL, **p = &objects, *pending;
81f2bb1f
LT
226 while (list) {
227 struct commit *commit = pop_most_recent_commit(&list, SEEN);
228
9ce43d1c 229 p = process_tree(commit->tree, p, "");
a3437b8c 230 if (process_commit(commit) == STOP)
81f2bb1f 231 break;
81f2bb1f 232 }
36f8d174
LT
233 for (pending = pending_objects; pending; pending = pending->next) {
234 struct object *obj = pending->item;
235 const char *name = pending->name;
236 if (obj->flags & (UNINTERESTING | SEEN))
237 continue;
238 if (obj->type == tag_type) {
239 obj->flags |= SEEN;
240 p = add_object(obj, p, name);
241 continue;
242 }
243 if (obj->type == tree_type) {
244 p = process_tree((struct tree *)obj, p, name);
245 continue;
246 }
247 if (obj->type == blob_type) {
248 p = process_blob((struct blob *)obj, p, name);
249 continue;
250 }
251 die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
252 }
9de48752 253 while (objects) {
c807f771
JH
254 /* An object with name "foo\n0000000000000000000000000000000000000000"
255 * can be used confuse downstream git-pack-objects very badly.
256 */
257 const char *ep = strchr(objects->name, '\n');
258 if (ep) {
259 printf("%s %.*s\n", sha1_to_hex(objects->item->sha1),
260 (int) (ep - objects->name),
261 objects->name);
262 }
263 else
264 printf("%s %s\n", sha1_to_hex(objects->item->sha1), objects->name);
9de48752
LT
265 objects = objects->next;
266 }
267}
268
269static void mark_blob_uninteresting(struct blob *blob)
270{
271 if (!blob_objects)
272 return;
273 if (blob->object.flags & UNINTERESTING)
274 return;
275 blob->object.flags |= UNINTERESTING;
276}
277
278static void mark_tree_uninteresting(struct tree *tree)
279{
280 struct object *obj = &tree->object;
281 struct tree_entry_list *entry;
282
283 if (!tree_objects)
284 return;
285 if (obj->flags & UNINTERESTING)
286 return;
287 obj->flags |= UNINTERESTING;
454fbbcd
LT
288 if (!has_sha1_file(obj->sha1))
289 return;
9de48752
LT
290 if (parse_tree(tree) < 0)
291 die("bad tree %s", sha1_to_hex(obj->sha1));
292 entry = tree->entries;
b0d8923e 293 tree->entries = NULL;
9de48752 294 while (entry) {
b0d8923e 295 struct tree_entry_list *next = entry->next;
9de48752
LT
296 if (entry->directory)
297 mark_tree_uninteresting(entry->item.tree);
298 else
299 mark_blob_uninteresting(entry->item.blob);
b0d8923e
LT
300 free(entry);
301 entry = next;
9de48752 302 }
81f2bb1f
LT
303}
304
8906300f
LT
305static void mark_parents_uninteresting(struct commit *commit)
306{
307 struct commit_list *parents = commit->parents;
308
309 while (parents) {
310 struct commit *commit = parents->item;
311 commit->object.flags |= UNINTERESTING;
454fbbcd 312
6c3b84c8
LT
313 /*
314 * Normally we haven't parsed the parent
315 * yet, so we won't have a parent of a parent
316 * here. However, it may turn out that we've
317 * reached this commit some other way (where it
318 * wasn't uninteresting), in which case we need
319 * to mark its parents recursively too..
320 */
321 if (commit->parents)
322 mark_parents_uninteresting(commit);
323
454fbbcd
LT
324 /*
325 * A missing commit is ok iff its parent is marked
326 * uninteresting.
327 *
328 * We just mark such a thing parsed, so that when
329 * it is popped next time around, we won't be trying
330 * to parse it and get an error.
331 */
332 if (!has_sha1_file(commit->object.sha1))
333 commit->object.parsed = 1;
8906300f
LT
334 parents = parents->next;
335 }
336}
337
4311d328 338static int everybody_uninteresting(struct commit_list *orig)
8906300f 339{
4311d328 340 struct commit_list *list = orig;
8906300f
LT
341 while (list) {
342 struct commit *commit = list->item;
343 list = list->next;
344 if (commit->object.flags & UNINTERESTING)
345 continue;
346 return 0;
347 }
348 return 1;
349}
350
8b3a1e05
LT
351/*
352 * This is a truly stupid algorithm, but it's only
353 * used for bisection, and we just don't care enough.
354 *
355 * We care just barely enough to avoid recursing for
356 * non-merge entries.
357 */
358static int count_distance(struct commit_list *entry)
359{
360 int nr = 0;
361
362 while (entry) {
363 struct commit *commit = entry->item;
364 struct commit_list *p;
365
366 if (commit->object.flags & (UNINTERESTING | COUNTED))
367 break;
b3cfd939
LT
368 if (!paths || (commit->object.flags & TREECHANGE))
369 nr++;
8b3a1e05
LT
370 commit->object.flags |= COUNTED;
371 p = commit->parents;
372 entry = p;
373 if (p) {
374 p = p->next;
375 while (p) {
376 nr += count_distance(p);
377 p = p->next;
378 }
379 }
380 }
b3cfd939 381
8b3a1e05
LT
382 return nr;
383}
384
3d958064 385static void clear_distance(struct commit_list *list)
8b3a1e05
LT
386{
387 while (list) {
388 struct commit *commit = list->item;
389 commit->object.flags &= ~COUNTED;
390 list = list->next;
391 }
392}
393
394static struct commit_list *find_bisection(struct commit_list *list)
395{
396 int nr, closest;
397 struct commit_list *p, *best;
398
399 nr = 0;
400 p = list;
401 while (p) {
b3cfd939
LT
402 if (!paths || (p->item->object.flags & TREECHANGE))
403 nr++;
8b3a1e05
LT
404 p = p->next;
405 }
406 closest = 0;
407 best = list;
408
b3cfd939
LT
409 for (p = list; p; p = p->next) {
410 int distance;
411
412 if (paths && !(p->item->object.flags & TREECHANGE))
413 continue;
414
415 distance = count_distance(p);
8b3a1e05
LT
416 clear_distance(list);
417 if (nr - distance < distance)
418 distance = nr - distance;
419 if (distance > closest) {
420 best = p;
421 closest = distance;
422 }
8b3a1e05
LT
423 }
424 if (best)
425 best->next = NULL;
426 return best;
427}
428
5bdbaaa4
LT
429static void mark_edges_uninteresting(struct commit_list *list)
430{
431 for ( ; list; list = list->next) {
432 struct commit_list *parents = list->item->parents;
433
434 for ( ; parents; parents = parents->next) {
435 struct commit *commit = parents->item;
436 if (commit->object.flags & UNINTERESTING)
437 mark_tree_uninteresting(commit->tree);
438 }
439 }
440}
441
461cf59f
LT
442#define TREE_SAME 0
443#define TREE_NEW 1
444#define TREE_DIFFERENT 2
445static int tree_difference = TREE_SAME;
cf484544
LT
446
447static void file_add_remove(struct diff_options *options,
448 int addremove, unsigned mode,
449 const unsigned char *sha1,
450 const char *base, const char *path)
451{
461cf59f
LT
452 int diff = TREE_DIFFERENT;
453
454 /*
455 * Is it an add of a new file? It means that
456 * the old tree didn't have it at all, so we
457 * will turn "TREE_SAME" -> "TREE_NEW", but
458 * leave any "TREE_DIFFERENT" alone (and if
459 * it already was "TREE_NEW", we'll keep it
460 * "TREE_NEW" of course).
461 */
462 if (addremove == '+') {
463 diff = tree_difference;
464 if (diff != TREE_SAME)
465 return;
466 diff = TREE_NEW;
467 }
468 tree_difference = diff;
cf484544
LT
469}
470
471static void file_change(struct diff_options *options,
472 unsigned old_mode, unsigned new_mode,
473 const unsigned char *old_sha1,
474 const unsigned char *new_sha1,
475 const char *base, const char *path)
476{
461cf59f 477 tree_difference = TREE_DIFFERENT;
cf484544
LT
478}
479
480static struct diff_options diff_opt = {
481 .recursive = 1,
482 .add_remove = file_add_remove,
483 .change = file_change,
484};
485
461cf59f 486static int compare_tree(struct tree *t1, struct tree *t2)
1b9e059d 487{
461cf59f
LT
488 if (!t1)
489 return TREE_NEW;
490 if (!t2)
491 return TREE_DIFFERENT;
492 tree_difference = TREE_SAME;
1b9e059d 493 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
461cf59f
LT
494 return TREE_DIFFERENT;
495 return tree_difference;
1b9e059d
LT
496}
497
129adf4d
LT
498static int same_tree_as_empty(struct tree *t1)
499{
500 int retval;
501 void *tree;
502 struct tree_desc empty, real;
503
504 if (!t1)
505 return 0;
506
507 tree = read_object_with_reference(t1->object.sha1, "tree", &real.size, NULL);
508 if (!tree)
509 return 0;
510 real.buf = tree;
511
512 empty.buf = "";
513 empty.size = 0;
514
461cf59f 515 tree_difference = 0;
129adf4d
LT
516 retval = diff_tree(&empty, &real, "", &diff_opt);
517 free(tree);
518
461cf59f 519 return retval >= 0 && !tree_difference;
129adf4d
LT
520}
521
461cf59f 522static void try_to_simplify_commit(struct commit *commit)
cf484544 523{
461cf59f
LT
524 struct commit_list **pp, *parent;
525
cf484544 526 if (!commit->tree)
461cf59f 527 return;
cf484544 528
461cf59f
LT
529 if (!commit->parents) {
530 if (!same_tree_as_empty(commit->tree))
531 commit->object.flags |= TREECHANGE;
532 return;
533 }
534
535 pp = &commit->parents;
536 while ((parent = *pp) != NULL) {
cf484544 537 struct commit *p = parent->item;
461cf59f
LT
538
539 if (p->object.flags & UNINTERESTING) {
540 pp = &parent->next;
541 continue;
542 }
543
cf484544 544 parse_commit(p);
461cf59f
LT
545 switch (compare_tree(p->tree, commit->tree)) {
546 case TREE_SAME:
547 parent->next = NULL;
548 commit->parents = parent;
549 return;
550
551 case TREE_NEW:
552 if (remove_empty_trees && same_tree_as_empty(p->tree)) {
553 *pp = parent->next;
554 continue;
555 }
556 /* fallthrough */
557 case TREE_DIFFERENT:
558 pp = &parent->next;
cf484544 559 continue;
461cf59f
LT
560 }
561 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
cf484544 562 }
461cf59f 563 commit->object.flags |= TREECHANGE;
cf484544
LT
564}
565
566static void add_parents_to_list(struct commit *commit, struct commit_list **list)
567{
568 struct commit_list *parent = commit->parents;
569
570 /*
571 * If the commit is uninteresting, don't try to
572 * prune parents - we want the maximal uninteresting
573 * set.
574 *
575 * Normally we haven't parsed the parent
576 * yet, so we won't have a parent of a parent
577 * here. However, it may turn out that we've
578 * reached this commit some other way (where it
579 * wasn't uninteresting), in which case we need
580 * to mark its parents recursively too..
581 */
582 if (commit->object.flags & UNINTERESTING) {
583 while (parent) {
584 struct commit *p = parent->item;
585 parent = parent->next;
586 parse_commit(p);
587 p->object.flags |= UNINTERESTING;
588 if (p->parents)
589 mark_parents_uninteresting(p);
590 if (p->object.flags & SEEN)
591 continue;
592 p->object.flags |= SEEN;
593 insert_by_date(p, list);
594 }
595 return;
596 }
597
598 /*
461cf59f
LT
599 * Ok, the commit wasn't uninteresting. Try to
600 * simplify the commit history and find the parent
601 * that has no differences in the path set if one exists.
cf484544 602 */
461cf59f
LT
603 if (paths)
604 try_to_simplify_commit(commit);
cf484544 605
461cf59f 606 parent = commit->parents;
cf484544
LT
607 while (parent) {
608 struct commit *p = parent->item;
609
610 parent = parent->next;
611
612 parse_commit(p);
613 if (p->object.flags & SEEN)
614 continue;
615 p->object.flags |= SEEN;
616 insert_by_date(p, list);
617 }
618}
619
6da4016a 620static struct commit_list *limit_list(struct commit_list *list)
3b42a63c
LT
621{
622 struct commit_list *newlist = NULL;
623 struct commit_list **p = &newlist;
36f8d174 624 while (list) {
cf484544
LT
625 struct commit_list *entry = list;
626 struct commit *commit = list->item;
3b42a63c
LT
627 struct object *obj = &commit->object;
628
cf484544
LT
629 list = list->next;
630 free(entry);
631
27cfe2e2
LT
632 if (max_age != -1 && (commit->date < max_age))
633 obj->flags |= UNINTERESTING;
12d2a187
LT
634 if (unpacked && has_sha1_pack(obj->sha1))
635 obj->flags |= UNINTERESTING;
cf484544 636 add_parents_to_list(commit, &list);
337cb3fb 637 if (obj->flags & UNINTERESTING) {
3b42a63c
LT
638 mark_parents_uninteresting(commit);
639 if (everybody_uninteresting(list))
640 break;
641 continue;
642 }
27cfe2e2
LT
643 if (min_age != -1 && (commit->date > min_age))
644 continue;
3b42a63c 645 p = &commit_list_insert(commit, p)->next;
36f8d174 646 }
5bdbaaa4
LT
647 if (tree_objects)
648 mark_edges_uninteresting(newlist);
8b3a1e05
LT
649 if (bisect_list)
650 newlist = find_bisection(newlist);
3b42a63c
LT
651 return newlist;
652}
653
36f8d174
LT
654static void add_pending_object(struct object *obj, const char *name)
655{
656 add_object(obj, &pending_objects, name);
657}
658
19a7e715 659static struct commit *get_commit_reference(const char *name, const unsigned char *sha1, unsigned int flags)
3c90f03d 660{
36f8d174 661 struct object *object;
3c90f03d 662
36f8d174
LT
663 object = parse_object(sha1);
664 if (!object)
665 die("bad object %s", name);
666
667 /*
668 * Tag object? Look what it points to..
669 */
013aab82 670 while (object->type == tag_type) {
36f8d174
LT
671 struct tag *tag = (struct tag *) object;
672 object->flags |= flags;
673 if (tag_objects && !(object->flags & UNINTERESTING))
674 add_pending_object(object, tag->tag);
013aab82 675 object = parse_object(tag->tagged->sha1);
7f1335c7
SV
676 if (!object)
677 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
36f8d174
LT
678 }
679
680 /*
681 * Commit object? Just return it, we'll do all the complex
682 * reachability crud.
683 */
684 if (object->type == commit_type) {
685 struct commit *commit = (struct commit *)object;
686 object->flags |= flags;
687 if (parse_commit(commit) < 0)
688 die("unable to parse commit %s", name);
454fbbcd
LT
689 if (flags & UNINTERESTING)
690 mark_parents_uninteresting(commit);
36f8d174
LT
691 return commit;
692 }
693
694 /*
695 * Tree object? Either mark it uniniteresting, or add it
696 * to the list of objects to look at later..
697 */
698 if (object->type == tree_type) {
699 struct tree *tree = (struct tree *)object;
700 if (!tree_objects)
960bba0d 701 return NULL;
36f8d174
LT
702 if (flags & UNINTERESTING) {
703 mark_tree_uninteresting(tree);
704 return NULL;
705 }
706 add_pending_object(object, "");
707 return NULL;
708 }
709
710 /*
711 * Blob object? You know the drill by now..
712 */
713 if (object->type == blob_type) {
714 struct blob *blob = (struct blob *)object;
715 if (!blob_objects)
960bba0d 716 return NULL;
36f8d174
LT
717 if (flags & UNINTERESTING) {
718 mark_blob_uninteresting(blob);
719 return NULL;
720 }
721 add_pending_object(object, "");
722 return NULL;
723 }
724 die("%s is unknown object", name);
3c90f03d
LT
725}
726
1215879c
JH
727static void handle_one_commit(struct commit *com, struct commit_list **lst)
728{
729 if (!com || com->object.flags & SEEN)
730 return;
731 com->object.flags |= SEEN;
732 commit_list_insert(com, lst);
733}
734
e091eb93
JH
735/* for_each_ref() callback does not allow user data -- Yuck. */
736static struct commit_list **global_lst;
737
738static int include_one_commit(const char *path, const unsigned char *sha1)
739{
19a7e715 740 struct commit *com = get_commit_reference(path, sha1, 0);
e091eb93
JH
741 handle_one_commit(com, global_lst);
742 return 0;
743}
744
745static void handle_all(struct commit_list **lst)
746{
747 global_lst = lst;
748 for_each_ref(include_one_commit);
749 global_lst = NULL;
750}
1215879c 751
cf484544 752int main(int argc, const char **argv)
64745109 753{
cf484544 754 const char *prefix = setup_git_directory();
64745109 755 struct commit_list *list = NULL;
337cb3fb 756 int i, limited = 0;
64745109 757
fcfda02b 758 for (i = 1 ; i < argc; i++) {
337cb3fb 759 int flags;
cf484544 760 const char *arg = argv[i];
1215879c 761 char *dotdot;
337cb3fb 762 struct commit *commit;
19a7e715 763 unsigned char sha1[20];
fcfda02b 764
8233340c
EW
765 /* accept -<digit>, like traditilnal "head" */
766 if ((*arg == '-') && isdigit(arg[1])) {
767 max_count = atoi(arg + 1);
768 continue;
769 }
3af06987
EW
770 if (!strcmp(arg, "-n")) {
771 if (++i >= argc)
772 die("-n requires an argument");
773 max_count = atoi(argv[i]);
774 continue;
775 }
776 if (!strncmp(arg,"-n",2)) {
777 max_count = atoi(arg + 2);
778 continue;
779 }
fcfda02b
KS
780 if (!strncmp(arg, "--max-count=", 12)) {
781 max_count = atoi(arg + 12);
a6f68d47
LT
782 continue;
783 }
784 if (!strncmp(arg, "--max-age=", 10)) {
fcfda02b 785 max_age = atoi(arg + 10);
27cfe2e2 786 limited = 1;
a6f68d47
LT
787 continue;
788 }
789 if (!strncmp(arg, "--min-age=", 10)) {
fcfda02b 790 min_age = atoi(arg + 10);
27cfe2e2 791 limited = 1;
a6f68d47 792 continue;
fcfda02b 793 }
a6f68d47
LT
794 if (!strcmp(arg, "--header")) {
795 verbose_header = 1;
796 continue;
797 }
000182ea
LT
798 if (!strncmp(arg, "--pretty", 8)) {
799 commit_format = get_commit_format(arg+8);
9d97aa64 800 verbose_header = 1;
9d97aa64 801 hdr_termination = '\n';
d87449c5 802 if (commit_format == CMIT_FMT_ONELINE)
d998a089 803 commit_prefix = "";
d87449c5 804 else
d998a089 805 commit_prefix = "commit ";
9d97aa64
LT
806 continue;
807 }
76cd8eb6
JS
808 if (!strncmp(arg, "--no-merges", 11)) {
809 no_merges = 1;
810 continue;
811 }
97658004
LT
812 if (!strcmp(arg, "--parents")) {
813 show_parents = 1;
814 continue;
815 }
8b3a1e05
LT
816 if (!strcmp(arg, "--bisect")) {
817 bisect_list = 1;
818 continue;
819 }
e091eb93
JH
820 if (!strcmp(arg, "--all")) {
821 handle_all(&list);
822 continue;
823 }
9de48752 824 if (!strcmp(arg, "--objects")) {
3c90f03d 825 tag_objects = 1;
9de48752
LT
826 tree_objects = 1;
827 blob_objects = 1;
828 continue;
829 }
12d2a187
LT
830 if (!strcmp(arg, "--unpacked")) {
831 unpacked = 1;
832 limited = 1;
833 continue;
834 }
12ba7eaf 835 if (!strcmp(arg, "--merge-order")) {
a3437b8c
JS
836 merge_order = 1;
837 continue;
838 }
12ba7eaf 839 if (!strcmp(arg, "--show-breaks")) {
a3437b8c
JS
840 show_breaks = 1;
841 continue;
842 }
d2d02a49
LT
843 if (!strcmp(arg, "--topo-order")) {
844 topo_order = 1;
e6c3505b 845 limited = 1;
d2d02a49
LT
846 continue;
847 }
1b9e059d
LT
848 if (!strcmp(arg, "--dense")) {
849 dense = 1;
850 continue;
851 }
7b34c2fa
LT
852 if (!strcmp(arg, "--sparse")) {
853 dense = 0;
854 continue;
855 }
461cf59f
LT
856 if (!strcmp(arg, "--remove-empty")) {
857 remove_empty_trees = 1;
858 continue;
859 }
cf484544 860 if (!strcmp(arg, "--")) {
7b34c2fa 861 i++;
cf484544
LT
862 break;
863 }
a6f68d47 864
1215879c
JH
865 if (show_breaks && !merge_order)
866 usage(rev_list_usage);
867
337cb3fb 868 flags = 0;
1215879c
JH
869 dotdot = strstr(arg, "..");
870 if (dotdot) {
19a7e715 871 unsigned char from_sha1[20];
1215879c 872 char *next = dotdot + 2;
1215879c 873 *dotdot = 0;
2a7055ae
LT
874 if (!*next)
875 next = "HEAD";
19a7e715
LT
876 if (!get_sha1(arg, from_sha1) && !get_sha1(next, sha1)) {
877 struct commit *exclude;
878 struct commit *include;
879
880 exclude = get_commit_reference(arg, from_sha1, UNINTERESTING);
881 include = get_commit_reference(next, sha1, 0);
882 if (!exclude || !include)
883 die("Invalid revision range %s..%s", arg, next);
1215879c
JH
884 limited = 1;
885 handle_one_commit(exclude, &list);
886 handle_one_commit(include, &list);
887 continue;
888 }
2a7055ae 889 *dotdot = '.';
1215879c 890 }
337cb3fb
LT
891 if (*arg == '^') {
892 flags = UNINTERESTING;
893 arg++;
894 limited = 1;
895 }
d8f6b342
LT
896 if (get_sha1(arg, sha1) < 0) {
897 struct stat st;
898 if (lstat(arg, &st) < 0)
899 die("'%s': %s", arg, strerror(errno));
7b34c2fa 900 break;
d8f6b342 901 }
19a7e715 902 commit = get_commit_reference(arg, sha1, flags);
1215879c 903 handle_one_commit(commit, &list);
fcfda02b
KS
904 }
905
ef1cc2cc
JH
906 if (!list &&
907 (!(tag_objects||tree_objects||blob_objects) && !pending_objects))
7b34c2fa
LT
908 usage(rev_list_usage);
909
910 paths = get_pathspec(prefix, argv + i);
911 if (paths) {
912 limited = 1;
913 diff_tree_setup_paths(paths);
914 }
915
60ab26de 916 save_commit_buffer = verbose_header;
8805ccac 917 track_object_refs = 0;
60ab26de 918
a3437b8c 919 if (!merge_order) {
a7336ae5 920 sort_by_date(&list);
fe5f51ce
LT
921 if (list && !limited && max_count == 1 &&
922 !tag_objects && !tree_objects && !blob_objects) {
923 show_commit(list->item);
924 return 0;
925 }
17ebe977 926 if (limited)
a3437b8c 927 list = limit_list(list);
d2d02a49
LT
928 if (topo_order)
929 sort_in_topological_order(&list);
a3437b8c 930 show_commit_list(list);
a3437b8c 931 } else {
dd53c7ab 932#ifndef NO_OPENSSL
a3437b8c 933 if (sort_list_in_merge_order(list, &process_commit)) {
dd53c7ab 934 die("merge order sort failed\n");
a3437b8c 935 }
dd53c7ab
PB
936#else
937 die("merge order sort unsupported, OpenSSL not linked");
938#endif
a3437b8c 939 }
8906300f 940
64745109
LT
941 return 0;
942}