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