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