]>
Commit | Line | Data |
---|---|---|
4b182421 | 1 | #include "cache.h" |
ff5ebe39 DB |
2 | #include "commit.h" |
3 | #include "tree.h" | |
4 | #include "blob.h" | |
c418eda4 | 5 | #include "tag.h" |
944d8589 | 6 | #include "refs.h" |
f9253394 | 7 | #include "pack.h" |
53dc3f3e | 8 | #include "cache-tree.h" |
e9a95bef | 9 | #include "tree-walk.h" |
ff5ebe39 DB |
10 | |
11 | #define REACHABLE 0x0001 | |
2d9c58c6 | 12 | #define SEEN 0x0002 |
d9839e03 | 13 | |
96f1e58f DR |
14 | static int show_root; |
15 | static int show_tags; | |
16 | static int show_unreachable; | |
566842f6 | 17 | static int include_reflogs = 1; |
96f1e58f DR |
18 | static int check_full; |
19 | static int check_strict; | |
20 | static int keep_cache_objects; | |
d9839e03 | 21 | static unsigned char head_sha1[20]; |
e2b4f635 JH |
22 | static int errors_found; |
23 | #define ERROR_OBJECT 01 | |
24 | #define ERROR_REACHABLE 02 | |
d9839e03 | 25 | |
962554c6 | 26 | #ifdef NO_D_INO_IN_DIRENT |
35a730f0 JH |
27 | #define SORT_DIRENT 0 |
28 | #define DIRENT_SORT_HINT(de) 0 | |
29 | #else | |
30 | #define SORT_DIRENT 1 | |
31 | #define DIRENT_SORT_HINT(de) ((de)->d_ino) | |
32 | #endif | |
f1f0d088 PB |
33 | |
34 | static void objreport(struct object *obj, const char *severity, | |
35 | const char *err, va_list params) | |
36 | { | |
37 | fprintf(stderr, "%s in %s %s: ", | |
885a86ab | 38 | severity, typename(obj->type), sha1_to_hex(obj->sha1)); |
f1f0d088 PB |
39 | vfprintf(stderr, err, params); |
40 | fputs("\n", stderr); | |
41 | } | |
42 | ||
a7928f8e | 43 | static int objerror(struct object *obj, const char *err, ...) |
f1f0d088 PB |
44 | { |
45 | va_list params; | |
46 | va_start(params, err); | |
e2b4f635 | 47 | errors_found |= ERROR_OBJECT; |
f1f0d088 PB |
48 | objreport(obj, "error", err, params); |
49 | va_end(params); | |
50 | return -1; | |
51 | } | |
52 | ||
a7928f8e | 53 | static int objwarning(struct object *obj, const char *err, ...) |
f1f0d088 PB |
54 | { |
55 | va_list params; | |
56 | va_start(params, err); | |
57 | objreport(obj, "warning", err, params); | |
58 | va_end(params); | |
59 | return -1; | |
60 | } | |
61 | ||
18af29f2 LT |
62 | /* |
63 | * Check a single reachable object | |
64 | */ | |
65 | static void check_reachable_object(struct object *obj) | |
66 | { | |
67 | const struct object_refs *refs; | |
68 | ||
69 | /* | |
70 | * We obviously want the object to be parsed, | |
71 | * except if it was in a pack-file and we didn't | |
72 | * do a full fsck | |
73 | */ | |
74 | if (!obj->parsed) { | |
efec43c0 | 75 | if (has_sha1_pack(obj->sha1, NULL)) |
18af29f2 LT |
76 | return; /* it is in pack - forget about it */ |
77 | printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1)); | |
e2b4f635 | 78 | errors_found |= ERROR_REACHABLE; |
18af29f2 LT |
79 | return; |
80 | } | |
81 | ||
82 | /* | |
83 | * Check that everything that we try to reference is also good. | |
84 | */ | |
85 | refs = lookup_object_refs(obj); | |
86 | if (refs) { | |
87 | unsigned j; | |
88 | for (j = 0; j < refs->count; j++) { | |
89 | struct object *ref = refs->ref[j]; | |
90 | if (ref->parsed || | |
91 | (has_sha1_file(ref->sha1))) | |
92 | continue; | |
93 | printf("broken link from %7s %s\n", | |
94 | typename(obj->type), sha1_to_hex(obj->sha1)); | |
95 | printf(" to %7s %s\n", | |
96 | typename(ref->type), sha1_to_hex(ref->sha1)); | |
e2b4f635 | 97 | errors_found |= ERROR_REACHABLE; |
18af29f2 LT |
98 | } |
99 | } | |
100 | } | |
101 | ||
102 | /* | |
103 | * Check a single unreachable object | |
104 | */ | |
105 | static void check_unreachable_object(struct object *obj) | |
106 | { | |
107 | /* | |
108 | * Missing unreachable object? Ignore it. It's not like | |
109 | * we miss it (since it can't be reached), nor do we want | |
110 | * to complain about it being unreachable (since it does | |
111 | * not exist). | |
112 | */ | |
113 | if (!obj->parsed) | |
114 | return; | |
115 | ||
116 | /* | |
117 | * Unreachable object that exists? Show it if asked to, | |
118 | * since this is something that is prunable. | |
119 | */ | |
120 | if (show_unreachable) { | |
121 | printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1)); | |
122 | return; | |
123 | } | |
124 | ||
125 | /* | |
126 | * "!used" means that nothing at all points to it, including | |
3dff5379 | 127 | * other unreachable objects. In other words, it's the "tip" |
18af29f2 LT |
128 | * of some set of unreachable objects, usually a commit that |
129 | * got dropped. | |
130 | * | |
131 | * Such starting points are more interesting than some random | |
132 | * set of unreachable objects, so we show them even if the user | |
133 | * hasn't asked for _all_ unreachable objects. If you have | |
134 | * deleted a branch by mistake, this is a prime candidate to | |
135 | * start looking at, for example. | |
136 | */ | |
137 | if (!obj->used) { | |
138 | printf("dangling %s %s\n", typename(obj->type), | |
139 | sha1_to_hex(obj->sha1)); | |
140 | return; | |
141 | } | |
142 | ||
143 | /* | |
144 | * Otherwise? It's there, it's unreachable, and some other unreachable | |
145 | * object points to it. Ignore it - it's not interesting, and we showed | |
146 | * all the interesting cases above. | |
147 | */ | |
148 | } | |
149 | ||
150 | static void check_object(struct object *obj) | |
151 | { | |
152 | if (obj->flags & REACHABLE) | |
153 | check_reachable_object(obj); | |
154 | else | |
155 | check_unreachable_object(obj); | |
156 | } | |
f1f0d088 | 157 | |
8ba0bbb2 LT |
158 | static void check_connectivity(void) |
159 | { | |
fc046a75 | 160 | int i, max; |
8ba0bbb2 | 161 | |
8ba0bbb2 | 162 | /* Look up all the requirements, warn about missing objects.. */ |
fc046a75 LT |
163 | max = get_max_object_index(); |
164 | for (i = 0; i < max; i++) { | |
fc046a75 | 165 | struct object *obj = get_indexed_object(i); |
8ba0bbb2 | 166 | |
18af29f2 LT |
167 | if (obj) |
168 | check_object(obj); | |
8ba0bbb2 LT |
169 | } |
170 | } | |
171 | ||
85003492 LT |
172 | /* |
173 | * The entries in a tree are ordered in the _path_ order, | |
174 | * which means that a directory entry is ordered by adding | |
175 | * a slash to the end of it. | |
176 | * | |
177 | * So a directory called "a" is ordered _after_ a file | |
178 | * called "a.c", because "a/" sorts after "a.c". | |
179 | */ | |
a4f35a2d JH |
180 | #define TREE_UNORDERED (-1) |
181 | #define TREE_HAS_DUPS (-2) | |
182 | ||
e9a95bef | 183 | static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2) |
85003492 | 184 | { |
e9a95bef LT |
185 | int len1 = strlen(name1); |
186 | int len2 = strlen(name2); | |
85003492 LT |
187 | int len = len1 < len2 ? len1 : len2; |
188 | unsigned char c1, c2; | |
189 | int cmp; | |
190 | ||
e9a95bef | 191 | cmp = memcmp(name1, name2, len); |
85003492 LT |
192 | if (cmp < 0) |
193 | return 0; | |
194 | if (cmp > 0) | |
a4f35a2d | 195 | return TREE_UNORDERED; |
85003492 LT |
196 | |
197 | /* | |
198 | * Ok, the first <len> characters are the same. | |
199 | * Now we need to order the next one, but turn | |
200 | * a '\0' into a '/' for a directory entry. | |
201 | */ | |
e9a95bef LT |
202 | c1 = name1[len]; |
203 | c2 = name2[len]; | |
a4f35a2d JH |
204 | if (!c1 && !c2) |
205 | /* | |
206 | * git-write-tree used to write out a nonsense tree that has | |
207 | * entries with the same name, one blob and one tree. Make | |
208 | * sure we do not have duplicate entries. | |
209 | */ | |
210 | return TREE_HAS_DUPS; | |
e9a95bef | 211 | if (!c1 && S_ISDIR(mode1)) |
85003492 | 212 | c1 = '/'; |
e9a95bef | 213 | if (!c2 && S_ISDIR(mode2)) |
85003492 | 214 | c2 = '/'; |
a4f35a2d | 215 | return c1 < c2 ? 0 : TREE_UNORDERED; |
85003492 LT |
216 | } |
217 | ||
c418eda4 | 218 | static int fsck_tree(struct tree *item) |
20222118 | 219 | { |
64071805 | 220 | int retval; |
85003492 | 221 | int has_full_path = 0; |
cb2cada6 | 222 | int has_empty_name = 0; |
64071805 LT |
223 | int has_zero_pad = 0; |
224 | int has_bad_modes = 0; | |
225 | int has_dup_entries = 0; | |
226 | int not_properly_sorted = 0; | |
e9a95bef LT |
227 | struct tree_desc desc; |
228 | unsigned o_mode; | |
229 | const char *o_name; | |
230 | const unsigned char *o_sha1; | |
85003492 | 231 | |
6fda5e51 | 232 | init_tree_desc(&desc, item->buffer, item->size); |
e9a95bef LT |
233 | |
234 | o_mode = 0; | |
235 | o_name = NULL; | |
236 | o_sha1 = NULL; | |
237 | while (desc.size) { | |
238 | unsigned mode; | |
239 | const char *name; | |
240 | const unsigned char *sha1; | |
241 | ||
242 | sha1 = tree_entry_extract(&desc, &name, &mode); | |
243 | ||
244 | if (strchr(name, '/')) | |
85003492 | 245 | has_full_path = 1; |
cb2cada6 SP |
246 | if (!*name) |
247 | has_empty_name = 1; | |
6fda5e51 | 248 | has_zero_pad |= *(char *)desc.buffer == '0'; |
e9a95bef | 249 | update_tree_entry(&desc); |
85003492 | 250 | |
e9a95bef | 251 | switch (mode) { |
42ea9cb2 | 252 | /* |
e9a95bef | 253 | * Standard modes.. |
42ea9cb2 LT |
254 | */ |
255 | case S_IFREG | 0755: | |
256 | case S_IFREG | 0644: | |
257 | case S_IFLNK: | |
258 | case S_IFDIR: | |
8d9721c8 | 259 | case S_IFDIRLNK: |
42ea9cb2 LT |
260 | break; |
261 | /* | |
262 | * This is nonstandard, but we had a few of these | |
263 | * early on when we honored the full set of mode | |
264 | * bits.. | |
265 | */ | |
266 | case S_IFREG | 0664: | |
de2eb7f6 LT |
267 | if (!check_strict) |
268 | break; | |
42ea9cb2 | 269 | default: |
64071805 | 270 | has_bad_modes = 1; |
42ea9cb2 LT |
271 | } |
272 | ||
e9a95bef LT |
273 | if (o_name) { |
274 | switch (verify_ordered(o_mode, o_name, mode, name)) { | |
a4f35a2d | 275 | case TREE_UNORDERED: |
64071805 LT |
276 | not_properly_sorted = 1; |
277 | break; | |
a4f35a2d | 278 | case TREE_HAS_DUPS: |
64071805 LT |
279 | has_dup_entries = 1; |
280 | break; | |
a4f35a2d JH |
281 | default: |
282 | break; | |
85003492 LT |
283 | } |
284 | } | |
285 | ||
e9a95bef LT |
286 | o_mode = mode; |
287 | o_name = name; | |
288 | o_sha1 = sha1; | |
85003492 | 289 | } |
136f2e54 LT |
290 | free(item->buffer); |
291 | item->buffer = NULL; | |
85003492 | 292 | |
64071805 | 293 | retval = 0; |
85003492 | 294 | if (has_full_path) { |
f1f0d088 | 295 | objwarning(&item->object, "contains full pathnames"); |
1ea34e36 | 296 | } |
cb2cada6 SP |
297 | if (has_empty_name) { |
298 | objwarning(&item->object, "contains empty pathname"); | |
299 | } | |
64071805 | 300 | if (has_zero_pad) { |
f1f0d088 | 301 | objwarning(&item->object, "contains zero-padded file modes"); |
64071805 LT |
302 | } |
303 | if (has_bad_modes) { | |
f1f0d088 | 304 | objwarning(&item->object, "contains bad file modes"); |
64071805 LT |
305 | } |
306 | if (has_dup_entries) { | |
f1f0d088 | 307 | retval = objerror(&item->object, "contains duplicate file entries"); |
64071805 LT |
308 | } |
309 | if (not_properly_sorted) { | |
f1f0d088 | 310 | retval = objerror(&item->object, "not properly sorted"); |
64071805 LT |
311 | } |
312 | return retval; | |
1ea34e36 LT |
313 | } |
314 | ||
c418eda4 | 315 | static int fsck_commit(struct commit *commit) |
1ea34e36 | 316 | { |
de2eb7f6 | 317 | char *buffer = commit->buffer; |
f1f0d088 | 318 | unsigned char tree_sha1[20], sha1[20]; |
de2eb7f6 LT |
319 | |
320 | if (memcmp(buffer, "tree ", 5)) | |
f1f0d088 PB |
321 | return objerror(&commit->object, "invalid format - expected 'tree' line"); |
322 | if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n') | |
323 | return objerror(&commit->object, "invalid 'tree' line format - bad sha1"); | |
de2eb7f6 LT |
324 | buffer += 46; |
325 | while (!memcmp(buffer, "parent ", 7)) { | |
326 | if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n') | |
f1f0d088 | 327 | return objerror(&commit->object, "invalid 'parent' line format - bad sha1"); |
de2eb7f6 LT |
328 | buffer += 48; |
329 | } | |
330 | if (memcmp(buffer, "author ", 7)) | |
f1f0d088 | 331 | return objerror(&commit->object, "invalid format - expected 'author' line"); |
bd1e17e2 LT |
332 | free(commit->buffer); |
333 | commit->buffer = NULL; | |
ff5ebe39 | 334 | if (!commit->tree) |
f1f0d088 | 335 | return objerror(&commit->object, "could not load commit's tree %s", tree_sha1); |
ab7df187 | 336 | if (!commit->parents && show_root) |
c418eda4 | 337 | printf("root %s\n", sha1_to_hex(commit->object.sha1)); |
e6948b6d | 338 | if (!commit->date) |
c418eda4 DB |
339 | printf("bad commit date in %s\n", |
340 | sha1_to_hex(commit->object.sha1)); | |
4728b861 LT |
341 | return 0; |
342 | } | |
343 | ||
c418eda4 | 344 | static int fsck_tag(struct tag *tag) |
ec4465ad | 345 | { |
92d4c85d LT |
346 | struct object *tagged = tag->tagged; |
347 | ||
348 | if (!tagged) { | |
f1f0d088 | 349 | return objerror(&tag->object, "could not load tagged object"); |
92d4c85d | 350 | } |
889262ea LT |
351 | if (!show_tags) |
352 | return 0; | |
353 | ||
885a86ab | 354 | printf("tagged %s %s", typename(tagged->type), sha1_to_hex(tagged->sha1)); |
92d4c85d | 355 | printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1)); |
ff5ebe39 | 356 | return 0; |
20222118 LT |
357 | } |
358 | ||
d72308e0 | 359 | static int fsck_sha1(const unsigned char *sha1) |
20222118 | 360 | { |
7e8c174a | 361 | struct object *obj = parse_object(sha1); |
e2b4f635 JH |
362 | if (!obj) { |
363 | errors_found |= ERROR_OBJECT; | |
364 | return error("%s: object corrupt or missing", | |
365 | sha1_to_hex(sha1)); | |
366 | } | |
2d9c58c6 LT |
367 | if (obj->flags & SEEN) |
368 | return 0; | |
369 | obj->flags |= SEEN; | |
1974632c | 370 | if (obj->type == OBJ_BLOB) |
7e8c174a | 371 | return 0; |
1974632c | 372 | if (obj->type == OBJ_TREE) |
7e8c174a | 373 | return fsck_tree((struct tree *) obj); |
1974632c | 374 | if (obj->type == OBJ_COMMIT) |
7e8c174a | 375 | return fsck_commit((struct commit *) obj); |
1974632c | 376 | if (obj->type == OBJ_TAG) |
7e8c174a | 377 | return fsck_tag((struct tag *) obj); |
e2b4f635 | 378 | |
f1f0d088 | 379 | /* By now, parse_object() would've returned NULL instead. */ |
e2b4f635 JH |
380 | return objerror(obj, "unknown type '%d' (internal fsck error)", |
381 | obj->type); | |
7e8c174a LT |
382 | } |
383 | ||
384 | /* | |
385 | * This is the sorting chunk size: make it reasonably | |
386 | * big so that we can sort well.. | |
387 | */ | |
388 | #define MAX_SHA1_ENTRIES (1024) | |
389 | ||
390 | struct sha1_entry { | |
391 | unsigned long ino; | |
20222118 | 392 | unsigned char sha1[20]; |
7e8c174a LT |
393 | }; |
394 | ||
395 | static struct { | |
396 | unsigned long nr; | |
397 | struct sha1_entry *entry[MAX_SHA1_ENTRIES]; | |
398 | } sha1_list; | |
399 | ||
400 | static int ino_compare(const void *_a, const void *_b) | |
401 | { | |
402 | const struct sha1_entry *a = _a, *b = _b; | |
403 | unsigned long ino1 = a->ino, ino2 = b->ino; | |
404 | return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0; | |
405 | } | |
406 | ||
407 | static void fsck_sha1_list(void) | |
408 | { | |
409 | int i, nr = sha1_list.nr; | |
410 | ||
35a730f0 JH |
411 | if (SORT_DIRENT) |
412 | qsort(sha1_list.entry, nr, | |
413 | sizeof(struct sha1_entry *), ino_compare); | |
7e8c174a LT |
414 | for (i = 0; i < nr; i++) { |
415 | struct sha1_entry *entry = sha1_list.entry[i]; | |
416 | unsigned char *sha1 = entry->sha1; | |
417 | ||
418 | sha1_list.entry[i] = NULL; | |
f1f0d088 | 419 | fsck_sha1(sha1); |
7e8c174a | 420 | free(entry); |
20222118 | 421 | } |
7e8c174a LT |
422 | sha1_list.nr = 0; |
423 | } | |
424 | ||
425 | static void add_sha1_list(unsigned char *sha1, unsigned long ino) | |
426 | { | |
427 | struct sha1_entry *entry = xmalloc(sizeof(*entry)); | |
428 | int nr; | |
429 | ||
430 | entry->ino = ino; | |
e702496e | 431 | hashcpy(entry->sha1, sha1); |
7e8c174a LT |
432 | nr = sha1_list.nr; |
433 | if (nr == MAX_SHA1_ENTRIES) { | |
434 | fsck_sha1_list(); | |
435 | nr = 0; | |
436 | } | |
437 | sha1_list.entry[nr] = entry; | |
438 | sha1_list.nr = ++nr; | |
20222118 LT |
439 | } |
440 | ||
b5524c82 | 441 | static void fsck_dir(int i, char *path) |
20222118 LT |
442 | { |
443 | DIR *dir = opendir(path); | |
444 | struct dirent *de; | |
445 | ||
230f1322 | 446 | if (!dir) |
b5524c82 | 447 | return; |
20222118 LT |
448 | |
449 | while ((de = readdir(dir)) != NULL) { | |
450 | char name[100]; | |
7e8c174a | 451 | unsigned char sha1[20]; |
20222118 LT |
452 | int len = strlen(de->d_name); |
453 | ||
454 | switch (len) { | |
455 | case 2: | |
456 | if (de->d_name[1] != '.') | |
457 | break; | |
458 | case 1: | |
459 | if (de->d_name[0] != '.') | |
460 | break; | |
461 | continue; | |
462 | case 38: | |
463 | sprintf(name, "%02x", i); | |
464 | memcpy(name+2, de->d_name, len+1); | |
7e8c174a LT |
465 | if (get_sha1_hex(name, sha1) < 0) |
466 | break; | |
35a730f0 | 467 | add_sha1_list(sha1, DIRENT_SORT_HINT(de)); |
7e8c174a | 468 | continue; |
20222118 LT |
469 | } |
470 | fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name); | |
471 | } | |
472 | closedir(dir); | |
20222118 LT |
473 | } |
474 | ||
96f1e58f | 475 | static int default_refs; |
944d8589 | 476 | |
883d60fa JS |
477 | static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1, |
478 | const char *email, unsigned long timestamp, int tz, | |
479 | const char *message, void *cb_data) | |
55dd5526 JH |
480 | { |
481 | struct object *obj; | |
482 | ||
483 | if (!is_null_sha1(osha1)) { | |
484 | obj = lookup_object(osha1); | |
485 | if (obj) { | |
486 | obj->used = 1; | |
487 | mark_reachable(obj, REACHABLE); | |
488 | } | |
489 | } | |
490 | obj = lookup_object(nsha1); | |
491 | if (obj) { | |
492 | obj->used = 1; | |
493 | mark_reachable(obj, REACHABLE); | |
494 | } | |
495 | return 0; | |
496 | } | |
497 | ||
eb8381c8 NP |
498 | static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data) |
499 | { | |
500 | for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL); | |
501 | return 0; | |
502 | } | |
503 | ||
8da19775 | 504 | static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
1024932f | 505 | { |
1024932f LT |
506 | struct object *obj; |
507 | ||
1024932f | 508 | obj = lookup_object(sha1); |
8a498a05 | 509 | if (!obj) { |
7aaa715d | 510 | if (has_sha1_file(sha1)) { |
659cacf5 | 511 | default_refs++; |
944d8589 | 512 | return 0; /* it is in a pack */ |
659cacf5 | 513 | } |
944d8589 LT |
514 | error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1)); |
515 | /* We'll continue with the rest despite the error.. */ | |
516 | return 0; | |
8a498a05 | 517 | } |
944d8589 | 518 | default_refs++; |
1024932f LT |
519 | obj->used = 1; |
520 | mark_reachable(obj, REACHABLE); | |
55dd5526 | 521 | |
7c4d07c7 | 522 | return 0; |
1024932f LT |
523 | } |
524 | ||
1024932f LT |
525 | static void get_default_heads(void) |
526 | { | |
cb5d709f | 527 | for_each_ref(fsck_handle_ref, NULL); |
566842f6 SP |
528 | if (include_reflogs) |
529 | for_each_reflog(fsck_handle_reflog, NULL); | |
071fa89e LT |
530 | |
531 | /* | |
532 | * Not having any default heads isn't really fatal, but | |
533 | * it does mean that "--unreachable" no longer makes any | |
534 | * sense (since in this case everything will obviously | |
535 | * be unreachable by definition. | |
536 | * | |
537 | * Showing dangling objects is valid, though (as those | |
538 | * dangling objects are likely lost heads). | |
539 | * | |
540 | * So we just print a warning about it, and clear the | |
541 | * "show_unreachable" flag. | |
542 | */ | |
543 | if (!default_refs) { | |
8eb2d0be | 544 | fprintf(stderr, "notice: No default references\n"); |
071fa89e LT |
545 | show_unreachable = 0; |
546 | } | |
1024932f LT |
547 | } |
548 | ||
8a498a05 JH |
549 | static void fsck_object_dir(const char *path) |
550 | { | |
551 | int i; | |
552 | for (i = 0; i < 256; i++) { | |
553 | static char dir[4096]; | |
554 | sprintf(dir, "%s/%02x", path, i); | |
555 | fsck_dir(i, dir); | |
556 | } | |
557 | fsck_sha1_list(); | |
558 | } | |
559 | ||
c3330383 LT |
560 | static int fsck_head_link(void) |
561 | { | |
c3330383 | 562 | unsigned char sha1[20]; |
8da19775 | 563 | int flag; |
8eb2d0be JH |
564 | int null_is_error = 0; |
565 | const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag); | |
566 | ||
567 | if (!head_points_at) | |
568 | return error("Invalid HEAD"); | |
569 | if (!strcmp(head_points_at, "HEAD")) | |
570 | /* detached HEAD */ | |
571 | null_is_error = 1; | |
572 | else if (prefixcmp(head_points_at, "refs/heads/")) | |
8098a178 | 573 | return error("HEAD points to something strange (%s)", |
5b10b091 | 574 | head_points_at); |
8eb2d0be JH |
575 | if (is_null_sha1(sha1)) { |
576 | if (null_is_error) | |
577 | return error("HEAD: detached HEAD points at nothing"); | |
578 | fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n", | |
579 | head_points_at + 11); | |
580 | } | |
c3330383 LT |
581 | return 0; |
582 | } | |
583 | ||
53dc3f3e JH |
584 | static int fsck_cache_tree(struct cache_tree *it) |
585 | { | |
586 | int i; | |
587 | int err = 0; | |
588 | ||
589 | if (0 <= it->entry_count) { | |
590 | struct object *obj = parse_object(it->sha1); | |
6d60bbef JH |
591 | if (!obj) { |
592 | error("%s: invalid sha1 pointer in cache-tree", | |
593 | sha1_to_hex(it->sha1)); | |
594 | return 1; | |
595 | } | |
cdc08b33 JH |
596 | mark_reachable(obj, REACHABLE); |
597 | obj->used = 1; | |
1974632c | 598 | if (obj->type != OBJ_TREE) |
53dc3f3e JH |
599 | err |= objerror(obj, "non-tree in cache-tree"); |
600 | } | |
601 | for (i = 0; i < it->subtree_nr; i++) | |
602 | err |= fsck_cache_tree(it->down[i]->cache_tree); | |
603 | return err; | |
604 | } | |
605 | ||
e2b4f635 JH |
606 | static const char fsck_usage[] = |
607 | "git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] " | |
608 | "[--strict] <head-sha1>*]"; | |
609 | ||
b4dfefe0 | 610 | int cmd_fsck(int argc, char **argv, const char *prefix) |
20222118 | 611 | { |
bcee6fd8 | 612 | int i, heads; |
20222118 | 613 | |
3a7c352b | 614 | track_object_refs = 1; |
e2b4f635 | 615 | errors_found = 0; |
61e2b015 | 616 | |
889262ea LT |
617 | for (i = 1; i < argc; i++) { |
618 | const char *arg = argv[i]; | |
619 | ||
620 | if (!strcmp(arg, "--unreachable")) { | |
621 | show_unreachable = 1; | |
622 | continue; | |
623 | } | |
624 | if (!strcmp(arg, "--tags")) { | |
625 | show_tags = 1; | |
626 | continue; | |
627 | } | |
ab7df187 LT |
628 | if (!strcmp(arg, "--root")) { |
629 | show_root = 1; | |
630 | continue; | |
631 | } | |
ae7c0c92 JH |
632 | if (!strcmp(arg, "--cache")) { |
633 | keep_cache_objects = 1; | |
634 | continue; | |
635 | } | |
566842f6 SP |
636 | if (!strcmp(arg, "--no-reflogs")) { |
637 | include_reflogs = 0; | |
638 | continue; | |
639 | } | |
8a498a05 JH |
640 | if (!strcmp(arg, "--full")) { |
641 | check_full = 1; | |
642 | continue; | |
643 | } | |
de2eb7f6 LT |
644 | if (!strcmp(arg, "--strict")) { |
645 | check_strict = 1; | |
646 | continue; | |
647 | } | |
889262ea | 648 | if (*arg == '-') |
e2b4f635 | 649 | usage(fsck_usage); |
889262ea LT |
650 | } |
651 | ||
c3330383 | 652 | fsck_head_link(); |
8a498a05 JH |
653 | fsck_object_dir(get_object_directory()); |
654 | if (check_full) { | |
d5a63b99 | 655 | struct alternate_object_database *alt; |
8a498a05 JH |
656 | struct packed_git *p; |
657 | prepare_alt_odb(); | |
d5a63b99 | 658 | for (alt = alt_odb_list; alt; alt = alt->next) { |
a3eb250f | 659 | char namebuf[PATH_MAX]; |
d5a63b99 JH |
660 | int namelen = alt->name - alt->base; |
661 | memcpy(namebuf, alt->base, namelen); | |
a3eb250f JH |
662 | namebuf[namelen - 1] = 0; |
663 | fsck_object_dir(namebuf); | |
8a498a05 JH |
664 | } |
665 | prepare_packed_git(); | |
f9253394 JH |
666 | for (p = packed_git; p; p = p->next) |
667 | /* verify gives error messages itself */ | |
f3bf9224 | 668 | verify_pack(p, 0); |
f9253394 | 669 | |
8a498a05 | 670 | for (p = packed_git; p; p = p->next) { |
57059091 | 671 | uint32_t i, num = p->num_objects; |
d72308e0 NP |
672 | for (i = 0; i < num; i++) |
673 | fsck_sha1(nth_packed_object_sha1(p, i)); | |
8a498a05 | 674 | } |
bcee6fd8 LT |
675 | } |
676 | ||
677 | heads = 0; | |
d9839e03 | 678 | for (i = 1; i < argc; i++) { |
889262ea LT |
679 | const char *arg = argv[i]; |
680 | ||
681 | if (*arg == '-') | |
d9839e03 | 682 | continue; |
889262ea | 683 | |
3c249c95 | 684 | if (!get_sha1(arg, head_sha1)) { |
770896e5 | 685 | struct object *obj = lookup_object(head_sha1); |
e1a1388d | 686 | |
770896e5 LT |
687 | /* Error is printed by lookup_object(). */ |
688 | if (!obj) | |
e1a1388d JF |
689 | continue; |
690 | ||
ff5ebe39 DB |
691 | obj->used = 1; |
692 | mark_reachable(obj, REACHABLE); | |
bcee6fd8 | 693 | heads++; |
d9839e03 LT |
694 | continue; |
695 | } | |
f1f0d088 | 696 | error("invalid parameter: expected sha1, got '%s'", arg); |
d9839e03 | 697 | } |
d9839e03 | 698 | |
1024932f | 699 | /* |
d1af002d | 700 | * If we've not been given any explicit head information, do the |
e7bd907d LT |
701 | * default ones from .git/refs. We also consider the index file |
702 | * in this case (ie this implies --cache). | |
1024932f | 703 | */ |
e7bd907d | 704 | if (!heads) { |
1024932f LT |
705 | get_default_heads(); |
706 | keep_cache_objects = 1; | |
707 | } | |
708 | ||
ae7c0c92 JH |
709 | if (keep_cache_objects) { |
710 | int i; | |
711 | read_cache(); | |
712 | for (i = 0; i < active_nr; i++) { | |
8d9721c8 LT |
713 | unsigned int mode; |
714 | struct blob *blob; | |
ae7c0c92 | 715 | struct object *obj; |
8d9721c8 LT |
716 | |
717 | mode = ntohl(active_cache[i]->ce_mode); | |
718 | if (S_ISDIRLNK(mode)) | |
719 | continue; | |
720 | blob = lookup_blob(active_cache[i]->sha1); | |
ae7c0c92 JH |
721 | if (!blob) |
722 | continue; | |
723 | obj = &blob->object; | |
724 | obj->used = 1; | |
725 | mark_reachable(obj, REACHABLE); | |
726 | } | |
53dc3f3e JH |
727 | if (active_cache_tree) |
728 | fsck_cache_tree(active_cache_tree); | |
ae7c0c92 JH |
729 | } |
730 | ||
8ba0bbb2 | 731 | check_connectivity(); |
e2b4f635 | 732 | return errors_found; |
20222118 | 733 | } |