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