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