githooks.txt: add missing word
[tgit.git] / builtin-fsck.c
blob30971ce0ad170350cc6aaa2d213d348c267263db
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"
14 #define REACHABLE 0x0001
15 #define SEEN 0x0002
17 static int show_root;
18 static int show_tags;
19 static int show_unreachable;
20 static int include_reflogs = 1;
21 static int check_full;
22 static int check_strict;
23 static int keep_cache_objects;
24 static unsigned char head_sha1[20];
25 static int errors_found;
26 static int write_lost_and_found;
27 static int verbose;
28 #define ERROR_OBJECT 01
29 #define ERROR_REACHABLE 02
31 #ifdef NO_D_INO_IN_DIRENT
32 #define SORT_DIRENT 0
33 #define DIRENT_SORT_HINT(de) 0
34 #else
35 #define SORT_DIRENT 1
36 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
37 #endif
39 static void objreport(struct object *obj, const char *severity,
40 const char *err, va_list params)
42 fprintf(stderr, "%s in %s %s: ",
43 severity, typename(obj->type), sha1_to_hex(obj->sha1));
44 vfprintf(stderr, err, params);
45 fputs("\n", stderr);
48 static int objerror(struct object *obj, const char *err, ...)
50 va_list params;
51 va_start(params, err);
52 errors_found |= ERROR_OBJECT;
53 objreport(obj, "error", err, params);
54 va_end(params);
55 return -1;
58 static int fsck_error_func(struct object *obj, int type, const char *err, ...)
60 va_list params;
61 va_start(params, err);
62 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params);
63 va_end(params);
64 return (type == FSCK_WARN) ? 0 : 1;
67 static struct object_array pending;
69 static int mark_object(struct object *obj, int type, void *data)
71 struct object *parent = data;
73 if (!obj) {
74 printf("broken link from %7s %s\n",
75 typename(parent->type), sha1_to_hex(parent->sha1));
76 printf("broken link from %7s %s\n",
77 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
78 errors_found |= ERROR_REACHABLE;
79 return 1;
82 if (type != OBJ_ANY && obj->type != type)
83 objerror(parent, "wrong object type in link");
85 if (obj->flags & REACHABLE)
86 return 0;
87 obj->flags |= REACHABLE;
88 if (!obj->parsed) {
89 if (parent && !has_sha1_file(obj->sha1)) {
90 printf("broken link from %7s %s\n",
91 typename(parent->type), sha1_to_hex(parent->sha1));
92 printf(" to %7s %s\n",
93 typename(obj->type), sha1_to_hex(obj->sha1));
94 errors_found |= ERROR_REACHABLE;
96 return 1;
99 add_object_array(obj, (void *) parent, &pending);
100 return 0;
103 static void mark_object_reachable(struct object *obj)
105 mark_object(obj, OBJ_ANY, 0);
108 static int traverse_one_object(struct object *obj, struct object *parent)
110 int result;
111 struct tree *tree = NULL;
113 if (obj->type == OBJ_TREE) {
114 obj->parsed = 0;
115 tree = (struct tree *)obj;
116 if (parse_tree(tree) < 0)
117 return 1; /* error already displayed */
119 result = fsck_walk(obj, mark_object, obj);
120 if (tree) {
121 free(tree->buffer);
122 tree->buffer = NULL;
124 return result;
127 static int traverse_reachable(void)
129 int result = 0;
130 while (pending.nr) {
131 struct object_array_entry *entry;
132 struct object *obj, *parent;
134 entry = pending.objects + --pending.nr;
135 obj = entry->item;
136 parent = (struct object *) entry->name;
137 result |= traverse_one_object(obj, parent);
139 return !!result;
142 static int mark_used(struct object *obj, int type, void *data)
144 if (!obj)
145 return 1;
146 obj->used = 1;
147 return 0;
151 * Check a single reachable object
153 static void check_reachable_object(struct object *obj)
156 * We obviously want the object to be parsed,
157 * except if it was in a pack-file and we didn't
158 * do a full fsck
160 if (!obj->parsed) {
161 if (has_sha1_pack(obj->sha1, NULL))
162 return; /* it is in pack - forget about it */
163 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
164 errors_found |= ERROR_REACHABLE;
165 return;
170 * Check a single unreachable object
172 static void check_unreachable_object(struct object *obj)
175 * Missing unreachable object? Ignore it. It's not like
176 * we miss it (since it can't be reached), nor do we want
177 * to complain about it being unreachable (since it does
178 * not exist).
180 if (!obj->parsed)
181 return;
184 * Unreachable object that exists? Show it if asked to,
185 * since this is something that is prunable.
187 if (show_unreachable) {
188 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
189 return;
193 * "!used" means that nothing at all points to it, including
194 * other unreachable objects. In other words, it's the "tip"
195 * of some set of unreachable objects, usually a commit that
196 * got dropped.
198 * Such starting points are more interesting than some random
199 * set of unreachable objects, so we show them even if the user
200 * hasn't asked for _all_ unreachable objects. If you have
201 * deleted a branch by mistake, this is a prime candidate to
202 * start looking at, for example.
204 if (!obj->used) {
205 printf("dangling %s %s\n", typename(obj->type),
206 sha1_to_hex(obj->sha1));
207 if (write_lost_and_found) {
208 char *filename = git_path("lost-found/%s/%s",
209 obj->type == OBJ_COMMIT ? "commit" : "other",
210 sha1_to_hex(obj->sha1));
211 FILE *f;
213 if (safe_create_leading_directories(filename)) {
214 error("Could not create lost-found");
215 return;
217 if (!(f = fopen(filename, "w")))
218 die("Could not open %s", filename);
219 if (obj->type == OBJ_BLOB) {
220 enum object_type type;
221 unsigned long size;
222 char *buf = read_sha1_file(obj->sha1,
223 &type, &size);
224 if (buf) {
225 fwrite(buf, size, 1, f);
226 free(buf);
228 } else
229 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
230 fclose(f);
232 return;
236 * Otherwise? It's there, it's unreachable, and some other unreachable
237 * object points to it. Ignore it - it's not interesting, and we showed
238 * all the interesting cases above.
242 static void check_object(struct object *obj)
244 if (verbose)
245 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
247 if (obj->flags & REACHABLE)
248 check_reachable_object(obj);
249 else
250 check_unreachable_object(obj);
253 static void check_connectivity(void)
255 int i, max;
257 /* Traverse the pending reachable objects */
258 traverse_reachable();
260 /* Look up all the requirements, warn about missing objects.. */
261 max = get_max_object_index();
262 if (verbose)
263 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
265 for (i = 0; i < max; i++) {
266 struct object *obj = get_indexed_object(i);
268 if (obj)
269 check_object(obj);
273 static int fsck_sha1(const unsigned char *sha1)
275 struct object *obj = parse_object(sha1);
276 if (!obj) {
277 errors_found |= ERROR_OBJECT;
278 return error("%s: object corrupt or missing",
279 sha1_to_hex(sha1));
281 if (obj->flags & SEEN)
282 return 0;
283 obj->flags |= SEEN;
285 if (verbose)
286 fprintf(stderr, "Checking %s %s\n",
287 typename(obj->type), sha1_to_hex(obj->sha1));
289 if (fsck_walk(obj, mark_used, 0))
290 objerror(obj, "broken links");
291 if (fsck_object(obj, check_strict, fsck_error_func))
292 return -1;
294 if (obj->type == OBJ_TREE) {
295 struct tree *item = (struct tree *) obj;
297 free(item->buffer);
298 item->buffer = NULL;
301 if (obj->type == OBJ_COMMIT) {
302 struct commit *commit = (struct commit *) obj;
304 free(commit->buffer);
305 commit->buffer = NULL;
307 if (!commit->parents && show_root)
308 printf("root %s\n", sha1_to_hex(commit->object.sha1));
311 if (obj->type == OBJ_TAG) {
312 struct tag *tag = (struct tag *) obj;
314 if (show_tags && tag->tagged) {
315 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
316 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
320 return 0;
324 * This is the sorting chunk size: make it reasonably
325 * big so that we can sort well..
327 #define MAX_SHA1_ENTRIES (1024)
329 struct sha1_entry {
330 unsigned long ino;
331 unsigned char sha1[20];
334 static struct {
335 unsigned long nr;
336 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
337 } sha1_list;
339 static int ino_compare(const void *_a, const void *_b)
341 const struct sha1_entry *a = _a, *b = _b;
342 unsigned long ino1 = a->ino, ino2 = b->ino;
343 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
346 static void fsck_sha1_list(void)
348 int i, nr = sha1_list.nr;
350 if (SORT_DIRENT)
351 qsort(sha1_list.entry, nr,
352 sizeof(struct sha1_entry *), ino_compare);
353 for (i = 0; i < nr; i++) {
354 struct sha1_entry *entry = sha1_list.entry[i];
355 unsigned char *sha1 = entry->sha1;
357 sha1_list.entry[i] = NULL;
358 fsck_sha1(sha1);
359 free(entry);
361 sha1_list.nr = 0;
364 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
366 struct sha1_entry *entry = xmalloc(sizeof(*entry));
367 int nr;
369 entry->ino = ino;
370 hashcpy(entry->sha1, sha1);
371 nr = sha1_list.nr;
372 if (nr == MAX_SHA1_ENTRIES) {
373 fsck_sha1_list();
374 nr = 0;
376 sha1_list.entry[nr] = entry;
377 sha1_list.nr = ++nr;
380 static void fsck_dir(int i, char *path)
382 DIR *dir = opendir(path);
383 struct dirent *de;
385 if (!dir)
386 return;
388 if (verbose)
389 fprintf(stderr, "Checking directory %s\n", path);
391 while ((de = readdir(dir)) != NULL) {
392 char name[100];
393 unsigned char sha1[20];
394 int len = strlen(de->d_name);
396 switch (len) {
397 case 2:
398 if (de->d_name[1] != '.')
399 break;
400 case 1:
401 if (de->d_name[0] != '.')
402 break;
403 continue;
404 case 38:
405 sprintf(name, "%02x", i);
406 memcpy(name+2, de->d_name, len+1);
407 if (get_sha1_hex(name, sha1) < 0)
408 break;
409 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
410 continue;
412 if (!prefixcmp(de->d_name, "tmp_obj_"))
413 continue;
414 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
416 closedir(dir);
419 static int default_refs;
421 static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
422 const char *email, unsigned long timestamp, int tz,
423 const char *message, void *cb_data)
425 struct object *obj;
427 if (verbose)
428 fprintf(stderr, "Checking reflog %s->%s\n",
429 sha1_to_hex(osha1), sha1_to_hex(nsha1));
431 if (!is_null_sha1(osha1)) {
432 obj = lookup_object(osha1);
433 if (obj) {
434 obj->used = 1;
435 mark_object_reachable(obj);
438 obj = lookup_object(nsha1);
439 if (obj) {
440 obj->used = 1;
441 mark_object_reachable(obj);
443 return 0;
446 static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
448 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
449 return 0;
452 static int is_branch(const char *refname)
454 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
457 static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
459 struct object *obj;
461 obj = parse_object(sha1);
462 if (!obj) {
463 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
464 /* We'll continue with the rest despite the error.. */
465 return 0;
467 if (obj->type != OBJ_COMMIT && is_branch(refname))
468 error("%s: not a commit", refname);
469 default_refs++;
470 obj->used = 1;
471 mark_object_reachable(obj);
473 return 0;
476 static void get_default_heads(void)
478 for_each_ref(fsck_handle_ref, NULL);
479 if (include_reflogs)
480 for_each_reflog(fsck_handle_reflog, NULL);
483 * Not having any default heads isn't really fatal, but
484 * it does mean that "--unreachable" no longer makes any
485 * sense (since in this case everything will obviously
486 * be unreachable by definition.
488 * Showing dangling objects is valid, though (as those
489 * dangling objects are likely lost heads).
491 * So we just print a warning about it, and clear the
492 * "show_unreachable" flag.
494 if (!default_refs) {
495 fprintf(stderr, "notice: No default references\n");
496 show_unreachable = 0;
500 static void fsck_object_dir(const char *path)
502 int i;
504 if (verbose)
505 fprintf(stderr, "Checking object directory\n");
507 for (i = 0; i < 256; i++) {
508 static char dir[4096];
509 sprintf(dir, "%s/%02x", path, i);
510 fsck_dir(i, dir);
512 fsck_sha1_list();
515 static int fsck_head_link(void)
517 unsigned char sha1[20];
518 int flag;
519 int null_is_error = 0;
520 const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
522 if (verbose)
523 fprintf(stderr, "Checking HEAD link\n");
525 if (!head_points_at)
526 return error("Invalid HEAD");
527 if (!strcmp(head_points_at, "HEAD"))
528 /* detached HEAD */
529 null_is_error = 1;
530 else if (prefixcmp(head_points_at, "refs/heads/"))
531 return error("HEAD points to something strange (%s)",
532 head_points_at);
533 if (is_null_sha1(sha1)) {
534 if (null_is_error)
535 return error("HEAD: detached HEAD points at nothing");
536 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
537 head_points_at + 11);
539 return 0;
542 static int fsck_cache_tree(struct cache_tree *it)
544 int i;
545 int err = 0;
547 if (verbose)
548 fprintf(stderr, "Checking cache tree\n");
550 if (0 <= it->entry_count) {
551 struct object *obj = parse_object(it->sha1);
552 if (!obj) {
553 error("%s: invalid sha1 pointer in cache-tree",
554 sha1_to_hex(it->sha1));
555 return 1;
557 mark_object_reachable(obj);
558 obj->used = 1;
559 if (obj->type != OBJ_TREE)
560 err |= objerror(obj, "non-tree in cache-tree");
562 for (i = 0; i < it->subtree_nr; i++)
563 err |= fsck_cache_tree(it->down[i]->cache_tree);
564 return err;
567 static char const * const fsck_usage[] = {
568 "git fsck [options] [<object>...]",
569 NULL
572 static struct option fsck_opts[] = {
573 OPT__VERBOSE(&verbose),
574 OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
575 OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
576 OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
577 OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
578 OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
579 OPT_BOOLEAN(0, "full", &check_full, "also consider alternate objects"),
580 OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
581 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
582 "write dangling objects in .git/lost-found"),
583 OPT_END(),
586 int cmd_fsck(int argc, const char **argv, const char *prefix)
588 int i, heads;
590 errors_found = 0;
592 argc = parse_options(argc, argv, fsck_opts, fsck_usage, 0);
593 if (write_lost_and_found) {
594 check_full = 1;
595 include_reflogs = 0;
598 fsck_head_link();
599 fsck_object_dir(get_object_directory());
600 if (check_full) {
601 struct alternate_object_database *alt;
602 struct packed_git *p;
603 prepare_alt_odb();
604 for (alt = alt_odb_list; alt; alt = alt->next) {
605 char namebuf[PATH_MAX];
606 int namelen = alt->name - alt->base;
607 memcpy(namebuf, alt->base, namelen);
608 namebuf[namelen - 1] = 0;
609 fsck_object_dir(namebuf);
611 prepare_packed_git();
612 for (p = packed_git; p; p = p->next)
613 /* verify gives error messages itself */
614 verify_pack(p);
616 for (p = packed_git; p; p = p->next) {
617 uint32_t j, num;
618 if (open_pack_index(p))
619 continue;
620 num = p->num_objects;
621 for (j = 0; j < num; j++)
622 fsck_sha1(nth_packed_object_sha1(p, j));
626 heads = 0;
627 for (i = 1; i < argc; i++) {
628 const char *arg = argv[i];
629 if (!get_sha1(arg, head_sha1)) {
630 struct object *obj = lookup_object(head_sha1);
632 /* Error is printed by lookup_object(). */
633 if (!obj)
634 continue;
636 obj->used = 1;
637 mark_object_reachable(obj);
638 heads++;
639 continue;
641 error("invalid parameter: expected sha1, got '%s'", arg);
645 * If we've not been given any explicit head information, do the
646 * default ones from .git/refs. We also consider the index file
647 * in this case (ie this implies --cache).
649 if (!heads) {
650 get_default_heads();
651 keep_cache_objects = 1;
654 if (keep_cache_objects) {
655 read_cache();
656 for (i = 0; i < active_nr; i++) {
657 unsigned int mode;
658 struct blob *blob;
659 struct object *obj;
661 mode = active_cache[i]->ce_mode;
662 if (S_ISGITLINK(mode))
663 continue;
664 blob = lookup_blob(active_cache[i]->sha1);
665 if (!blob)
666 continue;
667 obj = &blob->object;
668 obj->used = 1;
669 mark_object_reachable(obj);
671 if (active_cache_tree)
672 fsck_cache_tree(active_cache_tree);
675 check_connectivity();
676 return errors_found;