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