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