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