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