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