gitweb: Syntax highlighting support
[git/dscho.git] / builtin-fsck.c
blobfb5cbd762fd9cac3ccb79a78b442f1bc0c35365a
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"
15 #define REACHABLE 0x0001
16 #define SEEN 0x0002
18 static int show_root;
19 static int show_tags;
20 static int show_unreachable;
21 static int include_reflogs = 1;
22 static int check_full = 1;
23 static int check_strict;
24 static int keep_cache_objects;
25 static unsigned char head_sha1[20];
26 static const char *head_points_at;
27 static int errors_found;
28 static int write_lost_and_found;
29 static int verbose;
30 #define ERROR_OBJECT 01
31 #define ERROR_REACHABLE 02
33 #ifdef NO_D_INO_IN_DIRENT
34 #define SORT_DIRENT 0
35 #define DIRENT_SORT_HINT(de) 0
36 #else
37 #define SORT_DIRENT 1
38 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
39 #endif
41 static void objreport(struct object *obj, const char *severity,
42 const char *err, va_list params)
44 fprintf(stderr, "%s in %s %s: ",
45 severity, typename(obj->type), sha1_to_hex(obj->sha1));
46 vfprintf(stderr, err, params);
47 fputs("\n", stderr);
50 __attribute__((format (printf, 2, 3)))
51 static int objerror(struct object *obj, const char *err, ...)
53 va_list params;
54 va_start(params, err);
55 errors_found |= ERROR_OBJECT;
56 objreport(obj, "error", err, params);
57 va_end(params);
58 return -1;
61 __attribute__((format (printf, 3, 4)))
62 static int fsck_error_func(struct object *obj, int type, const char *err, ...)
64 va_list params;
65 va_start(params, err);
66 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params);
67 va_end(params);
68 return (type == FSCK_WARN) ? 0 : 1;
71 static struct object_array pending;
73 static int mark_object(struct object *obj, int type, void *data)
75 struct object *parent = data;
77 if (!obj) {
78 printf("broken link from %7s %s\n",
79 typename(parent->type), sha1_to_hex(parent->sha1));
80 printf("broken link from %7s %s\n",
81 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
82 errors_found |= ERROR_REACHABLE;
83 return 1;
86 if (type != OBJ_ANY && obj->type != type)
87 objerror(parent, "wrong object type in link");
89 if (obj->flags & REACHABLE)
90 return 0;
91 obj->flags |= REACHABLE;
92 if (!obj->parsed) {
93 if (parent && !has_sha1_file(obj->sha1)) {
94 printf("broken link from %7s %s\n",
95 typename(parent->type), sha1_to_hex(parent->sha1));
96 printf(" to %7s %s\n",
97 typename(obj->type), sha1_to_hex(obj->sha1));
98 errors_found |= ERROR_REACHABLE;
100 return 1;
103 add_object_array(obj, (void *) parent, &pending);
104 return 0;
107 static void mark_object_reachable(struct object *obj)
109 mark_object(obj, OBJ_ANY, NULL);
112 static int traverse_one_object(struct object *obj, struct object *parent)
114 int result;
115 struct tree *tree = NULL;
117 if (obj->type == OBJ_TREE) {
118 obj->parsed = 0;
119 tree = (struct tree *)obj;
120 if (parse_tree(tree) < 0)
121 return 1; /* error already displayed */
123 result = fsck_walk(obj, mark_object, obj);
124 if (tree) {
125 free(tree->buffer);
126 tree->buffer = NULL;
128 return result;
131 static int traverse_reachable(void)
133 int result = 0;
134 while (pending.nr) {
135 struct object_array_entry *entry;
136 struct object *obj, *parent;
138 entry = pending.objects + --pending.nr;
139 obj = entry->item;
140 parent = (struct object *) entry->name;
141 result |= traverse_one_object(obj, parent);
143 return !!result;
146 static int mark_used(struct object *obj, int type, void *data)
148 if (!obj)
149 return 1;
150 obj->used = 1;
151 return 0;
155 * Check a single reachable object
157 static void check_reachable_object(struct object *obj)
160 * We obviously want the object to be parsed,
161 * except if it was in a pack-file and we didn't
162 * do a full fsck
164 if (!obj->parsed) {
165 if (has_sha1_pack(obj->sha1))
166 return; /* it is in pack - forget about it */
167 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
168 errors_found |= ERROR_REACHABLE;
169 return;
173 static void dangling_object(struct object *obj)
175 char *filename;
176 FILE *f;
177 enum object_type type;
178 unsigned long size;
179 char *buf = NULL;
181 if (!write_lost_and_found)
182 goto report_and_exit;
184 filename = git_path("lost-found/%s/%s",
185 obj->type == OBJ_COMMIT ? "commit" : "other",
186 sha1_to_hex(obj->sha1));
188 if (safe_create_leading_directories(filename)) {
189 error("Could not create lost-found");
190 return;
192 if (!(f = fopen(filename, "w")))
193 die_errno("Could not open '%s'", filename);
194 if (obj->type == OBJ_BLOB || obj->type == OBJ_COMMIT)
195 buf = read_sha1_file(obj->sha1, &type, &size);
197 if (obj->type == OBJ_BLOB) {
198 if (buf) {
199 if (fwrite(buf, size, 1, f) != 1)
200 die_errno("Could not write '%s'", filename);
201 free(buf);
203 } else
204 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
205 if (fclose(f))
206 die_errno("Could not finish '%s'", filename);
208 if (obj->type == OBJ_COMMIT) {
209 struct strbuf sb = STRBUF_INIT;
210 struct commit *commit = lookup_commit(obj->sha1);
211 int reported = 0;
213 if (!commit->buffer)
214 commit->buffer = buf;
215 if (commit->buffer) {
216 parse_commit(commit);
217 pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb,
218 0, NULL, NULL, 0, 0);
219 printf("dangling commit %s (%s)\n",
220 sha1_to_hex(obj->sha1), sb.buf);
221 reported = 1;
223 strbuf_release(&sb);
224 free(commit->buffer);
225 if (buf && commit->buffer != buf)
226 free(buf);
227 commit->buffer = NULL;
228 if (reported)
229 return;
232 report_and_exit:
233 printf("dangling %s %s\n", typename(obj->type),
234 sha1_to_hex(obj->sha1));
238 * Check a single unreachable object
240 static void check_unreachable_object(struct object *obj)
243 * Missing unreachable object? Ignore it. It's not like
244 * we miss it (since it can't be reached), nor do we want
245 * to complain about it being unreachable (since it does
246 * not exist).
248 if (!obj->parsed)
249 return;
252 * Unreachable object that exists? Show it if asked to,
253 * since this is something that is prunable.
255 if (show_unreachable) {
256 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
257 return;
261 * "!used" means that nothing at all points to it, including
262 * other unreachable objects. In other words, it's the "tip"
263 * of some set of unreachable objects, usually a commit that
264 * got dropped.
266 * Such starting points are more interesting than some random
267 * set of unreachable objects, so we show them even if the user
268 * hasn't asked for _all_ unreachable objects. If you have
269 * deleted a branch by mistake, this is a prime candidate to
270 * start looking at, for example.
272 if (!obj->used)
273 dangling_object(obj);
276 * Otherwise? It's there, it's unreachable, and some other unreachable
277 * object points to it. Ignore it - it's not interesting, and we showed
278 * all the interesting cases above.
282 static void check_object(struct object *obj)
284 if (verbose)
285 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
287 if (obj->flags & REACHABLE)
288 check_reachable_object(obj);
289 else
290 check_unreachable_object(obj);
293 static void check_connectivity(void)
295 int i, max;
297 /* Traverse the pending reachable objects */
298 traverse_reachable();
300 /* Look up all the requirements, warn about missing objects.. */
301 max = get_max_object_index();
302 if (verbose)
303 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
305 for (i = 0; i < max; i++) {
306 struct object *obj = get_indexed_object(i);
308 if (obj)
309 check_object(obj);
313 static int fsck_sha1(const unsigned char *sha1)
315 struct object *obj = parse_object(sha1);
316 if (!obj) {
317 errors_found |= ERROR_OBJECT;
318 return error("%s: object corrupt or missing",
319 sha1_to_hex(sha1));
321 if (obj->flags & SEEN)
322 return 0;
323 obj->flags |= SEEN;
325 if (verbose)
326 fprintf(stderr, "Checking %s %s\n",
327 typename(obj->type), sha1_to_hex(obj->sha1));
329 if (fsck_walk(obj, mark_used, NULL))
330 objerror(obj, "broken links");
331 if (fsck_object(obj, check_strict, fsck_error_func))
332 return -1;
334 if (obj->type == OBJ_TREE) {
335 struct tree *item = (struct tree *) obj;
337 free(item->buffer);
338 item->buffer = NULL;
341 if (obj->type == OBJ_COMMIT) {
342 struct commit *commit = (struct commit *) obj;
344 free(commit->buffer);
345 commit->buffer = NULL;
347 if (!commit->parents && show_root)
348 printf("root %s\n", sha1_to_hex(commit->object.sha1));
351 if (obj->type == OBJ_TAG) {
352 struct tag *tag = (struct tag *) obj;
354 if (show_tags && tag->tagged) {
355 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
356 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
360 return 0;
364 * This is the sorting chunk size: make it reasonably
365 * big so that we can sort well..
367 #define MAX_SHA1_ENTRIES (1024)
369 struct sha1_entry {
370 unsigned long ino;
371 unsigned char sha1[20];
374 static struct {
375 unsigned long nr;
376 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
377 } sha1_list;
379 static int ino_compare(const void *_a, const void *_b)
381 const struct sha1_entry *a = _a, *b = _b;
382 unsigned long ino1 = a->ino, ino2 = b->ino;
383 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
386 static void fsck_sha1_list(void)
388 int i, nr = sha1_list.nr;
390 if (SORT_DIRENT)
391 qsort(sha1_list.entry, nr,
392 sizeof(struct sha1_entry *), ino_compare);
393 for (i = 0; i < nr; i++) {
394 struct sha1_entry *entry = sha1_list.entry[i];
395 unsigned char *sha1 = entry->sha1;
397 sha1_list.entry[i] = NULL;
398 fsck_sha1(sha1);
399 free(entry);
401 sha1_list.nr = 0;
404 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
406 struct sha1_entry *entry = xmalloc(sizeof(*entry));
407 int nr;
409 entry->ino = ino;
410 hashcpy(entry->sha1, sha1);
411 nr = sha1_list.nr;
412 if (nr == MAX_SHA1_ENTRIES) {
413 fsck_sha1_list();
414 nr = 0;
416 sha1_list.entry[nr] = entry;
417 sha1_list.nr = ++nr;
420 static void fsck_dir(int i, char *path)
422 DIR *dir = opendir(path);
423 struct dirent *de;
425 if (!dir)
426 return;
428 if (verbose)
429 fprintf(stderr, "Checking directory %s\n", path);
431 while ((de = readdir(dir)) != NULL) {
432 char name[100];
433 unsigned char sha1[20];
435 if (is_dot_or_dotdot(de->d_name))
436 continue;
437 if (strlen(de->d_name) == 38) {
438 sprintf(name, "%02x", i);
439 memcpy(name+2, de->d_name, 39);
440 if (get_sha1_hex(name, sha1) < 0)
441 break;
442 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
443 continue;
445 if (!prefixcmp(de->d_name, "tmp_obj_"))
446 continue;
447 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
449 closedir(dir);
452 static int default_refs;
454 static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
455 const char *email, unsigned long timestamp, int tz,
456 const char *message, void *cb_data)
458 struct object *obj;
460 if (verbose)
461 fprintf(stderr, "Checking reflog %s->%s\n",
462 sha1_to_hex(osha1), sha1_to_hex(nsha1));
464 if (!is_null_sha1(osha1)) {
465 obj = lookup_object(osha1);
466 if (obj) {
467 obj->used = 1;
468 mark_object_reachable(obj);
471 obj = lookup_object(nsha1);
472 if (obj) {
473 obj->used = 1;
474 mark_object_reachable(obj);
476 return 0;
479 static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
481 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
482 return 0;
485 static int is_branch(const char *refname)
487 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
490 static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
492 struct object *obj;
494 obj = parse_object(sha1);
495 if (!obj) {
496 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
497 /* We'll continue with the rest despite the error.. */
498 return 0;
500 if (obj->type != OBJ_COMMIT && is_branch(refname))
501 error("%s: not a commit", refname);
502 default_refs++;
503 obj->used = 1;
504 mark_object_reachable(obj);
506 return 0;
509 static void get_default_heads(void)
511 if (head_points_at && !is_null_sha1(head_sha1))
512 fsck_handle_ref("HEAD", head_sha1, 0, NULL);
513 for_each_ref(fsck_handle_ref, NULL);
514 if (include_reflogs)
515 for_each_reflog(fsck_handle_reflog, NULL);
518 * Not having any default heads isn't really fatal, but
519 * it does mean that "--unreachable" no longer makes any
520 * sense (since in this case everything will obviously
521 * be unreachable by definition.
523 * Showing dangling objects is valid, though (as those
524 * dangling objects are likely lost heads).
526 * So we just print a warning about it, and clear the
527 * "show_unreachable" flag.
529 if (!default_refs) {
530 fprintf(stderr, "notice: No default references\n");
531 show_unreachable = 0;
535 static void fsck_object_dir(const char *path)
537 int i;
539 if (verbose)
540 fprintf(stderr, "Checking object directory\n");
542 for (i = 0; i < 256; i++) {
543 static char dir[4096];
544 sprintf(dir, "%s/%02x", path, i);
545 fsck_dir(i, dir);
547 fsck_sha1_list();
550 static int fsck_head_link(void)
552 int flag;
553 int null_is_error = 0;
555 if (verbose)
556 fprintf(stderr, "Checking HEAD link\n");
558 head_points_at = resolve_ref("HEAD", head_sha1, 0, &flag);
559 if (!head_points_at)
560 return error("Invalid HEAD");
561 if (!strcmp(head_points_at, "HEAD"))
562 /* detached HEAD */
563 null_is_error = 1;
564 else if (prefixcmp(head_points_at, "refs/heads/"))
565 return error("HEAD points to something strange (%s)",
566 head_points_at);
567 if (is_null_sha1(head_sha1)) {
568 if (null_is_error)
569 return error("HEAD: detached HEAD points at nothing");
570 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
571 head_points_at + 11);
573 return 0;
576 static int fsck_cache_tree(struct cache_tree *it)
578 int i;
579 int err = 0;
581 if (verbose)
582 fprintf(stderr, "Checking cache tree\n");
584 if (0 <= it->entry_count) {
585 struct object *obj = parse_object(it->sha1);
586 if (!obj) {
587 error("%s: invalid sha1 pointer in cache-tree",
588 sha1_to_hex(it->sha1));
589 return 1;
591 mark_object_reachable(obj);
592 obj->used = 1;
593 if (obj->type != OBJ_TREE)
594 err |= objerror(obj, "non-tree in cache-tree");
596 for (i = 0; i < it->subtree_nr; i++)
597 err |= fsck_cache_tree(it->down[i]->cache_tree);
598 return err;
601 static char const * const fsck_usage[] = {
602 "git fsck [options] [<object>...]",
603 NULL
606 static struct option fsck_opts[] = {
607 OPT__VERBOSE(&verbose),
608 OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
609 OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
610 OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
611 OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
612 OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
613 OPT_BOOLEAN(0, "full", &check_full, "also consider packs and alternate objects"),
614 OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
615 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
616 "write dangling objects in .git/lost-found"),
617 OPT_END(),
620 int cmd_fsck(int argc, const char **argv, const char *prefix)
622 int i, heads;
623 struct alternate_object_database *alt;
625 errors_found = 0;
626 read_replace_refs = 0;
628 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
629 if (write_lost_and_found) {
630 check_full = 1;
631 include_reflogs = 0;
634 fsck_head_link();
635 fsck_object_dir(get_object_directory());
637 prepare_alt_odb();
638 for (alt = alt_odb_list; alt; alt = alt->next) {
639 char namebuf[PATH_MAX];
640 int namelen = alt->name - alt->base;
641 memcpy(namebuf, alt->base, namelen);
642 namebuf[namelen - 1] = 0;
643 fsck_object_dir(namebuf);
646 if (check_full) {
647 struct packed_git *p;
649 prepare_packed_git();
650 for (p = packed_git; p; p = p->next)
651 /* verify gives error messages itself */
652 verify_pack(p);
654 for (p = packed_git; p; p = p->next) {
655 uint32_t j, num;
656 if (open_pack_index(p))
657 continue;
658 num = p->num_objects;
659 for (j = 0; j < num; j++)
660 fsck_sha1(nth_packed_object_sha1(p, j));
664 heads = 0;
665 for (i = 0; i < argc; i++) {
666 const char *arg = argv[i];
667 unsigned char sha1[20];
668 if (!get_sha1(arg, sha1)) {
669 struct object *obj = lookup_object(sha1);
671 /* Error is printed by lookup_object(). */
672 if (!obj)
673 continue;
675 obj->used = 1;
676 mark_object_reachable(obj);
677 heads++;
678 continue;
680 error("invalid parameter: expected sha1, got '%s'", arg);
684 * If we've not been given any explicit head information, do the
685 * default ones from .git/refs. We also consider the index file
686 * in this case (ie this implies --cache).
688 if (!heads) {
689 get_default_heads();
690 keep_cache_objects = 1;
693 if (keep_cache_objects) {
694 read_cache();
695 for (i = 0; i < active_nr; i++) {
696 unsigned int mode;
697 struct blob *blob;
698 struct object *obj;
700 mode = active_cache[i]->ce_mode;
701 if (S_ISGITLINK(mode))
702 continue;
703 blob = lookup_blob(active_cache[i]->sha1);
704 if (!blob)
705 continue;
706 obj = &blob->object;
707 obj->used = 1;
708 mark_object_reachable(obj);
710 if (active_cache_tree)
711 fsck_cache_tree(active_cache_tree);
714 check_connectivity();
715 return errors_found;