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