]> git.ipfire.org Git - thirdparty/git.git/blame - fsck-cache.c
[PATCH] Port rev-tree to parsing functions
[thirdparty/git.git] / fsck-cache.c
CommitLineData
20222118
LT
1#include "cache.h"
2
3#include <sys/types.h>
4#include <dirent.h>
5
458754a9 6#include "revision.h"
d9839e03
LT
7
8static int show_unreachable = 0;
d9839e03
LT
9static unsigned char head_sha1[20];
10
8ba0bbb2
LT
11static void check_connectivity(void)
12{
13 int i;
14
8ba0bbb2 15 /* Look up all the requirements, warn about missing objects.. */
d9839e03
LT
16 for (i = 0; i < nr_revs; i++) {
17 struct revision *rev = revs[i];
8ba0bbb2 18
d9839e03
LT
19 if (show_unreachable && !(rev->flags & REACHABLE)) {
20 printf("unreachable %s\n", sha1_to_hex(rev->sha1));
8ba0bbb2 21 continue;
d9839e03 22 }
8ba0bbb2 23
d9839e03
LT
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 }
8ba0bbb2
LT
35 }
36}
37
38static void mark_needs_sha1(unsigned char *parent, const char * tag, unsigned char *child)
20222118 39{
d9839e03
LT
40 struct revision * child_rev = add_relationship(lookup_rev(parent), child);
41 child_rev->flags |= USED;
20222118
LT
42}
43
1ea34e36 44static int mark_sha1_seen(unsigned char *sha1, char *tag)
20222118 45{
d9839e03 46 struct revision *rev = lookup_rev(sha1);
8ba0bbb2 47
d9839e03 48 rev->flags |= SEEN;
1ea34e36 49 return 0;
20222118
LT
50}
51
1ea34e36 52static int fsck_tree(unsigned char *sha1, void *data, unsigned long size)
20222118 53{
4e6616ab
LT
54 int warn_old_tree = 1;
55
1ea34e36
LT
56 while (size) {
57 int len = 1+strlen(data);
58 unsigned char *file_sha1 = data + len;
59 char *path = strchr(data, ' ');
f768846e
LT
60 unsigned int mode;
61 if (size < len + 20 || !path || sscanf(data, "%o", &mode) != 1)
1ea34e36 62 return -1;
4e6616ab
LT
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
1ea34e36
LT
70 data += len + 20;
71 size -= len + 20;
f768846e 72 mark_needs_sha1(sha1, S_ISDIR(mode) ? "tree" : "blob", file_sha1);
1ea34e36 73 }
59c1e249 74 return 0;
1ea34e36
LT
75}
76
77static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
78{
16d4d1ba 79 int parents;
1ea34e36
LT
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' */
16d4d1ba 89 parents = 0;
1ea34e36
LT
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' */
16d4d1ba 95 parents++;
1ea34e36 96 }
16d4d1ba 97 if (!parents)
d9839e03 98 printf("root %s\n", sha1_to_hex(sha1));
59c1e249 99 return 0;
20222118
LT
100}
101
102static int fsck_entry(unsigned char *sha1, char *tag, void *data, unsigned long size)
103{
1ea34e36 104 if (!strcmp(tag, "blob")) {
20222118 105 /* Nothing to check */;
1ea34e36
LT
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
20222118
LT
113 return -1;
114 return mark_sha1_seen(sha1, tag);
115}
116
117static 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
137static int fsck_dir(int i, char *path)
138{
139 DIR *dir = opendir(path);
140 struct dirent *de;
141
142 if (!dir) {
2de381f9 143 return error("missing sha1 directory '%s'", path);
20222118
LT
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
170int main(int argc, char **argv)
171{
bcee6fd8 172 int i, heads;
20222118
LT
173 char *sha1_dir;
174
bcee6fd8
LT
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;
d9839e03
LT
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)) {
01796b0e 189 mark_reachable(lookup_rev(head_sha1), REACHABLE);
bcee6fd8 190 heads++;
d9839e03
LT
191 continue;
192 }
bcee6fd8 193 error("fsck-cache [[--unreachable] <head-sha1>*]");
d9839e03 194 }
d9839e03 195
bcee6fd8
LT
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");
20222118 202 }
bcee6fd8 203
8ba0bbb2 204 check_connectivity();
20222118
LT
205 return 0;
206}