]> git.ipfire.org Git - thirdparty/git.git/blame - revision.c
Merge branch 'jc/lt-tree-n-cache-tree' into lt/tree-2
[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"
9
10static char *path_name(struct name_path *path, const char *name)
11{
12 struct name_path *p;
13 char *n, *m;
14 int nlen = strlen(name);
15 int len = nlen + 1;
16
17 for (p = path; p; p = p->up) {
18 if (p->elem_len)
19 len += p->elem_len + 1;
20 }
21 n = xmalloc(len);
22 m = n + len - (nlen + 1);
23 strcpy(m, name);
24 for (p = path; p; p = p->up) {
25 if (p->elem_len) {
26 m -= p->elem_len + 1;
27 memcpy(m, p->elem, p->elem_len);
28 m[p->elem_len] = '/';
29 }
30 }
31 return n;
32}
33
34struct object_list **add_object(struct object *obj,
35 struct object_list **p,
36 struct name_path *path,
37 const char *name)
38{
39 struct object_list *entry = xmalloc(sizeof(*entry));
40 entry->item = obj;
41 entry->next = *p;
42 entry->name = path_name(path, name);
43 *p = entry;
44 return &entry->next;
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;
ae563542 57 struct object *obj = &tree->object;
ae563542
LT
58
59 if (obj->flags & UNINTERESTING)
60 return;
61 obj->flags |= UNINTERESTING;
62 if (!has_sha1_file(obj->sha1))
63 return;
64 if (parse_tree(tree) < 0)
65 die("bad tree %s", sha1_to_hex(obj->sha1));
f75e53ed
LT
66
67 desc.buf = tree->buffer;
68 desc.size = tree->size;
69 while (desc.size) {
70 unsigned mode;
71 const char *name;
72 const unsigned char *sha1;
73
74 sha1 = tree_entry_extract(&desc, &name, &mode);
75 update_tree_entry(&desc);
76
77 if (S_ISDIR(mode))
78 mark_tree_uninteresting(lookup_tree(sha1));
ae563542 79 else
f75e53ed 80 mark_blob_uninteresting(lookup_blob(sha1));
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
126static void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
127{
128 add_object(obj, &revs->pending_objects, NULL, name);
129}
130
cd2bdc53 131static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
ae563542
LT
132{
133 struct object *object;
134
135 object = parse_object(sha1);
136 if (!object)
137 die("bad object %s", name);
cd2bdc53
LT
138 object->flags |= flags;
139 return object;
140}
141
142static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
143{
144 unsigned long flags = object->flags;
ae563542
LT
145
146 /*
147 * Tag object? Look what it points to..
148 */
149 while (object->type == tag_type) {
150 struct tag *tag = (struct tag *) object;
cd2bdc53 151 if (revs->tag_objects && !(flags & UNINTERESTING))
ae563542
LT
152 add_pending_object(revs, object, tag->tag);
153 object = parse_object(tag->tagged->sha1);
154 if (!object)
155 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
156 }
157
158 /*
159 * Commit object? Just return it, we'll do all the complex
160 * reachability crud.
161 */
162 if (object->type == commit_type) {
163 struct commit *commit = (struct commit *)object;
ae563542
LT
164 if (parse_commit(commit) < 0)
165 die("unable to parse commit %s", name);
d9a83684 166 if (flags & UNINTERESTING) {
4262c1b0 167 commit->object.flags |= UNINTERESTING;
ae563542 168 mark_parents_uninteresting(commit);
d9a83684
LT
169 revs->limited = 1;
170 }
ae563542
LT
171 return commit;
172 }
173
174 /*
175 * Tree object? Either mark it uniniteresting, or add it
176 * to the list of objects to look at later..
177 */
178 if (object->type == tree_type) {
179 struct tree *tree = (struct tree *)object;
180 if (!revs->tree_objects)
181 return NULL;
182 if (flags & UNINTERESTING) {
183 mark_tree_uninteresting(tree);
184 return NULL;
185 }
186 add_pending_object(revs, object, "");
187 return NULL;
188 }
189
190 /*
191 * Blob object? You know the drill by now..
192 */
193 if (object->type == blob_type) {
194 struct blob *blob = (struct blob *)object;
195 if (!revs->blob_objects)
196 return NULL;
197 if (flags & UNINTERESTING) {
198 mark_blob_uninteresting(blob);
199 return NULL;
200 }
201 add_pending_object(revs, object, "");
202 return NULL;
203 }
204 die("%s is unknown object", name);
205}
206
a4a88b2b
LT
207static int everybody_uninteresting(struct commit_list *orig)
208{
209 struct commit_list *list = orig;
210 while (list) {
211 struct commit *commit = list->item;
212 list = list->next;
213 if (commit->object.flags & UNINTERESTING)
214 continue;
215 return 0;
216 }
217 return 1;
218}
219
8efdc326 220static int tree_difference = REV_TREE_SAME;
a4a88b2b
LT
221
222static void file_add_remove(struct diff_options *options,
223 int addremove, unsigned mode,
224 const unsigned char *sha1,
225 const char *base, const char *path)
226{
8efdc326 227 int diff = REV_TREE_DIFFERENT;
a4a88b2b
LT
228
229 /*
8efdc326
FK
230 * Is it an add of a new file? It means that the old tree
231 * didn't have it at all, so we will turn "REV_TREE_SAME" ->
232 * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
233 * (and if it already was "REV_TREE_NEW", we'll keep it
234 * "REV_TREE_NEW" of course).
a4a88b2b
LT
235 */
236 if (addremove == '+') {
237 diff = tree_difference;
8efdc326 238 if (diff != REV_TREE_SAME)
a4a88b2b 239 return;
8efdc326 240 diff = REV_TREE_NEW;
a4a88b2b
LT
241 }
242 tree_difference = diff;
243}
244
245static void file_change(struct diff_options *options,
246 unsigned old_mode, unsigned new_mode,
247 const unsigned char *old_sha1,
248 const unsigned char *new_sha1,
249 const char *base, const char *path)
250{
8efdc326 251 tree_difference = REV_TREE_DIFFERENT;
a4a88b2b
LT
252}
253
c4e05b1a 254int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
a4a88b2b
LT
255{
256 if (!t1)
8efdc326 257 return REV_TREE_NEW;
a4a88b2b 258 if (!t2)
8efdc326
FK
259 return REV_TREE_DIFFERENT;
260 tree_difference = REV_TREE_SAME;
c4e05b1a 261 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
cd2bdc53 262 &revs->pruning) < 0)
8efdc326 263 return REV_TREE_DIFFERENT;
a4a88b2b
LT
264 return tree_difference;
265}
266
c4e05b1a 267int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
a4a88b2b
LT
268{
269 int retval;
270 void *tree;
271 struct tree_desc empty, real;
272
273 if (!t1)
274 return 0;
275
8e440259 276 tree = read_object_with_reference(t1->object.sha1, tree_type, &real.size, NULL);
a4a88b2b
LT
277 if (!tree)
278 return 0;
279 real.buf = tree;
280
281 empty.buf = "";
282 empty.size = 0;
283
284 tree_difference = 0;
cd2bdc53 285 retval = diff_tree(&empty, &real, "", &revs->pruning);
a4a88b2b
LT
286 free(tree);
287
288 return retval >= 0 && !tree_difference;
289}
290
291static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
292{
293 struct commit_list **pp, *parent;
f3219fbb 294 int tree_changed = 0;
a4a88b2b
LT
295
296 if (!commit->tree)
297 return;
298
299 if (!commit->parents) {
c4e05b1a 300 if (!rev_same_tree_as_empty(revs, commit->tree))
a4a88b2b
LT
301 commit->object.flags |= TREECHANGE;
302 return;
303 }
304
305 pp = &commit->parents;
306 while ((parent = *pp) != NULL) {
307 struct commit *p = parent->item;
308
a4a88b2b 309 parse_commit(p);
c4e05b1a 310 switch (rev_compare_tree(revs, p->tree, commit->tree)) {
8efdc326 311 case REV_TREE_SAME:
f3219fbb
JH
312 if (p->object.flags & UNINTERESTING) {
313 /* Even if a merge with an uninteresting
314 * side branch brought the entire change
315 * we are interested in, we do not want
316 * to lose the other branches of this
317 * merge, so we just keep going.
318 */
319 pp = &parent->next;
320 continue;
321 }
a4a88b2b
LT
322 parent->next = NULL;
323 commit->parents = parent;
324 return;
325
8efdc326
FK
326 case REV_TREE_NEW:
327 if (revs->remove_empty_trees &&
c4e05b1a 328 rev_same_tree_as_empty(revs, p->tree)) {
c348f31a
JH
329 /* We are adding all the specified
330 * paths from this parent, so the
331 * history beyond this parent is not
332 * interesting. Remove its parents
333 * (they are grandparents for us).
334 * IOW, we pretend this parent is a
335 * "root" commit.
a41e109c 336 */
c348f31a
JH
337 parse_commit(p);
338 p->parents = NULL;
a4a88b2b
LT
339 }
340 /* fallthrough */
8efdc326 341 case REV_TREE_DIFFERENT:
f3219fbb 342 tree_changed = 1;
a4a88b2b
LT
343 pp = &parent->next;
344 continue;
345 }
346 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
347 }
f3219fbb
JH
348 if (tree_changed)
349 commit->object.flags |= TREECHANGE;
a4a88b2b
LT
350}
351
352static void add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
353{
354 struct commit_list *parent = commit->parents;
355
3381c790
LT
356 if (commit->object.flags & ADDED)
357 return;
358 commit->object.flags |= ADDED;
359
a4a88b2b
LT
360 /*
361 * If the commit is uninteresting, don't try to
362 * prune parents - we want the maximal uninteresting
363 * set.
364 *
365 * Normally we haven't parsed the parent
366 * yet, so we won't have a parent of a parent
367 * here. However, it may turn out that we've
368 * reached this commit some other way (where it
369 * wasn't uninteresting), in which case we need
370 * to mark its parents recursively too..
371 */
372 if (commit->object.flags & UNINTERESTING) {
373 while (parent) {
374 struct commit *p = parent->item;
375 parent = parent->next;
376 parse_commit(p);
377 p->object.flags |= UNINTERESTING;
378 if (p->parents)
379 mark_parents_uninteresting(p);
380 if (p->object.flags & SEEN)
381 continue;
382 p->object.flags |= SEEN;
383 insert_by_date(p, list);
384 }
385 return;
386 }
387
388 /*
389 * Ok, the commit wasn't uninteresting. Try to
390 * simplify the commit history and find the parent
391 * that has no differences in the path set if one exists.
392 */
8efdc326
FK
393 if (revs->prune_fn)
394 revs->prune_fn(revs, commit);
a4a88b2b 395
ba1d4505
LT
396 if (revs->no_walk)
397 return;
398
a4a88b2b
LT
399 parent = commit->parents;
400 while (parent) {
401 struct commit *p = parent->item;
402
403 parent = parent->next;
404
405 parse_commit(p);
406 if (p->object.flags & SEEN)
407 continue;
408 p->object.flags |= SEEN;
409 insert_by_date(p, list);
410 }
411}
412
413static void limit_list(struct rev_info *revs)
414{
415 struct commit_list *list = revs->commits;
416 struct commit_list *newlist = NULL;
417 struct commit_list **p = &newlist;
418
419 while (list) {
420 struct commit_list *entry = list;
421 struct commit *commit = list->item;
422 struct object *obj = &commit->object;
423
424 list = list->next;
425 free(entry);
426
427 if (revs->max_age != -1 && (commit->date < revs->max_age))
428 obj->flags |= UNINTERESTING;
429 if (revs->unpacked && has_sha1_pack(obj->sha1))
430 obj->flags |= UNINTERESTING;
431 add_parents_to_list(revs, commit, &list);
432 if (obj->flags & UNINTERESTING) {
433 mark_parents_uninteresting(commit);
434 if (everybody_uninteresting(list))
435 break;
436 continue;
437 }
438 if (revs->min_age != -1 && (commit->date > revs->min_age))
439 continue;
440 p = &commit_list_insert(commit, p)->next;
441 }
384e99a4 442 if (revs->boundary) {
4c0fea0f
JH
443 /* mark the ones that are on the result list first */
444 for (list = newlist; list; list = list->next) {
445 struct commit *commit = list->item;
446 commit->object.flags |= TMP_MARK;
447 }
448 for (list = newlist; list; list = list->next) {
384e99a4
JH
449 struct commit *commit = list->item;
450 struct object *obj = &commit->object;
4c0fea0f
JH
451 struct commit_list *parent;
452 if (obj->flags & UNINTERESTING)
384e99a4 453 continue;
4c0fea0f
JH
454 for (parent = commit->parents;
455 parent;
456 parent = parent->next) {
384e99a4 457 struct commit *pcommit = parent->item;
384e99a4
JH
458 if (!(pcommit->object.flags & UNINTERESTING))
459 continue;
460 pcommit->object.flags |= BOUNDARY;
4c0fea0f
JH
461 if (pcommit->object.flags & TMP_MARK)
462 continue;
463 pcommit->object.flags |= TMP_MARK;
384e99a4
JH
464 p = &commit_list_insert(pcommit, p)->next;
465 }
4c0fea0f
JH
466 }
467 for (list = newlist; list; list = list->next) {
468 struct commit *commit = list->item;
469 commit->object.flags &= ~TMP_MARK;
384e99a4
JH
470 }
471 }
a4a88b2b
LT
472 revs->commits = newlist;
473}
474
ae563542
LT
475static int all_flags;
476static struct rev_info *all_revs;
477
478static int handle_one_ref(const char *path, const unsigned char *sha1)
479{
cd2bdc53
LT
480 struct object *object = get_reference(all_revs, path, sha1, all_flags);
481 add_pending_object(all_revs, object, "");
ae563542
LT
482 return 0;
483}
484
485static void handle_all(struct rev_info *revs, unsigned flags)
486{
487 all_revs = revs;
488 all_flags = flags;
489 for_each_ref(handle_one_ref);
490}
491
ea4a19e1
JH
492static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
493{
494 unsigned char sha1[20];
495 struct object *it;
496 struct commit *commit;
497 struct commit_list *parents;
498
499 if (*arg == '^') {
500 flags ^= UNINTERESTING;
501 arg++;
502 }
503 if (get_sha1(arg, sha1))
504 return 0;
505 while (1) {
506 it = get_reference(revs, arg, sha1, 0);
507 if (strcmp(it->type, tag_type))
508 break;
509 memcpy(sha1, ((struct tag*)it)->tagged->sha1, 20);
510 }
511 if (strcmp(it->type, commit_type))
512 return 0;
513 commit = (struct commit *)it;
514 for (parents = commit->parents; parents; parents = parents->next) {
515 it = &parents->item->object;
516 it->flags |= flags;
517 add_pending_object(revs, it, arg);
518 }
519 return 1;
520}
521
8efdc326
FK
522void init_revisions(struct rev_info *revs)
523{
524 memset(revs, 0, sizeof(*revs));
8e8f9987 525
6b9c58f4 526 revs->abbrev = DEFAULT_ABBREV;
8e8f9987 527 revs->ignore_merges = 1;
cd2bdc53
LT
528 revs->pruning.recursive = 1;
529 revs->pruning.add_remove = file_add_remove;
530 revs->pruning.change = file_change;
8efdc326
FK
531 revs->lifo = 1;
532 revs->dense = 1;
533 revs->prefix = setup_git_directory();
534 revs->max_age = -1;
535 revs->min_age = -1;
536 revs->max_count = -1;
537
538 revs->prune_fn = NULL;
539 revs->prune_data = NULL;
540
541 revs->topo_setter = topo_sort_default_setter;
542 revs->topo_getter = topo_sort_default_getter;
cd2bdc53 543
cd2bdc53
LT
544 revs->commit_format = CMIT_FMT_DEFAULT;
545
546 diff_setup(&revs->diffopt);
8efdc326
FK
547}
548
ae563542
LT
549/*
550 * Parse revision information, filling in the "rev_info" structure,
551 * and removing the used arguments from the argument list.
552 *
765ac8ec
LT
553 * Returns the number of arguments left that weren't recognized
554 * (which are also moved to the head of the argument list)
ae563542 555 */
a4a88b2b 556int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
ae563542
LT
557{
558 int i, flags, seen_dashdash;
765ac8ec 559 const char **unrecognized = argv + 1;
ae563542
LT
560 int left = 1;
561
ae563542
LT
562 /* First, search for "--" */
563 seen_dashdash = 0;
564 for (i = 1; i < argc; i++) {
565 const char *arg = argv[i];
566 if (strcmp(arg, "--"))
567 continue;
568 argv[i] = NULL;
569 argc = i;
8efdc326 570 revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
ae563542
LT
571 seen_dashdash = 1;
572 break;
573 }
574
575 flags = 0;
576 for (i = 1; i < argc; i++) {
cd2bdc53 577 struct object *object;
ae563542
LT
578 const char *arg = argv[i];
579 unsigned char sha1[20];
580 char *dotdot;
581 int local_flags;
582
583 if (*arg == '-') {
cd2bdc53 584 int opts;
ae563542
LT
585 if (!strncmp(arg, "--max-count=", 12)) {
586 revs->max_count = atoi(arg + 12);
587 continue;
588 }
de5f2bf3 589 /* accept -<digit>, like traditional "head" */
64bc6e3d
JH
590 if ((*arg == '-') && isdigit(arg[1])) {
591 revs->max_count = atoi(arg + 1);
592 continue;
593 }
594 if (!strcmp(arg, "-n")) {
595 if (argc <= i + 1)
596 die("-n requires an argument");
597 revs->max_count = atoi(argv[++i]);
598 continue;
599 }
600 if (!strncmp(arg,"-n",2)) {
601 revs->max_count = atoi(arg + 2);
602 continue;
603 }
ae563542
LT
604 if (!strncmp(arg, "--max-age=", 10)) {
605 revs->max_age = atoi(arg + 10);
ae563542
LT
606 continue;
607 }
fd751667
JH
608 if (!strncmp(arg, "--since=", 8)) {
609 revs->max_age = approxidate(arg + 8);
fd751667
JH
610 continue;
611 }
612 if (!strncmp(arg, "--after=", 8)) {
613 revs->max_age = approxidate(arg + 8);
53069686
JH
614 continue;
615 }
616 if (!strncmp(arg, "--min-age=", 10)) {
617 revs->min_age = atoi(arg + 10);
fd751667
JH
618 continue;
619 }
620 if (!strncmp(arg, "--before=", 9)) {
621 revs->min_age = approxidate(arg + 9);
fd751667
JH
622 continue;
623 }
624 if (!strncmp(arg, "--until=", 8)) {
625 revs->min_age = approxidate(arg + 8);
fd751667
JH
626 continue;
627 }
ae563542
LT
628 if (!strcmp(arg, "--all")) {
629 handle_all(revs, flags);
630 continue;
631 }
632 if (!strcmp(arg, "--not")) {
633 flags ^= UNINTERESTING;
634 continue;
635 }
636 if (!strcmp(arg, "--default")) {
637 if (++i >= argc)
638 die("bad --default argument");
639 def = argv[i];
640 continue;
641 }
642 if (!strcmp(arg, "--topo-order")) {
643 revs->topo_order = 1;
644 continue;
645 }
646 if (!strcmp(arg, "--date-order")) {
647 revs->lifo = 0;
648 revs->topo_order = 1;
649 continue;
650 }
7b0c9966
LT
651 if (!strcmp(arg, "--parents")) {
652 revs->parents = 1;
653 continue;
654 }
ae563542
LT
655 if (!strcmp(arg, "--dense")) {
656 revs->dense = 1;
657 continue;
658 }
659 if (!strcmp(arg, "--sparse")) {
660 revs->dense = 0;
661 continue;
662 }
663 if (!strcmp(arg, "--remove-empty")) {
664 revs->remove_empty_trees = 1;
665 continue;
666 }
5cdeae71 667 if (!strcmp(arg, "--no-merges")) {
765ac8ec
LT
668 revs->no_merges = 1;
669 continue;
670 }
384e99a4
JH
671 if (!strcmp(arg, "--boundary")) {
672 revs->boundary = 1;
673 continue;
674 }
ae563542
LT
675 if (!strcmp(arg, "--objects")) {
676 revs->tag_objects = 1;
677 revs->tree_objects = 1;
678 revs->blob_objects = 1;
679 continue;
680 }
681 if (!strcmp(arg, "--objects-edge")) {
682 revs->tag_objects = 1;
683 revs->tree_objects = 1;
684 revs->blob_objects = 1;
685 revs->edge_hint = 1;
686 continue;
687 }
d9a83684
LT
688 if (!strcmp(arg, "--unpacked")) {
689 revs->unpacked = 1;
d9a83684
LT
690 continue;
691 }
cd2bdc53
LT
692 if (!strcmp(arg, "-r")) {
693 revs->diff = 1;
694 revs->diffopt.recursive = 1;
695 continue;
696 }
697 if (!strcmp(arg, "-t")) {
698 revs->diff = 1;
699 revs->diffopt.recursive = 1;
700 revs->diffopt.tree_in_recursive = 1;
701 continue;
702 }
703 if (!strcmp(arg, "-m")) {
704 revs->ignore_merges = 0;
705 continue;
706 }
707 if (!strcmp(arg, "-c")) {
708 revs->diff = 1;
0fe7c1de 709 revs->dense_combined_merges = 0;
cd2bdc53
LT
710 revs->combine_merges = 1;
711 continue;
712 }
713 if (!strcmp(arg, "--cc")) {
714 revs->diff = 1;
715 revs->dense_combined_merges = 1;
716 revs->combine_merges = 1;
717 continue;
718 }
719 if (!strcmp(arg, "-v")) {
720 revs->verbose_header = 1;
cd2bdc53
LT
721 continue;
722 }
723 if (!strncmp(arg, "--pretty", 8)) {
724 revs->verbose_header = 1;
cd2bdc53
LT
725 revs->commit_format = get_commit_format(arg+8);
726 continue;
727 }
728 if (!strcmp(arg, "--root")) {
729 revs->show_root_diff = 1;
730 continue;
731 }
732 if (!strcmp(arg, "--no-commit-id")) {
733 revs->no_commit_id = 1;
734 continue;
735 }
736 if (!strcmp(arg, "--always")) {
737 revs->always_show_header = 1;
738 continue;
739 }
740 if (!strcmp(arg, "--no-abbrev")) {
741 revs->abbrev = 0;
742 continue;
743 }
744 if (!strcmp(arg, "--abbrev")) {
745 revs->abbrev = DEFAULT_ABBREV;
746 continue;
747 }
508d9e37
LT
748 if (!strncmp(arg, "--abbrev=", 9)) {
749 revs->abbrev = strtoul(arg + 9, NULL, 10);
750 if (revs->abbrev < MINIMUM_ABBREV)
751 revs->abbrev = MINIMUM_ABBREV;
752 else if (revs->abbrev > 40)
753 revs->abbrev = 40;
754 continue;
755 }
cd2bdc53
LT
756 if (!strcmp(arg, "--abbrev-commit")) {
757 revs->abbrev_commit = 1;
758 continue;
759 }
760 if (!strcmp(arg, "--full-diff")) {
761 revs->diff = 1;
762 revs->full_diff = 1;
763 continue;
764 }
765 opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
766 if (opts > 0) {
767 revs->diff = 1;
768 i += opts - 1;
769 continue;
770 }
ae563542
LT
771 *unrecognized++ = arg;
772 left++;
773 continue;
774 }
775 dotdot = strstr(arg, "..");
776 if (dotdot) {
777 unsigned char from_sha1[20];
0c8b106b
JH
778 const char *next = dotdot + 2;
779 const char *this = arg;
ae563542
LT
780 *dotdot = 0;
781 if (!*next)
0c8b106b 782 next = "HEAD";
ce4a7063 783 if (dotdot == arg)
0c8b106b 784 this = "HEAD";
ce4a7063
JH
785 if (!get_sha1(this, from_sha1) &&
786 !get_sha1(next, sha1)) {
cd2bdc53
LT
787 struct object *exclude;
788 struct object *include;
ae563542 789
cd2bdc53
LT
790 exclude = get_reference(revs, this, from_sha1, flags ^ UNINTERESTING);
791 include = get_reference(revs, next, sha1, flags);
ae563542
LT
792 if (!exclude || !include)
793 die("Invalid revision range %s..%s", arg, next);
ea92f41f
JH
794
795 if (!seen_dashdash) {
796 *dotdot = '.';
797 verify_non_filename(revs->prefix, arg);
798 }
cd2bdc53
LT
799 add_pending_object(revs, exclude, this);
800 add_pending_object(revs, include, next);
ae563542
LT
801 continue;
802 }
803 *dotdot = '.';
804 }
ea4a19e1
JH
805 dotdot = strstr(arg, "^@");
806 if (dotdot && !dotdot[2]) {
807 *dotdot = 0;
808 if (add_parents_only(revs, arg, flags))
809 continue;
810 *dotdot = '^';
811 }
ae563542
LT
812 local_flags = 0;
813 if (*arg == '^') {
814 local_flags = UNINTERESTING;
815 arg++;
816 }
31fff305 817 if (get_sha1(arg, sha1)) {
ae563542
LT
818 int j;
819
820 if (seen_dashdash || local_flags)
821 die("bad revision '%s'", arg);
822
ea92f41f
JH
823 /* If we didn't have a "--":
824 * (1) all filenames must exist;
825 * (2) all rev-args must not be interpretable
826 * as a valid filename.
827 * but the latter we have checked in the main loop.
828 */
e23d0b4a
LT
829 for (j = i; j < argc; j++)
830 verify_filename(revs->prefix, argv[j]);
831
8efdc326 832 revs->prune_data = get_pathspec(revs->prefix, argv + i);
ae563542
LT
833 break;
834 }
ea92f41f
JH
835 if (!seen_dashdash)
836 verify_non_filename(revs->prefix, arg);
cd2bdc53
LT
837 object = get_reference(revs, arg, sha1, flags ^ local_flags);
838 add_pending_object(revs, object, arg);
ae563542 839 }
cd2bdc53 840 if (def && !revs->pending_objects) {
ae563542 841 unsigned char sha1[20];
cd2bdc53 842 struct object *object;
31fff305 843 if (get_sha1(def, sha1))
ae563542 844 die("bad default revision '%s'", def);
cd2bdc53
LT
845 object = get_reference(revs, def, sha1, 0);
846 add_pending_object(revs, object, def);
ae563542 847 }
8efdc326 848
bbbc8c3a 849 if (revs->topo_order || revs->unpacked)
53069686
JH
850 revs->limited = 1;
851
8efdc326 852 if (revs->prune_data) {
cd2bdc53 853 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
8efdc326 854 revs->prune_fn = try_to_simplify_commit;
cd2bdc53
LT
855 if (!revs->full_diff)
856 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
8efdc326 857 }
cd2bdc53
LT
858 if (revs->combine_merges) {
859 revs->ignore_merges = 0;
96ab4f4e
JH
860 if (revs->dense_combined_merges &&
861 (revs->diffopt.output_format != DIFF_FORMAT_DIFFSTAT))
cd2bdc53
LT
862 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
863 }
cd2bdc53
LT
864 revs->diffopt.abbrev = revs->abbrev;
865 diff_setup_done(&revs->diffopt);
8efdc326 866
ae563542
LT
867 return left;
868}
a4a88b2b
LT
869
870void prepare_revision_walk(struct rev_info *revs)
871{
cd2bdc53
LT
872 struct object_list *list;
873
874 list = revs->pending_objects;
875 revs->pending_objects = NULL;
876 while (list) {
877 struct commit *commit = handle_commit(revs, list->item, list->name);
878 if (commit) {
879 if (!(commit->object.flags & SEEN)) {
880 commit->object.flags |= SEEN;
881 insert_by_date(commit, &revs->commits);
882 }
883 }
884 list = list->next;
885 }
886
ba1d4505
LT
887 if (revs->no_walk)
888 return;
a4a88b2b
LT
889 if (revs->limited)
890 limit_list(revs);
891 if (revs->topo_order)
8efdc326
FK
892 sort_in_topological_order_fn(&revs->commits, revs->lifo,
893 revs->topo_setter,
894 revs->topo_getter);
a4a88b2b
LT
895}
896
3381c790 897static int rewrite_one(struct rev_info *revs, struct commit **pp)
765ac8ec
LT
898{
899 for (;;) {
900 struct commit *p = *pp;
3381c790
LT
901 if (!revs->limited)
902 add_parents_to_list(revs, p, &revs->commits);
765ac8ec
LT
903 if (p->object.flags & (TREECHANGE | UNINTERESTING))
904 return 0;
905 if (!p->parents)
906 return -1;
907 *pp = p->parents->item;
908 }
909}
910
3381c790 911static void rewrite_parents(struct rev_info *revs, struct commit *commit)
765ac8ec
LT
912{
913 struct commit_list **pp = &commit->parents;
914 while (*pp) {
915 struct commit_list *parent = *pp;
3381c790 916 if (rewrite_one(revs, &parent->item) < 0) {
765ac8ec
LT
917 *pp = parent->next;
918 continue;
919 }
920 pp = &parent->next;
921 }
922}
923
1b65a5aa
JH
924static void mark_boundary_to_show(struct commit *commit)
925{
926 struct commit_list *p = commit->parents;
927 while (p) {
928 commit = p->item;
929 p = p->next;
930 if (commit->object.flags & BOUNDARY)
931 commit->object.flags |= BOUNDARY_SHOW;
932 }
933}
934
a4a88b2b
LT
935struct commit *get_revision(struct rev_info *revs)
936{
765ac8ec 937 struct commit_list *list = revs->commits;
765ac8ec
LT
938
939 if (!list)
a4a88b2b 940 return NULL;
a4a88b2b 941
765ac8ec 942 /* Check the max_count ... */
765ac8ec
LT
943 switch (revs->max_count) {
944 case -1:
945 break;
946 case 0:
947 return NULL;
948 default:
949 revs->max_count--;
950 }
a4a88b2b 951
765ac8ec 952 do {
ea5ed3ab
LT
953 struct commit *commit = revs->commits->item;
954
2a0925be
LT
955 revs->commits = revs->commits->next;
956
957 /*
958 * If we haven't done the list limiting, we need to look at
be7db6e5
LT
959 * the parents here. We also need to do the date-based limiting
960 * that we'd otherwise have done in limit_list().
2a0925be 961 */
be7db6e5 962 if (!revs->limited) {
22c31bf1
JH
963 if ((revs->unpacked &&
964 has_sha1_pack(commit->object.sha1)) ||
965 (revs->max_age != -1 &&
966 (commit->date < revs->max_age)))
be7db6e5 967 continue;
2a0925be 968 add_parents_to_list(revs, commit, &revs->commits);
be7db6e5 969 }
384e99a4 970 if (commit->object.flags & SHOWN)
2a0925be 971 continue;
1b65a5aa
JH
972
973 /* We want to show boundary commits only when their
974 * children are shown. When path-limiter is in effect,
975 * rewrite_parents() drops some commits from getting shown,
976 * and there is no point showing boundary parents that
977 * are not shown. After rewrite_parents() rewrites the
978 * parents of a commit that is shown, we mark the boundary
979 * parents with BOUNDARY_SHOW.
980 */
981 if (commit->object.flags & BOUNDARY_SHOW) {
982 commit->object.flags |= SHOWN;
983 return commit;
984 }
985 if (commit->object.flags & UNINTERESTING)
2a0925be 986 continue;
765ac8ec 987 if (revs->min_age != -1 && (commit->date > revs->min_age))
2a0925be 988 continue;
384e99a4
JH
989 if (revs->no_merges &&
990 commit->parents && commit->parents->next)
2a0925be 991 continue;
8efdc326 992 if (revs->prune_fn && revs->dense) {
765ac8ec 993 if (!(commit->object.flags & TREECHANGE))
2a0925be
LT
994 continue;
995 if (revs->parents)
3381c790 996 rewrite_parents(revs, commit);
384e99a4 997 }
1b65a5aa
JH
998 if (revs->boundary)
999 mark_boundary_to_show(commit);
765ac8ec
LT
1000 commit->object.flags |= SHOWN;
1001 return commit;
1002 } while (revs->commits);
1003 return NULL;
1004}