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