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