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