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