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