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