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