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