rev-list: allow -<n> as shorthand for --max-count=<n>
[git.git] / rev-list.c
1 #include "cache.h"
2 #include "refs.h"
3 #include "tag.h"
4 #include "commit.h"
5 #include "tree.h"
6 #include "blob.h"
7 #include "epoch.h"
8 #include "diff.h"
9
10 #define SEEN            (1u << 0)
11 #define INTERESTING     (1u << 1)
12 #define COUNTED         (1u << 2)
13 #define SHOWN           (1u << 3)
14 #define TREECHANGE      (1u << 4)
15
16 static const char rev_list_usage[] =
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 "    --remove-empty\n"
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 ;
37
38 static int dense = 1;
39 static int unpacked = 0;
40 static int bisect_list = 0;
41 static int tag_objects = 0;
42 static int tree_objects = 0;
43 static int blob_objects = 0;
44 static int verbose_header = 0;
45 static int show_parents = 0;
46 static int hdr_termination = 0;
47 static const char *commit_prefix = "";
48 static unsigned long max_age = -1;
49 static unsigned long min_age = -1;
50 static int max_count = -1;
51 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
52 static int merge_order = 0;
53 static int show_breaks = 0;
54 static int stop_traversal = 0;
55 static int topo_order = 0;
56 static int no_merges = 0;
57 static const char **paths = NULL;
58 static int remove_empty_trees = 0;
59
60 static void show_commit(struct commit *commit)
61 {
62         commit->object.flags |= SHOWN;
63         if (show_breaks) {
64                 commit_prefix = "| ";
65                 if (commit->object.flags & DISCONTINUITY) {
66                         commit_prefix = "^ ";     
67                 } else if (commit->object.flags & BOUNDARY) {
68                         commit_prefix = "= ";
69                 } 
70         }                       
71         printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
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         }
79         if (commit_format == CMIT_FMT_ONELINE)
80                 putchar(' ');
81         else
82                 putchar('\n');
83
84         if (verbose_header) {
85                 static char pretty_header[16384];
86                 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), 0);
87                 printf("%s%c", pretty_header, hdr_termination);
88         }
89         fflush(stdout);
90 }
91
92 static int rewrite_one(struct commit **pp)
93 {
94         for (;;) {
95                 struct commit *p = *pp;
96                 if (p->object.flags & (TREECHANGE | UNINTERESTING))
97                         return 0;
98                 if (!p->parents)
99                         return -1;
100                 *pp = p->parents->item;
101         }
102 }
103
104 static void rewrite_parents(struct commit *commit)
105 {
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;
114         }
115 }
116
117 static int filter_commit(struct commit * commit)
118 {
119         if (stop_traversal && (commit->object.flags & BOUNDARY))
120                 return STOP;
121         if (commit->object.flags & (UNINTERESTING|SHOWN))
122                 return CONTINUE;
123         if (min_age != -1 && (commit->date > min_age))
124                 return CONTINUE;
125         if (max_age != -1 && (commit->date < max_age)) {
126                 stop_traversal=1;
127                 return CONTINUE;
128         }
129         if (no_merges && (commit->parents && commit->parents->next))
130                 return CONTINUE;
131         if (paths && dense) {
132                 if (!(commit->object.flags & TREECHANGE))
133                         return CONTINUE;
134                 rewrite_parents(commit);
135         }
136         return DO;
137 }
138
139 static 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;
149         }
150
151         if (max_count != -1 && !max_count--)
152                 return STOP;
153
154         show_commit(commit);
155
156         return CONTINUE;
157 }
158
159 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
160 {
161         struct object_list *entry = xmalloc(sizeof(*entry));
162         entry->item = obj;
163         entry->next = *p;
164         entry->name = name;
165         *p = entry;
166         return &entry->next;
167 }
168
169 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
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;
178         return add_object(obj, p, name);
179 }
180
181 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
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;
193         p = add_object(obj, p, name);
194         entry = tree->entries;
195         tree->entries = NULL;
196         while (entry) {
197                 struct tree_entry_list *next = entry->next;
198                 if (entry->directory)
199                         p = process_tree(entry->item.tree, p, entry->name);
200                 else
201                         p = process_blob(entry->item.blob, p, entry->name);
202                 free(entry);
203                 entry = next;
204         }
205         return p;
206 }
207
208 static struct object_list *pending_objects = NULL;
209
210 static void show_commit_list(struct commit_list *list)
211 {
212         struct object_list *objects = NULL, **p = &objects, *pending;
213         while (list) {
214                 struct commit *commit = pop_most_recent_commit(&list, SEEN);
215
216                 p = process_tree(commit->tree, p, "");
217                 if (process_commit(commit) == STOP)
218                         break;
219         }
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         }
240         while (objects) {
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);
252                 objects = objects->next;
253         }
254 }
255
256 static 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
265 static 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;
275         if (!has_sha1_file(obj->sha1))
276                 return;
277         if (parse_tree(tree) < 0)
278                 die("bad tree %s", sha1_to_hex(obj->sha1));
279         entry = tree->entries;
280         tree->entries = NULL;
281         while (entry) {
282                 struct tree_entry_list *next = entry->next;
283                 if (entry->directory)
284                         mark_tree_uninteresting(entry->item.tree);
285                 else
286                         mark_blob_uninteresting(entry->item.blob);
287                 free(entry);
288                 entry = next;
289         }
290 }
291
292 static 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;
299
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
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;
321                 parents = parents->next;
322         }
323 }
324
325 static int everybody_uninteresting(struct commit_list *orig)
326 {
327         struct commit_list *list = orig;
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
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  */
345 static 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;
355                 if (!paths || (commit->object.flags & TREECHANGE))
356                         nr++;
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         }
368
369         return nr;
370 }
371
372 static void clear_distance(struct commit_list *list)
373 {
374         while (list) {
375                 struct commit *commit = list->item;
376                 commit->object.flags &= ~COUNTED;
377                 list = list->next;
378         }
379 }
380
381 static 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) {
389                 if (!paths || (p->item->object.flags & TREECHANGE))
390                         nr++;
391                 p = p->next;
392         }
393         closest = 0;
394         best = list;
395
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);
403                 clear_distance(list);
404                 if (nr - distance < distance)
405                         distance = nr - distance;
406                 if (distance > closest) {
407                         best = p;
408                         closest = distance;
409                 }
410         }
411         if (best)
412                 best->next = NULL;
413         return best;
414 }
415
416 static 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
429 #define TREE_SAME       0
430 #define TREE_NEW        1
431 #define TREE_DIFFERENT  2
432 static int tree_difference = TREE_SAME;
433
434 static 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 {
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;
456 }
457
458 static 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 {
464         tree_difference = TREE_DIFFERENT;
465 }
466
467 static struct diff_options diff_opt = {
468         .recursive = 1,
469         .add_remove = file_add_remove,
470         .change = file_change,
471 };
472
473 static int compare_tree(struct tree *t1, struct tree *t2)
474 {
475         if (!t1)
476                 return TREE_NEW;
477         if (!t2)
478                 return TREE_DIFFERENT;
479         tree_difference = TREE_SAME;
480         if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
481                 return TREE_DIFFERENT;
482         return tree_difference;
483 }
484
485 static 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
502         tree_difference = 0;
503         retval = diff_tree(&empty, &real, "", &diff_opt);
504         free(tree);
505
506         return retval >= 0 && !tree_difference;
507 }
508
509 static void try_to_simplify_commit(struct commit *commit)
510 {
511         struct commit_list **pp, *parent;
512
513         if (!commit->tree)
514                 return;
515
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) {
524                 struct commit *p = parent->item;
525
526                 if (p->object.flags & UNINTERESTING) {
527                         pp = &parent->next;
528                         continue;
529                 }
530
531                 parse_commit(p);
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;
546                         continue;
547                 }
548                 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
549         }
550         commit->object.flags |= TREECHANGE;
551 }
552
553 static 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         /*
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.
589          */
590         if (paths)
591                 try_to_simplify_commit(commit);
592
593         parent = commit->parents;
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
607 static struct commit_list *limit_list(struct commit_list *list)
608 {
609         struct commit_list *newlist = NULL;
610         struct commit_list **p = &newlist;
611         while (list) {
612                 struct commit_list *entry = list;
613                 struct commit *commit = list->item;
614                 struct object *obj = &commit->object;
615
616                 list = list->next;
617                 free(entry);
618
619                 if (max_age != -1 && (commit->date < max_age))
620                         obj->flags |= UNINTERESTING;
621                 if (unpacked && has_sha1_pack(obj->sha1))
622                         obj->flags |= UNINTERESTING;
623                 add_parents_to_list(commit, &list);
624                 if (obj->flags & UNINTERESTING) {
625                         mark_parents_uninteresting(commit);
626                         if (everybody_uninteresting(list))
627                                 break;
628                         continue;
629                 }
630                 if (min_age != -1 && (commit->date > min_age))
631                         continue;
632                 p = &commit_list_insert(commit, p)->next;
633         }
634         if (tree_objects)
635                 mark_edges_uninteresting(newlist);
636         if (bisect_list)
637                 newlist = find_bisection(newlist);
638         return newlist;
639 }
640
641 static void add_pending_object(struct object *obj, const char *name)
642 {
643         add_object(obj, &pending_objects, name);
644 }
645
646 static struct commit *get_commit_reference(const char *name, const unsigned char *sha1, unsigned int flags)
647 {
648         struct object *object;
649
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          */
657         while (object->type == tag_type) {
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);
662                 object = parse_object(tag->tagged->sha1);
663                 if (!object)
664                         die("bad object %s", sha1_to_hex(tag->tagged->sha1));
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);
676                 if (flags & UNINTERESTING)
677                         mark_parents_uninteresting(commit);
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)
688                         return NULL;
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)
703                         return NULL;
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);
712 }
713
714 static 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
722 /* for_each_ref() callback does not allow user data -- Yuck. */
723 static struct commit_list **global_lst;
724
725 static int include_one_commit(const char *path, const unsigned char *sha1)
726 {
727         struct commit *com = get_commit_reference(path, sha1, 0);
728         handle_one_commit(com, global_lst);
729         return 0;
730 }
731
732 static void handle_all(struct commit_list **lst)
733 {
734         global_lst = lst;
735         for_each_ref(include_one_commit);
736         global_lst = NULL;
737 }
738
739 int main(int argc, const char **argv)
740 {
741         const char *prefix = setup_git_directory();
742         struct commit_list *list = NULL;
743         int i, limited = 0;
744
745         for (i = 1 ; i < argc; i++) {
746                 int flags;
747                 const char *arg = argv[i];
748                 char *dotdot;
749                 struct commit *commit;
750                 unsigned char sha1[20];
751
752                 /* accept -<digit>, like traditilnal "head" */
753                 if ((*arg == '-') && isdigit(arg[1])) {
754                         max_count = atoi(arg + 1);
755                         continue;
756                 }
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                 }
767                 if (!strncmp(arg, "--max-count=", 12)) {
768                         max_count = atoi(arg + 12);
769                         continue;
770                 }
771                 if (!strncmp(arg, "--max-age=", 10)) {
772                         max_age = atoi(arg + 10);
773                         limited = 1;
774                         continue;
775                 }
776                 if (!strncmp(arg, "--min-age=", 10)) {
777                         min_age = atoi(arg + 10);
778                         limited = 1;
779                         continue;
780                 }
781                 if (!strcmp(arg, "--header")) {
782                         verbose_header = 1;
783                         continue;
784                 }
785                 if (!strncmp(arg, "--pretty", 8)) {
786                         commit_format = get_commit_format(arg+8);
787                         verbose_header = 1;
788                         hdr_termination = '\n';
789                         if (commit_format == CMIT_FMT_ONELINE)
790                                 commit_prefix = "";
791                         else
792                                 commit_prefix = "commit ";
793                         continue;
794                 }
795                 if (!strncmp(arg, "--no-merges", 11)) {
796                         no_merges = 1;
797                         continue;
798                 }
799                 if (!strcmp(arg, "--parents")) {
800                         show_parents = 1;
801                         continue;
802                 }
803                 if (!strcmp(arg, "--bisect")) {
804                         bisect_list = 1;
805                         continue;
806                 }
807                 if (!strcmp(arg, "--all")) {
808                         handle_all(&list);
809                         continue;
810                 }
811                 if (!strcmp(arg, "--objects")) {
812                         tag_objects = 1;
813                         tree_objects = 1;
814                         blob_objects = 1;
815                         continue;
816                 }
817                 if (!strcmp(arg, "--unpacked")) {
818                         unpacked = 1;
819                         limited = 1;
820                         continue;
821                 }
822                 if (!strcmp(arg, "--merge-order")) {
823                         merge_order = 1;
824                         continue;
825                 }
826                 if (!strcmp(arg, "--show-breaks")) {
827                         show_breaks = 1;
828                         continue;
829                 }
830                 if (!strcmp(arg, "--topo-order")) {
831                         topo_order = 1;
832                         limited = 1;
833                         continue;
834                 }
835                 if (!strcmp(arg, "--dense")) {
836                         dense = 1;
837                         continue;
838                 }
839                 if (!strcmp(arg, "--sparse")) {
840                         dense = 0;
841                         continue;
842                 }
843                 if (!strcmp(arg, "--remove-empty")) {
844                         remove_empty_trees = 1;
845                         continue;
846                 }
847                 if (!strcmp(arg, "--")) {
848                         i++;
849                         break;
850                 }
851
852                 if (show_breaks && !merge_order)
853                         usage(rev_list_usage);
854
855                 flags = 0;
856                 dotdot = strstr(arg, "..");
857                 if (dotdot) {
858                         unsigned char from_sha1[20];
859                         char *next = dotdot + 2;
860                         *dotdot = 0;
861                         if (!*next)
862                                 next = "HEAD";
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);
871                                 limited = 1;
872                                 handle_one_commit(exclude, &list);
873                                 handle_one_commit(include, &list);
874                                 continue;
875                         }
876                         *dotdot = '.';
877                 }
878                 if (*arg == '^') {
879                         flags = UNINTERESTING;
880                         arg++;
881                         limited = 1;
882                 }
883                 if (get_sha1(arg, sha1) < 0) {
884                         struct stat st;
885                         if (lstat(arg, &st) < 0)
886                                 die("'%s': %s", arg, strerror(errno));
887                         break;
888                 }
889                 commit = get_commit_reference(arg, sha1, flags);
890                 handle_one_commit(commit, &list);
891         }
892
893         if (!list &&
894             (!(tag_objects||tree_objects||blob_objects) && !pending_objects))
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
903         save_commit_buffer = verbose_header;
904         track_object_refs = 0;
905
906         if (!merge_order) {             
907                 sort_by_date(&list);
908                 if (list && !limited && max_count == 1 &&
909                     !tag_objects && !tree_objects && !blob_objects) {
910                         show_commit(list->item);
911                         return 0;
912                 }
913                 if (limited)
914                         list = limit_list(list);
915                 if (topo_order)
916                         sort_in_topological_order(&list);
917                 show_commit_list(list);
918         } else {
919 #ifndef NO_OPENSSL
920                 if (sort_list_in_merge_order(list, &process_commit)) {
921                         die("merge order sort failed\n");
922                 }
923 #else
924                 die("merge order sort unsupported, OpenSSL not linked");
925 #endif
926         }
927
928         return 0;
929 }