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