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