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