]> git.ipfire.org Git - thirdparty/git.git/blob - revision.c
fetch: support hideRefs to speed up connectivity checks
[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, "fetch") && strcmp(section, "receive") &&
1578 strcmp(section, "uploadpack"))
1579 die(_("unsupported section for hidden refs: %s"), section);
1580
1581 if (exclusions->hidden_refs_configured)
1582 die(_("--exclude-hidden= passed more than once"));
1583
1584 cb.exclusions = exclusions;
1585 cb.section = section;
1586
1587 git_config(hide_refs_config, &cb);
1588 }
1589
1590 struct all_refs_cb {
1591 int all_flags;
1592 int warned_bad_reflog;
1593 struct rev_info *all_revs;
1594 const char *name_for_errormsg;
1595 struct worktree *wt;
1596 };
1597
1598 static int handle_one_ref(const char *path, const struct object_id *oid,
1599 int flag UNUSED,
1600 void *cb_data)
1601 {
1602 struct all_refs_cb *cb = cb_data;
1603 struct object *object;
1604
1605 if (ref_excluded(&cb->all_revs->ref_excludes, path))
1606 return 0;
1607
1608 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1609 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1610 add_pending_object(cb->all_revs, object, path);
1611 return 0;
1612 }
1613
1614 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1615 unsigned flags)
1616 {
1617 cb->all_revs = revs;
1618 cb->all_flags = flags;
1619 revs->rev_input_given = 1;
1620 cb->wt = NULL;
1621 }
1622
1623 static void handle_refs(struct ref_store *refs,
1624 struct rev_info *revs, unsigned flags,
1625 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1626 {
1627 struct all_refs_cb cb;
1628
1629 if (!refs) {
1630 /* this could happen with uninitialized submodules */
1631 return;
1632 }
1633
1634 init_all_refs_cb(&cb, revs, flags);
1635 for_each(refs, handle_one_ref, &cb);
1636 }
1637
1638 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1639 {
1640 struct all_refs_cb *cb = cb_data;
1641 if (!is_null_oid(oid)) {
1642 struct object *o = parse_object(cb->all_revs->repo, oid);
1643 if (o) {
1644 o->flags |= cb->all_flags;
1645 /* ??? CMDLINEFLAGS ??? */
1646 add_pending_object(cb->all_revs, o, "");
1647 }
1648 else if (!cb->warned_bad_reflog) {
1649 warning("reflog of '%s' references pruned commits",
1650 cb->name_for_errormsg);
1651 cb->warned_bad_reflog = 1;
1652 }
1653 }
1654 }
1655
1656 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1657 const char *email UNUSED,
1658 timestamp_t timestamp UNUSED,
1659 int tz UNUSED,
1660 const char *message UNUSED,
1661 void *cb_data)
1662 {
1663 handle_one_reflog_commit(ooid, cb_data);
1664 handle_one_reflog_commit(noid, cb_data);
1665 return 0;
1666 }
1667
1668 static int handle_one_reflog(const char *refname_in_wt,
1669 const struct object_id *oid UNUSED,
1670 int flag UNUSED, void *cb_data)
1671 {
1672 struct all_refs_cb *cb = cb_data;
1673 struct strbuf refname = STRBUF_INIT;
1674
1675 cb->warned_bad_reflog = 0;
1676 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1677 cb->name_for_errormsg = refname.buf;
1678 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1679 refname.buf,
1680 handle_one_reflog_ent, cb_data);
1681 strbuf_release(&refname);
1682 return 0;
1683 }
1684
1685 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1686 {
1687 struct worktree **worktrees, **p;
1688
1689 worktrees = get_worktrees();
1690 for (p = worktrees; *p; p++) {
1691 struct worktree *wt = *p;
1692
1693 if (wt->is_current)
1694 continue;
1695
1696 cb->wt = wt;
1697 refs_for_each_reflog(get_worktree_ref_store(wt),
1698 handle_one_reflog,
1699 cb);
1700 }
1701 free_worktrees(worktrees);
1702 }
1703
1704 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1705 {
1706 struct all_refs_cb cb;
1707
1708 cb.all_revs = revs;
1709 cb.all_flags = flags;
1710 cb.wt = NULL;
1711 for_each_reflog(handle_one_reflog, &cb);
1712
1713 if (!revs->single_worktree)
1714 add_other_reflogs_to_pending(&cb);
1715 }
1716
1717 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1718 struct strbuf *path, unsigned int flags)
1719 {
1720 size_t baselen = path->len;
1721 int i;
1722
1723 if (it->entry_count >= 0) {
1724 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1725 tree->object.flags |= flags;
1726 add_pending_object_with_path(revs, &tree->object, "",
1727 040000, path->buf);
1728 }
1729
1730 for (i = 0; i < it->subtree_nr; i++) {
1731 struct cache_tree_sub *sub = it->down[i];
1732 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1733 add_cache_tree(sub->cache_tree, revs, path, flags);
1734 strbuf_setlen(path, baselen);
1735 }
1736
1737 }
1738
1739 static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1740 {
1741 struct string_list_item *item;
1742 struct string_list *resolve_undo = istate->resolve_undo;
1743
1744 if (!resolve_undo)
1745 return;
1746
1747 for_each_string_list_item(item, resolve_undo) {
1748 const char *path = item->string;
1749 struct resolve_undo_info *ru = item->util;
1750 int i;
1751
1752 if (!ru)
1753 continue;
1754 for (i = 0; i < 3; i++) {
1755 struct blob *blob;
1756
1757 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1758 continue;
1759
1760 blob = lookup_blob(revs->repo, &ru->oid[i]);
1761 if (!blob) {
1762 warning(_("resolve-undo records `%s` which is missing"),
1763 oid_to_hex(&ru->oid[i]));
1764 continue;
1765 }
1766 add_pending_object_with_path(revs, &blob->object, "",
1767 ru->mode[i], path);
1768 }
1769 }
1770 }
1771
1772 static void do_add_index_objects_to_pending(struct rev_info *revs,
1773 struct index_state *istate,
1774 unsigned int flags)
1775 {
1776 int i;
1777
1778 /* TODO: audit for interaction with sparse-index. */
1779 ensure_full_index(istate);
1780 for (i = 0; i < istate->cache_nr; i++) {
1781 struct cache_entry *ce = istate->cache[i];
1782 struct blob *blob;
1783
1784 if (S_ISGITLINK(ce->ce_mode))
1785 continue;
1786
1787 blob = lookup_blob(revs->repo, &ce->oid);
1788 if (!blob)
1789 die("unable to add index blob to traversal");
1790 blob->object.flags |= flags;
1791 add_pending_object_with_path(revs, &blob->object, "",
1792 ce->ce_mode, ce->name);
1793 }
1794
1795 if (istate->cache_tree) {
1796 struct strbuf path = STRBUF_INIT;
1797 add_cache_tree(istate->cache_tree, revs, &path, flags);
1798 strbuf_release(&path);
1799 }
1800
1801 add_resolve_undo_to_pending(istate, revs);
1802 }
1803
1804 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1805 {
1806 struct worktree **worktrees, **p;
1807
1808 repo_read_index(revs->repo);
1809 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1810
1811 if (revs->single_worktree)
1812 return;
1813
1814 worktrees = get_worktrees();
1815 for (p = worktrees; *p; p++) {
1816 struct worktree *wt = *p;
1817 struct index_state istate = INDEX_STATE_INIT(revs->repo);
1818
1819 if (wt->is_current)
1820 continue; /* current index already taken care of */
1821
1822 if (read_index_from(&istate,
1823 worktree_git_path(wt, "index"),
1824 get_worktree_git_dir(wt)) > 0)
1825 do_add_index_objects_to_pending(revs, &istate, flags);
1826 discard_index(&istate);
1827 }
1828 free_worktrees(worktrees);
1829 }
1830
1831 struct add_alternate_refs_data {
1832 struct rev_info *revs;
1833 unsigned int flags;
1834 };
1835
1836 static void add_one_alternate_ref(const struct object_id *oid,
1837 void *vdata)
1838 {
1839 const char *name = ".alternate";
1840 struct add_alternate_refs_data *data = vdata;
1841 struct object *obj;
1842
1843 obj = get_reference(data->revs, name, oid, data->flags);
1844 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1845 add_pending_object(data->revs, obj, name);
1846 }
1847
1848 static void add_alternate_refs_to_pending(struct rev_info *revs,
1849 unsigned int flags)
1850 {
1851 struct add_alternate_refs_data data;
1852 data.revs = revs;
1853 data.flags = flags;
1854 for_each_alternate_ref(add_one_alternate_ref, &data);
1855 }
1856
1857 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1858 int exclude_parent)
1859 {
1860 struct object_id oid;
1861 struct object *it;
1862 struct commit *commit;
1863 struct commit_list *parents;
1864 int parent_number;
1865 const char *arg = arg_;
1866
1867 if (*arg == '^') {
1868 flags ^= UNINTERESTING | BOTTOM;
1869 arg++;
1870 }
1871 if (get_oid_committish(arg, &oid))
1872 return 0;
1873 while (1) {
1874 it = get_reference(revs, arg, &oid, 0);
1875 if (!it && revs->ignore_missing)
1876 return 0;
1877 if (it->type != OBJ_TAG)
1878 break;
1879 if (!((struct tag*)it)->tagged)
1880 return 0;
1881 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1882 }
1883 if (it->type != OBJ_COMMIT)
1884 return 0;
1885 commit = (struct commit *)it;
1886 if (exclude_parent &&
1887 exclude_parent > commit_list_count(commit->parents))
1888 return 0;
1889 for (parents = commit->parents, parent_number = 1;
1890 parents;
1891 parents = parents->next, parent_number++) {
1892 if (exclude_parent && parent_number != exclude_parent)
1893 continue;
1894
1895 it = &parents->item->object;
1896 it->flags |= flags;
1897 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1898 add_pending_object(revs, it, arg);
1899 }
1900 return 1;
1901 }
1902
1903 void repo_init_revisions(struct repository *r,
1904 struct rev_info *revs,
1905 const char *prefix)
1906 {
1907 struct rev_info blank = REV_INFO_INIT;
1908 memcpy(revs, &blank, sizeof(*revs));
1909
1910 revs->repo = r;
1911 revs->pruning.repo = r;
1912 revs->pruning.add_remove = file_add_remove;
1913 revs->pruning.change = file_change;
1914 revs->pruning.change_fn_data = revs;
1915 revs->prefix = prefix;
1916
1917 grep_init(&revs->grep_filter, revs->repo);
1918 revs->grep_filter.status_only = 1;
1919
1920 repo_diff_setup(revs->repo, &revs->diffopt);
1921 if (prefix && !revs->diffopt.prefix) {
1922 revs->diffopt.prefix = prefix;
1923 revs->diffopt.prefix_length = strlen(prefix);
1924 }
1925
1926 init_display_notes(&revs->notes_opt);
1927 list_objects_filter_init(&revs->filter);
1928 init_ref_exclusions(&revs->ref_excludes);
1929 }
1930
1931 static void add_pending_commit_list(struct rev_info *revs,
1932 struct commit_list *commit_list,
1933 unsigned int flags)
1934 {
1935 while (commit_list) {
1936 struct object *object = &commit_list->item->object;
1937 object->flags |= flags;
1938 add_pending_object(revs, object, oid_to_hex(&object->oid));
1939 commit_list = commit_list->next;
1940 }
1941 }
1942
1943 static void prepare_show_merge(struct rev_info *revs)
1944 {
1945 struct commit_list *bases;
1946 struct commit *head, *other;
1947 struct object_id oid;
1948 const char **prune = NULL;
1949 int i, prune_num = 1; /* counting terminating NULL */
1950 struct index_state *istate = revs->repo->index;
1951
1952 if (get_oid("HEAD", &oid))
1953 die("--merge without HEAD?");
1954 head = lookup_commit_or_die(&oid, "HEAD");
1955 if (get_oid("MERGE_HEAD", &oid))
1956 die("--merge without MERGE_HEAD?");
1957 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1958 add_pending_object(revs, &head->object, "HEAD");
1959 add_pending_object(revs, &other->object, "MERGE_HEAD");
1960 bases = get_merge_bases(head, other);
1961 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1962 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1963 free_commit_list(bases);
1964 head->object.flags |= SYMMETRIC_LEFT;
1965
1966 if (!istate->cache_nr)
1967 repo_read_index(revs->repo);
1968 for (i = 0; i < istate->cache_nr; i++) {
1969 const struct cache_entry *ce = istate->cache[i];
1970 if (!ce_stage(ce))
1971 continue;
1972 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1973 prune_num++;
1974 REALLOC_ARRAY(prune, prune_num);
1975 prune[prune_num-2] = ce->name;
1976 prune[prune_num-1] = NULL;
1977 }
1978 while ((i+1 < istate->cache_nr) &&
1979 ce_same_name(ce, istate->cache[i+1]))
1980 i++;
1981 }
1982 clear_pathspec(&revs->prune_data);
1983 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1984 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1985 revs->limited = 1;
1986 }
1987
1988 static int dotdot_missing(const char *arg, char *dotdot,
1989 struct rev_info *revs, int symmetric)
1990 {
1991 if (revs->ignore_missing)
1992 return 0;
1993 /* de-munge so we report the full argument */
1994 *dotdot = '.';
1995 die(symmetric
1996 ? "Invalid symmetric difference expression %s"
1997 : "Invalid revision range %s", arg);
1998 }
1999
2000 static int handle_dotdot_1(const char *arg, char *dotdot,
2001 struct rev_info *revs, int flags,
2002 int cant_be_filename,
2003 struct object_context *a_oc,
2004 struct object_context *b_oc)
2005 {
2006 const char *a_name, *b_name;
2007 struct object_id a_oid, b_oid;
2008 struct object *a_obj, *b_obj;
2009 unsigned int a_flags, b_flags;
2010 int symmetric = 0;
2011 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
2012 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
2013
2014 a_name = arg;
2015 if (!*a_name)
2016 a_name = "HEAD";
2017
2018 b_name = dotdot + 2;
2019 if (*b_name == '.') {
2020 symmetric = 1;
2021 b_name++;
2022 }
2023 if (!*b_name)
2024 b_name = "HEAD";
2025
2026 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2027 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2028 return -1;
2029
2030 if (!cant_be_filename) {
2031 *dotdot = '.';
2032 verify_non_filename(revs->prefix, arg);
2033 *dotdot = '\0';
2034 }
2035
2036 a_obj = parse_object(revs->repo, &a_oid);
2037 b_obj = parse_object(revs->repo, &b_oid);
2038 if (!a_obj || !b_obj)
2039 return dotdot_missing(arg, dotdot, revs, symmetric);
2040
2041 if (!symmetric) {
2042 /* just A..B */
2043 b_flags = flags;
2044 a_flags = flags_exclude;
2045 } else {
2046 /* A...B -- find merge bases between the two */
2047 struct commit *a, *b;
2048 struct commit_list *exclude;
2049
2050 a = lookup_commit_reference(revs->repo, &a_obj->oid);
2051 b = lookup_commit_reference(revs->repo, &b_obj->oid);
2052 if (!a || !b)
2053 return dotdot_missing(arg, dotdot, revs, symmetric);
2054
2055 exclude = get_merge_bases(a, b);
2056 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2057 flags_exclude);
2058 add_pending_commit_list(revs, exclude, flags_exclude);
2059 free_commit_list(exclude);
2060
2061 b_flags = flags;
2062 a_flags = flags | SYMMETRIC_LEFT;
2063 }
2064
2065 a_obj->flags |= a_flags;
2066 b_obj->flags |= b_flags;
2067 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2068 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2069 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2070 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2071 return 0;
2072 }
2073
2074 static int handle_dotdot(const char *arg,
2075 struct rev_info *revs, int flags,
2076 int cant_be_filename)
2077 {
2078 struct object_context a_oc, b_oc;
2079 char *dotdot = strstr(arg, "..");
2080 int ret;
2081
2082 if (!dotdot)
2083 return -1;
2084
2085 memset(&a_oc, 0, sizeof(a_oc));
2086 memset(&b_oc, 0, sizeof(b_oc));
2087
2088 *dotdot = '\0';
2089 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2090 &a_oc, &b_oc);
2091 *dotdot = '.';
2092
2093 free(a_oc.path);
2094 free(b_oc.path);
2095
2096 return ret;
2097 }
2098
2099 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2100 {
2101 struct object_context oc;
2102 char *mark;
2103 struct object *object;
2104 struct object_id oid;
2105 int local_flags;
2106 const char *arg = arg_;
2107 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2108 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2109
2110 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2111
2112 if (!cant_be_filename && !strcmp(arg, "..")) {
2113 /*
2114 * Just ".."? That is not a range but the
2115 * pathspec for the parent directory.
2116 */
2117 return -1;
2118 }
2119
2120 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2121 return 0;
2122
2123 mark = strstr(arg, "^@");
2124 if (mark && !mark[2]) {
2125 *mark = 0;
2126 if (add_parents_only(revs, arg, flags, 0))
2127 return 0;
2128 *mark = '^';
2129 }
2130 mark = strstr(arg, "^!");
2131 if (mark && !mark[2]) {
2132 *mark = 0;
2133 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2134 *mark = '^';
2135 }
2136 mark = strstr(arg, "^-");
2137 if (mark) {
2138 int exclude_parent = 1;
2139
2140 if (mark[2]) {
2141 if (strtol_i(mark + 2, 10, &exclude_parent) ||
2142 exclude_parent < 1)
2143 return -1;
2144 }
2145
2146 *mark = 0;
2147 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2148 *mark = '^';
2149 }
2150
2151 local_flags = 0;
2152 if (*arg == '^') {
2153 local_flags = UNINTERESTING | BOTTOM;
2154 arg++;
2155 }
2156
2157 if (revarg_opt & REVARG_COMMITTISH)
2158 get_sha1_flags |= GET_OID_COMMITTISH;
2159
2160 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2161 return revs->ignore_missing ? 0 : -1;
2162 if (!cant_be_filename)
2163 verify_non_filename(revs->prefix, arg);
2164 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2165 if (!object)
2166 return revs->ignore_missing ? 0 : -1;
2167 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2168 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2169 free(oc.path);
2170 return 0;
2171 }
2172
2173 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2174 {
2175 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2176 if (!ret)
2177 revs->rev_input_given = 1;
2178 return ret;
2179 }
2180
2181 static void read_pathspec_from_stdin(struct strbuf *sb,
2182 struct strvec *prune)
2183 {
2184 while (strbuf_getline(sb, stdin) != EOF)
2185 strvec_push(prune, sb->buf);
2186 }
2187
2188 static void read_revisions_from_stdin(struct rev_info *revs,
2189 struct strvec *prune)
2190 {
2191 struct strbuf sb;
2192 int seen_dashdash = 0;
2193 int save_warning;
2194
2195 save_warning = warn_on_object_refname_ambiguity;
2196 warn_on_object_refname_ambiguity = 0;
2197
2198 strbuf_init(&sb, 1000);
2199 while (strbuf_getline(&sb, stdin) != EOF) {
2200 int len = sb.len;
2201 if (!len)
2202 break;
2203 if (sb.buf[0] == '-') {
2204 if (len == 2 && sb.buf[1] == '-') {
2205 seen_dashdash = 1;
2206 break;
2207 }
2208 die("options not supported in --stdin mode");
2209 }
2210 if (handle_revision_arg(sb.buf, revs, 0,
2211 REVARG_CANNOT_BE_FILENAME))
2212 die("bad revision '%s'", sb.buf);
2213 }
2214 if (seen_dashdash)
2215 read_pathspec_from_stdin(&sb, prune);
2216
2217 strbuf_release(&sb);
2218 warn_on_object_refname_ambiguity = save_warning;
2219 }
2220
2221 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2222 {
2223 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2224 }
2225
2226 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2227 {
2228 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2229 }
2230
2231 static void add_message_grep(struct rev_info *revs, const char *pattern)
2232 {
2233 add_grep(revs, pattern, GREP_PATTERN_BODY);
2234 }
2235
2236 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2237 int *unkc, const char **unkv,
2238 const struct setup_revision_opt* opt)
2239 {
2240 const char *arg = argv[0];
2241 const char *optarg = NULL;
2242 int argcount;
2243 const unsigned hexsz = the_hash_algo->hexsz;
2244
2245 /* pseudo revision arguments */
2246 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2247 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2248 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2249 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2250 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2251 !strcmp(arg, "--indexed-objects") ||
2252 !strcmp(arg, "--alternate-refs") ||
2253 starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
2254 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2255 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2256 {
2257 unkv[(*unkc)++] = arg;
2258 return 1;
2259 }
2260
2261 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2262 revs->max_count = atoi(optarg);
2263 revs->no_walk = 0;
2264 return argcount;
2265 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2266 revs->skip_count = atoi(optarg);
2267 return argcount;
2268 } else if ((*arg == '-') && isdigit(arg[1])) {
2269 /* accept -<digit>, like traditional "head" */
2270 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2271 revs->max_count < 0)
2272 die("'%s': not a non-negative integer", arg + 1);
2273 revs->no_walk = 0;
2274 } else if (!strcmp(arg, "-n")) {
2275 if (argc <= 1)
2276 return error("-n requires an argument");
2277 revs->max_count = atoi(argv[1]);
2278 revs->no_walk = 0;
2279 return 2;
2280 } else if (skip_prefix(arg, "-n", &optarg)) {
2281 revs->max_count = atoi(optarg);
2282 revs->no_walk = 0;
2283 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2284 revs->max_age = atoi(optarg);
2285 return argcount;
2286 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2287 revs->max_age = approxidate(optarg);
2288 return argcount;
2289 } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2290 revs->max_age_as_filter = approxidate(optarg);
2291 return argcount;
2292 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2293 revs->max_age = approxidate(optarg);
2294 return argcount;
2295 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2296 revs->min_age = atoi(optarg);
2297 return argcount;
2298 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2299 revs->min_age = approxidate(optarg);
2300 return argcount;
2301 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2302 revs->min_age = approxidate(optarg);
2303 return argcount;
2304 } else if (!strcmp(arg, "--first-parent")) {
2305 revs->first_parent_only = 1;
2306 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2307 revs->exclude_first_parent_only = 1;
2308 } else if (!strcmp(arg, "--ancestry-path")) {
2309 revs->ancestry_path = 1;
2310 revs->simplify_history = 0;
2311 revs->limited = 1;
2312 revs->ancestry_path_implicit_bottoms = 1;
2313 } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2314 struct commit *c;
2315 struct object_id oid;
2316 const char *msg = _("could not get commit for ancestry-path argument %s");
2317
2318 revs->ancestry_path = 1;
2319 revs->simplify_history = 0;
2320 revs->limited = 1;
2321
2322 if (repo_get_oid_committish(revs->repo, optarg, &oid))
2323 return error(msg, optarg);
2324 get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2325 c = lookup_commit_reference(revs->repo, &oid);
2326 if (!c)
2327 return error(msg, optarg);
2328 commit_list_insert(c, &revs->ancestry_path_bottoms);
2329 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2330 init_reflog_walk(&revs->reflog_info);
2331 } else if (!strcmp(arg, "--default")) {
2332 if (argc <= 1)
2333 return error("bad --default argument");
2334 revs->def = argv[1];
2335 return 2;
2336 } else if (!strcmp(arg, "--merge")) {
2337 revs->show_merge = 1;
2338 } else if (!strcmp(arg, "--topo-order")) {
2339 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2340 revs->topo_order = 1;
2341 } else if (!strcmp(arg, "--simplify-merges")) {
2342 revs->simplify_merges = 1;
2343 revs->topo_order = 1;
2344 revs->rewrite_parents = 1;
2345 revs->simplify_history = 0;
2346 revs->limited = 1;
2347 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2348 revs->simplify_merges = 1;
2349 revs->topo_order = 1;
2350 revs->rewrite_parents = 1;
2351 revs->simplify_history = 0;
2352 revs->simplify_by_decoration = 1;
2353 revs->limited = 1;
2354 revs->prune = 1;
2355 } else if (!strcmp(arg, "--date-order")) {
2356 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2357 revs->topo_order = 1;
2358 } else if (!strcmp(arg, "--author-date-order")) {
2359 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2360 revs->topo_order = 1;
2361 } else if (!strcmp(arg, "--early-output")) {
2362 revs->early_output = 100;
2363 revs->topo_order = 1;
2364 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2365 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2366 die("'%s': not a non-negative integer", optarg);
2367 revs->topo_order = 1;
2368 } else if (!strcmp(arg, "--parents")) {
2369 revs->rewrite_parents = 1;
2370 revs->print_parents = 1;
2371 } else if (!strcmp(arg, "--dense")) {
2372 revs->dense = 1;
2373 } else if (!strcmp(arg, "--sparse")) {
2374 revs->dense = 0;
2375 } else if (!strcmp(arg, "--in-commit-order")) {
2376 revs->tree_blobs_in_commit_order = 1;
2377 } else if (!strcmp(arg, "--remove-empty")) {
2378 revs->remove_empty_trees = 1;
2379 } else if (!strcmp(arg, "--merges")) {
2380 revs->min_parents = 2;
2381 } else if (!strcmp(arg, "--no-merges")) {
2382 revs->max_parents = 1;
2383 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2384 revs->min_parents = atoi(optarg);
2385 } else if (!strcmp(arg, "--no-min-parents")) {
2386 revs->min_parents = 0;
2387 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2388 revs->max_parents = atoi(optarg);
2389 } else if (!strcmp(arg, "--no-max-parents")) {
2390 revs->max_parents = -1;
2391 } else if (!strcmp(arg, "--boundary")) {
2392 revs->boundary = 1;
2393 } else if (!strcmp(arg, "--left-right")) {
2394 revs->left_right = 1;
2395 } else if (!strcmp(arg, "--left-only")) {
2396 if (revs->right_only)
2397 die("--left-only is incompatible with --right-only"
2398 " or --cherry");
2399 revs->left_only = 1;
2400 } else if (!strcmp(arg, "--right-only")) {
2401 if (revs->left_only)
2402 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2403 revs->right_only = 1;
2404 } else if (!strcmp(arg, "--cherry")) {
2405 if (revs->left_only)
2406 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2407 revs->cherry_mark = 1;
2408 revs->right_only = 1;
2409 revs->max_parents = 1;
2410 revs->limited = 1;
2411 } else if (!strcmp(arg, "--count")) {
2412 revs->count = 1;
2413 } else if (!strcmp(arg, "--cherry-mark")) {
2414 if (revs->cherry_pick)
2415 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2416 revs->cherry_mark = 1;
2417 revs->limited = 1; /* needs limit_list() */
2418 } else if (!strcmp(arg, "--cherry-pick")) {
2419 if (revs->cherry_mark)
2420 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2421 revs->cherry_pick = 1;
2422 revs->limited = 1;
2423 } else if (!strcmp(arg, "--objects")) {
2424 revs->tag_objects = 1;
2425 revs->tree_objects = 1;
2426 revs->blob_objects = 1;
2427 } else if (!strcmp(arg, "--objects-edge")) {
2428 revs->tag_objects = 1;
2429 revs->tree_objects = 1;
2430 revs->blob_objects = 1;
2431 revs->edge_hint = 1;
2432 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2433 revs->tag_objects = 1;
2434 revs->tree_objects = 1;
2435 revs->blob_objects = 1;
2436 revs->edge_hint = 1;
2437 revs->edge_hint_aggressive = 1;
2438 } else if (!strcmp(arg, "--verify-objects")) {
2439 revs->tag_objects = 1;
2440 revs->tree_objects = 1;
2441 revs->blob_objects = 1;
2442 revs->verify_objects = 1;
2443 disable_commit_graph(revs->repo);
2444 } else if (!strcmp(arg, "--unpacked")) {
2445 revs->unpacked = 1;
2446 } else if (starts_with(arg, "--unpacked=")) {
2447 die(_("--unpacked=<packfile> no longer supported"));
2448 } else if (!strcmp(arg, "--no-kept-objects")) {
2449 revs->no_kept_objects = 1;
2450 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2451 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2452 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2453 revs->no_kept_objects = 1;
2454 if (!strcmp(optarg, "in-core"))
2455 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2456 if (!strcmp(optarg, "on-disk"))
2457 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2458 } else if (!strcmp(arg, "-r")) {
2459 revs->diff = 1;
2460 revs->diffopt.flags.recursive = 1;
2461 } else if (!strcmp(arg, "-t")) {
2462 revs->diff = 1;
2463 revs->diffopt.flags.recursive = 1;
2464 revs->diffopt.flags.tree_in_recursive = 1;
2465 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2466 return argcount;
2467 } else if (!strcmp(arg, "-v")) {
2468 revs->verbose_header = 1;
2469 } else if (!strcmp(arg, "--pretty")) {
2470 revs->verbose_header = 1;
2471 revs->pretty_given = 1;
2472 get_commit_format(NULL, revs);
2473 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2474 skip_prefix(arg, "--format=", &optarg)) {
2475 /*
2476 * Detached form ("--pretty X" as opposed to "--pretty=X")
2477 * not allowed, since the argument is optional.
2478 */
2479 revs->verbose_header = 1;
2480 revs->pretty_given = 1;
2481 get_commit_format(optarg, revs);
2482 } else if (!strcmp(arg, "--expand-tabs")) {
2483 revs->expand_tabs_in_log = 8;
2484 } else if (!strcmp(arg, "--no-expand-tabs")) {
2485 revs->expand_tabs_in_log = 0;
2486 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2487 int val;
2488 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2489 die("'%s': not a non-negative integer", arg);
2490 revs->expand_tabs_in_log = val;
2491 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2492 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2493 revs->show_notes_given = 1;
2494 } else if (!strcmp(arg, "--show-signature")) {
2495 revs->show_signature = 1;
2496 } else if (!strcmp(arg, "--no-show-signature")) {
2497 revs->show_signature = 0;
2498 } else if (!strcmp(arg, "--show-linear-break")) {
2499 revs->break_bar = " ..........";
2500 revs->track_linear = 1;
2501 revs->track_first_time = 1;
2502 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2503 revs->break_bar = xstrdup(optarg);
2504 revs->track_linear = 1;
2505 revs->track_first_time = 1;
2506 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2507 skip_prefix(arg, "--notes=", &optarg)) {
2508 if (starts_with(arg, "--show-notes=") &&
2509 revs->notes_opt.use_default_notes < 0)
2510 revs->notes_opt.use_default_notes = 1;
2511 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2512 revs->show_notes_given = 1;
2513 } else if (!strcmp(arg, "--no-notes")) {
2514 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2515 revs->show_notes_given = 1;
2516 } else if (!strcmp(arg, "--standard-notes")) {
2517 revs->show_notes_given = 1;
2518 revs->notes_opt.use_default_notes = 1;
2519 } else if (!strcmp(arg, "--no-standard-notes")) {
2520 revs->notes_opt.use_default_notes = 0;
2521 } else if (!strcmp(arg, "--oneline")) {
2522 revs->verbose_header = 1;
2523 get_commit_format("oneline", revs);
2524 revs->pretty_given = 1;
2525 revs->abbrev_commit = 1;
2526 } else if (!strcmp(arg, "--graph")) {
2527 graph_clear(revs->graph);
2528 revs->graph = graph_init(revs);
2529 } else if (!strcmp(arg, "--no-graph")) {
2530 graph_clear(revs->graph);
2531 revs->graph = NULL;
2532 } else if (!strcmp(arg, "--encode-email-headers")) {
2533 revs->encode_email_headers = 1;
2534 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2535 revs->encode_email_headers = 0;
2536 } else if (!strcmp(arg, "--root")) {
2537 revs->show_root_diff = 1;
2538 } else if (!strcmp(arg, "--no-commit-id")) {
2539 revs->no_commit_id = 1;
2540 } else if (!strcmp(arg, "--always")) {
2541 revs->always_show_header = 1;
2542 } else if (!strcmp(arg, "--no-abbrev")) {
2543 revs->abbrev = 0;
2544 } else if (!strcmp(arg, "--abbrev")) {
2545 revs->abbrev = DEFAULT_ABBREV;
2546 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2547 revs->abbrev = strtoul(optarg, NULL, 10);
2548 if (revs->abbrev < MINIMUM_ABBREV)
2549 revs->abbrev = MINIMUM_ABBREV;
2550 else if (revs->abbrev > hexsz)
2551 revs->abbrev = hexsz;
2552 } else if (!strcmp(arg, "--abbrev-commit")) {
2553 revs->abbrev_commit = 1;
2554 revs->abbrev_commit_given = 1;
2555 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2556 revs->abbrev_commit = 0;
2557 } else if (!strcmp(arg, "--full-diff")) {
2558 revs->diff = 1;
2559 revs->full_diff = 1;
2560 } else if (!strcmp(arg, "--show-pulls")) {
2561 revs->show_pulls = 1;
2562 } else if (!strcmp(arg, "--full-history")) {
2563 revs->simplify_history = 0;
2564 } else if (!strcmp(arg, "--relative-date")) {
2565 revs->date_mode.type = DATE_RELATIVE;
2566 revs->date_mode_explicit = 1;
2567 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2568 parse_date_format(optarg, &revs->date_mode);
2569 revs->date_mode_explicit = 1;
2570 return argcount;
2571 } else if (!strcmp(arg, "--log-size")) {
2572 revs->show_log_size = 1;
2573 }
2574 /*
2575 * Grepping the commit log
2576 */
2577 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2578 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2579 return argcount;
2580 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2581 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2582 return argcount;
2583 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2584 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2585 return argcount;
2586 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2587 add_message_grep(revs, optarg);
2588 return argcount;
2589 } else if (!strcmp(arg, "--basic-regexp")) {
2590 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2591 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2592 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2593 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2594 revs->grep_filter.ignore_case = 1;
2595 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2596 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2597 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2598 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2599 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2600 } else if (!strcmp(arg, "--all-match")) {
2601 revs->grep_filter.all_match = 1;
2602 } else if (!strcmp(arg, "--invert-grep")) {
2603 revs->grep_filter.no_body_match = 1;
2604 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2605 if (strcmp(optarg, "none"))
2606 git_log_output_encoding = xstrdup(optarg);
2607 else
2608 git_log_output_encoding = "";
2609 return argcount;
2610 } else if (!strcmp(arg, "--reverse")) {
2611 revs->reverse ^= 1;
2612 } else if (!strcmp(arg, "--children")) {
2613 revs->children.name = "children";
2614 revs->limited = 1;
2615 } else if (!strcmp(arg, "--ignore-missing")) {
2616 revs->ignore_missing = 1;
2617 } else if (opt && opt->allow_exclude_promisor_objects &&
2618 !strcmp(arg, "--exclude-promisor-objects")) {
2619 if (fetch_if_missing)
2620 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2621 revs->exclude_promisor_objects = 1;
2622 } else {
2623 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2624 if (!opts)
2625 unkv[(*unkc)++] = arg;
2626 return opts;
2627 }
2628
2629 return 1;
2630 }
2631
2632 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2633 const struct option *options,
2634 const char * const usagestr[])
2635 {
2636 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2637 &ctx->cpidx, ctx->out, NULL);
2638 if (n <= 0) {
2639 error("unknown option `%s'", ctx->argv[0]);
2640 usage_with_options(usagestr, options);
2641 }
2642 ctx->argv += n;
2643 ctx->argc -= n;
2644 }
2645
2646 void revision_opts_finish(struct rev_info *revs)
2647 {
2648 if (revs->graph && revs->track_linear)
2649 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2650
2651 if (revs->graph) {
2652 revs->topo_order = 1;
2653 revs->rewrite_parents = 1;
2654 }
2655 }
2656
2657 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2658 void *cb_data, const char *term)
2659 {
2660 struct strbuf bisect_refs = STRBUF_INIT;
2661 int status;
2662 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2663 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data);
2664 strbuf_release(&bisect_refs);
2665 return status;
2666 }
2667
2668 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2669 {
2670 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2671 }
2672
2673 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2674 {
2675 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2676 }
2677
2678 static int handle_revision_pseudo_opt(struct rev_info *revs,
2679 const char **argv, int *flags)
2680 {
2681 const char *arg = argv[0];
2682 const char *optarg;
2683 struct ref_store *refs;
2684 int argcount;
2685
2686 if (revs->repo != the_repository) {
2687 /*
2688 * We need some something like get_submodule_worktrees()
2689 * before we can go through all worktrees of a submodule,
2690 * .e.g with adding all HEADs from --all, which is not
2691 * supported right now, so stick to single worktree.
2692 */
2693 if (!revs->single_worktree)
2694 BUG("--single-worktree cannot be used together with submodule");
2695 }
2696 refs = get_main_ref_store(revs->repo);
2697
2698 /*
2699 * NOTE!
2700 *
2701 * Commands like "git shortlog" will not accept the options below
2702 * unless parse_revision_opt queues them (as opposed to erroring
2703 * out).
2704 *
2705 * When implementing your new pseudo-option, remember to
2706 * register it in the list at the top of handle_revision_opt.
2707 */
2708 if (!strcmp(arg, "--all")) {
2709 handle_refs(refs, revs, *flags, refs_for_each_ref);
2710 handle_refs(refs, revs, *flags, refs_head_ref);
2711 if (!revs->single_worktree) {
2712 struct all_refs_cb cb;
2713
2714 init_all_refs_cb(&cb, revs, *flags);
2715 other_head_refs(handle_one_ref, &cb);
2716 }
2717 clear_ref_exclusions(&revs->ref_excludes);
2718 } else if (!strcmp(arg, "--branches")) {
2719 if (revs->ref_excludes.hidden_refs_configured)
2720 return error(_("--exclude-hidden cannot be used together with --branches"));
2721 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2722 clear_ref_exclusions(&revs->ref_excludes);
2723 } else if (!strcmp(arg, "--bisect")) {
2724 read_bisect_terms(&term_bad, &term_good);
2725 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2726 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2727 for_each_good_bisect_ref);
2728 revs->bisect = 1;
2729 } else if (!strcmp(arg, "--tags")) {
2730 if (revs->ref_excludes.hidden_refs_configured)
2731 return error(_("--exclude-hidden cannot be used together with --tags"));
2732 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2733 clear_ref_exclusions(&revs->ref_excludes);
2734 } else if (!strcmp(arg, "--remotes")) {
2735 if (revs->ref_excludes.hidden_refs_configured)
2736 return error(_("--exclude-hidden cannot be used together with --remotes"));
2737 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2738 clear_ref_exclusions(&revs->ref_excludes);
2739 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2740 struct all_refs_cb cb;
2741 init_all_refs_cb(&cb, revs, *flags);
2742 for_each_glob_ref(handle_one_ref, optarg, &cb);
2743 clear_ref_exclusions(&revs->ref_excludes);
2744 return argcount;
2745 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2746 add_ref_exclusion(&revs->ref_excludes, optarg);
2747 return argcount;
2748 } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2749 exclude_hidden_refs(&revs->ref_excludes, optarg);
2750 return argcount;
2751 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2752 struct all_refs_cb cb;
2753 if (revs->ref_excludes.hidden_refs_configured)
2754 return error(_("--exclude-hidden cannot be used together with --branches"));
2755 init_all_refs_cb(&cb, revs, *flags);
2756 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2757 clear_ref_exclusions(&revs->ref_excludes);
2758 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2759 struct all_refs_cb cb;
2760 if (revs->ref_excludes.hidden_refs_configured)
2761 return error(_("--exclude-hidden cannot be used together with --tags"));
2762 init_all_refs_cb(&cb, revs, *flags);
2763 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2764 clear_ref_exclusions(&revs->ref_excludes);
2765 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2766 struct all_refs_cb cb;
2767 if (revs->ref_excludes.hidden_refs_configured)
2768 return error(_("--exclude-hidden cannot be used together with --remotes"));
2769 init_all_refs_cb(&cb, revs, *flags);
2770 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2771 clear_ref_exclusions(&revs->ref_excludes);
2772 } else if (!strcmp(arg, "--reflog")) {
2773 add_reflogs_to_pending(revs, *flags);
2774 } else if (!strcmp(arg, "--indexed-objects")) {
2775 add_index_objects_to_pending(revs, *flags);
2776 } else if (!strcmp(arg, "--alternate-refs")) {
2777 add_alternate_refs_to_pending(revs, *flags);
2778 } else if (!strcmp(arg, "--not")) {
2779 *flags ^= UNINTERESTING | BOTTOM;
2780 } else if (!strcmp(arg, "--no-walk")) {
2781 revs->no_walk = 1;
2782 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2783 /*
2784 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2785 * not allowed, since the argument is optional.
2786 */
2787 revs->no_walk = 1;
2788 if (!strcmp(optarg, "sorted"))
2789 revs->unsorted_input = 0;
2790 else if (!strcmp(optarg, "unsorted"))
2791 revs->unsorted_input = 1;
2792 else
2793 return error("invalid argument to --no-walk");
2794 } else if (!strcmp(arg, "--do-walk")) {
2795 revs->no_walk = 0;
2796 } else if (!strcmp(arg, "--single-worktree")) {
2797 revs->single_worktree = 1;
2798 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2799 parse_list_objects_filter(&revs->filter, arg);
2800 } else if (!strcmp(arg, ("--no-filter"))) {
2801 list_objects_filter_set_no_filter(&revs->filter);
2802 } else {
2803 return 0;
2804 }
2805
2806 return 1;
2807 }
2808
2809 static void NORETURN diagnose_missing_default(const char *def)
2810 {
2811 int flags;
2812 const char *refname;
2813
2814 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2815 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2816 die(_("your current branch appears to be broken"));
2817
2818 skip_prefix(refname, "refs/heads/", &refname);
2819 die(_("your current branch '%s' does not have any commits yet"),
2820 refname);
2821 }
2822
2823 /*
2824 * Parse revision information, filling in the "rev_info" structure,
2825 * and removing the used arguments from the argument list.
2826 *
2827 * Returns the number of arguments left that weren't recognized
2828 * (which are also moved to the head of the argument list)
2829 */
2830 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2831 {
2832 int i, flags, left, seen_dashdash, revarg_opt;
2833 struct strvec prune_data = STRVEC_INIT;
2834 int seen_end_of_options = 0;
2835
2836 /* First, search for "--" */
2837 if (opt && opt->assume_dashdash) {
2838 seen_dashdash = 1;
2839 } else {
2840 seen_dashdash = 0;
2841 for (i = 1; i < argc; i++) {
2842 const char *arg = argv[i];
2843 if (strcmp(arg, "--"))
2844 continue;
2845 if (opt && opt->free_removed_argv_elements)
2846 free((char *)argv[i]);
2847 argv[i] = NULL;
2848 argc = i;
2849 if (argv[i + 1])
2850 strvec_pushv(&prune_data, argv + i + 1);
2851 seen_dashdash = 1;
2852 break;
2853 }
2854 }
2855
2856 /* Second, deal with arguments and options */
2857 flags = 0;
2858 revarg_opt = opt ? opt->revarg_opt : 0;
2859 if (seen_dashdash)
2860 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2861 for (left = i = 1; i < argc; i++) {
2862 const char *arg = argv[i];
2863 if (!seen_end_of_options && *arg == '-') {
2864 int opts;
2865
2866 opts = handle_revision_pseudo_opt(
2867 revs, argv + i,
2868 &flags);
2869 if (opts > 0) {
2870 i += opts - 1;
2871 continue;
2872 }
2873
2874 if (!strcmp(arg, "--stdin")) {
2875 if (revs->disable_stdin) {
2876 argv[left++] = arg;
2877 continue;
2878 }
2879 if (revs->read_from_stdin++)
2880 die("--stdin given twice?");
2881 read_revisions_from_stdin(revs, &prune_data);
2882 continue;
2883 }
2884
2885 if (!strcmp(arg, "--end-of-options")) {
2886 seen_end_of_options = 1;
2887 continue;
2888 }
2889
2890 opts = handle_revision_opt(revs, argc - i, argv + i,
2891 &left, argv, opt);
2892 if (opts > 0) {
2893 i += opts - 1;
2894 continue;
2895 }
2896 if (opts < 0)
2897 exit(128);
2898 continue;
2899 }
2900
2901
2902 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2903 int j;
2904 if (seen_dashdash || *arg == '^')
2905 die("bad revision '%s'", arg);
2906
2907 /* If we didn't have a "--":
2908 * (1) all filenames must exist;
2909 * (2) all rev-args must not be interpretable
2910 * as a valid filename.
2911 * but the latter we have checked in the main loop.
2912 */
2913 for (j = i; j < argc; j++)
2914 verify_filename(revs->prefix, argv[j], j == i);
2915
2916 strvec_pushv(&prune_data, argv + i);
2917 break;
2918 }
2919 }
2920 revision_opts_finish(revs);
2921
2922 if (prune_data.nr) {
2923 /*
2924 * If we need to introduce the magic "a lone ':' means no
2925 * pathspec whatsoever", here is the place to do so.
2926 *
2927 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2928 * prune_data.nr = 0;
2929 * prune_data.alloc = 0;
2930 * free(prune_data.path);
2931 * prune_data.path = NULL;
2932 * } else {
2933 * terminate prune_data.alloc with NULL and
2934 * call init_pathspec() to set revs->prune_data here.
2935 * }
2936 */
2937 parse_pathspec(&revs->prune_data, 0, 0,
2938 revs->prefix, prune_data.v);
2939 }
2940 strvec_clear(&prune_data);
2941
2942 if (!revs->def)
2943 revs->def = opt ? opt->def : NULL;
2944 if (opt && opt->tweak)
2945 opt->tweak(revs, opt);
2946 if (revs->show_merge)
2947 prepare_show_merge(revs);
2948 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2949 struct object_id oid;
2950 struct object *object;
2951 struct object_context oc;
2952 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2953 diagnose_missing_default(revs->def);
2954 object = get_reference(revs, revs->def, &oid, 0);
2955 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2956 }
2957
2958 /* Did the user ask for any diff output? Run the diff! */
2959 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2960 revs->diff = 1;
2961
2962 /* Pickaxe, diff-filter and rename following need diffs */
2963 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2964 revs->diffopt.filter ||
2965 revs->diffopt.flags.follow_renames)
2966 revs->diff = 1;
2967
2968 if (revs->diffopt.objfind)
2969 revs->simplify_history = 0;
2970
2971 if (revs->line_level_traverse) {
2972 if (want_ancestry(revs))
2973 revs->limited = 1;
2974 revs->topo_order = 1;
2975 }
2976
2977 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2978 revs->limited = 1;
2979
2980 if (revs->prune_data.nr) {
2981 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2982 /* Can't prune commits with rename following: the paths change.. */
2983 if (!revs->diffopt.flags.follow_renames)
2984 revs->prune = 1;
2985 if (!revs->full_diff)
2986 copy_pathspec(&revs->diffopt.pathspec,
2987 &revs->prune_data);
2988 }
2989
2990 diff_merges_setup_revs(revs);
2991
2992 revs->diffopt.abbrev = revs->abbrev;
2993
2994 diff_setup_done(&revs->diffopt);
2995
2996 if (!is_encoding_utf8(get_log_output_encoding()))
2997 revs->grep_filter.ignore_locale = 1;
2998 compile_grep_patterns(&revs->grep_filter);
2999
3000 if (revs->reverse && revs->reflog_info)
3001 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
3002 if (revs->reflog_info && revs->limited)
3003 die("cannot combine --walk-reflogs with history-limiting options");
3004 if (revs->rewrite_parents && revs->children.name)
3005 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3006 if (revs->filter.choice && !revs->blob_objects)
3007 die(_("object filtering requires --objects"));
3008
3009 /*
3010 * Limitations on the graph functionality
3011 */
3012 if (revs->reverse && revs->graph)
3013 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
3014
3015 if (revs->reflog_info && revs->graph)
3016 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
3017 if (revs->no_walk && revs->graph)
3018 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3019 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
3020 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3021
3022 if (revs->line_level_traverse &&
3023 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
3024 die(_("-L does not yet support diff formats besides -p and -s"));
3025
3026 if (revs->expand_tabs_in_log < 0)
3027 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
3028
3029 return left;
3030 }
3031
3032 static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3033 {
3034 unsigned int i;
3035
3036 for (i = 0; i < cmdline->nr; i++)
3037 free((char *)cmdline->rev[i].name);
3038 free(cmdline->rev);
3039 }
3040
3041 static void release_revisions_mailmap(struct string_list *mailmap)
3042 {
3043 if (!mailmap)
3044 return;
3045 clear_mailmap(mailmap);
3046 free(mailmap);
3047 }
3048
3049 static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3050
3051 void release_revisions(struct rev_info *revs)
3052 {
3053 free_commit_list(revs->commits);
3054 free_commit_list(revs->ancestry_path_bottoms);
3055 object_array_clear(&revs->pending);
3056 object_array_clear(&revs->boundary_commits);
3057 release_revisions_cmdline(&revs->cmdline);
3058 list_objects_filter_release(&revs->filter);
3059 clear_pathspec(&revs->prune_data);
3060 date_mode_release(&revs->date_mode);
3061 release_revisions_mailmap(revs->mailmap);
3062 free_grep_patterns(&revs->grep_filter);
3063 graph_clear(revs->graph);
3064 /* TODO (need to handle "no_free"): diff_free(&revs->diffopt) */
3065 diff_free(&revs->pruning);
3066 reflog_walk_info_release(revs->reflog_info);
3067 release_revisions_topo_walk_info(revs->topo_walk_info);
3068 }
3069
3070 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3071 {
3072 struct commit_list *l = xcalloc(1, sizeof(*l));
3073
3074 l->item = child;
3075 l->next = add_decoration(&revs->children, &parent->object, l);
3076 }
3077
3078 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3079 {
3080 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3081 struct commit_list **pp, *p;
3082 int surviving_parents;
3083
3084 /* Examine existing parents while marking ones we have seen... */
3085 pp = &commit->parents;
3086 surviving_parents = 0;
3087 while ((p = *pp) != NULL) {
3088 struct commit *parent = p->item;
3089 if (parent->object.flags & TMP_MARK) {
3090 *pp = p->next;
3091 if (ts)
3092 compact_treesame(revs, commit, surviving_parents);
3093 continue;
3094 }
3095 parent->object.flags |= TMP_MARK;
3096 surviving_parents++;
3097 pp = &p->next;
3098 }
3099 /* clear the temporary mark */
3100 for (p = commit->parents; p; p = p->next) {
3101 p->item->object.flags &= ~TMP_MARK;
3102 }
3103 /* no update_treesame() - removing duplicates can't affect TREESAME */
3104 return surviving_parents;
3105 }
3106
3107 struct merge_simplify_state {
3108 struct commit *simplified;
3109 };
3110
3111 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3112 {
3113 struct merge_simplify_state *st;
3114
3115 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3116 if (!st) {
3117 CALLOC_ARRAY(st, 1);
3118 add_decoration(&revs->merge_simplification, &commit->object, st);
3119 }
3120 return st;
3121 }
3122
3123 static int mark_redundant_parents(struct commit *commit)
3124 {
3125 struct commit_list *h = reduce_heads(commit->parents);
3126 int i = 0, marked = 0;
3127 struct commit_list *po, *pn;
3128
3129 /* Want these for sanity-checking only */
3130 int orig_cnt = commit_list_count(commit->parents);
3131 int cnt = commit_list_count(h);
3132
3133 /*
3134 * Not ready to remove items yet, just mark them for now, based
3135 * on the output of reduce_heads(). reduce_heads outputs the reduced
3136 * set in its original order, so this isn't too hard.
3137 */
3138 po = commit->parents;
3139 pn = h;
3140 while (po) {
3141 if (pn && po->item == pn->item) {
3142 pn = pn->next;
3143 i++;
3144 } else {
3145 po->item->object.flags |= TMP_MARK;
3146 marked++;
3147 }
3148 po=po->next;
3149 }
3150
3151 if (i != cnt || cnt+marked != orig_cnt)
3152 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3153
3154 free_commit_list(h);
3155
3156 return marked;
3157 }
3158
3159 static int mark_treesame_root_parents(struct commit *commit)
3160 {
3161 struct commit_list *p;
3162 int marked = 0;
3163
3164 for (p = commit->parents; p; p = p->next) {
3165 struct commit *parent = p->item;
3166 if (!parent->parents && (parent->object.flags & TREESAME)) {
3167 parent->object.flags |= TMP_MARK;
3168 marked++;
3169 }
3170 }
3171
3172 return marked;
3173 }
3174
3175 /*
3176 * Awkward naming - this means one parent we are TREESAME to.
3177 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3178 * empty tree). Better name suggestions?
3179 */
3180 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3181 {
3182 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3183 struct commit *unmarked = NULL, *marked = NULL;
3184 struct commit_list *p;
3185 unsigned n;
3186
3187 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3188 if (ts->treesame[n]) {
3189 if (p->item->object.flags & TMP_MARK) {
3190 if (!marked)
3191 marked = p->item;
3192 } else {
3193 if (!unmarked) {
3194 unmarked = p->item;
3195 break;
3196 }
3197 }
3198 }
3199 }
3200
3201 /*
3202 * If we are TREESAME to a marked-for-deletion parent, but not to any
3203 * unmarked parents, unmark the first TREESAME parent. This is the
3204 * parent that the default simplify_history==1 scan would have followed,
3205 * and it doesn't make sense to omit that path when asking for a
3206 * simplified full history. Retaining it improves the chances of
3207 * understanding odd missed merges that took an old version of a file.
3208 *
3209 * Example:
3210 *
3211 * I--------*X A modified the file, but mainline merge X used
3212 * \ / "-s ours", so took the version from I. X is
3213 * `-*A--' TREESAME to I and !TREESAME to A.
3214 *
3215 * Default log from X would produce "I". Without this check,
3216 * --full-history --simplify-merges would produce "I-A-X", showing
3217 * the merge commit X and that it changed A, but not making clear that
3218 * it had just taken the I version. With this check, the topology above
3219 * is retained.
3220 *
3221 * Note that it is possible that the simplification chooses a different
3222 * TREESAME parent from the default, in which case this test doesn't
3223 * activate, and we _do_ drop the default parent. Example:
3224 *
3225 * I------X A modified the file, but it was reverted in B,
3226 * \ / meaning mainline merge X is TREESAME to both
3227 * *A-*B parents.
3228 *
3229 * Default log would produce "I" by following the first parent;
3230 * --full-history --simplify-merges will produce "I-A-B". But this is a
3231 * reasonable result - it presents a logical full history leading from
3232 * I to X, and X is not an important merge.
3233 */
3234 if (!unmarked && marked) {
3235 marked->object.flags &= ~TMP_MARK;
3236 return 1;
3237 }
3238
3239 return 0;
3240 }
3241
3242 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3243 {
3244 struct commit_list **pp, *p;
3245 int nth_parent, removed = 0;
3246
3247 pp = &commit->parents;
3248 nth_parent = 0;
3249 while ((p = *pp) != NULL) {
3250 struct commit *parent = p->item;
3251 if (parent->object.flags & TMP_MARK) {
3252 parent->object.flags &= ~TMP_MARK;
3253 *pp = p->next;
3254 free(p);
3255 removed++;
3256 compact_treesame(revs, commit, nth_parent);
3257 continue;
3258 }
3259 pp = &p->next;
3260 nth_parent++;
3261 }
3262
3263 /* Removing parents can only increase TREESAMEness */
3264 if (removed && !(commit->object.flags & TREESAME))
3265 update_treesame(revs, commit);
3266
3267 return nth_parent;
3268 }
3269
3270 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3271 {
3272 struct commit_list *p;
3273 struct commit *parent;
3274 struct merge_simplify_state *st, *pst;
3275 int cnt;
3276
3277 st = locate_simplify_state(revs, commit);
3278
3279 /*
3280 * Have we handled this one?
3281 */
3282 if (st->simplified)
3283 return tail;
3284
3285 /*
3286 * An UNINTERESTING commit simplifies to itself, so does a
3287 * root commit. We do not rewrite parents of such commit
3288 * anyway.
3289 */
3290 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3291 st->simplified = commit;
3292 return tail;
3293 }
3294
3295 /*
3296 * Do we know what commit all of our parents that matter
3297 * should be rewritten to? Otherwise we are not ready to
3298 * rewrite this one yet.
3299 */
3300 for (cnt = 0, p = commit->parents; p; p = p->next) {
3301 pst = locate_simplify_state(revs, p->item);
3302 if (!pst->simplified) {
3303 tail = &commit_list_insert(p->item, tail)->next;
3304 cnt++;
3305 }
3306 if (revs->first_parent_only)
3307 break;
3308 }
3309 if (cnt) {
3310 tail = &commit_list_insert(commit, tail)->next;
3311 return tail;
3312 }
3313
3314 /*
3315 * Rewrite our list of parents. Note that this cannot
3316 * affect our TREESAME flags in any way - a commit is
3317 * always TREESAME to its simplification.
3318 */
3319 for (p = commit->parents; p; p = p->next) {
3320 pst = locate_simplify_state(revs, p->item);
3321 p->item = pst->simplified;
3322 if (revs->first_parent_only)
3323 break;
3324 }
3325
3326 if (revs->first_parent_only)
3327 cnt = 1;
3328 else
3329 cnt = remove_duplicate_parents(revs, commit);
3330
3331 /*
3332 * It is possible that we are a merge and one side branch
3333 * does not have any commit that touches the given paths;
3334 * in such a case, the immediate parent from that branch
3335 * will be rewritten to be the merge base.
3336 *
3337 * o----X X: the commit we are looking at;
3338 * / / o: a commit that touches the paths;
3339 * ---o----'
3340 *
3341 * Further, a merge of an independent branch that doesn't
3342 * touch the path will reduce to a treesame root parent:
3343 *
3344 * ----o----X X: the commit we are looking at;
3345 * / o: a commit that touches the paths;
3346 * r r: a root commit not touching the paths
3347 *
3348 * Detect and simplify both cases.
3349 */
3350 if (1 < cnt) {
3351 int marked = mark_redundant_parents(commit);
3352 marked += mark_treesame_root_parents(commit);
3353 if (marked)
3354 marked -= leave_one_treesame_to_parent(revs, commit);
3355 if (marked)
3356 cnt = remove_marked_parents(revs, commit);
3357 }
3358
3359 /*
3360 * A commit simplifies to itself if it is a root, if it is
3361 * UNINTERESTING, if it touches the given paths, or if it is a
3362 * merge and its parents don't simplify to one relevant commit
3363 * (the first two cases are already handled at the beginning of
3364 * this function).
3365 *
3366 * Otherwise, it simplifies to what its sole relevant parent
3367 * simplifies to.
3368 */
3369 if (!cnt ||
3370 (commit->object.flags & UNINTERESTING) ||
3371 !(commit->object.flags & TREESAME) ||
3372 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3373 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3374 st->simplified = commit;
3375 else {
3376 pst = locate_simplify_state(revs, parent);
3377 st->simplified = pst->simplified;
3378 }
3379 return tail;
3380 }
3381
3382 static void simplify_merges(struct rev_info *revs)
3383 {
3384 struct commit_list *list, *next;
3385 struct commit_list *yet_to_do, **tail;
3386 struct commit *commit;
3387
3388 if (!revs->prune)
3389 return;
3390
3391 /* feed the list reversed */
3392 yet_to_do = NULL;
3393 for (list = revs->commits; list; list = next) {
3394 commit = list->item;
3395 next = list->next;
3396 /*
3397 * Do not free(list) here yet; the original list
3398 * is used later in this function.
3399 */
3400 commit_list_insert(commit, &yet_to_do);
3401 }
3402 while (yet_to_do) {
3403 list = yet_to_do;
3404 yet_to_do = NULL;
3405 tail = &yet_to_do;
3406 while (list) {
3407 commit = pop_commit(&list);
3408 tail = simplify_one(revs, commit, tail);
3409 }
3410 }
3411
3412 /* clean up the result, removing the simplified ones */
3413 list = revs->commits;
3414 revs->commits = NULL;
3415 tail = &revs->commits;
3416 while (list) {
3417 struct merge_simplify_state *st;
3418
3419 commit = pop_commit(&list);
3420 st = locate_simplify_state(revs, commit);
3421 if (st->simplified == commit)
3422 tail = &commit_list_insert(commit, tail)->next;
3423 }
3424 }
3425
3426 static void set_children(struct rev_info *revs)
3427 {
3428 struct commit_list *l;
3429 for (l = revs->commits; l; l = l->next) {
3430 struct commit *commit = l->item;
3431 struct commit_list *p;
3432
3433 for (p = commit->parents; p; p = p->next)
3434 add_child(revs, p->item, commit);
3435 }
3436 }
3437
3438 void reset_revision_walk(void)
3439 {
3440 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3441 }
3442
3443 static int mark_uninteresting(const struct object_id *oid,
3444 struct packed_git *pack,
3445 uint32_t pos,
3446 void *cb)
3447 {
3448 struct rev_info *revs = cb;
3449 struct object *o = lookup_unknown_object(revs->repo, oid);
3450 o->flags |= UNINTERESTING | SEEN;
3451 return 0;
3452 }
3453
3454 define_commit_slab(indegree_slab, int);
3455 define_commit_slab(author_date_slab, timestamp_t);
3456
3457 struct topo_walk_info {
3458 timestamp_t min_generation;
3459 struct prio_queue explore_queue;
3460 struct prio_queue indegree_queue;
3461 struct prio_queue topo_queue;
3462 struct indegree_slab indegree;
3463 struct author_date_slab author_date;
3464 };
3465
3466 static int topo_walk_atexit_registered;
3467 static unsigned int count_explore_walked;
3468 static unsigned int count_indegree_walked;
3469 static unsigned int count_topo_walked;
3470
3471 static void trace2_topo_walk_statistics_atexit(void)
3472 {
3473 struct json_writer jw = JSON_WRITER_INIT;
3474
3475 jw_object_begin(&jw, 0);
3476 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3477 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3478 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3479 jw_end(&jw);
3480
3481 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3482
3483 jw_release(&jw);
3484 }
3485
3486 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3487 {
3488 if (c->object.flags & flag)
3489 return;
3490
3491 c->object.flags |= flag;
3492 prio_queue_put(q, c);
3493 }
3494
3495 static void explore_walk_step(struct rev_info *revs)
3496 {
3497 struct topo_walk_info *info = revs->topo_walk_info;
3498 struct commit_list *p;
3499 struct commit *c = prio_queue_get(&info->explore_queue);
3500
3501 if (!c)
3502 return;
3503
3504 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3505 return;
3506
3507 count_explore_walked++;
3508
3509 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3510 record_author_date(&info->author_date, c);
3511
3512 if (revs->max_age != -1 && (c->date < revs->max_age))
3513 c->object.flags |= UNINTERESTING;
3514
3515 if (process_parents(revs, c, NULL, NULL) < 0)
3516 return;
3517
3518 if (c->object.flags & UNINTERESTING)
3519 mark_parents_uninteresting(revs, c);
3520
3521 for (p = c->parents; p; p = p->next)
3522 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3523 }
3524
3525 static void explore_to_depth(struct rev_info *revs,
3526 timestamp_t gen_cutoff)
3527 {
3528 struct topo_walk_info *info = revs->topo_walk_info;
3529 struct commit *c;
3530 while ((c = prio_queue_peek(&info->explore_queue)) &&
3531 commit_graph_generation(c) >= gen_cutoff)
3532 explore_walk_step(revs);
3533 }
3534
3535 static void indegree_walk_step(struct rev_info *revs)
3536 {
3537 struct commit_list *p;
3538 struct topo_walk_info *info = revs->topo_walk_info;
3539 struct commit *c = prio_queue_get(&info->indegree_queue);
3540
3541 if (!c)
3542 return;
3543
3544 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3545 return;
3546
3547 count_indegree_walked++;
3548
3549 explore_to_depth(revs, commit_graph_generation(c));
3550
3551 for (p = c->parents; p; p = p->next) {
3552 struct commit *parent = p->item;
3553 int *pi = indegree_slab_at(&info->indegree, parent);
3554
3555 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3556 return;
3557
3558 if (*pi)
3559 (*pi)++;
3560 else
3561 *pi = 2;
3562
3563 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3564
3565 if (revs->first_parent_only)
3566 return;
3567 }
3568 }
3569
3570 static void compute_indegrees_to_depth(struct rev_info *revs,
3571 timestamp_t gen_cutoff)
3572 {
3573 struct topo_walk_info *info = revs->topo_walk_info;
3574 struct commit *c;
3575 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3576 commit_graph_generation(c) >= gen_cutoff)
3577 indegree_walk_step(revs);
3578 }
3579
3580 static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3581 {
3582 if (!info)
3583 return;
3584 clear_prio_queue(&info->explore_queue);
3585 clear_prio_queue(&info->indegree_queue);
3586 clear_prio_queue(&info->topo_queue);
3587 clear_indegree_slab(&info->indegree);
3588 clear_author_date_slab(&info->author_date);
3589 free(info);
3590 }
3591
3592 static void reset_topo_walk(struct rev_info *revs)
3593 {
3594 release_revisions_topo_walk_info(revs->topo_walk_info);
3595 revs->topo_walk_info = NULL;
3596 }
3597
3598 static void init_topo_walk(struct rev_info *revs)
3599 {
3600 struct topo_walk_info *info;
3601 struct commit_list *list;
3602 if (revs->topo_walk_info)
3603 reset_topo_walk(revs);
3604
3605 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3606 info = revs->topo_walk_info;
3607 memset(info, 0, sizeof(struct topo_walk_info));
3608
3609 init_indegree_slab(&info->indegree);
3610 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3611 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3612 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3613
3614 switch (revs->sort_order) {
3615 default: /* REV_SORT_IN_GRAPH_ORDER */
3616 info->topo_queue.compare = NULL;
3617 break;
3618 case REV_SORT_BY_COMMIT_DATE:
3619 info->topo_queue.compare = compare_commits_by_commit_date;
3620 break;
3621 case REV_SORT_BY_AUTHOR_DATE:
3622 init_author_date_slab(&info->author_date);
3623 info->topo_queue.compare = compare_commits_by_author_date;
3624 info->topo_queue.cb_data = &info->author_date;
3625 break;
3626 }
3627
3628 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3629 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3630
3631 info->min_generation = GENERATION_NUMBER_INFINITY;
3632 for (list = revs->commits; list; list = list->next) {
3633 struct commit *c = list->item;
3634 timestamp_t generation;
3635
3636 if (repo_parse_commit_gently(revs->repo, c, 1))
3637 continue;
3638
3639 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3640 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3641
3642 generation = commit_graph_generation(c);
3643 if (generation < info->min_generation)
3644 info->min_generation = generation;
3645
3646 *(indegree_slab_at(&info->indegree, c)) = 1;
3647
3648 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3649 record_author_date(&info->author_date, c);
3650 }
3651 compute_indegrees_to_depth(revs, info->min_generation);
3652
3653 for (list = revs->commits; list; list = list->next) {
3654 struct commit *c = list->item;
3655
3656 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3657 prio_queue_put(&info->topo_queue, c);
3658 }
3659
3660 /*
3661 * This is unfortunate; the initial tips need to be shown
3662 * in the order given from the revision traversal machinery.
3663 */
3664 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3665 prio_queue_reverse(&info->topo_queue);
3666
3667 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3668 atexit(trace2_topo_walk_statistics_atexit);
3669 topo_walk_atexit_registered = 1;
3670 }
3671 }
3672
3673 static struct commit *next_topo_commit(struct rev_info *revs)
3674 {
3675 struct commit *c;
3676 struct topo_walk_info *info = revs->topo_walk_info;
3677
3678 /* pop next off of topo_queue */
3679 c = prio_queue_get(&info->topo_queue);
3680
3681 if (c)
3682 *(indegree_slab_at(&info->indegree, c)) = 0;
3683
3684 return c;
3685 }
3686
3687 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3688 {
3689 struct commit_list *p;
3690 struct topo_walk_info *info = revs->topo_walk_info;
3691 if (process_parents(revs, commit, NULL, NULL) < 0) {
3692 if (!revs->ignore_missing_links)
3693 die("Failed to traverse parents of commit %s",
3694 oid_to_hex(&commit->object.oid));
3695 }
3696
3697 count_topo_walked++;
3698
3699 for (p = commit->parents; p; p = p->next) {
3700 struct commit *parent = p->item;
3701 int *pi;
3702 timestamp_t generation;
3703
3704 if (parent->object.flags & UNINTERESTING)
3705 continue;
3706
3707 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3708 continue;
3709
3710 generation = commit_graph_generation(parent);
3711 if (generation < info->min_generation) {
3712 info->min_generation = generation;
3713 compute_indegrees_to_depth(revs, info->min_generation);
3714 }
3715
3716 pi = indegree_slab_at(&info->indegree, parent);
3717
3718 (*pi)--;
3719 if (*pi == 1)
3720 prio_queue_put(&info->topo_queue, parent);
3721
3722 if (revs->first_parent_only)
3723 return;
3724 }
3725 }
3726
3727 int prepare_revision_walk(struct rev_info *revs)
3728 {
3729 int i;
3730 struct object_array old_pending;
3731 struct commit_list **next = &revs->commits;
3732
3733 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3734 revs->pending.nr = 0;
3735 revs->pending.alloc = 0;
3736 revs->pending.objects = NULL;
3737 for (i = 0; i < old_pending.nr; i++) {
3738 struct object_array_entry *e = old_pending.objects + i;
3739 struct commit *commit = handle_commit(revs, e);
3740 if (commit) {
3741 if (!(commit->object.flags & SEEN)) {
3742 commit->object.flags |= SEEN;
3743 next = commit_list_append(commit, next);
3744 }
3745 }
3746 }
3747 object_array_clear(&old_pending);
3748
3749 /* Signal whether we need per-parent treesame decoration */
3750 if (revs->simplify_merges ||
3751 (revs->limited && limiting_can_increase_treesame(revs)))
3752 revs->treesame.name = "treesame";
3753
3754 if (revs->exclude_promisor_objects) {
3755 for_each_packed_object(mark_uninteresting, revs,
3756 FOR_EACH_OBJECT_PROMISOR_ONLY);
3757 }
3758
3759 if (!revs->reflog_info)
3760 prepare_to_use_bloom_filter(revs);
3761 if (!revs->unsorted_input)
3762 commit_list_sort_by_date(&revs->commits);
3763 if (revs->no_walk)
3764 return 0;
3765 if (revs->limited) {
3766 if (limit_list(revs) < 0)
3767 return -1;
3768 if (revs->topo_order)
3769 sort_in_topological_order(&revs->commits, revs->sort_order);
3770 } else if (revs->topo_order)
3771 init_topo_walk(revs);
3772 if (revs->line_level_traverse && want_ancestry(revs))
3773 /*
3774 * At the moment we can only do line-level log with parent
3775 * rewriting by performing this expensive pre-filtering step.
3776 * If parent rewriting is not requested, then we rather
3777 * perform the line-level log filtering during the regular
3778 * history traversal.
3779 */
3780 line_log_filter(revs);
3781 if (revs->simplify_merges)
3782 simplify_merges(revs);
3783 if (revs->children.name)
3784 set_children(revs);
3785
3786 return 0;
3787 }
3788
3789 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3790 struct commit **pp,
3791 struct prio_queue *queue)
3792 {
3793 for (;;) {
3794 struct commit *p = *pp;
3795 if (!revs->limited)
3796 if (process_parents(revs, p, NULL, queue) < 0)
3797 return rewrite_one_error;
3798 if (p->object.flags & UNINTERESTING)
3799 return rewrite_one_ok;
3800 if (!(p->object.flags & TREESAME))
3801 return rewrite_one_ok;
3802 if (!p->parents)
3803 return rewrite_one_noparents;
3804 if (!(p = one_relevant_parent(revs, p->parents)))
3805 return rewrite_one_ok;
3806 *pp = p;
3807 }
3808 }
3809
3810 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3811 {
3812 while (q->nr) {
3813 struct commit *item = prio_queue_peek(q);
3814 struct commit_list *p = *list;
3815
3816 if (p && p->item->date >= item->date)
3817 list = &p->next;
3818 else {
3819 p = commit_list_insert(item, list);
3820 list = &p->next; /* skip newly added item */
3821 prio_queue_get(q); /* pop item */
3822 }
3823 }
3824 }
3825
3826 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3827 {
3828 struct prio_queue queue = { compare_commits_by_commit_date };
3829 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3830 merge_queue_into_list(&queue, &revs->commits);
3831 clear_prio_queue(&queue);
3832 return ret;
3833 }
3834
3835 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3836 rewrite_parent_fn_t rewrite_parent)
3837 {
3838 struct commit_list **pp = &commit->parents;
3839 while (*pp) {
3840 struct commit_list *parent = *pp;
3841 switch (rewrite_parent(revs, &parent->item)) {
3842 case rewrite_one_ok:
3843 break;
3844 case rewrite_one_noparents:
3845 *pp = parent->next;
3846 continue;
3847 case rewrite_one_error:
3848 return -1;
3849 }
3850 pp = &parent->next;
3851 }
3852 remove_duplicate_parents(revs, commit);
3853 return 0;
3854 }
3855
3856 static int commit_match(struct commit *commit, struct rev_info *opt)
3857 {
3858 int retval;
3859 const char *encoding;
3860 const char *message;
3861 struct strbuf buf = STRBUF_INIT;
3862
3863 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3864 return 1;
3865
3866 /* Prepend "fake" headers as needed */
3867 if (opt->grep_filter.use_reflog_filter) {
3868 strbuf_addstr(&buf, "reflog ");
3869 get_reflog_message(&buf, opt->reflog_info);
3870 strbuf_addch(&buf, '\n');
3871 }
3872
3873 /*
3874 * We grep in the user's output encoding, under the assumption that it
3875 * is the encoding they are most likely to write their grep pattern
3876 * for. In addition, it means we will match the "notes" encoding below,
3877 * so we will not end up with a buffer that has two different encodings
3878 * in it.
3879 */
3880 encoding = get_log_output_encoding();
3881 message = logmsg_reencode(commit, NULL, encoding);
3882
3883 /* Copy the commit to temporary if we are using "fake" headers */
3884 if (buf.len)
3885 strbuf_addstr(&buf, message);
3886
3887 if (opt->grep_filter.header_list && opt->mailmap) {
3888 const char *commit_headers[] = { "author ", "committer ", NULL };
3889
3890 if (!buf.len)
3891 strbuf_addstr(&buf, message);
3892
3893 apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
3894 }
3895
3896 /* Append "fake" message parts as needed */
3897 if (opt->show_notes) {
3898 if (!buf.len)
3899 strbuf_addstr(&buf, message);
3900 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3901 }
3902
3903 /*
3904 * Find either in the original commit message, or in the temporary.
3905 * Note that we cast away the constness of "message" here. It is
3906 * const because it may come from the cached commit buffer. That's OK,
3907 * because we know that it is modifiable heap memory, and that while
3908 * grep_buffer may modify it for speed, it will restore any
3909 * changes before returning.
3910 */
3911 if (buf.len)
3912 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3913 else
3914 retval = grep_buffer(&opt->grep_filter,
3915 (char *)message, strlen(message));
3916 strbuf_release(&buf);
3917 unuse_commit_buffer(commit, message);
3918 return retval;
3919 }
3920
3921 static inline int want_ancestry(const struct rev_info *revs)
3922 {
3923 return (revs->rewrite_parents || revs->children.name);
3924 }
3925
3926 /*
3927 * Return a timestamp to be used for --since/--until comparisons for this
3928 * commit, based on the revision options.
3929 */
3930 static timestamp_t comparison_date(const struct rev_info *revs,
3931 struct commit *commit)
3932 {
3933 return revs->reflog_info ?
3934 get_reflog_timestamp(revs->reflog_info) :
3935 commit->date;
3936 }
3937
3938 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3939 {
3940 if (commit->object.flags & SHOWN)
3941 return commit_ignore;
3942 if (revs->unpacked && has_object_pack(&commit->object.oid))
3943 return commit_ignore;
3944 if (revs->no_kept_objects) {
3945 if (has_object_kept_pack(&commit->object.oid,
3946 revs->keep_pack_cache_flags))
3947 return commit_ignore;
3948 }
3949 if (commit->object.flags & UNINTERESTING)
3950 return commit_ignore;
3951 if (revs->line_level_traverse && !want_ancestry(revs)) {
3952 /*
3953 * In case of line-level log with parent rewriting
3954 * prepare_revision_walk() already took care of all line-level
3955 * log filtering, and there is nothing left to do here.
3956 *
3957 * If parent rewriting was not requested, then this is the
3958 * place to perform the line-level log filtering. Notably,
3959 * this check, though expensive, must come before the other,
3960 * cheaper filtering conditions, because the tracked line
3961 * ranges must be adjusted even when the commit will end up
3962 * being ignored based on other conditions.
3963 */
3964 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3965 return commit_ignore;
3966 }
3967 if (revs->min_age != -1 &&
3968 comparison_date(revs, commit) > revs->min_age)
3969 return commit_ignore;
3970 if (revs->max_age_as_filter != -1 &&
3971 comparison_date(revs, commit) < revs->max_age_as_filter)
3972 return commit_ignore;
3973 if (revs->min_parents || (revs->max_parents >= 0)) {
3974 int n = commit_list_count(commit->parents);
3975 if ((n < revs->min_parents) ||
3976 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3977 return commit_ignore;
3978 }
3979 if (!commit_match(commit, revs))
3980 return commit_ignore;
3981 if (revs->prune && revs->dense) {
3982 /* Commit without changes? */
3983 if (commit->object.flags & TREESAME) {
3984 int n;
3985 struct commit_list *p;
3986 /* drop merges unless we want parenthood */
3987 if (!want_ancestry(revs))
3988 return commit_ignore;
3989
3990 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3991 return commit_show;
3992
3993 /*
3994 * If we want ancestry, then need to keep any merges
3995 * between relevant commits to tie together topology.
3996 * For consistency with TREESAME and simplification
3997 * use "relevant" here rather than just INTERESTING,
3998 * to treat bottom commit(s) as part of the topology.
3999 */
4000 for (n = 0, p = commit->parents; p; p = p->next)
4001 if (relevant_commit(p->item))
4002 if (++n >= 2)
4003 return commit_show;
4004 return commit_ignore;
4005 }
4006 }
4007 return commit_show;
4008 }
4009
4010 define_commit_slab(saved_parents, struct commit_list *);
4011
4012 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4013
4014 /*
4015 * You may only call save_parents() once per commit (this is checked
4016 * for non-root commits).
4017 */
4018 static void save_parents(struct rev_info *revs, struct commit *commit)
4019 {
4020 struct commit_list **pp;
4021
4022 if (!revs->saved_parents_slab) {
4023 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4024 init_saved_parents(revs->saved_parents_slab);
4025 }
4026
4027 pp = saved_parents_at(revs->saved_parents_slab, commit);
4028
4029 /*
4030 * When walking with reflogs, we may visit the same commit
4031 * several times: once for each appearance in the reflog.
4032 *
4033 * In this case, save_parents() will be called multiple times.
4034 * We want to keep only the first set of parents. We need to
4035 * store a sentinel value for an empty (i.e., NULL) parent
4036 * list to distinguish it from a not-yet-saved list, however.
4037 */
4038 if (*pp)
4039 return;
4040 if (commit->parents)
4041 *pp = copy_commit_list(commit->parents);
4042 else
4043 *pp = EMPTY_PARENT_LIST;
4044 }
4045
4046 static void free_saved_parents(struct rev_info *revs)
4047 {
4048 if (revs->saved_parents_slab)
4049 clear_saved_parents(revs->saved_parents_slab);
4050 }
4051
4052 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4053 {
4054 struct commit_list *parents;
4055
4056 if (!revs->saved_parents_slab)
4057 return commit->parents;
4058
4059 parents = *saved_parents_at(revs->saved_parents_slab, commit);
4060 if (parents == EMPTY_PARENT_LIST)
4061 return NULL;
4062 return parents;
4063 }
4064
4065 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4066 {
4067 enum commit_action action = get_commit_action(revs, commit);
4068
4069 if (action == commit_show &&
4070 revs->prune && revs->dense && want_ancestry(revs)) {
4071 /*
4072 * --full-diff on simplified parents is no good: it
4073 * will show spurious changes from the commits that
4074 * were elided. So we save the parents on the side
4075 * when --full-diff is in effect.
4076 */
4077 if (revs->full_diff)
4078 save_parents(revs, commit);
4079 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4080 return commit_error;
4081 }
4082 return action;
4083 }
4084
4085 static void track_linear(struct rev_info *revs, struct commit *commit)
4086 {
4087 if (revs->track_first_time) {
4088 revs->linear = 1;
4089 revs->track_first_time = 0;
4090 } else {
4091 struct commit_list *p;
4092 for (p = revs->previous_parents; p; p = p->next)
4093 if (p->item == NULL || /* first commit */
4094 oideq(&p->item->object.oid, &commit->object.oid))
4095 break;
4096 revs->linear = p != NULL;
4097 }
4098 if (revs->reverse) {
4099 if (revs->linear)
4100 commit->object.flags |= TRACK_LINEAR;
4101 }
4102 free_commit_list(revs->previous_parents);
4103 revs->previous_parents = copy_commit_list(commit->parents);
4104 }
4105
4106 static struct commit *get_revision_1(struct rev_info *revs)
4107 {
4108 while (1) {
4109 struct commit *commit;
4110
4111 if (revs->reflog_info)
4112 commit = next_reflog_entry(revs->reflog_info);
4113 else if (revs->topo_walk_info)
4114 commit = next_topo_commit(revs);
4115 else
4116 commit = pop_commit(&revs->commits);
4117
4118 if (!commit)
4119 return NULL;
4120
4121 if (revs->reflog_info)
4122 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4123
4124 /*
4125 * If we haven't done the list limiting, we need to look at
4126 * the parents here. We also need to do the date-based limiting
4127 * that we'd otherwise have done in limit_list().
4128 */
4129 if (!revs->limited) {
4130 if (revs->max_age != -1 &&
4131 comparison_date(revs, commit) < revs->max_age)
4132 continue;
4133
4134 if (revs->reflog_info)
4135 try_to_simplify_commit(revs, commit);
4136 else if (revs->topo_walk_info)
4137 expand_topo_walk(revs, commit);
4138 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4139 if (!revs->ignore_missing_links)
4140 die("Failed to traverse parents of commit %s",
4141 oid_to_hex(&commit->object.oid));
4142 }
4143 }
4144
4145 switch (simplify_commit(revs, commit)) {
4146 case commit_ignore:
4147 continue;
4148 case commit_error:
4149 die("Failed to simplify parents of commit %s",
4150 oid_to_hex(&commit->object.oid));
4151 default:
4152 if (revs->track_linear)
4153 track_linear(revs, commit);
4154 return commit;
4155 }
4156 }
4157 }
4158
4159 /*
4160 * Return true for entries that have not yet been shown. (This is an
4161 * object_array_each_func_t.)
4162 */
4163 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
4164 {
4165 return !(entry->item->flags & SHOWN);
4166 }
4167
4168 /*
4169 * If array is on the verge of a realloc, garbage-collect any entries
4170 * that have already been shown to try to free up some space.
4171 */
4172 static void gc_boundary(struct object_array *array)
4173 {
4174 if (array->nr == array->alloc)
4175 object_array_filter(array, entry_unshown, NULL);
4176 }
4177
4178 static void create_boundary_commit_list(struct rev_info *revs)
4179 {
4180 unsigned i;
4181 struct commit *c;
4182 struct object_array *array = &revs->boundary_commits;
4183 struct object_array_entry *objects = array->objects;
4184
4185 /*
4186 * If revs->commits is non-NULL at this point, an error occurred in
4187 * get_revision_1(). Ignore the error and continue printing the
4188 * boundary commits anyway. (This is what the code has always
4189 * done.)
4190 */
4191 free_commit_list(revs->commits);
4192 revs->commits = NULL;
4193
4194 /*
4195 * Put all of the actual boundary commits from revs->boundary_commits
4196 * into revs->commits
4197 */
4198 for (i = 0; i < array->nr; i++) {
4199 c = (struct commit *)(objects[i].item);
4200 if (!c)
4201 continue;
4202 if (!(c->object.flags & CHILD_SHOWN))
4203 continue;
4204 if (c->object.flags & (SHOWN | BOUNDARY))
4205 continue;
4206 c->object.flags |= BOUNDARY;
4207 commit_list_insert(c, &revs->commits);
4208 }
4209
4210 /*
4211 * If revs->topo_order is set, sort the boundary commits
4212 * in topological order
4213 */
4214 sort_in_topological_order(&revs->commits, revs->sort_order);
4215 }
4216
4217 static struct commit *get_revision_internal(struct rev_info *revs)
4218 {
4219 struct commit *c = NULL;
4220 struct commit_list *l;
4221
4222 if (revs->boundary == 2) {
4223 /*
4224 * All of the normal commits have already been returned,
4225 * and we are now returning boundary commits.
4226 * create_boundary_commit_list() has populated
4227 * revs->commits with the remaining commits to return.
4228 */
4229 c = pop_commit(&revs->commits);
4230 if (c)
4231 c->object.flags |= SHOWN;
4232 return c;
4233 }
4234
4235 /*
4236 * If our max_count counter has reached zero, then we are done. We
4237 * don't simply return NULL because we still might need to show
4238 * boundary commits. But we want to avoid calling get_revision_1, which
4239 * might do a considerable amount of work finding the next commit only
4240 * for us to throw it away.
4241 *
4242 * If it is non-zero, then either we don't have a max_count at all
4243 * (-1), or it is still counting, in which case we decrement.
4244 */
4245 if (revs->max_count) {
4246 c = get_revision_1(revs);
4247 if (c) {
4248 while (revs->skip_count > 0) {
4249 revs->skip_count--;
4250 c = get_revision_1(revs);
4251 if (!c)
4252 break;
4253 }
4254 }
4255
4256 if (revs->max_count > 0)
4257 revs->max_count--;
4258 }
4259
4260 if (c)
4261 c->object.flags |= SHOWN;
4262
4263 if (!revs->boundary)
4264 return c;
4265
4266 if (!c) {
4267 /*
4268 * get_revision_1() runs out the commits, and
4269 * we are done computing the boundaries.
4270 * switch to boundary commits output mode.
4271 */
4272 revs->boundary = 2;
4273
4274 /*
4275 * Update revs->commits to contain the list of
4276 * boundary commits.
4277 */
4278 create_boundary_commit_list(revs);
4279
4280 return get_revision_internal(revs);
4281 }
4282
4283 /*
4284 * boundary commits are the commits that are parents of the
4285 * ones we got from get_revision_1() but they themselves are
4286 * not returned from get_revision_1(). Before returning
4287 * 'c', we need to mark its parents that they could be boundaries.
4288 */
4289
4290 for (l = c->parents; l; l = l->next) {
4291 struct object *p;
4292 p = &(l->item->object);
4293 if (p->flags & (CHILD_SHOWN | SHOWN))
4294 continue;
4295 p->flags |= CHILD_SHOWN;
4296 gc_boundary(&revs->boundary_commits);
4297 add_object_array(p, NULL, &revs->boundary_commits);
4298 }
4299
4300 return c;
4301 }
4302
4303 struct commit *get_revision(struct rev_info *revs)
4304 {
4305 struct commit *c;
4306 struct commit_list *reversed;
4307
4308 if (revs->reverse) {
4309 reversed = NULL;
4310 while ((c = get_revision_internal(revs)))
4311 commit_list_insert(c, &reversed);
4312 revs->commits = reversed;
4313 revs->reverse = 0;
4314 revs->reverse_output_stage = 1;
4315 }
4316
4317 if (revs->reverse_output_stage) {
4318 c = pop_commit(&revs->commits);
4319 if (revs->track_linear)
4320 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4321 return c;
4322 }
4323
4324 c = get_revision_internal(revs);
4325 if (c && revs->graph)
4326 graph_update(revs->graph, c);
4327 if (!c) {
4328 free_saved_parents(revs);
4329 free_commit_list(revs->previous_parents);
4330 revs->previous_parents = NULL;
4331 }
4332 return c;
4333 }
4334
4335 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4336 {
4337 if (commit->object.flags & BOUNDARY)
4338 return "-";
4339 else if (commit->object.flags & UNINTERESTING)
4340 return "^";
4341 else if (commit->object.flags & PATCHSAME)
4342 return "=";
4343 else if (!revs || revs->left_right) {
4344 if (commit->object.flags & SYMMETRIC_LEFT)
4345 return "<";
4346 else
4347 return ">";
4348 } else if (revs->graph)
4349 return "*";
4350 else if (revs->cherry_mark)
4351 return "+";
4352 return "";
4353 }
4354
4355 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4356 {
4357 const char *mark = get_revision_mark(revs, commit);
4358 if (!strlen(mark))
4359 return;
4360 fputs(mark, stdout);
4361 putchar(' ');
4362 }