Merge branch 'jc/fsck' into next
[git] / revision.c
1 #include "cache.h"
2 #include "tag.h"
3 #include "blob.h"
4 #include "tree.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "refs.h"
8 #include "revision.h"
9
10 static 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
34 struct 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
47 static void mark_blob_uninteresting(struct blob *blob)
48 {
49         if (blob->object.flags & UNINTERESTING)
50                 return;
51         blob->object.flags |= UNINTERESTING;
52 }
53
54 void mark_tree_uninteresting(struct tree *tree)
55 {
56         struct object *obj = &tree->object;
57         struct tree_entry_list *entry;
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));
66         entry = tree->entries;
67         tree->entries = NULL;
68         while (entry) {
69                 struct tree_entry_list *next = entry->next;
70                 if (entry->directory)
71                         mark_tree_uninteresting(entry->item.tree);
72                 else
73                         mark_blob_uninteresting(entry->item.blob);
74                 free(entry);
75                 entry = next;
76         }
77 }
78
79 void mark_parents_uninteresting(struct commit *commit)
80 {
81         struct commit_list *parents = commit->parents;
82
83         while (parents) {
84                 struct commit *commit = parents->item;
85                 if (!(commit->object.flags & UNINTERESTING)) {
86                         commit->object.flags |= UNINTERESTING;
87
88                         /*
89                          * Normally we haven't parsed the parent
90                          * yet, so we won't have a parent of a parent
91                          * here. However, it may turn out that we've
92                          * reached this commit some other way (where it
93                          * wasn't uninteresting), in which case we need
94                          * to mark its parents recursively too..
95                          */
96                         if (commit->parents)
97                                 mark_parents_uninteresting(commit);
98                 }
99
100                 /*
101                  * A missing commit is ok iff its parent is marked
102                  * uninteresting.
103                  *
104                  * We just mark such a thing parsed, so that when
105                  * it is popped next time around, we won't be trying
106                  * to parse it and get an error.
107                  */
108                 if (!has_sha1_file(commit->object.sha1))
109                         commit->object.parsed = 1;
110                 parents = parents->next;
111         }
112 }
113
114 static void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
115 {
116         add_object(obj, &revs->pending_objects, NULL, name);
117 }
118
119 static struct commit *get_commit_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
120 {
121         struct object *object;
122
123         object = parse_object(sha1);
124         if (!object)
125                 die("bad object %s", name);
126
127         /*
128          * Tag object? Look what it points to..
129          */
130         while (object->type == tag_type) {
131                 struct tag *tag = (struct tag *) object;
132                 object->flags |= flags;
133                 if (revs->tag_objects && !(object->flags & UNINTERESTING))
134                         add_pending_object(revs, object, tag->tag);
135                 object = parse_object(tag->tagged->sha1);
136                 if (!object)
137                         die("bad object %s", sha1_to_hex(tag->tagged->sha1));
138         }
139
140         /*
141          * Commit object? Just return it, we'll do all the complex
142          * reachability crud.
143          */
144         if (object->type == commit_type) {
145                 struct commit *commit = (struct commit *)object;
146                 object->flags |= flags;
147                 if (parse_commit(commit) < 0)
148                         die("unable to parse commit %s", name);
149                 if (flags & UNINTERESTING) {
150                         mark_parents_uninteresting(commit);
151                         revs->limited = 1;
152                 }
153                 return commit;
154         }
155
156         /*
157          * Tree object? Either mark it uniniteresting, or add it
158          * to the list of objects to look at later..
159          */
160         if (object->type == tree_type) {
161                 struct tree *tree = (struct tree *)object;
162                 if (!revs->tree_objects)
163                         return NULL;
164                 if (flags & UNINTERESTING) {
165                         mark_tree_uninteresting(tree);
166                         return NULL;
167                 }
168                 add_pending_object(revs, object, "");
169                 return NULL;
170         }
171
172         /*
173          * Blob object? You know the drill by now..
174          */
175         if (object->type == blob_type) {
176                 struct blob *blob = (struct blob *)object;
177                 if (!revs->blob_objects)
178                         return NULL;
179                 if (flags & UNINTERESTING) {
180                         mark_blob_uninteresting(blob);
181                         return NULL;
182                 }
183                 add_pending_object(revs, object, "");
184                 return NULL;
185         }
186         die("%s is unknown object", name);
187 }
188
189 static int everybody_uninteresting(struct commit_list *orig)
190 {
191         struct commit_list *list = orig;
192         while (list) {
193                 struct commit *commit = list->item;
194                 list = list->next;
195                 if (commit->object.flags & UNINTERESTING)
196                         continue;
197                 return 0;
198         }
199         return 1;
200 }
201
202 #define TREE_SAME       0
203 #define TREE_NEW        1
204 #define TREE_DIFFERENT  2
205 static int tree_difference = TREE_SAME;
206
207 static void file_add_remove(struct diff_options *options,
208                     int addremove, unsigned mode,
209                     const unsigned char *sha1,
210                     const char *base, const char *path)
211 {
212         int diff = TREE_DIFFERENT;
213
214         /*
215          * Is it an add of a new file? It means that
216          * the old tree didn't have it at all, so we
217          * will turn "TREE_SAME" -> "TREE_NEW", but
218          * leave any "TREE_DIFFERENT" alone (and if
219          * it already was "TREE_NEW", we'll keep it
220          * "TREE_NEW" of course).
221          */
222         if (addremove == '+') {
223                 diff = tree_difference;
224                 if (diff != TREE_SAME)
225                         return;
226                 diff = TREE_NEW;
227         }
228         tree_difference = diff;
229 }
230
231 static void file_change(struct diff_options *options,
232                  unsigned old_mode, unsigned new_mode,
233                  const unsigned char *old_sha1,
234                  const unsigned char *new_sha1,
235                  const char *base, const char *path)
236 {
237         tree_difference = TREE_DIFFERENT;
238 }
239
240 static struct diff_options diff_opt = {
241         .recursive = 1,
242         .add_remove = file_add_remove,
243         .change = file_change,
244 };
245
246 static int compare_tree(struct tree *t1, struct tree *t2)
247 {
248         if (!t1)
249                 return TREE_NEW;
250         if (!t2)
251                 return TREE_DIFFERENT;
252         tree_difference = TREE_SAME;
253         if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
254                 return TREE_DIFFERENT;
255         return tree_difference;
256 }
257
258 static int same_tree_as_empty(struct tree *t1)
259 {
260         int retval;
261         void *tree;
262         struct tree_desc empty, real;
263
264         if (!t1)
265                 return 0;
266
267         tree = read_object_with_reference(t1->object.sha1, "tree", &real.size, NULL);
268         if (!tree)
269                 return 0;
270         real.buf = tree;
271
272         empty.buf = "";
273         empty.size = 0;
274
275         tree_difference = 0;
276         retval = diff_tree(&empty, &real, "", &diff_opt);
277         free(tree);
278
279         return retval >= 0 && !tree_difference;
280 }
281
282 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
283 {
284         struct commit_list **pp, *parent;
285
286         if (!commit->tree)
287                 return;
288
289         if (!commit->parents) {
290                 if (!same_tree_as_empty(commit->tree))
291                         commit->object.flags |= TREECHANGE;
292                 return;
293         }
294
295         pp = &commit->parents;
296         while ((parent = *pp) != NULL) {
297                 struct commit *p = parent->item;
298
299                 if (p->object.flags & UNINTERESTING) {
300                         pp = &parent->next;
301                         continue;
302                 }
303
304                 parse_commit(p);
305                 switch (compare_tree(p->tree, commit->tree)) {
306                 case TREE_SAME:
307                         parent->next = NULL;
308                         commit->parents = parent;
309                         return;
310
311                 case TREE_NEW:
312                         if (revs->remove_empty_trees && same_tree_as_empty(p->tree)) {
313                                 *pp = parent->next;
314                                 continue;
315                         }
316                 /* fallthrough */
317                 case TREE_DIFFERENT:
318                         pp = &parent->next;
319                         continue;
320                 }
321                 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
322         }
323         commit->object.flags |= TREECHANGE;
324 }
325
326 static void add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
327 {
328         struct commit_list *parent = commit->parents;
329
330         /*
331          * If the commit is uninteresting, don't try to
332          * prune parents - we want the maximal uninteresting
333          * set.
334          *
335          * Normally we haven't parsed the parent
336          * yet, so we won't have a parent of a parent
337          * here. However, it may turn out that we've
338          * reached this commit some other way (where it
339          * wasn't uninteresting), in which case we need
340          * to mark its parents recursively too..
341          */
342         if (commit->object.flags & UNINTERESTING) {
343                 while (parent) {
344                         struct commit *p = parent->item;
345                         parent = parent->next;
346                         parse_commit(p);
347                         p->object.flags |= UNINTERESTING;
348                         if (p->parents)
349                                 mark_parents_uninteresting(p);
350                         if (p->object.flags & SEEN)
351                                 continue;
352                         p->object.flags |= SEEN;
353                         insert_by_date(p, list);
354                 }
355                 return;
356         }
357
358         /*
359          * Ok, the commit wasn't uninteresting. Try to
360          * simplify the commit history and find the parent
361          * that has no differences in the path set if one exists.
362          */
363         if (revs->paths)
364                 try_to_simplify_commit(revs, commit);
365
366         parent = commit->parents;
367         while (parent) {
368                 struct commit *p = parent->item;
369
370                 parent = parent->next;
371
372                 parse_commit(p);
373                 if (p->object.flags & SEEN)
374                         continue;
375                 p->object.flags |= SEEN;
376                 insert_by_date(p, list);
377         }
378 }
379
380 static void limit_list(struct rev_info *revs)
381 {
382         struct commit_list *list = revs->commits;
383         struct commit_list *newlist = NULL;
384         struct commit_list **p = &newlist;
385
386         if (revs->paths)
387                 diff_tree_setup_paths(revs->paths);
388
389         while (list) {
390                 struct commit_list *entry = list;
391                 struct commit *commit = list->item;
392                 struct object *obj = &commit->object;
393
394                 list = list->next;
395                 free(entry);
396
397                 if (revs->max_age != -1 && (commit->date < revs->max_age))
398                         obj->flags |= UNINTERESTING;
399                 if (revs->unpacked && has_sha1_pack(obj->sha1))
400                         obj->flags |= UNINTERESTING;
401                 add_parents_to_list(revs, commit, &list);
402                 if (obj->flags & UNINTERESTING) {
403                         mark_parents_uninteresting(commit);
404                         if (everybody_uninteresting(list))
405                                 break;
406                         continue;
407                 }
408                 if (revs->min_age != -1 && (commit->date > revs->min_age))
409                         continue;
410                 p = &commit_list_insert(commit, p)->next;
411         }
412         revs->commits = newlist;
413 }
414
415 static void add_one_commit(struct commit *commit, struct rev_info *revs)
416 {
417         if (!commit || (commit->object.flags & SEEN))
418                 return;
419         commit->object.flags |= SEEN;
420         commit_list_insert(commit, &revs->commits);
421 }
422
423 static int all_flags;
424 static struct rev_info *all_revs;
425
426 static int handle_one_ref(const char *path, const unsigned char *sha1)
427 {
428         struct commit *commit = get_commit_reference(all_revs, path, sha1, all_flags);
429         add_one_commit(commit, all_revs);
430         return 0;
431 }
432
433 static void handle_all(struct rev_info *revs, unsigned flags)
434 {
435         all_revs = revs;
436         all_flags = flags;
437         for_each_ref(handle_one_ref);
438 }
439
440 /*
441  * Parse revision information, filling in the "rev_info" structure,
442  * and removing the used arguments from the argument list.
443  *
444  * Returns the number of arguments left that weren't recognized
445  * (which are also moved to the head of the argument list)
446  */
447 int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
448 {
449         int i, flags, seen_dashdash;
450         const char **unrecognized = argv + 1;
451         int left = 1;
452
453         memset(revs, 0, sizeof(*revs));
454         revs->lifo = 1;
455         revs->dense = 1;
456         revs->prefix = setup_git_directory();
457         revs->max_age = -1;
458         revs->min_age = -1;
459         revs->max_count = -1;
460
461         /* First, search for "--" */
462         seen_dashdash = 0;
463         for (i = 1; i < argc; i++) {
464                 const char *arg = argv[i];
465                 if (strcmp(arg, "--"))
466                         continue;
467                 argv[i] = NULL;
468                 argc = i;
469                 revs->paths = get_pathspec(revs->prefix, argv + i + 1);
470                 seen_dashdash = 1;
471                 break;
472         }
473
474         flags = 0;
475         for (i = 1; i < argc; i++) {
476                 struct commit *commit;
477                 const char *arg = argv[i];
478                 unsigned char sha1[20];
479                 char *dotdot;
480                 int local_flags;
481
482                 if (*arg == '-') {
483                         if (!strncmp(arg, "--max-count=", 12)) {
484                                 revs->max_count = atoi(arg + 12);
485                                 continue;
486                         }
487                         /* accept -<digit>, like traditilnal "head" */
488                         if ((*arg == '-') && isdigit(arg[1])) {
489                                 revs->max_count = atoi(arg + 1);
490                                 continue;
491                         }
492                         if (!strcmp(arg, "-n")) {
493                                 if (argc <= i + 1)
494                                         die("-n requires an argument");
495                                 revs->max_count = atoi(argv[++i]);
496                                 continue;
497                         }
498                         if (!strncmp(arg,"-n",2)) {
499                                 revs->max_count = atoi(arg + 2);
500                                 continue;
501                         }
502                         if (!strncmp(arg, "--max-age=", 10)) {
503                                 revs->max_age = atoi(arg + 10);
504                                 revs->limited = 1;
505                                 continue;
506                         }
507                         if (!strncmp(arg, "--min-age=", 10)) {
508                                 revs->min_age = atoi(arg + 10);
509                                 revs->limited = 1;
510                                 continue;
511                         }
512                         if (!strncmp(arg, "--since=", 8)) {
513                                 revs->max_age = approxidate(arg + 8);
514                                 revs->limited = 1;
515                                 continue;
516                         }
517                         if (!strncmp(arg, "--after=", 8)) {
518                                 revs->max_age = approxidate(arg + 8);
519                                 revs->limited = 1;
520                                 continue;
521                         }
522                         if (!strncmp(arg, "--before=", 9)) {
523                                 revs->min_age = approxidate(arg + 9);
524                                 revs->limited = 1;
525                                 continue;
526                         }
527                         if (!strncmp(arg, "--until=", 8)) {
528                                 revs->min_age = approxidate(arg + 8);
529                                 revs->limited = 1;
530                                 continue;
531                         }
532                         if (!strcmp(arg, "--all")) {
533                                 handle_all(revs, flags);
534                                 continue;
535                         }
536                         if (!strcmp(arg, "--not")) {
537                                 flags ^= UNINTERESTING;
538                                 continue;
539                         }
540                         if (!strcmp(arg, "--default")) {
541                                 if (++i >= argc)
542                                         die("bad --default argument");
543                                 def = argv[i];
544                                 continue;
545                         }
546                         if (!strcmp(arg, "--topo-order")) {
547                                 revs->topo_order = 1;
548                                 revs->limited = 1;
549                                 continue;
550                         }
551                         if (!strcmp(arg, "--date-order")) {
552                                 revs->lifo = 0;
553                                 revs->topo_order = 1;
554                                 revs->limited = 1;
555                                 continue;
556                         }
557                         if (!strcmp(arg, "--dense")) {
558                                 revs->dense = 1;
559                                 continue;
560                         }
561                         if (!strcmp(arg, "--sparse")) {
562                                 revs->dense = 0;
563                                 continue;
564                         }
565                         if (!strcmp(arg, "--remove-empty")) {
566                                 revs->remove_empty_trees = 1;
567                                 continue;
568                         }
569                         if (!strncmp(arg, "--no-merges", 11)) {
570                                 revs->no_merges = 1;
571                                 continue;
572                         }
573                         if (!strcmp(arg, "--objects")) {
574                                 revs->tag_objects = 1;
575                                 revs->tree_objects = 1;
576                                 revs->blob_objects = 1;
577                                 continue;
578                         }
579                         if (!strcmp(arg, "--objects-edge")) {
580                                 revs->tag_objects = 1;
581                                 revs->tree_objects = 1;
582                                 revs->blob_objects = 1;
583                                 revs->edge_hint = 1;
584                                 continue;
585                         }
586                         if (!strcmp(arg, "--unpacked")) {
587                                 revs->unpacked = 1;
588                                 revs->limited = 1;
589                                 continue;
590                         }
591                         *unrecognized++ = arg;
592                         left++;
593                         continue;
594                 }
595                 dotdot = strstr(arg, "..");
596                 if (dotdot) {
597                         unsigned char from_sha1[20];
598                         char *next = dotdot + 2;
599                         *dotdot = 0;
600                         if (!*next)
601                                 next = "HEAD";
602                         if (!get_sha1(arg, from_sha1) && !get_sha1(next, sha1)) {
603                                 struct commit *exclude;
604                                 struct commit *include;
605
606                                 exclude = get_commit_reference(revs, arg, from_sha1, flags ^ UNINTERESTING);
607                                 include = get_commit_reference(revs, next, sha1, flags);
608                                 if (!exclude || !include)
609                                         die("Invalid revision range %s..%s", arg, next);
610                                 add_one_commit(exclude, revs);
611                                 add_one_commit(include, revs);
612                                 continue;
613                         }
614                         *dotdot = '.';
615                 }
616                 local_flags = 0;
617                 if (*arg == '^') {
618                         local_flags = UNINTERESTING;
619                         arg++;
620                 }
621                 if (get_sha1(arg, sha1) < 0) {
622                         struct stat st;
623                         int j;
624
625                         if (seen_dashdash || local_flags)
626                                 die("bad revision '%s'", arg);
627
628                         /* If we didn't have a "--", all filenames must exist */
629                         for (j = i; j < argc; j++) {
630                                 if (lstat(argv[j], &st) < 0)
631                                         die("'%s': %s", arg, strerror(errno));
632                         }
633                         revs->paths = get_pathspec(revs->prefix, argv + i);
634                         break;
635                 }
636                 commit = get_commit_reference(revs, arg, sha1, flags ^ local_flags);
637                 add_one_commit(commit, revs);
638         }
639         if (def && !revs->commits) {
640                 unsigned char sha1[20];
641                 struct commit *commit;
642                 if (get_sha1(def, sha1) < 0)
643                         die("bad default revision '%s'", def);
644                 commit = get_commit_reference(revs, def, sha1, 0);
645                 add_one_commit(commit, revs);
646         }
647         if (revs->paths)
648                 revs->limited = 1;
649         return left;
650 }
651
652 void prepare_revision_walk(struct rev_info *revs)
653 {
654         sort_by_date(&revs->commits);
655         if (revs->limited)
656                 limit_list(revs);
657         if (revs->topo_order)
658                 sort_in_topological_order(&revs->commits, revs->lifo);
659 }
660
661 static int rewrite_one(struct commit **pp)
662 {
663         for (;;) {
664                 struct commit *p = *pp;
665                 if (p->object.flags & (TREECHANGE | UNINTERESTING))
666                         return 0;
667                 if (!p->parents)
668                         return -1;
669                 *pp = p->parents->item;
670         }
671 }
672
673 static void rewrite_parents(struct commit *commit)
674 {
675         struct commit_list **pp = &commit->parents;
676         while (*pp) {
677                 struct commit_list *parent = *pp;
678                 if (rewrite_one(&parent->item) < 0) {
679                         *pp = parent->next;
680                         continue;
681                 }
682                 pp = &parent->next;
683         }
684 }
685
686 struct commit *get_revision(struct rev_info *revs)
687 {
688         struct commit_list *list = revs->commits;
689
690         if (!list)
691                 return NULL;
692
693         /* Check the max_count ... */
694         switch (revs->max_count) {
695         case -1:
696                 break;
697         case 0:
698                 return NULL;
699         default:
700                 revs->max_count--;
701         }
702
703         do {
704                 struct commit *commit = revs->commits->item;
705
706                 if (commit->object.flags & (UNINTERESTING|SHOWN))
707                         goto next;
708                 if (revs->min_age != -1 && (commit->date > revs->min_age))
709                         goto next;
710                 if (revs->max_age != -1 && (commit->date < revs->max_age))
711                         return NULL;
712                 if (revs->no_merges && commit->parents && commit->parents->next)
713                         goto next;
714                 if (revs->paths && revs->dense) {
715                         if (!(commit->object.flags & TREECHANGE))
716                                 goto next;
717                         rewrite_parents(commit);
718                 }
719                 /* More to go? */
720                 if (revs->max_count)
721                         pop_most_recent_commit(&revs->commits, SEEN);
722                 commit->object.flags |= SHOWN;
723                 return commit;
724 next:
725                 pop_most_recent_commit(&revs->commits, SEEN);
726         } while (revs->commits);
727         return NULL;
728 }