revision: --skip=<n>
[git] / revision.c
1 #include "cache.h"
2 #include "tag.h"
3 #include "blob.h"
4 #include "tree.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "refs.h"
8 #include "revision.h"
9 #include "grep.h"
10
11 static char *path_name(struct name_path *path, const char *name)
12 {
13         struct name_path *p;
14         char *n, *m;
15         int nlen = strlen(name);
16         int len = nlen + 1;
17
18         for (p = path; p; p = p->up) {
19                 if (p->elem_len)
20                         len += p->elem_len + 1;
21         }
22         n = xmalloc(len);
23         m = n + len - (nlen + 1);
24         strcpy(m, name);
25         for (p = path; p; p = p->up) {
26                 if (p->elem_len) {
27                         m -= p->elem_len + 1;
28                         memcpy(m, p->elem, p->elem_len);
29                         m[p->elem_len] = '/';
30                 }
31         }
32         return n;
33 }
34
35 void add_object(struct object *obj,
36                 struct object_array *p,
37                 struct name_path *path,
38                 const char *name)
39 {
40         add_object_array(obj, path_name(path, name), p);
41 }
42
43 static void mark_blob_uninteresting(struct blob *blob)
44 {
45         if (blob->object.flags & UNINTERESTING)
46                 return;
47         blob->object.flags |= UNINTERESTING;
48 }
49
50 void mark_tree_uninteresting(struct tree *tree)
51 {
52         struct tree_desc desc;
53         struct name_entry entry;
54         struct object *obj = &tree->object;
55
56         if (obj->flags & UNINTERESTING)
57                 return;
58         obj->flags |= UNINTERESTING;
59         if (!has_sha1_file(obj->sha1))
60                 return;
61         if (parse_tree(tree) < 0)
62                 die("bad tree %s", sha1_to_hex(obj->sha1));
63
64         desc.buf = tree->buffer;
65         desc.size = tree->size;
66         while (tree_entry(&desc, &entry)) {
67                 if (S_ISDIR(entry.mode))
68                         mark_tree_uninteresting(lookup_tree(entry.sha1));
69                 else
70                         mark_blob_uninteresting(lookup_blob(entry.sha1));
71         }
72
73         /*
74          * We don't care about the tree any more
75          * after it has been marked uninteresting.
76          */
77         free(tree->buffer);
78         tree->buffer = NULL;
79 }
80
81 void mark_parents_uninteresting(struct commit *commit)
82 {
83         struct commit_list *parents = commit->parents;
84
85         while (parents) {
86                 struct commit *commit = parents->item;
87                 if (!(commit->object.flags & UNINTERESTING)) {
88                         commit->object.flags |= UNINTERESTING;
89
90                         /*
91                          * Normally we haven't parsed the parent
92                          * yet, so we won't have a parent of a parent
93                          * here. However, it may turn out that we've
94                          * reached this commit some other way (where it
95                          * wasn't uninteresting), in which case we need
96                          * to mark its parents recursively too..
97                          */
98                         if (commit->parents)
99                                 mark_parents_uninteresting(commit);
100                 }
101
102                 /*
103                  * A missing commit is ok iff its parent is marked
104                  * uninteresting.
105                  *
106                  * We just mark such a thing parsed, so that when
107                  * it is popped next time around, we won't be trying
108                  * to parse it and get an error.
109                  */
110                 if (!has_sha1_file(commit->object.sha1))
111                         commit->object.parsed = 1;
112                 parents = parents->next;
113         }
114 }
115
116 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
117 {
118         add_object_array(obj, name, &revs->pending);
119 }
120
121 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
122 {
123         struct object *object;
124
125         object = parse_object(sha1);
126         if (!object)
127                 die("bad object %s", name);
128         object->flags |= flags;
129         return object;
130 }
131
132 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
133 {
134         unsigned long flags = object->flags;
135
136         /*
137          * Tag object? Look what it points to..
138          */
139         while (object->type == OBJ_TAG) {
140                 struct tag *tag = (struct tag *) object;
141                 if (revs->tag_objects && !(flags & UNINTERESTING))
142                         add_pending_object(revs, object, tag->tag);
143                 object = parse_object(tag->tagged->sha1);
144                 if (!object)
145                         die("bad object %s", sha1_to_hex(tag->tagged->sha1));
146         }
147
148         /*
149          * Commit object? Just return it, we'll do all the complex
150          * reachability crud.
151          */
152         if (object->type == OBJ_COMMIT) {
153                 struct commit *commit = (struct commit *)object;
154                 if (parse_commit(commit) < 0)
155                         die("unable to parse commit %s", name);
156                 if (flags & UNINTERESTING) {
157                         commit->object.flags |= UNINTERESTING;
158                         mark_parents_uninteresting(commit);
159                         revs->limited = 1;
160                 }
161                 return commit;
162         }
163
164         /*
165          * Tree object? Either mark it uniniteresting, or add it
166          * to the list of objects to look at later..
167          */
168         if (object->type == OBJ_TREE) {
169                 struct tree *tree = (struct tree *)object;
170                 if (!revs->tree_objects)
171                         return NULL;
172                 if (flags & UNINTERESTING) {
173                         mark_tree_uninteresting(tree);
174                         return NULL;
175                 }
176                 add_pending_object(revs, object, "");
177                 return NULL;
178         }
179
180         /*
181          * Blob object? You know the drill by now..
182          */
183         if (object->type == OBJ_BLOB) {
184                 struct blob *blob = (struct blob *)object;
185                 if (!revs->blob_objects)
186                         return NULL;
187                 if (flags & UNINTERESTING) {
188                         mark_blob_uninteresting(blob);
189                         return NULL;
190                 }
191                 add_pending_object(revs, object, "");
192                 return NULL;
193         }
194         die("%s is unknown object", name);
195 }
196
197 static int everybody_uninteresting(struct commit_list *orig)
198 {
199         struct commit_list *list = orig;
200         while (list) {
201                 struct commit *commit = list->item;
202                 list = list->next;
203                 if (commit->object.flags & UNINTERESTING)
204                         continue;
205                 return 0;
206         }
207         return 1;
208 }
209
210 static int tree_difference = REV_TREE_SAME;
211
212 static void file_add_remove(struct diff_options *options,
213                     int addremove, unsigned mode,
214                     const unsigned char *sha1,
215                     const char *base, const char *path)
216 {
217         int diff = REV_TREE_DIFFERENT;
218
219         /*
220          * Is it an add of a new file? It means that the old tree
221          * didn't have it at all, so we will turn "REV_TREE_SAME" ->
222          * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
223          * (and if it already was "REV_TREE_NEW", we'll keep it
224          * "REV_TREE_NEW" of course).
225          */
226         if (addremove == '+') {
227                 diff = tree_difference;
228                 if (diff != REV_TREE_SAME)
229                         return;
230                 diff = REV_TREE_NEW;
231         }
232         tree_difference = diff;
233 }
234
235 static void file_change(struct diff_options *options,
236                  unsigned old_mode, unsigned new_mode,
237                  const unsigned char *old_sha1,
238                  const unsigned char *new_sha1,
239                  const char *base, const char *path)
240 {
241         tree_difference = REV_TREE_DIFFERENT;
242 }
243
244 int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
245 {
246         if (!t1)
247                 return REV_TREE_NEW;
248         if (!t2)
249                 return REV_TREE_DIFFERENT;
250         tree_difference = REV_TREE_SAME;
251         if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
252                            &revs->pruning) < 0)
253                 return REV_TREE_DIFFERENT;
254         return tree_difference;
255 }
256
257 int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
258 {
259         int retval;
260         void *tree;
261         struct tree_desc empty, real;
262
263         if (!t1)
264                 return 0;
265
266         tree = read_object_with_reference(t1->object.sha1, tree_type, &real.size, NULL);
267         if (!tree)
268                 return 0;
269         real.buf = tree;
270
271         empty.buf = "";
272         empty.size = 0;
273
274         tree_difference = 0;
275         retval = diff_tree(&empty, &real, "", &revs->pruning);
276         free(tree);
277
278         return retval >= 0 && !tree_difference;
279 }
280
281 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
282 {
283         struct commit_list **pp, *parent;
284         int tree_changed = 0, tree_same = 0;
285
286         if (!commit->tree)
287                 return;
288
289         if (!commit->parents) {
290                 if (!rev_same_tree_as_empty(revs, commit->tree))
291                         commit->object.flags |= TREECHANGE;
292                 return;
293         }
294
295         pp = &commit->parents;
296         while ((parent = *pp) != NULL) {
297                 struct commit *p = parent->item;
298
299                 parse_commit(p);
300                 switch (rev_compare_tree(revs, p->tree, commit->tree)) {
301                 case REV_TREE_SAME:
302                         tree_same = 1;
303                         if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
304                                 /* Even if a merge with an uninteresting
305                                  * side branch brought the entire change
306                                  * we are interested in, we do not want
307                                  * to lose the other branches of this
308                                  * merge, so we just keep going.
309                                  */
310                                 pp = &parent->next;
311                                 continue;
312                         }
313                         parent->next = NULL;
314                         commit->parents = parent;
315                         return;
316
317                 case REV_TREE_NEW:
318                         if (revs->remove_empty_trees &&
319                             rev_same_tree_as_empty(revs, p->tree)) {
320                                 /* We are adding all the specified
321                                  * paths from this parent, so the
322                                  * history beyond this parent is not
323                                  * interesting.  Remove its parents
324                                  * (they are grandparents for us).
325                                  * IOW, we pretend this parent is a
326                                  * "root" commit.
327                                  */
328                                 parse_commit(p);
329                                 p->parents = NULL;
330                         }
331                 /* fallthrough */
332                 case REV_TREE_DIFFERENT:
333                         tree_changed = 1;
334                         pp = &parent->next;
335                         continue;
336                 }
337                 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
338         }
339         if (tree_changed && !tree_same)
340                 commit->object.flags |= TREECHANGE;
341 }
342
343 static void add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
344 {
345         struct commit_list *parent = commit->parents;
346
347         if (commit->object.flags & ADDED)
348                 return;
349         commit->object.flags |= ADDED;
350
351         /*
352          * If the commit is uninteresting, don't try to
353          * prune parents - we want the maximal uninteresting
354          * set.
355          *
356          * Normally we haven't parsed the parent
357          * yet, so we won't have a parent of a parent
358          * here. However, it may turn out that we've
359          * reached this commit some other way (where it
360          * wasn't uninteresting), in which case we need
361          * to mark its parents recursively too..
362          */
363         if (commit->object.flags & UNINTERESTING) {
364                 while (parent) {
365                         struct commit *p = parent->item;
366                         parent = parent->next;
367                         parse_commit(p);
368                         p->object.flags |= UNINTERESTING;
369                         if (p->parents)
370                                 mark_parents_uninteresting(p);
371                         if (p->object.flags & SEEN)
372                                 continue;
373                         p->object.flags |= SEEN;
374                         insert_by_date(p, list);
375                 }
376                 return;
377         }
378
379         /*
380          * Ok, the commit wasn't uninteresting. Try to
381          * simplify the commit history and find the parent
382          * that has no differences in the path set if one exists.
383          */
384         if (revs->prune_fn)
385                 revs->prune_fn(revs, commit);
386
387         if (revs->no_walk)
388                 return;
389
390         parent = commit->parents;
391         while (parent) {
392                 struct commit *p = parent->item;
393
394                 parent = parent->next;
395
396                 parse_commit(p);
397                 if (p->object.flags & SEEN)
398                         continue;
399                 p->object.flags |= SEEN;
400                 insert_by_date(p, list);
401         }
402 }
403
404 static void limit_list(struct rev_info *revs)
405 {
406         struct commit_list *list = revs->commits;
407         struct commit_list *newlist = NULL;
408         struct commit_list **p = &newlist;
409
410         while (list) {
411                 struct commit_list *entry = list;
412                 struct commit *commit = list->item;
413                 struct object *obj = &commit->object;
414
415                 list = list->next;
416                 free(entry);
417
418                 if (revs->max_age != -1 && (commit->date < revs->max_age))
419                         obj->flags |= UNINTERESTING;
420                 add_parents_to_list(revs, commit, &list);
421                 if (obj->flags & UNINTERESTING) {
422                         mark_parents_uninteresting(commit);
423                         if (everybody_uninteresting(list))
424                                 break;
425                         continue;
426                 }
427                 if (revs->min_age != -1 && (commit->date > revs->min_age))
428                         continue;
429                 p = &commit_list_insert(commit, p)->next;
430         }
431         if (revs->boundary) {
432                 /* mark the ones that are on the result list first */
433                 for (list = newlist; list; list = list->next) {
434                         struct commit *commit = list->item;
435                         commit->object.flags |= TMP_MARK;
436                 }
437                 for (list = newlist; list; list = list->next) {
438                         struct commit *commit = list->item;
439                         struct object *obj = &commit->object;
440                         struct commit_list *parent;
441                         if (obj->flags & UNINTERESTING)
442                                 continue;
443                         for (parent = commit->parents;
444                              parent;
445                              parent = parent->next) {
446                                 struct commit *pcommit = parent->item;
447                                 if (!(pcommit->object.flags & UNINTERESTING))
448                                         continue;
449                                 pcommit->object.flags |= BOUNDARY;
450                                 if (pcommit->object.flags & TMP_MARK)
451                                         continue;
452                                 pcommit->object.flags |= TMP_MARK;
453                                 p = &commit_list_insert(pcommit, p)->next;
454                         }
455                 }
456                 for (list = newlist; list; list = list->next) {
457                         struct commit *commit = list->item;
458                         commit->object.flags &= ~TMP_MARK;
459                 }
460         }
461         revs->commits = newlist;
462 }
463
464 static int all_flags;
465 static struct rev_info *all_revs;
466
467 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
468 {
469         struct object *object = get_reference(all_revs, path, sha1, all_flags);
470         add_pending_object(all_revs, object, "");
471         return 0;
472 }
473
474 static void handle_all(struct rev_info *revs, unsigned flags)
475 {
476         all_revs = revs;
477         all_flags = flags;
478         for_each_ref(handle_one_ref, NULL);
479 }
480
481 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
482 {
483         unsigned char sha1[20];
484         struct object *it;
485         struct commit *commit;
486         struct commit_list *parents;
487
488         if (*arg == '^') {
489                 flags ^= UNINTERESTING;
490                 arg++;
491         }
492         if (get_sha1(arg, sha1))
493                 return 0;
494         while (1) {
495                 it = get_reference(revs, arg, sha1, 0);
496                 if (it->type != OBJ_TAG)
497                         break;
498                 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
499         }
500         if (it->type != OBJ_COMMIT)
501                 return 0;
502         commit = (struct commit *)it;
503         for (parents = commit->parents; parents; parents = parents->next) {
504                 it = &parents->item->object;
505                 it->flags |= flags;
506                 add_pending_object(revs, it, arg);
507         }
508         return 1;
509 }
510
511 void init_revisions(struct rev_info *revs, const char *prefix)
512 {
513         memset(revs, 0, sizeof(*revs));
514
515         revs->abbrev = DEFAULT_ABBREV;
516         revs->ignore_merges = 1;
517         revs->simplify_history = 1;
518         revs->pruning.recursive = 1;
519         revs->pruning.add_remove = file_add_remove;
520         revs->pruning.change = file_change;
521         revs->lifo = 1;
522         revs->dense = 1;
523         revs->prefix = prefix;
524         revs->max_age = -1;
525         revs->min_age = -1;
526         revs->skip_count = -1;
527         revs->max_count = -1;
528
529         revs->prune_fn = NULL;
530         revs->prune_data = NULL;
531
532         revs->topo_setter = topo_sort_default_setter;
533         revs->topo_getter = topo_sort_default_getter;
534
535         revs->commit_format = CMIT_FMT_DEFAULT;
536
537         diff_setup(&revs->diffopt);
538 }
539
540 static void add_pending_commit_list(struct rev_info *revs,
541                                     struct commit_list *commit_list,
542                                     unsigned int flags)
543 {
544         while (commit_list) {
545                 struct object *object = &commit_list->item->object;
546                 object->flags |= flags;
547                 add_pending_object(revs, object, sha1_to_hex(object->sha1));
548                 commit_list = commit_list->next;
549         }
550 }
551
552 static void prepare_show_merge(struct rev_info *revs)
553 {
554         struct commit_list *bases;
555         struct commit *head, *other;
556         unsigned char sha1[20];
557         const char **prune = NULL;
558         int i, prune_num = 1; /* counting terminating NULL */
559
560         if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
561                 die("--merge without HEAD?");
562         if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
563                 die("--merge without MERGE_HEAD?");
564         add_pending_object(revs, &head->object, "HEAD");
565         add_pending_object(revs, &other->object, "MERGE_HEAD");
566         bases = get_merge_bases(head, other, 1);
567         while (bases) {
568                 struct commit *it = bases->item;
569                 struct commit_list *n = bases->next;
570                 free(bases);
571                 bases = n;
572                 it->object.flags |= UNINTERESTING;
573                 add_pending_object(revs, &it->object, "(merge-base)");
574         }
575
576         if (!active_nr)
577                 read_cache();
578         for (i = 0; i < active_nr; i++) {
579                 struct cache_entry *ce = active_cache[i];
580                 if (!ce_stage(ce))
581                         continue;
582                 if (ce_path_match(ce, revs->prune_data)) {
583                         prune_num++;
584                         prune = xrealloc(prune, sizeof(*prune) * prune_num);
585                         prune[prune_num-2] = ce->name;
586                         prune[prune_num-1] = NULL;
587                 }
588                 while ((i+1 < active_nr) &&
589                        ce_same_name(ce, active_cache[i+1]))
590                         i++;
591         }
592         revs->prune_data = prune;
593 }
594
595 int handle_revision_arg(const char *arg, struct rev_info *revs,
596                         int flags,
597                         int cant_be_filename)
598 {
599         char *dotdot;
600         struct object *object;
601         unsigned char sha1[20];
602         int local_flags;
603
604         dotdot = strstr(arg, "..");
605         if (dotdot) {
606                 unsigned char from_sha1[20];
607                 const char *next = dotdot + 2;
608                 const char *this = arg;
609                 int symmetric = *next == '.';
610                 unsigned int flags_exclude = flags ^ UNINTERESTING;
611
612                 *dotdot = 0;
613                 next += symmetric;
614
615                 if (!*next)
616                         next = "HEAD";
617                 if (dotdot == arg)
618                         this = "HEAD";
619                 if (!get_sha1(this, from_sha1) &&
620                     !get_sha1(next, sha1)) {
621                         struct commit *a, *b;
622                         struct commit_list *exclude;
623
624                         a = lookup_commit_reference(from_sha1);
625                         b = lookup_commit_reference(sha1);
626                         if (!a || !b) {
627                                 die(symmetric ?
628                                     "Invalid symmetric difference expression %s...%s" :
629                                     "Invalid revision range %s..%s",
630                                     arg, next);
631                         }
632
633                         if (!cant_be_filename) {
634                                 *dotdot = '.';
635                                 verify_non_filename(revs->prefix, arg);
636                         }
637
638                         if (symmetric) {
639                                 exclude = get_merge_bases(a, b, 1);
640                                 add_pending_commit_list(revs, exclude,
641                                                         flags_exclude);
642                                 free_commit_list(exclude);
643                                 a->object.flags |= flags;
644                         } else
645                                 a->object.flags |= flags_exclude;
646                         b->object.flags |= flags;
647                         add_pending_object(revs, &a->object, this);
648                         add_pending_object(revs, &b->object, next);
649                         return 0;
650                 }
651                 *dotdot = '.';
652         }
653         dotdot = strstr(arg, "^@");
654         if (dotdot && !dotdot[2]) {
655                 *dotdot = 0;
656                 if (add_parents_only(revs, arg, flags))
657                         return 0;
658                 *dotdot = '^';
659         }
660         dotdot = strstr(arg, "^!");
661         if (dotdot && !dotdot[2]) {
662                 *dotdot = 0;
663                 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
664                         *dotdot = '^';
665         }
666
667         local_flags = 0;
668         if (*arg == '^') {
669                 local_flags = UNINTERESTING;
670                 arg++;
671         }
672         if (get_sha1(arg, sha1))
673                 return -1;
674         if (!cant_be_filename)
675                 verify_non_filename(revs->prefix, arg);
676         object = get_reference(revs, arg, sha1, flags ^ local_flags);
677         add_pending_object(revs, object, arg);
678         return 0;
679 }
680
681 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
682 {
683         if (!revs->grep_filter) {
684                 struct grep_opt *opt = xcalloc(1, sizeof(*opt));
685                 opt->status_only = 1;
686                 opt->pattern_tail = &(opt->pattern_list);
687                 opt->regflags = REG_NEWLINE;
688                 revs->grep_filter = opt;
689         }
690         append_grep_pattern(revs->grep_filter, ptn,
691                             "command line", 0, what);
692 }
693
694 static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern)
695 {
696         char *pat;
697         const char *prefix;
698         int patlen, fldlen;
699
700         fldlen = strlen(field);
701         patlen = strlen(pattern);
702         pat = xmalloc(patlen + fldlen + 10);
703         prefix = ".*";
704         if (*pattern == '^') {
705                 prefix = "";
706                 pattern++;
707         }
708         sprintf(pat, "^%s %s%s", field, prefix, pattern);
709         add_grep(revs, pat, GREP_PATTERN_HEAD);
710 }
711
712 static void add_message_grep(struct rev_info *revs, const char *pattern)
713 {
714         add_grep(revs, pattern, GREP_PATTERN_BODY);
715 }
716
717 static void add_ignore_packed(struct rev_info *revs, const char *name)
718 {
719         int num = ++revs->num_ignore_packed;
720
721         revs->ignore_packed = xrealloc(revs->ignore_packed,
722                                        sizeof(const char **) * (num + 1));
723         revs->ignore_packed[num-1] = name;
724         revs->ignore_packed[num] = NULL;
725 }
726
727 /*
728  * Parse revision information, filling in the "rev_info" structure,
729  * and removing the used arguments from the argument list.
730  *
731  * Returns the number of arguments left that weren't recognized
732  * (which are also moved to the head of the argument list)
733  */
734 int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
735 {
736         int i, flags, seen_dashdash, show_merge;
737         const char **unrecognized = argv + 1;
738         int left = 1;
739         int all_match = 0;
740
741         /* First, search for "--" */
742         seen_dashdash = 0;
743         for (i = 1; i < argc; i++) {
744                 const char *arg = argv[i];
745                 if (strcmp(arg, "--"))
746                         continue;
747                 argv[i] = NULL;
748                 argc = i;
749                 revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
750                 seen_dashdash = 1;
751                 break;
752         }
753
754         flags = show_merge = 0;
755         for (i = 1; i < argc; i++) {
756                 const char *arg = argv[i];
757                 if (*arg == '-') {
758                         int opts;
759                         if (!strncmp(arg, "--max-count=", 12)) {
760                                 revs->max_count = atoi(arg + 12);
761                                 continue;
762                         }
763                         if (!strncmp(arg, "--skip=", 7)) {
764                                 revs->skip_count = atoi(arg + 7);
765                                 continue;
766                         }
767                         /* accept -<digit>, like traditional "head" */
768                         if ((*arg == '-') && isdigit(arg[1])) {
769                                 revs->max_count = atoi(arg + 1);
770                                 continue;
771                         }
772                         if (!strcmp(arg, "-n")) {
773                                 if (argc <= i + 1)
774                                         die("-n requires an argument");
775                                 revs->max_count = atoi(argv[++i]);
776                                 continue;
777                         }
778                         if (!strncmp(arg,"-n",2)) {
779                                 revs->max_count = atoi(arg + 2);
780                                 continue;
781                         }
782                         if (!strncmp(arg, "--max-age=", 10)) {
783                                 revs->max_age = atoi(arg + 10);
784                                 continue;
785                         }
786                         if (!strncmp(arg, "--since=", 8)) {
787                                 revs->max_age = approxidate(arg + 8);
788                                 continue;
789                         }
790                         if (!strncmp(arg, "--after=", 8)) {
791                                 revs->max_age = approxidate(arg + 8);
792                                 continue;
793                         }
794                         if (!strncmp(arg, "--min-age=", 10)) {
795                                 revs->min_age = atoi(arg + 10);
796                                 continue;
797                         }
798                         if (!strncmp(arg, "--before=", 9)) {
799                                 revs->min_age = approxidate(arg + 9);
800                                 continue;
801                         }
802                         if (!strncmp(arg, "--until=", 8)) {
803                                 revs->min_age = approxidate(arg + 8);
804                                 continue;
805                         }
806                         if (!strcmp(arg, "--all")) {
807                                 handle_all(revs, flags);
808                                 continue;
809                         }
810                         if (!strcmp(arg, "--not")) {
811                                 flags ^= UNINTERESTING;
812                                 continue;
813                         }
814                         if (!strcmp(arg, "--default")) {
815                                 if (++i >= argc)
816                                         die("bad --default argument");
817                                 def = argv[i];
818                                 continue;
819                         }
820                         if (!strcmp(arg, "--merge")) {
821                                 show_merge = 1;
822                                 continue;
823                         }
824                         if (!strcmp(arg, "--topo-order")) {
825                                 revs->topo_order = 1;
826                                 continue;
827                         }
828                         if (!strcmp(arg, "--date-order")) {
829                                 revs->lifo = 0;
830                                 revs->topo_order = 1;
831                                 continue;
832                         }
833                         if (!strcmp(arg, "--parents")) {
834                                 revs->parents = 1;
835                                 continue;
836                         }
837                         if (!strcmp(arg, "--dense")) {
838                                 revs->dense = 1;
839                                 continue;
840                         }
841                         if (!strcmp(arg, "--sparse")) {
842                                 revs->dense = 0;
843                                 continue;
844                         }
845                         if (!strcmp(arg, "--remove-empty")) {
846                                 revs->remove_empty_trees = 1;
847                                 continue;
848                         }
849                         if (!strcmp(arg, "--no-merges")) {
850                                 revs->no_merges = 1;
851                                 continue;
852                         }
853                         if (!strcmp(arg, "--boundary")) {
854                                 revs->boundary = 1;
855                                 continue;
856                         }
857                         if (!strcmp(arg, "--objects")) {
858                                 revs->tag_objects = 1;
859                                 revs->tree_objects = 1;
860                                 revs->blob_objects = 1;
861                                 continue;
862                         }
863                         if (!strcmp(arg, "--objects-edge")) {
864                                 revs->tag_objects = 1;
865                                 revs->tree_objects = 1;
866                                 revs->blob_objects = 1;
867                                 revs->edge_hint = 1;
868                                 continue;
869                         }
870                         if (!strcmp(arg, "--unpacked")) {
871                                 revs->unpacked = 1;
872                                 free(revs->ignore_packed);
873                                 revs->ignore_packed = NULL;
874                                 revs->num_ignore_packed = 0;
875                                 continue;
876                         }
877                         if (!strncmp(arg, "--unpacked=", 11)) {
878                                 revs->unpacked = 1;
879                                 add_ignore_packed(revs, arg+11);
880                                 continue;
881                         }
882                         if (!strcmp(arg, "-r")) {
883                                 revs->diff = 1;
884                                 revs->diffopt.recursive = 1;
885                                 continue;
886                         }
887                         if (!strcmp(arg, "-t")) {
888                                 revs->diff = 1;
889                                 revs->diffopt.recursive = 1;
890                                 revs->diffopt.tree_in_recursive = 1;
891                                 continue;
892                         }
893                         if (!strcmp(arg, "-m")) {
894                                 revs->ignore_merges = 0;
895                                 continue;
896                         }
897                         if (!strcmp(arg, "-c")) {
898                                 revs->diff = 1;
899                                 revs->dense_combined_merges = 0;
900                                 revs->combine_merges = 1;
901                                 continue;
902                         }
903                         if (!strcmp(arg, "--cc")) {
904                                 revs->diff = 1;
905                                 revs->dense_combined_merges = 1;
906                                 revs->combine_merges = 1;
907                                 continue;
908                         }
909                         if (!strcmp(arg, "-v")) {
910                                 revs->verbose_header = 1;
911                                 continue;
912                         }
913                         if (!strncmp(arg, "--pretty", 8)) {
914                                 revs->verbose_header = 1;
915                                 revs->commit_format = get_commit_format(arg+8);
916                                 continue;
917                         }
918                         if (!strcmp(arg, "--root")) {
919                                 revs->show_root_diff = 1;
920                                 continue;
921                         }
922                         if (!strcmp(arg, "--no-commit-id")) {
923                                 revs->no_commit_id = 1;
924                                 continue;
925                         }
926                         if (!strcmp(arg, "--always")) {
927                                 revs->always_show_header = 1;
928                                 continue;
929                         }
930                         if (!strcmp(arg, "--no-abbrev")) {
931                                 revs->abbrev = 0;
932                                 continue;
933                         }
934                         if (!strcmp(arg, "--abbrev")) {
935                                 revs->abbrev = DEFAULT_ABBREV;
936                                 continue;
937                         }
938                         if (!strncmp(arg, "--abbrev=", 9)) {
939                                 revs->abbrev = strtoul(arg + 9, NULL, 10);
940                                 if (revs->abbrev < MINIMUM_ABBREV)
941                                         revs->abbrev = MINIMUM_ABBREV;
942                                 else if (revs->abbrev > 40)
943                                         revs->abbrev = 40;
944                                 continue;
945                         }
946                         if (!strcmp(arg, "--abbrev-commit")) {
947                                 revs->abbrev_commit = 1;
948                                 continue;
949                         }
950                         if (!strcmp(arg, "--full-diff")) {
951                                 revs->diff = 1;
952                                 revs->full_diff = 1;
953                                 continue;
954                         }
955                         if (!strcmp(arg, "--full-history")) {
956                                 revs->simplify_history = 0;
957                                 continue;
958                         }
959                         if (!strcmp(arg, "--relative-date")) {
960                                 revs->relative_date = 1;
961                                 continue;
962                         }
963
964                         /*
965                          * Grepping the commit log
966                          */
967                         if (!strncmp(arg, "--author=", 9)) {
968                                 add_header_grep(revs, "author", arg+9);
969                                 continue;
970                         }
971                         if (!strncmp(arg, "--committer=", 12)) {
972                                 add_header_grep(revs, "committer", arg+12);
973                                 continue;
974                         }
975                         if (!strncmp(arg, "--grep=", 7)) {
976                                 add_message_grep(revs, arg+7);
977                                 continue;
978                         }
979                         if (!strcmp(arg, "--all-match")) {
980                                 all_match = 1;
981                                 continue;
982                         }
983
984                         opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
985                         if (opts > 0) {
986                                 revs->diff = 1;
987                                 i += opts - 1;
988                                 continue;
989                         }
990                         *unrecognized++ = arg;
991                         left++;
992                         continue;
993                 }
994
995                 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
996                         int j;
997                         if (seen_dashdash || *arg == '^')
998                                 die("bad revision '%s'", arg);
999
1000                         /* If we didn't have a "--":
1001                          * (1) all filenames must exist;
1002                          * (2) all rev-args must not be interpretable
1003                          *     as a valid filename.
1004                          * but the latter we have checked in the main loop.
1005                          */
1006                         for (j = i; j < argc; j++)
1007                                 verify_filename(revs->prefix, argv[j]);
1008
1009                         revs->prune_data = get_pathspec(revs->prefix,
1010                                                         argv + i);
1011                         break;
1012                 }
1013         }
1014
1015         if (show_merge)
1016                 prepare_show_merge(revs);
1017         if (def && !revs->pending.nr) {
1018                 unsigned char sha1[20];
1019                 struct object *object;
1020                 if (get_sha1(def, sha1))
1021                         die("bad default revision '%s'", def);
1022                 object = get_reference(revs, def, sha1, 0);
1023                 add_pending_object(revs, object, def);
1024         }
1025
1026         if (revs->topo_order)
1027                 revs->limited = 1;
1028
1029         if (revs->prune_data) {
1030                 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1031                 revs->prune_fn = try_to_simplify_commit;
1032                 if (!revs->full_diff)
1033                         diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1034         }
1035         if (revs->combine_merges) {
1036                 revs->ignore_merges = 0;
1037                 if (revs->dense_combined_merges && !revs->diffopt.output_format)
1038                         revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1039         }
1040         revs->diffopt.abbrev = revs->abbrev;
1041         if (diff_setup_done(&revs->diffopt) < 0)
1042                 die("diff_setup_done failed");
1043
1044         if (revs->grep_filter) {
1045                 revs->grep_filter->all_match = all_match;
1046                 compile_grep_patterns(revs->grep_filter);
1047         }
1048
1049         return left;
1050 }
1051
1052 void prepare_revision_walk(struct rev_info *revs)
1053 {
1054         int nr = revs->pending.nr;
1055         struct object_array_entry *list = revs->pending.objects;
1056
1057         revs->pending.nr = 0;
1058         revs->pending.alloc = 0;
1059         revs->pending.objects = NULL;
1060         while (--nr >= 0) {
1061                 struct commit *commit = handle_commit(revs, list->item, list->name);
1062                 if (commit) {
1063                         if (!(commit->object.flags & SEEN)) {
1064                                 commit->object.flags |= SEEN;
1065                                 insert_by_date(commit, &revs->commits);
1066                         }
1067                 }
1068                 list++;
1069         }
1070
1071         if (revs->no_walk)
1072                 return;
1073         if (revs->limited)
1074                 limit_list(revs);
1075         if (revs->topo_order)
1076                 sort_in_topological_order_fn(&revs->commits, revs->lifo,
1077                                              revs->topo_setter,
1078                                              revs->topo_getter);
1079 }
1080
1081 static int rewrite_one(struct rev_info *revs, struct commit **pp)
1082 {
1083         for (;;) {
1084                 struct commit *p = *pp;
1085                 if (!revs->limited)
1086                         add_parents_to_list(revs, p, &revs->commits);
1087                 if (p->parents && p->parents->next)
1088                         return 0;
1089                 if (p->object.flags & (TREECHANGE | UNINTERESTING))
1090                         return 0;
1091                 if (!p->parents)
1092                         return -1;
1093                 *pp = p->parents->item;
1094         }
1095 }
1096
1097 static void rewrite_parents(struct rev_info *revs, struct commit *commit)
1098 {
1099         struct commit_list **pp = &commit->parents;
1100         while (*pp) {
1101                 struct commit_list *parent = *pp;
1102                 if (rewrite_one(revs, &parent->item) < 0) {
1103                         *pp = parent->next;
1104                         continue;
1105                 }
1106                 pp = &parent->next;
1107         }
1108 }
1109
1110 static void mark_boundary_to_show(struct commit *commit)
1111 {
1112         struct commit_list *p = commit->parents;
1113         while (p) {
1114                 commit = p->item;
1115                 p = p->next;
1116                 if (commit->object.flags & BOUNDARY)
1117                         commit->object.flags |= BOUNDARY_SHOW;
1118         }
1119 }
1120
1121 static int commit_match(struct commit *commit, struct rev_info *opt)
1122 {
1123         if (!opt->grep_filter)
1124                 return 1;
1125         return grep_buffer(opt->grep_filter,
1126                            NULL, /* we say nothing, not even filename */
1127                            commit->buffer, strlen(commit->buffer));
1128 }
1129
1130 static struct commit *get_revision_1(struct rev_info *revs)
1131 {
1132         if (!revs->commits)
1133                 return NULL;
1134
1135         do {
1136                 struct commit_list *entry = revs->commits;
1137                 struct commit *commit = entry->item;
1138
1139                 revs->commits = entry->next;
1140                 free(entry);
1141
1142                 /*
1143                  * If we haven't done the list limiting, we need to look at
1144                  * the parents here. We also need to do the date-based limiting
1145                  * that we'd otherwise have done in limit_list().
1146                  */
1147                 if (!revs->limited) {
1148                         if (revs->max_age != -1 &&
1149                             (commit->date < revs->max_age))
1150                                 continue;
1151                         add_parents_to_list(revs, commit, &revs->commits);
1152                 }
1153                 if (commit->object.flags & SHOWN)
1154                         continue;
1155
1156                 if (revs->unpacked && has_sha1_pack(commit->object.sha1,
1157                                                     revs->ignore_packed))
1158                     continue;
1159
1160                 /* We want to show boundary commits only when their
1161                  * children are shown.  When path-limiter is in effect,
1162                  * rewrite_parents() drops some commits from getting shown,
1163                  * and there is no point showing boundary parents that
1164                  * are not shown.  After rewrite_parents() rewrites the
1165                  * parents of a commit that is shown, we mark the boundary
1166                  * parents with BOUNDARY_SHOW.
1167                  */
1168                 if (commit->object.flags & BOUNDARY_SHOW) {
1169                         commit->object.flags |= SHOWN;
1170                         return commit;
1171                 }
1172                 if (commit->object.flags & UNINTERESTING)
1173                         continue;
1174                 if (revs->min_age != -1 && (commit->date > revs->min_age))
1175                         continue;
1176                 if (revs->no_merges &&
1177                     commit->parents && commit->parents->next)
1178                         continue;
1179                 if (!commit_match(commit, revs))
1180                         continue;
1181                 if (revs->prune_fn && revs->dense) {
1182                         /* Commit without changes? */
1183                         if (!(commit->object.flags & TREECHANGE)) {
1184                                 /* drop merges unless we want parenthood */
1185                                 if (!revs->parents)
1186                                         continue;
1187                                 /* non-merge - always ignore it */
1188                                 if (!commit->parents || !commit->parents->next)
1189                                         continue;
1190                         }
1191                         if (revs->parents)
1192                                 rewrite_parents(revs, commit);
1193                 }
1194                 if (revs->boundary)
1195                         mark_boundary_to_show(commit);
1196                 commit->object.flags |= SHOWN;
1197                 return commit;
1198         } while (revs->commits);
1199         return NULL;
1200 }
1201
1202 struct commit *get_revision(struct rev_info *revs)
1203 {
1204         struct commit *c = NULL;
1205
1206         if (0 < revs->skip_count) {
1207                 while ((c = get_revision_1(revs)) != NULL) {
1208                         if (revs->skip_count-- <= 0)
1209                                 break;
1210                 }
1211         }
1212
1213         /* Check the max_count ... */
1214         switch (revs->max_count) {
1215         case -1:
1216                 break;
1217         case 0:
1218                 return NULL;
1219         default:
1220                 revs->max_count--;
1221         }
1222         if (c)
1223                 return c;
1224         return get_revision_1(revs);
1225 }