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