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