]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fsck.c
packfile: remove unused member from struct pack_entry
[thirdparty/git.git] / builtin / fsck.c
CommitLineData
baffc0e7 1#include "builtin.h"
4b182421 2#include "cache.h"
031dc927 3#include "repository.h"
b2141fc1 4#include "config.h"
ff5ebe39
DB
5#include "commit.h"
6#include "tree.h"
7#include "blob.h"
c418eda4 8#include "tag.h"
944d8589 9#include "refs.h"
f9253394 10#include "pack.h"
53dc3f3e 11#include "cache-tree.h"
e9a95bef 12#include "tree-walk.h"
271b8d25 13#include "fsck.h"
5ac0a206 14#include "parse-options.h"
8ca12c0d 15#include "dir.h"
1e49f22f 16#include "progress.h"
6f7f3beb 17#include "streaming.h"
90cf590f 18#include "decorate.h"
0317f455 19#include "packfile.h"
0d4a1321 20#include "object-store.h"
ff5ebe39
DB
21
22#define REACHABLE 0x0001
2d9c58c6 23#define SEEN 0x0002
6e454b9a 24#define HAS_OBJ 0x0004
092c55d0
JT
25/* This flag is set if something points to this object. */
26#define USED 0x0008
d9839e03 27
96f1e58f
DR
28static int show_root;
29static int show_tags;
30static int show_unreachable;
566842f6 31static int include_reflogs = 1;
f29cd393 32static int check_full = 1;
02976bf8 33static int connectivity_only;
96f1e58f
DR
34static int check_strict;
35static int keep_cache_objects;
22410549
JS
36static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
37static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
635b99a0 38static struct object_id head_oid;
469e2ebf 39static const char *head_points_at;
e2b4f635 40static int errors_found;
68f6c019 41static int write_lost_and_found;
20f1eb6b 42static int verbose;
1e49f22f 43static int show_progress = -1;
c6a13b2c 44static int show_dangling = 1;
90cf590f 45static int name_objects;
e2b4f635
JH
46#define ERROR_OBJECT 01
47#define ERROR_REACHABLE 02
a3ed7552 48#define ERROR_PACK 04
122f76f5 49#define ERROR_REFS 010
d9839e03 50
993a21b0
JS
51static const char *describe_object(struct object *obj)
52{
90cf590f
JS
53 static struct strbuf buf = STRBUF_INIT;
54 char *name = name_objects ?
55 lookup_decoration(fsck_walk_options.object_names, obj) : NULL;
56
57 strbuf_reset(&buf);
58 strbuf_addstr(&buf, oid_to_hex(&obj->oid));
59 if (name)
60 strbuf_addf(&buf, " (%s)", name);
61
62 return buf.buf;
993a21b0
JS
63}
64
97ca7ca8
JK
65static const char *printable_type(struct object *obj)
66{
67 const char *ret;
68
a2b22854 69 if (obj->type == OBJ_NONE) {
abef9020 70 enum object_type type = oid_object_info(&obj->oid, NULL);
a2b22854
JK
71 if (type > 0)
72 object_as_type(obj, type, 0);
73 }
74
debca9d2 75 ret = type_name(obj->type);
97ca7ca8
JK
76 if (!ret)
77 ret = "unknown";
78
79 return ret;
80}
81
2becf00f 82static int fsck_config(const char *var, const char *value, void *cb)
f1f0d088 83{
1335f732
JS
84 if (strcmp(var, "fsck.skiplist") == 0) {
85 const char *path;
86 struct strbuf sb = STRBUF_INIT;
87
88 if (git_config_pathname(&path, var, value))
89 return 1;
90 strbuf_addf(&sb, "skiplist=%s", path);
91 free((char *)path);
92 fsck_set_msg_types(&fsck_obj_options, sb.buf);
93 strbuf_release(&sb);
94 return 0;
95 }
96
2becf00f
JS
97 if (skip_prefix(var, "fsck.", &var)) {
98 fsck_set_msg_type(&fsck_obj_options, var, value);
99 return 0;
100 }
101
102 return git_default_config(var, value, cb);
103}
104
c99ba492
JS
105static void objreport(struct object *obj, const char *msg_type,
106 const char *err)
f1f0d088 107{
c99ba492 108 fprintf(stderr, "%s in %s %s: %s\n",
97ca7ca8 109 msg_type, printable_type(obj), describe_object(obj), err);
f1f0d088
PB
110}
111
c99ba492 112static int objerror(struct object *obj, const char *err)
f1f0d088 113{
e2b4f635 114 errors_found |= ERROR_OBJECT;
c99ba492 115 objreport(obj, "error", err);
f1f0d088
PB
116 return -1;
117}
118
1cd772cc
JS
119static int fsck_error_func(struct fsck_options *o,
120 struct object *obj, int type, const char *message)
f1f0d088 121{
c99ba492 122 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", message);
ba002f3b 123 return (type == FSCK_WARN) ? 0 : 1;
f1f0d088
PB
124}
125
04d39759
LT
126static struct object_array pending;
127
22410549 128static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
271b8d25 129{
271b8d25 130 struct object *parent = data;
271b8d25 131
a1cdc251
JH
132 /*
133 * The only case data is NULL or type is OBJ_ANY is when
134 * mark_object_reachable() calls us. All the callers of
135 * that function has non-NULL obj hence ...
136 */
271b8d25 137 if (!obj) {
a1cdc251 138 /* ... these references to parent->fld are safe here */
271b8d25 139 printf("broken link from %7s %s\n",
97ca7ca8 140 printable_type(parent), describe_object(parent));
271b8d25 141 printf("broken link from %7s %s\n",
debca9d2 142 (type == OBJ_ANY ? "unknown" : type_name(type)), "unknown");
271b8d25
MK
143 errors_found |= ERROR_REACHABLE;
144 return 1;
145 }
146
147 if (type != OBJ_ANY && obj->type != type)
a1cdc251 148 /* ... and the reference to parent is safe here */
271b8d25
MK
149 objerror(parent, "wrong object type in link");
150
151 if (obj->flags & REACHABLE)
152 return 0;
153 obj->flags |= REACHABLE;
caba7fc3
JT
154
155 if (is_promisor_object(&obj->oid))
156 /*
157 * Further recursion does not need to be performed on this
158 * object since it is a promisor object (so it does not need to
159 * be added to "pending").
160 */
161 return 0;
162
6e454b9a 163 if (!(obj->flags & HAS_OBJ)) {
f2fd0760 164 if (parent && !has_object_file(&obj->oid)) {
271b8d25 165 printf("broken link from %7s %s\n",
97ca7ca8 166 printable_type(parent), describe_object(parent));
271b8d25 167 printf(" to %7s %s\n",
97ca7ca8 168 printable_type(obj), describe_object(obj));
271b8d25
MK
169 errors_found |= ERROR_REACHABLE;
170 }
171 return 1;
172 }
173
16aa3bfc 174 add_object_array(obj, NULL, &pending);
04d39759
LT
175 return 0;
176}
177
178static void mark_object_reachable(struct object *obj)
179{
22410549 180 mark_object(obj, OBJ_ANY, NULL, NULL);
04d39759
LT
181}
182
a1cdc251 183static int traverse_one_object(struct object *obj)
04d39759 184{
ba3a08ca
EW
185 int result = fsck_walk(obj, obj, &fsck_walk_options);
186
187 if (obj->type == OBJ_TREE) {
188 struct tree *tree = (struct tree *)obj;
189 free_tree_buffer(tree);
190 }
191 return result;
271b8d25
MK
192}
193
04d39759 194static int traverse_reachable(void)
271b8d25 195{
1e49f22f
NTND
196 struct progress *progress = NULL;
197 unsigned int nr = 0;
04d39759 198 int result = 0;
1e49f22f 199 if (show_progress)
8aade107 200 progress = start_delayed_progress(_("Checking connectivity"), 0);
04d39759 201 while (pending.nr) {
71992039 202 result |= traverse_one_object(object_array_pop(&pending));
1e49f22f 203 display_progress(progress, ++nr);
04d39759 204 }
1e49f22f 205 stop_progress(&progress);
04d39759 206 return !!result;
271b8d25
MK
207}
208
22410549 209static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
271b8d25
MK
210{
211 if (!obj)
212 return 1;
092c55d0 213 obj->flags |= USED;
271b8d25 214 return 0;
f1f0d088
PB
215}
216
18af29f2
LT
217/*
218 * Check a single reachable object
219 */
220static void check_reachable_object(struct object *obj)
221{
18af29f2
LT
222 /*
223 * We obviously want the object to be parsed,
224 * except if it was in a pack-file and we didn't
225 * do a full fsck
226 */
6e454b9a 227 if (!(obj->flags & HAS_OBJ)) {
caba7fc3
JT
228 if (is_promisor_object(&obj->oid))
229 return;
ed1c9977 230 if (has_sha1_pack(obj->oid.hash))
18af29f2 231 return; /* it is in pack - forget about it */
97ca7ca8 232 printf("missing %s %s\n", printable_type(obj),
993a21b0 233 describe_object(obj));
e2b4f635 234 errors_found |= ERROR_REACHABLE;
18af29f2
LT
235 return;
236 }
18af29f2
LT
237}
238
239/*
240 * Check a single unreachable object
241 */
242static void check_unreachable_object(struct object *obj)
243{
244 /*
245 * Missing unreachable object? Ignore it. It's not like
246 * we miss it (since it can't be reached), nor do we want
247 * to complain about it being unreachable (since it does
248 * not exist).
249 */
b4584e4f 250 if (!(obj->flags & HAS_OBJ))
18af29f2
LT
251 return;
252
253 /*
254 * Unreachable object that exists? Show it if asked to,
255 * since this is something that is prunable.
256 */
257 if (show_unreachable) {
97ca7ca8 258 printf("unreachable %s %s\n", printable_type(obj),
993a21b0 259 describe_object(obj));
18af29f2
LT
260 return;
261 }
262
263 /*
092c55d0 264 * "!USED" means that nothing at all points to it, including
3dff5379 265 * other unreachable objects. In other words, it's the "tip"
18af29f2
LT
266 * of some set of unreachable objects, usually a commit that
267 * got dropped.
268 *
269 * Such starting points are more interesting than some random
270 * set of unreachable objects, so we show them even if the user
271 * hasn't asked for _all_ unreachable objects. If you have
272 * deleted a branch by mistake, this is a prime candidate to
273 * start looking at, for example.
274 */
092c55d0 275 if (!(obj->flags & USED)) {
c6a13b2c 276 if (show_dangling)
97ca7ca8 277 printf("dangling %s %s\n", printable_type(obj),
993a21b0 278 describe_object(obj));
68f6c019 279 if (write_lost_and_found) {
fcd12db6 280 char *filename = git_pathdup("lost-found/%s/%s",
68f6c019 281 obj->type == OBJ_COMMIT ? "commit" : "other",
993a21b0 282 describe_object(obj));
68f6c019
JS
283 FILE *f;
284
dcf69262 285 if (safe_create_leading_directories_const(filename)) {
68f6c019 286 error("Could not create lost-found");
fcd12db6 287 free(filename);
68f6c019
JS
288 return;
289 }
23a9e071 290 f = xfopen(filename, "w");
16a7fcfe 291 if (obj->type == OBJ_BLOB) {
7eda0e4f 292 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
eb726f2d 293 die_errno("Could not write '%s'", filename);
16a7fcfe 294 } else
993a21b0 295 fprintf(f, "%s\n", describe_object(obj));
47d32af2 296 if (fclose(f))
d824cbba
TR
297 die_errno("Could not finish '%s'",
298 filename);
fcd12db6 299 free(filename);
68f6c019 300 }
18af29f2
LT
301 return;
302 }
303
304 /*
305 * Otherwise? It's there, it's unreachable, and some other unreachable
306 * object points to it. Ignore it - it's not interesting, and we showed
307 * all the interesting cases above.
308 */
309}
310
311static void check_object(struct object *obj)
312{
20f1eb6b 313 if (verbose)
993a21b0 314 fprintf(stderr, "Checking %s\n", describe_object(obj));
20f1eb6b 315
18af29f2
LT
316 if (obj->flags & REACHABLE)
317 check_reachable_object(obj);
318 else
319 check_unreachable_object(obj);
320}
f1f0d088 321
8ba0bbb2
LT
322static void check_connectivity(void)
323{
fc046a75 324 int i, max;
8ba0bbb2 325
04d39759
LT
326 /* Traverse the pending reachable objects */
327 traverse_reachable();
328
8ba0bbb2 329 /* Look up all the requirements, warn about missing objects.. */
fc046a75 330 max = get_max_object_index();
20f1eb6b
JS
331 if (verbose)
332 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
333
fc046a75 334 for (i = 0; i < max; i++) {
fc046a75 335 struct object *obj = get_indexed_object(i);
8ba0bbb2 336
18af29f2
LT
337 if (obj)
338 check_object(obj);
8ba0bbb2
LT
339 }
340}
341
c9486eb0 342static int fsck_obj(struct object *obj)
85003492 343{
83cd6f90
RS
344 int err;
345
ba002f3b 346 if (obj->flags & SEEN)
85003492 347 return 0;
ba002f3b 348 obj->flags |= SEEN;
85003492 349
20f1eb6b 350 if (verbose)
ba002f3b 351 fprintf(stderr, "Checking %s %s\n",
97ca7ca8 352 printable_type(obj), describe_object(obj));
42ea9cb2 353
22410549 354 if (fsck_walk(obj, NULL, &fsck_obj_options))
ba002f3b 355 objerror(obj, "broken links");
83cd6f90
RS
356 err = fsck_object(obj, NULL, 0, &fsck_obj_options);
357 if (err)
358 goto out;
1ea34e36 359
ba002f3b
MK
360 if (obj->type == OBJ_COMMIT) {
361 struct commit *commit = (struct commit *) obj;
de2eb7f6 362
ba002f3b 363 if (!commit->parents && show_root)
993a21b0 364 printf("root %s\n", describe_object(&commit->object));
ba002f3b 365 }
92d4c85d 366
ba002f3b
MK
367 if (obj->type == OBJ_TAG) {
368 struct tag *tag = (struct tag *) obj;
20f1eb6b 369
ba002f3b 370 if (show_tags && tag->tagged) {
97ca7ca8 371 printf("tagged %s %s", printable_type(tag->tagged),
993a21b0
JS
372 describe_object(tag->tagged));
373 printf(" (%s) in %s\n", tag->tag,
374 describe_object(&tag->object));
ba002f3b 375 }
92d4c85d 376 }
889262ea 377
83cd6f90
RS
378out:
379 if (obj->type == OBJ_TREE)
380 free_tree_buffer((struct tree *)obj);
381 if (obj->type == OBJ_COMMIT)
382 free_commit_buffer((struct commit *)obj);
383 return err;
20222118
LT
384}
385
9fd75046 386static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
c9486eb0
NTND
387 unsigned long size, void *buffer, int *eaten)
388{
ec9d2249
NTND
389 /*
390 * Note, buffer may be NULL if type is OBJ_BLOB. See
391 * verify_packfile(), data_valid variable for details.
392 */
c9486eb0 393 struct object *obj;
c251c83d 394 obj = parse_object_buffer(oid, type, size, buffer, eaten);
c9486eb0
NTND
395 if (!obj) {
396 errors_found |= ERROR_OBJECT;
9fd75046 397 return error("%s: object corrupt or missing", oid_to_hex(oid));
c9486eb0 398 }
092c55d0
JT
399 obj->flags &= ~(REACHABLE | SEEN);
400 obj->flags |= HAS_OBJ;
c9486eb0
NTND
401 return fsck_obj(obj);
402}
403
96f1e58f 404static int default_refs;
944d8589 405
9461d272 406static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
dddbad72 407 timestamp_t timestamp)
55dd5526
JH
408{
409 struct object *obj;
410
9461d272 411 if (!is_null_oid(oid)) {
412 obj = lookup_object(oid->hash);
c2d17b3b 413 if (obj && (obj->flags & HAS_OBJ)) {
90cf590f
JS
414 if (timestamp && name_objects)
415 add_decoration(fsck_walk_options.object_names,
416 obj,
cb71f8bd 417 xstrfmt("%s@{%"PRItime"}", refname, timestamp));
092c55d0 418 obj->flags |= USED;
271b8d25 419 mark_object_reachable(obj);
498f1f61 420 } else if (!is_promisor_object(oid)) {
9461d272 421 error("%s: invalid reflog entry %s", refname, oid_to_hex(oid));
19bf6c9b 422 errors_found |= ERROR_REACHABLE;
55dd5526
JH
423 }
424 }
d66ae59b
MH
425}
426
9461d272 427static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
dddbad72 428 const char *email, timestamp_t timestamp, int tz,
883d60fa 429 const char *message, void *cb_data)
55dd5526 430{
19bf6c9b 431 const char *refname = cb_data;
55dd5526 432
20f1eb6b
JS
433 if (verbose)
434 fprintf(stderr, "Checking reflog %s->%s\n",
9461d272 435 oid_to_hex(ooid), oid_to_hex(noid));
20f1eb6b 436
9461d272 437 fsck_handle_reflog_oid(refname, ooid, 0);
438 fsck_handle_reflog_oid(refname, noid, timestamp);
55dd5526
JH
439 return 0;
440}
441
635b99a0
MH
442static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
443 int flag, void *cb_data)
eb8381c8 444{
19bf6c9b 445 for_each_reflog_ent(logname, fsck_handle_reflog_ent, (void *)logname);
eb8381c8
NP
446 return 0;
447}
448
635b99a0
MH
449static int fsck_handle_ref(const char *refname, const struct object_id *oid,
450 int flag, void *cb_data)
1024932f 451{
1024932f
LT
452 struct object *obj;
453
c251c83d 454 obj = parse_object(oid);
8a498a05 455 if (!obj) {
43f25158
JT
456 if (is_promisor_object(oid)) {
457 /*
458 * Increment default_refs anyway, because this is a
459 * valid ref.
460 */
461 default_refs++;
462 return 0;
463 }
635b99a0 464 error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
30d1038d 465 errors_found |= ERROR_REACHABLE;
944d8589
LT
466 /* We'll continue with the rest despite the error.. */
467 return 0;
8a498a05 468 }
122f76f5 469 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
6232f62b 470 error("%s: not a commit", refname);
122f76f5
JH
471 errors_found |= ERROR_REFS;
472 }
944d8589 473 default_refs++;
092c55d0 474 obj->flags |= USED;
90cf590f
JS
475 if (name_objects)
476 add_decoration(fsck_walk_options.object_names,
477 obj, xstrdup(refname));
271b8d25 478 mark_object_reachable(obj);
55dd5526 479
7c4d07c7 480 return 0;
1024932f
LT
481}
482
1024932f
LT
483static void get_default_heads(void)
484{
635b99a0
MH
485 if (head_points_at && !is_null_oid(&head_oid))
486 fsck_handle_ref("HEAD", &head_oid, 0, NULL);
487 for_each_rawref(fsck_handle_ref, NULL);
566842f6 488 if (include_reflogs)
635b99a0 489 for_each_reflog(fsck_handle_reflog, NULL);
071fa89e
LT
490
491 /*
492 * Not having any default heads isn't really fatal, but
493 * it does mean that "--unreachable" no longer makes any
494 * sense (since in this case everything will obviously
495 * be unreachable by definition.
496 *
497 * Showing dangling objects is valid, though (as those
498 * dangling objects are likely lost heads).
499 *
500 * So we just print a warning about it, and clear the
501 * "show_unreachable" flag.
502 */
503 if (!default_refs) {
8eb2d0be 504 fprintf(stderr, "notice: No default references\n");
071fa89e
LT
505 show_unreachable = 0;
506 }
1024932f
LT
507}
508
76c1d9a0 509static struct object *parse_loose_object(const struct object_id *oid,
c68b489e
JK
510 const char *path)
511{
512 struct object *obj;
513 void *contents;
514 enum object_type type;
515 unsigned long size;
516 int eaten;
517
d61d87bd 518 if (read_loose_object(path, oid, &type, &size, &contents) < 0)
c68b489e
JK
519 return NULL;
520
521 if (!contents && type != OBJ_BLOB)
522 die("BUG: read_loose_object streamed a non-blob");
523
c251c83d 524 obj = parse_object_buffer(oid, type, size, contents, &eaten);
c68b489e
JK
525
526 if (!eaten)
527 free(contents);
528 return obj;
529}
530
76c1d9a0 531static int fsck_loose(const struct object_id *oid, const char *path, void *data)
f0766bf9 532{
76c1d9a0 533 struct object *obj = parse_loose_object(oid, path);
c68b489e
JK
534
535 if (!obj) {
536 errors_found |= ERROR_OBJECT;
537 error("%s: object corrupt or missing: %s",
76c1d9a0 538 oid_to_hex(oid), path);
c68b489e
JK
539 return 0; /* keep checking other objects */
540 }
541
092c55d0
JT
542 obj->flags &= ~(REACHABLE | SEEN);
543 obj->flags |= HAS_OBJ;
c68b489e 544 if (fsck_obj(obj))
f0766bf9
JK
545 errors_found |= ERROR_OBJECT;
546 return 0;
547}
548
549static int fsck_cruft(const char *basename, const char *path, void *data)
550{
551 if (!starts_with(basename, "tmp_obj_"))
552 fprintf(stderr, "bad sha1 file: %s\n", path);
553 return 0;
554}
555
70c49050 556static int fsck_subdir(unsigned int nr, const char *path, void *progress)
f0766bf9
JK
557{
558 display_progress(progress, nr + 1);
559 return 0;
560}
561
8a498a05
JH
562static void fsck_object_dir(const char *path)
563{
1e49f22f 564 struct progress *progress = NULL;
20f1eb6b
JS
565
566 if (verbose)
567 fprintf(stderr, "Checking object directory\n");
568
1e49f22f 569 if (show_progress)
754dbc43 570 progress = start_progress(_("Checking object directories"), 256);
f0766bf9
JK
571
572 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
573 progress);
574 display_progress(progress, 256);
1e49f22f 575 stop_progress(&progress);
8a498a05
JH
576}
577
c3330383
LT
578static int fsck_head_link(void)
579{
8eb2d0be 580 int null_is_error = 0;
8eb2d0be 581
20f1eb6b
JS
582 if (verbose)
583 fprintf(stderr, "Checking HEAD link\n");
584
49e61479 585 head_points_at = resolve_ref_unsafe("HEAD", 0, &head_oid, NULL);
122f76f5
JH
586 if (!head_points_at) {
587 errors_found |= ERROR_REFS;
8eb2d0be 588 return error("Invalid HEAD");
122f76f5 589 }
8eb2d0be
JH
590 if (!strcmp(head_points_at, "HEAD"))
591 /* detached HEAD */
592 null_is_error = 1;
122f76f5
JH
593 else if (!starts_with(head_points_at, "refs/heads/")) {
594 errors_found |= ERROR_REFS;
8098a178 595 return error("HEAD points to something strange (%s)",
5b10b091 596 head_points_at);
122f76f5 597 }
635b99a0 598 if (is_null_oid(&head_oid)) {
122f76f5
JH
599 if (null_is_error) {
600 errors_found |= ERROR_REFS;
8eb2d0be 601 return error("HEAD: detached HEAD points at nothing");
122f76f5 602 }
8eb2d0be
JH
603 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
604 head_points_at + 11);
605 }
c3330383
LT
606 return 0;
607}
608
53dc3f3e
JH
609static int fsck_cache_tree(struct cache_tree *it)
610{
611 int i;
612 int err = 0;
613
20f1eb6b
JS
614 if (verbose)
615 fprintf(stderr, "Checking cache tree\n");
616
53dc3f3e 617 if (0 <= it->entry_count) {
c251c83d 618 struct object *obj = parse_object(&it->oid);
6d60bbef
JH
619 if (!obj) {
620 error("%s: invalid sha1 pointer in cache-tree",
e0a92804 621 oid_to_hex(&it->oid));
122f76f5 622 errors_found |= ERROR_REFS;
6d60bbef
JH
623 return 1;
624 }
092c55d0 625 obj->flags |= USED;
90cf590f
JS
626 if (name_objects)
627 add_decoration(fsck_walk_options.object_names,
628 obj, xstrdup(":"));
a1cdc251 629 mark_object_reachable(obj);
1974632c 630 if (obj->type != OBJ_TREE)
53dc3f3e
JH
631 err |= objerror(obj, "non-tree in cache-tree");
632 }
633 for (i = 0; i < it->subtree_nr; i++)
634 err |= fsck_cache_tree(it->down[i]->cache_tree);
635 return err;
636}
637
76c1d9a0 638static void mark_object_for_connectivity(const struct object_id *oid)
3e3f8bd6 639{
76c1d9a0 640 struct object *obj = lookup_unknown_object(oid->hash);
3e3f8bd6
JK
641 obj->flags |= HAS_OBJ;
642}
643
76c1d9a0 644static int mark_loose_for_connectivity(const struct object_id *oid,
3e3f8bd6
JK
645 const char *path,
646 void *data)
647{
76c1d9a0 648 mark_object_for_connectivity(oid);
3e3f8bd6
JK
649 return 0;
650}
651
76c1d9a0 652static int mark_packed_for_connectivity(const struct object_id *oid,
3e3f8bd6
JK
653 struct packed_git *pack,
654 uint32_t pos,
655 void *data)
656{
76c1d9a0 657 mark_object_for_connectivity(oid);
3e3f8bd6
JK
658 return 0;
659}
660
5ac0a206 661static char const * const fsck_usage[] = {
9c9b4f2f 662 N_("git fsck [<options>] [<object>...]"),
5ac0a206
PH
663 NULL
664};
665
666static struct option fsck_opts[] = {
cf8fe315 667 OPT__VERBOSE(&verbose, N_("be verbose")),
d5d09d47 668 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
cf8fe315 669 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
d5d09d47
SB
670 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
671 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
672 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
673 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
674 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
02976bf8 675 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
d5d09d47
SB
676 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
677 OPT_BOOL(0, "lost-found", &write_lost_and_found,
cf8fe315
NTND
678 N_("write dangling objects in .git/lost-found")),
679 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
90cf590f 680 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
5ac0a206
PH
681 OPT_END(),
682};
e2b4f635 683
baffc0e7 684int cmd_fsck(int argc, const char **argv, const char *prefix)
20222118 685{
78e7b98f 686 int i;
e15ef669 687 struct alternate_object_database *alt;
20222118 688
8b4c0103
JT
689 /* fsck knows how to handle missing promisor objects */
690 fetch_if_missing = 0;
691
e2b4f635 692 errors_found = 0;
afc711b8 693 check_replace_refs = 0;
61e2b015 694
37782920 695 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
1e49f22f 696
22410549
JS
697 fsck_walk_options.walk = mark_object;
698 fsck_obj_options.walk = mark_used;
699 fsck_obj_options.error_func = fsck_error_func;
700 if (check_strict)
701 fsck_obj_options.strict = 1;
702
1e49f22f
NTND
703 if (show_progress == -1)
704 show_progress = isatty(2);
705 if (verbose)
706 show_progress = 0;
707
5ac0a206
PH
708 if (write_lost_and_found) {
709 check_full = 1;
710 include_reflogs = 0;
889262ea
LT
711 }
712
90cf590f
JS
713 if (name_objects)
714 fsck_walk_options.object_names =
715 xcalloc(1, sizeof(struct decoration));
716
2becf00f
JS
717 git_config(fsck_config, NULL);
718
c3330383 719 fsck_head_link();
3e3f8bd6
JK
720 if (connectivity_only) {
721 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
722 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
723 } else {
031dc927
SB
724 struct alternate_object_database *alt_odb_list;
725
02976bf8 726 fsck_object_dir(get_object_directory());
e15ef669 727
0b209034 728 prepare_alt_odb(the_repository);
031dc927 729 alt_odb_list = the_repository->objects->alt_odb_list;
597f9134
JK
730 for (alt = alt_odb_list; alt; alt = alt->next)
731 fsck_object_dir(alt->path);
e15ef669 732
3e3f8bd6
JK
733 if (check_full) {
734 struct packed_git *p;
735 uint32_t total = 0, count = 0;
736 struct progress *progress = NULL;
e15ef669 737
3e3f8bd6 738 if (show_progress) {
a80d72db
SB
739 for (p = get_packed_git(the_repository); p;
740 p = p->next) {
3e3f8bd6
JK
741 if (open_pack_index(p))
742 continue;
743 total += p->num_objects;
744 }
1e49f22f 745
3e3f8bd6
JK
746 progress = start_progress(_("Checking objects"), total);
747 }
a80d72db
SB
748 for (p = get_packed_git(the_repository); p;
749 p = p->next) {
3e3f8bd6
JK
750 /* verify gives error messages itself */
751 if (verify_pack(p, fsck_obj_buffer,
752 progress, count))
753 errors_found |= ERROR_PACK;
754 count += p->num_objects;
1e49f22f 755 }
3e3f8bd6 756 stop_progress(&progress);
1e49f22f 757 }
bcee6fd8
LT
758 }
759
3aed2fda 760 for (i = 0; i < argc; i++) {
a6080a0a 761 const char *arg = argv[i];
aca6065c 762 struct object_id oid;
763 if (!get_oid(arg, &oid)) {
764 struct object *obj = lookup_object(oid.hash);
e1a1388d 765
c2d17b3b 766 if (!obj || !(obj->flags & HAS_OBJ)) {
096c9b8b
JT
767 if (is_promisor_object(&oid))
768 continue;
aca6065c 769 error("%s: object missing", oid_to_hex(&oid));
c6c7b16d 770 errors_found |= ERROR_OBJECT;
e1a1388d 771 continue;
c6c7b16d 772 }
e1a1388d 773
092c55d0 774 obj->flags |= USED;
90cf590f
JS
775 if (name_objects)
776 add_decoration(fsck_walk_options.object_names,
777 obj, xstrdup(arg));
271b8d25 778 mark_object_reachable(obj);
d9839e03
LT
779 continue;
780 }
f1f0d088 781 error("invalid parameter: expected sha1, got '%s'", arg);
c6c7b16d 782 errors_found |= ERROR_OBJECT;
d9839e03 783 }
d9839e03 784
1024932f 785 /*
d1af002d 786 * If we've not been given any explicit head information, do the
e7bd907d
LT
787 * default ones from .git/refs. We also consider the index file
788 * in this case (ie this implies --cache).
1024932f 789 */
c3271a0e 790 if (!argc) {
1024932f
LT
791 get_default_heads();
792 keep_cache_objects = 1;
793 }
794
ae7c0c92 795 if (keep_cache_objects) {
a33fc72f 796 verify_index_checksum = 1;
00ec50e5 797 verify_ce_order = 1;
ae7c0c92
JH
798 read_cache();
799 for (i = 0; i < active_nr; i++) {
8d9721c8
LT
800 unsigned int mode;
801 struct blob *blob;
ae7c0c92 802 struct object *obj;
8d9721c8 803
7a51ed66 804 mode = active_cache[i]->ce_mode;
302b9282 805 if (S_ISGITLINK(mode))
8d9721c8 806 continue;
3aca1fc6 807 blob = lookup_blob(&active_cache[i]->oid);
ae7c0c92
JH
808 if (!blob)
809 continue;
810 obj = &blob->object;
092c55d0 811 obj->flags |= USED;
90cf590f
JS
812 if (name_objects)
813 add_decoration(fsck_walk_options.object_names,
814 obj,
815 xstrfmt(":%s", active_cache[i]->name));
271b8d25 816 mark_object_reachable(obj);
ae7c0c92 817 }
53dc3f3e
JH
818 if (active_cache_tree)
819 fsck_cache_tree(active_cache_tree);
ae7c0c92
JH
820 }
821
8ba0bbb2 822 check_connectivity();
e2b4f635 823 return errors_found;
20222118 824}