fixup.cc5711424b7ae36276a40c06ede5d95f87ca20f0
[git/dscho.git] / builtin-fsck.c
blob091b4d25d66d09a8c0d76bf553aa459f21a95150
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;
180 struct pretty_print_context pp_context;
182 if (!write_lost_and_found)
183 goto report_and_exit;
185 memset(&pp_context, 0, sizeof(pp_context));
187 filename = git_path("lost-found/%s/%s",
188 obj->type == OBJ_COMMIT ? "commit" : "other",
189 sha1_to_hex(obj->sha1));
191 if (safe_create_leading_directories(filename)) {
192 error("Could not create lost-found");
193 return;
195 if (!(f = fopen(filename, "w")))
196 die_errno("Could not open '%s'", filename);
197 if (obj->type == OBJ_BLOB || obj->type == OBJ_COMMIT)
198 buf = read_sha1_file(obj->sha1, &type, &size);
200 if (obj->type == OBJ_BLOB) {
201 if (buf) {
202 if (fwrite(buf, size, 1, f) != 1)
203 die_errno("Could not write '%s'", filename);
204 free(buf);
206 } else
207 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
208 if (fclose(f))
209 die_errno("Could not finish '%s'", filename);
211 if (obj->type == OBJ_COMMIT) {
212 struct strbuf sb = STRBUF_INIT;
213 struct commit *commit = lookup_commit(obj->sha1);
214 int reported = 0;
216 if (!commit->buffer)
217 commit->buffer = buf;
218 if (commit->buffer) {
219 parse_commit(commit);
220 pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb,
221 &pp_context);
222 printf("dangling commit %s (%s)\n",
223 sha1_to_hex(obj->sha1), sb.buf);
224 reported = 1;
226 strbuf_release(&sb);
227 free(commit->buffer);
228 if (buf && commit->buffer != buf)
229 free(buf);
230 commit->buffer = NULL;
231 if (reported)
232 return;
235 report_and_exit:
236 printf("dangling %s %s\n", typename(obj->type),
237 sha1_to_hex(obj->sha1));
241 * Check a single unreachable object
243 static void check_unreachable_object(struct object *obj)
246 * Missing unreachable object? Ignore it. It's not like
247 * we miss it (since it can't be reached), nor do we want
248 * to complain about it being unreachable (since it does
249 * not exist).
251 if (!obj->parsed)
252 return;
255 * Unreachable object that exists? Show it if asked to,
256 * since this is something that is prunable.
258 if (show_unreachable) {
259 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
260 return;
264 * "!used" means that nothing at all points to it, including
265 * other unreachable objects. In other words, it's the "tip"
266 * of some set of unreachable objects, usually a commit that
267 * got dropped.
269 * Such starting points are more interesting than some random
270 * set of unreachable objects, so we show them even if the user
271 * hasn't asked for _all_ unreachable objects. If you have
272 * deleted a branch by mistake, this is a prime candidate to
273 * start looking at, for example.
275 if (!obj->used)
276 dangling_object(obj);
279 * Otherwise? It's there, it's unreachable, and some other unreachable
280 * object points to it. Ignore it - it's not interesting, and we showed
281 * all the interesting cases above.
285 static void check_object(struct object *obj)
287 if (verbose)
288 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
290 if (obj->flags & REACHABLE)
291 check_reachable_object(obj);
292 else
293 check_unreachable_object(obj);
296 static void check_connectivity(void)
298 int i, max;
300 /* Traverse the pending reachable objects */
301 traverse_reachable();
303 /* Look up all the requirements, warn about missing objects.. */
304 max = get_max_object_index();
305 if (verbose)
306 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
308 for (i = 0; i < max; i++) {
309 struct object *obj = get_indexed_object(i);
311 if (obj)
312 check_object(obj);
316 static int fsck_sha1(const unsigned char *sha1)
318 struct object *obj = parse_object(sha1);
319 if (!obj) {
320 errors_found |= ERROR_OBJECT;
321 return error("%s: object corrupt or missing",
322 sha1_to_hex(sha1));
324 if (obj->flags & SEEN)
325 return 0;
326 obj->flags |= SEEN;
328 if (verbose)
329 fprintf(stderr, "Checking %s %s\n",
330 typename(obj->type), sha1_to_hex(obj->sha1));
332 if (fsck_walk(obj, mark_used, NULL))
333 objerror(obj, "broken links");
334 if (fsck_object(obj, check_strict, fsck_error_func))
335 return -1;
337 if (obj->type == OBJ_TREE) {
338 struct tree *item = (struct tree *) obj;
340 free(item->buffer);
341 item->buffer = NULL;
344 if (obj->type == OBJ_COMMIT) {
345 struct commit *commit = (struct commit *) obj;
347 free(commit->buffer);
348 commit->buffer = NULL;
350 if (!commit->parents && show_root)
351 printf("root %s\n", sha1_to_hex(commit->object.sha1));
354 if (obj->type == OBJ_TAG) {
355 struct tag *tag = (struct tag *) obj;
357 if (show_tags && tag->tagged) {
358 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
359 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
363 return 0;
367 * This is the sorting chunk size: make it reasonably
368 * big so that we can sort well..
370 #define MAX_SHA1_ENTRIES (1024)
372 struct sha1_entry {
373 unsigned long ino;
374 unsigned char sha1[20];
377 static struct {
378 unsigned long nr;
379 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
380 } sha1_list;
382 static int ino_compare(const void *_a, const void *_b)
384 const struct sha1_entry *a = _a, *b = _b;
385 unsigned long ino1 = a->ino, ino2 = b->ino;
386 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
389 static void fsck_sha1_list(void)
391 int i, nr = sha1_list.nr;
393 if (SORT_DIRENT)
394 qsort(sha1_list.entry, nr,
395 sizeof(struct sha1_entry *), ino_compare);
396 for (i = 0; i < nr; i++) {
397 struct sha1_entry *entry = sha1_list.entry[i];
398 unsigned char *sha1 = entry->sha1;
400 sha1_list.entry[i] = NULL;
401 fsck_sha1(sha1);
402 free(entry);
404 sha1_list.nr = 0;
407 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
409 struct sha1_entry *entry = xmalloc(sizeof(*entry));
410 int nr;
412 entry->ino = ino;
413 hashcpy(entry->sha1, sha1);
414 nr = sha1_list.nr;
415 if (nr == MAX_SHA1_ENTRIES) {
416 fsck_sha1_list();
417 nr = 0;
419 sha1_list.entry[nr] = entry;
420 sha1_list.nr = ++nr;
423 static void fsck_dir(int i, char *path)
425 DIR *dir = opendir(path);
426 struct dirent *de;
428 if (!dir)
429 return;
431 if (verbose)
432 fprintf(stderr, "Checking directory %s\n", path);
434 while ((de = readdir(dir)) != NULL) {
435 char name[100];
436 unsigned char sha1[20];
438 if (is_dot_or_dotdot(de->d_name))
439 continue;
440 if (strlen(de->d_name) == 38) {
441 sprintf(name, "%02x", i);
442 memcpy(name+2, de->d_name, 39);
443 if (get_sha1_hex(name, sha1) < 0)
444 break;
445 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
446 continue;
448 if (!prefixcmp(de->d_name, "tmp_obj_"))
449 continue;
450 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
452 closedir(dir);
455 static int default_refs;
457 static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
458 const char *email, unsigned long timestamp, int tz,
459 const char *message, void *cb_data)
461 struct object *obj;
463 if (verbose)
464 fprintf(stderr, "Checking reflog %s->%s\n",
465 sha1_to_hex(osha1), sha1_to_hex(nsha1));
467 if (!is_null_sha1(osha1)) {
468 obj = lookup_object(osha1);
469 if (obj) {
470 obj->used = 1;
471 mark_object_reachable(obj);
474 obj = lookup_object(nsha1);
475 if (obj) {
476 obj->used = 1;
477 mark_object_reachable(obj);
479 return 0;
482 static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
484 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
485 return 0;
488 static int is_branch(const char *refname)
490 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
493 static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
495 struct object *obj;
497 obj = parse_object(sha1);
498 if (!obj) {
499 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
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_sha1(head_sha1))
515 fsck_handle_ref("HEAD", head_sha1, 0, NULL);
516 for_each_ref(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;
542 if (verbose)
543 fprintf(stderr, "Checking object directory\n");
545 for (i = 0; i < 256; i++) {
546 static char dir[4096];
547 sprintf(dir, "%s/%02x", path, i);
548 fsck_dir(i, dir);
550 fsck_sha1_list();
553 static int fsck_head_link(void)
555 int flag;
556 int null_is_error = 0;
558 if (verbose)
559 fprintf(stderr, "Checking HEAD link\n");
561 head_points_at = resolve_ref("HEAD", head_sha1, 0, &flag);
562 if (!head_points_at)
563 return error("Invalid HEAD");
564 if (!strcmp(head_points_at, "HEAD"))
565 /* detached HEAD */
566 null_is_error = 1;
567 else if (prefixcmp(head_points_at, "refs/heads/"))
568 return error("HEAD points to something strange (%s)",
569 head_points_at);
570 if (is_null_sha1(head_sha1)) {
571 if (null_is_error)
572 return error("HEAD: detached HEAD points at nothing");
573 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
574 head_points_at + 11);
576 return 0;
579 static int fsck_cache_tree(struct cache_tree *it)
581 int i;
582 int err = 0;
584 if (verbose)
585 fprintf(stderr, "Checking cache tree\n");
587 if (0 <= it->entry_count) {
588 struct object *obj = parse_object(it->sha1);
589 if (!obj) {
590 error("%s: invalid sha1 pointer in cache-tree",
591 sha1_to_hex(it->sha1));
592 return 1;
594 mark_object_reachable(obj);
595 obj->used = 1;
596 if (obj->type != OBJ_TREE)
597 err |= objerror(obj, "non-tree in cache-tree");
599 for (i = 0; i < it->subtree_nr; i++)
600 err |= fsck_cache_tree(it->down[i]->cache_tree);
601 return err;
604 static char const * const fsck_usage[] = {
605 "git fsck [options] [<object>...]",
606 NULL
609 static struct option fsck_opts[] = {
610 OPT__VERBOSE(&verbose),
611 OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
612 OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
613 OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
614 OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
615 OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
616 OPT_BOOLEAN(0, "full", &check_full, "also consider packs and alternate objects"),
617 OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
618 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
619 "write dangling objects in .git/lost-found"),
620 OPT_END(),
623 int cmd_fsck(int argc, const char **argv, const char *prefix)
625 int i, heads;
626 struct alternate_object_database *alt;
628 errors_found = 0;
629 read_replace_refs = 0;
631 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
632 if (write_lost_and_found) {
633 check_full = 1;
634 include_reflogs = 0;
637 fsck_head_link();
638 fsck_object_dir(get_object_directory());
640 prepare_alt_odb();
641 for (alt = alt_odb_list; alt; alt = alt->next) {
642 char namebuf[PATH_MAX];
643 int namelen = alt->name - alt->base;
644 memcpy(namebuf, alt->base, namelen);
645 namebuf[namelen - 1] = 0;
646 fsck_object_dir(namebuf);
649 if (check_full) {
650 struct packed_git *p;
652 prepare_packed_git();
653 for (p = packed_git; p; p = p->next)
654 /* verify gives error messages itself */
655 verify_pack(p);
657 for (p = packed_git; p; p = p->next) {
658 uint32_t j, num;
659 if (open_pack_index(p))
660 continue;
661 num = p->num_objects;
662 for (j = 0; j < num; j++)
663 fsck_sha1(nth_packed_object_sha1(p, j));
667 heads = 0;
668 for (i = 0; i < argc; i++) {
669 const char *arg = argv[i];
670 unsigned char sha1[20];
671 if (!get_sha1(arg, sha1)) {
672 struct object *obj = lookup_object(sha1);
674 /* Error is printed by lookup_object(). */
675 if (!obj)
676 continue;
678 obj->used = 1;
679 mark_object_reachable(obj);
680 heads++;
681 continue;
683 error("invalid parameter: expected sha1, got '%s'", arg);
687 * If we've not been given any explicit head information, do the
688 * default ones from .git/refs. We also consider the index file
689 * in this case (ie this implies --cache).
691 if (!heads) {
692 get_default_heads();
693 keep_cache_objects = 1;
696 if (keep_cache_objects) {
697 read_cache();
698 for (i = 0; i < active_nr; i++) {
699 unsigned int mode;
700 struct blob *blob;
701 struct object *obj;
703 mode = active_cache[i]->ce_mode;
704 if (S_ISGITLINK(mode))
705 continue;
706 blob = lookup_blob(active_cache[i]->sha1);
707 if (!blob)
708 continue;
709 obj = &blob->object;
710 obj->used = 1;
711 mark_object_reachable(obj);
713 if (active_cache_tree)
714 fsck_cache_tree(active_cache_tree);
717 check_connectivity();
718 return errors_found;