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