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