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