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