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