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