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