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