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