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