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