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