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