]> git.ipfire.org Git - thirdparty/git.git/blob - revision.c
cache API: add a "INDEX_STATE_INIT" macro/function, add release_index()
[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 = INDEX_STATE_INIT;
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 graph_clear(revs->graph);
3063 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3064 diff_free(&revs->pruning);
3065 reflog_walk_info_release(revs->reflog_info);
3066 release_revisions_topo_walk_info(revs->topo_walk_info);
3067 }
3068
3069 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3070 {
3071 struct commit_list *l = xcalloc(1, sizeof(*l));
3072
3073 l->item = child;
3074 l->next = add_decoration(&revs->children, &parent->object, l);
3075 }
3076
3077 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3078 {
3079 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3080 struct commit_list **pp, *p;
3081 int surviving_parents;
3082
3083 /* Examine existing parents while marking ones we have seen... */
3084 pp = &commit->parents;
3085 surviving_parents = 0;
3086 while ((p = *pp) != NULL) {
3087 struct commit *parent = p->item;
3088 if (parent->object.flags & TMP_MARK) {
3089 *pp = p->next;
3090 if (ts)
3091 compact_treesame(revs, commit, surviving_parents);
3092 continue;
3093 }
3094 parent->object.flags |= TMP_MARK;
3095 surviving_parents++;
3096 pp = &p->next;
3097 }
3098 /* clear the temporary mark */
3099 for (p = commit->parents; p; p = p->next) {
3100 p->item->object.flags &= ~TMP_MARK;
3101 }
3102 /* no update_treesame() - removing duplicates can't affect TREESAME */
3103 return surviving_parents;
3104 }
3105
3106 struct merge_simplify_state {
3107 struct commit *simplified;
3108 };
3109
3110 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3111 {
3112 struct merge_simplify_state *st;
3113
3114 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3115 if (!st) {
3116 CALLOC_ARRAY(st, 1);
3117 add_decoration(&revs->merge_simplification, &commit->object, st);
3118 }
3119 return st;
3120 }
3121
3122 static int mark_redundant_parents(struct commit *commit)
3123 {
3124 struct commit_list *h = reduce_heads(commit->parents);
3125 int i = 0, marked = 0;
3126 struct commit_list *po, *pn;
3127
3128 /* Want these for sanity-checking only */
3129 int orig_cnt = commit_list_count(commit->parents);
3130 int cnt = commit_list_count(h);
3131
3132 /*
3133 * Not ready to remove items yet, just mark them for now, based
3134 * on the output of reduce_heads(). reduce_heads outputs the reduced
3135 * set in its original order, so this isn't too hard.
3136 */
3137 po = commit->parents;
3138 pn = h;
3139 while (po) {
3140 if (pn && po->item == pn->item) {
3141 pn = pn->next;
3142 i++;
3143 } else {
3144 po->item->object.flags |= TMP_MARK;
3145 marked++;
3146 }
3147 po=po->next;
3148 }
3149
3150 if (i != cnt || cnt+marked != orig_cnt)
3151 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3152
3153 free_commit_list(h);
3154
3155 return marked;
3156 }
3157
3158 static int mark_treesame_root_parents(struct commit *commit)
3159 {
3160 struct commit_list *p;
3161 int marked = 0;
3162
3163 for (p = commit->parents; p; p = p->next) {
3164 struct commit *parent = p->item;
3165 if (!parent->parents && (parent->object.flags & TREESAME)) {
3166 parent->object.flags |= TMP_MARK;
3167 marked++;
3168 }
3169 }
3170
3171 return marked;
3172 }
3173
3174 /*
3175 * Awkward naming - this means one parent we are TREESAME to.
3176 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3177 * empty tree). Better name suggestions?
3178 */
3179 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3180 {
3181 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3182 struct commit *unmarked = NULL, *marked = NULL;
3183 struct commit_list *p;
3184 unsigned n;
3185
3186 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3187 if (ts->treesame[n]) {
3188 if (p->item->object.flags & TMP_MARK) {
3189 if (!marked)
3190 marked = p->item;
3191 } else {
3192 if (!unmarked) {
3193 unmarked = p->item;
3194 break;
3195 }
3196 }
3197 }
3198 }
3199
3200 /*
3201 * If we are TREESAME to a marked-for-deletion parent, but not to any
3202 * unmarked parents, unmark the first TREESAME parent. This is the
3203 * parent that the default simplify_history==1 scan would have followed,
3204 * and it doesn't make sense to omit that path when asking for a
3205 * simplified full history. Retaining it improves the chances of
3206 * understanding odd missed merges that took an old version of a file.
3207 *
3208 * Example:
3209 *
3210 * I--------*X A modified the file, but mainline merge X used
3211 * \ / "-s ours", so took the version from I. X is
3212 * `-*A--' TREESAME to I and !TREESAME to A.
3213 *
3214 * Default log from X would produce "I". Without this check,
3215 * --full-history --simplify-merges would produce "I-A-X", showing
3216 * the merge commit X and that it changed A, but not making clear that
3217 * it had just taken the I version. With this check, the topology above
3218 * is retained.
3219 *
3220 * Note that it is possible that the simplification chooses a different
3221 * TREESAME parent from the default, in which case this test doesn't
3222 * activate, and we _do_ drop the default parent. Example:
3223 *
3224 * I------X A modified the file, but it was reverted in B,
3225 * \ / meaning mainline merge X is TREESAME to both
3226 * *A-*B parents.
3227 *
3228 * Default log would produce "I" by following the first parent;
3229 * --full-history --simplify-merges will produce "I-A-B". But this is a
3230 * reasonable result - it presents a logical full history leading from
3231 * I to X, and X is not an important merge.
3232 */
3233 if (!unmarked && marked) {
3234 marked->object.flags &= ~TMP_MARK;
3235 return 1;
3236 }
3237
3238 return 0;
3239 }
3240
3241 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3242 {
3243 struct commit_list **pp, *p;
3244 int nth_parent, removed = 0;
3245
3246 pp = &commit->parents;
3247 nth_parent = 0;
3248 while ((p = *pp) != NULL) {
3249 struct commit *parent = p->item;
3250 if (parent->object.flags & TMP_MARK) {
3251 parent->object.flags &= ~TMP_MARK;
3252 *pp = p->next;
3253 free(p);
3254 removed++;
3255 compact_treesame(revs, commit, nth_parent);
3256 continue;
3257 }
3258 pp = &p->next;
3259 nth_parent++;
3260 }
3261
3262 /* Removing parents can only increase TREESAMEness */
3263 if (removed && !(commit->object.flags & TREESAME))
3264 update_treesame(revs, commit);
3265
3266 return nth_parent;
3267 }
3268
3269 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3270 {
3271 struct commit_list *p;
3272 struct commit *parent;
3273 struct merge_simplify_state *st, *pst;
3274 int cnt;
3275
3276 st = locate_simplify_state(revs, commit);
3277
3278 /*
3279 * Have we handled this one?
3280 */
3281 if (st->simplified)
3282 return tail;
3283
3284 /*
3285 * An UNINTERESTING commit simplifies to itself, so does a
3286 * root commit. We do not rewrite parents of such commit
3287 * anyway.
3288 */
3289 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3290 st->simplified = commit;
3291 return tail;
3292 }
3293
3294 /*
3295 * Do we know what commit all of our parents that matter
3296 * should be rewritten to? Otherwise we are not ready to
3297 * rewrite this one yet.
3298 */
3299 for (cnt = 0, p = commit->parents; p; p = p->next) {
3300 pst = locate_simplify_state(revs, p->item);
3301 if (!pst->simplified) {
3302 tail = &commit_list_insert(p->item, tail)->next;
3303 cnt++;
3304 }
3305 if (revs->first_parent_only)
3306 break;
3307 }
3308 if (cnt) {
3309 tail = &commit_list_insert(commit, tail)->next;
3310 return tail;
3311 }
3312
3313 /*
3314 * Rewrite our list of parents. Note that this cannot
3315 * affect our TREESAME flags in any way - a commit is
3316 * always TREESAME to its simplification.
3317 */
3318 for (p = commit->parents; p; p = p->next) {
3319 pst = locate_simplify_state(revs, p->item);
3320 p->item = pst->simplified;
3321 if (revs->first_parent_only)
3322 break;
3323 }
3324
3325 if (revs->first_parent_only)
3326 cnt = 1;
3327 else
3328 cnt = remove_duplicate_parents(revs, commit);
3329
3330 /*
3331 * It is possible that we are a merge and one side branch
3332 * does not have any commit that touches the given paths;
3333 * in such a case, the immediate parent from that branch
3334 * will be rewritten to be the merge base.
3335 *
3336 * o----X X: the commit we are looking at;
3337 * / / o: a commit that touches the paths;
3338 * ---o----'
3339 *
3340 * Further, a merge of an independent branch that doesn't
3341 * touch the path will reduce to a treesame root parent:
3342 *
3343 * ----o----X X: the commit we are looking at;
3344 * / o: a commit that touches the paths;
3345 * r r: a root commit not touching the paths
3346 *
3347 * Detect and simplify both cases.
3348 */
3349 if (1 < cnt) {
3350 int marked = mark_redundant_parents(commit);
3351 marked += mark_treesame_root_parents(commit);
3352 if (marked)
3353 marked -= leave_one_treesame_to_parent(revs, commit);
3354 if (marked)
3355 cnt = remove_marked_parents(revs, commit);
3356 }
3357
3358 /*
3359 * A commit simplifies to itself if it is a root, if it is
3360 * UNINTERESTING, if it touches the given paths, or if it is a
3361 * merge and its parents don't simplify to one relevant commit
3362 * (the first two cases are already handled at the beginning of
3363 * this function).
3364 *
3365 * Otherwise, it simplifies to what its sole relevant parent
3366 * simplifies to.
3367 */
3368 if (!cnt ||
3369 (commit->object.flags & UNINTERESTING) ||
3370 !(commit->object.flags & TREESAME) ||
3371 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3372 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3373 st->simplified = commit;
3374 else {
3375 pst = locate_simplify_state(revs, parent);
3376 st->simplified = pst->simplified;
3377 }
3378 return tail;
3379 }
3380
3381 static void simplify_merges(struct rev_info *revs)
3382 {
3383 struct commit_list *list, *next;
3384 struct commit_list *yet_to_do, **tail;
3385 struct commit *commit;
3386
3387 if (!revs->prune)
3388 return;
3389
3390 /* feed the list reversed */
3391 yet_to_do = NULL;
3392 for (list = revs->commits; list; list = next) {
3393 commit = list->item;
3394 next = list->next;
3395 /*
3396 * Do not free(list) here yet; the original list
3397 * is used later in this function.
3398 */
3399 commit_list_insert(commit, &yet_to_do);
3400 }
3401 while (yet_to_do) {
3402 list = yet_to_do;
3403 yet_to_do = NULL;
3404 tail = &yet_to_do;
3405 while (list) {
3406 commit = pop_commit(&list);
3407 tail = simplify_one(revs, commit, tail);
3408 }
3409 }
3410
3411 /* clean up the result, removing the simplified ones */
3412 list = revs->commits;
3413 revs->commits = NULL;
3414 tail = &revs->commits;
3415 while (list) {
3416 struct merge_simplify_state *st;
3417
3418 commit = pop_commit(&list);
3419 st = locate_simplify_state(revs, commit);
3420 if (st->simplified == commit)
3421 tail = &commit_list_insert(commit, tail)->next;
3422 }
3423 }
3424
3425 static void set_children(struct rev_info *revs)
3426 {
3427 struct commit_list *l;
3428 for (l = revs->commits; l; l = l->next) {
3429 struct commit *commit = l->item;
3430 struct commit_list *p;
3431
3432 for (p = commit->parents; p; p = p->next)
3433 add_child(revs, p->item, commit);
3434 }
3435 }
3436
3437 void reset_revision_walk(void)
3438 {
3439 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3440 }
3441
3442 static int mark_uninteresting(const struct object_id *oid,
3443 struct packed_git *pack,
3444 uint32_t pos,
3445 void *cb)
3446 {
3447 struct rev_info *revs = cb;
3448 struct object *o = lookup_unknown_object(revs->repo, oid);
3449 o->flags |= UNINTERESTING | SEEN;
3450 return 0;
3451 }
3452
3453 define_commit_slab(indegree_slab, int);
3454 define_commit_slab(author_date_slab, timestamp_t);
3455
3456 struct topo_walk_info {
3457 timestamp_t min_generation;
3458 struct prio_queue explore_queue;
3459 struct prio_queue indegree_queue;
3460 struct prio_queue topo_queue;
3461 struct indegree_slab indegree;
3462 struct author_date_slab author_date;
3463 };
3464
3465 static int topo_walk_atexit_registered;
3466 static unsigned int count_explore_walked;
3467 static unsigned int count_indegree_walked;
3468 static unsigned int count_topo_walked;
3469
3470 static void trace2_topo_walk_statistics_atexit(void)
3471 {
3472 struct json_writer jw = JSON_WRITER_INIT;
3473
3474 jw_object_begin(&jw, 0);
3475 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3476 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3477 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3478 jw_end(&jw);
3479
3480 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3481
3482 jw_release(&jw);
3483 }
3484
3485 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3486 {
3487 if (c->object.flags & flag)
3488 return;
3489
3490 c->object.flags |= flag;
3491 prio_queue_put(q, c);
3492 }
3493
3494 static void explore_walk_step(struct rev_info *revs)
3495 {
3496 struct topo_walk_info *info = revs->topo_walk_info;
3497 struct commit_list *p;
3498 struct commit *c = prio_queue_get(&info->explore_queue);
3499
3500 if (!c)
3501 return;
3502
3503 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3504 return;
3505
3506 count_explore_walked++;
3507
3508 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3509 record_author_date(&info->author_date, c);
3510
3511 if (revs->max_age != -1 && (c->date < revs->max_age))
3512 c->object.flags |= UNINTERESTING;
3513
3514 if (process_parents(revs, c, NULL, NULL) < 0)
3515 return;
3516
3517 if (c->object.flags & UNINTERESTING)
3518 mark_parents_uninteresting(revs, c);
3519
3520 for (p = c->parents; p; p = p->next)
3521 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3522 }
3523
3524 static void explore_to_depth(struct rev_info *revs,
3525 timestamp_t gen_cutoff)
3526 {
3527 struct topo_walk_info *info = revs->topo_walk_info;
3528 struct commit *c;
3529 while ((c = prio_queue_peek(&info->explore_queue)) &&
3530 commit_graph_generation(c) >= gen_cutoff)
3531 explore_walk_step(revs);
3532 }
3533
3534 static void indegree_walk_step(struct rev_info *revs)
3535 {
3536 struct commit_list *p;
3537 struct topo_walk_info *info = revs->topo_walk_info;
3538 struct commit *c = prio_queue_get(&info->indegree_queue);
3539
3540 if (!c)
3541 return;
3542
3543 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3544 return;
3545
3546 count_indegree_walked++;
3547
3548 explore_to_depth(revs, commit_graph_generation(c));
3549
3550 for (p = c->parents; p; p = p->next) {
3551 struct commit *parent = p->item;
3552 int *pi = indegree_slab_at(&info->indegree, parent);
3553
3554 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3555 return;
3556
3557 if (*pi)
3558 (*pi)++;
3559 else
3560 *pi = 2;
3561
3562 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3563
3564 if (revs->first_parent_only)
3565 return;
3566 }
3567 }
3568
3569 static void compute_indegrees_to_depth(struct rev_info *revs,
3570 timestamp_t gen_cutoff)
3571 {
3572 struct topo_walk_info *info = revs->topo_walk_info;
3573 struct commit *c;
3574 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3575 commit_graph_generation(c) >= gen_cutoff)
3576 indegree_walk_step(revs);
3577 }
3578
3579 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3580 {
3581 if (!info)
3582 return;
3583 clear_prio_queue(&info->explore_queue);
3584 clear_prio_queue(&info->indegree_queue);
3585 clear_prio_queue(&info->topo_queue);
3586 clear_indegree_slab(&info->indegree);
3587 clear_author_date_slab(&info->author_date);
3588 free(info);
3589 }
3590
3591 static void reset_topo_walk(struct rev_info *revs)
3592 {
3593 release_revisions_topo_walk_info(revs->topo_walk_info);
3594 revs->topo_walk_info = NULL;
3595 }
3596
3597 static void init_topo_walk(struct rev_info *revs)
3598 {
3599 struct topo_walk_info *info;
3600 struct commit_list *list;
3601 if (revs->topo_walk_info)
3602 reset_topo_walk(revs);
3603
3604 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3605 info = revs->topo_walk_info;
3606 memset(info, 0, sizeof(struct topo_walk_info));
3607
3608 init_indegree_slab(&info->indegree);
3609 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3610 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3611 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3612
3613 switch (revs->sort_order) {
3614 default: /* REV_SORT_IN_GRAPH_ORDER */
3615 info->topo_queue.compare = NULL;
3616 break;
3617 case REV_SORT_BY_COMMIT_DATE:
3618 info->topo_queue.compare = compare_commits_by_commit_date;
3619 break;
3620 case REV_SORT_BY_AUTHOR_DATE:
3621 init_author_date_slab(&info->author_date);
3622 info->topo_queue.compare = compare_commits_by_author_date;
3623 info->topo_queue.cb_data = &info->author_date;
3624 break;
3625 }
3626
3627 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3628 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3629
3630 info->min_generation = GENERATION_NUMBER_INFINITY;
3631 for (list = revs->commits; list; list = list->next) {
3632 struct commit *c = list->item;
3633 timestamp_t generation;
3634
3635 if (repo_parse_commit_gently(revs->repo, c, 1))
3636 continue;
3637
3638 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3639 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3640
3641 generation = commit_graph_generation(c);
3642 if (generation < info->min_generation)
3643 info->min_generation = generation;
3644
3645 *(indegree_slab_at(&info->indegree, c)) = 1;
3646
3647 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3648 record_author_date(&info->author_date, c);
3649 }
3650 compute_indegrees_to_depth(revs, info->min_generation);
3651
3652 for (list = revs->commits; list; list = list->next) {
3653 struct commit *c = list->item;
3654
3655 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3656 prio_queue_put(&info->topo_queue, c);
3657 }
3658
3659 /*
3660 * This is unfortunate; the initial tips need to be shown
3661 * in the order given from the revision traversal machinery.
3662 */
3663 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3664 prio_queue_reverse(&info->topo_queue);
3665
3666 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3667 atexit(trace2_topo_walk_statistics_atexit);
3668 topo_walk_atexit_registered = 1;
3669 }
3670 }
3671
3672 static struct commit *next_topo_commit(struct rev_info *revs)
3673 {
3674 struct commit *c;
3675 struct topo_walk_info *info = revs->topo_walk_info;
3676
3677 /* pop next off of topo_queue */
3678 c = prio_queue_get(&info->topo_queue);
3679
3680 if (c)
3681 *(indegree_slab_at(&info->indegree, c)) = 0;
3682
3683 return c;
3684 }
3685
3686 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3687 {
3688 struct commit_list *p;
3689 struct topo_walk_info *info = revs->topo_walk_info;
3690 if (process_parents(revs, commit, NULL, NULL) < 0) {
3691 if (!revs->ignore_missing_links)
3692 die("Failed to traverse parents of commit %s",
3693 oid_to_hex(&commit->object.oid));
3694 }
3695
3696 count_topo_walked++;
3697
3698 for (p = commit->parents; p; p = p->next) {
3699 struct commit *parent = p->item;
3700 int *pi;
3701 timestamp_t generation;
3702
3703 if (parent->object.flags & UNINTERESTING)
3704 continue;
3705
3706 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3707 continue;
3708
3709 generation = commit_graph_generation(parent);
3710 if (generation < info->min_generation) {
3711 info->min_generation = generation;
3712 compute_indegrees_to_depth(revs, info->min_generation);
3713 }
3714
3715 pi = indegree_slab_at(&info->indegree, parent);
3716
3717 (*pi)--;
3718 if (*pi == 1)
3719 prio_queue_put(&info->topo_queue, parent);
3720
3721 if (revs->first_parent_only)
3722 return;
3723 }
3724 }
3725
3726 int prepare_revision_walk(struct rev_info *revs)
3727 {
3728 int i;
3729 struct object_array old_pending;
3730 struct commit_list **next = &revs->commits;
3731
3732 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3733 revs->pending.nr = 0;
3734 revs->pending.alloc = 0;
3735 revs->pending.objects = NULL;
3736 for (i = 0; i < old_pending.nr; i++) {
3737 struct object_array_entry *e = old_pending.objects + i;
3738 struct commit *commit = handle_commit(revs, e);
3739 if (commit) {
3740 if (!(commit->object.flags & SEEN)) {
3741 commit->object.flags |= SEEN;
3742 next = commit_list_append(commit, next);
3743 }
3744 }
3745 }
3746 object_array_clear(&old_pending);
3747
3748 /* Signal whether we need per-parent treesame decoration */
3749 if (revs->simplify_merges ||
3750 (revs->limited && limiting_can_increase_treesame(revs)))
3751 revs->treesame.name = "treesame";
3752
3753 if (revs->exclude_promisor_objects) {
3754 for_each_packed_object(mark_uninteresting, revs,
3755 FOR_EACH_OBJECT_PROMISOR_ONLY);
3756 }
3757
3758 if (!revs->reflog_info)
3759 prepare_to_use_bloom_filter(revs);
3760 if (!revs->unsorted_input)
3761 commit_list_sort_by_date(&revs->commits);
3762 if (revs->no_walk)
3763 return 0;
3764 if (revs->limited) {
3765 if (limit_list(revs) < 0)
3766 return -1;
3767 if (revs->topo_order)
3768 sort_in_topological_order(&revs->commits, revs->sort_order);
3769 } else if (revs->topo_order)
3770 init_topo_walk(revs);
3771 if (revs->line_level_traverse && want_ancestry(revs))
3772 /*
3773 * At the moment we can only do line-level log with parent
3774 * rewriting by performing this expensive pre-filtering step.
3775 * If parent rewriting is not requested, then we rather
3776 * perform the line-level log filtering during the regular
3777 * history traversal.
3778 */
3779 line_log_filter(revs);
3780 if (revs->simplify_merges)
3781 simplify_merges(revs);
3782 if (revs->children.name)
3783 set_children(revs);
3784
3785 return 0;
3786 }
3787
3788 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3789 struct commit **pp,
3790 struct prio_queue *queue)
3791 {
3792 for (;;) {
3793 struct commit *p = *pp;
3794 if (!revs->limited)
3795 if (process_parents(revs, p, NULL, queue) < 0)
3796 return rewrite_one_error;
3797 if (p->object.flags & UNINTERESTING)
3798 return rewrite_one_ok;
3799 if (!(p->object.flags & TREESAME))
3800 return rewrite_one_ok;
3801 if (!p->parents)
3802 return rewrite_one_noparents;
3803 if (!(p = one_relevant_parent(revs, p->parents)))
3804 return rewrite_one_ok;
3805 *pp = p;
3806 }
3807 }
3808
3809 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3810 {
3811 while (q->nr) {
3812 struct commit *item = prio_queue_peek(q);
3813 struct commit_list *p = *list;
3814
3815 if (p && p->item->date >= item->date)
3816 list = &p->next;
3817 else {
3818 p = commit_list_insert(item, list);
3819 list = &p->next; /* skip newly added item */
3820 prio_queue_get(q); /* pop item */
3821 }
3822 }
3823 }
3824
3825 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3826 {
3827 struct prio_queue queue = { compare_commits_by_commit_date };
3828 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3829 merge_queue_into_list(&queue, &revs->commits);
3830 clear_prio_queue(&queue);
3831 return ret;
3832 }
3833
3834 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3835 rewrite_parent_fn_t rewrite_parent)
3836 {
3837 struct commit_list **pp = &commit->parents;
3838 while (*pp) {
3839 struct commit_list *parent = *pp;
3840 switch (rewrite_parent(revs, &parent->item)) {
3841 case rewrite_one_ok:
3842 break;
3843 case rewrite_one_noparents:
3844 *pp = parent->next;
3845 continue;
3846 case rewrite_one_error:
3847 return -1;
3848 }
3849 pp = &parent->next;
3850 }
3851 remove_duplicate_parents(revs, commit);
3852 return 0;
3853 }
3854
3855 static int commit_match(struct commit *commit, struct rev_info *opt)
3856 {
3857 int retval;
3858 const char *encoding;
3859 const char *message;
3860 struct strbuf buf = STRBUF_INIT;
3861
3862 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3863 return 1;
3864
3865 /* Prepend "fake" headers as needed */
3866 if (opt->grep_filter.use_reflog_filter) {
3867 strbuf_addstr(&buf, "reflog ");
3868 get_reflog_message(&buf, opt->reflog_info);
3869 strbuf_addch(&buf, '\n');
3870 }
3871
3872 /*
3873 * We grep in the user's output encoding, under the assumption that it
3874 * is the encoding they are most likely to write their grep pattern
3875 * for. In addition, it means we will match the "notes" encoding below,
3876 * so we will not end up with a buffer that has two different encodings
3877 * in it.
3878 */
3879 encoding = get_log_output_encoding();
3880 message = logmsg_reencode(commit, NULL, encoding);
3881
3882 /* Copy the commit to temporary if we are using "fake" headers */
3883 if (buf.len)
3884 strbuf_addstr(&buf, message);
3885
3886 if (opt->grep_filter.header_list && opt->mailmap) {
3887 const char *commit_headers[] = { "author ", "committer ", NULL };
3888
3889 if (!buf.len)
3890 strbuf_addstr(&buf, message);
3891
3892 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
3893 }
3894
3895 /* Append "fake" message parts as needed */
3896 if (opt->show_notes) {
3897 if (!buf.len)
3898 strbuf_addstr(&buf, message);
3899 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3900 }
3901
3902 /*
3903 * Find either in the original commit message, or in the temporary.
3904 * Note that we cast away the constness of "message" here. It is
3905 * const because it may come from the cached commit buffer. That's OK,
3906 * because we know that it is modifiable heap memory, and that while
3907 * grep_buffer may modify it for speed, it will restore any
3908 * changes before returning.
3909 */
3910 if (buf.len)
3911 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3912 else
3913 retval = grep_buffer(&opt->grep_filter,
3914 (char *)message, strlen(message));
3915 strbuf_release(&buf);
3916 unuse_commit_buffer(commit, message);
3917 return retval;
3918 }
3919
3920 static inline int want_ancestry(const struct rev_info *revs)
3921 {
3922 return (revs->rewrite_parents || revs->children.name);
3923 }
3924
3925 /*
3926 * Return a timestamp to be used for --since/--until comparisons for this
3927 * commit, based on the revision options.
3928 */
3929 static timestamp_t comparison_date(const struct rev_info *revs,
3930 struct commit *commit)
3931 {
3932 return revs->reflog_info ?
3933 get_reflog_timestamp(revs->reflog_info) :
3934 commit->date;
3935 }
3936
3937 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3938 {
3939 if (commit->object.flags & SHOWN)
3940 return commit_ignore;
3941 if (revs->unpacked && has_object_pack(&commit->object.oid))
3942 return commit_ignore;
3943 if (revs->no_kept_objects) {
3944 if (has_object_kept_pack(&commit->object.oid,
3945 revs->keep_pack_cache_flags))
3946 return commit_ignore;
3947 }
3948 if (commit->object.flags & UNINTERESTING)
3949 return commit_ignore;
3950 if (revs->line_level_traverse && !want_ancestry(revs)) {
3951 /*
3952 * In case of line-level log with parent rewriting
3953 * prepare_revision_walk() already took care of all line-level
3954 * log filtering, and there is nothing left to do here.
3955 *
3956 * If parent rewriting was not requested, then this is the
3957 * place to perform the line-level log filtering. Notably,
3958 * this check, though expensive, must come before the other,
3959 * cheaper filtering conditions, because the tracked line
3960 * ranges must be adjusted even when the commit will end up
3961 * being ignored based on other conditions.
3962 */
3963 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3964 return commit_ignore;
3965 }
3966 if (revs->min_age != -1 &&
3967 comparison_date(revs, commit) > revs->min_age)
3968 return commit_ignore;
3969 if (revs->max_age_as_filter != -1 &&
3970 comparison_date(revs, commit) < revs->max_age_as_filter)
3971 return commit_ignore;
3972 if (revs->min_parents || (revs->max_parents >= 0)) {
3973 int n = commit_list_count(commit->parents);
3974 if ((n < revs->min_parents) ||
3975 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3976 return commit_ignore;
3977 }
3978 if (!commit_match(commit, revs))
3979 return commit_ignore;
3980 if (revs->prune && revs->dense) {
3981 /* Commit without changes? */
3982 if (commit->object.flags & TREESAME) {
3983 int n;
3984 struct commit_list *p;
3985 /* drop merges unless we want parenthood */
3986 if (!want_ancestry(revs))
3987 return commit_ignore;
3988
3989 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3990 return commit_show;
3991
3992 /*
3993 * If we want ancestry, then need to keep any merges
3994 * between relevant commits to tie together topology.
3995 * For consistency with TREESAME and simplification
3996 * use "relevant" here rather than just INTERESTING,
3997 * to treat bottom commit(s) as part of the topology.
3998 */
3999 for (n = 0, p = commit->parents; p; p = p->next)
4000 if (relevant_commit(p->item))
4001 if (++n >= 2)
4002 return commit_show;
4003 return commit_ignore;
4004 }
4005 }
4006 return commit_show;
4007 }
4008
4009 define_commit_slab(saved_parents, struct commit_list *);
4010
4011 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4012
4013 /*
4014 * You may only call save_parents() once per commit (this is checked
4015 * for non-root commits).
4016 */
4017 static void save_parents(struct rev_info *revs, struct commit *commit)
4018 {
4019 struct commit_list **pp;
4020
4021 if (!revs->saved_parents_slab) {
4022 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4023 init_saved_parents(revs->saved_parents_slab);
4024 }
4025
4026 pp = saved_parents_at(revs->saved_parents_slab, commit);
4027
4028 /*
4029 * When walking with reflogs, we may visit the same commit
4030 * several times: once for each appearance in the reflog.
4031 *
4032 * In this case, save_parents() will be called multiple times.
4033 * We want to keep only the first set of parents. We need to
4034 * store a sentinel value for an empty (i.e., NULL) parent
4035 * list to distinguish it from a not-yet-saved list, however.
4036 */
4037 if (*pp)
4038 return;
4039 if (commit->parents)
4040 *pp = copy_commit_list(commit->parents);
4041 else
4042 *pp = EMPTY_PARENT_LIST;
4043 }
4044
4045 static void free_saved_parents(struct rev_info *revs)
4046 {
4047 if (revs->saved_parents_slab)
4048 clear_saved_parents(revs->saved_parents_slab);
4049 }
4050
4051 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4052 {
4053 struct commit_list *parents;
4054
4055 if (!revs->saved_parents_slab)
4056 return commit->parents;
4057
4058 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4059 if (parents == EMPTY_PARENT_LIST)
4060 return NULL;
4061 return parents;
4062 }
4063
4064 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4065 {
4066 enum commit_action action = get_commit_action(revs, commit);
4067
4068 if (action == commit_show &&
4069 revs->prune && revs->dense && want_ancestry(revs)) {
4070 /*
4071 * --full-diff on simplified parents is no good: it
4072 * will show spurious changes from the commits that
4073 * were elided. So we save the parents on the side
4074 * when --full-diff is in effect.
4075 */
4076 if (revs->full_diff)
4077 save_parents(revs, commit);
4078 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4079 return commit_error;
4080 }
4081 return action;
4082 }
4083
4084 static void track_linear(struct rev_info *revs, struct commit *commit)
4085 {
4086 if (revs->track_first_time) {
4087 revs->linear = 1;
4088 revs->track_first_time = 0;
4089 } else {
4090 struct commit_list *p;
4091 for (p = revs->previous_parents; p; p = p->next)
4092 if (p->item == NULL || /* first commit */
4093 oideq(&p->item->object.oid, &commit->object.oid))
4094 break;
4095 revs->linear = p != NULL;
4096 }
4097 if (revs->reverse) {
4098 if (revs->linear)
4099 commit->object.flags |= TRACK_LINEAR;
4100 }
4101 free_commit_list(revs->previous_parents);
4102 revs->previous_parents = copy_commit_list(commit->parents);
4103 }
4104
4105 static struct commit *get_revision_1(struct rev_info *revs)
4106 {
4107 while (1) {
4108 struct commit *commit;
4109
4110 if (revs->reflog_info)
4111 commit = next_reflog_entry(revs->reflog_info);
4112 else if (revs->topo_walk_info)
4113 commit = next_topo_commit(revs);
4114 else
4115 commit = pop_commit(&revs->commits);
4116
4117 if (!commit)
4118 return NULL;
4119
4120 if (revs->reflog_info)
4121 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4122
4123 /*
4124 * If we haven't done the list limiting, we need to look at
4125 * the parents here. We also need to do the date-based limiting
4126 * that we'd otherwise have done in limit_list().
4127 */
4128 if (!revs->limited) {
4129 if (revs->max_age != -1 &&
4130 comparison_date(revs, commit) < revs->max_age)
4131 continue;
4132
4133 if (revs->reflog_info)
4134 try_to_simplify_commit(revs, commit);
4135 else if (revs->topo_walk_info)
4136 expand_topo_walk(revs, commit);
4137 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4138 if (!revs->ignore_missing_links)
4139 die("Failed to traverse parents of commit %s",
4140 oid_to_hex(&commit->object.oid));
4141 }
4142 }
4143
4144 switch (simplify_commit(revs, commit)) {
4145 case commit_ignore:
4146 continue;
4147 case commit_error:
4148 die("Failed to simplify parents of commit %s",
4149 oid_to_hex(&commit->object.oid));
4150 default:
4151 if (revs->track_linear)
4152 track_linear(revs, commit);
4153 return commit;
4154 }
4155 }
4156 }
4157
4158 /*
4159 * Return true for entries that have not yet been shown. (This is an
4160 * object_array_each_func_t.)
4161 */
4162 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
4163 {
4164 return !(entry->item->flags & SHOWN);
4165 }
4166
4167 /*
4168 * If array is on the verge of a realloc, garbage-collect any entries
4169 * that have already been shown to try to free up some space.
4170 */
4171 static void gc_boundary(struct object_array *array)
4172 {
4173 if (array->nr == array->alloc)
4174 object_array_filter(array, entry_unshown, NULL);
4175 }
4176
4177 static void create_boundary_commit_list(struct rev_info *revs)
4178 {
4179 unsigned i;
4180 struct commit *c;
4181 struct object_array *array = &revs->boundary_commits;
4182 struct object_array_entry *objects = array->objects;
4183
4184 /*
4185 * If revs->commits is non-NULL at this point, an error occurred in
4186 * get_revision_1(). Ignore the error and continue printing the
4187 * boundary commits anyway. (This is what the code has always
4188 * done.)
4189 */
4190 free_commit_list(revs->commits);
4191 revs->commits = NULL;
4192
4193 /*
4194 * Put all of the actual boundary commits from revs->boundary_commits
4195 * into revs->commits
4196 */
4197 for (i = 0; i < array->nr; i++) {
4198 c = (struct commit *)(objects[i].item);
4199 if (!c)
4200 continue;
4201 if (!(c->object.flags & CHILD_SHOWN))
4202 continue;
4203 if (c->object.flags & (SHOWN | BOUNDARY))
4204 continue;
4205 c->object.flags |= BOUNDARY;
4206 commit_list_insert(c, &revs->commits);
4207 }
4208
4209 /*
4210 * If revs->topo_order is set, sort the boundary commits
4211 * in topological order
4212 */
4213 sort_in_topological_order(&revs->commits, revs->sort_order);
4214 }
4215
4216 static struct commit *get_revision_internal(struct rev_info *revs)
4217 {
4218 struct commit *c = NULL;
4219 struct commit_list *l;
4220
4221 if (revs->boundary == 2) {
4222 /*
4223 * All of the normal commits have already been returned,
4224 * and we are now returning boundary commits.
4225 * create_boundary_commit_list() has populated
4226 * revs->commits with the remaining commits to return.
4227 */
4228 c = pop_commit(&revs->commits);
4229 if (c)
4230 c->object.flags |= SHOWN;
4231 return c;
4232 }
4233
4234 /*
4235 * If our max_count counter has reached zero, then we are done. We
4236 * don't simply return NULL because we still might need to show
4237 * boundary commits. But we want to avoid calling get_revision_1, which
4238 * might do a considerable amount of work finding the next commit only
4239 * for us to throw it away.
4240 *
4241 * If it is non-zero, then either we don't have a max_count at all
4242 * (-1), or it is still counting, in which case we decrement.
4243 */
4244 if (revs->max_count) {
4245 c = get_revision_1(revs);
4246 if (c) {
4247 while (revs->skip_count > 0) {
4248 revs->skip_count--;
4249 c = get_revision_1(revs);
4250 if (!c)
4251 break;
4252 }
4253 }
4254
4255 if (revs->max_count > 0)
4256 revs->max_count--;
4257 }
4258
4259 if (c)
4260 c->object.flags |= SHOWN;
4261
4262 if (!revs->boundary)
4263 return c;
4264
4265 if (!c) {
4266 /*
4267 * get_revision_1() runs out the commits, and
4268 * we are done computing the boundaries.
4269 * switch to boundary commits output mode.
4270 */
4271 revs->boundary = 2;
4272
4273 /*
4274 * Update revs->commits to contain the list of
4275 * boundary commits.
4276 */
4277 create_boundary_commit_list(revs);
4278
4279 return get_revision_internal(revs);
4280 }
4281
4282 /*
4283 * boundary commits are the commits that are parents of the
4284 * ones we got from get_revision_1() but they themselves are
4285 * not returned from get_revision_1(). Before returning
4286 * 'c', we need to mark its parents that they could be boundaries.
4287 */
4288
4289 for (l = c->parents; l; l = l->next) {
4290 struct object *p;
4291 p = &(l->item->object);
4292 if (p->flags & (CHILD_SHOWN | SHOWN))
4293 continue;
4294 p->flags |= CHILD_SHOWN;
4295 gc_boundary(&revs->boundary_commits);
4296 add_object_array(p, NULL, &revs->boundary_commits);
4297 }
4298
4299 return c;
4300 }
4301
4302 struct commit *get_revision(struct rev_info *revs)
4303 {
4304 struct commit *c;
4305 struct commit_list *reversed;
4306
4307 if (revs->reverse) {
4308 reversed = NULL;
4309 while ((c = get_revision_internal(revs)))
4310 commit_list_insert(c, &reversed);
4311 revs->commits = reversed;
4312 revs->reverse = 0;
4313 revs->reverse_output_stage = 1;
4314 }
4315
4316 if (revs->reverse_output_stage) {
4317 c = pop_commit(&revs->commits);
4318 if (revs->track_linear)
4319 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4320 return c;
4321 }
4322
4323 c = get_revision_internal(revs);
4324 if (c && revs->graph)
4325 graph_update(revs->graph, c);
4326 if (!c) {
4327 free_saved_parents(revs);
4328 free_commit_list(revs->previous_parents);
4329 revs->previous_parents = NULL;
4330 }
4331 return c;
4332 }
4333
4334 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4335 {
4336 if (commit->object.flags & BOUNDARY)
4337 return "-";
4338 else if (commit->object.flags & UNINTERESTING)
4339 return "^";
4340 else if (commit->object.flags & PATCHSAME)
4341 return "=";
4342 else if (!revs || revs->left_right) {
4343 if (commit->object.flags & SYMMETRIC_LEFT)
4344 return "<";
4345 else
4346 return ">";
4347 } else if (revs->graph)
4348 return "*";
4349 else if (revs->cherry_mark)
4350 return "+";
4351 return "";
4352 }
4353
4354 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4355 {
4356 const char *mark = get_revision_mark(revs, commit);
4357 if (!strlen(mark))
4358 return;
4359 fputs(mark, stdout);
4360 putchar(' ');
4361 }