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