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