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