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