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