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