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