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