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