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