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