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