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