Merge branch 'tb/ref-filter-empty-modifier' into maint
[git/gitweb.git] / builtin / fsck.c
blobd18244ab546bae863e6ef81d58663dc5c27c49c0
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "commit.h"
5 #include "tree.h"
6 #include "blob.h"
7 #include "tag.h"
8 #include "refs.h"
9 #include "pack.h"
10 #include "cache-tree.h"
11 #include "tree-walk.h"
12 #include "fsck.h"
13 #include "parse-options.h"
14 #include "dir.h"
15 #include "progress.h"
16 #include "streaming.h"
17 #include "decorate.h"
19 #define REACHABLE 0x0001
20 #define SEEN 0x0002
21 #define HAS_OBJ 0x0004
22 /* This flag is set if something points to this object. */
23 #define USED 0x0008
25 static int show_root;
26 static int show_tags;
27 static int show_unreachable;
28 static int include_reflogs = 1;
29 static int check_full = 1;
30 static int connectivity_only;
31 static int check_strict;
32 static int keep_cache_objects;
33 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
34 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
35 static struct object_id head_oid;
36 static const char *head_points_at;
37 static int errors_found;
38 static int write_lost_and_found;
39 static int verbose;
40 static int show_progress = -1;
41 static int show_dangling = 1;
42 static int name_objects;
43 #define ERROR_OBJECT 01
44 #define ERROR_REACHABLE 02
45 #define ERROR_PACK 04
46 #define ERROR_REFS 010
48 static const char *describe_object(struct object *obj)
50 static struct strbuf buf = STRBUF_INIT;
51 char *name = name_objects ?
52 lookup_decoration(fsck_walk_options.object_names, obj) : NULL;
54 strbuf_reset(&buf);
55 strbuf_addstr(&buf, oid_to_hex(&obj->oid));
56 if (name)
57 strbuf_addf(&buf, " (%s)", name);
59 return buf.buf;
62 static const char *printable_type(struct object *obj)
64 const char *ret;
66 if (obj->type == OBJ_NONE) {
67 enum object_type type = sha1_object_info(obj->oid.hash, NULL);
68 if (type > 0)
69 object_as_type(obj, type, 0);
72 ret = typename(obj->type);
73 if (!ret)
74 ret = "unknown";
76 return ret;
79 static int fsck_config(const char *var, const char *value, void *cb)
81 if (strcmp(var, "fsck.skiplist") == 0) {
82 const char *path;
83 struct strbuf sb = STRBUF_INIT;
85 if (git_config_pathname(&path, var, value))
86 return 1;
87 strbuf_addf(&sb, "skiplist=%s", path);
88 free((char *)path);
89 fsck_set_msg_types(&fsck_obj_options, sb.buf);
90 strbuf_release(&sb);
91 return 0;
94 if (skip_prefix(var, "fsck.", &var)) {
95 fsck_set_msg_type(&fsck_obj_options, var, value);
96 return 0;
99 return git_default_config(var, value, cb);
102 static void objreport(struct object *obj, const char *msg_type,
103 const char *err)
105 fprintf(stderr, "%s in %s %s: %s\n",
106 msg_type, printable_type(obj), describe_object(obj), err);
109 static int objerror(struct object *obj, const char *err)
111 errors_found |= ERROR_OBJECT;
112 objreport(obj, "error", err);
113 return -1;
116 static int fsck_error_func(struct fsck_options *o,
117 struct object *obj, int type, const char *message)
119 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", message);
120 return (type == FSCK_WARN) ? 0 : 1;
123 static struct object_array pending;
125 static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
127 struct object *parent = data;
130 * The only case data is NULL or type is OBJ_ANY is when
131 * mark_object_reachable() calls us. All the callers of
132 * that function has non-NULL obj hence ...
134 if (!obj) {
135 /* ... these references to parent->fld are safe here */
136 printf("broken link from %7s %s\n",
137 printable_type(parent), describe_object(parent));
138 printf("broken link from %7s %s\n",
139 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
140 errors_found |= ERROR_REACHABLE;
141 return 1;
144 if (type != OBJ_ANY && obj->type != type)
145 /* ... and the reference to parent is safe here */
146 objerror(parent, "wrong object type in link");
148 if (obj->flags & REACHABLE)
149 return 0;
150 obj->flags |= REACHABLE;
151 if (!(obj->flags & HAS_OBJ)) {
152 if (parent && !has_object_file(&obj->oid)) {
153 printf("broken link from %7s %s\n",
154 printable_type(parent), describe_object(parent));
155 printf(" to %7s %s\n",
156 printable_type(obj), describe_object(obj));
157 errors_found |= ERROR_REACHABLE;
159 return 1;
162 add_object_array(obj, NULL, &pending);
163 return 0;
166 static void mark_object_reachable(struct object *obj)
168 mark_object(obj, OBJ_ANY, NULL, NULL);
171 static int traverse_one_object(struct object *obj)
173 return fsck_walk(obj, obj, &fsck_walk_options);
176 static int traverse_reachable(void)
178 struct progress *progress = NULL;
179 unsigned int nr = 0;
180 int result = 0;
181 if (show_progress)
182 progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
183 while (pending.nr) {
184 struct object_array_entry *entry;
185 struct object *obj;
187 entry = pending.objects + --pending.nr;
188 obj = entry->item;
189 result |= traverse_one_object(obj);
190 display_progress(progress, ++nr);
192 stop_progress(&progress);
193 return !!result;
196 static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
198 if (!obj)
199 return 1;
200 obj->flags |= USED;
201 return 0;
205 * Check a single reachable object
207 static void check_reachable_object(struct object *obj)
210 * We obviously want the object to be parsed,
211 * except if it was in a pack-file and we didn't
212 * do a full fsck
214 if (!(obj->flags & HAS_OBJ)) {
215 if (has_sha1_pack(obj->oid.hash))
216 return; /* it is in pack - forget about it */
217 printf("missing %s %s\n", printable_type(obj),
218 describe_object(obj));
219 errors_found |= ERROR_REACHABLE;
220 return;
225 * Check a single unreachable object
227 static void check_unreachable_object(struct object *obj)
230 * Missing unreachable object? Ignore it. It's not like
231 * we miss it (since it can't be reached), nor do we want
232 * to complain about it being unreachable (since it does
233 * not exist).
235 if (!(obj->flags & HAS_OBJ))
236 return;
239 * Unreachable object that exists? Show it if asked to,
240 * since this is something that is prunable.
242 if (show_unreachable) {
243 printf("unreachable %s %s\n", printable_type(obj),
244 describe_object(obj));
245 return;
249 * "!USED" means that nothing at all points to it, including
250 * other unreachable objects. In other words, it's the "tip"
251 * of some set of unreachable objects, usually a commit that
252 * got dropped.
254 * Such starting points are more interesting than some random
255 * set of unreachable objects, so we show them even if the user
256 * hasn't asked for _all_ unreachable objects. If you have
257 * deleted a branch by mistake, this is a prime candidate to
258 * start looking at, for example.
260 if (!(obj->flags & USED)) {
261 if (show_dangling)
262 printf("dangling %s %s\n", printable_type(obj),
263 describe_object(obj));
264 if (write_lost_and_found) {
265 char *filename = git_pathdup("lost-found/%s/%s",
266 obj->type == OBJ_COMMIT ? "commit" : "other",
267 describe_object(obj));
268 FILE *f;
270 if (safe_create_leading_directories_const(filename)) {
271 error("Could not create lost-found");
272 free(filename);
273 return;
275 f = xfopen(filename, "w");
276 if (obj->type == OBJ_BLOB) {
277 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
278 die_errno("Could not write '%s'", filename);
279 } else
280 fprintf(f, "%s\n", describe_object(obj));
281 if (fclose(f))
282 die_errno("Could not finish '%s'",
283 filename);
284 free(filename);
286 return;
290 * Otherwise? It's there, it's unreachable, and some other unreachable
291 * object points to it. Ignore it - it's not interesting, and we showed
292 * all the interesting cases above.
296 static void check_object(struct object *obj)
298 if (verbose)
299 fprintf(stderr, "Checking %s\n", describe_object(obj));
301 if (obj->flags & REACHABLE)
302 check_reachable_object(obj);
303 else
304 check_unreachable_object(obj);
307 static void check_connectivity(void)
309 int i, max;
311 /* Traverse the pending reachable objects */
312 traverse_reachable();
314 /* Look up all the requirements, warn about missing objects.. */
315 max = get_max_object_index();
316 if (verbose)
317 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
319 for (i = 0; i < max; i++) {
320 struct object *obj = get_indexed_object(i);
322 if (obj)
323 check_object(obj);
327 static int fsck_obj(struct object *obj)
329 int err;
331 if (obj->flags & SEEN)
332 return 0;
333 obj->flags |= SEEN;
335 if (verbose)
336 fprintf(stderr, "Checking %s %s\n",
337 printable_type(obj), describe_object(obj));
339 if (fsck_walk(obj, NULL, &fsck_obj_options))
340 objerror(obj, "broken links");
341 err = fsck_object(obj, NULL, 0, &fsck_obj_options);
342 if (err)
343 goto out;
345 if (obj->type == OBJ_COMMIT) {
346 struct commit *commit = (struct commit *) obj;
348 if (!commit->parents && show_root)
349 printf("root %s\n", describe_object(&commit->object));
352 if (obj->type == OBJ_TAG) {
353 struct tag *tag = (struct tag *) obj;
355 if (show_tags && tag->tagged) {
356 printf("tagged %s %s", printable_type(tag->tagged),
357 describe_object(tag->tagged));
358 printf(" (%s) in %s\n", tag->tag,
359 describe_object(&tag->object));
363 out:
364 if (obj->type == OBJ_TREE)
365 free_tree_buffer((struct tree *)obj);
366 if (obj->type == OBJ_COMMIT)
367 free_commit_buffer((struct commit *)obj);
368 return err;
371 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
372 unsigned long size, void *buffer, int *eaten)
375 * Note, buffer may be NULL if type is OBJ_BLOB. See
376 * verify_packfile(), data_valid variable for details.
378 struct object *obj;
379 obj = parse_object_buffer(oid, type, size, buffer, eaten);
380 if (!obj) {
381 errors_found |= ERROR_OBJECT;
382 return error("%s: object corrupt or missing", oid_to_hex(oid));
384 obj->flags &= ~(REACHABLE | SEEN);
385 obj->flags |= HAS_OBJ;
386 return fsck_obj(obj);
389 static int default_refs;
391 static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
392 timestamp_t timestamp)
394 struct object *obj;
396 if (!is_null_oid(oid)) {
397 obj = lookup_object(oid->hash);
398 if (obj && (obj->flags & HAS_OBJ)) {
399 if (timestamp && name_objects)
400 add_decoration(fsck_walk_options.object_names,
401 obj,
402 xstrfmt("%s@{%"PRItime"}", refname, timestamp));
403 obj->flags |= USED;
404 mark_object_reachable(obj);
405 } else {
406 error("%s: invalid reflog entry %s", refname, oid_to_hex(oid));
407 errors_found |= ERROR_REACHABLE;
412 static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
413 const char *email, timestamp_t timestamp, int tz,
414 const char *message, void *cb_data)
416 const char *refname = cb_data;
418 if (verbose)
419 fprintf(stderr, "Checking reflog %s->%s\n",
420 oid_to_hex(ooid), oid_to_hex(noid));
422 fsck_handle_reflog_oid(refname, ooid, 0);
423 fsck_handle_reflog_oid(refname, noid, timestamp);
424 return 0;
427 static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
428 int flag, void *cb_data)
430 for_each_reflog_ent(logname, fsck_handle_reflog_ent, (void *)logname);
431 return 0;
434 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
435 int flag, void *cb_data)
437 struct object *obj;
439 obj = parse_object(oid);
440 if (!obj) {
441 error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
442 errors_found |= ERROR_REACHABLE;
443 /* We'll continue with the rest despite the error.. */
444 return 0;
446 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
447 error("%s: not a commit", refname);
448 errors_found |= ERROR_REFS;
450 default_refs++;
451 obj->flags |= USED;
452 if (name_objects)
453 add_decoration(fsck_walk_options.object_names,
454 obj, xstrdup(refname));
455 mark_object_reachable(obj);
457 return 0;
460 static void get_default_heads(void)
462 if (head_points_at && !is_null_oid(&head_oid))
463 fsck_handle_ref("HEAD", &head_oid, 0, NULL);
464 for_each_rawref(fsck_handle_ref, NULL);
465 if (include_reflogs)
466 for_each_reflog(fsck_handle_reflog, NULL);
469 * Not having any default heads isn't really fatal, but
470 * it does mean that "--unreachable" no longer makes any
471 * sense (since in this case everything will obviously
472 * be unreachable by definition.
474 * Showing dangling objects is valid, though (as those
475 * dangling objects are likely lost heads).
477 * So we just print a warning about it, and clear the
478 * "show_unreachable" flag.
480 if (!default_refs) {
481 fprintf(stderr, "notice: No default references\n");
482 show_unreachable = 0;
486 static struct object *parse_loose_object(const struct object_id *oid,
487 const char *path)
489 struct object *obj;
490 void *contents;
491 enum object_type type;
492 unsigned long size;
493 int eaten;
495 if (read_loose_object(path, oid->hash, &type, &size, &contents) < 0)
496 return NULL;
498 if (!contents && type != OBJ_BLOB)
499 die("BUG: read_loose_object streamed a non-blob");
501 obj = parse_object_buffer(oid, type, size, contents, &eaten);
503 if (!eaten)
504 free(contents);
505 return obj;
508 static int fsck_loose(const struct object_id *oid, const char *path, void *data)
510 struct object *obj = parse_loose_object(oid, path);
512 if (!obj) {
513 errors_found |= ERROR_OBJECT;
514 error("%s: object corrupt or missing: %s",
515 oid_to_hex(oid), path);
516 return 0; /* keep checking other objects */
519 obj->flags &= ~(REACHABLE | SEEN);
520 obj->flags |= HAS_OBJ;
521 if (fsck_obj(obj))
522 errors_found |= ERROR_OBJECT;
523 return 0;
526 static int fsck_cruft(const char *basename, const char *path, void *data)
528 if (!starts_with(basename, "tmp_obj_"))
529 fprintf(stderr, "bad sha1 file: %s\n", path);
530 return 0;
533 static int fsck_subdir(unsigned int nr, const char *path, void *progress)
535 display_progress(progress, nr + 1);
536 return 0;
539 static void fsck_object_dir(const char *path)
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);
549 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
550 progress);
551 display_progress(progress, 256);
552 stop_progress(&progress);
555 static int fsck_head_link(void)
557 int null_is_error = 0;
559 if (verbose)
560 fprintf(stderr, "Checking HEAD link\n");
562 head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, NULL);
563 if (!head_points_at) {
564 errors_found |= ERROR_REFS;
565 return error("Invalid HEAD");
567 if (!strcmp(head_points_at, "HEAD"))
568 /* detached HEAD */
569 null_is_error = 1;
570 else if (!starts_with(head_points_at, "refs/heads/")) {
571 errors_found |= ERROR_REFS;
572 return error("HEAD points to something strange (%s)",
573 head_points_at);
575 if (is_null_oid(&head_oid)) {
576 if (null_is_error) {
577 errors_found |= ERROR_REFS;
578 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->oid);
596 if (!obj) {
597 error("%s: invalid sha1 pointer in cache-tree",
598 oid_to_hex(&it->oid));
599 errors_found |= ERROR_REFS;
600 return 1;
602 obj->flags |= USED;
603 if (name_objects)
604 add_decoration(fsck_walk_options.object_names,
605 obj, xstrdup(":"));
606 mark_object_reachable(obj);
607 if (obj->type != OBJ_TREE)
608 err |= objerror(obj, "non-tree in cache-tree");
610 for (i = 0; i < it->subtree_nr; i++)
611 err |= fsck_cache_tree(it->down[i]->cache_tree);
612 return err;
615 static void mark_object_for_connectivity(const struct object_id *oid)
617 struct object *obj = lookup_unknown_object(oid->hash);
618 obj->flags |= HAS_OBJ;
621 static int mark_loose_for_connectivity(const struct object_id *oid,
622 const char *path,
623 void *data)
625 mark_object_for_connectivity(oid);
626 return 0;
629 static int mark_packed_for_connectivity(const struct object_id *oid,
630 struct packed_git *pack,
631 uint32_t pos,
632 void *data)
634 mark_object_for_connectivity(oid);
635 return 0;
638 static char const * const fsck_usage[] = {
639 N_("git fsck [<options>] [<object>...]"),
640 NULL
643 static struct option fsck_opts[] = {
644 OPT__VERBOSE(&verbose, N_("be verbose")),
645 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
646 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
647 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
648 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
649 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
650 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
651 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
652 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
653 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
654 OPT_BOOL(0, "lost-found", &write_lost_and_found,
655 N_("write dangling objects in .git/lost-found")),
656 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
657 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
658 OPT_END(),
661 int cmd_fsck(int argc, const char **argv, const char *prefix)
663 int i;
664 struct alternate_object_database *alt;
666 errors_found = 0;
667 check_replace_refs = 0;
669 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
671 fsck_walk_options.walk = mark_object;
672 fsck_obj_options.walk = mark_used;
673 fsck_obj_options.error_func = fsck_error_func;
674 if (check_strict)
675 fsck_obj_options.strict = 1;
677 if (show_progress == -1)
678 show_progress = isatty(2);
679 if (verbose)
680 show_progress = 0;
682 if (write_lost_and_found) {
683 check_full = 1;
684 include_reflogs = 0;
687 if (name_objects)
688 fsck_walk_options.object_names =
689 xcalloc(1, sizeof(struct decoration));
691 git_config(fsck_config, NULL);
693 fsck_head_link();
694 if (connectivity_only) {
695 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
696 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
697 } else {
698 fsck_object_dir(get_object_directory());
700 prepare_alt_odb();
701 for (alt = alt_odb_list; alt; alt = alt->next)
702 fsck_object_dir(alt->path);
704 if (check_full) {
705 struct packed_git *p;
706 uint32_t total = 0, count = 0;
707 struct progress *progress = NULL;
709 prepare_packed_git();
711 if (show_progress) {
712 for (p = packed_git; p; p = p->next) {
713 if (open_pack_index(p))
714 continue;
715 total += p->num_objects;
718 progress = start_progress(_("Checking objects"), total);
720 for (p = packed_git; p; p = p->next) {
721 /* verify gives error messages itself */
722 if (verify_pack(p, fsck_obj_buffer,
723 progress, count))
724 errors_found |= ERROR_PACK;
725 count += p->num_objects;
727 stop_progress(&progress);
731 for (i = 0; i < argc; i++) {
732 const char *arg = argv[i];
733 unsigned char sha1[20];
734 if (!get_sha1(arg, sha1)) {
735 struct object *obj = lookup_object(sha1);
737 if (!obj || !(obj->flags & HAS_OBJ)) {
738 error("%s: object missing", sha1_to_hex(sha1));
739 errors_found |= ERROR_OBJECT;
740 continue;
743 obj->flags |= USED;
744 if (name_objects)
745 add_decoration(fsck_walk_options.object_names,
746 obj, xstrdup(arg));
747 mark_object_reachable(obj);
748 continue;
750 error("invalid parameter: expected sha1, got '%s'", arg);
751 errors_found |= ERROR_OBJECT;
755 * If we've not been given any explicit head information, do the
756 * default ones from .git/refs. We also consider the index file
757 * in this case (ie this implies --cache).
759 if (!argc) {
760 get_default_heads();
761 keep_cache_objects = 1;
764 if (keep_cache_objects) {
765 verify_index_checksum = 1;
766 read_cache();
767 for (i = 0; i < active_nr; i++) {
768 unsigned int mode;
769 struct blob *blob;
770 struct object *obj;
772 mode = active_cache[i]->ce_mode;
773 if (S_ISGITLINK(mode))
774 continue;
775 blob = lookup_blob(&active_cache[i]->oid);
776 if (!blob)
777 continue;
778 obj = &blob->object;
779 obj->flags |= USED;
780 if (name_objects)
781 add_decoration(fsck_walk_options.object_names,
782 obj,
783 xstrfmt(":%s", active_cache[i]->name));
784 mark_object_reachable(obj);
786 if (active_cache_tree)
787 fsck_cache_tree(active_cache_tree);
790 check_connectivity();
791 return errors_found;