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