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