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