Code

revision.c: explain what tree_difference does
[git.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"
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;
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 }
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 }
44 static void mark_blob_uninteresting(struct blob *blob)
45 {
46         if (blob->object.flags & UNINTERESTING)
47                 return;
48         blob->object.flags |= UNINTERESTING;
49 }
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;
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));
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         }
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 }
82 void mark_parents_uninteresting(struct commit *commit)
83 {
84         struct commit_list *parents = commit->parents;
86         while (parents) {
87                 struct commit *commit = parents->item;
88                 if (!(commit->object.flags & UNINTERESTING)) {
89                         commit->object.flags |= UNINTERESTING;
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                 }
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         }
117 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
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);
127 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
129         struct object *object;
131         object = parse_object(sha1);
132         if (!object)
133                 die("bad object %s", name);
134         object->flags |= flags;
135         return object;
138 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
140         unsigned long flags = object->flags;
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         }
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         }
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         }
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);
203 static int everybody_uninteresting(struct commit_list *orig)
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;
216 /*
217  * The goal is to get REV_TREE_NEW as the result only if the
218  * diff consists of all '+' (and no other changes), and
219  * REV_TREE_DIFFERENT otherwise (of course if the trees are
220  * the same we want REV_TREE_SAME).  That means that once we
221  * get to REV_TREE_DIFFERENT, we do not have to look any further.
222  */
223 static int tree_difference = REV_TREE_SAME;
225 static void file_add_remove(struct diff_options *options,
226                     int addremove, unsigned mode,
227                     const unsigned char *sha1,
228                     const char *base, const char *path)
230         int diff = REV_TREE_DIFFERENT;
232         /*
233          * Is it an add of a new file? It means that the old tree
234          * didn't have it at all, so we will turn "REV_TREE_SAME" ->
235          * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
236          * (and if it already was "REV_TREE_NEW", we'll keep it
237          * "REV_TREE_NEW" of course).
238          */
239         if (addremove == '+') {
240                 diff = tree_difference;
241                 if (diff != REV_TREE_SAME)
242                         return;
243                 diff = REV_TREE_NEW;
244         }
245         tree_difference = diff;
248 static void file_change(struct diff_options *options,
249                  unsigned old_mode, unsigned new_mode,
250                  const unsigned char *old_sha1,
251                  const unsigned char *new_sha1,
252                  const char *base, const char *path)
254         tree_difference = REV_TREE_DIFFERENT;
257 int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
259         if (!t1)
260                 return REV_TREE_NEW;
261         if (!t2)
262                 return REV_TREE_DIFFERENT;
263         tree_difference = REV_TREE_SAME;
264         if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
265                            &revs->pruning) < 0)
266                 return REV_TREE_DIFFERENT;
267         return tree_difference;
270 int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
272         int retval;
273         void *tree;
274         struct tree_desc empty, real;
276         if (!t1)
277                 return 0;
279         tree = read_object_with_reference(t1->object.sha1, tree_type, &real.size, NULL);
280         if (!tree)
281                 return 0;
282         real.buf = tree;
284         empty.buf = "";
285         empty.size = 0;
287         tree_difference = REV_TREE_SAME;
288         retval = diff_tree(&empty, &real, "", &revs->pruning);
289         free(tree);
291         return retval >= 0 && (tree_difference == REV_TREE_SAME);
294 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
296         struct commit_list **pp, *parent;
297         int tree_changed = 0, tree_same = 0;
299         if (!commit->tree)
300                 return;
302         if (!commit->parents) {
303                 if (!rev_same_tree_as_empty(revs, commit->tree))
304                         commit->object.flags |= TREECHANGE;
305                 return;
306         }
308         pp = &commit->parents;
309         while ((parent = *pp) != NULL) {
310                 struct commit *p = parent->item;
312                 parse_commit(p);
313                 switch (rev_compare_tree(revs, p->tree, commit->tree)) {
314                 case REV_TREE_SAME:
315                         tree_same = 1;
316                         if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
317                                 /* Even if a merge with an uninteresting
318                                  * side branch brought the entire change
319                                  * we are interested in, we do not want
320                                  * to lose the other branches of this
321                                  * merge, so we just keep going.
322                                  */
323                                 pp = &parent->next;
324                                 continue;
325                         }
326                         parent->next = NULL;
327                         commit->parents = parent;
328                         return;
330                 case REV_TREE_NEW:
331                         if (revs->remove_empty_trees &&
332                             rev_same_tree_as_empty(revs, p->tree)) {
333                                 /* We are adding all the specified
334                                  * paths from this parent, so the
335                                  * history beyond this parent is not
336                                  * interesting.  Remove its parents
337                                  * (they are grandparents for us).
338                                  * IOW, we pretend this parent is a
339                                  * "root" commit.
340                                  */
341                                 parse_commit(p);
342                                 p->parents = NULL;
343                         }
344                 /* fallthrough */
345                 case REV_TREE_DIFFERENT:
346                         tree_changed = 1;
347                         pp = &parent->next;
348                         continue;
349                 }
350                 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
351         }
352         if (tree_changed && !tree_same)
353                 commit->object.flags |= TREECHANGE;
356 static void add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
358         struct commit_list *parent = commit->parents;
359         unsigned left_flag;
361         if (commit->object.flags & ADDED)
362                 return;
363         commit->object.flags |= ADDED;
365         /*
366          * If the commit is uninteresting, don't try to
367          * prune parents - we want the maximal uninteresting
368          * set.
369          *
370          * Normally we haven't parsed the parent
371          * yet, so we won't have a parent of a parent
372          * here. However, it may turn out that we've
373          * reached this commit some other way (where it
374          * wasn't uninteresting), in which case we need
375          * to mark its parents recursively too..
376          */
377         if (commit->object.flags & UNINTERESTING) {
378                 while (parent) {
379                         struct commit *p = parent->item;
380                         parent = parent->next;
381                         parse_commit(p);
382                         p->object.flags |= UNINTERESTING;
383                         if (p->parents)
384                                 mark_parents_uninteresting(p);
385                         if (p->object.flags & SEEN)
386                                 continue;
387                         p->object.flags |= SEEN;
388                         insert_by_date(p, list);
389                 }
390                 return;
391         }
393         /*
394          * Ok, the commit wasn't uninteresting. Try to
395          * simplify the commit history and find the parent
396          * that has no differences in the path set if one exists.
397          */
398         if (revs->prune_fn)
399                 revs->prune_fn(revs, commit);
401         if (revs->no_walk)
402                 return;
404         left_flag = (commit->object.flags & SYMMETRIC_LEFT);
405         parent = commit->parents;
406         while (parent) {
407                 struct commit *p = parent->item;
409                 parent = parent->next;
411                 parse_commit(p);
412                 p->object.flags |= left_flag;
413                 if (p->object.flags & SEEN)
414                         continue;
415                 p->object.flags |= SEEN;
416                 insert_by_date(p, list);
417         }
420 static void limit_list(struct rev_info *revs)
422         struct commit_list *list = revs->commits;
423         struct commit_list *newlist = NULL;
424         struct commit_list **p = &newlist;
426         while (list) {
427                 struct commit_list *entry = list;
428                 struct commit *commit = list->item;
429                 struct object *obj = &commit->object;
431                 list = list->next;
432                 free(entry);
434                 if (revs->max_age != -1 && (commit->date < revs->max_age))
435                         obj->flags |= UNINTERESTING;
436                 add_parents_to_list(revs, commit, &list);
437                 if (obj->flags & UNINTERESTING) {
438                         mark_parents_uninteresting(commit);
439                         if (everybody_uninteresting(list))
440                                 break;
441                         continue;
442                 }
443                 if (revs->min_age != -1 && (commit->date > revs->min_age))
444                         continue;
445                 p = &commit_list_insert(commit, p)->next;
446         }
447         revs->commits = newlist;
450 struct all_refs_cb {
451         int all_flags;
452         int warned_bad_reflog;
453         struct rev_info *all_revs;
454         const char *name_for_errormsg;
455 };
457 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
459         struct all_refs_cb *cb = cb_data;
460         struct object *object = get_reference(cb->all_revs, path, sha1,
461                                               cb->all_flags);
462         add_pending_object(cb->all_revs, object, path);
463         return 0;
466 static void handle_all(struct rev_info *revs, unsigned flags)
468         struct all_refs_cb cb;
469         cb.all_revs = revs;
470         cb.all_flags = flags;
471         for_each_ref(handle_one_ref, &cb);
474 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
476         struct all_refs_cb *cb = cb_data;
477         if (!is_null_sha1(sha1)) {
478                 struct object *o = parse_object(sha1);
479                 if (o) {
480                         o->flags |= cb->all_flags;
481                         add_pending_object(cb->all_revs, o, "");
482                 }
483                 else if (!cb->warned_bad_reflog) {
484                         warn("reflog of '%s' references pruned commits",
485                                 cb->name_for_errormsg);
486                         cb->warned_bad_reflog = 1;
487                 }
488         }
491 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
492                 const char *email, unsigned long timestamp, int tz,
493                 const char *message, void *cb_data)
495         handle_one_reflog_commit(osha1, cb_data);
496         handle_one_reflog_commit(nsha1, cb_data);
497         return 0;
500 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
502         struct all_refs_cb *cb = cb_data;
503         cb->warned_bad_reflog = 0;
504         cb->name_for_errormsg = path;
505         for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
506         return 0;
509 static void handle_reflog(struct rev_info *revs, unsigned flags)
511         struct all_refs_cb cb;
512         cb.all_revs = revs;
513         cb.all_flags = flags;
514         for_each_reflog(handle_one_reflog, &cb);
517 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
519         unsigned char sha1[20];
520         struct object *it;
521         struct commit *commit;
522         struct commit_list *parents;
524         if (*arg == '^') {
525                 flags ^= UNINTERESTING;
526                 arg++;
527         }
528         if (get_sha1(arg, sha1))
529                 return 0;
530         while (1) {
531                 it = get_reference(revs, arg, sha1, 0);
532                 if (it->type != OBJ_TAG)
533                         break;
534                 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
535         }
536         if (it->type != OBJ_COMMIT)
537                 return 0;
538         commit = (struct commit *)it;
539         for (parents = commit->parents; parents; parents = parents->next) {
540                 it = &parents->item->object;
541                 it->flags |= flags;
542                 add_pending_object(revs, it, arg);
543         }
544         return 1;
547 void init_revisions(struct rev_info *revs, const char *prefix)
549         memset(revs, 0, sizeof(*revs));
551         revs->abbrev = DEFAULT_ABBREV;
552         revs->ignore_merges = 1;
553         revs->simplify_history = 1;
554         revs->pruning.recursive = 1;
555         revs->pruning.add_remove = file_add_remove;
556         revs->pruning.change = file_change;
557         revs->lifo = 1;
558         revs->dense = 1;
559         revs->prefix = prefix;
560         revs->max_age = -1;
561         revs->min_age = -1;
562         revs->skip_count = -1;
563         revs->max_count = -1;
565         revs->prune_fn = NULL;
566         revs->prune_data = NULL;
568         revs->topo_setter = topo_sort_default_setter;
569         revs->topo_getter = topo_sort_default_getter;
571         revs->commit_format = CMIT_FMT_DEFAULT;
573         diff_setup(&revs->diffopt);
576 static void add_pending_commit_list(struct rev_info *revs,
577                                     struct commit_list *commit_list,
578                                     unsigned int flags)
580         while (commit_list) {
581                 struct object *object = &commit_list->item->object;
582                 object->flags |= flags;
583                 add_pending_object(revs, object, sha1_to_hex(object->sha1));
584                 commit_list = commit_list->next;
585         }
588 static void prepare_show_merge(struct rev_info *revs)
590         struct commit_list *bases;
591         struct commit *head, *other;
592         unsigned char sha1[20];
593         const char **prune = NULL;
594         int i, prune_num = 1; /* counting terminating NULL */
596         if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
597                 die("--merge without HEAD?");
598         if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
599                 die("--merge without MERGE_HEAD?");
600         add_pending_object(revs, &head->object, "HEAD");
601         add_pending_object(revs, &other->object, "MERGE_HEAD");
602         bases = get_merge_bases(head, other, 1);
603         while (bases) {
604                 struct commit *it = bases->item;
605                 struct commit_list *n = bases->next;
606                 free(bases);
607                 bases = n;
608                 it->object.flags |= UNINTERESTING;
609                 add_pending_object(revs, &it->object, "(merge-base)");
610         }
612         if (!active_nr)
613                 read_cache();
614         for (i = 0; i < active_nr; i++) {
615                 struct cache_entry *ce = active_cache[i];
616                 if (!ce_stage(ce))
617                         continue;
618                 if (ce_path_match(ce, revs->prune_data)) {
619                         prune_num++;
620                         prune = xrealloc(prune, sizeof(*prune) * prune_num);
621                         prune[prune_num-2] = ce->name;
622                         prune[prune_num-1] = NULL;
623                 }
624                 while ((i+1 < active_nr) &&
625                        ce_same_name(ce, active_cache[i+1]))
626                         i++;
627         }
628         revs->prune_data = prune;
631 int handle_revision_arg(const char *arg, struct rev_info *revs,
632                         int flags,
633                         int cant_be_filename)
635         char *dotdot;
636         struct object *object;
637         unsigned char sha1[20];
638         int local_flags;
640         dotdot = strstr(arg, "..");
641         if (dotdot) {
642                 unsigned char from_sha1[20];
643                 const char *next = dotdot + 2;
644                 const char *this = arg;
645                 int symmetric = *next == '.';
646                 unsigned int flags_exclude = flags ^ UNINTERESTING;
648                 *dotdot = 0;
649                 next += symmetric;
651                 if (!*next)
652                         next = "HEAD";
653                 if (dotdot == arg)
654                         this = "HEAD";
655                 if (!get_sha1(this, from_sha1) &&
656                     !get_sha1(next, sha1)) {
657                         struct commit *a, *b;
658                         struct commit_list *exclude;
660                         a = lookup_commit_reference(from_sha1);
661                         b = lookup_commit_reference(sha1);
662                         if (!a || !b) {
663                                 die(symmetric ?
664                                     "Invalid symmetric difference expression %s...%s" :
665                                     "Invalid revision range %s..%s",
666                                     arg, next);
667                         }
669                         if (!cant_be_filename) {
670                                 *dotdot = '.';
671                                 verify_non_filename(revs->prefix, arg);
672                         }
674                         if (symmetric) {
675                                 exclude = get_merge_bases(a, b, 1);
676                                 add_pending_commit_list(revs, exclude,
677                                                         flags_exclude);
678                                 free_commit_list(exclude);
679                                 a->object.flags |= flags | SYMMETRIC_LEFT;
680                         } else
681                                 a->object.flags |= flags_exclude;
682                         b->object.flags |= flags;
683                         add_pending_object(revs, &a->object, this);
684                         add_pending_object(revs, &b->object, next);
685                         return 0;
686                 }
687                 *dotdot = '.';
688         }
689         dotdot = strstr(arg, "^@");
690         if (dotdot && !dotdot[2]) {
691                 *dotdot = 0;
692                 if (add_parents_only(revs, arg, flags))
693                         return 0;
694                 *dotdot = '^';
695         }
696         dotdot = strstr(arg, "^!");
697         if (dotdot && !dotdot[2]) {
698                 *dotdot = 0;
699                 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
700                         *dotdot = '^';
701         }
703         local_flags = 0;
704         if (*arg == '^') {
705                 local_flags = UNINTERESTING;
706                 arg++;
707         }
708         if (get_sha1(arg, sha1))
709                 return -1;
710         if (!cant_be_filename)
711                 verify_non_filename(revs->prefix, arg);
712         object = get_reference(revs, arg, sha1, flags ^ local_flags);
713         add_pending_object(revs, object, arg);
714         return 0;
717 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
719         if (!revs->grep_filter) {
720                 struct grep_opt *opt = xcalloc(1, sizeof(*opt));
721                 opt->status_only = 1;
722                 opt->pattern_tail = &(opt->pattern_list);
723                 opt->regflags = REG_NEWLINE;
724                 revs->grep_filter = opt;
725         }
726         append_grep_pattern(revs->grep_filter, ptn,
727                             "command line", 0, what);
730 static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern)
732         char *pat;
733         const char *prefix;
734         int patlen, fldlen;
736         fldlen = strlen(field);
737         patlen = strlen(pattern);
738         pat = xmalloc(patlen + fldlen + 10);
739         prefix = ".*";
740         if (*pattern == '^') {
741                 prefix = "";
742                 pattern++;
743         }
744         sprintf(pat, "^%s %s%s", field, prefix, pattern);
745         add_grep(revs, pat, GREP_PATTERN_HEAD);
748 static void add_message_grep(struct rev_info *revs, const char *pattern)
750         add_grep(revs, pattern, GREP_PATTERN_BODY);
753 static void add_ignore_packed(struct rev_info *revs, const char *name)
755         int num = ++revs->num_ignore_packed;
757         revs->ignore_packed = xrealloc(revs->ignore_packed,
758                                        sizeof(const char **) * (num + 1));
759         revs->ignore_packed[num-1] = name;
760         revs->ignore_packed[num] = NULL;
763 /*
764  * Parse revision information, filling in the "rev_info" structure,
765  * and removing the used arguments from the argument list.
766  *
767  * Returns the number of arguments left that weren't recognized
768  * (which are also moved to the head of the argument list)
769  */
770 int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
772         int i, flags, seen_dashdash, show_merge;
773         const char **unrecognized = argv + 1;
774         int left = 1;
775         int all_match = 0;
777         /* First, search for "--" */
778         seen_dashdash = 0;
779         for (i = 1; i < argc; i++) {
780                 const char *arg = argv[i];
781                 if (strcmp(arg, "--"))
782                         continue;
783                 argv[i] = NULL;
784                 argc = i;
785                 revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
786                 seen_dashdash = 1;
787                 break;
788         }
790         flags = show_merge = 0;
791         for (i = 1; i < argc; i++) {
792                 const char *arg = argv[i];
793                 if (*arg == '-') {
794                         int opts;
795                         if (!prefixcmp(arg, "--max-count=")) {
796                                 revs->max_count = atoi(arg + 12);
797                                 continue;
798                         }
799                         if (!prefixcmp(arg, "--skip=")) {
800                                 revs->skip_count = atoi(arg + 7);
801                                 continue;
802                         }
803                         /* accept -<digit>, like traditional "head" */
804                         if ((*arg == '-') && isdigit(arg[1])) {
805                                 revs->max_count = atoi(arg + 1);
806                                 continue;
807                         }
808                         if (!strcmp(arg, "-n")) {
809                                 if (argc <= i + 1)
810                                         die("-n requires an argument");
811                                 revs->max_count = atoi(argv[++i]);
812                                 continue;
813                         }
814                         if (!prefixcmp(arg, "-n")) {
815                                 revs->max_count = atoi(arg + 2);
816                                 continue;
817                         }
818                         if (!prefixcmp(arg, "--max-age=")) {
819                                 revs->max_age = atoi(arg + 10);
820                                 continue;
821                         }
822                         if (!prefixcmp(arg, "--since=")) {
823                                 revs->max_age = approxidate(arg + 8);
824                                 continue;
825                         }
826                         if (!prefixcmp(arg, "--after=")) {
827                                 revs->max_age = approxidate(arg + 8);
828                                 continue;
829                         }
830                         if (!prefixcmp(arg, "--min-age=")) {
831                                 revs->min_age = atoi(arg + 10);
832                                 continue;
833                         }
834                         if (!prefixcmp(arg, "--before=")) {
835                                 revs->min_age = approxidate(arg + 9);
836                                 continue;
837                         }
838                         if (!prefixcmp(arg, "--until=")) {
839                                 revs->min_age = approxidate(arg + 8);
840                                 continue;
841                         }
842                         if (!strcmp(arg, "--all")) {
843                                 handle_all(revs, flags);
844                                 continue;
845                         }
846                         if (!strcmp(arg, "--reflog")) {
847                                 handle_reflog(revs, flags);
848                                 continue;
849                         }
850                         if (!strcmp(arg, "-g") ||
851                                         !strcmp(arg, "--walk-reflogs")) {
852                                 init_reflog_walk(&revs->reflog_info);
853                                 continue;
854                         }
855                         if (!strcmp(arg, "--not")) {
856                                 flags ^= UNINTERESTING;
857                                 continue;
858                         }
859                         if (!strcmp(arg, "--default")) {
860                                 if (++i >= argc)
861                                         die("bad --default argument");
862                                 def = argv[i];
863                                 continue;
864                         }
865                         if (!strcmp(arg, "--merge")) {
866                                 show_merge = 1;
867                                 continue;
868                         }
869                         if (!strcmp(arg, "--topo-order")) {
870                                 revs->topo_order = 1;
871                                 continue;
872                         }
873                         if (!strcmp(arg, "--date-order")) {
874                                 revs->lifo = 0;
875                                 revs->topo_order = 1;
876                                 continue;
877                         }
878                         if (!strcmp(arg, "--parents")) {
879                                 revs->parents = 1;
880                                 continue;
881                         }
882                         if (!strcmp(arg, "--dense")) {
883                                 revs->dense = 1;
884                                 continue;
885                         }
886                         if (!strcmp(arg, "--sparse")) {
887                                 revs->dense = 0;
888                                 continue;
889                         }
890                         if (!strcmp(arg, "--remove-empty")) {
891                                 revs->remove_empty_trees = 1;
892                                 continue;
893                         }
894                         if (!strcmp(arg, "--no-merges")) {
895                                 revs->no_merges = 1;
896                                 continue;
897                         }
898                         if (!strcmp(arg, "--boundary")) {
899                                 revs->boundary = 1;
900                                 continue;
901                         }
902                         if (!strcmp(arg, "--left-right")) {
903                                 revs->left_right = 1;
904                                 continue;
905                         }
906                         if (!strcmp(arg, "--objects")) {
907                                 revs->tag_objects = 1;
908                                 revs->tree_objects = 1;
909                                 revs->blob_objects = 1;
910                                 continue;
911                         }
912                         if (!strcmp(arg, "--objects-edge")) {
913                                 revs->tag_objects = 1;
914                                 revs->tree_objects = 1;
915                                 revs->blob_objects = 1;
916                                 revs->edge_hint = 1;
917                                 continue;
918                         }
919                         if (!strcmp(arg, "--unpacked")) {
920                                 revs->unpacked = 1;
921                                 free(revs->ignore_packed);
922                                 revs->ignore_packed = NULL;
923                                 revs->num_ignore_packed = 0;
924                                 continue;
925                         }
926                         if (!prefixcmp(arg, "--unpacked=")) {
927                                 revs->unpacked = 1;
928                                 add_ignore_packed(revs, arg+11);
929                                 continue;
930                         }
931                         if (!strcmp(arg, "-r")) {
932                                 revs->diff = 1;
933                                 revs->diffopt.recursive = 1;
934                                 continue;
935                         }
936                         if (!strcmp(arg, "-t")) {
937                                 revs->diff = 1;
938                                 revs->diffopt.recursive = 1;
939                                 revs->diffopt.tree_in_recursive = 1;
940                                 continue;
941                         }
942                         if (!strcmp(arg, "-m")) {
943                                 revs->ignore_merges = 0;
944                                 continue;
945                         }
946                         if (!strcmp(arg, "-c")) {
947                                 revs->diff = 1;
948                                 revs->dense_combined_merges = 0;
949                                 revs->combine_merges = 1;
950                                 continue;
951                         }
952                         if (!strcmp(arg, "--cc")) {
953                                 revs->diff = 1;
954                                 revs->dense_combined_merges = 1;
955                                 revs->combine_merges = 1;
956                                 continue;
957                         }
958                         if (!strcmp(arg, "-v")) {
959                                 revs->verbose_header = 1;
960                                 continue;
961                         }
962                         if (!prefixcmp(arg, "--pretty")) {
963                                 revs->verbose_header = 1;
964                                 revs->commit_format = get_commit_format(arg+8);
965                                 continue;
966                         }
967                         if (!strcmp(arg, "--root")) {
968                                 revs->show_root_diff = 1;
969                                 continue;
970                         }
971                         if (!strcmp(arg, "--no-commit-id")) {
972                                 revs->no_commit_id = 1;
973                                 continue;
974                         }
975                         if (!strcmp(arg, "--always")) {
976                                 revs->always_show_header = 1;
977                                 continue;
978                         }
979                         if (!strcmp(arg, "--no-abbrev")) {
980                                 revs->abbrev = 0;
981                                 continue;
982                         }
983                         if (!strcmp(arg, "--abbrev")) {
984                                 revs->abbrev = DEFAULT_ABBREV;
985                                 continue;
986                         }
987                         if (!prefixcmp(arg, "--abbrev=")) {
988                                 revs->abbrev = strtoul(arg + 9, NULL, 10);
989                                 if (revs->abbrev < MINIMUM_ABBREV)
990                                         revs->abbrev = MINIMUM_ABBREV;
991                                 else if (revs->abbrev > 40)
992                                         revs->abbrev = 40;
993                                 continue;
994                         }
995                         if (!strcmp(arg, "--abbrev-commit")) {
996                                 revs->abbrev_commit = 1;
997                                 continue;
998                         }
999                         if (!strcmp(arg, "--full-diff")) {
1000                                 revs->diff = 1;
1001                                 revs->full_diff = 1;
1002                                 continue;
1003                         }
1004                         if (!strcmp(arg, "--full-history")) {
1005                                 revs->simplify_history = 0;
1006                                 continue;
1007                         }
1008                         if (!strcmp(arg, "--relative-date")) {
1009                                 revs->relative_date = 1;
1010                                 continue;
1011                         }
1013                         /*
1014                          * Grepping the commit log
1015                          */
1016                         if (!prefixcmp(arg, "--author=")) {
1017                                 add_header_grep(revs, "author", arg+9);
1018                                 continue;
1019                         }
1020                         if (!prefixcmp(arg, "--committer=")) {
1021                                 add_header_grep(revs, "committer", arg+12);
1022                                 continue;
1023                         }
1024                         if (!prefixcmp(arg, "--grep=")) {
1025                                 add_message_grep(revs, arg+7);
1026                                 continue;
1027                         }
1028                         if (!strcmp(arg, "--all-match")) {
1029                                 all_match = 1;
1030                                 continue;
1031                         }
1032                         if (!prefixcmp(arg, "--encoding=")) {
1033                                 arg += 11;
1034                                 if (strcmp(arg, "none"))
1035                                         git_log_output_encoding = strdup(arg);
1036                                 else
1037                                         git_log_output_encoding = "";
1038                                 continue;
1039                         }
1040                         if (!strcmp(arg, "--reverse")) {
1041                                 revs->reverse ^= 1;
1042                                 continue;
1043                         }
1045                         opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
1046                         if (opts > 0) {
1047                                 revs->diff = 1;
1048                                 i += opts - 1;
1049                                 continue;
1050                         }
1051                         *unrecognized++ = arg;
1052                         left++;
1053                         continue;
1054                 }
1056                 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1057                         int j;
1058                         if (seen_dashdash || *arg == '^')
1059                                 die("bad revision '%s'", arg);
1061                         /* If we didn't have a "--":
1062                          * (1) all filenames must exist;
1063                          * (2) all rev-args must not be interpretable
1064                          *     as a valid filename.
1065                          * but the latter we have checked in the main loop.
1066                          */
1067                         for (j = i; j < argc; j++)
1068                                 verify_filename(revs->prefix, argv[j]);
1070                         revs->prune_data = get_pathspec(revs->prefix,
1071                                                         argv + i);
1072                         break;
1073                 }
1074         }
1076         if (show_merge)
1077                 prepare_show_merge(revs);
1078         if (def && !revs->pending.nr) {
1079                 unsigned char sha1[20];
1080                 struct object *object;
1081                 if (get_sha1(def, sha1))
1082                         die("bad default revision '%s'", def);
1083                 object = get_reference(revs, def, sha1, 0);
1084                 add_pending_object(revs, object, def);
1085         }
1087         if (revs->topo_order)
1088                 revs->limited = 1;
1090         if (revs->prune_data) {
1091                 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1092                 revs->prune_fn = try_to_simplify_commit;
1093                 if (!revs->full_diff)
1094                         diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1095         }
1096         if (revs->combine_merges) {
1097                 revs->ignore_merges = 0;
1098                 if (revs->dense_combined_merges && !revs->diffopt.output_format)
1099                         revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1100         }
1101         revs->diffopt.abbrev = revs->abbrev;
1102         if (diff_setup_done(&revs->diffopt) < 0)
1103                 die("diff_setup_done failed");
1105         if (revs->grep_filter) {
1106                 revs->grep_filter->all_match = all_match;
1107                 compile_grep_patterns(revs->grep_filter);
1108         }
1110         return left;
1113 void prepare_revision_walk(struct rev_info *revs)
1115         int nr = revs->pending.nr;
1116         struct object_array_entry *e, *list;
1118         e = list = revs->pending.objects;
1119         revs->pending.nr = 0;
1120         revs->pending.alloc = 0;
1121         revs->pending.objects = NULL;
1122         while (--nr >= 0) {
1123                 struct commit *commit = handle_commit(revs, e->item, e->name);
1124                 if (commit) {
1125                         if (!(commit->object.flags & SEEN)) {
1126                                 commit->object.flags |= SEEN;
1127                                 insert_by_date(commit, &revs->commits);
1128                         }
1129                 }
1130                 e++;
1131         }
1132         free(list);
1134         if (revs->no_walk)
1135                 return;
1136         if (revs->limited)
1137                 limit_list(revs);
1138         if (revs->topo_order)
1139                 sort_in_topological_order_fn(&revs->commits, revs->lifo,
1140                                              revs->topo_setter,
1141                                              revs->topo_getter);
1144 static int rewrite_one(struct rev_info *revs, struct commit **pp)
1146         for (;;) {
1147                 struct commit *p = *pp;
1148                 if (!revs->limited)
1149                         add_parents_to_list(revs, p, &revs->commits);
1150                 if (p->parents && p->parents->next)
1151                         return 0;
1152                 if (p->object.flags & (TREECHANGE | UNINTERESTING))
1153                         return 0;
1154                 if (!p->parents)
1155                         return -1;
1156                 *pp = p->parents->item;
1157         }
1160 static void rewrite_parents(struct rev_info *revs, struct commit *commit)
1162         struct commit_list **pp = &commit->parents;
1163         while (*pp) {
1164                 struct commit_list *parent = *pp;
1165                 if (rewrite_one(revs, &parent->item) < 0) {
1166                         *pp = parent->next;
1167                         continue;
1168                 }
1169                 pp = &parent->next;
1170         }
1173 static int commit_match(struct commit *commit, struct rev_info *opt)
1175         if (!opt->grep_filter)
1176                 return 1;
1177         return grep_buffer(opt->grep_filter,
1178                            NULL, /* we say nothing, not even filename */
1179                            commit->buffer, strlen(commit->buffer));
1182 static struct commit *get_revision_1(struct rev_info *revs)
1184         if (!revs->commits)
1185                 return NULL;
1187         do {
1188                 struct commit_list *entry = revs->commits;
1189                 struct commit *commit = entry->item;
1191                 revs->commits = entry->next;
1192                 free(entry);
1194                 if (revs->reflog_info)
1195                         fake_reflog_parent(revs->reflog_info, commit);
1197                 /*
1198                  * If we haven't done the list limiting, we need to look at
1199                  * the parents here. We also need to do the date-based limiting
1200                  * that we'd otherwise have done in limit_list().
1201                  */
1202                 if (!revs->limited) {
1203                         if (revs->max_age != -1 &&
1204                             (commit->date < revs->max_age))
1205                                 continue;
1206                         add_parents_to_list(revs, commit, &revs->commits);
1207                 }
1208                 if (commit->object.flags & SHOWN)
1209                         continue;
1211                 if (revs->unpacked && has_sha1_pack(commit->object.sha1,
1212                                                     revs->ignore_packed))
1213                     continue;
1215                 if (commit->object.flags & UNINTERESTING)
1216                         continue;
1217                 if (revs->min_age != -1 && (commit->date > revs->min_age))
1218                         continue;
1219                 if (revs->no_merges &&
1220                     commit->parents && commit->parents->next)
1221                         continue;
1222                 if (!commit_match(commit, revs))
1223                         continue;
1224                 if (revs->prune_fn && revs->dense) {
1225                         /* Commit without changes? */
1226                         if (!(commit->object.flags & TREECHANGE)) {
1227                                 /* drop merges unless we want parenthood */
1228                                 if (!revs->parents)
1229                                         continue;
1230                                 /* non-merge - always ignore it */
1231                                 if (!commit->parents || !commit->parents->next)
1232                                         continue;
1233                         }
1234                         if (revs->parents)
1235                                 rewrite_parents(revs, commit);
1236                 }
1237                 return commit;
1238         } while (revs->commits);
1239         return NULL;
1242 static void gc_boundary(struct object_array *array)
1244         unsigned nr = array->nr;
1245         unsigned alloc = array->alloc;
1246         struct object_array_entry *objects = array->objects;
1248         if (alloc <= nr) {
1249                 unsigned i, j;
1250                 for (i = j = 0; i < nr; i++) {
1251                         if (objects[i].item->flags & SHOWN)
1252                                 continue;
1253                         if (i != j)
1254                                 objects[j] = objects[i];
1255                         j++;
1256                 }
1257                 for (i = j; i < nr; i++)
1258                         objects[i].item = NULL;
1259                 array->nr = j;
1260         }
1263 struct commit *get_revision(struct rev_info *revs)
1265         struct commit *c = NULL;
1266         struct commit_list *l;
1268         if (revs->boundary == 2) {
1269                 unsigned i;
1270                 struct object_array *array = &revs->boundary_commits;
1271                 struct object_array_entry *objects = array->objects;
1272                 for (i = 0; i < array->nr; i++) {
1273                         c = (struct commit *)(objects[i].item);
1274                         if (!c)
1275                                 continue;
1276                         if (!(c->object.flags & CHILD_SHOWN))
1277                                 continue;
1278                         if (!(c->object.flags & SHOWN))
1279                                 break;
1280                 }
1281                 if (array->nr <= i)
1282                         return NULL;
1284                 c->object.flags |= SHOWN | BOUNDARY;
1285                 return c;
1286         }
1288         if (revs->reverse) {
1289                 int limit = -1;
1291                 if (0 <= revs->max_count) {
1292                         limit = revs->max_count;
1293                         if (0 < revs->skip_count)
1294                                 limit += revs->skip_count;
1295                 }
1296                 l = NULL;
1297                 while ((c = get_revision_1(revs))) {
1298                         commit_list_insert(c, &l);
1299                         if ((0 < limit) && !--limit)
1300                                 break;
1301                 }
1302                 revs->commits = l;
1303                 revs->reverse = 0;
1304                 revs->max_count = -1;
1305                 c = NULL;
1306         }
1308         /*
1309          * Now pick up what they want to give us
1310          */
1311         c = get_revision_1(revs);
1312         if (c) {
1313                 while (0 < revs->skip_count) {
1314                         revs->skip_count--;
1315                         c = get_revision_1(revs);
1316                         if (!c)
1317                                 break;
1318                 }
1319         }
1321         /*
1322          * Check the max_count.
1323          */
1324         switch (revs->max_count) {
1325         case -1:
1326                 break;
1327         case 0:
1328                 c = NULL;
1329                 break;
1330         default:
1331                 revs->max_count--;
1332         }
1334         if (c)
1335                 c->object.flags |= SHOWN;
1337         if (!revs->boundary) {
1338                 return c;
1339         }
1341         if (!c) {
1342                 /*
1343                  * get_revision_1() runs out the commits, and
1344                  * we are done computing the boundaries.
1345                  * switch to boundary commits output mode.
1346                  */
1347                 revs->boundary = 2;
1348                 return get_revision(revs);
1349         }
1351         /*
1352          * boundary commits are the commits that are parents of the
1353          * ones we got from get_revision_1() but they themselves are
1354          * not returned from get_revision_1().  Before returning
1355          * 'c', we need to mark its parents that they could be boundaries.
1356          */
1358         for (l = c->parents; l; l = l->next) {
1359                 struct object *p;
1360                 p = &(l->item->object);
1361                 if (p->flags & (CHILD_SHOWN | SHOWN))
1362                         continue;
1363                 p->flags |= CHILD_SHOWN;
1364                 gc_boundary(&revs->boundary_commits);
1365                 add_object_array(p, NULL, &revs->boundary_commits);
1366         }
1368         return c;