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