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