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