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