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