fsck: introduce `git fsck --connectivity-only`
[git/gitweb.git] / builtin / fsck.c
blob070909ef208fe6815898b35539540c39da8448ad
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "blob.h"
6 #include "tag.h"
7 #include "refs.h"
8 #include "pack.h"
9 #include "cache-tree.h"
10 #include "tree-walk.h"
11 #include "fsck.h"
12 #include "parse-options.h"
13 #include "dir.h"
14 #include "progress.h"
15 #include "streaming.h"
17 #define REACHABLE 0x0001
18 #define SEEN 0x0002
19 #define HAS_OBJ 0x0004
21 static int show_root;
22 static int show_tags;
23 static int show_unreachable;
24 static int include_reflogs = 1;
25 static int check_full = 1;
26 static int connectivity_only;
27 static int check_strict;
28 static int keep_cache_objects;
29 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
30 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
31 static struct object_id head_oid;
32 static const char *head_points_at;
33 static int errors_found;
34 static int write_lost_and_found;
35 static int verbose;
36 static int show_progress = -1;
37 static int show_dangling = 1;
38 #define ERROR_OBJECT 01
39 #define ERROR_REACHABLE 02
40 #define ERROR_PACK 04
42 #ifdef NO_D_INO_IN_DIRENT
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
50 static int fsck_config(const char *var, const char *value, void *cb)
52 if (skip_prefix(var, "fsck.", &var)) {
53 fsck_set_msg_type(&fsck_obj_options, var, value);
54 return 0;
57 return git_default_config(var, value, cb);
60 static void objreport(struct object *obj, const char *msg_type,
61 const char *err)
63 fprintf(stderr, "%s in %s %s: %s\n",
64 msg_type, typename(obj->type), sha1_to_hex(obj->sha1), err);
67 static int objerror(struct object *obj, const char *err)
69 errors_found |= ERROR_OBJECT;
70 objreport(obj, "error", err);
71 return -1;
74 static int fsck_error_func(struct object *obj, int type, const char *message)
76 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", message);
77 return (type == FSCK_WARN) ? 0 : 1;
80 static struct object_array pending;
82 static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
84 struct object *parent = data;
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 ...
91 if (!obj) {
92 /* ... these references to parent->fld are safe here */
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;
101 if (type != OBJ_ANY && obj->type != type)
102 /* ... and the reference to parent is safe here */
103 objerror(parent, "wrong object type in link");
105 if (obj->flags & REACHABLE)
106 return 0;
107 obj->flags |= REACHABLE;
108 if (!(obj->flags & HAS_OBJ)) {
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;
116 return 1;
119 add_object_array(obj, NULL, &pending);
120 return 0;
123 static void mark_object_reachable(struct object *obj)
125 mark_object(obj, OBJ_ANY, NULL, NULL);
128 static int traverse_one_object(struct object *obj)
130 int result;
131 struct tree *tree = NULL;
133 if (obj->type == OBJ_TREE) {
134 tree = (struct tree *)obj;
135 if (parse_tree(tree) < 0)
136 return 1; /* error already displayed */
138 result = fsck_walk(obj, obj, &fsck_walk_options);
139 if (tree)
140 free_tree_buffer(tree);
141 return result;
144 static int traverse_reachable(void)
146 struct progress *progress = NULL;
147 unsigned int nr = 0;
148 int result = 0;
149 if (show_progress)
150 progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
151 while (pending.nr) {
152 struct object_array_entry *entry;
153 struct object *obj;
155 entry = pending.objects + --pending.nr;
156 obj = entry->item;
157 result |= traverse_one_object(obj);
158 display_progress(progress, ++nr);
160 stop_progress(&progress);
161 return !!result;
164 static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
166 if (!obj)
167 return 1;
168 obj->used = 1;
169 return 0;
173 * Check a single reachable object
175 static void check_reachable_object(struct object *obj)
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
182 if (!(obj->flags & HAS_OBJ)) {
183 if (has_sha1_pack(obj->sha1))
184 return; /* it is in pack - forget about it */
185 if (connectivity_only && has_sha1_file(obj->sha1))
186 return;
187 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
188 errors_found |= ERROR_REACHABLE;
189 return;
194 * Check a single unreachable object
196 static void check_unreachable_object(struct object *obj)
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).
204 if (!obj->parsed)
205 return;
208 * Unreachable object that exists? Show it if asked to,
209 * since this is something that is prunable.
211 if (show_unreachable) {
212 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
213 return;
217 * "!used" means that nothing at all points to it, including
218 * other unreachable objects. In other words, it's the "tip"
219 * of some set of unreachable objects, usually a commit that
220 * got dropped.
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.
228 if (!obj->used) {
229 if (show_dangling)
230 printf("dangling %s %s\n", typename(obj->type),
231 sha1_to_hex(obj->sha1));
232 if (write_lost_and_found) {
233 const char *filename = git_path("lost-found/%s/%s",
234 obj->type == OBJ_COMMIT ? "commit" : "other",
235 sha1_to_hex(obj->sha1));
236 FILE *f;
238 if (safe_create_leading_directories_const(filename)) {
239 error("Could not create lost-found");
240 return;
242 if (!(f = fopen(filename, "w")))
243 die_errno("Could not open '%s'", filename);
244 if (obj->type == OBJ_BLOB) {
245 if (stream_blob_to_fd(fileno(f), obj->sha1, NULL, 1))
246 die_errno("Could not write '%s'", filename);
247 } else
248 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
249 if (fclose(f))
250 die_errno("Could not finish '%s'",
251 filename);
253 return;
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.
263 static void check_object(struct object *obj)
265 if (verbose)
266 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
268 if (obj->flags & REACHABLE)
269 check_reachable_object(obj);
270 else
271 check_unreachable_object(obj);
274 static void check_connectivity(void)
276 int i, max;
278 /* Traverse the pending reachable objects */
279 traverse_reachable();
281 /* Look up all the requirements, warn about missing objects.. */
282 max = get_max_object_index();
283 if (verbose)
284 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
286 for (i = 0; i < max; i++) {
287 struct object *obj = get_indexed_object(i);
289 if (obj)
290 check_object(obj);
294 static int fsck_obj(struct object *obj)
296 if (obj->flags & SEEN)
297 return 0;
298 obj->flags |= SEEN;
300 if (verbose)
301 fprintf(stderr, "Checking %s %s\n",
302 typename(obj->type), sha1_to_hex(obj->sha1));
304 if (fsck_walk(obj, NULL, &fsck_obj_options))
305 objerror(obj, "broken links");
306 if (fsck_object(obj, NULL, 0, &fsck_obj_options))
307 return -1;
309 if (obj->type == OBJ_TREE) {
310 struct tree *item = (struct tree *) obj;
312 free_tree_buffer(item);
315 if (obj->type == OBJ_COMMIT) {
316 struct commit *commit = (struct commit *) obj;
318 free_commit_buffer(commit);
320 if (!commit->parents && show_root)
321 printf("root %s\n", sha1_to_hex(commit->object.sha1));
324 if (obj->type == OBJ_TAG) {
325 struct tag *tag = (struct tag *) obj;
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));
333 return 0;
336 static int fsck_sha1(const unsigned char *sha1)
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));
344 obj->flags |= HAS_OBJ;
345 return fsck_obj(obj);
348 static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
349 unsigned long size, void *buffer, int *eaten)
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));
357 obj->flags = HAS_OBJ;
358 return fsck_obj(obj);
362 * This is the sorting chunk size: make it reasonably
363 * big so that we can sort well..
365 #define MAX_SHA1_ENTRIES (1024)
367 struct sha1_entry {
368 unsigned long ino;
369 unsigned char sha1[20];
372 static struct {
373 unsigned long nr;
374 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
375 } sha1_list;
377 static int ino_compare(const void *_a, const void *_b)
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;
384 static void fsck_sha1_list(void)
386 int i, nr = sha1_list.nr;
388 if (SORT_DIRENT)
389 qsort(sha1_list.entry, nr,
390 sizeof(struct sha1_entry *), ino_compare);
391 for (i = 0; i < nr; i++) {
392 struct sha1_entry *entry = sha1_list.entry[i];
393 unsigned char *sha1 = entry->sha1;
395 sha1_list.entry[i] = NULL;
396 if (fsck_sha1(sha1))
397 errors_found |= ERROR_OBJECT;
398 free(entry);
400 sha1_list.nr = 0;
403 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
405 struct sha1_entry *entry = xmalloc(sizeof(*entry));
406 int nr;
408 entry->ino = ino;
409 hashcpy(entry->sha1, sha1);
410 nr = sha1_list.nr;
411 if (nr == MAX_SHA1_ENTRIES) {
412 fsck_sha1_list();
413 nr = 0;
415 sha1_list.entry[nr] = entry;
416 sha1_list.nr = ++nr;
419 static inline int is_loose_object_file(struct dirent *de,
420 char *name, unsigned char *sha1)
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);
428 static void fsck_dir(int i, char *path)
430 DIR *dir = opendir(path);
431 struct dirent *de;
432 char name[100];
434 if (!dir)
435 return;
437 if (verbose)
438 fprintf(stderr, "Checking directory %s\n", path);
440 sprintf(name, "%02x", i);
441 while ((de = readdir(dir)) != NULL) {
442 unsigned char sha1[20];
444 if (is_dot_or_dotdot(de->d_name))
445 continue;
446 if (is_loose_object_file(de, name, sha1)) {
447 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
448 continue;
450 if (starts_with(de->d_name, "tmp_obj_"))
451 continue;
452 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
454 closedir(dir);
457 static int default_refs;
459 static 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)
463 struct object *obj;
465 if (verbose)
466 fprintf(stderr, "Checking reflog %s->%s\n",
467 sha1_to_hex(osha1), sha1_to_hex(nsha1));
469 if (!is_null_sha1(osha1)) {
470 obj = lookup_object(osha1);
471 if (obj) {
472 obj->used = 1;
473 mark_object_reachable(obj);
476 obj = lookup_object(nsha1);
477 if (obj) {
478 obj->used = 1;
479 mark_object_reachable(obj);
481 return 0;
484 static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
485 int flag, void *cb_data)
487 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
488 return 0;
491 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
492 int flag, void *cb_data)
494 struct object *obj;
496 obj = parse_object(oid->hash);
497 if (!obj) {
498 error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
499 errors_found |= ERROR_REACHABLE;
500 /* We'll continue with the rest despite the error.. */
501 return 0;
503 if (obj->type != OBJ_COMMIT && is_branch(refname))
504 error("%s: not a commit", refname);
505 default_refs++;
506 obj->used = 1;
507 mark_object_reachable(obj);
509 return 0;
512 static void get_default_heads(void)
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);
517 if (include_reflogs)
518 for_each_reflog(fsck_handle_reflog, NULL);
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.
526 * Showing dangling objects is valid, though (as those
527 * dangling objects are likely lost heads).
529 * So we just print a warning about it, and clear the
530 * "show_unreachable" flag.
532 if (!default_refs) {
533 fprintf(stderr, "notice: No default references\n");
534 show_unreachable = 0;
538 static void fsck_object_dir(const char *path)
540 int i;
541 struct progress *progress = NULL;
543 if (verbose)
544 fprintf(stderr, "Checking object directory\n");
546 if (show_progress)
547 progress = start_progress(_("Checking object directories"), 256);
548 for (i = 0; i < 256; i++) {
549 static char dir[4096];
550 sprintf(dir, "%s/%02x", path, i);
551 fsck_dir(i, dir);
552 display_progress(progress, i+1);
554 stop_progress(&progress);
555 fsck_sha1_list();
558 static int fsck_head_link(void)
560 int flag;
561 int null_is_error = 0;
563 if (verbose)
564 fprintf(stderr, "Checking HEAD link\n");
566 head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, &flag);
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 (!starts_with(head_points_at, "refs/heads/"))
573 return error("HEAD points to something strange (%s)",
574 head_points_at);
575 if (is_null_oid(&head_oid)) {
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);
581 return 0;
584 static int fsck_cache_tree(struct cache_tree *it)
586 int i;
587 int err = 0;
589 if (verbose)
590 fprintf(stderr, "Checking cache tree\n");
592 if (0 <= it->entry_count) {
593 struct object *obj = parse_object(it->sha1);
594 if (!obj) {
595 error("%s: invalid sha1 pointer in cache-tree",
596 sha1_to_hex(it->sha1));
597 return 1;
599 obj->used = 1;
600 mark_object_reachable(obj);
601 if (obj->type != OBJ_TREE)
602 err |= objerror(obj, "non-tree in cache-tree");
604 for (i = 0; i < it->subtree_nr; i++)
605 err |= fsck_cache_tree(it->down[i]->cache_tree);
606 return err;
609 static char const * const fsck_usage[] = {
610 N_("git fsck [<options>] [<object>...]"),
611 NULL
614 static struct option fsck_opts[] = {
615 OPT__VERBOSE(&verbose, N_("be verbose")),
616 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
617 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
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")),
623 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
624 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
625 OPT_BOOL(0, "lost-found", &write_lost_and_found,
626 N_("write dangling objects in .git/lost-found")),
627 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
628 OPT_END(),
631 int cmd_fsck(int argc, const char **argv, const char *prefix)
633 int i, heads;
634 struct alternate_object_database *alt;
636 errors_found = 0;
637 check_replace_refs = 0;
639 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
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;
647 if (show_progress == -1)
648 show_progress = isatty(2);
649 if (verbose)
650 show_progress = 0;
652 if (write_lost_and_found) {
653 check_full = 1;
654 include_reflogs = 0;
657 git_config(fsck_config, NULL);
659 fsck_head_link();
660 if (!connectivity_only)
661 fsck_object_dir(get_object_directory());
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);
672 if (check_full) {
673 struct packed_git *p;
674 uint32_t total = 0, count = 0;
675 struct progress *progress = NULL;
677 prepare_packed_git();
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;
686 progress = start_progress(_("Checking objects"), total);
688 for (p = packed_git; p; p = p->next) {
689 /* verify gives error messages itself */
690 if (verify_pack(p, fsck_obj_buffer,
691 progress, count))
692 errors_found |= ERROR_PACK;
693 count += p->num_objects;
695 stop_progress(&progress);
698 heads = 0;
699 for (i = 0; i < argc; i++) {
700 const char *arg = argv[i];
701 unsigned char sha1[20];
702 if (!get_sha1(arg, sha1)) {
703 struct object *obj = lookup_object(sha1);
705 /* Error is printed by lookup_object(). */
706 if (!obj)
707 continue;
709 obj->used = 1;
710 mark_object_reachable(obj);
711 heads++;
712 continue;
714 error("invalid parameter: expected sha1, got '%s'", arg);
718 * If we've not been given any explicit head information, do the
719 * default ones from .git/refs. We also consider the index file
720 * in this case (ie this implies --cache).
722 if (!heads) {
723 get_default_heads();
724 keep_cache_objects = 1;
727 if (keep_cache_objects) {
728 read_cache();
729 for (i = 0; i < active_nr; i++) {
730 unsigned int mode;
731 struct blob *blob;
732 struct object *obj;
734 mode = active_cache[i]->ce_mode;
735 if (S_ISGITLINK(mode))
736 continue;
737 blob = lookup_blob(active_cache[i]->sha1);
738 if (!blob)
739 continue;
740 obj = &blob->object;
741 obj->used = 1;
742 mark_object_reachable(obj);
744 if (active_cache_tree)
745 fsck_cache_tree(active_cache_tree);
748 check_connectivity();
749 return errors_found;