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