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