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