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