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