Merge branch 'jc/mv'
[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 #define TMP_MARK        (1u << 5) /* for isolated cases; clean after use */
16
17 static const char rev_list_usage[] =
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"
25 "    --remove-empty\n"
26 "    --all\n"
27 "  ordering output:\n"
28 "    --merge-order [ --show-breaks ]\n"
29 "    --topo-order\n"
30 "    --date-order\n"
31 "  formatting output:\n"
32 "    --parents\n"
33 "    --objects\n"
34 "    --unpacked\n"
35 "    --header | --pretty\n"
36 "    --abbrev=nr | --no-abbrev\n"
37 "  special purpose:\n"
38 "    --bisect"
39 ;
40
41 static int dense = 1;
42 static int unpacked = 0;
43 static int bisect_list = 0;
44 static int tag_objects = 0;
45 static int tree_objects = 0;
46 static int blob_objects = 0;
47 static int verbose_header = 0;
48 static int abbrev = DEFAULT_ABBREV;
49 static int show_parents = 0;
50 static int hdr_termination = 0;
51 static const char *commit_prefix = "";
52 static unsigned long max_age = -1;
53 static unsigned long min_age = -1;
54 static int max_count = -1;
55 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
56 static int merge_order = 0;
57 static int show_breaks = 0;
58 static int stop_traversal = 0;
59 static int topo_order = 0;
60 static int lifo = 1;
61 static int no_merges = 0;
62 static const char **paths = NULL;
63 static int remove_empty_trees = 0;
64
65 static void show_commit(struct commit *commit)
66 {
67         commit->object.flags |= SHOWN;
68         if (show_breaks) {
69                 commit_prefix = "| ";
70                 if (commit->object.flags & DISCONTINUITY) {
71                         commit_prefix = "^ ";     
72                 } else if (commit->object.flags & BOUNDARY) {
73                         commit_prefix = "= ";
74                 } 
75         }                       
76         printf("%s%s", commit_prefix, sha1_to_hex(commit->object.sha1));
77         if (show_parents) {
78                 struct commit_list *parents = commit->parents;
79                 while (parents) {
80                         struct object *o = &(parents->item->object);
81                         parents = parents->next;
82                         if (o->flags & TMP_MARK)
83                                 continue;
84                         printf(" %s", sha1_to_hex(o->sha1));
85                         o->flags |= TMP_MARK;
86                 }
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;
95         }
96         if (commit_format == CMIT_FMT_ONELINE)
97                 putchar(' ');
98         else
99                 putchar('\n');
100
101         if (verbose_header) {
102                 static char pretty_header[16384];
103                 pretty_print_commit(commit_format, commit, ~0, pretty_header, sizeof(pretty_header), abbrev);
104                 printf("%s%c", pretty_header, hdr_termination);
105         }
106         fflush(stdout);
107 }
108
109 static int rewrite_one(struct commit **pp)
110 {
111         for (;;) {
112                 struct commit *p = *pp;
113                 if (p->object.flags & (TREECHANGE | UNINTERESTING))
114                         return 0;
115                 if (!p->parents)
116                         return -1;
117                 *pp = p->parents->item;
118         }
119 }
120
121 static void rewrite_parents(struct commit *commit)
122 {
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;
131         }
132 }
133
134 static int filter_commit(struct commit * commit)
135 {
136         if (stop_traversal && (commit->object.flags & BOUNDARY))
137                 return STOP;
138         if (commit->object.flags & (UNINTERESTING|SHOWN))
139                 return CONTINUE;
140         if (min_age != -1 && (commit->date > min_age))
141                 return CONTINUE;
142         if (max_age != -1 && (commit->date < max_age)) {
143                 stop_traversal=1;
144                 return CONTINUE;
145         }
146         if (no_merges && (commit->parents && commit->parents->next))
147                 return CONTINUE;
148         if (paths && dense) {
149                 if (!(commit->object.flags & TREECHANGE))
150                         return CONTINUE;
151                 rewrite_parents(commit);
152         }
153         return DO;
154 }
155
156 static 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;
166         }
167
168         if (max_count != -1 && !max_count--)
169                 return STOP;
170
171         show_commit(commit);
172
173         return CONTINUE;
174 }
175
176 static struct object_list **add_object(struct object *obj, struct object_list **p, const char *name)
177 {
178         struct object_list *entry = xmalloc(sizeof(*entry));
179         entry->item = obj;
180         entry->next = *p;
181         entry->name = name;
182         *p = entry;
183         return &entry->next;
184 }
185
186 static struct object_list **process_blob(struct blob *blob, struct object_list **p, const char *name)
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;
195         return add_object(obj, p, name);
196 }
197
198 static struct object_list **process_tree(struct tree *tree, struct object_list **p, const char *name)
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;
210         p = add_object(obj, p, name);
211         entry = tree->entries;
212         tree->entries = NULL;
213         while (entry) {
214                 struct tree_entry_list *next = entry->next;
215                 if (entry->directory)
216                         p = process_tree(entry->item.tree, p, entry->name);
217                 else
218                         p = process_blob(entry->item.blob, p, entry->name);
219                 free(entry);
220                 entry = next;
221         }
222         return p;
223 }
224
225 static struct object_list *pending_objects = NULL;
226
227 static void show_commit_list(struct commit_list *list)
228 {
229         struct object_list *objects = NULL, **p = &objects, *pending;
230         while (list) {
231                 struct commit *commit = pop_most_recent_commit(&list, SEEN);
232
233                 p = process_tree(commit->tree, p, "");
234                 if (process_commit(commit) == STOP)
235                         break;
236         }
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         }
257         while (objects) {
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);
269                 objects = objects->next;
270         }
271 }
272
273 static 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
282 static 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;
292         if (!has_sha1_file(obj->sha1))
293                 return;
294         if (parse_tree(tree) < 0)
295                 die("bad tree %s", sha1_to_hex(obj->sha1));
296         entry = tree->entries;
297         tree->entries = NULL;
298         while (entry) {
299                 struct tree_entry_list *next = entry->next;
300                 if (entry->directory)
301                         mark_tree_uninteresting(entry->item.tree);
302                 else
303                         mark_blob_uninteresting(entry->item.blob);
304                 free(entry);
305                 entry = next;
306         }
307 }
308
309 static 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;
316
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
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;
338                 parents = parents->next;
339         }
340 }
341
342 static int everybody_uninteresting(struct commit_list *orig)
343 {
344         struct commit_list *list = orig;
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
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  */
362 static 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;
372                 if (!paths || (commit->object.flags & TREECHANGE))
373                         nr++;
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         }
385
386         return nr;
387 }
388
389 static void clear_distance(struct commit_list *list)
390 {
391         while (list) {
392                 struct commit *commit = list->item;
393                 commit->object.flags &= ~COUNTED;
394                 list = list->next;
395         }
396 }
397
398 static 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) {
406                 if (!paths || (p->item->object.flags & TREECHANGE))
407                         nr++;
408                 p = p->next;
409         }
410         closest = 0;
411         best = list;
412
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);
420                 clear_distance(list);
421                 if (nr - distance < distance)
422                         distance = nr - distance;
423                 if (distance > closest) {
424                         best = p;
425                         closest = distance;
426                 }
427         }
428         if (best)
429                 best->next = NULL;
430         return best;
431 }
432
433 static 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
446 #define TREE_SAME       0
447 #define TREE_NEW        1
448 #define TREE_DIFFERENT  2
449 static int tree_difference = TREE_SAME;
450
451 static 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 {
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;
473 }
474
475 static 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 {
481         tree_difference = TREE_DIFFERENT;
482 }
483
484 static struct diff_options diff_opt = {
485         .recursive = 1,
486         .add_remove = file_add_remove,
487         .change = file_change,
488 };
489
490 static int compare_tree(struct tree *t1, struct tree *t2)
491 {
492         if (!t1)
493                 return TREE_NEW;
494         if (!t2)
495                 return TREE_DIFFERENT;
496         tree_difference = TREE_SAME;
497         if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
498                 return TREE_DIFFERENT;
499         return tree_difference;
500 }
501
502 static 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
519         tree_difference = 0;
520         retval = diff_tree(&empty, &real, "", &diff_opt);
521         free(tree);
522
523         return retval >= 0 && !tree_difference;
524 }
525
526 static void try_to_simplify_commit(struct commit *commit)
527 {
528         struct commit_list **pp, *parent;
529
530         if (!commit->tree)
531                 return;
532
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) {
541                 struct commit *p = parent->item;
542
543                 if (p->object.flags & UNINTERESTING) {
544                         pp = &parent->next;
545                         continue;
546                 }
547
548                 parse_commit(p);
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;
563                         continue;
564                 }
565                 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
566         }
567         commit->object.flags |= TREECHANGE;
568 }
569
570 static 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         /*
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.
606          */
607         if (paths)
608                 try_to_simplify_commit(commit);
609
610         parent = commit->parents;
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
624 static struct commit_list *limit_list(struct commit_list *list)
625 {
626         struct commit_list *newlist = NULL;
627         struct commit_list **p = &newlist;
628         while (list) {
629                 struct commit_list *entry = list;
630                 struct commit *commit = list->item;
631                 struct object *obj = &commit->object;
632
633                 list = list->next;
634                 free(entry);
635
636                 if (max_age != -1 && (commit->date < max_age))
637                         obj->flags |= UNINTERESTING;
638                 if (unpacked && has_sha1_pack(obj->sha1))
639                         obj->flags |= UNINTERESTING;
640                 add_parents_to_list(commit, &list);
641                 if (obj->flags & UNINTERESTING) {
642                         mark_parents_uninteresting(commit);
643                         if (everybody_uninteresting(list))
644                                 break;
645                         continue;
646                 }
647                 if (min_age != -1 && (commit->date > min_age))
648                         continue;
649                 p = &commit_list_insert(commit, p)->next;
650         }
651         if (tree_objects)
652                 mark_edges_uninteresting(newlist);
653         if (bisect_list)
654                 newlist = find_bisection(newlist);
655         return newlist;
656 }
657
658 static void add_pending_object(struct object *obj, const char *name)
659 {
660         add_object(obj, &pending_objects, name);
661 }
662
663 static struct commit *get_commit_reference(const char *name, const unsigned char *sha1, unsigned int flags)
664 {
665         struct object *object;
666
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          */
674         while (object->type == tag_type) {
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);
679                 object = parse_object(tag->tagged->sha1);
680                 if (!object)
681                         die("bad object %s", sha1_to_hex(tag->tagged->sha1));
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);
693                 if (flags & UNINTERESTING)
694                         mark_parents_uninteresting(commit);
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)
705                         return NULL;
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)
720                         return NULL;
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);
729 }
730
731 static 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
739 /* for_each_ref() callback does not allow user data -- Yuck. */
740 static struct commit_list **global_lst;
741
742 static int include_one_commit(const char *path, const unsigned char *sha1)
743 {
744         struct commit *com = get_commit_reference(path, sha1, 0);
745         handle_one_commit(com, global_lst);
746         return 0;
747 }
748
749 static void handle_all(struct commit_list **lst)
750 {
751         global_lst = lst;
752         for_each_ref(include_one_commit);
753         global_lst = NULL;
754 }
755
756 int main(int argc, const char **argv)
757 {
758         const char *prefix = setup_git_directory();
759         struct commit_list *list = NULL;
760         int i, limited = 0;
761
762         for (i = 1 ; i < argc; i++) {
763                 int flags;
764                 const char *arg = argv[i];
765                 char *dotdot;
766                 struct commit *commit;
767                 unsigned char sha1[20];
768
769                 /* accept -<digit>, like traditilnal "head" */
770                 if ((*arg == '-') && isdigit(arg[1])) {
771                         max_count = atoi(arg + 1);
772                         continue;
773                 }
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                 }
784                 if (!strncmp(arg, "--max-count=", 12)) {
785                         max_count = atoi(arg + 12);
786                         continue;
787                 }
788                 if (!strncmp(arg, "--max-age=", 10)) {
789                         max_age = atoi(arg + 10);
790                         limited = 1;
791                         continue;
792                 }
793                 if (!strncmp(arg, "--min-age=", 10)) {
794                         min_age = atoi(arg + 10);
795                         limited = 1;
796                         continue;
797                 }
798                 if (!strcmp(arg, "--header")) {
799                         verbose_header = 1;
800                         continue;
801                 }
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                 }
814                 if (!strncmp(arg, "--pretty", 8)) {
815                         commit_format = get_commit_format(arg+8);
816                         verbose_header = 1;
817                         hdr_termination = '\n';
818                         if (commit_format == CMIT_FMT_ONELINE)
819                                 commit_prefix = "";
820                         else
821                                 commit_prefix = "commit ";
822                         continue;
823                 }
824                 if (!strncmp(arg, "--no-merges", 11)) {
825                         no_merges = 1;
826                         continue;
827                 }
828                 if (!strcmp(arg, "--parents")) {
829                         show_parents = 1;
830                         continue;
831                 }
832                 if (!strcmp(arg, "--bisect")) {
833                         bisect_list = 1;
834                         continue;
835                 }
836                 if (!strcmp(arg, "--all")) {
837                         handle_all(&list);
838                         continue;
839                 }
840                 if (!strcmp(arg, "--objects")) {
841                         tag_objects = 1;
842                         tree_objects = 1;
843                         blob_objects = 1;
844                         continue;
845                 }
846                 if (!strcmp(arg, "--unpacked")) {
847                         unpacked = 1;
848                         limited = 1;
849                         continue;
850                 }
851                 if (!strcmp(arg, "--merge-order")) {
852                         merge_order = 1;
853                         continue;
854                 }
855                 if (!strcmp(arg, "--show-breaks")) {
856                         show_breaks = 1;
857                         continue;
858                 }
859                 if (!strcmp(arg, "--topo-order")) {
860                         topo_order = 1;
861                         lifo = 1;
862                         limited = 1;
863                         continue;
864                 }
865                 if (!strcmp(arg, "--date-order")) {
866                         topo_order = 1;
867                         lifo = 0;
868                         limited = 1;
869                         continue;
870                 }
871                 if (!strcmp(arg, "--dense")) {
872                         dense = 1;
873                         continue;
874                 }
875                 if (!strcmp(arg, "--sparse")) {
876                         dense = 0;
877                         continue;
878                 }
879                 if (!strcmp(arg, "--remove-empty")) {
880                         remove_empty_trees = 1;
881                         continue;
882                 }
883                 if (!strcmp(arg, "--")) {
884                         i++;
885                         break;
886                 }
887
888                 if (show_breaks && !merge_order)
889                         usage(rev_list_usage);
890
891                 flags = 0;
892                 dotdot = strstr(arg, "..");
893                 if (dotdot) {
894                         unsigned char from_sha1[20];
895                         char *next = dotdot + 2;
896                         *dotdot = 0;
897                         if (!*next)
898                                 next = "HEAD";
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);
907                                 limited = 1;
908                                 handle_one_commit(exclude, &list);
909                                 handle_one_commit(include, &list);
910                                 continue;
911                         }
912                         *dotdot = '.';
913                 }
914                 if (*arg == '^') {
915                         flags = UNINTERESTING;
916                         arg++;
917                         limited = 1;
918                 }
919                 if (get_sha1(arg, sha1) < 0) {
920                         struct stat st;
921                         if (lstat(arg, &st) < 0)
922                                 die("'%s': %s", arg, strerror(errno));
923                         break;
924                 }
925                 commit = get_commit_reference(arg, sha1, flags);
926                 handle_one_commit(commit, &list);
927         }
928
929         if (!list &&
930             (!(tag_objects||tree_objects||blob_objects) && !pending_objects))
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
939         save_commit_buffer = verbose_header;
940         track_object_refs = 0;
941
942         if (!merge_order) {             
943                 sort_by_date(&list);
944                 if (list && !limited && max_count == 1 &&
945                     !tag_objects && !tree_objects && !blob_objects) {
946                         show_commit(list->item);
947                         return 0;
948                 }
949                 if (limited)
950                         list = limit_list(list);
951                 if (topo_order)
952                         sort_in_topological_order(&list, lifo);
953                 show_commit_list(list);
954         } else {
955 #ifndef NO_OPENSSL
956                 if (sort_list_in_merge_order(list, &process_commit)) {
957                         die("merge order sort failed\n");
958                 }
959 #else
960                 die("merge order sort unsupported, OpenSSL not linked");
961 #endif
962         }
963
964         return 0;
965 }