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