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