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