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