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