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