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