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