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