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