l10n: bg.po: Updated Bulgarian translation (5482t)
[git.git] / builtin / fsck.c
blobf7916f06ed51119576035c9381c4dace2dbbecf2
1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
2 #include "builtin.h"
3 #include "cache.h"
4 #include "repository.h"
5 #include "config.h"
6 #include "commit.h"
7 #include "tree.h"
8 #include "blob.h"
9 #include "tag.h"
10 #include "refs.h"
11 #include "pack.h"
12 #include "cache-tree.h"
13 #include "tree-walk.h"
14 #include "fsck.h"
15 #include "parse-options.h"
16 #include "dir.h"
17 #include "progress.h"
18 #include "streaming.h"
19 #include "decorate.h"
20 #include "packfile.h"
21 #include "object-store.h"
22 #include "resolve-undo.h"
23 #include "run-command.h"
24 #include "worktree.h"
26 #define REACHABLE 0x0001
27 #define SEEN 0x0002
28 #define HAS_OBJ 0x0004
29 /* This flag is set if something points to this object. */
30 #define USED 0x0008
32 static int show_root;
33 static int show_tags;
34 static int show_unreachable;
35 static int include_reflogs = 1;
36 static int check_full = 1;
37 static int connectivity_only;
38 static int check_strict;
39 static int keep_cache_objects;
40 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
41 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
42 static int errors_found;
43 static int write_lost_and_found;
44 static int verbose;
45 static int show_progress = -1;
46 static int show_dangling = 1;
47 static int name_objects;
48 #define ERROR_OBJECT 01
49 #define ERROR_REACHABLE 02
50 #define ERROR_PACK 04
51 #define ERROR_REFS 010
52 #define ERROR_COMMIT_GRAPH 020
53 #define ERROR_MULTI_PACK_INDEX 040
55 static const char *describe_object(const struct object_id *oid)
57 return fsck_describe_object(&fsck_walk_options, oid);
60 static const char *printable_type(const struct object_id *oid,
61 enum object_type type)
63 const char *ret;
65 if (type == OBJ_NONE)
66 type = oid_object_info(the_repository, oid, NULL);
68 ret = type_name(type);
69 if (!ret)
70 ret = _("unknown");
72 return ret;
75 static int objerror(struct object *obj, const char *err)
77 errors_found |= ERROR_OBJECT;
78 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
79 fprintf_ln(stderr, _("error in %s %s: %s"),
80 printable_type(&obj->oid, obj->type),
81 describe_object(&obj->oid), err);
82 return -1;
85 static int fsck_error_func(struct fsck_options *o,
86 const struct object_id *oid,
87 enum object_type object_type,
88 enum fsck_msg_type msg_type,
89 enum fsck_msg_id msg_id,
90 const char *message)
92 switch (msg_type) {
93 case FSCK_WARN:
94 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
95 fprintf_ln(stderr, _("warning in %s %s: %s"),
96 printable_type(oid, object_type),
97 describe_object(oid), message);
98 return 0;
99 case FSCK_ERROR:
100 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
101 fprintf_ln(stderr, _("error in %s %s: %s"),
102 printable_type(oid, object_type),
103 describe_object(oid), message);
104 return 1;
105 default:
106 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
107 msg_type);
111 static struct object_array pending;
113 static int mark_object(struct object *obj, enum object_type type,
114 void *data, struct fsck_options *options)
116 struct object *parent = data;
119 * The only case data is NULL or type is OBJ_ANY is when
120 * mark_object_reachable() calls us. All the callers of
121 * that function has non-NULL obj hence ...
123 if (!obj) {
124 /* ... these references to parent->fld are safe here */
125 printf_ln(_("broken link from %7s %s"),
126 printable_type(&parent->oid, parent->type),
127 describe_object(&parent->oid));
128 printf_ln(_("broken link from %7s %s"),
129 (type == OBJ_ANY ? _("unknown") : type_name(type)),
130 _("unknown"));
131 errors_found |= ERROR_REACHABLE;
132 return 1;
135 if (type != OBJ_ANY && obj->type != type)
136 /* ... and the reference to parent is safe here */
137 objerror(parent, _("wrong object type in link"));
139 if (obj->flags & REACHABLE)
140 return 0;
141 obj->flags |= REACHABLE;
143 if (is_promisor_object(&obj->oid))
145 * Further recursion does not need to be performed on this
146 * object since it is a promisor object (so it does not need to
147 * be added to "pending").
149 return 0;
151 if (!(obj->flags & HAS_OBJ)) {
152 if (parent && !has_object(the_repository, &obj->oid, 1)) {
153 printf_ln(_("broken link from %7s %s\n"
154 " to %7s %s"),
155 printable_type(&parent->oid, parent->type),
156 describe_object(&parent->oid),
157 printable_type(&obj->oid, obj->type),
158 describe_object(&obj->oid));
159 errors_found |= ERROR_REACHABLE;
161 return 1;
164 add_object_array(obj, NULL, &pending);
165 return 0;
168 static void mark_object_reachable(struct object *obj)
170 mark_object(obj, OBJ_ANY, NULL, NULL);
173 static int traverse_one_object(struct object *obj)
175 int result = fsck_walk(obj, obj, &fsck_walk_options);
177 if (obj->type == OBJ_TREE) {
178 struct tree *tree = (struct tree *)obj;
179 free_tree_buffer(tree);
181 return result;
184 static int traverse_reachable(void)
186 struct progress *progress = NULL;
187 unsigned int nr = 0;
188 int result = 0;
189 if (show_progress)
190 progress = start_delayed_progress(_("Checking connectivity"), 0);
191 while (pending.nr) {
192 result |= traverse_one_object(object_array_pop(&pending));
193 display_progress(progress, ++nr);
195 stop_progress(&progress);
196 return !!result;
199 static int mark_used(struct object *obj, enum object_type object_type,
200 void *data, struct fsck_options *options)
202 if (!obj)
203 return 1;
204 obj->flags |= USED;
205 return 0;
208 static void mark_unreachable_referents(const struct object_id *oid)
210 struct fsck_options options = FSCK_OPTIONS_DEFAULT;
211 struct object *obj = lookup_object(the_repository, oid);
213 if (!obj || !(obj->flags & HAS_OBJ))
214 return; /* not part of our original set */
215 if (obj->flags & REACHABLE)
216 return; /* reachable objects already traversed */
219 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
220 * (and we want to avoid parsing blobs).
222 if (obj->type == OBJ_NONE) {
223 enum object_type type = oid_object_info(the_repository,
224 &obj->oid, NULL);
225 if (type > 0)
226 object_as_type(obj, type, 0);
229 options.walk = mark_used;
230 fsck_walk(obj, NULL, &options);
233 static int mark_loose_unreachable_referents(const struct object_id *oid,
234 const char *path,
235 void *data)
237 mark_unreachable_referents(oid);
238 return 0;
241 static int mark_packed_unreachable_referents(const struct object_id *oid,
242 struct packed_git *pack,
243 uint32_t pos,
244 void *data)
246 mark_unreachable_referents(oid);
247 return 0;
251 * Check a single reachable object
253 static void check_reachable_object(struct object *obj)
256 * We obviously want the object to be parsed,
257 * except if it was in a pack-file and we didn't
258 * do a full fsck
260 if (!(obj->flags & HAS_OBJ)) {
261 if (is_promisor_object(&obj->oid))
262 return;
263 if (has_object_pack(&obj->oid))
264 return; /* it is in pack - forget about it */
265 printf_ln(_("missing %s %s"),
266 printable_type(&obj->oid, obj->type),
267 describe_object(&obj->oid));
268 errors_found |= ERROR_REACHABLE;
269 return;
274 * Check a single unreachable object
276 static void check_unreachable_object(struct object *obj)
279 * Missing unreachable object? Ignore it. It's not like
280 * we miss it (since it can't be reached), nor do we want
281 * to complain about it being unreachable (since it does
282 * not exist).
284 if (!(obj->flags & HAS_OBJ))
285 return;
288 * Unreachable object that exists? Show it if asked to,
289 * since this is something that is prunable.
291 if (show_unreachable) {
292 printf_ln(_("unreachable %s %s"),
293 printable_type(&obj->oid, obj->type),
294 describe_object(&obj->oid));
295 return;
299 * "!USED" means that nothing at all points to it, including
300 * other unreachable objects. In other words, it's the "tip"
301 * of some set of unreachable objects, usually a commit that
302 * got dropped.
304 * Such starting points are more interesting than some random
305 * set of unreachable objects, so we show them even if the user
306 * hasn't asked for _all_ unreachable objects. If you have
307 * deleted a branch by mistake, this is a prime candidate to
308 * start looking at, for example.
310 if (!(obj->flags & USED)) {
311 if (show_dangling)
312 printf_ln(_("dangling %s %s"),
313 printable_type(&obj->oid, obj->type),
314 describe_object(&obj->oid));
315 if (write_lost_and_found) {
316 char *filename = git_pathdup("lost-found/%s/%s",
317 obj->type == OBJ_COMMIT ? "commit" : "other",
318 describe_object(&obj->oid));
319 FILE *f;
321 if (safe_create_leading_directories_const(filename)) {
322 error(_("could not create lost-found"));
323 free(filename);
324 return;
326 f = xfopen(filename, "w");
327 if (obj->type == OBJ_BLOB) {
328 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
329 die_errno(_("could not write '%s'"), filename);
330 } else
331 fprintf(f, "%s\n", describe_object(&obj->oid));
332 if (fclose(f))
333 die_errno(_("could not finish '%s'"),
334 filename);
335 free(filename);
337 return;
341 * Otherwise? It's there, it's unreachable, and some other unreachable
342 * object points to it. Ignore it - it's not interesting, and we showed
343 * all the interesting cases above.
347 static void check_object(struct object *obj)
349 if (verbose)
350 fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid));
352 if (obj->flags & REACHABLE)
353 check_reachable_object(obj);
354 else
355 check_unreachable_object(obj);
358 static void check_connectivity(void)
360 int i, max;
362 /* Traverse the pending reachable objects */
363 traverse_reachable();
366 * With --connectivity-only, we won't have actually opened and marked
367 * unreachable objects with USED. Do that now to make --dangling, etc
368 * accurate.
370 if (connectivity_only && (show_dangling || write_lost_and_found)) {
372 * Even though we already have a "struct object" for each of
373 * these in memory, we must not iterate over the internal
374 * object hash as we do below. Our loop would potentially
375 * resize the hash, making our iteration invalid.
377 * Instead, we'll just go back to the source list of objects,
378 * and ignore any that weren't present in our earlier
379 * traversal.
381 for_each_loose_object(mark_loose_unreachable_referents, NULL, 0);
382 for_each_packed_object(mark_packed_unreachable_referents, NULL, 0);
385 /* Look up all the requirements, warn about missing objects.. */
386 max = get_max_object_index();
387 if (verbose)
388 fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max);
390 for (i = 0; i < max; i++) {
391 struct object *obj = get_indexed_object(i);
393 if (obj)
394 check_object(obj);
398 static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
400 int err;
402 if (obj->flags & SEEN)
403 return 0;
404 obj->flags |= SEEN;
406 if (verbose)
407 fprintf_ln(stderr, _("Checking %s %s"),
408 printable_type(&obj->oid, obj->type),
409 describe_object(&obj->oid));
411 if (fsck_walk(obj, NULL, &fsck_obj_options))
412 objerror(obj, _("broken links"));
413 err = fsck_object(obj, buffer, size, &fsck_obj_options);
414 if (err)
415 goto out;
417 if (obj->type == OBJ_COMMIT) {
418 struct commit *commit = (struct commit *) obj;
420 if (!commit->parents && show_root)
421 printf_ln(_("root %s"),
422 describe_object(&commit->object.oid));
425 if (obj->type == OBJ_TAG) {
426 struct tag *tag = (struct tag *) obj;
428 if (show_tags && tag->tagged) {
429 printf_ln(_("tagged %s %s (%s) in %s"),
430 printable_type(&tag->tagged->oid, tag->tagged->type),
431 describe_object(&tag->tagged->oid),
432 tag->tag,
433 describe_object(&tag->object.oid));
437 out:
438 if (obj->type == OBJ_TREE)
439 free_tree_buffer((struct tree *)obj);
440 if (obj->type == OBJ_COMMIT)
441 free_commit_buffer(the_repository->parsed_objects,
442 (struct commit *)obj);
443 return err;
446 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
447 unsigned long size, void *buffer, int *eaten)
450 * Note, buffer may be NULL if type is OBJ_BLOB. See
451 * verify_packfile(), data_valid variable for details.
453 struct object *obj;
454 obj = parse_object_buffer(the_repository, oid, type, size, buffer,
455 eaten);
456 if (!obj) {
457 errors_found |= ERROR_OBJECT;
458 return error(_("%s: object corrupt or missing"),
459 oid_to_hex(oid));
461 obj->flags &= ~(REACHABLE | SEEN);
462 obj->flags |= HAS_OBJ;
463 return fsck_obj(obj, buffer, size);
466 static int default_refs;
468 static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
469 timestamp_t timestamp)
471 struct object *obj;
473 if (!is_null_oid(oid)) {
474 obj = lookup_object(the_repository, oid);
475 if (obj && (obj->flags & HAS_OBJ)) {
476 if (timestamp)
477 fsck_put_object_name(&fsck_walk_options, oid,
478 "%s@{%"PRItime"}",
479 refname, timestamp);
480 obj->flags |= USED;
481 mark_object_reachable(obj);
482 } else if (!is_promisor_object(oid)) {
483 error(_("%s: invalid reflog entry %s"),
484 refname, oid_to_hex(oid));
485 errors_found |= ERROR_REACHABLE;
490 static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
491 const char *email UNUSED,
492 timestamp_t timestamp, int tz UNUSED,
493 const char *message UNUSED, void *cb_data)
495 const char *refname = cb_data;
497 if (verbose)
498 fprintf_ln(stderr, _("Checking reflog %s->%s"),
499 oid_to_hex(ooid), oid_to_hex(noid));
501 fsck_handle_reflog_oid(refname, ooid, 0);
502 fsck_handle_reflog_oid(refname, noid, timestamp);
503 return 0;
506 static int fsck_handle_reflog(const char *logname,
507 const struct object_id *oid UNUSED,
508 int flag UNUSED, void *cb_data)
510 struct strbuf refname = STRBUF_INIT;
512 strbuf_worktree_ref(cb_data, &refname, logname);
513 for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
514 strbuf_release(&refname);
515 return 0;
518 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
519 int flag UNUSED, void *cb_data UNUSED)
521 struct object *obj;
523 obj = parse_object(the_repository, oid);
524 if (!obj) {
525 if (is_promisor_object(oid)) {
527 * Increment default_refs anyway, because this is a
528 * valid ref.
530 default_refs++;
531 return 0;
533 error(_("%s: invalid sha1 pointer %s"),
534 refname, oid_to_hex(oid));
535 errors_found |= ERROR_REACHABLE;
536 /* We'll continue with the rest despite the error.. */
537 return 0;
539 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
540 error(_("%s: not a commit"), refname);
541 errors_found |= ERROR_REFS;
543 default_refs++;
544 obj->flags |= USED;
545 fsck_put_object_name(&fsck_walk_options,
546 oid, "%s", refname);
547 mark_object_reachable(obj);
549 return 0;
552 static int fsck_head_link(const char *head_ref_name,
553 const char **head_points_at,
554 struct object_id *head_oid);
556 static void get_default_heads(void)
558 struct worktree **worktrees, **p;
559 const char *head_points_at;
560 struct object_id head_oid;
562 for_each_rawref(fsck_handle_ref, NULL);
564 worktrees = get_worktrees();
565 for (p = worktrees; *p; p++) {
566 struct worktree *wt = *p;
567 struct strbuf ref = STRBUF_INIT;
569 strbuf_worktree_ref(wt, &ref, "HEAD");
570 fsck_head_link(ref.buf, &head_points_at, &head_oid);
571 if (head_points_at && !is_null_oid(&head_oid))
572 fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
573 strbuf_release(&ref);
575 if (include_reflogs)
576 refs_for_each_reflog(get_worktree_ref_store(wt),
577 fsck_handle_reflog, wt);
579 free_worktrees(worktrees);
582 * Not having any default heads isn't really fatal, but
583 * it does mean that "--unreachable" no longer makes any
584 * sense (since in this case everything will obviously
585 * be unreachable by definition.
587 * Showing dangling objects is valid, though (as those
588 * dangling objects are likely lost heads).
590 * So we just print a warning about it, and clear the
591 * "show_unreachable" flag.
593 if (!default_refs) {
594 fprintf_ln(stderr, _("notice: No default references"));
595 show_unreachable = 0;
599 struct for_each_loose_cb
601 struct progress *progress;
602 struct strbuf obj_type;
605 static int fsck_loose(const struct object_id *oid, const char *path, void *data)
607 struct for_each_loose_cb *cb_data = data;
608 struct object *obj;
609 enum object_type type = OBJ_NONE;
610 unsigned long size;
611 void *contents = NULL;
612 int eaten;
613 struct object_info oi = OBJECT_INFO_INIT;
614 struct object_id real_oid = *null_oid();
615 int err = 0;
617 strbuf_reset(&cb_data->obj_type);
618 oi.type_name = &cb_data->obj_type;
619 oi.sizep = &size;
620 oi.typep = &type;
622 if (read_loose_object(path, oid, &real_oid, &contents, &oi) < 0) {
623 if (contents && !oideq(&real_oid, oid))
624 err = error(_("%s: hash-path mismatch, found at: %s"),
625 oid_to_hex(&real_oid), path);
626 else
627 err = error(_("%s: object corrupt or missing: %s"),
628 oid_to_hex(oid), path);
630 if (type != OBJ_NONE && type < 0)
631 err = error(_("%s: object is of unknown type '%s': %s"),
632 oid_to_hex(&real_oid), cb_data->obj_type.buf,
633 path);
634 if (err < 0) {
635 errors_found |= ERROR_OBJECT;
636 free(contents);
637 return 0; /* keep checking other objects */
640 if (!contents && type != OBJ_BLOB)
641 BUG("read_loose_object streamed a non-blob");
643 obj = parse_object_buffer(the_repository, oid, type, size,
644 contents, &eaten);
646 if (!obj) {
647 errors_found |= ERROR_OBJECT;
648 error(_("%s: object could not be parsed: %s"),
649 oid_to_hex(oid), path);
650 if (!eaten)
651 free(contents);
652 return 0; /* keep checking other objects */
655 obj->flags &= ~(REACHABLE | SEEN);
656 obj->flags |= HAS_OBJ;
657 if (fsck_obj(obj, contents, size))
658 errors_found |= ERROR_OBJECT;
660 if (!eaten)
661 free(contents);
662 return 0; /* keep checking other objects, even if we saw an error */
665 static int fsck_cruft(const char *basename, const char *path, void *data)
667 if (!starts_with(basename, "tmp_obj_"))
668 fprintf_ln(stderr, _("bad sha1 file: %s"), path);
669 return 0;
672 static int fsck_subdir(unsigned int nr, const char *path, void *data)
674 struct for_each_loose_cb *cb_data = data;
675 struct progress *progress = cb_data->progress;
676 display_progress(progress, nr + 1);
677 return 0;
680 static void fsck_object_dir(const char *path)
682 struct progress *progress = NULL;
683 struct for_each_loose_cb cb_data = {
684 .obj_type = STRBUF_INIT,
685 .progress = progress,
688 if (verbose)
689 fprintf_ln(stderr, _("Checking object directory"));
691 if (show_progress)
692 progress = start_progress(_("Checking object directories"), 256);
694 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
695 &cb_data);
696 display_progress(progress, 256);
697 stop_progress(&progress);
698 strbuf_release(&cb_data.obj_type);
701 static int fsck_head_link(const char *head_ref_name,
702 const char **head_points_at,
703 struct object_id *head_oid)
705 int null_is_error = 0;
707 if (verbose)
708 fprintf_ln(stderr, _("Checking %s link"), head_ref_name);
710 *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
711 if (!*head_points_at) {
712 errors_found |= ERROR_REFS;
713 return error(_("invalid %s"), head_ref_name);
715 if (!strcmp(*head_points_at, head_ref_name))
716 /* detached HEAD */
717 null_is_error = 1;
718 else if (!starts_with(*head_points_at, "refs/heads/")) {
719 errors_found |= ERROR_REFS;
720 return error(_("%s points to something strange (%s)"),
721 head_ref_name, *head_points_at);
723 if (is_null_oid(head_oid)) {
724 if (null_is_error) {
725 errors_found |= ERROR_REFS;
726 return error(_("%s: detached HEAD points at nothing"),
727 head_ref_name);
729 fprintf_ln(stderr,
730 _("notice: %s points to an unborn branch (%s)"),
731 head_ref_name, *head_points_at + 11);
733 return 0;
736 static int fsck_cache_tree(struct cache_tree *it)
738 int i;
739 int err = 0;
741 if (verbose)
742 fprintf_ln(stderr, _("Checking cache tree"));
744 if (0 <= it->entry_count) {
745 struct object *obj = parse_object(the_repository, &it->oid);
746 if (!obj) {
747 error(_("%s: invalid sha1 pointer in cache-tree"),
748 oid_to_hex(&it->oid));
749 errors_found |= ERROR_REFS;
750 return 1;
752 obj->flags |= USED;
753 fsck_put_object_name(&fsck_walk_options, &it->oid, ":");
754 mark_object_reachable(obj);
755 if (obj->type != OBJ_TREE)
756 err |= objerror(obj, _("non-tree in cache-tree"));
758 for (i = 0; i < it->subtree_nr; i++)
759 err |= fsck_cache_tree(it->down[i]->cache_tree);
760 return err;
763 static int fsck_resolve_undo(struct index_state *istate)
765 struct string_list_item *item;
766 struct string_list *resolve_undo = istate->resolve_undo;
768 if (!resolve_undo)
769 return 0;
771 for_each_string_list_item(item, resolve_undo) {
772 const char *path = item->string;
773 struct resolve_undo_info *ru = item->util;
774 int i;
776 if (!ru)
777 continue;
778 for (i = 0; i < 3; i++) {
779 struct object *obj;
781 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
782 continue;
784 obj = parse_object(the_repository, &ru->oid[i]);
785 if (!obj) {
786 error(_("%s: invalid sha1 pointer in resolve-undo"),
787 oid_to_hex(&ru->oid[i]));
788 errors_found |= ERROR_REFS;
789 continue;
791 obj->flags |= USED;
792 fsck_put_object_name(&fsck_walk_options, &ru->oid[i],
793 ":(%d):%s", i, path);
794 mark_object_reachable(obj);
797 return 0;
800 static void mark_object_for_connectivity(const struct object_id *oid)
802 struct object *obj = lookup_unknown_object(the_repository, oid);
803 obj->flags |= HAS_OBJ;
806 static int mark_loose_for_connectivity(const struct object_id *oid,
807 const char *path,
808 void *data)
810 mark_object_for_connectivity(oid);
811 return 0;
814 static int mark_packed_for_connectivity(const struct object_id *oid,
815 struct packed_git *pack,
816 uint32_t pos,
817 void *data)
819 mark_object_for_connectivity(oid);
820 return 0;
823 static char const * const fsck_usage[] = {
824 N_("git fsck [<options>] [<object>...]"),
825 NULL
828 static struct option fsck_opts[] = {
829 OPT__VERBOSE(&verbose, N_("be verbose")),
830 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
831 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
832 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
833 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
834 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
835 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
836 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
837 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
838 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
839 OPT_BOOL(0, "lost-found", &write_lost_and_found,
840 N_("write dangling objects in .git/lost-found")),
841 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
842 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
843 OPT_END(),
846 int cmd_fsck(int argc, const char **argv, const char *prefix)
848 int i;
849 struct object_directory *odb;
851 /* fsck knows how to handle missing promisor objects */
852 fetch_if_missing = 0;
854 errors_found = 0;
855 read_replace_refs = 0;
857 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
859 fsck_walk_options.walk = mark_object;
860 fsck_obj_options.walk = mark_used;
861 fsck_obj_options.error_func = fsck_error_func;
862 if (check_strict)
863 fsck_obj_options.strict = 1;
865 if (show_progress == -1)
866 show_progress = isatty(2);
867 if (verbose)
868 show_progress = 0;
870 if (write_lost_and_found) {
871 check_full = 1;
872 include_reflogs = 0;
875 if (name_objects)
876 fsck_enable_object_names(&fsck_walk_options);
878 git_config(git_fsck_config, &fsck_obj_options);
879 prepare_repo_settings(the_repository);
881 if (connectivity_only) {
882 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
883 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
884 } else {
885 prepare_alt_odb(the_repository);
886 for (odb = the_repository->objects->odb; odb; odb = odb->next)
887 fsck_object_dir(odb->path);
889 if (check_full) {
890 struct packed_git *p;
891 uint32_t total = 0, count = 0;
892 struct progress *progress = NULL;
894 if (show_progress) {
895 for (p = get_all_packs(the_repository); p;
896 p = p->next) {
897 if (open_pack_index(p))
898 continue;
899 total += p->num_objects;
902 progress = start_progress(_("Checking objects"), total);
904 for (p = get_all_packs(the_repository); p;
905 p = p->next) {
906 /* verify gives error messages itself */
907 if (verify_pack(the_repository,
908 p, fsck_obj_buffer,
909 progress, count))
910 errors_found |= ERROR_PACK;
911 count += p->num_objects;
913 stop_progress(&progress);
916 if (fsck_finish(&fsck_obj_options))
917 errors_found |= ERROR_OBJECT;
920 for (i = 0; i < argc; i++) {
921 const char *arg = argv[i];
922 struct object_id oid;
923 if (!get_oid(arg, &oid)) {
924 struct object *obj = lookup_object(the_repository,
925 &oid);
927 if (!obj || !(obj->flags & HAS_OBJ)) {
928 if (is_promisor_object(&oid))
929 continue;
930 error(_("%s: object missing"), oid_to_hex(&oid));
931 errors_found |= ERROR_OBJECT;
932 continue;
935 obj->flags |= USED;
936 fsck_put_object_name(&fsck_walk_options, &oid,
937 "%s", arg);
938 mark_object_reachable(obj);
939 continue;
941 error(_("invalid parameter: expected sha1, got '%s'"), arg);
942 errors_found |= ERROR_OBJECT;
946 * If we've not been given any explicit head information, do the
947 * default ones from .git/refs. We also consider the index file
948 * in this case (ie this implies --cache).
950 if (!argc) {
951 get_default_heads();
952 keep_cache_objects = 1;
955 if (keep_cache_objects) {
956 verify_index_checksum = 1;
957 verify_ce_order = 1;
958 read_cache();
959 /* TODO: audit for interaction with sparse-index. */
960 ensure_full_index(&the_index);
961 for (i = 0; i < active_nr; i++) {
962 unsigned int mode;
963 struct blob *blob;
964 struct object *obj;
966 mode = active_cache[i]->ce_mode;
967 if (S_ISGITLINK(mode))
968 continue;
969 blob = lookup_blob(the_repository,
970 &active_cache[i]->oid);
971 if (!blob)
972 continue;
973 obj = &blob->object;
974 obj->flags |= USED;
975 fsck_put_object_name(&fsck_walk_options, &obj->oid,
976 ":%s", active_cache[i]->name);
977 mark_object_reachable(obj);
979 if (active_cache_tree)
980 fsck_cache_tree(active_cache_tree);
981 fsck_resolve_undo(&the_index);
984 check_connectivity();
986 if (the_repository->settings.core_commit_graph) {
987 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
989 prepare_alt_odb(the_repository);
990 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
991 child_process_init(&commit_graph_verify);
992 commit_graph_verify.git_cmd = 1;
993 strvec_pushl(&commit_graph_verify.args, "commit-graph",
994 "verify", "--object-dir", odb->path, NULL);
995 if (run_command(&commit_graph_verify))
996 errors_found |= ERROR_COMMIT_GRAPH;
1000 if (the_repository->settings.core_multi_pack_index) {
1001 struct child_process midx_verify = CHILD_PROCESS_INIT;
1003 prepare_alt_odb(the_repository);
1004 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
1005 child_process_init(&midx_verify);
1006 midx_verify.git_cmd = 1;
1007 strvec_pushl(&midx_verify.args, "multi-pack-index",
1008 "verify", "--object-dir", odb->path, NULL);
1009 if (run_command(&midx_verify))
1010 errors_found |= ERROR_MULTI_PACK_INDEX;
1014 return errors_found;