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