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