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