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