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