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