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