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