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