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