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