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