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