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