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