]>
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" |
271b8d25 | 11 | #include "fsck.h" |
5ac0a206 | 12 | #include "parse-options.h" |
8ca12c0d | 13 | #include "dir.h" |
1e49f22f | 14 | #include "progress.h" |
6f7f3beb | 15 | #include "streaming.h" |
ff5ebe39 DB |
16 | |
17 | #define REACHABLE 0x0001 | |
2d9c58c6 | 18 | #define SEEN 0x0002 |
6e454b9a | 19 | #define HAS_OBJ 0x0004 |
d9839e03 | 20 | |
96f1e58f DR |
21 | static int show_root; |
22 | static int show_tags; | |
23 | static int show_unreachable; | |
566842f6 | 24 | static int include_reflogs = 1; |
f29cd393 | 25 | static int check_full = 1; |
02976bf8 | 26 | static int connectivity_only; |
96f1e58f DR |
27 | static int check_strict; |
28 | static int keep_cache_objects; | |
22410549 JS |
29 | static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT; |
30 | static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT; | |
635b99a0 | 31 | static struct object_id head_oid; |
469e2ebf | 32 | static const char *head_points_at; |
e2b4f635 | 33 | static int errors_found; |
68f6c019 | 34 | static int write_lost_and_found; |
20f1eb6b | 35 | static int verbose; |
1e49f22f | 36 | static int show_progress = -1; |
c6a13b2c | 37 | static int show_dangling = 1; |
e2b4f635 JH |
38 | #define ERROR_OBJECT 01 |
39 | #define ERROR_REACHABLE 02 | |
a3ed7552 | 40 | #define ERROR_PACK 04 |
d9839e03 | 41 | |
962554c6 | 42 | #ifdef NO_D_INO_IN_DIRENT |
35a730f0 JH |
43 | #define SORT_DIRENT 0 |
44 | #define DIRENT_SORT_HINT(de) 0 | |
45 | #else | |
46 | #define SORT_DIRENT 1 | |
47 | #define DIRENT_SORT_HINT(de) ((de)->d_ino) | |
48 | #endif | |
f1f0d088 | 49 | |
2becf00f | 50 | static int fsck_config(const char *var, const char *value, void *cb) |
f1f0d088 | 51 | { |
1335f732 JS |
52 | if (strcmp(var, "fsck.skiplist") == 0) { |
53 | const char *path; | |
54 | struct strbuf sb = STRBUF_INIT; | |
55 | ||
56 | if (git_config_pathname(&path, var, value)) | |
57 | return 1; | |
58 | strbuf_addf(&sb, "skiplist=%s", path); | |
59 | free((char *)path); | |
60 | fsck_set_msg_types(&fsck_obj_options, sb.buf); | |
61 | strbuf_release(&sb); | |
62 | return 0; | |
63 | } | |
64 | ||
2becf00f JS |
65 | if (skip_prefix(var, "fsck.", &var)) { |
66 | fsck_set_msg_type(&fsck_obj_options, var, value); | |
67 | return 0; | |
68 | } | |
69 | ||
70 | return git_default_config(var, value, cb); | |
71 | } | |
72 | ||
c99ba492 JS |
73 | static void objreport(struct object *obj, const char *msg_type, |
74 | const char *err) | |
f1f0d088 | 75 | { |
c99ba492 JS |
76 | fprintf(stderr, "%s in %s %s: %s\n", |
77 | msg_type, typename(obj->type), sha1_to_hex(obj->sha1), err); | |
f1f0d088 PB |
78 | } |
79 | ||
c99ba492 | 80 | static int objerror(struct object *obj, const char *err) |
f1f0d088 | 81 | { |
e2b4f635 | 82 | errors_found |= ERROR_OBJECT; |
c99ba492 | 83 | objreport(obj, "error", err); |
f1f0d088 PB |
84 | return -1; |
85 | } | |
86 | ||
c99ba492 | 87 | static int fsck_error_func(struct object *obj, int type, const char *message) |
f1f0d088 | 88 | { |
c99ba492 | 89 | objreport(obj, (type == FSCK_WARN) ? "warning" : "error", message); |
ba002f3b | 90 | return (type == FSCK_WARN) ? 0 : 1; |
f1f0d088 PB |
91 | } |
92 | ||
04d39759 LT |
93 | static struct object_array pending; |
94 | ||
22410549 | 95 | static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options) |
271b8d25 | 96 | { |
271b8d25 | 97 | struct object *parent = data; |
271b8d25 | 98 | |
a1cdc251 JH |
99 | /* |
100 | * The only case data is NULL or type is OBJ_ANY is when | |
101 | * mark_object_reachable() calls us. All the callers of | |
102 | * that function has non-NULL obj hence ... | |
103 | */ | |
271b8d25 | 104 | if (!obj) { |
a1cdc251 | 105 | /* ... these references to parent->fld are safe here */ |
271b8d25 MK |
106 | printf("broken link from %7s %s\n", |
107 | typename(parent->type), sha1_to_hex(parent->sha1)); | |
108 | printf("broken link from %7s %s\n", | |
109 | (type == OBJ_ANY ? "unknown" : typename(type)), "unknown"); | |
110 | errors_found |= ERROR_REACHABLE; | |
111 | return 1; | |
112 | } | |
113 | ||
114 | if (type != OBJ_ANY && obj->type != type) | |
a1cdc251 | 115 | /* ... and the reference to parent is safe here */ |
271b8d25 MK |
116 | objerror(parent, "wrong object type in link"); |
117 | ||
118 | if (obj->flags & REACHABLE) | |
119 | return 0; | |
120 | obj->flags |= REACHABLE; | |
6e454b9a | 121 | if (!(obj->flags & HAS_OBJ)) { |
271b8d25 MK |
122 | if (parent && !has_sha1_file(obj->sha1)) { |
123 | printf("broken link from %7s %s\n", | |
124 | typename(parent->type), sha1_to_hex(parent->sha1)); | |
125 | printf(" to %7s %s\n", | |
126 | typename(obj->type), sha1_to_hex(obj->sha1)); | |
127 | errors_found |= ERROR_REACHABLE; | |
128 | } | |
129 | return 1; | |
130 | } | |
131 | ||
16aa3bfc | 132 | add_object_array(obj, NULL, &pending); |
04d39759 LT |
133 | return 0; |
134 | } | |
135 | ||
136 | static void mark_object_reachable(struct object *obj) | |
137 | { | |
22410549 | 138 | mark_object(obj, OBJ_ANY, NULL, NULL); |
04d39759 LT |
139 | } |
140 | ||
a1cdc251 | 141 | static int traverse_one_object(struct object *obj) |
04d39759 LT |
142 | { |
143 | int result; | |
144 | struct tree *tree = NULL; | |
145 | ||
271b8d25 | 146 | if (obj->type == OBJ_TREE) { |
271b8d25 MK |
147 | tree = (struct tree *)obj; |
148 | if (parse_tree(tree) < 0) | |
149 | return 1; /* error already displayed */ | |
150 | } | |
22410549 | 151 | result = fsck_walk(obj, obj, &fsck_walk_options); |
6e454b9a JK |
152 | if (tree) |
153 | free_tree_buffer(tree); | |
271b8d25 MK |
154 | return result; |
155 | } | |
156 | ||
04d39759 | 157 | static int traverse_reachable(void) |
271b8d25 | 158 | { |
1e49f22f NTND |
159 | struct progress *progress = NULL; |
160 | unsigned int nr = 0; | |
04d39759 | 161 | int result = 0; |
1e49f22f | 162 | if (show_progress) |
754dbc43 | 163 | progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2); |
04d39759 LT |
164 | while (pending.nr) { |
165 | struct object_array_entry *entry; | |
c0aa335c | 166 | struct object *obj; |
04d39759 LT |
167 | |
168 | entry = pending.objects + --pending.nr; | |
169 | obj = entry->item; | |
a1cdc251 | 170 | result |= traverse_one_object(obj); |
1e49f22f | 171 | display_progress(progress, ++nr); |
04d39759 | 172 | } |
1e49f22f | 173 | stop_progress(&progress); |
04d39759 | 174 | return !!result; |
271b8d25 MK |
175 | } |
176 | ||
22410549 | 177 | static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options) |
271b8d25 MK |
178 | { |
179 | if (!obj) | |
180 | return 1; | |
181 | obj->used = 1; | |
182 | return 0; | |
f1f0d088 PB |
183 | } |
184 | ||
18af29f2 LT |
185 | /* |
186 | * Check a single reachable object | |
187 | */ | |
188 | static void check_reachable_object(struct object *obj) | |
189 | { | |
18af29f2 LT |
190 | /* |
191 | * We obviously want the object to be parsed, | |
192 | * except if it was in a pack-file and we didn't | |
193 | * do a full fsck | |
194 | */ | |
6e454b9a | 195 | if (!(obj->flags & HAS_OBJ)) { |
cd673c1f | 196 | if (has_sha1_pack(obj->sha1)) |
18af29f2 | 197 | return; /* it is in pack - forget about it */ |
02976bf8 JS |
198 | if (connectivity_only && has_sha1_file(obj->sha1)) |
199 | return; | |
18af29f2 | 200 | printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1)); |
e2b4f635 | 201 | errors_found |= ERROR_REACHABLE; |
18af29f2 LT |
202 | return; |
203 | } | |
18af29f2 LT |
204 | } |
205 | ||
206 | /* | |
207 | * Check a single unreachable object | |
208 | */ | |
209 | static void check_unreachable_object(struct object *obj) | |
210 | { | |
211 | /* | |
212 | * Missing unreachable object? Ignore it. It's not like | |
213 | * we miss it (since it can't be reached), nor do we want | |
214 | * to complain about it being unreachable (since it does | |
215 | * not exist). | |
216 | */ | |
217 | if (!obj->parsed) | |
218 | return; | |
219 | ||
220 | /* | |
221 | * Unreachable object that exists? Show it if asked to, | |
222 | * since this is something that is prunable. | |
223 | */ | |
224 | if (show_unreachable) { | |
225 | printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1)); | |
226 | return; | |
227 | } | |
228 | ||
229 | /* | |
230 | * "!used" means that nothing at all points to it, including | |
3dff5379 | 231 | * other unreachable objects. In other words, it's the "tip" |
18af29f2 LT |
232 | * of some set of unreachable objects, usually a commit that |
233 | * got dropped. | |
234 | * | |
235 | * Such starting points are more interesting than some random | |
236 | * set of unreachable objects, so we show them even if the user | |
237 | * hasn't asked for _all_ unreachable objects. If you have | |
238 | * deleted a branch by mistake, this is a prime candidate to | |
239 | * start looking at, for example. | |
240 | */ | |
241 | if (!obj->used) { | |
c6a13b2c JH |
242 | if (show_dangling) |
243 | printf("dangling %s %s\n", typename(obj->type), | |
244 | sha1_to_hex(obj->sha1)); | |
68f6c019 | 245 | if (write_lost_and_found) { |
dcf69262 | 246 | const char *filename = git_path("lost-found/%s/%s", |
68f6c019 JS |
247 | obj->type == OBJ_COMMIT ? "commit" : "other", |
248 | sha1_to_hex(obj->sha1)); | |
249 | FILE *f; | |
250 | ||
dcf69262 | 251 | if (safe_create_leading_directories_const(filename)) { |
68f6c019 JS |
252 | error("Could not create lost-found"); |
253 | return; | |
254 | } | |
255 | if (!(f = fopen(filename, "w"))) | |
0721c314 | 256 | die_errno("Could not open '%s'", filename); |
16a7fcfe | 257 | if (obj->type == OBJ_BLOB) { |
6f7f3beb | 258 | if (stream_blob_to_fd(fileno(f), obj->sha1, NULL, 1)) |
eb726f2d | 259 | die_errno("Could not write '%s'", filename); |
16a7fcfe JS |
260 | } else |
261 | fprintf(f, "%s\n", sha1_to_hex(obj->sha1)); | |
47d32af2 | 262 | if (fclose(f)) |
d824cbba TR |
263 | die_errno("Could not finish '%s'", |
264 | filename); | |
68f6c019 | 265 | } |
18af29f2 LT |
266 | return; |
267 | } | |
268 | ||
269 | /* | |
270 | * Otherwise? It's there, it's unreachable, and some other unreachable | |
271 | * object points to it. Ignore it - it's not interesting, and we showed | |
272 | * all the interesting cases above. | |
273 | */ | |
274 | } | |
275 | ||
276 | static void check_object(struct object *obj) | |
277 | { | |
20f1eb6b JS |
278 | if (verbose) |
279 | fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1)); | |
280 | ||
18af29f2 LT |
281 | if (obj->flags & REACHABLE) |
282 | check_reachable_object(obj); | |
283 | else | |
284 | check_unreachable_object(obj); | |
285 | } | |
f1f0d088 | 286 | |
8ba0bbb2 LT |
287 | static void check_connectivity(void) |
288 | { | |
fc046a75 | 289 | int i, max; |
8ba0bbb2 | 290 | |
04d39759 LT |
291 | /* Traverse the pending reachable objects */ |
292 | traverse_reachable(); | |
293 | ||
8ba0bbb2 | 294 | /* Look up all the requirements, warn about missing objects.. */ |
fc046a75 | 295 | max = get_max_object_index(); |
20f1eb6b JS |
296 | if (verbose) |
297 | fprintf(stderr, "Checking connectivity (%d objects)\n", max); | |
298 | ||
fc046a75 | 299 | for (i = 0; i < max; i++) { |
fc046a75 | 300 | struct object *obj = get_indexed_object(i); |
8ba0bbb2 | 301 | |
18af29f2 LT |
302 | if (obj) |
303 | check_object(obj); | |
8ba0bbb2 LT |
304 | } |
305 | } | |
306 | ||
c9486eb0 | 307 | static int fsck_obj(struct object *obj) |
85003492 | 308 | { |
ba002f3b | 309 | if (obj->flags & SEEN) |
85003492 | 310 | return 0; |
ba002f3b | 311 | obj->flags |= SEEN; |
85003492 | 312 | |
20f1eb6b | 313 | if (verbose) |
ba002f3b MK |
314 | fprintf(stderr, "Checking %s %s\n", |
315 | typename(obj->type), sha1_to_hex(obj->sha1)); | |
42ea9cb2 | 316 | |
22410549 | 317 | if (fsck_walk(obj, NULL, &fsck_obj_options)) |
ba002f3b | 318 | objerror(obj, "broken links"); |
22410549 | 319 | if (fsck_object(obj, NULL, 0, &fsck_obj_options)) |
ba002f3b | 320 | return -1; |
85003492 | 321 | |
ba002f3b MK |
322 | if (obj->type == OBJ_TREE) { |
323 | struct tree *item = (struct tree *) obj; | |
85003492 | 324 | |
6e454b9a | 325 | free_tree_buffer(item); |
1ea34e36 | 326 | } |
1ea34e36 | 327 | |
ba002f3b MK |
328 | if (obj->type == OBJ_COMMIT) { |
329 | struct commit *commit = (struct commit *) obj; | |
de2eb7f6 | 330 | |
0fb370da | 331 | free_commit_buffer(commit); |
4728b861 | 332 | |
ba002f3b MK |
333 | if (!commit->parents && show_root) |
334 | printf("root %s\n", sha1_to_hex(commit->object.sha1)); | |
335 | } | |
92d4c85d | 336 | |
ba002f3b MK |
337 | if (obj->type == OBJ_TAG) { |
338 | struct tag *tag = (struct tag *) obj; | |
20f1eb6b | 339 | |
ba002f3b MK |
340 | if (show_tags && tag->tagged) { |
341 | printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1)); | |
342 | printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1)); | |
343 | } | |
92d4c85d | 344 | } |
889262ea | 345 | |
ff5ebe39 | 346 | return 0; |
20222118 LT |
347 | } |
348 | ||
c9486eb0 NTND |
349 | static int fsck_sha1(const unsigned char *sha1) |
350 | { | |
351 | struct object *obj = parse_object(sha1); | |
352 | if (!obj) { | |
353 | errors_found |= ERROR_OBJECT; | |
354 | return error("%s: object corrupt or missing", | |
355 | sha1_to_hex(sha1)); | |
356 | } | |
6e454b9a | 357 | obj->flags |= HAS_OBJ; |
c9486eb0 NTND |
358 | return fsck_obj(obj); |
359 | } | |
360 | ||
361 | static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type, | |
362 | unsigned long size, void *buffer, int *eaten) | |
363 | { | |
364 | struct object *obj; | |
365 | obj = parse_object_buffer(sha1, type, size, buffer, eaten); | |
366 | if (!obj) { | |
367 | errors_found |= ERROR_OBJECT; | |
368 | return error("%s: object corrupt or missing", sha1_to_hex(sha1)); | |
369 | } | |
6e454b9a | 370 | obj->flags = HAS_OBJ; |
c9486eb0 NTND |
371 | return fsck_obj(obj); |
372 | } | |
373 | ||
7e8c174a LT |
374 | /* |
375 | * This is the sorting chunk size: make it reasonably | |
376 | * big so that we can sort well.. | |
377 | */ | |
378 | #define MAX_SHA1_ENTRIES (1024) | |
379 | ||
380 | struct sha1_entry { | |
381 | unsigned long ino; | |
20222118 | 382 | unsigned char sha1[20]; |
7e8c174a LT |
383 | }; |
384 | ||
385 | static struct { | |
386 | unsigned long nr; | |
387 | struct sha1_entry *entry[MAX_SHA1_ENTRIES]; | |
388 | } sha1_list; | |
389 | ||
390 | static int ino_compare(const void *_a, const void *_b) | |
391 | { | |
392 | const struct sha1_entry *a = _a, *b = _b; | |
393 | unsigned long ino1 = a->ino, ino2 = b->ino; | |
394 | return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0; | |
395 | } | |
396 | ||
397 | static void fsck_sha1_list(void) | |
398 | { | |
399 | int i, nr = sha1_list.nr; | |
400 | ||
35a730f0 JH |
401 | if (SORT_DIRENT) |
402 | qsort(sha1_list.entry, nr, | |
403 | sizeof(struct sha1_entry *), ino_compare); | |
7e8c174a LT |
404 | for (i = 0; i < nr; i++) { |
405 | struct sha1_entry *entry = sha1_list.entry[i]; | |
406 | unsigned char *sha1 = entry->sha1; | |
407 | ||
408 | sha1_list.entry[i] = NULL; | |
2e770fe4 JK |
409 | if (fsck_sha1(sha1)) |
410 | errors_found |= ERROR_OBJECT; | |
7e8c174a | 411 | free(entry); |
20222118 | 412 | } |
7e8c174a LT |
413 | sha1_list.nr = 0; |
414 | } | |
415 | ||
416 | static void add_sha1_list(unsigned char *sha1, unsigned long ino) | |
417 | { | |
418 | struct sha1_entry *entry = xmalloc(sizeof(*entry)); | |
419 | int nr; | |
420 | ||
421 | entry->ino = ino; | |
e702496e | 422 | hashcpy(entry->sha1, sha1); |
7e8c174a LT |
423 | nr = sha1_list.nr; |
424 | if (nr == MAX_SHA1_ENTRIES) { | |
425 | fsck_sha1_list(); | |
426 | nr = 0; | |
427 | } | |
428 | sha1_list.entry[nr] = entry; | |
429 | sha1_list.nr = ++nr; | |
20222118 LT |
430 | } |
431 | ||
ea6f0a23 JH |
432 | static inline int is_loose_object_file(struct dirent *de, |
433 | char *name, unsigned char *sha1) | |
434 | { | |
435 | if (strlen(de->d_name) != 38) | |
436 | return 0; | |
437 | memcpy(name + 2, de->d_name, 39); | |
438 | return !get_sha1_hex(name, sha1); | |
439 | } | |
440 | ||
b5524c82 | 441 | static void fsck_dir(int i, char *path) |
20222118 LT |
442 | { |
443 | DIR *dir = opendir(path); | |
444 | struct dirent *de; | |
ea6f0a23 | 445 | char name[100]; |
20222118 | 446 | |
230f1322 | 447 | if (!dir) |
b5524c82 | 448 | return; |
20222118 | 449 | |
20f1eb6b JS |
450 | if (verbose) |
451 | fprintf(stderr, "Checking directory %s\n", path); | |
452 | ||
ea6f0a23 | 453 | sprintf(name, "%02x", i); |
20222118 | 454 | while ((de = readdir(dir)) != NULL) { |
7e8c174a | 455 | unsigned char sha1[20]; |
20222118 | 456 | |
8ca12c0d | 457 | if (is_dot_or_dotdot(de->d_name)) |
20222118 | 458 | continue; |
ea6f0a23 | 459 | if (is_loose_object_file(de, name, sha1)) { |
35a730f0 | 460 | add_sha1_list(sha1, DIRENT_SORT_HINT(de)); |
7e8c174a | 461 | continue; |
20222118 | 462 | } |
59556548 | 463 | if (starts_with(de->d_name, "tmp_obj_")) |
a08c53a1 | 464 | continue; |
20222118 LT |
465 | fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name); |
466 | } | |
467 | closedir(dir); | |
20222118 LT |
468 | } |
469 | ||
96f1e58f | 470 | static int default_refs; |
944d8589 | 471 | |
19bf6c9b | 472 | static void fsck_handle_reflog_sha1(const char *refname, unsigned char *sha1) |
55dd5526 JH |
473 | { |
474 | struct object *obj; | |
475 | ||
d66ae59b MH |
476 | if (!is_null_sha1(sha1)) { |
477 | obj = lookup_object(sha1); | |
55dd5526 JH |
478 | if (obj) { |
479 | obj->used = 1; | |
271b8d25 | 480 | mark_object_reachable(obj); |
19bf6c9b MH |
481 | } else { |
482 | error("%s: invalid reflog entry %s", refname, sha1_to_hex(sha1)); | |
483 | errors_found |= ERROR_REACHABLE; | |
55dd5526 JH |
484 | } |
485 | } | |
d66ae59b MH |
486 | } |
487 | ||
883d60fa JS |
488 | static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1, |
489 | const char *email, unsigned long timestamp, int tz, | |
490 | const char *message, void *cb_data) | |
55dd5526 | 491 | { |
19bf6c9b | 492 | const char *refname = cb_data; |
55dd5526 | 493 | |
20f1eb6b JS |
494 | if (verbose) |
495 | fprintf(stderr, "Checking reflog %s->%s\n", | |
496 | sha1_to_hex(osha1), sha1_to_hex(nsha1)); | |
497 | ||
19bf6c9b MH |
498 | fsck_handle_reflog_sha1(refname, osha1); |
499 | fsck_handle_reflog_sha1(refname, nsha1); | |
55dd5526 JH |
500 | return 0; |
501 | } | |
502 | ||
635b99a0 MH |
503 | static int fsck_handle_reflog(const char *logname, const struct object_id *oid, |
504 | int flag, void *cb_data) | |
eb8381c8 | 505 | { |
19bf6c9b | 506 | for_each_reflog_ent(logname, fsck_handle_reflog_ent, (void *)logname); |
eb8381c8 NP |
507 | return 0; |
508 | } | |
509 | ||
635b99a0 MH |
510 | static int fsck_handle_ref(const char *refname, const struct object_id *oid, |
511 | int flag, void *cb_data) | |
1024932f | 512 | { |
1024932f LT |
513 | struct object *obj; |
514 | ||
635b99a0 | 515 | obj = parse_object(oid->hash); |
8a498a05 | 516 | if (!obj) { |
635b99a0 | 517 | error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid)); |
30d1038d | 518 | errors_found |= ERROR_REACHABLE; |
944d8589 LT |
519 | /* We'll continue with the rest despite the error.. */ |
520 | return 0; | |
8a498a05 | 521 | } |
6232f62b LT |
522 | if (obj->type != OBJ_COMMIT && is_branch(refname)) |
523 | error("%s: not a commit", refname); | |
944d8589 | 524 | default_refs++; |
1024932f | 525 | obj->used = 1; |
271b8d25 | 526 | mark_object_reachable(obj); |
55dd5526 | 527 | |
7c4d07c7 | 528 | return 0; |
1024932f LT |
529 | } |
530 | ||
1024932f LT |
531 | static void get_default_heads(void) |
532 | { | |
635b99a0 MH |
533 | if (head_points_at && !is_null_oid(&head_oid)) |
534 | fsck_handle_ref("HEAD", &head_oid, 0, NULL); | |
535 | for_each_rawref(fsck_handle_ref, NULL); | |
566842f6 | 536 | if (include_reflogs) |
635b99a0 | 537 | for_each_reflog(fsck_handle_reflog, NULL); |
071fa89e LT |
538 | |
539 | /* | |
540 | * Not having any default heads isn't really fatal, but | |
541 | * it does mean that "--unreachable" no longer makes any | |
542 | * sense (since in this case everything will obviously | |
543 | * be unreachable by definition. | |
544 | * | |
545 | * Showing dangling objects is valid, though (as those | |
546 | * dangling objects are likely lost heads). | |
547 | * | |
548 | * So we just print a warning about it, and clear the | |
549 | * "show_unreachable" flag. | |
550 | */ | |
551 | if (!default_refs) { | |
8eb2d0be | 552 | fprintf(stderr, "notice: No default references\n"); |
071fa89e LT |
553 | show_unreachable = 0; |
554 | } | |
1024932f LT |
555 | } |
556 | ||
8a498a05 JH |
557 | static void fsck_object_dir(const char *path) |
558 | { | |
559 | int i; | |
1e49f22f | 560 | struct progress *progress = NULL; |
20f1eb6b JS |
561 | |
562 | if (verbose) | |
563 | fprintf(stderr, "Checking object directory\n"); | |
564 | ||
1e49f22f | 565 | if (show_progress) |
754dbc43 | 566 | progress = start_progress(_("Checking object directories"), 256); |
8a498a05 JH |
567 | for (i = 0; i < 256; i++) { |
568 | static char dir[4096]; | |
569 | sprintf(dir, "%s/%02x", path, i); | |
570 | fsck_dir(i, dir); | |
1e49f22f | 571 | display_progress(progress, i+1); |
8a498a05 | 572 | } |
1e49f22f | 573 | stop_progress(&progress); |
8a498a05 JH |
574 | fsck_sha1_list(); |
575 | } | |
576 | ||
c3330383 LT |
577 | static int fsck_head_link(void) |
578 | { | |
8da19775 | 579 | int flag; |
8eb2d0be | 580 | int null_is_error = 0; |
8eb2d0be | 581 | |
20f1eb6b JS |
582 | if (verbose) |
583 | fprintf(stderr, "Checking HEAD link\n"); | |
584 | ||
635b99a0 | 585 | head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, &flag); |
8eb2d0be JH |
586 | if (!head_points_at) |
587 | return error("Invalid HEAD"); | |
588 | if (!strcmp(head_points_at, "HEAD")) | |
589 | /* detached HEAD */ | |
590 | null_is_error = 1; | |
59556548 | 591 | else if (!starts_with(head_points_at, "refs/heads/")) |
8098a178 | 592 | return error("HEAD points to something strange (%s)", |
5b10b091 | 593 | head_points_at); |
635b99a0 | 594 | if (is_null_oid(&head_oid)) { |
8eb2d0be JH |
595 | if (null_is_error) |
596 | return error("HEAD: detached HEAD points at nothing"); | |
597 | fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n", | |
598 | head_points_at + 11); | |
599 | } | |
c3330383 LT |
600 | return 0; |
601 | } | |
602 | ||
53dc3f3e JH |
603 | static int fsck_cache_tree(struct cache_tree *it) |
604 | { | |
605 | int i; | |
606 | int err = 0; | |
607 | ||
20f1eb6b JS |
608 | if (verbose) |
609 | fprintf(stderr, "Checking cache tree\n"); | |
610 | ||
53dc3f3e JH |
611 | if (0 <= it->entry_count) { |
612 | struct object *obj = parse_object(it->sha1); | |
6d60bbef JH |
613 | if (!obj) { |
614 | error("%s: invalid sha1 pointer in cache-tree", | |
615 | sha1_to_hex(it->sha1)); | |
616 | return 1; | |
617 | } | |
cdc08b33 | 618 | obj->used = 1; |
a1cdc251 | 619 | mark_object_reachable(obj); |
1974632c | 620 | if (obj->type != OBJ_TREE) |
53dc3f3e JH |
621 | err |= objerror(obj, "non-tree in cache-tree"); |
622 | } | |
623 | for (i = 0; i < it->subtree_nr; i++) | |
624 | err |= fsck_cache_tree(it->down[i]->cache_tree); | |
625 | return err; | |
626 | } | |
627 | ||
5ac0a206 | 628 | static char const * const fsck_usage[] = { |
9c9b4f2f | 629 | N_("git fsck [<options>] [<object>...]"), |
5ac0a206 PH |
630 | NULL |
631 | }; | |
632 | ||
633 | static struct option fsck_opts[] = { | |
cf8fe315 | 634 | OPT__VERBOSE(&verbose, N_("be verbose")), |
d5d09d47 | 635 | OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")), |
cf8fe315 | 636 | OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")), |
d5d09d47 SB |
637 | OPT_BOOL(0, "tags", &show_tags, N_("report tags")), |
638 | OPT_BOOL(0, "root", &show_root, N_("report root nodes")), | |
639 | OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")), | |
640 | OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")), | |
641 | OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")), | |
02976bf8 | 642 | OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")), |
d5d09d47 SB |
643 | OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")), |
644 | OPT_BOOL(0, "lost-found", &write_lost_and_found, | |
cf8fe315 NTND |
645 | N_("write dangling objects in .git/lost-found")), |
646 | OPT_BOOL(0, "progress", &show_progress, N_("show progress")), | |
5ac0a206 PH |
647 | OPT_END(), |
648 | }; | |
e2b4f635 | 649 | |
baffc0e7 | 650 | int cmd_fsck(int argc, const char **argv, const char *prefix) |
20222118 | 651 | { |
bcee6fd8 | 652 | int i, heads; |
e15ef669 | 653 | struct alternate_object_database *alt; |
20222118 | 654 | |
e2b4f635 | 655 | errors_found = 0; |
afc711b8 | 656 | check_replace_refs = 0; |
61e2b015 | 657 | |
37782920 | 658 | argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0); |
1e49f22f | 659 | |
22410549 JS |
660 | fsck_walk_options.walk = mark_object; |
661 | fsck_obj_options.walk = mark_used; | |
662 | fsck_obj_options.error_func = fsck_error_func; | |
663 | if (check_strict) | |
664 | fsck_obj_options.strict = 1; | |
665 | ||
1e49f22f NTND |
666 | if (show_progress == -1) |
667 | show_progress = isatty(2); | |
668 | if (verbose) | |
669 | show_progress = 0; | |
670 | ||
5ac0a206 PH |
671 | if (write_lost_and_found) { |
672 | check_full = 1; | |
673 | include_reflogs = 0; | |
889262ea LT |
674 | } |
675 | ||
2becf00f JS |
676 | git_config(fsck_config, NULL); |
677 | ||
c3330383 | 678 | fsck_head_link(); |
02976bf8 JS |
679 | if (!connectivity_only) |
680 | fsck_object_dir(get_object_directory()); | |
e15ef669 JH |
681 | |
682 | prepare_alt_odb(); | |
683 | for (alt = alt_odb_list; alt; alt = alt->next) { | |
684 | char namebuf[PATH_MAX]; | |
685 | int namelen = alt->name - alt->base; | |
686 | memcpy(namebuf, alt->base, namelen); | |
687 | namebuf[namelen - 1] = 0; | |
688 | fsck_object_dir(namebuf); | |
689 | } | |
690 | ||
8a498a05 | 691 | if (check_full) { |
8a498a05 | 692 | struct packed_git *p; |
1e49f22f NTND |
693 | uint32_t total = 0, count = 0; |
694 | struct progress *progress = NULL; | |
e15ef669 | 695 | |
8a498a05 | 696 | prepare_packed_git(); |
1e49f22f NTND |
697 | |
698 | if (show_progress) { | |
699 | for (p = packed_git; p; p = p->next) { | |
700 | if (open_pack_index(p)) | |
701 | continue; | |
702 | total += p->num_objects; | |
703 | } | |
704 | ||
754dbc43 | 705 | progress = start_progress(_("Checking objects"), total); |
1e49f22f NTND |
706 | } |
707 | for (p = packed_git; p; p = p->next) { | |
f9253394 | 708 | /* verify gives error messages itself */ |
1e49f22f NTND |
709 | if (verify_pack(p, fsck_obj_buffer, |
710 | progress, count)) | |
a3ed7552 | 711 | errors_found |= ERROR_PACK; |
1e49f22f NTND |
712 | count += p->num_objects; |
713 | } | |
714 | stop_progress(&progress); | |
bcee6fd8 LT |
715 | } |
716 | ||
717 | heads = 0; | |
3aed2fda | 718 | for (i = 0; i < argc; i++) { |
a6080a0a | 719 | const char *arg = argv[i]; |
469e2ebf JH |
720 | unsigned char sha1[20]; |
721 | if (!get_sha1(arg, sha1)) { | |
722 | struct object *obj = lookup_object(sha1); | |
e1a1388d | 723 | |
770896e5 LT |
724 | /* Error is printed by lookup_object(). */ |
725 | if (!obj) | |
e1a1388d JF |
726 | continue; |
727 | ||
ff5ebe39 | 728 | obj->used = 1; |
271b8d25 | 729 | mark_object_reachable(obj); |
bcee6fd8 | 730 | heads++; |
d9839e03 LT |
731 | continue; |
732 | } | |
f1f0d088 | 733 | error("invalid parameter: expected sha1, got '%s'", arg); |
d9839e03 | 734 | } |
d9839e03 | 735 | |
1024932f | 736 | /* |
d1af002d | 737 | * If we've not been given any explicit head information, do the |
e7bd907d LT |
738 | * default ones from .git/refs. We also consider the index file |
739 | * in this case (ie this implies --cache). | |
1024932f | 740 | */ |
e7bd907d | 741 | if (!heads) { |
1024932f LT |
742 | get_default_heads(); |
743 | keep_cache_objects = 1; | |
744 | } | |
745 | ||
ae7c0c92 | 746 | if (keep_cache_objects) { |
ae7c0c92 JH |
747 | read_cache(); |
748 | for (i = 0; i < active_nr; i++) { | |
8d9721c8 LT |
749 | unsigned int mode; |
750 | struct blob *blob; | |
ae7c0c92 | 751 | struct object *obj; |
8d9721c8 | 752 | |
7a51ed66 | 753 | mode = active_cache[i]->ce_mode; |
302b9282 | 754 | if (S_ISGITLINK(mode)) |
8d9721c8 LT |
755 | continue; |
756 | blob = lookup_blob(active_cache[i]->sha1); | |
ae7c0c92 JH |
757 | if (!blob) |
758 | continue; | |
759 | obj = &blob->object; | |
760 | obj->used = 1; | |
271b8d25 | 761 | mark_object_reachable(obj); |
ae7c0c92 | 762 | } |
53dc3f3e JH |
763 | if (active_cache_tree) |
764 | fsck_cache_tree(active_cache_tree); | |
ae7c0c92 JH |
765 | } |
766 | ||
8ba0bbb2 | 767 | check_connectivity(); |
e2b4f635 | 768 | return errors_found; |
20222118 | 769 | } |