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