git-submodule.sh: Don't use $path variable in eval_gettext string
[git/mingw.git] / builtin / fsck.c
blob67eb553c7dc3d8ce62fbbefbe64a90c6431963c7
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"
16 #define REACHABLE 0x0001
17 #define SEEN 0x0002
19 static int show_root;
20 static int show_tags;
21 static int show_unreachable;
22 static int include_reflogs = 1;
23 static int check_full = 1;
24 static int check_strict;
25 static int keep_cache_objects;
26 static unsigned char head_sha1[20];
27 static const char *head_points_at;
28 static int errors_found;
29 static int write_lost_and_found;
30 static int verbose;
31 static int show_progress = -1;
32 static int show_dangling = 1;
33 #define ERROR_OBJECT 01
34 #define ERROR_REACHABLE 02
35 #define ERROR_PACK 04
37 #ifdef NO_D_INO_IN_DIRENT
38 #define SORT_DIRENT 0
39 #define DIRENT_SORT_HINT(de) 0
40 #else
41 #define SORT_DIRENT 1
42 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
43 #endif
45 static void objreport(struct object *obj, const char *severity,
46 const char *err, va_list params)
48 fprintf(stderr, "%s in %s %s: ",
49 severity, typename(obj->type), sha1_to_hex(obj->sha1));
50 vfprintf(stderr, err, params);
51 fputs("\n", stderr);
54 __attribute__((format (printf, 2, 3)))
55 static int objerror(struct object *obj, const char *err, ...)
57 va_list params;
58 va_start(params, err);
59 errors_found |= ERROR_OBJECT;
60 objreport(obj, "error", err, params);
61 va_end(params);
62 return -1;
65 __attribute__((format (printf, 3, 4)))
66 static int fsck_error_func(struct object *obj, int type, const char *err, ...)
68 va_list params;
69 va_start(params, err);
70 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params);
71 va_end(params);
72 return (type == FSCK_WARN) ? 0 : 1;
75 static struct object_array pending;
77 static int mark_object(struct object *obj, int type, void *data)
79 struct object *parent = data;
82 * The only case data is NULL or type is OBJ_ANY is when
83 * mark_object_reachable() calls us. All the callers of
84 * that function has non-NULL obj hence ...
86 if (!obj) {
87 /* ... these references to parent->fld are safe here */
88 printf("broken link from %7s %s\n",
89 typename(parent->type), sha1_to_hex(parent->sha1));
90 printf("broken link from %7s %s\n",
91 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
92 errors_found |= ERROR_REACHABLE;
93 return 1;
96 if (type != OBJ_ANY && obj->type != type)
97 /* ... and the reference to parent is safe here */
98 objerror(parent, "wrong object type in link");
100 if (obj->flags & REACHABLE)
101 return 0;
102 obj->flags |= REACHABLE;
103 if (!obj->parsed) {
104 if (parent && !has_sha1_file(obj->sha1)) {
105 printf("broken link from %7s %s\n",
106 typename(parent->type), sha1_to_hex(parent->sha1));
107 printf(" to %7s %s\n",
108 typename(obj->type), sha1_to_hex(obj->sha1));
109 errors_found |= ERROR_REACHABLE;
111 return 1;
114 add_object_array(obj, (void *) parent, &pending);
115 return 0;
118 static void mark_object_reachable(struct object *obj)
120 mark_object(obj, OBJ_ANY, NULL);
123 static int traverse_one_object(struct object *obj)
125 int result;
126 struct tree *tree = NULL;
128 if (obj->type == OBJ_TREE) {
129 obj->parsed = 0;
130 tree = (struct tree *)obj;
131 if (parse_tree(tree) < 0)
132 return 1; /* error already displayed */
134 result = fsck_walk(obj, mark_object, obj);
135 if (tree) {
136 free(tree->buffer);
137 tree->buffer = NULL;
139 return result;
142 static int traverse_reachable(void)
144 struct progress *progress = NULL;
145 unsigned int nr = 0;
146 int result = 0;
147 if (show_progress)
148 progress = start_progress_delay("Checking connectivity", 0, 0, 2);
149 while (pending.nr) {
150 struct object_array_entry *entry;
151 struct object *obj;
153 entry = pending.objects + --pending.nr;
154 obj = entry->item;
155 result |= traverse_one_object(obj);
156 display_progress(progress, ++nr);
158 stop_progress(&progress);
159 return !!result;
162 static int mark_used(struct object *obj, int type, void *data)
164 if (!obj)
165 return 1;
166 obj->used = 1;
167 return 0;
171 * Check a single reachable object
173 static void check_reachable_object(struct object *obj)
176 * We obviously want the object to be parsed,
177 * except if it was in a pack-file and we didn't
178 * do a full fsck
180 if (!obj->parsed) {
181 if (has_sha1_pack(obj->sha1))
182 return; /* it is in pack - forget about it */
183 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
184 errors_found |= ERROR_REACHABLE;
185 return;
190 * Check a single unreachable object
192 static void check_unreachable_object(struct object *obj)
195 * Missing unreachable object? Ignore it. It's not like
196 * we miss it (since it can't be reached), nor do we want
197 * to complain about it being unreachable (since it does
198 * not exist).
200 if (!obj->parsed)
201 return;
204 * Unreachable object that exists? Show it if asked to,
205 * since this is something that is prunable.
207 if (show_unreachable) {
208 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
209 return;
213 * "!used" means that nothing at all points to it, including
214 * other unreachable objects. In other words, it's the "tip"
215 * of some set of unreachable objects, usually a commit that
216 * got dropped.
218 * Such starting points are more interesting than some random
219 * set of unreachable objects, so we show them even if the user
220 * hasn't asked for _all_ unreachable objects. If you have
221 * deleted a branch by mistake, this is a prime candidate to
222 * start looking at, for example.
224 if (!obj->used) {
225 if (show_dangling)
226 printf("dangling %s %s\n", typename(obj->type),
227 sha1_to_hex(obj->sha1));
228 if (write_lost_and_found) {
229 char *filename = git_path("lost-found/%s/%s",
230 obj->type == OBJ_COMMIT ? "commit" : "other",
231 sha1_to_hex(obj->sha1));
232 FILE *f;
234 if (safe_create_leading_directories(filename)) {
235 error("Could not create lost-found");
236 return;
238 if (!(f = fopen(filename, "w")))
239 die_errno("Could not open '%s'", filename);
240 if (obj->type == OBJ_BLOB) {
241 enum object_type type;
242 unsigned long size;
243 char *buf = read_sha1_file(obj->sha1,
244 &type, &size);
245 if (buf && fwrite(buf, 1, size, f) != size)
246 die_errno("Could not write '%s'", filename);
247 free(buf);
248 } else
249 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
250 if (fclose(f))
251 die_errno("Could not finish '%s'",
252 filename);
254 return;
258 * Otherwise? It's there, it's unreachable, and some other unreachable
259 * object points to it. Ignore it - it's not interesting, and we showed
260 * all the interesting cases above.
264 static void check_object(struct object *obj)
266 if (verbose)
267 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
269 if (obj->flags & REACHABLE)
270 check_reachable_object(obj);
271 else
272 check_unreachable_object(obj);
275 static void check_connectivity(void)
277 int i, max;
279 /* Traverse the pending reachable objects */
280 traverse_reachable();
282 /* Look up all the requirements, warn about missing objects.. */
283 max = get_max_object_index();
284 if (verbose)
285 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
287 for (i = 0; i < max; i++) {
288 struct object *obj = get_indexed_object(i);
290 if (obj)
291 check_object(obj);
295 static int fsck_obj(struct object *obj)
297 if (obj->flags & SEEN)
298 return 0;
299 obj->flags |= SEEN;
301 if (verbose)
302 fprintf(stderr, "Checking %s %s\n",
303 typename(obj->type), sha1_to_hex(obj->sha1));
305 if (fsck_walk(obj, mark_used, NULL))
306 objerror(obj, "broken links");
307 if (fsck_object(obj, check_strict, fsck_error_func))
308 return -1;
310 if (obj->type == OBJ_TREE) {
311 struct tree *item = (struct tree *) obj;
313 free(item->buffer);
314 item->buffer = NULL;
317 if (obj->type == OBJ_COMMIT) {
318 struct commit *commit = (struct commit *) obj;
320 free(commit->buffer);
321 commit->buffer = NULL;
323 if (!commit->parents && show_root)
324 printf("root %s\n", sha1_to_hex(commit->object.sha1));
327 if (obj->type == OBJ_TAG) {
328 struct tag *tag = (struct tag *) obj;
330 if (show_tags && tag->tagged) {
331 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
332 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
336 return 0;
339 static int fsck_sha1(const unsigned char *sha1)
341 struct object *obj = parse_object(sha1);
342 if (!obj) {
343 errors_found |= ERROR_OBJECT;
344 return error("%s: object corrupt or missing",
345 sha1_to_hex(sha1));
347 return fsck_obj(obj);
350 static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
351 unsigned long size, void *buffer, int *eaten)
353 struct object *obj;
354 obj = parse_object_buffer(sha1, type, size, buffer, eaten);
355 if (!obj) {
356 errors_found |= ERROR_OBJECT;
357 return error("%s: object corrupt or missing", sha1_to_hex(sha1));
359 return fsck_obj(obj);
363 * This is the sorting chunk size: make it reasonably
364 * big so that we can sort well..
366 #define MAX_SHA1_ENTRIES (1024)
368 struct sha1_entry {
369 unsigned long ino;
370 unsigned char sha1[20];
373 static struct {
374 unsigned long nr;
375 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
376 } sha1_list;
378 static int ino_compare(const void *_a, const void *_b)
380 const struct sha1_entry *a = _a, *b = _b;
381 unsigned long ino1 = a->ino, ino2 = b->ino;
382 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
385 static void fsck_sha1_list(void)
387 int i, nr = sha1_list.nr;
389 if (SORT_DIRENT)
390 qsort(sha1_list.entry, nr,
391 sizeof(struct sha1_entry *), ino_compare);
392 for (i = 0; i < nr; i++) {
393 struct sha1_entry *entry = sha1_list.entry[i];
394 unsigned char *sha1 = entry->sha1;
396 sha1_list.entry[i] = NULL;
397 fsck_sha1(sha1);
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 (!prefixcmp(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 unsigned char *sha1, int flag, void *cb_data)
486 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
487 return 0;
490 static int is_branch(const char *refname)
492 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
495 static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
497 struct object *obj;
499 obj = parse_object(sha1);
500 if (!obj) {
501 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
502 /* We'll continue with the rest despite the error.. */
503 return 0;
505 if (obj->type != OBJ_COMMIT && is_branch(refname))
506 error("%s: not a commit", refname);
507 default_refs++;
508 obj->used = 1;
509 mark_object_reachable(obj);
511 return 0;
514 static void get_default_heads(void)
516 if (head_points_at && !is_null_sha1(head_sha1))
517 fsck_handle_ref("HEAD", head_sha1, 0, NULL);
518 for_each_ref(fsck_handle_ref, NULL);
519 if (include_reflogs)
520 for_each_reflog(fsck_handle_reflog, NULL);
523 * Not having any default heads isn't really fatal, but
524 * it does mean that "--unreachable" no longer makes any
525 * sense (since in this case everything will obviously
526 * be unreachable by definition.
528 * Showing dangling objects is valid, though (as those
529 * dangling objects are likely lost heads).
531 * So we just print a warning about it, and clear the
532 * "show_unreachable" flag.
534 if (!default_refs) {
535 fprintf(stderr, "notice: No default references\n");
536 show_unreachable = 0;
540 static void fsck_object_dir(const char *path)
542 int i;
543 struct progress *progress = NULL;
545 if (verbose)
546 fprintf(stderr, "Checking object directory\n");
548 if (show_progress)
549 progress = start_progress("Checking object directories", 256);
550 for (i = 0; i < 256; i++) {
551 static char dir[4096];
552 sprintf(dir, "%s/%02x", path, i);
553 fsck_dir(i, dir);
554 display_progress(progress, i+1);
556 stop_progress(&progress);
557 fsck_sha1_list();
560 static int fsck_head_link(void)
562 int flag;
563 int null_is_error = 0;
565 if (verbose)
566 fprintf(stderr, "Checking HEAD link\n");
568 head_points_at = resolve_ref_unsafe("HEAD", head_sha1, 0, &flag);
569 if (!head_points_at)
570 return error("Invalid HEAD");
571 if (!strcmp(head_points_at, "HEAD"))
572 /* detached HEAD */
573 null_is_error = 1;
574 else if (prefixcmp(head_points_at, "refs/heads/"))
575 return error("HEAD points to something strange (%s)",
576 head_points_at);
577 if (is_null_sha1(head_sha1)) {
578 if (null_is_error)
579 return error("HEAD: detached HEAD points at nothing");
580 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
581 head_points_at + 11);
583 return 0;
586 static int fsck_cache_tree(struct cache_tree *it)
588 int i;
589 int err = 0;
591 if (verbose)
592 fprintf(stderr, "Checking cache tree\n");
594 if (0 <= it->entry_count) {
595 struct object *obj = parse_object(it->sha1);
596 if (!obj) {
597 error("%s: invalid sha1 pointer in cache-tree",
598 sha1_to_hex(it->sha1));
599 return 1;
601 obj->used = 1;
602 mark_object_reachable(obj);
603 if (obj->type != OBJ_TREE)
604 err |= objerror(obj, "non-tree in cache-tree");
606 for (i = 0; i < it->subtree_nr; i++)
607 err |= fsck_cache_tree(it->down[i]->cache_tree);
608 return err;
611 static char const * const fsck_usage[] = {
612 "git fsck [options] [<object>...]",
613 NULL
616 static struct option fsck_opts[] = {
617 OPT__VERBOSE(&verbose, "be verbose"),
618 OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
619 OPT_BOOL(0, "dangling", &show_dangling, "show dangling objects"),
620 OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
621 OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
622 OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
623 OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
624 OPT_BOOLEAN(0, "full", &check_full, "also consider packs and alternate objects"),
625 OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
626 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
627 "write dangling objects in .git/lost-found"),
628 OPT_BOOL(0, "progress", &show_progress, "show progress"),
629 OPT_END(),
632 int cmd_fsck(int argc, const char **argv, const char *prefix)
634 int i, heads;
635 struct alternate_object_database *alt;
637 errors_found = 0;
638 read_replace_refs = 0;
640 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
642 if (show_progress == -1)
643 show_progress = isatty(2);
644 if (verbose)
645 show_progress = 0;
647 if (write_lost_and_found) {
648 check_full = 1;
649 include_reflogs = 0;
652 fsck_head_link();
653 fsck_object_dir(get_object_directory());
655 prepare_alt_odb();
656 for (alt = alt_odb_list; alt; alt = alt->next) {
657 char namebuf[PATH_MAX];
658 int namelen = alt->name - alt->base;
659 memcpy(namebuf, alt->base, namelen);
660 namebuf[namelen - 1] = 0;
661 fsck_object_dir(namebuf);
664 if (check_full) {
665 struct packed_git *p;
666 uint32_t total = 0, count = 0;
667 struct progress *progress = NULL;
669 prepare_packed_git();
671 if (show_progress) {
672 for (p = packed_git; p; p = p->next) {
673 if (open_pack_index(p))
674 continue;
675 total += p->num_objects;
678 progress = start_progress("Checking objects", total);
680 for (p = packed_git; p; p = p->next) {
681 /* verify gives error messages itself */
682 if (verify_pack(p, fsck_obj_buffer,
683 progress, count))
684 errors_found |= ERROR_PACK;
685 count += p->num_objects;
687 stop_progress(&progress);
690 heads = 0;
691 for (i = 0; i < argc; i++) {
692 const char *arg = argv[i];
693 unsigned char sha1[20];
694 if (!get_sha1(arg, sha1)) {
695 struct object *obj = lookup_object(sha1);
697 /* Error is printed by lookup_object(). */
698 if (!obj)
699 continue;
701 obj->used = 1;
702 mark_object_reachable(obj);
703 heads++;
704 continue;
706 error("invalid parameter: expected sha1, got '%s'", arg);
710 * If we've not been given any explicit head information, do the
711 * default ones from .git/refs. We also consider the index file
712 * in this case (ie this implies --cache).
714 if (!heads) {
715 get_default_heads();
716 keep_cache_objects = 1;
719 if (keep_cache_objects) {
720 read_cache();
721 for (i = 0; i < active_nr; i++) {
722 unsigned int mode;
723 struct blob *blob;
724 struct object *obj;
726 mode = active_cache[i]->ce_mode;
727 if (S_ISGITLINK(mode))
728 continue;
729 blob = lookup_blob(active_cache[i]->sha1);
730 if (!blob)
731 continue;
732 obj = &blob->object;
733 obj->used = 1;
734 mark_object_reachable(obj);
736 if (active_cache_tree)
737 fsck_cache_tree(active_cache_tree);
740 check_connectivity();
741 return errors_found;