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