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