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