Automatic merge of /home/torvalds/junkio/.git/
[git/git.git] / fsck-cache.c
CommitLineData
20222118
LT
1#include <sys/types.h>
2#include <dirent.h>
3
4b182421 4#include "cache.h"
ff5ebe39
DB
5#include "commit.h"
6#include "tree.h"
7#include "blob.h"
c418eda4 8#include "tag.h"
ff5ebe39
DB
9
10#define REACHABLE 0x0001
d9839e03 11
ab7df187 12static int show_root = 0;
889262ea 13static int show_tags = 0;
d9839e03 14static int show_unreachable = 0;
d9839e03
LT
15static unsigned char head_sha1[20];
16
8ba0bbb2
LT
17static void check_connectivity(void)
18{
19 int i;
20
8ba0bbb2 21 /* Look up all the requirements, warn about missing objects.. */
ff5ebe39
DB
22 for (i = 0; i < nr_objs; i++) {
23 struct object *obj = objs[i];
c418eda4 24 struct object_list *refs;
8ba0bbb2 25
3a6a23e6
LT
26 if (!obj->parsed) {
27 printf("missing %s %s\n", obj->type, sha1_to_hex(obj->sha1));
28 continue;
29 }
30
31 for (refs = obj->refs; refs; refs = refs->next) {
32 if (refs->item->parsed)
33 continue;
34 printf("broken link from %7s %s\n",
35 obj->type, sha1_to_hex(obj->sha1));
36 printf(" to %7s %s\n",
aa034134 37 refs->item->type, sha1_to_hex(refs->item->sha1));
3a6a23e6
LT
38 }
39
40 /* Don't bother with tag reachability. */
41 if (obj->type == tag_type)
42 continue;
43
ff5ebe39 44 if (show_unreachable && !(obj->flags & REACHABLE)) {
f43b8abc 45 printf("unreachable %s %s\n", obj->type, sha1_to_hex(obj->sha1));
8ba0bbb2 46 continue;
d9839e03 47 }
8ba0bbb2 48
ff5ebe39
DB
49 if (!obj->used) {
50 printf("dangling %s %s\n", obj->type,
51 sha1_to_hex(obj->sha1));
d9839e03 52 }
8ba0bbb2
LT
53 }
54}
55
85003492
LT
56/*
57 * The entries in a tree are ordered in the _path_ order,
58 * which means that a directory entry is ordered by adding
59 * a slash to the end of it.
60 *
61 * So a directory called "a" is ordered _after_ a file
62 * called "a.c", because "a/" sorts after "a.c".
63 */
64static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
65{
66 int len1 = strlen(a->name);
67 int len2 = strlen(b->name);
68 int len = len1 < len2 ? len1 : len2;
69 unsigned char c1, c2;
70 int cmp;
71
72 cmp = memcmp(a->name, b->name, len);
73 if (cmp < 0)
74 return 0;
75 if (cmp > 0)
76 return -1;
77
78 /*
79 * Ok, the first <len> characters are the same.
80 * Now we need to order the next one, but turn
81 * a '\0' into a '/' for a directory entry.
82 */
83 c1 = a->name[len];
84 c2 = b->name[len];
85 if (!c1 && a->directory)
86 c1 = '/';
87 if (!c2 && b->directory)
88 c2 = '/';
89 return c1 < c2 ? 0 : -1;
90}
91
c418eda4 92static int fsck_tree(struct tree *item)
20222118 93{
85003492
LT
94 int has_full_path = 0;
95 struct tree_entry_list *entry, *last;
96
97 last = NULL;
98 for (entry = item->entries; entry; entry = entry->next) {
99 if (strchr(entry->name, '/'))
100 has_full_path = 1;
101
102 if (last) {
103 if (verify_ordered(last, entry) < 0) {
104 fprintf(stderr, "tree %s not ordered\n",
105 sha1_to_hex(item->object.sha1));
106 return -1;
107 }
108 }
109
110 last = entry;
111 }
112
113 if (has_full_path) {
ff5ebe39 114 fprintf(stderr, "warning: fsck-cache: tree %s "
c418eda4
DB
115 "has full pathnames in it\n",
116 sha1_to_hex(item->object.sha1));
1ea34e36 117 }
85003492 118
59c1e249 119 return 0;
1ea34e36
LT
120}
121
c418eda4 122static int fsck_commit(struct commit *commit)
1ea34e36 123{
ff5ebe39 124 if (!commit->tree)
1ea34e36 125 return -1;
ab7df187 126 if (!commit->parents && show_root)
c418eda4 127 printf("root %s\n", sha1_to_hex(commit->object.sha1));
e6948b6d 128 if (!commit->date)
c418eda4
DB
129 printf("bad commit date in %s\n",
130 sha1_to_hex(commit->object.sha1));
4728b861
LT
131 return 0;
132}
133
c418eda4 134static int fsck_tag(struct tag *tag)
ec4465ad 135{
889262ea
LT
136 if (!show_tags)
137 return 0;
138
c418eda4
DB
139 printf("tagged %s %s",
140 tag->tagged->type,
141 sha1_to_hex(tag->tagged->sha1));
142 printf(" (%s) in %s\n",
143 tag->tag, sha1_to_hex(tag->object.sha1));
ff5ebe39 144 return 0;
20222118
LT
145}
146
7e8c174a 147static int fsck_sha1(unsigned char *sha1)
20222118 148{
7e8c174a
LT
149 struct object *obj = parse_object(sha1);
150 if (!obj)
151 return -1;
152 if (obj->type == blob_type)
153 return 0;
154 if (obj->type == tree_type)
155 return fsck_tree((struct tree *) obj);
156 if (obj->type == commit_type)
157 return fsck_commit((struct commit *) obj);
158 if (obj->type == tag_type)
159 return fsck_tag((struct tag *) obj);
160 return -1;
161}
162
163/*
164 * This is the sorting chunk size: make it reasonably
165 * big so that we can sort well..
166 */
167#define MAX_SHA1_ENTRIES (1024)
168
169struct sha1_entry {
170 unsigned long ino;
20222118 171 unsigned char sha1[20];
7e8c174a
LT
172};
173
174static struct {
175 unsigned long nr;
176 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
177} sha1_list;
178
179static int ino_compare(const void *_a, const void *_b)
180{
181 const struct sha1_entry *a = _a, *b = _b;
182 unsigned long ino1 = a->ino, ino2 = b->ino;
183 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
184}
185
186static void fsck_sha1_list(void)
187{
188 int i, nr = sha1_list.nr;
189
190 qsort(sha1_list.entry, nr, sizeof(struct sha1_entry *), ino_compare);
191 for (i = 0; i < nr; i++) {
192 struct sha1_entry *entry = sha1_list.entry[i];
193 unsigned char *sha1 = entry->sha1;
194
195 sha1_list.entry[i] = NULL;
196 if (fsck_sha1(sha1) < 0)
197 fprintf(stderr, "bad sha1 entry '%s'\n", sha1_to_hex(sha1));
198 free(entry);
20222118 199 }
7e8c174a
LT
200 sha1_list.nr = 0;
201}
202
203static void add_sha1_list(unsigned char *sha1, unsigned long ino)
204{
205 struct sha1_entry *entry = xmalloc(sizeof(*entry));
206 int nr;
207
208 entry->ino = ino;
209 memcpy(entry->sha1, sha1, 20);
210 nr = sha1_list.nr;
211 if (nr == MAX_SHA1_ENTRIES) {
212 fsck_sha1_list();
213 nr = 0;
214 }
215 sha1_list.entry[nr] = entry;
216 sha1_list.nr = ++nr;
20222118
LT
217}
218
219static int fsck_dir(int i, char *path)
220{
221 DIR *dir = opendir(path);
222 struct dirent *de;
223
224 if (!dir) {
2de381f9 225 return error("missing sha1 directory '%s'", path);
20222118
LT
226 }
227
228 while ((de = readdir(dir)) != NULL) {
229 char name[100];
7e8c174a 230 unsigned char sha1[20];
20222118
LT
231 int len = strlen(de->d_name);
232
233 switch (len) {
234 case 2:
235 if (de->d_name[1] != '.')
236 break;
237 case 1:
238 if (de->d_name[0] != '.')
239 break;
240 continue;
241 case 38:
242 sprintf(name, "%02x", i);
243 memcpy(name+2, de->d_name, len+1);
7e8c174a
LT
244 if (get_sha1_hex(name, sha1) < 0)
245 break;
246 add_sha1_list(sha1, de->d_ino);
247 continue;
20222118
LT
248 }
249 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
250 }
251 closedir(dir);
252 return 0;
253}
254
255int main(int argc, char **argv)
256{
bcee6fd8 257 int i, heads;
20222118
LT
258 char *sha1_dir;
259
889262ea
LT
260 for (i = 1; i < argc; i++) {
261 const char *arg = argv[i];
262
263 if (!strcmp(arg, "--unreachable")) {
264 show_unreachable = 1;
265 continue;
266 }
267 if (!strcmp(arg, "--tags")) {
268 show_tags = 1;
269 continue;
270 }
ab7df187
LT
271 if (!strcmp(arg, "--root")) {
272 show_root = 1;
273 continue;
274 }
889262ea
LT
275 if (*arg == '-')
276 usage("fsck-cache [--tags] [[--unreachable] <head-sha1>*]");
277 }
278
bcee6fd8
LT
279 sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
280 for (i = 0; i < 256; i++) {
281 static char dir[4096];
282 sprintf(dir, "%s/%02x", sha1_dir, i);
283 fsck_dir(i, dir);
284 }
7e8c174a 285 fsck_sha1_list();
bcee6fd8
LT
286
287 heads = 0;
d9839e03 288 for (i = 1; i < argc; i++) {
889262ea
LT
289 const char *arg = argv[i];
290
291 if (*arg == '-')
d9839e03 292 continue;
889262ea 293
3c249c95 294 if (!get_sha1(arg, head_sha1)) {
e1a1388d
JF
295 struct commit *commit = lookup_commit(head_sha1);
296 struct object *obj;
297
298 /* Error is printed by lookup_commit(). */
299 if (!commit)
300 continue;
301
302 obj = &commit->object;
ff5ebe39
DB
303 obj->used = 1;
304 mark_reachable(obj, REACHABLE);
bcee6fd8 305 heads++;
d9839e03
LT
306 continue;
307 }
889262ea 308 error("expected sha1, got %s", arg);
d9839e03 309 }
d9839e03 310
bcee6fd8
LT
311 if (!heads) {
312 if (show_unreachable) {
313 fprintf(stderr, "unable to do reachability without a head\n");
314 show_unreachable = 0;
315 }
316 fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
20222118 317 }
bcee6fd8 318
8ba0bbb2 319 check_connectivity();
20222118
LT
320 return 0;
321}