tree-walk: finally switch over tree descriptors to contain a pre-parsed entry
[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 "graph.h"
10 #include "grep.h"
11 #include "reflog-walk.h"
12 #include "patch-ids.h"
13 #include "decorate.h"
14 #include "log-tree.h"
15 #include "string-list.h"
16 #include "line-log.h"
17 #include "mailmap.h"
18 #include "commit-slab.h"
19
20 volatile show_early_output_fn_t show_early_output;
21
22 char *path_name(const struct name_path *path, const char *name)
23 {
24         const struct name_path *p;
25         char *n, *m;
26         int nlen = strlen(name);
27         int len = nlen + 1;
28
29         for (p = path; p; p = p->up) {
30                 if (p->elem_len)
31                         len += p->elem_len + 1;
32         }
33         n = xmalloc(len);
34         m = n + len - (nlen + 1);
35         strcpy(m, name);
36         for (p = path; p; p = p->up) {
37                 if (p->elem_len) {
38                         m -= p->elem_len + 1;
39                         memcpy(m, p->elem, p->elem_len);
40                         m[p->elem_len] = '/';
41                 }
42         }
43         return n;
44 }
45
46 static int show_path_component_truncated(FILE *out, const char *name, int len)
47 {
48         int cnt;
49         for (cnt = 0; cnt < len; cnt++) {
50                 int ch = name[cnt];
51                 if (!ch || ch == '\n')
52                         return -1;
53                 fputc(ch, out);
54         }
55         return len;
56 }
57
58 static int show_path_truncated(FILE *out, const struct name_path *path)
59 {
60         int emitted, ours;
61
62         if (!path)
63                 return 0;
64         emitted = show_path_truncated(out, path->up);
65         if (emitted < 0)
66                 return emitted;
67         if (emitted)
68                 fputc('/', out);
69         ours = show_path_component_truncated(out, path->elem, path->elem_len);
70         if (ours < 0)
71                 return ours;
72         return ours || emitted;
73 }
74
75 void show_object_with_name(FILE *out, struct object *obj,
76                            const struct name_path *path, const char *component)
77 {
78         struct name_path leaf;
79         leaf.up = (struct name_path *)path;
80         leaf.elem = component;
81         leaf.elem_len = strlen(component);
82
83         fprintf(out, "%s ", sha1_to_hex(obj->sha1));
84         show_path_truncated(out, &leaf);
85         fputc('\n', out);
86 }
87
88 void add_object(struct object *obj,
89                 struct object_array *p,
90                 struct name_path *path,
91                 const char *name)
92 {
93         char *pn = path_name(path, name);
94         add_object_array(obj, pn, p);
95         free(pn);
96 }
97
98 static void mark_blob_uninteresting(struct blob *blob)
99 {
100         if (!blob)
101                 return;
102         if (blob->object.flags & UNINTERESTING)
103                 return;
104         blob->object.flags |= UNINTERESTING;
105 }
106
107 static void mark_tree_contents_uninteresting(struct tree *tree)
108 {
109         struct tree_desc desc;
110         struct name_entry entry;
111         struct object *obj = &tree->object;
112
113         if (!has_sha1_file(obj->sha1))
114                 return;
115         if (parse_tree(tree) < 0)
116                 die("bad tree %s", sha1_to_hex(obj->sha1));
117
118         init_tree_desc(&desc, tree->buffer, tree->size);
119         while (tree_entry(&desc, &entry)) {
120                 switch (object_type(entry.mode)) {
121                 case OBJ_TREE:
122                         mark_tree_uninteresting(lookup_tree(entry.sha1));
123                         break;
124                 case OBJ_BLOB:
125                         mark_blob_uninteresting(lookup_blob(entry.sha1));
126                         break;
127                 default:
128                         /* Subproject commit - not in this repository */
129                         break;
130                 }
131         }
132
133         /*
134          * We don't care about the tree any more
135          * after it has been marked uninteresting.
136          */
137         free_tree_buffer(tree);
138 }
139
140 void mark_tree_uninteresting(struct tree *tree)
141 {
142         struct object *obj = &tree->object;
143
144         if (!tree)
145                 return;
146         if (obj->flags & UNINTERESTING)
147                 return;
148         obj->flags |= UNINTERESTING;
149         mark_tree_contents_uninteresting(tree);
150 }
151
152 void mark_parents_uninteresting(struct commit *commit)
153 {
154         struct commit_list *parents = NULL, *l;
155
156         for (l = commit->parents; l; l = l->next)
157                 commit_list_insert(l->item, &parents);
158
159         while (parents) {
160                 struct commit *commit = parents->item;
161                 l = parents;
162                 parents = parents->next;
163                 free(l);
164
165                 while (commit) {
166                         /*
167                          * A missing commit is ok iff its parent is marked
168                          * uninteresting.
169                          *
170                          * We just mark such a thing parsed, so that when
171                          * it is popped next time around, we won't be trying
172                          * to parse it and get an error.
173                          */
174                         if (!has_sha1_file(commit->object.sha1))
175                                 commit->object.parsed = 1;
176
177                         if (commit->object.flags & UNINTERESTING)
178                                 break;
179
180                         commit->object.flags |= UNINTERESTING;
181
182                         /*
183                          * Normally we haven't parsed the parent
184                          * yet, so we won't have a parent of a parent
185                          * here. However, it may turn out that we've
186                          * reached this commit some other way (where it
187                          * wasn't uninteresting), in which case we need
188                          * to mark its parents recursively too..
189                          */
190                         if (!commit->parents)
191                                 break;
192
193                         for (l = commit->parents->next; l; l = l->next)
194                                 commit_list_insert(l->item, &parents);
195                         commit = commit->parents->item;
196                 }
197         }
198 }
199
200 static void add_pending_object_with_mode(struct rev_info *revs,
201                                          struct object *obj,
202                                          const char *name, unsigned mode)
203 {
204         if (!obj)
205                 return;
206         if (revs->no_walk && (obj->flags & UNINTERESTING))
207                 revs->no_walk = 0;
208         if (revs->reflog_info && obj->type == OBJ_COMMIT) {
209                 struct strbuf buf = STRBUF_INIT;
210                 int len = interpret_branch_name(name, 0, &buf);
211                 int st;
212
213                 if (0 < len && name[len] && buf.len)
214                         strbuf_addstr(&buf, name + len);
215                 st = add_reflog_for_walk(revs->reflog_info,
216                                          (struct commit *)obj,
217                                          buf.buf[0] ? buf.buf: name);
218                 strbuf_release(&buf);
219                 if (st)
220                         return;
221         }
222         add_object_array_with_mode(obj, name, &revs->pending, mode);
223 }
224
225 void add_pending_object(struct rev_info *revs,
226                         struct object *obj, const char *name)
227 {
228         add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
229 }
230
231 void add_head_to_pending(struct rev_info *revs)
232 {
233         unsigned char sha1[20];
234         struct object *obj;
235         if (get_sha1("HEAD", sha1))
236                 return;
237         obj = parse_object(sha1);
238         if (!obj)
239                 return;
240         add_pending_object(revs, obj, "HEAD");
241 }
242
243 static struct object *get_reference(struct rev_info *revs, const char *name,
244                                     const unsigned char *sha1,
245                                     unsigned int flags)
246 {
247         struct object *object;
248
249         object = parse_object(sha1);
250         if (!object) {
251                 if (revs->ignore_missing)
252                         return object;
253                 die("bad object %s", name);
254         }
255         object->flags |= flags;
256         return object;
257 }
258
259 void add_pending_sha1(struct rev_info *revs, const char *name,
260                       const unsigned char *sha1, unsigned int flags)
261 {
262         struct object *object = get_reference(revs, name, sha1, flags);
263         add_pending_object(revs, object, name);
264 }
265
266 static struct commit *handle_commit(struct rev_info *revs,
267                                     struct object *object, const char *name)
268 {
269         unsigned long flags = object->flags;
270
271         /*
272          * Tag object? Look what it points to..
273          */
274         while (object->type == OBJ_TAG) {
275                 struct tag *tag = (struct tag *) object;
276                 if (revs->tag_objects && !(flags & UNINTERESTING))
277                         add_pending_object(revs, object, tag->tag);
278                 if (!tag->tagged)
279                         die("bad tag");
280                 object = parse_object(tag->tagged->sha1);
281                 if (!object) {
282                         if (flags & UNINTERESTING)
283                                 return NULL;
284                         die("bad object %s", sha1_to_hex(tag->tagged->sha1));
285                 }
286                 object->flags |= flags;
287         }
288
289         /*
290          * Commit object? Just return it, we'll do all the complex
291          * reachability crud.
292          */
293         if (object->type == OBJ_COMMIT) {
294                 struct commit *commit = (struct commit *)object;
295                 if (parse_commit(commit) < 0)
296                         die("unable to parse commit %s", name);
297                 if (flags & UNINTERESTING) {
298                         mark_parents_uninteresting(commit);
299                         revs->limited = 1;
300                 }
301                 if (revs->show_source && !commit->util)
302                         commit->util = (void *) name;
303                 return commit;
304         }
305
306         /*
307          * Tree object? Either mark it uninteresting, or add it
308          * to the list of objects to look at later..
309          */
310         if (object->type == OBJ_TREE) {
311                 struct tree *tree = (struct tree *)object;
312                 if (!revs->tree_objects)
313                         return NULL;
314                 if (flags & UNINTERESTING) {
315                         mark_tree_contents_uninteresting(tree);
316                         return NULL;
317                 }
318                 add_pending_object(revs, object, "");
319                 return NULL;
320         }
321
322         /*
323          * Blob object? You know the drill by now..
324          */
325         if (object->type == OBJ_BLOB) {
326                 if (!revs->blob_objects)
327                         return NULL;
328                 if (flags & UNINTERESTING)
329                         return NULL;
330                 add_pending_object(revs, object, "");
331                 return NULL;
332         }
333         die("%s is unknown object", name);
334 }
335
336 static int everybody_uninteresting(struct commit_list *orig)
337 {
338         struct commit_list *list = orig;
339         while (list) {
340                 struct commit *commit = list->item;
341                 list = list->next;
342                 if (commit->object.flags & UNINTERESTING)
343                         continue;
344                 return 0;
345         }
346         return 1;
347 }
348
349 /*
350  * A definition of "relevant" commit that we can use to simplify limited graphs
351  * by eliminating side branches.
352  *
353  * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
354  * in our list), or that is a specified BOTTOM commit. Then after computing
355  * a limited list, during processing we can generally ignore boundary merges
356  * coming from outside the graph, (ie from irrelevant parents), and treat
357  * those merges as if they were single-parent. TREESAME is defined to consider
358  * only relevant parents, if any. If we are TREESAME to our on-graph parents,
359  * we don't care if we were !TREESAME to non-graph parents.
360  *
361  * Treating bottom commits as relevant ensures that a limited graph's
362  * connection to the actual bottom commit is not viewed as a side branch, but
363  * treated as part of the graph. For example:
364  *
365  *   ....Z...A---X---o---o---B
366  *        .     /
367  *         W---Y
368  *
369  * When computing "A..B", the A-X connection is at least as important as
370  * Y-X, despite A being flagged UNINTERESTING.
371  *
372  * And when computing --ancestry-path "A..B", the A-X connection is more
373  * important than Y-X, despite both A and Y being flagged UNINTERESTING.
374  */
375 static inline int relevant_commit(struct commit *commit)
376 {
377         return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
378 }
379
380 /*
381  * Return a single relevant commit from a parent list. If we are a TREESAME
382  * commit, and this selects one of our parents, then we can safely simplify to
383  * that parent.
384  */
385 static struct commit *one_relevant_parent(const struct rev_info *revs,
386                                           struct commit_list *orig)
387 {
388         struct commit_list *list = orig;
389         struct commit *relevant = NULL;
390
391         if (!orig)
392                 return NULL;
393
394         /*
395          * For 1-parent commits, or if first-parent-only, then return that
396          * first parent (even if not "relevant" by the above definition).
397          * TREESAME will have been set purely on that parent.
398          */
399         if (revs->first_parent_only || !orig->next)
400                 return orig->item;
401
402         /*
403          * For multi-parent commits, identify a sole relevant parent, if any.
404          * If we have only one relevant parent, then TREESAME will be set purely
405          * with regard to that parent, and we can simplify accordingly.
406          *
407          * If we have more than one relevant parent, or no relevant parents
408          * (and multiple irrelevant ones), then we can't select a parent here
409          * and return NULL.
410          */
411         while (list) {
412                 struct commit *commit = list->item;
413                 list = list->next;
414                 if (relevant_commit(commit)) {
415                         if (relevant)
416                                 return NULL;
417                         relevant = commit;
418                 }
419         }
420         return relevant;
421 }
422
423 /*
424  * The goal is to get REV_TREE_NEW as the result only if the
425  * diff consists of all '+' (and no other changes), REV_TREE_OLD
426  * if the whole diff is removal of old data, and otherwise
427  * REV_TREE_DIFFERENT (of course if the trees are the same we
428  * want REV_TREE_SAME).
429  * That means that once we get to REV_TREE_DIFFERENT, we do not
430  * have to look any further.
431  */
432 static int tree_difference = REV_TREE_SAME;
433
434 static void file_add_remove(struct diff_options *options,
435                     int addremove, unsigned mode,
436                     const unsigned char *sha1,
437                     int sha1_valid,
438                     const char *fullpath, unsigned dirty_submodule)
439 {
440         int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
441
442         tree_difference |= diff;
443         if (tree_difference == REV_TREE_DIFFERENT)
444                 DIFF_OPT_SET(options, HAS_CHANGES);
445 }
446
447 static void file_change(struct diff_options *options,
448                  unsigned old_mode, unsigned new_mode,
449                  const unsigned char *old_sha1,
450                  const unsigned char *new_sha1,
451                  int old_sha1_valid, int new_sha1_valid,
452                  const char *fullpath,
453                  unsigned old_dirty_submodule, unsigned new_dirty_submodule)
454 {
455         tree_difference = REV_TREE_DIFFERENT;
456         DIFF_OPT_SET(options, HAS_CHANGES);
457 }
458
459 static int rev_compare_tree(struct rev_info *revs,
460                             struct commit *parent, struct commit *commit)
461 {
462         struct tree *t1 = parent->tree;
463         struct tree *t2 = commit->tree;
464
465         if (!t1)
466                 return REV_TREE_NEW;
467         if (!t2)
468                 return REV_TREE_OLD;
469
470         if (revs->simplify_by_decoration) {
471                 /*
472                  * If we are simplifying by decoration, then the commit
473                  * is worth showing if it has a tag pointing at it.
474                  */
475                 if (lookup_decoration(&name_decoration, &commit->object))
476                         return REV_TREE_DIFFERENT;
477                 /*
478                  * A commit that is not pointed by a tag is uninteresting
479                  * if we are not limited by path.  This means that you will
480                  * see the usual "commits that touch the paths" plus any
481                  * tagged commit by specifying both --simplify-by-decoration
482                  * and pathspec.
483                  */
484                 if (!revs->prune_data.nr)
485                         return REV_TREE_SAME;
486         }
487
488         tree_difference = REV_TREE_SAME;
489         DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
490         if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
491                            &revs->pruning) < 0)
492                 return REV_TREE_DIFFERENT;
493         return tree_difference;
494 }
495
496 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
497 {
498         int retval;
499         struct tree *t1 = commit->tree;
500
501         if (!t1)
502                 return 0;
503
504         tree_difference = REV_TREE_SAME;
505         DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
506         retval = diff_tree_sha1(NULL, t1->object.sha1, "", &revs->pruning);
507
508         return retval >= 0 && (tree_difference == REV_TREE_SAME);
509 }
510
511 struct treesame_state {
512         unsigned int nparents;
513         unsigned char treesame[FLEX_ARRAY];
514 };
515
516 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
517 {
518         unsigned n = commit_list_count(commit->parents);
519         struct treesame_state *st = xcalloc(1, sizeof(*st) + n);
520         st->nparents = n;
521         add_decoration(&revs->treesame, &commit->object, st);
522         return st;
523 }
524
525 /*
526  * Must be called immediately after removing the nth_parent from a commit's
527  * parent list, if we are maintaining the per-parent treesame[] decoration.
528  * This does not recalculate the master TREESAME flag - update_treesame()
529  * should be called to update it after a sequence of treesame[] modifications
530  * that may have affected it.
531  */
532 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
533 {
534         struct treesame_state *st;
535         int old_same;
536
537         if (!commit->parents) {
538                 /*
539                  * Have just removed the only parent from a non-merge.
540                  * Different handling, as we lack decoration.
541                  */
542                 if (nth_parent != 0)
543                         die("compact_treesame %u", nth_parent);
544                 old_same = !!(commit->object.flags & TREESAME);
545                 if (rev_same_tree_as_empty(revs, commit))
546                         commit->object.flags |= TREESAME;
547                 else
548                         commit->object.flags &= ~TREESAME;
549                 return old_same;
550         }
551
552         st = lookup_decoration(&revs->treesame, &commit->object);
553         if (!st || nth_parent >= st->nparents)
554                 die("compact_treesame %u", nth_parent);
555
556         old_same = st->treesame[nth_parent];
557         memmove(st->treesame + nth_parent,
558                 st->treesame + nth_parent + 1,
559                 st->nparents - nth_parent - 1);
560
561         /*
562          * If we've just become a non-merge commit, update TREESAME
563          * immediately, and remove the no-longer-needed decoration.
564          * If still a merge, defer update until update_treesame().
565          */
566         if (--st->nparents == 1) {
567                 if (commit->parents->next)
568                         die("compact_treesame parents mismatch");
569                 if (st->treesame[0] && revs->dense)
570                         commit->object.flags |= TREESAME;
571                 else
572                         commit->object.flags &= ~TREESAME;
573                 free(add_decoration(&revs->treesame, &commit->object, NULL));
574         }
575
576         return old_same;
577 }
578
579 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
580 {
581         if (commit->parents && commit->parents->next) {
582                 unsigned n;
583                 struct treesame_state *st;
584                 struct commit_list *p;
585                 unsigned relevant_parents;
586                 unsigned relevant_change, irrelevant_change;
587
588                 st = lookup_decoration(&revs->treesame, &commit->object);
589                 if (!st)
590                         die("update_treesame %s", sha1_to_hex(commit->object.sha1));
591                 relevant_parents = 0;
592                 relevant_change = irrelevant_change = 0;
593                 for (p = commit->parents, n = 0; p; n++, p = p->next) {
594                         if (relevant_commit(p->item)) {
595                                 relevant_change |= !st->treesame[n];
596                                 relevant_parents++;
597                         } else
598                                 irrelevant_change |= !st->treesame[n];
599                 }
600                 if (relevant_parents ? relevant_change : irrelevant_change)
601                         commit->object.flags &= ~TREESAME;
602                 else
603                         commit->object.flags |= TREESAME;
604         }
605
606         return commit->object.flags & TREESAME;
607 }
608
609 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
610 {
611         /*
612          * TREESAME is irrelevant unless prune && dense;
613          * if simplify_history is set, we can't have a mixture of TREESAME and
614          *    !TREESAME INTERESTING parents (and we don't have treesame[]
615          *    decoration anyway);
616          * if first_parent_only is set, then the TREESAME flag is locked
617          *    against the first parent (and again we lack treesame[] decoration).
618          */
619         return revs->prune && revs->dense &&
620                !revs->simplify_history &&
621                !revs->first_parent_only;
622 }
623
624 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
625 {
626         struct commit_list **pp, *parent;
627         struct treesame_state *ts = NULL;
628         int relevant_change = 0, irrelevant_change = 0;
629         int relevant_parents, nth_parent;
630
631         /*
632          * If we don't do pruning, everything is interesting
633          */
634         if (!revs->prune)
635                 return;
636
637         if (!commit->tree)
638                 return;
639
640         if (!commit->parents) {
641                 if (rev_same_tree_as_empty(revs, commit))
642                         commit->object.flags |= TREESAME;
643                 return;
644         }
645
646         /*
647          * Normal non-merge commit? If we don't want to make the
648          * history dense, we consider it always to be a change..
649          */
650         if (!revs->dense && !commit->parents->next)
651                 return;
652
653         for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
654              (parent = *pp) != NULL;
655              pp = &parent->next, nth_parent++) {
656                 struct commit *p = parent->item;
657                 if (relevant_commit(p))
658                         relevant_parents++;
659
660                 if (nth_parent == 1) {
661                         /*
662                          * This our second loop iteration - so we now know
663                          * we're dealing with a merge.
664                          *
665                          * Do not compare with later parents when we care only about
666                          * the first parent chain, in order to avoid derailing the
667                          * traversal to follow a side branch that brought everything
668                          * in the path we are limited to by the pathspec.
669                          */
670                         if (revs->first_parent_only)
671                                 break;
672                         /*
673                          * If this will remain a potentially-simplifiable
674                          * merge, remember per-parent treesame if needed.
675                          * Initialise the array with the comparison from our
676                          * first iteration.
677                          */
678                         if (revs->treesame.name &&
679                             !revs->simplify_history &&
680                             !(commit->object.flags & UNINTERESTING)) {
681                                 ts = initialise_treesame(revs, commit);
682                                 if (!(irrelevant_change || relevant_change))
683                                         ts->treesame[0] = 1;
684                         }
685                 }
686                 if (parse_commit(p) < 0)
687                         die("cannot simplify commit %s (because of %s)",
688                             sha1_to_hex(commit->object.sha1),
689                             sha1_to_hex(p->object.sha1));
690                 switch (rev_compare_tree(revs, p, commit)) {
691                 case REV_TREE_SAME:
692                         if (!revs->simplify_history || !relevant_commit(p)) {
693                                 /* Even if a merge with an uninteresting
694                                  * side branch brought the entire change
695                                  * we are interested in, we do not want
696                                  * to lose the other branches of this
697                                  * merge, so we just keep going.
698                                  */
699                                 if (ts)
700                                         ts->treesame[nth_parent] = 1;
701                                 continue;
702                         }
703                         parent->next = NULL;
704                         commit->parents = parent;
705                         commit->object.flags |= TREESAME;
706                         return;
707
708                 case REV_TREE_NEW:
709                         if (revs->remove_empty_trees &&
710                             rev_same_tree_as_empty(revs, p)) {
711                                 /* We are adding all the specified
712                                  * paths from this parent, so the
713                                  * history beyond this parent is not
714                                  * interesting.  Remove its parents
715                                  * (they are grandparents for us).
716                                  * IOW, we pretend this parent is a
717                                  * "root" commit.
718                                  */
719                                 if (parse_commit(p) < 0)
720                                         die("cannot simplify commit %s (invalid %s)",
721                                             sha1_to_hex(commit->object.sha1),
722                                             sha1_to_hex(p->object.sha1));
723                                 p->parents = NULL;
724                         }
725                 /* fallthrough */
726                 case REV_TREE_OLD:
727                 case REV_TREE_DIFFERENT:
728                         if (relevant_commit(p))
729                                 relevant_change = 1;
730                         else
731                                 irrelevant_change = 1;
732                         continue;
733                 }
734                 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
735         }
736
737         /*
738          * TREESAME is straightforward for single-parent commits. For merge
739          * commits, it is most useful to define it so that "irrelevant"
740          * parents cannot make us !TREESAME - if we have any relevant
741          * parents, then we only consider TREESAMEness with respect to them,
742          * allowing irrelevant merges from uninteresting branches to be
743          * simplified away. Only if we have only irrelevant parents do we
744          * base TREESAME on them. Note that this logic is replicated in
745          * update_treesame, which should be kept in sync.
746          */
747         if (relevant_parents ? !relevant_change : !irrelevant_change)
748                 commit->object.flags |= TREESAME;
749 }
750
751 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
752                     struct commit_list *cached_base, struct commit_list **cache)
753 {
754         struct commit_list *new_entry;
755
756         if (cached_base && p->date < cached_base->item->date)
757                 new_entry = commit_list_insert_by_date(p, &cached_base->next);
758         else
759                 new_entry = commit_list_insert_by_date(p, head);
760
761         if (cache && (!*cache || p->date < (*cache)->item->date))
762                 *cache = new_entry;
763 }
764
765 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
766                     struct commit_list **list, struct commit_list **cache_ptr)
767 {
768         struct commit_list *parent = commit->parents;
769         unsigned left_flag;
770         struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
771
772         if (commit->object.flags & ADDED)
773                 return 0;
774         commit->object.flags |= ADDED;
775
776         /*
777          * If the commit is uninteresting, don't try to
778          * prune parents - we want the maximal uninteresting
779          * set.
780          *
781          * Normally we haven't parsed the parent
782          * yet, so we won't have a parent of a parent
783          * here. However, it may turn out that we've
784          * reached this commit some other way (where it
785          * wasn't uninteresting), in which case we need
786          * to mark its parents recursively too..
787          */
788         if (commit->object.flags & UNINTERESTING) {
789                 while (parent) {
790                         struct commit *p = parent->item;
791                         parent = parent->next;
792                         if (p)
793                                 p->object.flags |= UNINTERESTING;
794                         if (parse_commit(p) < 0)
795                                 continue;
796                         if (p->parents)
797                                 mark_parents_uninteresting(p);
798                         if (p->object.flags & SEEN)
799                                 continue;
800                         p->object.flags |= SEEN;
801                         commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
802                 }
803                 return 0;
804         }
805
806         /*
807          * Ok, the commit wasn't uninteresting. Try to
808          * simplify the commit history and find the parent
809          * that has no differences in the path set if one exists.
810          */
811         try_to_simplify_commit(revs, commit);
812
813         if (revs->no_walk)
814                 return 0;
815
816         left_flag = (commit->object.flags & SYMMETRIC_LEFT);
817
818         for (parent = commit->parents; parent; parent = parent->next) {
819                 struct commit *p = parent->item;
820
821                 if (parse_commit(p) < 0)
822                         return -1;
823                 if (revs->show_source && !p->util)
824                         p->util = commit->util;
825                 p->object.flags |= left_flag;
826                 if (!(p->object.flags & SEEN)) {
827                         p->object.flags |= SEEN;
828                         commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
829                 }
830                 if (revs->first_parent_only)
831                         break;
832         }
833         return 0;
834 }
835
836 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
837 {
838         struct commit_list *p;
839         int left_count = 0, right_count = 0;
840         int left_first;
841         struct patch_ids ids;
842         unsigned cherry_flag;
843
844         /* First count the commits on the left and on the right */
845         for (p = list; p; p = p->next) {
846                 struct commit *commit = p->item;
847                 unsigned flags = commit->object.flags;
848                 if (flags & BOUNDARY)
849                         ;
850                 else if (flags & SYMMETRIC_LEFT)
851                         left_count++;
852                 else
853                         right_count++;
854         }
855
856         if (!left_count || !right_count)
857                 return;
858
859         left_first = left_count < right_count;
860         init_patch_ids(&ids);
861         ids.diffopts.pathspec = revs->diffopt.pathspec;
862
863         /* Compute patch-ids for one side */
864         for (p = list; p; p = p->next) {
865                 struct commit *commit = p->item;
866                 unsigned flags = commit->object.flags;
867
868                 if (flags & BOUNDARY)
869                         continue;
870                 /*
871                  * If we have fewer left, left_first is set and we omit
872                  * commits on the right branch in this loop.  If we have
873                  * fewer right, we skip the left ones.
874                  */
875                 if (left_first != !!(flags & SYMMETRIC_LEFT))
876                         continue;
877                 commit->util = add_commit_patch_id(commit, &ids);
878         }
879
880         /* either cherry_mark or cherry_pick are true */
881         cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
882
883         /* Check the other side */
884         for (p = list; p; p = p->next) {
885                 struct commit *commit = p->item;
886                 struct patch_id *id;
887                 unsigned flags = commit->object.flags;
888
889                 if (flags & BOUNDARY)
890                         continue;
891                 /*
892                  * If we have fewer left, left_first is set and we omit
893                  * commits on the left branch in this loop.
894                  */
895                 if (left_first == !!(flags & SYMMETRIC_LEFT))
896                         continue;
897
898                 /*
899                  * Have we seen the same patch id?
900                  */
901                 id = has_commit_patch_id(commit, &ids);
902                 if (!id)
903                         continue;
904                 id->seen = 1;
905                 commit->object.flags |= cherry_flag;
906         }
907
908         /* Now check the original side for seen ones */
909         for (p = list; p; p = p->next) {
910                 struct commit *commit = p->item;
911                 struct patch_id *ent;
912
913                 ent = commit->util;
914                 if (!ent)
915                         continue;
916                 if (ent->seen)
917                         commit->object.flags |= cherry_flag;
918                 commit->util = NULL;
919         }
920
921         free_patch_ids(&ids);
922 }
923
924 /* How many extra uninteresting commits we want to see.. */
925 #define SLOP 5
926
927 static int still_interesting(struct commit_list *src, unsigned long date, int slop)
928 {
929         /*
930          * No source list at all? We're definitely done..
931          */
932         if (!src)
933                 return 0;
934
935         /*
936          * Does the destination list contain entries with a date
937          * before the source list? Definitely _not_ done.
938          */
939         if (date <= src->item->date)
940                 return SLOP;
941
942         /*
943          * Does the source list still have interesting commits in
944          * it? Definitely not done..
945          */
946         if (!everybody_uninteresting(src))
947                 return SLOP;
948
949         /* Ok, we're closing in.. */
950         return slop-1;
951 }
952
953 /*
954  * "rev-list --ancestry-path A..B" computes commits that are ancestors
955  * of B but not ancestors of A but further limits the result to those
956  * that are descendants of A.  This takes the list of bottom commits and
957  * the result of "A..B" without --ancestry-path, and limits the latter
958  * further to the ones that can reach one of the commits in "bottom".
959  */
960 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
961 {
962         struct commit_list *p;
963         struct commit_list *rlist = NULL;
964         int made_progress;
965
966         /*
967          * Reverse the list so that it will be likely that we would
968          * process parents before children.
969          */
970         for (p = list; p; p = p->next)
971                 commit_list_insert(p->item, &rlist);
972
973         for (p = bottom; p; p = p->next)
974                 p->item->object.flags |= TMP_MARK;
975
976         /*
977          * Mark the ones that can reach bottom commits in "list",
978          * in a bottom-up fashion.
979          */
980         do {
981                 made_progress = 0;
982                 for (p = rlist; p; p = p->next) {
983                         struct commit *c = p->item;
984                         struct commit_list *parents;
985                         if (c->object.flags & (TMP_MARK | UNINTERESTING))
986                                 continue;
987                         for (parents = c->parents;
988                              parents;
989                              parents = parents->next) {
990                                 if (!(parents->item->object.flags & TMP_MARK))
991                                         continue;
992                                 c->object.flags |= TMP_MARK;
993                                 made_progress = 1;
994                                 break;
995                         }
996                 }
997         } while (made_progress);
998
999         /*
1000          * NEEDSWORK: decide if we want to remove parents that are
1001          * not marked with TMP_MARK from commit->parents for commits
1002          * in the resulting list.  We may not want to do that, though.
1003          */
1004
1005         /*
1006          * The ones that are not marked with TMP_MARK are uninteresting
1007          */
1008         for (p = list; p; p = p->next) {
1009                 struct commit *c = p->item;
1010                 if (c->object.flags & TMP_MARK)
1011                         continue;
1012                 c->object.flags |= UNINTERESTING;
1013         }
1014
1015         /* We are done with the TMP_MARK */
1016         for (p = list; p; p = p->next)
1017                 p->item->object.flags &= ~TMP_MARK;
1018         for (p = bottom; p; p = p->next)
1019                 p->item->object.flags &= ~TMP_MARK;
1020         free_commit_list(rlist);
1021 }
1022
1023 /*
1024  * Before walking the history, keep the set of "negative" refs the
1025  * caller has asked to exclude.
1026  *
1027  * This is used to compute "rev-list --ancestry-path A..B", as we need
1028  * to filter the result of "A..B" further to the ones that can actually
1029  * reach A.
1030  */
1031 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1032 {
1033         struct commit_list *elem, *bottom = NULL;
1034         for (elem = list; elem; elem = elem->next)
1035                 if (elem->item->object.flags & BOTTOM)
1036                         commit_list_insert(elem->item, &bottom);
1037         return bottom;
1038 }
1039
1040 /* Assumes either left_only or right_only is set */
1041 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1042 {
1043         struct commit_list *p;
1044
1045         for (p = list; p; p = p->next) {
1046                 struct commit *commit = p->item;
1047
1048                 if (revs->right_only) {
1049                         if (commit->object.flags & SYMMETRIC_LEFT)
1050                                 commit->object.flags |= SHOWN;
1051                 } else  /* revs->left_only is set */
1052                         if (!(commit->object.flags & SYMMETRIC_LEFT))
1053                                 commit->object.flags |= SHOWN;
1054         }
1055 }
1056
1057 static int limit_list(struct rev_info *revs)
1058 {
1059         int slop = SLOP;
1060         unsigned long date = ~0ul;
1061         struct commit_list *list = revs->commits;
1062         struct commit_list *newlist = NULL;
1063         struct commit_list **p = &newlist;
1064         struct commit_list *bottom = NULL;
1065
1066         if (revs->ancestry_path) {
1067                 bottom = collect_bottom_commits(list);
1068                 if (!bottom)
1069                         die("--ancestry-path given but there are no bottom commits");
1070         }
1071
1072         while (list) {
1073                 struct commit_list *entry = list;
1074                 struct commit *commit = list->item;
1075                 struct object *obj = &commit->object;
1076                 show_early_output_fn_t show;
1077
1078                 list = list->next;
1079                 free(entry);
1080
1081                 if (revs->max_age != -1 && (commit->date < revs->max_age))
1082                         obj->flags |= UNINTERESTING;
1083                 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1084                         return -1;
1085                 if (obj->flags & UNINTERESTING) {
1086                         mark_parents_uninteresting(commit);
1087                         if (revs->show_all)
1088                                 p = &commit_list_insert(commit, p)->next;
1089                         slop = still_interesting(list, date, slop);
1090                         if (slop)
1091                                 continue;
1092                         /* If showing all, add the whole pending list to the end */
1093                         if (revs->show_all)
1094                                 *p = list;
1095                         break;
1096                 }
1097                 if (revs->min_age != -1 && (commit->date > revs->min_age))
1098                         continue;
1099                 date = commit->date;
1100                 p = &commit_list_insert(commit, p)->next;
1101
1102                 show = show_early_output;
1103                 if (!show)
1104                         continue;
1105
1106                 show(revs, newlist);
1107                 show_early_output = NULL;
1108         }
1109         if (revs->cherry_pick || revs->cherry_mark)
1110                 cherry_pick_list(newlist, revs);
1111
1112         if (revs->left_only || revs->right_only)
1113                 limit_left_right(newlist, revs);
1114
1115         if (bottom) {
1116                 limit_to_ancestry(bottom, newlist);
1117                 free_commit_list(bottom);
1118         }
1119
1120         /*
1121          * Check if any commits have become TREESAME by some of their parents
1122          * becoming UNINTERESTING.
1123          */
1124         if (limiting_can_increase_treesame(revs))
1125                 for (list = newlist; list; list = list->next) {
1126                         struct commit *c = list->item;
1127                         if (c->object.flags & (UNINTERESTING | TREESAME))
1128                                 continue;
1129                         update_treesame(revs, c);
1130                 }
1131
1132         revs->commits = newlist;
1133         return 0;
1134 }
1135
1136 /*
1137  * Add an entry to refs->cmdline with the specified information.
1138  * *name is copied.
1139  */
1140 static void add_rev_cmdline(struct rev_info *revs,
1141                             struct object *item,
1142                             const char *name,
1143                             int whence,
1144                             unsigned flags)
1145 {
1146         struct rev_cmdline_info *info = &revs->cmdline;
1147         int nr = info->nr;
1148
1149         ALLOC_GROW(info->rev, nr + 1, info->alloc);
1150         info->rev[nr].item = item;
1151         info->rev[nr].name = xstrdup(name);
1152         info->rev[nr].whence = whence;
1153         info->rev[nr].flags = flags;
1154         info->nr++;
1155 }
1156
1157 static void add_rev_cmdline_list(struct rev_info *revs,
1158                                  struct commit_list *commit_list,
1159                                  int whence,
1160                                  unsigned flags)
1161 {
1162         while (commit_list) {
1163                 struct object *object = &commit_list->item->object;
1164                 add_rev_cmdline(revs, object, sha1_to_hex(object->sha1),
1165                                 whence, flags);
1166                 commit_list = commit_list->next;
1167         }
1168 }
1169
1170 struct all_refs_cb {
1171         int all_flags;
1172         int warned_bad_reflog;
1173         struct rev_info *all_revs;
1174         const char *name_for_errormsg;
1175 };
1176
1177 int ref_excluded(struct string_list *ref_excludes, const char *path)
1178 {
1179         struct string_list_item *item;
1180
1181         if (!ref_excludes)
1182                 return 0;
1183         for_each_string_list_item(item, ref_excludes) {
1184                 if (!fnmatch(item->string, path, 0))
1185                         return 1;
1186         }
1187         return 0;
1188 }
1189
1190 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
1191 {
1192         struct all_refs_cb *cb = cb_data;
1193         struct object *object;
1194
1195         if (ref_excluded(cb->all_revs->ref_excludes, path))
1196             return 0;
1197
1198         object = get_reference(cb->all_revs, path, sha1, cb->all_flags);
1199         add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1200         add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags);
1201         return 0;
1202 }
1203
1204 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1205         unsigned flags)
1206 {
1207         cb->all_revs = revs;
1208         cb->all_flags = flags;
1209 }
1210
1211 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1212 {
1213         if (*ref_excludes_p) {
1214                 string_list_clear(*ref_excludes_p, 0);
1215                 free(*ref_excludes_p);
1216         }
1217         *ref_excludes_p = NULL;
1218 }
1219
1220 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1221 {
1222         if (!*ref_excludes_p) {
1223                 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1224                 (*ref_excludes_p)->strdup_strings = 1;
1225         }
1226         string_list_append(*ref_excludes_p, exclude);
1227 }
1228
1229 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
1230                 int (*for_each)(const char *, each_ref_fn, void *))
1231 {
1232         struct all_refs_cb cb;
1233         init_all_refs_cb(&cb, revs, flags);
1234         for_each(submodule, handle_one_ref, &cb);
1235 }
1236
1237 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
1238 {
1239         struct all_refs_cb *cb = cb_data;
1240         if (!is_null_sha1(sha1)) {
1241                 struct object *o = parse_object(sha1);
1242                 if (o) {
1243                         o->flags |= cb->all_flags;
1244                         /* ??? CMDLINEFLAGS ??? */
1245                         add_pending_object(cb->all_revs, o, "");
1246                 }
1247                 else if (!cb->warned_bad_reflog) {
1248                         warning("reflog of '%s' references pruned commits",
1249                                 cb->name_for_errormsg);
1250                         cb->warned_bad_reflog = 1;
1251                 }
1252         }
1253 }
1254
1255 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
1256                 const char *email, unsigned long timestamp, int tz,
1257                 const char *message, void *cb_data)
1258 {
1259         handle_one_reflog_commit(osha1, cb_data);
1260         handle_one_reflog_commit(nsha1, cb_data);
1261         return 0;
1262 }
1263
1264 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
1265 {
1266         struct all_refs_cb *cb = cb_data;
1267         cb->warned_bad_reflog = 0;
1268         cb->name_for_errormsg = path;
1269         for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
1270         return 0;
1271 }
1272
1273 static void handle_reflog(struct rev_info *revs, unsigned flags)
1274 {
1275         struct all_refs_cb cb;
1276         cb.all_revs = revs;
1277         cb.all_flags = flags;
1278         for_each_reflog(handle_one_reflog, &cb);
1279 }
1280
1281 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
1282 {
1283         unsigned char sha1[20];
1284         struct object *it;
1285         struct commit *commit;
1286         struct commit_list *parents;
1287         const char *arg = arg_;
1288
1289         if (*arg == '^') {
1290                 flags ^= UNINTERESTING | BOTTOM;
1291                 arg++;
1292         }
1293         if (get_sha1_committish(arg, sha1))
1294                 return 0;
1295         while (1) {
1296                 it = get_reference(revs, arg, sha1, 0);
1297                 if (!it && revs->ignore_missing)
1298                         return 0;
1299                 if (it->type != OBJ_TAG)
1300                         break;
1301                 if (!((struct tag*)it)->tagged)
1302                         return 0;
1303                 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
1304         }
1305         if (it->type != OBJ_COMMIT)
1306                 return 0;
1307         commit = (struct commit *)it;
1308         for (parents = commit->parents; parents; parents = parents->next) {
1309                 it = &parents->item->object;
1310                 it->flags |= flags;
1311                 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1312                 add_pending_object(revs, it, arg);
1313         }
1314         return 1;
1315 }
1316
1317 void init_revisions(struct rev_info *revs, const char *prefix)
1318 {
1319         memset(revs, 0, sizeof(*revs));
1320
1321         revs->abbrev = DEFAULT_ABBREV;
1322         revs->ignore_merges = 1;
1323         revs->simplify_history = 1;
1324         DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1325         DIFF_OPT_SET(&revs->pruning, QUICK);
1326         revs->pruning.add_remove = file_add_remove;
1327         revs->pruning.change = file_change;
1328         revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1329         revs->dense = 1;
1330         revs->prefix = prefix;
1331         revs->max_age = -1;
1332         revs->min_age = -1;
1333         revs->skip_count = -1;
1334         revs->max_count = -1;
1335         revs->max_parents = -1;
1336
1337         revs->commit_format = CMIT_FMT_DEFAULT;
1338
1339         init_grep_defaults();
1340         grep_init(&revs->grep_filter, prefix);
1341         revs->grep_filter.status_only = 1;
1342         revs->grep_filter.regflags = REG_NEWLINE;
1343
1344         diff_setup(&revs->diffopt);
1345         if (prefix && !revs->diffopt.prefix) {
1346                 revs->diffopt.prefix = prefix;
1347                 revs->diffopt.prefix_length = strlen(prefix);
1348         }
1349
1350         revs->notes_opt.use_default_notes = -1;
1351 }
1352
1353 static void add_pending_commit_list(struct rev_info *revs,
1354                                     struct commit_list *commit_list,
1355                                     unsigned int flags)
1356 {
1357         while (commit_list) {
1358                 struct object *object = &commit_list->item->object;
1359                 object->flags |= flags;
1360                 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1361                 commit_list = commit_list->next;
1362         }
1363 }
1364
1365 static void prepare_show_merge(struct rev_info *revs)
1366 {
1367         struct commit_list *bases;
1368         struct commit *head, *other;
1369         unsigned char sha1[20];
1370         const char **prune = NULL;
1371         int i, prune_num = 1; /* counting terminating NULL */
1372
1373         if (get_sha1("HEAD", sha1))
1374                 die("--merge without HEAD?");
1375         head = lookup_commit_or_die(sha1, "HEAD");
1376         if (get_sha1("MERGE_HEAD", sha1))
1377                 die("--merge without MERGE_HEAD?");
1378         other = lookup_commit_or_die(sha1, "MERGE_HEAD");
1379         add_pending_object(revs, &head->object, "HEAD");
1380         add_pending_object(revs, &other->object, "MERGE_HEAD");
1381         bases = get_merge_bases(head, other, 1);
1382         add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1383         add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1384         free_commit_list(bases);
1385         head->object.flags |= SYMMETRIC_LEFT;
1386
1387         if (!active_nr)
1388                 read_cache();
1389         for (i = 0; i < active_nr; i++) {
1390                 const struct cache_entry *ce = active_cache[i];
1391                 if (!ce_stage(ce))
1392                         continue;
1393                 if (ce_path_match(ce, &revs->prune_data)) {
1394                         prune_num++;
1395                         prune = xrealloc(prune, sizeof(*prune) * prune_num);
1396                         prune[prune_num-2] = ce->name;
1397                         prune[prune_num-1] = NULL;
1398                 }
1399                 while ((i+1 < active_nr) &&
1400                        ce_same_name(ce, active_cache[i+1]))
1401                         i++;
1402         }
1403         free_pathspec(&revs->prune_data);
1404         parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1405                        PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1406         revs->limited = 1;
1407 }
1408
1409 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1410 {
1411         struct object_context oc;
1412         char *dotdot;
1413         struct object *object;
1414         unsigned char sha1[20];
1415         int local_flags;
1416         const char *arg = arg_;
1417         int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1418         unsigned get_sha1_flags = 0;
1419
1420         flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1421
1422         dotdot = strstr(arg, "..");
1423         if (dotdot) {
1424                 unsigned char from_sha1[20];
1425                 const char *next = dotdot + 2;
1426                 const char *this = arg;
1427                 int symmetric = *next == '.';
1428                 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1429                 static const char head_by_default[] = "HEAD";
1430                 unsigned int a_flags;
1431
1432                 *dotdot = 0;
1433                 next += symmetric;
1434
1435                 if (!*next)
1436                         next = head_by_default;
1437                 if (dotdot == arg)
1438                         this = head_by_default;
1439                 if (this == head_by_default && next == head_by_default &&
1440                     !symmetric) {
1441                         /*
1442                          * Just ".."?  That is not a range but the
1443                          * pathspec for the parent directory.
1444                          */
1445                         if (!cant_be_filename) {
1446                                 *dotdot = '.';
1447                                 return -1;
1448                         }
1449                 }
1450                 if (!get_sha1_committish(this, from_sha1) &&
1451                     !get_sha1_committish(next, sha1)) {
1452                         struct object *a_obj, *b_obj;
1453
1454                         if (!cant_be_filename) {
1455                                 *dotdot = '.';
1456                                 verify_non_filename(revs->prefix, arg);
1457                         }
1458
1459                         a_obj = parse_object(from_sha1);
1460                         b_obj = parse_object(sha1);
1461                         if (!a_obj || !b_obj) {
1462                         missing:
1463                                 if (revs->ignore_missing)
1464                                         return 0;
1465                                 die(symmetric
1466                                     ? "Invalid symmetric difference expression %s"
1467                                     : "Invalid revision range %s", arg);
1468                         }
1469
1470                         if (!symmetric) {
1471                                 /* just A..B */
1472                                 a_flags = flags_exclude;
1473                         } else {
1474                                 /* A...B -- find merge bases between the two */
1475                                 struct commit *a, *b;
1476                                 struct commit_list *exclude;
1477
1478                                 a = (a_obj->type == OBJ_COMMIT
1479                                      ? (struct commit *)a_obj
1480                                      : lookup_commit_reference(a_obj->sha1));
1481                                 b = (b_obj->type == OBJ_COMMIT
1482                                      ? (struct commit *)b_obj
1483                                      : lookup_commit_reference(b_obj->sha1));
1484                                 if (!a || !b)
1485                                         goto missing;
1486                                 exclude = get_merge_bases(a, b, 1);
1487                                 add_rev_cmdline_list(revs, exclude,
1488                                                      REV_CMD_MERGE_BASE,
1489                                                      flags_exclude);
1490                                 add_pending_commit_list(revs, exclude,
1491                                                         flags_exclude);
1492                                 free_commit_list(exclude);
1493
1494                                 a_flags = flags | SYMMETRIC_LEFT;
1495                         }
1496
1497                         a_obj->flags |= a_flags;
1498                         b_obj->flags |= flags;
1499                         add_rev_cmdline(revs, a_obj, this,
1500                                         REV_CMD_LEFT, a_flags);
1501                         add_rev_cmdline(revs, b_obj, next,
1502                                         REV_CMD_RIGHT, flags);
1503                         add_pending_object(revs, a_obj, this);
1504                         add_pending_object(revs, b_obj, next);
1505                         return 0;
1506                 }
1507                 *dotdot = '.';
1508         }
1509         dotdot = strstr(arg, "^@");
1510         if (dotdot && !dotdot[2]) {
1511                 *dotdot = 0;
1512                 if (add_parents_only(revs, arg, flags))
1513                         return 0;
1514                 *dotdot = '^';
1515         }
1516         dotdot = strstr(arg, "^!");
1517         if (dotdot && !dotdot[2]) {
1518                 *dotdot = 0;
1519                 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM)))
1520                         *dotdot = '^';
1521         }
1522
1523         local_flags = 0;
1524         if (*arg == '^') {
1525                 local_flags = UNINTERESTING | BOTTOM;
1526                 arg++;
1527         }
1528
1529         if (revarg_opt & REVARG_COMMITTISH)
1530                 get_sha1_flags = GET_SHA1_COMMITTISH;
1531
1532         if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
1533                 return revs->ignore_missing ? 0 : -1;
1534         if (!cant_be_filename)
1535                 verify_non_filename(revs->prefix, arg);
1536         object = get_reference(revs, arg, sha1, flags ^ local_flags);
1537         add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1538         add_pending_object_with_mode(revs, object, arg, oc.mode);
1539         return 0;
1540 }
1541
1542 struct cmdline_pathspec {
1543         int alloc;
1544         int nr;
1545         const char **path;
1546 };
1547
1548 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1549 {
1550         while (*av) {
1551                 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1552                 prune->path[prune->nr++] = *(av++);
1553         }
1554 }
1555
1556 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1557                                      struct cmdline_pathspec *prune)
1558 {
1559         while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1560                 int len = sb->len;
1561                 if (len && sb->buf[len - 1] == '\n')
1562                         sb->buf[--len] = '\0';
1563                 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1564                 prune->path[prune->nr++] = xstrdup(sb->buf);
1565         }
1566 }
1567
1568 static void read_revisions_from_stdin(struct rev_info *revs,
1569                                       struct cmdline_pathspec *prune)
1570 {
1571         struct strbuf sb;
1572         int seen_dashdash = 0;
1573
1574         strbuf_init(&sb, 1000);
1575         while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1576                 int len = sb.len;
1577                 if (len && sb.buf[len - 1] == '\n')
1578                         sb.buf[--len] = '\0';
1579                 if (!len)
1580                         break;
1581                 if (sb.buf[0] == '-') {
1582                         if (len == 2 && sb.buf[1] == '-') {
1583                                 seen_dashdash = 1;
1584                                 break;
1585                         }
1586                         die("options not supported in --stdin mode");
1587                 }
1588                 if (handle_revision_arg(sb.buf, revs, 0,
1589                                         REVARG_CANNOT_BE_FILENAME))
1590                         die("bad revision '%s'", sb.buf);
1591         }
1592         if (seen_dashdash)
1593                 read_pathspec_from_stdin(revs, &sb, prune);
1594         strbuf_release(&sb);
1595 }
1596
1597 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1598 {
1599         append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1600 }
1601
1602 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1603 {
1604         append_header_grep_pattern(&revs->grep_filter, field, pattern);
1605 }
1606
1607 static void add_message_grep(struct rev_info *revs, const char *pattern)
1608 {
1609         add_grep(revs, pattern, GREP_PATTERN_BODY);
1610 }
1611
1612 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1613                                int *unkc, const char **unkv)
1614 {
1615         const char *arg = argv[0];
1616         const char *optarg;
1617         int argcount;
1618
1619         /* pseudo revision arguments */
1620         if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1621             !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1622             !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1623             !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1624             !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1625             starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1626             starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1627         {
1628                 unkv[(*unkc)++] = arg;
1629                 return 1;
1630         }
1631
1632         if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1633                 revs->max_count = atoi(optarg);
1634                 revs->no_walk = 0;
1635                 return argcount;
1636         } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1637                 revs->skip_count = atoi(optarg);
1638                 return argcount;
1639         } else if ((*arg == '-') && isdigit(arg[1])) {
1640         /* accept -<digit>, like traditional "head" */
1641                 revs->max_count = atoi(arg + 1);
1642                 revs->no_walk = 0;
1643         } else if (!strcmp(arg, "-n")) {
1644                 if (argc <= 1)
1645                         return error("-n requires an argument");
1646                 revs->max_count = atoi(argv[1]);
1647                 revs->no_walk = 0;
1648                 return 2;
1649         } else if (starts_with(arg, "-n")) {
1650                 revs->max_count = atoi(arg + 2);
1651                 revs->no_walk = 0;
1652         } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1653                 revs->max_age = atoi(optarg);
1654                 return argcount;
1655         } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1656                 revs->max_age = approxidate(optarg);
1657                 return argcount;
1658         } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1659                 revs->max_age = approxidate(optarg);
1660                 return argcount;
1661         } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1662                 revs->min_age = atoi(optarg);
1663                 return argcount;
1664         } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1665                 revs->min_age = approxidate(optarg);
1666                 return argcount;
1667         } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1668                 revs->min_age = approxidate(optarg);
1669                 return argcount;
1670         } else if (!strcmp(arg, "--first-parent")) {
1671                 revs->first_parent_only = 1;
1672         } else if (!strcmp(arg, "--ancestry-path")) {
1673                 revs->ancestry_path = 1;
1674                 revs->simplify_history = 0;
1675                 revs->limited = 1;
1676         } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1677                 init_reflog_walk(&revs->reflog_info);
1678         } else if (!strcmp(arg, "--default")) {
1679                 if (argc <= 1)
1680                         return error("bad --default argument");
1681                 revs->def = argv[1];
1682                 return 2;
1683         } else if (!strcmp(arg, "--merge")) {
1684                 revs->show_merge = 1;
1685         } else if (!strcmp(arg, "--topo-order")) {
1686                 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1687                 revs->topo_order = 1;
1688         } else if (!strcmp(arg, "--simplify-merges")) {
1689                 revs->simplify_merges = 1;
1690                 revs->topo_order = 1;
1691                 revs->rewrite_parents = 1;
1692                 revs->simplify_history = 0;
1693                 revs->limited = 1;
1694         } else if (!strcmp(arg, "--simplify-by-decoration")) {
1695                 revs->simplify_merges = 1;
1696                 revs->topo_order = 1;
1697                 revs->rewrite_parents = 1;
1698                 revs->simplify_history = 0;
1699                 revs->simplify_by_decoration = 1;
1700                 revs->limited = 1;
1701                 revs->prune = 1;
1702                 load_ref_decorations(DECORATE_SHORT_REFS);
1703         } else if (!strcmp(arg, "--date-order")) {
1704                 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1705                 revs->topo_order = 1;
1706         } else if (!strcmp(arg, "--author-date-order")) {
1707                 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1708                 revs->topo_order = 1;
1709         } else if (starts_with(arg, "--early-output")) {
1710                 int count = 100;
1711                 switch (arg[14]) {
1712                 case '=':
1713                         count = atoi(arg+15);
1714                         /* Fallthrough */
1715                 case 0:
1716                         revs->topo_order = 1;
1717                        revs->early_output = count;
1718                 }
1719         } else if (!strcmp(arg, "--parents")) {
1720                 revs->rewrite_parents = 1;
1721                 revs->print_parents = 1;
1722         } else if (!strcmp(arg, "--dense")) {
1723                 revs->dense = 1;
1724         } else if (!strcmp(arg, "--sparse")) {
1725                 revs->dense = 0;
1726         } else if (!strcmp(arg, "--show-all")) {
1727                 revs->show_all = 1;
1728         } else if (!strcmp(arg, "--remove-empty")) {
1729                 revs->remove_empty_trees = 1;
1730         } else if (!strcmp(arg, "--merges")) {
1731                 revs->min_parents = 2;
1732         } else if (!strcmp(arg, "--no-merges")) {
1733                 revs->max_parents = 1;
1734         } else if (starts_with(arg, "--min-parents=")) {
1735                 revs->min_parents = atoi(arg+14);
1736         } else if (starts_with(arg, "--no-min-parents")) {
1737                 revs->min_parents = 0;
1738         } else if (starts_with(arg, "--max-parents=")) {
1739                 revs->max_parents = atoi(arg+14);
1740         } else if (starts_with(arg, "--no-max-parents")) {
1741                 revs->max_parents = -1;
1742         } else if (!strcmp(arg, "--boundary")) {
1743                 revs->boundary = 1;
1744         } else if (!strcmp(arg, "--left-right")) {
1745                 revs->left_right = 1;
1746         } else if (!strcmp(arg, "--left-only")) {
1747                 if (revs->right_only)
1748                         die("--left-only is incompatible with --right-only"
1749                             " or --cherry");
1750                 revs->left_only = 1;
1751         } else if (!strcmp(arg, "--right-only")) {
1752                 if (revs->left_only)
1753                         die("--right-only is incompatible with --left-only");
1754                 revs->right_only = 1;
1755         } else if (!strcmp(arg, "--cherry")) {
1756                 if (revs->left_only)
1757                         die("--cherry is incompatible with --left-only");
1758                 revs->cherry_mark = 1;
1759                 revs->right_only = 1;
1760                 revs->max_parents = 1;
1761                 revs->limited = 1;
1762         } else if (!strcmp(arg, "--count")) {
1763                 revs->count = 1;
1764         } else if (!strcmp(arg, "--cherry-mark")) {
1765                 if (revs->cherry_pick)
1766                         die("--cherry-mark is incompatible with --cherry-pick");
1767                 revs->cherry_mark = 1;
1768                 revs->limited = 1; /* needs limit_list() */
1769         } else if (!strcmp(arg, "--cherry-pick")) {
1770                 if (revs->cherry_mark)
1771                         die("--cherry-pick is incompatible with --cherry-mark");
1772                 revs->cherry_pick = 1;
1773                 revs->limited = 1;
1774         } else if (!strcmp(arg, "--objects")) {
1775                 revs->tag_objects = 1;
1776                 revs->tree_objects = 1;
1777                 revs->blob_objects = 1;
1778         } else if (!strcmp(arg, "--objects-edge")) {
1779                 revs->tag_objects = 1;
1780                 revs->tree_objects = 1;
1781                 revs->blob_objects = 1;
1782                 revs->edge_hint = 1;
1783         } else if (!strcmp(arg, "--verify-objects")) {
1784                 revs->tag_objects = 1;
1785                 revs->tree_objects = 1;
1786                 revs->blob_objects = 1;
1787                 revs->verify_objects = 1;
1788         } else if (!strcmp(arg, "--unpacked")) {
1789                 revs->unpacked = 1;
1790         } else if (starts_with(arg, "--unpacked=")) {
1791                 die("--unpacked=<packfile> no longer supported.");
1792         } else if (!strcmp(arg, "-r")) {
1793                 revs->diff = 1;
1794                 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1795         } else if (!strcmp(arg, "-t")) {
1796                 revs->diff = 1;
1797                 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1798                 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1799         } else if (!strcmp(arg, "-m")) {
1800                 revs->ignore_merges = 0;
1801         } else if (!strcmp(arg, "-c")) {
1802                 revs->diff = 1;
1803                 revs->dense_combined_merges = 0;
1804                 revs->combine_merges = 1;
1805         } else if (!strcmp(arg, "--cc")) {
1806                 revs->diff = 1;
1807                 revs->dense_combined_merges = 1;
1808                 revs->combine_merges = 1;
1809         } else if (!strcmp(arg, "-v")) {
1810                 revs->verbose_header = 1;
1811         } else if (!strcmp(arg, "--pretty")) {
1812                 revs->verbose_header = 1;
1813                 revs->pretty_given = 1;
1814                 get_commit_format(arg+8, revs);
1815         } else if (starts_with(arg, "--pretty=") || starts_with(arg, "--format=")) {
1816                 /*
1817                  * Detached form ("--pretty X" as opposed to "--pretty=X")
1818                  * not allowed, since the argument is optional.
1819                  */
1820                 revs->verbose_header = 1;
1821                 revs->pretty_given = 1;
1822                 get_commit_format(arg+9, revs);
1823         } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1824                 revs->show_notes = 1;
1825                 revs->show_notes_given = 1;
1826                 revs->notes_opt.use_default_notes = 1;
1827         } else if (!strcmp(arg, "--show-signature")) {
1828                 revs->show_signature = 1;
1829         } else if (starts_with(arg, "--show-notes=") ||
1830                    starts_with(arg, "--notes=")) {
1831                 struct strbuf buf = STRBUF_INIT;
1832                 revs->show_notes = 1;
1833                 revs->show_notes_given = 1;
1834                 if (starts_with(arg, "--show-notes")) {
1835                         if (revs->notes_opt.use_default_notes < 0)
1836                                 revs->notes_opt.use_default_notes = 1;
1837                         strbuf_addstr(&buf, arg+13);
1838                 }
1839                 else
1840                         strbuf_addstr(&buf, arg+8);
1841                 expand_notes_ref(&buf);
1842                 string_list_append(&revs->notes_opt.extra_notes_refs,
1843                                    strbuf_detach(&buf, NULL));
1844         } else if (!strcmp(arg, "--no-notes")) {
1845                 revs->show_notes = 0;
1846                 revs->show_notes_given = 1;
1847                 revs->notes_opt.use_default_notes = -1;
1848                 /* we have been strdup'ing ourselves, so trick
1849                  * string_list into free()ing strings */
1850                 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1851                 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1852                 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1853         } else if (!strcmp(arg, "--standard-notes")) {
1854                 revs->show_notes_given = 1;
1855                 revs->notes_opt.use_default_notes = 1;
1856         } else if (!strcmp(arg, "--no-standard-notes")) {
1857                 revs->notes_opt.use_default_notes = 0;
1858         } else if (!strcmp(arg, "--oneline")) {
1859                 revs->verbose_header = 1;
1860                 get_commit_format("oneline", revs);
1861                 revs->pretty_given = 1;
1862                 revs->abbrev_commit = 1;
1863         } else if (!strcmp(arg, "--graph")) {
1864                 revs->topo_order = 1;
1865                 revs->rewrite_parents = 1;
1866                 revs->graph = graph_init(revs);
1867         } else if (!strcmp(arg, "--root")) {
1868                 revs->show_root_diff = 1;
1869         } else if (!strcmp(arg, "--no-commit-id")) {
1870                 revs->no_commit_id = 1;
1871         } else if (!strcmp(arg, "--always")) {
1872                 revs->always_show_header = 1;
1873         } else if (!strcmp(arg, "--no-abbrev")) {
1874                 revs->abbrev = 0;
1875         } else if (!strcmp(arg, "--abbrev")) {
1876                 revs->abbrev = DEFAULT_ABBREV;
1877         } else if (starts_with(arg, "--abbrev=")) {
1878                 revs->abbrev = strtoul(arg + 9, NULL, 10);
1879                 if (revs->abbrev < MINIMUM_ABBREV)
1880                         revs->abbrev = MINIMUM_ABBREV;
1881                 else if (revs->abbrev > 40)
1882                         revs->abbrev = 40;
1883         } else if (!strcmp(arg, "--abbrev-commit")) {
1884                 revs->abbrev_commit = 1;
1885                 revs->abbrev_commit_given = 1;
1886         } else if (!strcmp(arg, "--no-abbrev-commit")) {
1887                 revs->abbrev_commit = 0;
1888         } else if (!strcmp(arg, "--full-diff")) {
1889                 revs->diff = 1;
1890                 revs->full_diff = 1;
1891         } else if (!strcmp(arg, "--full-history")) {
1892                 revs->simplify_history = 0;
1893         } else if (!strcmp(arg, "--relative-date")) {
1894                 revs->date_mode = DATE_RELATIVE;
1895                 revs->date_mode_explicit = 1;
1896         } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1897                 revs->date_mode = parse_date_format(optarg);
1898                 revs->date_mode_explicit = 1;
1899                 return argcount;
1900         } else if (!strcmp(arg, "--log-size")) {
1901                 revs->show_log_size = 1;
1902         }
1903         /*
1904          * Grepping the commit log
1905          */
1906         else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1907                 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1908                 return argcount;
1909         } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1910                 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1911                 return argcount;
1912         } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
1913                 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
1914                 return argcount;
1915         } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1916                 add_message_grep(revs, optarg);
1917                 return argcount;
1918         } else if (!strcmp(arg, "--grep-debug")) {
1919                 revs->grep_filter.debug = 1;
1920         } else if (!strcmp(arg, "--basic-regexp")) {
1921                 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter);
1922         } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1923                 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter);
1924         } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1925                 revs->grep_filter.regflags |= REG_ICASE;
1926                 DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
1927         } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1928                 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter);
1929         } else if (!strcmp(arg, "--perl-regexp")) {
1930                 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
1931         } else if (!strcmp(arg, "--all-match")) {
1932                 revs->grep_filter.all_match = 1;
1933         } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1934                 if (strcmp(optarg, "none"))
1935                         git_log_output_encoding = xstrdup(optarg);
1936                 else
1937                         git_log_output_encoding = "";
1938                 return argcount;
1939         } else if (!strcmp(arg, "--reverse")) {
1940                 revs->reverse ^= 1;
1941         } else if (!strcmp(arg, "--children")) {
1942                 revs->children.name = "children";
1943                 revs->limited = 1;
1944         } else if (!strcmp(arg, "--ignore-missing")) {
1945                 revs->ignore_missing = 1;
1946         } else {
1947                 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1948                 if (!opts)
1949                         unkv[(*unkc)++] = arg;
1950                 return opts;
1951         }
1952
1953         return 1;
1954 }
1955
1956 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1957                         const struct option *options,
1958                         const char * const usagestr[])
1959 {
1960         int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1961                                     &ctx->cpidx, ctx->out);
1962         if (n <= 0) {
1963                 error("unknown option `%s'", ctx->argv[0]);
1964                 usage_with_options(usagestr, options);
1965         }
1966         ctx->argv += n;
1967         ctx->argc -= n;
1968 }
1969
1970 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1971 {
1972         return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
1973 }
1974
1975 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1976 {
1977         return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
1978 }
1979
1980 static int handle_revision_pseudo_opt(const char *submodule,
1981                                 struct rev_info *revs,
1982                                 int argc, const char **argv, int *flags)
1983 {
1984         const char *arg = argv[0];
1985         const char *optarg;
1986         int argcount;
1987
1988         /*
1989          * NOTE!
1990          *
1991          * Commands like "git shortlog" will not accept the options below
1992          * unless parse_revision_opt queues them (as opposed to erroring
1993          * out).
1994          *
1995          * When implementing your new pseudo-option, remember to
1996          * register it in the list at the top of handle_revision_opt.
1997          */
1998         if (!strcmp(arg, "--all")) {
1999                 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
2000                 handle_refs(submodule, revs, *flags, head_ref_submodule);
2001                 clear_ref_exclusion(&revs->ref_excludes);
2002         } else if (!strcmp(arg, "--branches")) {
2003                 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
2004                 clear_ref_exclusion(&revs->ref_excludes);
2005         } else if (!strcmp(arg, "--bisect")) {
2006                 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
2007                 handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
2008                 revs->bisect = 1;
2009         } else if (!strcmp(arg, "--tags")) {
2010                 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
2011                 clear_ref_exclusion(&revs->ref_excludes);
2012         } else if (!strcmp(arg, "--remotes")) {
2013                 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
2014                 clear_ref_exclusion(&revs->ref_excludes);
2015         } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2016                 struct all_refs_cb cb;
2017                 init_all_refs_cb(&cb, revs, *flags);
2018                 for_each_glob_ref(handle_one_ref, optarg, &cb);
2019                 clear_ref_exclusion(&revs->ref_excludes);
2020                 return argcount;
2021         } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2022                 add_ref_exclusion(&revs->ref_excludes, optarg);
2023                 return argcount;
2024         } else if (starts_with(arg, "--branches=")) {
2025                 struct all_refs_cb cb;
2026                 init_all_refs_cb(&cb, revs, *flags);
2027                 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
2028                 clear_ref_exclusion(&revs->ref_excludes);
2029         } else if (starts_with(arg, "--tags=")) {
2030                 struct all_refs_cb cb;
2031                 init_all_refs_cb(&cb, revs, *flags);
2032                 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
2033                 clear_ref_exclusion(&revs->ref_excludes);
2034         } else if (starts_with(arg, "--remotes=")) {
2035                 struct all_refs_cb cb;
2036                 init_all_refs_cb(&cb, revs, *flags);
2037                 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
2038                 clear_ref_exclusion(&revs->ref_excludes);
2039         } else if (!strcmp(arg, "--reflog")) {
2040                 handle_reflog(revs, *flags);
2041         } else if (!strcmp(arg, "--not")) {
2042                 *flags ^= UNINTERESTING | BOTTOM;
2043         } else if (!strcmp(arg, "--no-walk")) {
2044                 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2045         } else if (starts_with(arg, "--no-walk=")) {
2046                 /*
2047                  * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2048                  * not allowed, since the argument is optional.
2049                  */
2050                 if (!strcmp(arg + 10, "sorted"))
2051                         revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2052                 else if (!strcmp(arg + 10, "unsorted"))
2053                         revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2054                 else
2055                         return error("invalid argument to --no-walk");
2056         } else if (!strcmp(arg, "--do-walk")) {
2057                 revs->no_walk = 0;
2058         } else {
2059                 return 0;
2060         }
2061
2062         return 1;
2063 }
2064
2065 /*
2066  * Parse revision information, filling in the "rev_info" structure,
2067  * and removing the used arguments from the argument list.
2068  *
2069  * Returns the number of arguments left that weren't recognized
2070  * (which are also moved to the head of the argument list)
2071  */
2072 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2073 {
2074         int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2075         struct cmdline_pathspec prune_data;
2076         const char *submodule = NULL;
2077
2078         memset(&prune_data, 0, sizeof(prune_data));
2079         if (opt)
2080                 submodule = opt->submodule;
2081
2082         /* First, search for "--" */
2083         if (opt && opt->assume_dashdash) {
2084                 seen_dashdash = 1;
2085         } else {
2086                 seen_dashdash = 0;
2087                 for (i = 1; i < argc; i++) {
2088                         const char *arg = argv[i];
2089                         if (strcmp(arg, "--"))
2090                                 continue;
2091                         argv[i] = NULL;
2092                         argc = i;
2093                         if (argv[i + 1])
2094                                 append_prune_data(&prune_data, argv + i + 1);
2095                         seen_dashdash = 1;
2096                         break;
2097                 }
2098         }
2099
2100         /* Second, deal with arguments and options */
2101         flags = 0;
2102         revarg_opt = opt ? opt->revarg_opt : 0;
2103         if (seen_dashdash)
2104                 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2105         read_from_stdin = 0;
2106         for (left = i = 1; i < argc; i++) {
2107                 const char *arg = argv[i];
2108                 if (*arg == '-') {
2109                         int opts;
2110
2111                         opts = handle_revision_pseudo_opt(submodule,
2112                                                 revs, argc - i, argv + i,
2113                                                 &flags);
2114                         if (opts > 0) {
2115                                 i += opts - 1;
2116                                 continue;
2117                         }
2118
2119                         if (!strcmp(arg, "--stdin")) {
2120                                 if (revs->disable_stdin) {
2121                                         argv[left++] = arg;
2122                                         continue;
2123                                 }
2124                                 if (read_from_stdin++)
2125                                         die("--stdin given twice?");
2126                                 read_revisions_from_stdin(revs, &prune_data);
2127                                 continue;
2128                         }
2129
2130                         opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2131                         if (opts > 0) {
2132                                 i += opts - 1;
2133                                 continue;
2134                         }
2135                         if (opts < 0)
2136                                 exit(128);
2137                         continue;
2138                 }
2139
2140
2141                 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2142                         int j;
2143                         if (seen_dashdash || *arg == '^')
2144                                 die("bad revision '%s'", arg);
2145
2146                         /* If we didn't have a "--":
2147                          * (1) all filenames must exist;
2148                          * (2) all rev-args must not be interpretable
2149                          *     as a valid filename.
2150                          * but the latter we have checked in the main loop.
2151                          */
2152                         for (j = i; j < argc; j++)
2153                                 verify_filename(revs->prefix, argv[j], j == i);
2154
2155                         append_prune_data(&prune_data, argv + i);
2156                         break;
2157                 }
2158                 else
2159                         got_rev_arg = 1;
2160         }
2161
2162         if (prune_data.nr) {
2163                 /*
2164                  * If we need to introduce the magic "a lone ':' means no
2165                  * pathspec whatsoever", here is the place to do so.
2166                  *
2167                  * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2168                  *      prune_data.nr = 0;
2169                  *      prune_data.alloc = 0;
2170                  *      free(prune_data.path);
2171                  *      prune_data.path = NULL;
2172                  * } else {
2173                  *      terminate prune_data.alloc with NULL and
2174                  *      call init_pathspec() to set revs->prune_data here.
2175                  * }
2176                  */
2177                 ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc);
2178                 prune_data.path[prune_data.nr++] = NULL;
2179                 parse_pathspec(&revs->prune_data, 0, 0,
2180                                revs->prefix, prune_data.path);
2181         }
2182
2183         if (revs->def == NULL)
2184                 revs->def = opt ? opt->def : NULL;
2185         if (opt && opt->tweak)
2186                 opt->tweak(revs, opt);
2187         if (revs->show_merge)
2188                 prepare_show_merge(revs);
2189         if (revs->def && !revs->pending.nr && !got_rev_arg) {
2190                 unsigned char sha1[20];
2191                 struct object *object;
2192                 struct object_context oc;
2193                 if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2194                         die("bad default revision '%s'", revs->def);
2195                 object = get_reference(revs, revs->def, sha1, 0);
2196                 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2197         }
2198
2199         /* Did the user ask for any diff output? Run the diff! */
2200         if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2201                 revs->diff = 1;
2202
2203         /* Pickaxe, diff-filter and rename following need diffs */
2204         if (revs->diffopt.pickaxe ||
2205             revs->diffopt.filter ||
2206             DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2207                 revs->diff = 1;
2208
2209         if (revs->topo_order)
2210                 revs->limited = 1;
2211
2212         if (revs->prune_data.nr) {
2213                 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2214                 /* Can't prune commits with rename following: the paths change.. */
2215                 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2216                         revs->prune = 1;
2217                 if (!revs->full_diff)
2218                         copy_pathspec(&revs->diffopt.pathspec,
2219                                       &revs->prune_data);
2220         }
2221         if (revs->combine_merges)
2222                 revs->ignore_merges = 0;
2223         revs->diffopt.abbrev = revs->abbrev;
2224
2225         if (revs->line_level_traverse) {
2226                 revs->limited = 1;
2227                 revs->topo_order = 1;
2228         }
2229
2230         diff_setup_done(&revs->diffopt);
2231
2232         grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2233                                  &revs->grep_filter);
2234         compile_grep_patterns(&revs->grep_filter);
2235
2236         if (revs->reverse && revs->reflog_info)
2237                 die("cannot combine --reverse with --walk-reflogs");
2238         if (revs->rewrite_parents && revs->children.name)
2239                 die("cannot combine --parents and --children");
2240
2241         /*
2242          * Limitations on the graph functionality
2243          */
2244         if (revs->reverse && revs->graph)
2245                 die("cannot combine --reverse with --graph");
2246
2247         if (revs->reflog_info && revs->graph)
2248                 die("cannot combine --walk-reflogs with --graph");
2249         if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2250                 die("cannot use --grep-reflog without --walk-reflogs");
2251
2252         return left;
2253 }
2254
2255 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2256 {
2257         struct commit_list *l = xcalloc(1, sizeof(*l));
2258
2259         l->item = child;
2260         l->next = add_decoration(&revs->children, &parent->object, l);
2261 }
2262
2263 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2264 {
2265         struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2266         struct commit_list **pp, *p;
2267         int surviving_parents;
2268
2269         /* Examine existing parents while marking ones we have seen... */
2270         pp = &commit->parents;
2271         surviving_parents = 0;
2272         while ((p = *pp) != NULL) {
2273                 struct commit *parent = p->item;
2274                 if (parent->object.flags & TMP_MARK) {
2275                         *pp = p->next;
2276                         if (ts)
2277                                 compact_treesame(revs, commit, surviving_parents);
2278                         continue;
2279                 }
2280                 parent->object.flags |= TMP_MARK;
2281                 surviving_parents++;
2282                 pp = &p->next;
2283         }
2284         /* clear the temporary mark */
2285         for (p = commit->parents; p; p = p->next) {
2286                 p->item->object.flags &= ~TMP_MARK;
2287         }
2288         /* no update_treesame() - removing duplicates can't affect TREESAME */
2289         return surviving_parents;
2290 }
2291
2292 struct merge_simplify_state {
2293         struct commit *simplified;
2294 };
2295
2296 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2297 {
2298         struct merge_simplify_state *st;
2299
2300         st = lookup_decoration(&revs->merge_simplification, &commit->object);
2301         if (!st) {
2302                 st = xcalloc(1, sizeof(*st));
2303                 add_decoration(&revs->merge_simplification, &commit->object, st);
2304         }
2305         return st;
2306 }
2307
2308 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2309 {
2310         struct commit_list *h = reduce_heads(commit->parents);
2311         int i = 0, marked = 0;
2312         struct commit_list *po, *pn;
2313
2314         /* Want these for sanity-checking only */
2315         int orig_cnt = commit_list_count(commit->parents);
2316         int cnt = commit_list_count(h);
2317
2318         /*
2319          * Not ready to remove items yet, just mark them for now, based
2320          * on the output of reduce_heads(). reduce_heads outputs the reduced
2321          * set in its original order, so this isn't too hard.
2322          */
2323         po = commit->parents;
2324         pn = h;
2325         while (po) {
2326                 if (pn && po->item == pn->item) {
2327                         pn = pn->next;
2328                         i++;
2329                 } else {
2330                         po->item->object.flags |= TMP_MARK;
2331                         marked++;
2332                 }
2333                 po=po->next;
2334         }
2335
2336         if (i != cnt || cnt+marked != orig_cnt)
2337                 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2338
2339         free_commit_list(h);
2340
2341         return marked;
2342 }
2343
2344 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2345 {
2346         struct commit_list *p;
2347         int marked = 0;
2348
2349         for (p = commit->parents; p; p = p->next) {
2350                 struct commit *parent = p->item;
2351                 if (!parent->parents && (parent->object.flags & TREESAME)) {
2352                         parent->object.flags |= TMP_MARK;
2353                         marked++;
2354                 }
2355         }
2356
2357         return marked;
2358 }
2359
2360 /*
2361  * Awkward naming - this means one parent we are TREESAME to.
2362  * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2363  * empty tree). Better name suggestions?
2364  */
2365 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2366 {
2367         struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2368         struct commit *unmarked = NULL, *marked = NULL;
2369         struct commit_list *p;
2370         unsigned n;
2371
2372         for (p = commit->parents, n = 0; p; p = p->next, n++) {
2373                 if (ts->treesame[n]) {
2374                         if (p->item->object.flags & TMP_MARK) {
2375                                 if (!marked)
2376                                         marked = p->item;
2377                         } else {
2378                                 if (!unmarked) {
2379                                         unmarked = p->item;
2380                                         break;
2381                                 }
2382                         }
2383                 }
2384         }
2385
2386         /*
2387          * If we are TREESAME to a marked-for-deletion parent, but not to any
2388          * unmarked parents, unmark the first TREESAME parent. This is the
2389          * parent that the default simplify_history==1 scan would have followed,
2390          * and it doesn't make sense to omit that path when asking for a
2391          * simplified full history. Retaining it improves the chances of
2392          * understanding odd missed merges that took an old version of a file.
2393          *
2394          * Example:
2395          *
2396          *   I--------*X       A modified the file, but mainline merge X used
2397          *    \       /        "-s ours", so took the version from I. X is
2398          *     `-*A--'         TREESAME to I and !TREESAME to A.
2399          *
2400          * Default log from X would produce "I". Without this check,
2401          * --full-history --simplify-merges would produce "I-A-X", showing
2402          * the merge commit X and that it changed A, but not making clear that
2403          * it had just taken the I version. With this check, the topology above
2404          * is retained.
2405          *
2406          * Note that it is possible that the simplification chooses a different
2407          * TREESAME parent from the default, in which case this test doesn't
2408          * activate, and we _do_ drop the default parent. Example:
2409          *
2410          *   I------X         A modified the file, but it was reverted in B,
2411          *    \    /          meaning mainline merge X is TREESAME to both
2412          *    *A-*B           parents.
2413          *
2414          * Default log would produce "I" by following the first parent;
2415          * --full-history --simplify-merges will produce "I-A-B". But this is a
2416          * reasonable result - it presents a logical full history leading from
2417          * I to X, and X is not an important merge.
2418          */
2419         if (!unmarked && marked) {
2420                 marked->object.flags &= ~TMP_MARK;
2421                 return 1;
2422         }
2423
2424         return 0;
2425 }
2426
2427 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2428 {
2429         struct commit_list **pp, *p;
2430         int nth_parent, removed = 0;
2431
2432         pp = &commit->parents;
2433         nth_parent = 0;
2434         while ((p = *pp) != NULL) {
2435                 struct commit *parent = p->item;
2436                 if (parent->object.flags & TMP_MARK) {
2437                         parent->object.flags &= ~TMP_MARK;
2438                         *pp = p->next;
2439                         free(p);
2440                         removed++;
2441                         compact_treesame(revs, commit, nth_parent);
2442                         continue;
2443                 }
2444                 pp = &p->next;
2445                 nth_parent++;
2446         }
2447
2448         /* Removing parents can only increase TREESAMEness */
2449         if (removed && !(commit->object.flags & TREESAME))
2450                 update_treesame(revs, commit);
2451
2452         return nth_parent;
2453 }
2454
2455 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2456 {
2457         struct commit_list *p;
2458         struct commit *parent;
2459         struct merge_simplify_state *st, *pst;
2460         int cnt;
2461
2462         st = locate_simplify_state(revs, commit);
2463
2464         /*
2465          * Have we handled this one?
2466          */
2467         if (st->simplified)
2468                 return tail;
2469
2470         /*
2471          * An UNINTERESTING commit simplifies to itself, so does a
2472          * root commit.  We do not rewrite parents of such commit
2473          * anyway.
2474          */
2475         if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2476                 st->simplified = commit;
2477                 return tail;
2478         }
2479
2480         /*
2481          * Do we know what commit all of our parents that matter
2482          * should be rewritten to?  Otherwise we are not ready to
2483          * rewrite this one yet.
2484          */
2485         for (cnt = 0, p = commit->parents; p; p = p->next) {
2486                 pst = locate_simplify_state(revs, p->item);
2487                 if (!pst->simplified) {
2488                         tail = &commit_list_insert(p->item, tail)->next;
2489                         cnt++;
2490                 }
2491                 if (revs->first_parent_only)
2492                         break;
2493         }
2494         if (cnt) {
2495                 tail = &commit_list_insert(commit, tail)->next;
2496                 return tail;
2497         }
2498
2499         /*
2500          * Rewrite our list of parents. Note that this cannot
2501          * affect our TREESAME flags in any way - a commit is
2502          * always TREESAME to its simplification.
2503          */
2504         for (p = commit->parents; p; p = p->next) {
2505                 pst = locate_simplify_state(revs, p->item);
2506                 p->item = pst->simplified;
2507                 if (revs->first_parent_only)
2508                         break;
2509         }
2510
2511         if (revs->first_parent_only)
2512                 cnt = 1;
2513         else
2514                 cnt = remove_duplicate_parents(revs, commit);
2515
2516         /*
2517          * It is possible that we are a merge and one side branch
2518          * does not have any commit that touches the given paths;
2519          * in such a case, the immediate parent from that branch
2520          * will be rewritten to be the merge base.
2521          *
2522          *      o----X          X: the commit we are looking at;
2523          *     /    /           o: a commit that touches the paths;
2524          * ---o----'
2525          *
2526          * Further, a merge of an independent branch that doesn't
2527          * touch the path will reduce to a treesame root parent:
2528          *
2529          *  ----o----X          X: the commit we are looking at;
2530          *          /           o: a commit that touches the paths;
2531          *         r            r: a root commit not touching the paths
2532          *
2533          * Detect and simplify both cases.
2534          */
2535         if (1 < cnt) {
2536                 int marked = mark_redundant_parents(revs, commit);
2537                 marked += mark_treesame_root_parents(revs, commit);
2538                 if (marked)
2539                         marked -= leave_one_treesame_to_parent(revs, commit);
2540                 if (marked)
2541                         cnt = remove_marked_parents(revs, commit);
2542         }
2543
2544         /*
2545          * A commit simplifies to itself if it is a root, if it is
2546          * UNINTERESTING, if it touches the given paths, or if it is a
2547          * merge and its parents don't simplify to one relevant commit
2548          * (the first two cases are already handled at the beginning of
2549          * this function).
2550          *
2551          * Otherwise, it simplifies to what its sole relevant parent
2552          * simplifies to.
2553          */
2554         if (!cnt ||
2555             (commit->object.flags & UNINTERESTING) ||
2556             !(commit->object.flags & TREESAME) ||
2557             (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2558                 st->simplified = commit;
2559         else {
2560                 pst = locate_simplify_state(revs, parent);
2561                 st->simplified = pst->simplified;
2562         }
2563         return tail;
2564 }
2565
2566 static void simplify_merges(struct rev_info *revs)
2567 {
2568         struct commit_list *list, *next;
2569         struct commit_list *yet_to_do, **tail;
2570         struct commit *commit;
2571
2572         if (!revs->prune)
2573                 return;
2574
2575         /* feed the list reversed */
2576         yet_to_do = NULL;
2577         for (list = revs->commits; list; list = next) {
2578                 commit = list->item;
2579                 next = list->next;
2580                 /*
2581                  * Do not free(list) here yet; the original list
2582                  * is used later in this function.
2583                  */
2584                 commit_list_insert(commit, &yet_to_do);
2585         }
2586         while (yet_to_do) {
2587                 list = yet_to_do;
2588                 yet_to_do = NULL;
2589                 tail = &yet_to_do;
2590                 while (list) {
2591                         commit = list->item;
2592                         next = list->next;
2593                         free(list);
2594                         list = next;
2595                         tail = simplify_one(revs, commit, tail);
2596                 }
2597         }
2598
2599         /* clean up the result, removing the simplified ones */
2600         list = revs->commits;
2601         revs->commits = NULL;
2602         tail = &revs->commits;
2603         while (list) {
2604                 struct merge_simplify_state *st;
2605
2606                 commit = list->item;
2607                 next = list->next;
2608                 free(list);
2609                 list = next;
2610                 st = locate_simplify_state(revs, commit);
2611                 if (st->simplified == commit)
2612                         tail = &commit_list_insert(commit, tail)->next;
2613         }
2614 }
2615
2616 static void set_children(struct rev_info *revs)
2617 {
2618         struct commit_list *l;
2619         for (l = revs->commits; l; l = l->next) {
2620                 struct commit *commit = l->item;
2621                 struct commit_list *p;
2622
2623                 for (p = commit->parents; p; p = p->next)
2624                         add_child(revs, p->item, commit);
2625         }
2626 }
2627
2628 void reset_revision_walk(void)
2629 {
2630         clear_object_flags(SEEN | ADDED | SHOWN);
2631 }
2632
2633 int prepare_revision_walk(struct rev_info *revs)
2634 {
2635         int nr = revs->pending.nr;
2636         struct object_array_entry *e, *list;
2637         struct commit_list **next = &revs->commits;
2638
2639         e = list = revs->pending.objects;
2640         revs->pending.nr = 0;
2641         revs->pending.alloc = 0;
2642         revs->pending.objects = NULL;
2643         while (--nr >= 0) {
2644                 struct commit *commit = handle_commit(revs, e->item, e->name);
2645                 if (commit) {
2646                         if (!(commit->object.flags & SEEN)) {
2647                                 commit->object.flags |= SEEN;
2648                                 next = commit_list_append(commit, next);
2649                         }
2650                 }
2651                 e++;
2652         }
2653         if (!revs->leak_pending)
2654                 free(list);
2655
2656         /* Signal whether we need per-parent treesame decoration */
2657         if (revs->simplify_merges ||
2658             (revs->limited && limiting_can_increase_treesame(revs)))
2659                 revs->treesame.name = "treesame";
2660
2661         if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2662                 commit_list_sort_by_date(&revs->commits);
2663         if (revs->no_walk)
2664                 return 0;
2665         if (revs->limited)
2666                 if (limit_list(revs) < 0)
2667                         return -1;
2668         if (revs->topo_order)
2669                 sort_in_topological_order(&revs->commits, revs->sort_order);
2670         if (revs->line_level_traverse)
2671                 line_log_filter(revs);
2672         if (revs->simplify_merges)
2673                 simplify_merges(revs);
2674         if (revs->children.name)
2675                 set_children(revs);
2676         return 0;
2677 }
2678
2679 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2680 {
2681         struct commit_list *cache = NULL;
2682
2683         for (;;) {
2684                 struct commit *p = *pp;
2685                 if (!revs->limited)
2686                         if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2687                                 return rewrite_one_error;
2688                 if (p->object.flags & UNINTERESTING)
2689                         return rewrite_one_ok;
2690                 if (!(p->object.flags & TREESAME))
2691                         return rewrite_one_ok;
2692                 if (!p->parents)
2693                         return rewrite_one_noparents;
2694                 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2695                         return rewrite_one_ok;
2696                 *pp = p;
2697         }
2698 }
2699
2700 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2701         rewrite_parent_fn_t rewrite_parent)
2702 {
2703         struct commit_list **pp = &commit->parents;
2704         while (*pp) {
2705                 struct commit_list *parent = *pp;
2706                 switch (rewrite_parent(revs, &parent->item)) {
2707                 case rewrite_one_ok:
2708                         break;
2709                 case rewrite_one_noparents:
2710                         *pp = parent->next;
2711                         continue;
2712                 case rewrite_one_error:
2713                         return -1;
2714                 }
2715                 pp = &parent->next;
2716         }
2717         remove_duplicate_parents(revs, commit);
2718         return 0;
2719 }
2720
2721 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2722 {
2723         char *person, *endp;
2724         size_t len, namelen, maillen;
2725         const char *name;
2726         const char *mail;
2727         struct ident_split ident;
2728
2729         person = strstr(buf->buf, what);
2730         if (!person)
2731                 return 0;
2732
2733         person += strlen(what);
2734         endp = strchr(person, '\n');
2735         if (!endp)
2736                 return 0;
2737
2738         len = endp - person;
2739
2740         if (split_ident_line(&ident, person, len))
2741                 return 0;
2742
2743         mail = ident.mail_begin;
2744         maillen = ident.mail_end - ident.mail_begin;
2745         name = ident.name_begin;
2746         namelen = ident.name_end - ident.name_begin;
2747
2748         if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2749                 struct strbuf namemail = STRBUF_INIT;
2750
2751                 strbuf_addf(&namemail, "%.*s <%.*s>",
2752                             (int)namelen, name, (int)maillen, mail);
2753
2754                 strbuf_splice(buf, ident.name_begin - buf->buf,
2755                               ident.mail_end - ident.name_begin + 1,
2756                               namemail.buf, namemail.len);
2757
2758                 strbuf_release(&namemail);
2759
2760                 return 1;
2761         }
2762
2763         return 0;
2764 }
2765
2766 static int commit_match(struct commit *commit, struct rev_info *opt)
2767 {
2768         int retval;
2769         const char *encoding;
2770         char *message;
2771         struct strbuf buf = STRBUF_INIT;
2772
2773         if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2774                 return 1;
2775
2776         /* Prepend "fake" headers as needed */
2777         if (opt->grep_filter.use_reflog_filter) {
2778                 strbuf_addstr(&buf, "reflog ");
2779                 get_reflog_message(&buf, opt->reflog_info);
2780                 strbuf_addch(&buf, '\n');
2781         }
2782
2783         /*
2784          * We grep in the user's output encoding, under the assumption that it
2785          * is the encoding they are most likely to write their grep pattern
2786          * for. In addition, it means we will match the "notes" encoding below,
2787          * so we will not end up with a buffer that has two different encodings
2788          * in it.
2789          */
2790         encoding = get_log_output_encoding();
2791         message = logmsg_reencode(commit, NULL, encoding);
2792
2793         /* Copy the commit to temporary if we are using "fake" headers */
2794         if (buf.len)
2795                 strbuf_addstr(&buf, message);
2796
2797         if (opt->grep_filter.header_list && opt->mailmap) {
2798                 if (!buf.len)
2799                         strbuf_addstr(&buf, message);
2800
2801                 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2802                 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2803         }
2804
2805         /* Append "fake" message parts as needed */
2806         if (opt->show_notes) {
2807                 if (!buf.len)
2808                         strbuf_addstr(&buf, message);
2809                 format_display_notes(commit->object.sha1, &buf, encoding, 1);
2810         }
2811
2812         /* Find either in the original commit message, or in the temporary */
2813         if (buf.len)
2814                 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2815         else
2816                 retval = grep_buffer(&opt->grep_filter,
2817                                      message, strlen(message));
2818         strbuf_release(&buf);
2819         logmsg_free(message, commit);
2820         return retval;
2821 }
2822
2823 static inline int want_ancestry(const struct rev_info *revs)
2824 {
2825         return (revs->rewrite_parents || revs->children.name);
2826 }
2827
2828 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2829 {
2830         if (commit->object.flags & SHOWN)
2831                 return commit_ignore;
2832         if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2833                 return commit_ignore;
2834         if (revs->show_all)
2835                 return commit_show;
2836         if (commit->object.flags & UNINTERESTING)
2837                 return commit_ignore;
2838         if (revs->min_age != -1 && (commit->date > revs->min_age))
2839                 return commit_ignore;
2840         if (revs->min_parents || (revs->max_parents >= 0)) {
2841                 int n = commit_list_count(commit->parents);
2842                 if ((n < revs->min_parents) ||
2843                     ((revs->max_parents >= 0) && (n > revs->max_parents)))
2844                         return commit_ignore;
2845         }
2846         if (!commit_match(commit, revs))
2847                 return commit_ignore;
2848         if (revs->prune && revs->dense) {
2849                 /* Commit without changes? */
2850                 if (commit->object.flags & TREESAME) {
2851                         int n;
2852                         struct commit_list *p;
2853                         /* drop merges unless we want parenthood */
2854                         if (!want_ancestry(revs))
2855                                 return commit_ignore;
2856                         /*
2857                          * If we want ancestry, then need to keep any merges
2858                          * between relevant commits to tie together topology.
2859                          * For consistency with TREESAME and simplification
2860                          * use "relevant" here rather than just INTERESTING,
2861                          * to treat bottom commit(s) as part of the topology.
2862                          */
2863                         for (n = 0, p = commit->parents; p; p = p->next)
2864                                 if (relevant_commit(p->item))
2865                                         if (++n >= 2)
2866                                                 return commit_show;
2867                         return commit_ignore;
2868                 }
2869         }
2870         return commit_show;
2871 }
2872
2873 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
2874 {
2875         enum commit_action action = get_commit_action(revs, commit);
2876
2877         if (action == commit_show &&
2878             !revs->show_all &&
2879             revs->prune && revs->dense && want_ancestry(revs)) {
2880                 /*
2881                  * --full-diff on simplified parents is no good: it
2882                  * will show spurious changes from the commits that
2883                  * were elided.  So we save the parents on the side
2884                  * when --full-diff is in effect.
2885                  */
2886                 if (revs->full_diff)
2887                         save_parents(revs, commit);
2888                 if (rewrite_parents(revs, commit, rewrite_one) < 0)
2889                         return commit_error;
2890         }
2891         return action;
2892 }
2893
2894 static struct commit *get_revision_1(struct rev_info *revs)
2895 {
2896         if (!revs->commits)
2897                 return NULL;
2898
2899         do {
2900                 struct commit_list *entry = revs->commits;
2901                 struct commit *commit = entry->item;
2902
2903                 revs->commits = entry->next;
2904                 free(entry);
2905
2906                 if (revs->reflog_info) {
2907                         save_parents(revs, commit);
2908                         fake_reflog_parent(revs->reflog_info, commit);
2909                         commit->object.flags &= ~(ADDED | SEEN | SHOWN);
2910                 }
2911
2912                 /*
2913                  * If we haven't done the list limiting, we need to look at
2914                  * the parents here. We also need to do the date-based limiting
2915                  * that we'd otherwise have done in limit_list().
2916                  */
2917                 if (!revs->limited) {
2918                         if (revs->max_age != -1 &&
2919                             (commit->date < revs->max_age))
2920                                 continue;
2921                         if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
2922                                 die("Failed to traverse parents of commit %s",
2923                                     sha1_to_hex(commit->object.sha1));
2924                 }
2925
2926                 switch (simplify_commit(revs, commit)) {
2927                 case commit_ignore:
2928                         continue;
2929                 case commit_error:
2930                         die("Failed to simplify parents of commit %s",
2931                             sha1_to_hex(commit->object.sha1));
2932                 default:
2933                         return commit;
2934                 }
2935         } while (revs->commits);
2936         return NULL;
2937 }
2938
2939 /*
2940  * Return true for entries that have not yet been shown.  (This is an
2941  * object_array_each_func_t.)
2942  */
2943 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
2944 {
2945         return !(entry->item->flags & SHOWN);
2946 }
2947
2948 /*
2949  * If array is on the verge of a realloc, garbage-collect any entries
2950  * that have already been shown to try to free up some space.
2951  */
2952 static void gc_boundary(struct object_array *array)
2953 {
2954         if (array->nr == array->alloc)
2955                 object_array_filter(array, entry_unshown, NULL);
2956 }
2957
2958 static void create_boundary_commit_list(struct rev_info *revs)
2959 {
2960         unsigned i;
2961         struct commit *c;
2962         struct object_array *array = &revs->boundary_commits;
2963         struct object_array_entry *objects = array->objects;
2964
2965         /*
2966          * If revs->commits is non-NULL at this point, an error occurred in
2967          * get_revision_1().  Ignore the error and continue printing the
2968          * boundary commits anyway.  (This is what the code has always
2969          * done.)
2970          */
2971         if (revs->commits) {
2972                 free_commit_list(revs->commits);
2973                 revs->commits = NULL;
2974         }
2975
2976         /*
2977          * Put all of the actual boundary commits from revs->boundary_commits
2978          * into revs->commits
2979          */
2980         for (i = 0; i < array->nr; i++) {
2981                 c = (struct commit *)(objects[i].item);
2982                 if (!c)
2983                         continue;
2984                 if (!(c->object.flags & CHILD_SHOWN))
2985                         continue;
2986                 if (c->object.flags & (SHOWN | BOUNDARY))
2987                         continue;
2988                 c->object.flags |= BOUNDARY;
2989                 commit_list_insert(c, &revs->commits);
2990         }
2991
2992         /*
2993          * If revs->topo_order is set, sort the boundary commits
2994          * in topological order
2995          */
2996         sort_in_topological_order(&revs->commits, revs->sort_order);
2997 }
2998
2999 static struct commit *get_revision_internal(struct rev_info *revs)
3000 {
3001         struct commit *c = NULL;
3002         struct commit_list *l;
3003
3004         if (revs->boundary == 2) {
3005                 /*
3006                  * All of the normal commits have already been returned,
3007                  * and we are now returning boundary commits.
3008                  * create_boundary_commit_list() has populated
3009                  * revs->commits with the remaining commits to return.
3010                  */
3011                 c = pop_commit(&revs->commits);
3012                 if (c)
3013                         c->object.flags |= SHOWN;
3014                 return c;
3015         }
3016
3017         /*
3018          * If our max_count counter has reached zero, then we are done. We
3019          * don't simply return NULL because we still might need to show
3020          * boundary commits. But we want to avoid calling get_revision_1, which
3021          * might do a considerable amount of work finding the next commit only
3022          * for us to throw it away.
3023          *
3024          * If it is non-zero, then either we don't have a max_count at all
3025          * (-1), or it is still counting, in which case we decrement.
3026          */
3027         if (revs->max_count) {
3028                 c = get_revision_1(revs);
3029                 if (c) {
3030                         while (revs->skip_count > 0) {
3031                                 revs->skip_count--;
3032                                 c = get_revision_1(revs);
3033                                 if (!c)
3034                                         break;
3035                         }
3036                 }
3037
3038                 if (revs->max_count > 0)
3039                         revs->max_count--;
3040         }
3041
3042         if (c)
3043                 c->object.flags |= SHOWN;
3044
3045         if (!revs->boundary)
3046                 return c;
3047
3048         if (!c) {
3049                 /*
3050                  * get_revision_1() runs out the commits, and
3051                  * we are done computing the boundaries.
3052                  * switch to boundary commits output mode.
3053                  */
3054                 revs->boundary = 2;
3055
3056                 /*
3057                  * Update revs->commits to contain the list of
3058                  * boundary commits.
3059                  */
3060                 create_boundary_commit_list(revs);
3061
3062                 return get_revision_internal(revs);
3063         }
3064
3065         /*
3066          * boundary commits are the commits that are parents of the
3067          * ones we got from get_revision_1() but they themselves are
3068          * not returned from get_revision_1().  Before returning
3069          * 'c', we need to mark its parents that they could be boundaries.
3070          */
3071
3072         for (l = c->parents; l; l = l->next) {
3073                 struct object *p;
3074                 p = &(l->item->object);
3075                 if (p->flags & (CHILD_SHOWN | SHOWN))
3076                         continue;
3077                 p->flags |= CHILD_SHOWN;
3078                 gc_boundary(&revs->boundary_commits);
3079                 add_object_array(p, NULL, &revs->boundary_commits);
3080         }
3081
3082         return c;
3083 }
3084
3085 struct commit *get_revision(struct rev_info *revs)
3086 {
3087         struct commit *c;
3088         struct commit_list *reversed;
3089
3090         if (revs->reverse) {
3091                 reversed = NULL;
3092                 while ((c = get_revision_internal(revs)))
3093                         commit_list_insert(c, &reversed);
3094                 revs->commits = reversed;
3095                 revs->reverse = 0;
3096                 revs->reverse_output_stage = 1;
3097         }
3098
3099         if (revs->reverse_output_stage)
3100                 return pop_commit(&revs->commits);
3101
3102         c = get_revision_internal(revs);
3103         if (c && revs->graph)
3104                 graph_update(revs->graph, c);
3105         if (!c)
3106                 free_saved_parents(revs);
3107         return c;
3108 }
3109
3110 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3111 {
3112         if (commit->object.flags & BOUNDARY)
3113                 return "-";
3114         else if (commit->object.flags & UNINTERESTING)
3115                 return "^";
3116         else if (commit->object.flags & PATCHSAME)
3117                 return "=";
3118         else if (!revs || revs->left_right) {
3119                 if (commit->object.flags & SYMMETRIC_LEFT)
3120                         return "<";
3121                 else
3122                         return ">";
3123         } else if (revs->graph)
3124                 return "*";
3125         else if (revs->cherry_mark)
3126                 return "+";
3127         return "";
3128 }
3129
3130 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3131 {
3132         char *mark = get_revision_mark(revs, commit);
3133         if (!strlen(mark))
3134                 return;
3135         fputs(mark, stdout);
3136         putchar(' ');
3137 }
3138
3139 define_commit_slab(saved_parents, struct commit_list *);
3140
3141 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3142
3143 void save_parents(struct rev_info *revs, struct commit *commit)
3144 {
3145         struct commit_list **pp;
3146
3147         if (!revs->saved_parents_slab) {
3148                 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3149                 init_saved_parents(revs->saved_parents_slab);
3150         }
3151
3152         pp = saved_parents_at(revs->saved_parents_slab, commit);
3153
3154         /*
3155          * When walking with reflogs, we may visit the same commit
3156          * several times: once for each appearance in the reflog.
3157          *
3158          * In this case, save_parents() will be called multiple times.
3159          * We want to keep only the first set of parents.  We need to
3160          * store a sentinel value for an empty (i.e., NULL) parent
3161          * list to distinguish it from a not-yet-saved list, however.
3162          */
3163         if (*pp)
3164                 return;
3165         if (commit->parents)
3166                 *pp = copy_commit_list(commit->parents);
3167         else
3168                 *pp = EMPTY_PARENT_LIST;
3169 }
3170
3171 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3172 {
3173         struct commit_list *parents;
3174
3175         if (!revs->saved_parents_slab)
3176                 return commit->parents;
3177
3178         parents = *saved_parents_at(revs->saved_parents_slab, commit);
3179         if (parents == EMPTY_PARENT_LIST)
3180                 return NULL;
3181         return parents;
3182 }
3183
3184 void free_saved_parents(struct rev_info *revs)
3185 {
3186         if (revs->saved_parents_slab)
3187                 clear_saved_parents(revs->saved_parents_slab);
3188 }