]> git.ipfire.org Git - thirdparty/git.git/blame - revision.c
Invert numbers and names in the git-shortlog summary mode.
[thirdparty/git.git] / revision.c
CommitLineData
ae563542
LT
1#include "cache.h"
2#include "tag.h"
3#include "blob.h"
4#include "tree.h"
5#include "commit.h"
a4a88b2b 6#include "diff.h"
ae563542
LT
7#include "refs.h"
8#include "revision.h"
8ecae9b0 9#include "grep.h"
8860fd42 10#include "reflog-walk.h"
d7a17cad 11#include "patch-ids.h"
ae563542 12
cdcefbc9
LT
13volatile show_early_output_fn_t show_early_output;
14
ae563542
LT
15static char *path_name(struct name_path *path, const char *name)
16{
17 struct name_path *p;
18 char *n, *m;
19 int nlen = strlen(name);
20 int len = nlen + 1;
21
22 for (p = path; p; p = p->up) {
23 if (p->elem_len)
24 len += p->elem_len + 1;
25 }
26 n = xmalloc(len);
27 m = n + len - (nlen + 1);
28 strcpy(m, name);
29 for (p = path; p; p = p->up) {
30 if (p->elem_len) {
31 m -= p->elem_len + 1;
32 memcpy(m, p->elem, p->elem_len);
33 m[p->elem_len] = '/';
34 }
35 }
36 return n;
37}
38
1f1e895f
LT
39void add_object(struct object *obj,
40 struct object_array *p,
41 struct name_path *path,
42 const char *name)
ae563542 43{
1f1e895f 44 add_object_array(obj, path_name(path, name), p);
ae563542
LT
45}
46
47static void mark_blob_uninteresting(struct blob *blob)
48{
49 if (blob->object.flags & UNINTERESTING)
50 return;
51 blob->object.flags |= UNINTERESTING;
52}
53
54void mark_tree_uninteresting(struct tree *tree)
55{
f75e53ed 56 struct tree_desc desc;
4c068a98 57 struct name_entry entry;
ae563542 58 struct object *obj = &tree->object;
ae563542
LT
59
60 if (obj->flags & UNINTERESTING)
61 return;
62 obj->flags |= UNINTERESTING;
63 if (!has_sha1_file(obj->sha1))
64 return;
65 if (parse_tree(tree) < 0)
66 die("bad tree %s", sha1_to_hex(obj->sha1));
f75e53ed 67
6fda5e51 68 init_tree_desc(&desc, tree->buffer, tree->size);
4c068a98 69 while (tree_entry(&desc, &entry)) {
4d1012c3
LT
70 switch (object_type(entry.mode)) {
71 case OBJ_TREE:
4c068a98 72 mark_tree_uninteresting(lookup_tree(entry.sha1));
4d1012c3
LT
73 break;
74 case OBJ_BLOB:
4c068a98 75 mark_blob_uninteresting(lookup_blob(entry.sha1));
4d1012c3
LT
76 break;
77 default:
78 /* Subproject commit - not in this repository */
79 break;
80 }
ae563542 81 }
f75e53ed
LT
82
83 /*
84 * We don't care about the tree any more
85 * after it has been marked uninteresting.
86 */
87 free(tree->buffer);
88 tree->buffer = NULL;
ae563542
LT
89}
90
91void mark_parents_uninteresting(struct commit *commit)
92{
93 struct commit_list *parents = commit->parents;
94
95 while (parents) {
96 struct commit *commit = parents->item;
d2c4af73
MU
97 if (!(commit->object.flags & UNINTERESTING)) {
98 commit->object.flags |= UNINTERESTING;
ae563542 99
d2c4af73
MU
100 /*
101 * Normally we haven't parsed the parent
102 * yet, so we won't have a parent of a parent
103 * here. However, it may turn out that we've
104 * reached this commit some other way (where it
105 * wasn't uninteresting), in which case we need
106 * to mark its parents recursively too..
107 */
108 if (commit->parents)
109 mark_parents_uninteresting(commit);
110 }
ae563542
LT
111
112 /*
113 * A missing commit is ok iff its parent is marked
114 * uninteresting.
115 *
116 * We just mark such a thing parsed, so that when
117 * it is popped next time around, we won't be trying
118 * to parse it and get an error.
119 */
120 if (!has_sha1_file(commit->object.sha1))
121 commit->object.parsed = 1;
122 parents = parents->next;
123 }
124}
125
2d93b9fa 126static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
ae563542 127{
aa27e461
LT
128 if (revs->no_walk && (obj->flags & UNINTERESTING))
129 die("object ranges do not make sense when not walking revisions");
7b69b873
JS
130 if (revs->reflog_info && obj->type == OBJ_COMMIT &&
131 add_reflog_for_walk(revs->reflog_info,
132 (struct commit *)obj, name))
133 return;
bb6c2fba 134 add_object_array_with_mode(obj, name, &revs->pending, mode);
ae563542
LT
135}
136
2d93b9fa
JH
137void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
138{
139 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
140}
141
cd2bdc53 142static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
ae563542
LT
143{
144 struct object *object;
145
146 object = parse_object(sha1);
147 if (!object)
148 die("bad object %s", name);
cd2bdc53
LT
149 object->flags |= flags;
150 return object;
151}
152
153static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
154{
155 unsigned long flags = object->flags;
ae563542
LT
156
157 /*
158 * Tag object? Look what it points to..
159 */
1974632c 160 while (object->type == OBJ_TAG) {
ae563542 161 struct tag *tag = (struct tag *) object;
cd2bdc53 162 if (revs->tag_objects && !(flags & UNINTERESTING))
ae563542
LT
163 add_pending_object(revs, object, tag->tag);
164 object = parse_object(tag->tagged->sha1);
165 if (!object)
166 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
167 }
168
169 /*
170 * Commit object? Just return it, we'll do all the complex
171 * reachability crud.
172 */
1974632c 173 if (object->type == OBJ_COMMIT) {
ae563542 174 struct commit *commit = (struct commit *)object;
ae563542
LT
175 if (parse_commit(commit) < 0)
176 die("unable to parse commit %s", name);
d9a83684 177 if (flags & UNINTERESTING) {
4262c1b0 178 commit->object.flags |= UNINTERESTING;
ae563542 179 mark_parents_uninteresting(commit);
d9a83684
LT
180 revs->limited = 1;
181 }
ae563542
LT
182 return commit;
183 }
184
185 /*
186 * Tree object? Either mark it uniniteresting, or add it
187 * to the list of objects to look at later..
188 */
1974632c 189 if (object->type == OBJ_TREE) {
ae563542
LT
190 struct tree *tree = (struct tree *)object;
191 if (!revs->tree_objects)
192 return NULL;
193 if (flags & UNINTERESTING) {
194 mark_tree_uninteresting(tree);
195 return NULL;
196 }
197 add_pending_object(revs, object, "");
198 return NULL;
199 }
200
201 /*
202 * Blob object? You know the drill by now..
203 */
1974632c 204 if (object->type == OBJ_BLOB) {
ae563542
LT
205 struct blob *blob = (struct blob *)object;
206 if (!revs->blob_objects)
207 return NULL;
208 if (flags & UNINTERESTING) {
209 mark_blob_uninteresting(blob);
210 return NULL;
211 }
212 add_pending_object(revs, object, "");
213 return NULL;
214 }
215 die("%s is unknown object", name);
216}
217
a4a88b2b
LT
218static int everybody_uninteresting(struct commit_list *orig)
219{
220 struct commit_list *list = orig;
221 while (list) {
222 struct commit *commit = list->item;
223 list = list->next;
224 if (commit->object.flags & UNINTERESTING)
225 continue;
226 return 0;
227 }
228 return 1;
229}
230
0a4ba7f8
JH
231/*
232 * The goal is to get REV_TREE_NEW as the result only if the
233 * diff consists of all '+' (and no other changes), and
234 * REV_TREE_DIFFERENT otherwise (of course if the trees are
235 * the same we want REV_TREE_SAME). That means that once we
236 * get to REV_TREE_DIFFERENT, we do not have to look any further.
237 */
8efdc326 238static int tree_difference = REV_TREE_SAME;
a4a88b2b
LT
239
240static void file_add_remove(struct diff_options *options,
241 int addremove, unsigned mode,
242 const unsigned char *sha1,
243 const char *base, const char *path)
244{
8efdc326 245 int diff = REV_TREE_DIFFERENT;
a4a88b2b
LT
246
247 /*
8efdc326
FK
248 * Is it an add of a new file? It means that the old tree
249 * didn't have it at all, so we will turn "REV_TREE_SAME" ->
250 * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
251 * (and if it already was "REV_TREE_NEW", we'll keep it
252 * "REV_TREE_NEW" of course).
a4a88b2b
LT
253 */
254 if (addremove == '+') {
255 diff = tree_difference;
8efdc326 256 if (diff != REV_TREE_SAME)
a4a88b2b 257 return;
8efdc326 258 diff = REV_TREE_NEW;
a4a88b2b
LT
259 }
260 tree_difference = diff;
dd47aa31 261 if (tree_difference == REV_TREE_DIFFERENT)
8f67f8ae 262 DIFF_OPT_SET(options, HAS_CHANGES);
a4a88b2b
LT
263}
264
265static void file_change(struct diff_options *options,
266 unsigned old_mode, unsigned new_mode,
267 const unsigned char *old_sha1,
268 const unsigned char *new_sha1,
269 const char *base, const char *path)
270{
8efdc326 271 tree_difference = REV_TREE_DIFFERENT;
8f67f8ae 272 DIFF_OPT_SET(options, HAS_CHANGES);
a4a88b2b
LT
273}
274
2d93b9fa 275static int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
a4a88b2b
LT
276{
277 if (!t1)
8efdc326 278 return REV_TREE_NEW;
a4a88b2b 279 if (!t2)
8efdc326
FK
280 return REV_TREE_DIFFERENT;
281 tree_difference = REV_TREE_SAME;
8f67f8ae 282 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
c4e05b1a 283 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
cd2bdc53 284 &revs->pruning) < 0)
8efdc326 285 return REV_TREE_DIFFERENT;
a4a88b2b
LT
286 return tree_difference;
287}
288
2d93b9fa 289static int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
a4a88b2b
LT
290{
291 int retval;
292 void *tree;
6fda5e51 293 unsigned long size;
a4a88b2b
LT
294 struct tree_desc empty, real;
295
296 if (!t1)
297 return 0;
298
6fda5e51 299 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
a4a88b2b
LT
300 if (!tree)
301 return 0;
6fda5e51
LT
302 init_tree_desc(&real, tree, size);
303 init_tree_desc(&empty, "", 0);
a4a88b2b 304
0a4ba7f8 305 tree_difference = REV_TREE_SAME;
8f67f8ae 306 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
cd2bdc53 307 retval = diff_tree(&empty, &real, "", &revs->pruning);
a4a88b2b
LT
308 free(tree);
309
0a4ba7f8 310 return retval >= 0 && (tree_difference == REV_TREE_SAME);
a4a88b2b
LT
311}
312
313static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
314{
315 struct commit_list **pp, *parent;
6631c736 316 int tree_changed = 0, tree_same = 0;
a4a88b2b 317
53b2c823
LT
318 /*
319 * If we don't do pruning, everything is interesting
320 */
7dc0fe3b 321 if (!revs->prune)
53b2c823 322 return;
53b2c823 323
a4a88b2b
LT
324 if (!commit->tree)
325 return;
326
327 if (!commit->parents) {
7dc0fe3b
LT
328 if (rev_same_tree_as_empty(revs, commit->tree))
329 commit->object.flags |= TREESAME;
a4a88b2b
LT
330 return;
331 }
332
53b2c823
LT
333 /*
334 * Normal non-merge commit? If we don't want to make the
335 * history dense, we consider it always to be a change..
336 */
7dc0fe3b 337 if (!revs->dense && !commit->parents->next)
53b2c823 338 return;
53b2c823 339
a4a88b2b
LT
340 pp = &commit->parents;
341 while ((parent = *pp) != NULL) {
342 struct commit *p = parent->item;
343
cc0e6c5a
AR
344 if (parse_commit(p) < 0)
345 die("cannot simplify commit %s (because of %s)",
346 sha1_to_hex(commit->object.sha1),
347 sha1_to_hex(p->object.sha1));
c4e05b1a 348 switch (rev_compare_tree(revs, p->tree, commit->tree)) {
8efdc326 349 case REV_TREE_SAME:
6631c736 350 tree_same = 1;
9202434c 351 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
f3219fbb
JH
352 /* Even if a merge with an uninteresting
353 * side branch brought the entire change
354 * we are interested in, we do not want
355 * to lose the other branches of this
356 * merge, so we just keep going.
357 */
358 pp = &parent->next;
359 continue;
360 }
a4a88b2b
LT
361 parent->next = NULL;
362 commit->parents = parent;
7dc0fe3b 363 commit->object.flags |= TREESAME;
a4a88b2b
LT
364 return;
365
8efdc326
FK
366 case REV_TREE_NEW:
367 if (revs->remove_empty_trees &&
c4e05b1a 368 rev_same_tree_as_empty(revs, p->tree)) {
c348f31a
JH
369 /* We are adding all the specified
370 * paths from this parent, so the
371 * history beyond this parent is not
372 * interesting. Remove its parents
373 * (they are grandparents for us).
374 * IOW, we pretend this parent is a
375 * "root" commit.
a41e109c 376 */
cc0e6c5a
AR
377 if (parse_commit(p) < 0)
378 die("cannot simplify commit %s (invalid %s)",
379 sha1_to_hex(commit->object.sha1),
380 sha1_to_hex(p->object.sha1));
c348f31a 381 p->parents = NULL;
a4a88b2b
LT
382 }
383 /* fallthrough */
8efdc326 384 case REV_TREE_DIFFERENT:
f3219fbb 385 tree_changed = 1;
a4a88b2b
LT
386 pp = &parent->next;
387 continue;
388 }
389 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
390 }
6631c736 391 if (tree_changed && !tree_same)
7dc0fe3b
LT
392 return;
393 commit->object.flags |= TREESAME;
a4a88b2b
LT
394}
395
cc0e6c5a 396static int add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
a4a88b2b
LT
397{
398 struct commit_list *parent = commit->parents;
577ed5c2 399 unsigned left_flag;
0053e902 400 int add, rest;
a4a88b2b 401
3381c790 402 if (commit->object.flags & ADDED)
cc0e6c5a 403 return 0;
3381c790
LT
404 commit->object.flags |= ADDED;
405
a4a88b2b
LT
406 /*
407 * If the commit is uninteresting, don't try to
408 * prune parents - we want the maximal uninteresting
409 * set.
410 *
411 * Normally we haven't parsed the parent
412 * yet, so we won't have a parent of a parent
413 * here. However, it may turn out that we've
414 * reached this commit some other way (where it
415 * wasn't uninteresting), in which case we need
416 * to mark its parents recursively too..
417 */
418 if (commit->object.flags & UNINTERESTING) {
419 while (parent) {
420 struct commit *p = parent->item;
421 parent = parent->next;
cc0e6c5a
AR
422 if (parse_commit(p) < 0)
423 return -1;
a4a88b2b
LT
424 p->object.flags |= UNINTERESTING;
425 if (p->parents)
426 mark_parents_uninteresting(p);
427 if (p->object.flags & SEEN)
428 continue;
429 p->object.flags |= SEEN;
430 insert_by_date(p, list);
431 }
cc0e6c5a 432 return 0;
a4a88b2b
LT
433 }
434
435 /*
436 * Ok, the commit wasn't uninteresting. Try to
437 * simplify the commit history and find the parent
438 * that has no differences in the path set if one exists.
439 */
53b2c823 440 try_to_simplify_commit(revs, commit);
a4a88b2b 441
ba1d4505 442 if (revs->no_walk)
cc0e6c5a 443 return 0;
ba1d4505 444
577ed5c2 445 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
0053e902
JH
446
447 rest = !revs->first_parent_only;
448 for (parent = commit->parents, add = 1; parent; add = rest) {
a4a88b2b
LT
449 struct commit *p = parent->item;
450
451 parent = parent->next;
cc0e6c5a
AR
452 if (parse_commit(p) < 0)
453 return -1;
577ed5c2 454 p->object.flags |= left_flag;
a4a88b2b
LT
455 if (p->object.flags & SEEN)
456 continue;
457 p->object.flags |= SEEN;
0053e902
JH
458 if (add)
459 insert_by_date(p, list);
a4a88b2b 460 }
cc0e6c5a 461 return 0;
a4a88b2b
LT
462}
463
36d56de6 464static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
d7a17cad
JH
465{
466 struct commit_list *p;
467 int left_count = 0, right_count = 0;
468 int left_first;
469 struct patch_ids ids;
470
471 /* First count the commits on the left and on the right */
472 for (p = list; p; p = p->next) {
473 struct commit *commit = p->item;
474 unsigned flags = commit->object.flags;
475 if (flags & BOUNDARY)
476 ;
477 else if (flags & SYMMETRIC_LEFT)
478 left_count++;
479 else
480 right_count++;
481 }
482
483 left_first = left_count < right_count;
484 init_patch_ids(&ids);
36d56de6
JS
485 if (revs->diffopt.nr_paths) {
486 ids.diffopts.nr_paths = revs->diffopt.nr_paths;
487 ids.diffopts.paths = revs->diffopt.paths;
488 ids.diffopts.pathlens = revs->diffopt.pathlens;
489 }
d7a17cad
JH
490
491 /* Compute patch-ids for one side */
492 for (p = list; p; p = p->next) {
493 struct commit *commit = p->item;
494 unsigned flags = commit->object.flags;
495
496 if (flags & BOUNDARY)
497 continue;
498 /*
499 * If we have fewer left, left_first is set and we omit
500 * commits on the right branch in this loop. If we have
501 * fewer right, we skip the left ones.
502 */
503 if (left_first != !!(flags & SYMMETRIC_LEFT))
504 continue;
505 commit->util = add_commit_patch_id(commit, &ids);
506 }
507
508 /* Check the other side */
509 for (p = list; p; p = p->next) {
510 struct commit *commit = p->item;
511 struct patch_id *id;
512 unsigned flags = commit->object.flags;
513
514 if (flags & BOUNDARY)
515 continue;
516 /*
517 * If we have fewer left, left_first is set and we omit
518 * commits on the left branch in this loop.
519 */
520 if (left_first == !!(flags & SYMMETRIC_LEFT))
521 continue;
522
523 /*
524 * Have we seen the same patch id?
525 */
526 id = has_commit_patch_id(commit, &ids);
527 if (!id)
528 continue;
529 id->seen = 1;
530 commit->object.flags |= SHOWN;
531 }
532
533 /* Now check the original side for seen ones */
534 for (p = list; p; p = p->next) {
535 struct commit *commit = p->item;
536 struct patch_id *ent;
537
538 ent = commit->util;
539 if (!ent)
540 continue;
541 if (ent->seen)
542 commit->object.flags |= SHOWN;
543 commit->util = NULL;
544 }
545
546 free_patch_ids(&ids);
547}
548
cc0e6c5a 549static int limit_list(struct rev_info *revs)
a4a88b2b
LT
550{
551 struct commit_list *list = revs->commits;
552 struct commit_list *newlist = NULL;
553 struct commit_list **p = &newlist;
554
555 while (list) {
556 struct commit_list *entry = list;
557 struct commit *commit = list->item;
558 struct object *obj = &commit->object;
cdcefbc9 559 show_early_output_fn_t show;
a4a88b2b
LT
560
561 list = list->next;
562 free(entry);
563
564 if (revs->max_age != -1 && (commit->date < revs->max_age))
565 obj->flags |= UNINTERESTING;
cc0e6c5a
AR
566 if (add_parents_to_list(revs, commit, &list) < 0)
567 return -1;
a4a88b2b
LT
568 if (obj->flags & UNINTERESTING) {
569 mark_parents_uninteresting(commit);
570 if (everybody_uninteresting(list))
571 break;
572 continue;
573 }
574 if (revs->min_age != -1 && (commit->date > revs->min_age))
575 continue;
576 p = &commit_list_insert(commit, p)->next;
cdcefbc9
LT
577
578 show = show_early_output;
579 if (!show)
580 continue;
581
582 show(revs, newlist);
583 show_early_output = NULL;
a4a88b2b 584 }
d7a17cad 585 if (revs->cherry_pick)
36d56de6 586 cherry_pick_list(newlist, revs);
d7a17cad 587
a4a88b2b 588 revs->commits = newlist;
cc0e6c5a 589 return 0;
a4a88b2b
LT
590}
591
63049292
JH
592struct all_refs_cb {
593 int all_flags;
71b03b42 594 int warned_bad_reflog;
63049292
JH
595 struct rev_info *all_revs;
596 const char *name_for_errormsg;
597};
ae563542 598
8da19775 599static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
ae563542 600{
63049292
JH
601 struct all_refs_cb *cb = cb_data;
602 struct object *object = get_reference(cb->all_revs, path, sha1,
603 cb->all_flags);
3d1efd8f 604 add_pending_object(cb->all_revs, object, path);
ae563542
LT
605 return 0;
606}
607
608static void handle_all(struct rev_info *revs, unsigned flags)
609{
63049292
JH
610 struct all_refs_cb cb;
611 cb.all_revs = revs;
612 cb.all_flags = flags;
613 for_each_ref(handle_one_ref, &cb);
614}
615
71b03b42 616static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
63049292
JH
617{
618 struct all_refs_cb *cb = cb_data;
71b03b42
SP
619 if (!is_null_sha1(sha1)) {
620 struct object *o = parse_object(sha1);
621 if (o) {
622 o->flags |= cb->all_flags;
623 add_pending_object(cb->all_revs, o, "");
624 }
625 else if (!cb->warned_bad_reflog) {
46efd2d9 626 warning("reflog of '%s' references pruned commits",
71b03b42
SP
627 cb->name_for_errormsg);
628 cb->warned_bad_reflog = 1;
629 }
63049292 630 }
71b03b42
SP
631}
632
883d60fa
JS
633static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
634 const char *email, unsigned long timestamp, int tz,
635 const char *message, void *cb_data)
71b03b42
SP
636{
637 handle_one_reflog_commit(osha1, cb_data);
638 handle_one_reflog_commit(nsha1, cb_data);
63049292
JH
639 return 0;
640}
641
642static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
643{
644 struct all_refs_cb *cb = cb_data;
71b03b42 645 cb->warned_bad_reflog = 0;
63049292
JH
646 cb->name_for_errormsg = path;
647 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
648 return 0;
649}
650
651static void handle_reflog(struct rev_info *revs, unsigned flags)
652{
653 struct all_refs_cb cb;
654 cb.all_revs = revs;
655 cb.all_flags = flags;
25b51e2c 656 for_each_reflog(handle_one_reflog, &cb);
ae563542
LT
657}
658
ea4a19e1
JH
659static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
660{
661 unsigned char sha1[20];
662 struct object *it;
663 struct commit *commit;
664 struct commit_list *parents;
665
666 if (*arg == '^') {
667 flags ^= UNINTERESTING;
668 arg++;
669 }
670 if (get_sha1(arg, sha1))
671 return 0;
672 while (1) {
673 it = get_reference(revs, arg, sha1, 0);
1974632c 674 if (it->type != OBJ_TAG)
ea4a19e1 675 break;
e702496e 676 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
ea4a19e1 677 }
1974632c 678 if (it->type != OBJ_COMMIT)
ea4a19e1
JH
679 return 0;
680 commit = (struct commit *)it;
681 for (parents = commit->parents; parents; parents = parents->next) {
682 it = &parents->item->object;
683 it->flags |= flags;
684 add_pending_object(revs, it, arg);
685 }
686 return 1;
687}
688
db6296a5 689void init_revisions(struct rev_info *revs, const char *prefix)
8efdc326
FK
690{
691 memset(revs, 0, sizeof(*revs));
8e8f9987 692
6b9c58f4 693 revs->abbrev = DEFAULT_ABBREV;
8e8f9987 694 revs->ignore_merges = 1;
9202434c 695 revs->simplify_history = 1;
8f67f8ae
PH
696 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
697 DIFF_OPT_SET(&revs->pruning, QUIET);
cd2bdc53
LT
698 revs->pruning.add_remove = file_add_remove;
699 revs->pruning.change = file_change;
8efdc326
FK
700 revs->lifo = 1;
701 revs->dense = 1;
db6296a5 702 revs->prefix = prefix;
8efdc326
FK
703 revs->max_age = -1;
704 revs->min_age = -1;
d5db6c9e 705 revs->skip_count = -1;
8efdc326
FK
706 revs->max_count = -1;
707
cd2bdc53
LT
708 revs->commit_format = CMIT_FMT_DEFAULT;
709
710 diff_setup(&revs->diffopt);
8efdc326
FK
711}
712
0d2c9d67
RS
713static void add_pending_commit_list(struct rev_info *revs,
714 struct commit_list *commit_list,
715 unsigned int flags)
716{
717 while (commit_list) {
718 struct object *object = &commit_list->item->object;
719 object->flags |= flags;
720 add_pending_object(revs, object, sha1_to_hex(object->sha1));
721 commit_list = commit_list->next;
722 }
723}
724
ae3e5e1e
JH
725static void prepare_show_merge(struct rev_info *revs)
726{
727 struct commit_list *bases;
728 struct commit *head, *other;
729 unsigned char sha1[20];
730 const char **prune = NULL;
731 int i, prune_num = 1; /* counting terminating NULL */
732
733 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
734 die("--merge without HEAD?");
735 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
736 die("--merge without MERGE_HEAD?");
737 add_pending_object(revs, &head->object, "HEAD");
738 add_pending_object(revs, &other->object, "MERGE_HEAD");
739 bases = get_merge_bases(head, other, 1);
740 while (bases) {
741 struct commit *it = bases->item;
742 struct commit_list *n = bases->next;
743 free(bases);
744 bases = n;
745 it->object.flags |= UNINTERESTING;
746 add_pending_object(revs, &it->object, "(merge-base)");
747 }
748
749 if (!active_nr)
750 read_cache();
751 for (i = 0; i < active_nr; i++) {
752 struct cache_entry *ce = active_cache[i];
753 if (!ce_stage(ce))
754 continue;
755 if (ce_path_match(ce, revs->prune_data)) {
756 prune_num++;
757 prune = xrealloc(prune, sizeof(*prune) * prune_num);
758 prune[prune_num-2] = ce->name;
759 prune[prune_num-1] = NULL;
760 }
761 while ((i+1 < active_nr) &&
762 ce_same_name(ce, active_cache[i+1]))
763 i++;
764 }
765 revs->prune_data = prune;
766}
767
5d6f0935
JH
768int handle_revision_arg(const char *arg, struct rev_info *revs,
769 int flags,
770 int cant_be_filename)
771{
bb6c2fba 772 unsigned mode;
5d6f0935
JH
773 char *dotdot;
774 struct object *object;
775 unsigned char sha1[20];
776 int local_flags;
777
778 dotdot = strstr(arg, "..");
779 if (dotdot) {
780 unsigned char from_sha1[20];
781 const char *next = dotdot + 2;
782 const char *this = arg;
783 int symmetric = *next == '.';
784 unsigned int flags_exclude = flags ^ UNINTERESTING;
785
786 *dotdot = 0;
787 next += symmetric;
788
789 if (!*next)
790 next = "HEAD";
791 if (dotdot == arg)
792 this = "HEAD";
793 if (!get_sha1(this, from_sha1) &&
794 !get_sha1(next, sha1)) {
795 struct commit *a, *b;
796 struct commit_list *exclude;
797
798 a = lookup_commit_reference(from_sha1);
799 b = lookup_commit_reference(sha1);
800 if (!a || !b) {
801 die(symmetric ?
802 "Invalid symmetric difference expression %s...%s" :
803 "Invalid revision range %s..%s",
804 arg, next);
805 }
806
807 if (!cant_be_filename) {
808 *dotdot = '.';
809 verify_non_filename(revs->prefix, arg);
810 }
811
812 if (symmetric) {
813 exclude = get_merge_bases(a, b, 1);
814 add_pending_commit_list(revs, exclude,
815 flags_exclude);
816 free_commit_list(exclude);
577ed5c2 817 a->object.flags |= flags | SYMMETRIC_LEFT;
5d6f0935
JH
818 } else
819 a->object.flags |= flags_exclude;
820 b->object.flags |= flags;
821 add_pending_object(revs, &a->object, this);
822 add_pending_object(revs, &b->object, next);
823 return 0;
824 }
825 *dotdot = '.';
826 }
827 dotdot = strstr(arg, "^@");
828 if (dotdot && !dotdot[2]) {
829 *dotdot = 0;
830 if (add_parents_only(revs, arg, flags))
831 return 0;
832 *dotdot = '^';
833 }
62476c8e
JH
834 dotdot = strstr(arg, "^!");
835 if (dotdot && !dotdot[2]) {
836 *dotdot = 0;
837 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
838 *dotdot = '^';
839 }
840
5d6f0935
JH
841 local_flags = 0;
842 if (*arg == '^') {
843 local_flags = UNINTERESTING;
844 arg++;
845 }
bb6c2fba 846 if (get_sha1_with_mode(arg, sha1, &mode))
5d6f0935
JH
847 return -1;
848 if (!cant_be_filename)
849 verify_non_filename(revs->prefix, arg);
850 object = get_reference(revs, arg, sha1, flags ^ local_flags);
bb6c2fba 851 add_pending_object_with_mode(revs, object, arg, mode);
5d6f0935
JH
852 return 0;
853}
854
2d10c555 855static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
bd95fcd3 856{
2d10c555 857 if (!revs->grep_filter) {
bd95fcd3
JH
858 struct grep_opt *opt = xcalloc(1, sizeof(*opt));
859 opt->status_only = 1;
860 opt->pattern_tail = &(opt->pattern_list);
861 opt->regflags = REG_NEWLINE;
2d10c555 862 revs->grep_filter = opt;
bd95fcd3 863 }
2d10c555
JH
864 append_grep_pattern(revs->grep_filter, ptn,
865 "command line", 0, what);
866}
867
868static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern)
869{
870 char *pat;
871 const char *prefix;
872 int patlen, fldlen;
bd95fcd3
JH
873
874 fldlen = strlen(field);
875 patlen = strlen(pattern);
a2ed6ae4
LT
876 pat = xmalloc(patlen + fldlen + 10);
877 prefix = ".*";
878 if (*pattern == '^') {
879 prefix = "";
880 pattern++;
881 }
882 sprintf(pat, "^%s %s%s", field, prefix, pattern);
2d10c555 883 add_grep(revs, pat, GREP_PATTERN_HEAD);
bd95fcd3
JH
884}
885
886static void add_message_grep(struct rev_info *revs, const char *pattern)
887{
2d10c555 888 add_grep(revs, pattern, GREP_PATTERN_BODY);
bd95fcd3
JH
889}
890
106d710b
JH
891static void add_ignore_packed(struct rev_info *revs, const char *name)
892{
893 int num = ++revs->num_ignore_packed;
894
895 revs->ignore_packed = xrealloc(revs->ignore_packed,
896 sizeof(const char **) * (num + 1));
897 revs->ignore_packed[num-1] = name;
898 revs->ignore_packed[num] = NULL;
899}
900
ae563542
LT
901/*
902 * Parse revision information, filling in the "rev_info" structure,
903 * and removing the used arguments from the argument list.
904 *
765ac8ec
LT
905 * Returns the number of arguments left that weren't recognized
906 * (which are also moved to the head of the argument list)
ae563542 907 */
a4a88b2b 908int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
ae563542 909{
ae3e5e1e 910 int i, flags, seen_dashdash, show_merge;
765ac8ec 911 const char **unrecognized = argv + 1;
ae563542 912 int left = 1;
70d0afba 913 int all_match = 0;
93d496a5 914 int regflags = 0;
ae563542 915
ae563542
LT
916 /* First, search for "--" */
917 seen_dashdash = 0;
918 for (i = 1; i < argc; i++) {
919 const char *arg = argv[i];
920 if (strcmp(arg, "--"))
921 continue;
922 argv[i] = NULL;
923 argc = i;
a65f2005
JH
924 if (argv[i + 1])
925 revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
ae563542
LT
926 seen_dashdash = 1;
927 break;
928 }
929
ae3e5e1e 930 flags = show_merge = 0;
ae563542 931 for (i = 1; i < argc; i++) {
ae563542 932 const char *arg = argv[i];
ae563542 933 if (*arg == '-') {
cd2bdc53 934 int opts;
cc44c765 935 if (!prefixcmp(arg, "--max-count=")) {
ae563542
LT
936 revs->max_count = atoi(arg + 12);
937 continue;
938 }
cc44c765 939 if (!prefixcmp(arg, "--skip=")) {
d5db6c9e
JH
940 revs->skip_count = atoi(arg + 7);
941 continue;
942 }
de5f2bf3 943 /* accept -<digit>, like traditional "head" */
64bc6e3d
JH
944 if ((*arg == '-') && isdigit(arg[1])) {
945 revs->max_count = atoi(arg + 1);
946 continue;
947 }
948 if (!strcmp(arg, "-n")) {
949 if (argc <= i + 1)
950 die("-n requires an argument");
951 revs->max_count = atoi(argv[++i]);
952 continue;
953 }
1968d77d 954 if (!prefixcmp(arg, "-n")) {
64bc6e3d
JH
955 revs->max_count = atoi(arg + 2);
956 continue;
957 }
cc44c765 958 if (!prefixcmp(arg, "--max-age=")) {
ae563542 959 revs->max_age = atoi(arg + 10);
ae563542
LT
960 continue;
961 }
cc44c765 962 if (!prefixcmp(arg, "--since=")) {
fd751667 963 revs->max_age = approxidate(arg + 8);
fd751667
JH
964 continue;
965 }
cc44c765 966 if (!prefixcmp(arg, "--after=")) {
fd751667 967 revs->max_age = approxidate(arg + 8);
53069686
JH
968 continue;
969 }
cc44c765 970 if (!prefixcmp(arg, "--min-age=")) {
53069686 971 revs->min_age = atoi(arg + 10);
fd751667
JH
972 continue;
973 }
cc44c765 974 if (!prefixcmp(arg, "--before=")) {
fd751667 975 revs->min_age = approxidate(arg + 9);
fd751667
JH
976 continue;
977 }
cc44c765 978 if (!prefixcmp(arg, "--until=")) {
fd751667 979 revs->min_age = approxidate(arg + 8);
fd751667
JH
980 continue;
981 }
ae563542
LT
982 if (!strcmp(arg, "--all")) {
983 handle_all(revs, flags);
984 continue;
985 }
0053e902
JH
986 if (!strcmp(arg, "--first-parent")) {
987 revs->first_parent_only = 1;
988 continue;
989 }
63049292
JH
990 if (!strcmp(arg, "--reflog")) {
991 handle_reflog(revs, flags);
992 continue;
993 }
084ae0a7
JS
994 if (!strcmp(arg, "-g") ||
995 !strcmp(arg, "--walk-reflogs")) {
8860fd42
JS
996 init_reflog_walk(&revs->reflog_info);
997 continue;
998 }
ae563542
LT
999 if (!strcmp(arg, "--not")) {
1000 flags ^= UNINTERESTING;
1001 continue;
1002 }
1003 if (!strcmp(arg, "--default")) {
1004 if (++i >= argc)
1005 die("bad --default argument");
1006 def = argv[i];
1007 continue;
1008 }
ae3e5e1e
JH
1009 if (!strcmp(arg, "--merge")) {
1010 show_merge = 1;
1011 continue;
1012 }
ae563542
LT
1013 if (!strcmp(arg, "--topo-order")) {
1014 revs->topo_order = 1;
1015 continue;
1016 }
1017 if (!strcmp(arg, "--date-order")) {
1018 revs->lifo = 0;
1019 revs->topo_order = 1;
1020 continue;
1021 }
cdcefbc9
LT
1022 if (!prefixcmp(arg, "--early-output")) {
1023 int count = 100;
1024 switch (arg[14]) {
1025 case '=':
1026 count = atoi(arg+15);
1027 /* Fallthrough */
1028 case 0:
1029 revs->topo_order = 1;
1030 revs->early_output = count;
1031 continue;
1032 }
1033 }
7b0c9966
LT
1034 if (!strcmp(arg, "--parents")) {
1035 revs->parents = 1;
1036 continue;
1037 }
ae563542
LT
1038 if (!strcmp(arg, "--dense")) {
1039 revs->dense = 1;
1040 continue;
1041 }
1042 if (!strcmp(arg, "--sparse")) {
1043 revs->dense = 0;
1044 continue;
1045 }
1046 if (!strcmp(arg, "--remove-empty")) {
1047 revs->remove_empty_trees = 1;
1048 continue;
1049 }
5cdeae71 1050 if (!strcmp(arg, "--no-merges")) {
765ac8ec
LT
1051 revs->no_merges = 1;
1052 continue;
1053 }
384e99a4
JH
1054 if (!strcmp(arg, "--boundary")) {
1055 revs->boundary = 1;
1056 continue;
1057 }
8dce8235
JH
1058 if (!strcmp(arg, "--left-right")) {
1059 revs->left_right = 1;
74bd9029
JH
1060 continue;
1061 }
d7a17cad
JH
1062 if (!strcmp(arg, "--cherry-pick")) {
1063 revs->cherry_pick = 1;
023756f4 1064 revs->limited = 1;
d7a17cad
JH
1065 continue;
1066 }
ae563542
LT
1067 if (!strcmp(arg, "--objects")) {
1068 revs->tag_objects = 1;
1069 revs->tree_objects = 1;
1070 revs->blob_objects = 1;
1071 continue;
1072 }
1073 if (!strcmp(arg, "--objects-edge")) {
1074 revs->tag_objects = 1;
1075 revs->tree_objects = 1;
1076 revs->blob_objects = 1;
1077 revs->edge_hint = 1;
1078 continue;
1079 }
d9a83684
LT
1080 if (!strcmp(arg, "--unpacked")) {
1081 revs->unpacked = 1;
106d710b
JH
1082 free(revs->ignore_packed);
1083 revs->ignore_packed = NULL;
1084 revs->num_ignore_packed = 0;
1085 continue;
1086 }
cc44c765 1087 if (!prefixcmp(arg, "--unpacked=")) {
106d710b
JH
1088 revs->unpacked = 1;
1089 add_ignore_packed(revs, arg+11);
d9a83684
LT
1090 continue;
1091 }
cd2bdc53
LT
1092 if (!strcmp(arg, "-r")) {
1093 revs->diff = 1;
8f67f8ae 1094 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
cd2bdc53
LT
1095 continue;
1096 }
1097 if (!strcmp(arg, "-t")) {
1098 revs->diff = 1;
8f67f8ae
PH
1099 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1100 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
cd2bdc53
LT
1101 continue;
1102 }
1103 if (!strcmp(arg, "-m")) {
1104 revs->ignore_merges = 0;
1105 continue;
1106 }
1107 if (!strcmp(arg, "-c")) {
1108 revs->diff = 1;
0fe7c1de 1109 revs->dense_combined_merges = 0;
cd2bdc53
LT
1110 revs->combine_merges = 1;
1111 continue;
1112 }
1113 if (!strcmp(arg, "--cc")) {
1114 revs->diff = 1;
1115 revs->dense_combined_merges = 1;
1116 revs->combine_merges = 1;
1117 continue;
1118 }
1119 if (!strcmp(arg, "-v")) {
1120 revs->verbose_header = 1;
cd2bdc53
LT
1121 continue;
1122 }
cc44c765 1123 if (!prefixcmp(arg, "--pretty")) {
cd2bdc53 1124 revs->verbose_header = 1;
cd2bdc53
LT
1125 revs->commit_format = get_commit_format(arg+8);
1126 continue;
1127 }
1128 if (!strcmp(arg, "--root")) {
1129 revs->show_root_diff = 1;
1130 continue;
1131 }
1132 if (!strcmp(arg, "--no-commit-id")) {
1133 revs->no_commit_id = 1;
1134 continue;
1135 }
1136 if (!strcmp(arg, "--always")) {
1137 revs->always_show_header = 1;
1138 continue;
1139 }
1140 if (!strcmp(arg, "--no-abbrev")) {
1141 revs->abbrev = 0;
1142 continue;
1143 }
1144 if (!strcmp(arg, "--abbrev")) {
1145 revs->abbrev = DEFAULT_ABBREV;
1146 continue;
1147 }
cc44c765 1148 if (!prefixcmp(arg, "--abbrev=")) {
508d9e37
LT
1149 revs->abbrev = strtoul(arg + 9, NULL, 10);
1150 if (revs->abbrev < MINIMUM_ABBREV)
1151 revs->abbrev = MINIMUM_ABBREV;
1152 else if (revs->abbrev > 40)
1153 revs->abbrev = 40;
1154 continue;
1155 }
cd2bdc53
LT
1156 if (!strcmp(arg, "--abbrev-commit")) {
1157 revs->abbrev_commit = 1;
1158 continue;
1159 }
1160 if (!strcmp(arg, "--full-diff")) {
1161 revs->diff = 1;
1162 revs->full_diff = 1;
1163 continue;
1164 }
9202434c
LT
1165 if (!strcmp(arg, "--full-history")) {
1166 revs->simplify_history = 0;
1167 continue;
1168 }
3dfb9278 1169 if (!strcmp(arg, "--relative-date")) {
a7b02ccf
JH
1170 revs->date_mode = DATE_RELATIVE;
1171 continue;
1172 }
1173 if (!strncmp(arg, "--date=", 7)) {
856665f8 1174 revs->date_mode = parse_date_format(arg + 7);
3dfb9278
JF
1175 continue;
1176 }
9fa3465d
MC
1177 if (!strcmp(arg, "--log-size")) {
1178 revs->show_log_size = 1;
1179 continue;
1180 }
2d10c555
JH
1181
1182 /*
1183 * Grepping the commit log
1184 */
cc44c765 1185 if (!prefixcmp(arg, "--author=")) {
bd95fcd3
JH
1186 add_header_grep(revs, "author", arg+9);
1187 continue;
1188 }
cc44c765 1189 if (!prefixcmp(arg, "--committer=")) {
bd95fcd3
JH
1190 add_header_grep(revs, "committer", arg+12);
1191 continue;
1192 }
cc44c765 1193 if (!prefixcmp(arg, "--grep=")) {
bd95fcd3
JH
1194 add_message_grep(revs, arg+7);
1195 continue;
1196 }
e5633cbb
JH
1197 if (!strcmp(arg, "--extended-regexp") ||
1198 !strcmp(arg, "-E")) {
93d496a5
PB
1199 regflags |= REG_EXTENDED;
1200 continue;
1201 }
e5633cbb
JH
1202 if (!strcmp(arg, "--regexp-ignore-case") ||
1203 !strcmp(arg, "-i")) {
93d496a5
PB
1204 regflags |= REG_ICASE;
1205 continue;
1206 }
70d0afba
JH
1207 if (!strcmp(arg, "--all-match")) {
1208 all_match = 1;
1209 continue;
1210 }
cc44c765 1211 if (!prefixcmp(arg, "--encoding=")) {
7cbcf4d5
JH
1212 arg += 11;
1213 if (strcmp(arg, "none"))
567fb65e 1214 git_log_output_encoding = xstrdup(arg);
7cbcf4d5
JH
1215 else
1216 git_log_output_encoding = "";
1217 continue;
1218 }
9c5e66e9
JS
1219 if (!strcmp(arg, "--reverse")) {
1220 revs->reverse ^= 1;
1221 continue;
1222 }
8e64006e
JS
1223 if (!strcmp(arg, "--no-walk")) {
1224 revs->no_walk = 1;
1225 continue;
1226 }
1227 if (!strcmp(arg, "--do-walk")) {
1228 revs->no_walk = 0;
1229 continue;
1230 }
2d10c555 1231
cd2bdc53
LT
1232 opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
1233 if (opts > 0) {
cd2bdc53
LT
1234 i += opts - 1;
1235 continue;
1236 }
ae563542
LT
1237 *unrecognized++ = arg;
1238 left++;
1239 continue;
1240 }
ae563542 1241
5d6f0935
JH
1242 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1243 int j;
1244 if (seen_dashdash || *arg == '^')
ae563542
LT
1245 die("bad revision '%s'", arg);
1246
ea92f41f
JH
1247 /* If we didn't have a "--":
1248 * (1) all filenames must exist;
1249 * (2) all rev-args must not be interpretable
1250 * as a valid filename.
1251 * but the latter we have checked in the main loop.
1252 */
e23d0b4a
LT
1253 for (j = i; j < argc; j++)
1254 verify_filename(revs->prefix, argv[j]);
1255
5d6f0935
JH
1256 revs->prune_data = get_pathspec(revs->prefix,
1257 argv + i);
ae563542
LT
1258 break;
1259 }
ae563542 1260 }
5d6f0935 1261
93d496a5
PB
1262 if (revs->grep_filter)
1263 revs->grep_filter->regflags |= regflags;
1264
ae3e5e1e
JH
1265 if (show_merge)
1266 prepare_show_merge(revs);
1f1e895f 1267 if (def && !revs->pending.nr) {
ae563542 1268 unsigned char sha1[20];
cd2bdc53 1269 struct object *object;
bb6c2fba
MK
1270 unsigned mode;
1271 if (get_sha1_with_mode(def, sha1, &mode))
ae563542 1272 die("bad default revision '%s'", def);
cd2bdc53 1273 object = get_reference(revs, def, sha1, 0);
bb6c2fba 1274 add_pending_object_with_mode(revs, object, def, mode);
ae563542 1275 }
8efdc326 1276
b7bb760d
LT
1277 /* Did the user ask for any diff output? Run the diff! */
1278 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1279 revs->diff = 1;
1280
99516e35 1281 /* Pickaxe and rename following needs diffs */
8f67f8ae 1282 if (revs->diffopt.pickaxe || DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
b7bb760d
LT
1283 revs->diff = 1;
1284
9dad9d2e 1285 if (revs->topo_order)
53069686
JH
1286 revs->limited = 1;
1287
8efdc326 1288 if (revs->prune_data) {
cd2bdc53 1289 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
750f7b66 1290 /* Can't prune commits with rename following: the paths change.. */
8f67f8ae 1291 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
53b2c823 1292 revs->prune = 1;
cd2bdc53
LT
1293 if (!revs->full_diff)
1294 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
8efdc326 1295 }
cd2bdc53
LT
1296 if (revs->combine_merges) {
1297 revs->ignore_merges = 0;
0e677e1a 1298 if (revs->dense_combined_merges && !revs->diffopt.output_format)
cd2bdc53
LT
1299 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1300 }
cd2bdc53 1301 revs->diffopt.abbrev = revs->abbrev;
72ee96c0
JH
1302 if (diff_setup_done(&revs->diffopt) < 0)
1303 die("diff_setup_done failed");
8efdc326 1304
70d0afba
JH
1305 if (revs->grep_filter) {
1306 revs->grep_filter->all_match = all_match;
2d10c555 1307 compile_grep_patterns(revs->grep_filter);
70d0afba 1308 }
bd95fcd3 1309
d56651c0
SP
1310 if (revs->reverse && revs->reflog_info)
1311 die("cannot combine --reverse with --walk-reflogs");
1312
ae563542
LT
1313 return left;
1314}
a4a88b2b 1315
cc0e6c5a 1316int prepare_revision_walk(struct rev_info *revs)
a4a88b2b 1317{
1f1e895f 1318 int nr = revs->pending.nr;
94d23673 1319 struct object_array_entry *e, *list;
cd2bdc53 1320
94d23673 1321 e = list = revs->pending.objects;
1f1e895f
LT
1322 revs->pending.nr = 0;
1323 revs->pending.alloc = 0;
1324 revs->pending.objects = NULL;
1325 while (--nr >= 0) {
94d23673 1326 struct commit *commit = handle_commit(revs, e->item, e->name);
cd2bdc53
LT
1327 if (commit) {
1328 if (!(commit->object.flags & SEEN)) {
1329 commit->object.flags |= SEEN;
1330 insert_by_date(commit, &revs->commits);
1331 }
1332 }
94d23673 1333 e++;
cd2bdc53 1334 }
94d23673 1335 free(list);
cd2bdc53 1336
ba1d4505 1337 if (revs->no_walk)
cc0e6c5a 1338 return 0;
a4a88b2b 1339 if (revs->limited)
cc0e6c5a
AR
1340 if (limit_list(revs) < 0)
1341 return -1;
a4a88b2b 1342 if (revs->topo_order)
23c17d4a 1343 sort_in_topological_order(&revs->commits, revs->lifo);
cc0e6c5a 1344 return 0;
a4a88b2b
LT
1345}
1346
cc0e6c5a
AR
1347enum rewrite_result {
1348 rewrite_one_ok,
1349 rewrite_one_noparents,
1350 rewrite_one_error,
1351};
1352
1353static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
765ac8ec
LT
1354{
1355 for (;;) {
1356 struct commit *p = *pp;
3381c790 1357 if (!revs->limited)
cc0e6c5a
AR
1358 if (add_parents_to_list(revs, p, &revs->commits) < 0)
1359 return rewrite_one_error;
6631c736 1360 if (p->parents && p->parents->next)
cc0e6c5a 1361 return rewrite_one_ok;
7dc0fe3b
LT
1362 if (p->object.flags & UNINTERESTING)
1363 return rewrite_one_ok;
1364 if (!(p->object.flags & TREESAME))
cc0e6c5a 1365 return rewrite_one_ok;
765ac8ec 1366 if (!p->parents)
cc0e6c5a 1367 return rewrite_one_noparents;
765ac8ec
LT
1368 *pp = p->parents->item;
1369 }
1370}
1371
11d65967
JH
1372static void remove_duplicate_parents(struct commit *commit)
1373{
e1abc69b 1374 struct commit_list **pp, *p;
11d65967
JH
1375
1376 /* Examine existing parents while marking ones we have seen... */
e1abc69b
LT
1377 pp = &commit->parents;
1378 while ((p = *pp) != NULL) {
11d65967 1379 struct commit *parent = p->item;
e1abc69b
LT
1380 if (parent->object.flags & TMP_MARK) {
1381 *pp = p->next;
11d65967 1382 continue;
e1abc69b 1383 }
11d65967 1384 parent->object.flags |= TMP_MARK;
11d65967
JH
1385 pp = &p->next;
1386 }
1387 /* ... and clear the temporary mark */
1388 for (p = commit->parents; p; p = p->next)
1389 p->item->object.flags &= ~TMP_MARK;
1390}
1391
cc0e6c5a 1392static int rewrite_parents(struct rev_info *revs, struct commit *commit)
765ac8ec
LT
1393{
1394 struct commit_list **pp = &commit->parents;
1395 while (*pp) {
1396 struct commit_list *parent = *pp;
cc0e6c5a
AR
1397 switch (rewrite_one(revs, &parent->item)) {
1398 case rewrite_one_ok:
1399 break;
1400 case rewrite_one_noparents:
765ac8ec
LT
1401 *pp = parent->next;
1402 continue;
cc0e6c5a
AR
1403 case rewrite_one_error:
1404 return -1;
765ac8ec
LT
1405 }
1406 pp = &parent->next;
1407 }
11d65967 1408 remove_duplicate_parents(commit);
cc0e6c5a 1409 return 0;
765ac8ec
LT
1410}
1411
8ecae9b0
JH
1412static int commit_match(struct commit *commit, struct rev_info *opt)
1413{
2d10c555 1414 if (!opt->grep_filter)
8ecae9b0 1415 return 1;
2d10c555
JH
1416 return grep_buffer(opt->grep_filter,
1417 NULL, /* we say nothing, not even filename */
1418 commit->buffer, strlen(commit->buffer));
8ecae9b0
JH
1419}
1420
252a7c02
LT
1421enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
1422{
1423 if (commit->object.flags & SHOWN)
1424 return commit_ignore;
1425 if (revs->unpacked && has_sha1_pack(commit->object.sha1, revs->ignore_packed))
1426 return commit_ignore;
1427 if (commit->object.flags & UNINTERESTING)
1428 return commit_ignore;
1429 if (revs->min_age != -1 && (commit->date > revs->min_age))
1430 return commit_ignore;
1431 if (revs->no_merges && commit->parents && commit->parents->next)
1432 return commit_ignore;
1433 if (!commit_match(commit, revs))
1434 return commit_ignore;
53b2c823 1435 if (revs->prune && revs->dense) {
252a7c02 1436 /* Commit without changes? */
7dc0fe3b 1437 if (commit->object.flags & TREESAME) {
252a7c02
LT
1438 /* drop merges unless we want parenthood */
1439 if (!revs->parents)
1440 return commit_ignore;
1441 /* non-merge - always ignore it */
1442 if (!commit->parents || !commit->parents->next)
1443 return commit_ignore;
1444 }
1445 if (revs->parents && rewrite_parents(revs, commit) < 0)
1446 return commit_error;
1447 }
1448 return commit_show;
1449}
1450
d5db6c9e 1451static struct commit *get_revision_1(struct rev_info *revs)
a4a88b2b 1452{
d5db6c9e 1453 if (!revs->commits)
a4a88b2b 1454 return NULL;
a4a88b2b 1455
765ac8ec 1456 do {
cb115748
LT
1457 struct commit_list *entry = revs->commits;
1458 struct commit *commit = entry->item;
ea5ed3ab 1459
cb115748
LT
1460 revs->commits = entry->next;
1461 free(entry);
2a0925be 1462
8860fd42
JS
1463 if (revs->reflog_info)
1464 fake_reflog_parent(revs->reflog_info, commit);
1465
2a0925be
LT
1466 /*
1467 * If we haven't done the list limiting, we need to look at
be7db6e5
LT
1468 * the parents here. We also need to do the date-based limiting
1469 * that we'd otherwise have done in limit_list().
2a0925be 1470 */
be7db6e5 1471 if (!revs->limited) {
744f4985 1472 if (revs->max_age != -1 &&
86ab4906
JH
1473 (commit->date < revs->max_age))
1474 continue;
cc0e6c5a
AR
1475 if (add_parents_to_list(revs, commit, &revs->commits) < 0)
1476 return NULL;
be7db6e5 1477 }
744f4985 1478
252a7c02
LT
1479 switch (simplify_commit(revs, commit)) {
1480 case commit_ignore:
8ecae9b0 1481 continue;
252a7c02
LT
1482 case commit_error:
1483 return NULL;
1484 default:
1485 return commit;
384e99a4 1486 }
765ac8ec
LT
1487 } while (revs->commits);
1488 return NULL;
1489}
d5db6c9e 1490
86ab4906
JH
1491static void gc_boundary(struct object_array *array)
1492{
1493 unsigned nr = array->nr;
1494 unsigned alloc = array->alloc;
1495 struct object_array_entry *objects = array->objects;
1496
1497 if (alloc <= nr) {
1498 unsigned i, j;
1499 for (i = j = 0; i < nr; i++) {
1500 if (objects[i].item->flags & SHOWN)
1501 continue;
1502 if (i != j)
1503 objects[j] = objects[i];
1504 j++;
1505 }
892ae6bf 1506 for (i = j; i < nr; i++)
86ab4906
JH
1507 objects[i].item = NULL;
1508 array->nr = j;
1509 }
1510}
1511
d5db6c9e
JH
1512struct commit *get_revision(struct rev_info *revs)
1513{
1514 struct commit *c = NULL;
86ab4906
JH
1515 struct commit_list *l;
1516
1517 if (revs->boundary == 2) {
1518 unsigned i;
1519 struct object_array *array = &revs->boundary_commits;
1520 struct object_array_entry *objects = array->objects;
1521 for (i = 0; i < array->nr; i++) {
1522 c = (struct commit *)(objects[i].item);
1523 if (!c)
1524 continue;
1525 if (!(c->object.flags & CHILD_SHOWN))
1526 continue;
1527 if (!(c->object.flags & SHOWN))
1528 break;
9c5e66e9 1529 }
86ab4906 1530 if (array->nr <= i)
9c5e66e9 1531 return NULL;
86ab4906
JH
1532
1533 c->object.flags |= SHOWN | BOUNDARY;
9c5e66e9
JS
1534 return c;
1535 }
1536
86ab4906 1537 if (revs->reverse) {
c33d8593
JH
1538 int limit = -1;
1539
1540 if (0 <= revs->max_count) {
1541 limit = revs->max_count;
1542 if (0 < revs->skip_count)
1543 limit += revs->skip_count;
1544 }
86ab4906 1545 l = NULL;
c33d8593 1546 while ((c = get_revision_1(revs))) {
86ab4906 1547 commit_list_insert(c, &l);
c33d8593
JH
1548 if ((0 < limit) && !--limit)
1549 break;
1550 }
86ab4906
JH
1551 revs->commits = l;
1552 revs->reverse = 0;
c33d8593 1553 revs->max_count = -1;
2b064697 1554 c = NULL;
d5db6c9e
JH
1555 }
1556
86ab4906
JH
1557 /*
1558 * Now pick up what they want to give us
1559 */
8839ac94
JH
1560 c = get_revision_1(revs);
1561 if (c) {
1562 while (0 < revs->skip_count) {
1563 revs->skip_count--;
1564 c = get_revision_1(revs);
1565 if (!c)
1566 break;
1567 }
86ab4906
JH
1568 }
1569
1570 /*
1571 * Check the max_count.
1572 */
d5db6c9e
JH
1573 switch (revs->max_count) {
1574 case -1:
1575 break;
1576 case 0:
86ab4906
JH
1577 c = NULL;
1578 break;
d5db6c9e
JH
1579 default:
1580 revs->max_count--;
1581 }
86ab4906 1582
c33d8593
JH
1583 if (c)
1584 c->object.flags |= SHOWN;
1585
1586 if (!revs->boundary) {
d5db6c9e 1587 return c;
c33d8593 1588 }
86ab4906
JH
1589
1590 if (!c) {
1591 /*
1592 * get_revision_1() runs out the commits, and
1593 * we are done computing the boundaries.
1594 * switch to boundary commits output mode.
1595 */
1596 revs->boundary = 2;
1597 return get_revision(revs);
1598 }
1599
1600 /*
1601 * boundary commits are the commits that are parents of the
1602 * ones we got from get_revision_1() but they themselves are
1603 * not returned from get_revision_1(). Before returning
1604 * 'c', we need to mark its parents that they could be boundaries.
1605 */
1606
1607 for (l = c->parents; l; l = l->next) {
1608 struct object *p;
1609 p = &(l->item->object);
c33d8593 1610 if (p->flags & (CHILD_SHOWN | SHOWN))
86ab4906
JH
1611 continue;
1612 p->flags |= CHILD_SHOWN;
1613 gc_boundary(&revs->boundary_commits);
1614 add_object_array(p, NULL, &revs->boundary_commits);
1615 }
1616
1617 return c;
d5db6c9e 1618}