at commit 66f48561

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