Add "merge-tree" helper program. Maybe it's retarded, maybe it's helpful.
[git] / fsck-cache.c
1 #include "cache.h"
2
3 #include <sys/types.h>
4 #include <dirent.h>
5
6 #include "revision.h"
7
8 static int show_unreachable = 0;
9 static unsigned char head_sha1[20];
10
11 static void check_connectivity(void)
12 {
13         int i;
14
15         /* Look up all the requirements, warn about missing objects.. */
16         for (i = 0; i < nr_revs; i++) {
17                 struct revision *rev = revs[i];
18
19                 if (show_unreachable && !(rev->flags & REACHABLE)) {
20                         printf("unreachable %s\n", sha1_to_hex(rev->sha1));
21                         continue;
22                 }
23
24                 switch (rev->flags & (SEEN | USED)) {
25                 case 0:
26                         printf("bad %s\n", sha1_to_hex(rev->sha1));
27                         break;
28                 case USED:
29                         printf("missing %s\n", sha1_to_hex(rev->sha1));
30                         break;
31                 case SEEN:
32                         printf("dangling %s\n", sha1_to_hex(rev->sha1));
33                         break;
34                 }
35         }
36 }
37
38 static void mark_needs_sha1(unsigned char *parent, const char * tag, unsigned char *child)
39 {
40         struct revision * child_rev = add_relationship(lookup_rev(parent), child);
41         child_rev->flags |= USED;
42 }
43
44 static int mark_sha1_seen(unsigned char *sha1, char *tag)
45 {
46         struct revision *rev = lookup_rev(sha1);
47
48         rev->flags |= SEEN;
49         return 0;
50 }
51
52 static int fsck_tree(unsigned char *sha1, void *data, unsigned long size)
53 {
54         int warn_old_tree = 1;
55
56         while (size) {
57                 int len = 1+strlen(data);
58                 unsigned char *file_sha1 = data + len;
59                 char *path = strchr(data, ' ');
60                 unsigned int mode;
61                 if (size < len + 20 || !path || sscanf(data, "%o", &mode) != 1)
62                         return -1;
63
64                 /* Warn about trees that don't do the recursive thing.. */
65                 if (warn_old_tree && strchr(path, '/')) {
66                         fprintf(stderr, "warning: fsck-cache: tree %s has full pathnames in it\n", sha1_to_hex(sha1));
67                         warn_old_tree = 0;
68                 }
69
70                 data += len + 20;
71                 size -= len + 20;
72                 mark_needs_sha1(sha1, S_ISDIR(mode) ? "tree" : "blob", file_sha1);
73         }
74         return 0;
75 }
76
77 static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
78 {
79         int parents;
80         unsigned char tree_sha1[20];
81         unsigned char parent_sha1[20];
82
83         if (memcmp(data, "tree ", 5))
84                 return -1;
85         if (get_sha1_hex(data + 5, tree_sha1) < 0)
86                 return -1;
87         mark_needs_sha1(sha1, "tree", tree_sha1);
88         data += 5 + 40 + 1;     /* "tree " + <hex sha1> + '\n' */
89         parents = 0;
90         while (!memcmp(data, "parent ", 7)) {
91                 if (get_sha1_hex(data + 7, parent_sha1) < 0)
92                         return -1;
93                 mark_needs_sha1(sha1, "commit", parent_sha1);
94                 data += 7 + 40 + 1;     /* "parent " + <hex sha1> + '\n' */
95                 parents++;
96         }
97         if (!parents)
98                 printf("root %s\n", sha1_to_hex(sha1));
99         return 0;
100 }
101
102 static int fsck_entry(unsigned char *sha1, char *tag, void *data, unsigned long size)
103 {
104         if (!strcmp(tag, "blob")) {
105                 /* Nothing to check */;
106         } else if (!strcmp(tag, "tree")) {
107                 if (fsck_tree(sha1, data, size) < 0)
108                         return -1;
109         } else if (!strcmp(tag, "commit")) {
110                 if (fsck_commit(sha1, data, size) < 0)
111                         return -1;
112         } else
113                 return -1;
114         return mark_sha1_seen(sha1, tag);
115 }
116
117 static int fsck_name(char *hex)
118 {
119         unsigned char sha1[20];
120         if (!get_sha1_hex(hex, sha1)) {
121                 unsigned long mapsize;
122                 void *map = map_sha1_file(sha1, &mapsize);
123                 if (map) {
124                         char type[100];
125                         unsigned long size;
126                         void *buffer = NULL;
127                         if (!check_sha1_signature(sha1, map, mapsize))
128                                 buffer = unpack_sha1_file(map, mapsize, type, &size);
129                         munmap(map, mapsize);
130                         if (buffer && !fsck_entry(sha1, type, buffer, size))
131                                 return 0;
132                 }
133         }
134         return -1;
135 }
136
137 static int fsck_dir(int i, char *path)
138 {
139         DIR *dir = opendir(path);
140         struct dirent *de;
141
142         if (!dir) {
143                 return error("missing sha1 directory '%s'", path);
144         }
145
146         while ((de = readdir(dir)) != NULL) {
147                 char name[100];
148                 int len = strlen(de->d_name);
149
150                 switch (len) {
151                 case 2:
152                         if (de->d_name[1] != '.')
153                                 break;
154                 case 1:
155                         if (de->d_name[0] != '.')
156                                 break;
157                         continue;
158                 case 38:
159                         sprintf(name, "%02x", i);
160                         memcpy(name+2, de->d_name, len+1);
161                         if (!fsck_name(name))
162                                 continue;
163                 }
164                 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
165         }
166         closedir(dir);
167         return 0;
168 }
169
170 int main(int argc, char **argv)
171 {
172         int i, heads;
173         char *sha1_dir;
174
175         sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
176         for (i = 0; i < 256; i++) {
177                 static char dir[4096];
178                 sprintf(dir, "%s/%02x", sha1_dir, i);
179                 fsck_dir(i, dir);
180         }
181
182         heads = 0;
183         for (i = 1; i < argc; i++) {
184                 if (!strcmp(argv[i], "--unreachable")) {
185                         show_unreachable = 1;
186                         continue;
187                 }
188                 if (!get_sha1_hex(argv[i], head_sha1)) {
189                         mark_reachable(lookup_rev(head_sha1));
190                         heads++;
191                         continue;
192                 }
193                 error("fsck-cache [[--unreachable] <head-sha1>*]");
194         }
195
196         if (!heads) {
197                 if (show_unreachable) {
198                         fprintf(stderr, "unable to do reachability without a head\n");
199                         show_unreachable = 0; 
200                 }
201                 fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
202         }
203
204         check_connectivity();
205         return 0;
206 }