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