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