Merge branch 'ab/remove-implicit-use-of-the-repository' into en/header-split-cache-h
[alt-git.git] / builtin / fsck.c
blob095b39d39803492fd8142269d0b26cefb580b37e
1 #include "builtin.h"
2 #include "cache.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "repository.h"
6 #include "config.h"
7 #include "commit.h"
8 #include "tree.h"
9 #include "blob.h"
10 #include "tag.h"
11 #include "refs.h"
12 #include "pack.h"
13 #include "cache-tree.h"
14 #include "tree-walk.h"
15 #include "fsck.h"
16 #include "parse-options.h"
17 #include "dir.h"
18 #include "progress.h"
19 #include "streaming.h"
20 #include "decorate.h"
21 #include "packfile.h"
22 #include "object-store.h"
23 #include "replace-object.h"
24 #include "resolve-undo.h"
25 #include "run-command.h"
26 #include "worktree.h"
28 #define REACHABLE 0x0001
29 #define SEEN 0x0002
30 #define HAS_OBJ 0x0004
31 /* This flag is set if something points to this object. */
32 #define USED 0x0008
34 static int show_root;
35 static int show_tags;
36 static int show_unreachable;
37 static int include_reflogs = 1;
38 static int check_full = 1;
39 static int connectivity_only;
40 static int check_strict;
41 static int keep_cache_objects;
42 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
43 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
44 static int errors_found;
45 static int write_lost_and_found;
46 static int verbose;
47 static int show_progress = -1;
48 static int show_dangling = 1;
49 static int name_objects;
50 #define ERROR_OBJECT 01
51 #define ERROR_REACHABLE 02
52 #define ERROR_PACK 04
53 #define ERROR_REFS 010
54 #define ERROR_COMMIT_GRAPH 020
55 #define ERROR_MULTI_PACK_INDEX 040
57 static const char *describe_object(const struct object_id *oid)
59 return fsck_describe_object(&fsck_walk_options, oid);
62 static const char *printable_type(const struct object_id *oid,
63 enum object_type type)
65 const char *ret;
67 if (type == OBJ_NONE)
68 type = oid_object_info(the_repository, oid, NULL);
70 ret = type_name(type);
71 if (!ret)
72 ret = _("unknown");
74 return ret;
77 static int objerror(struct object *obj, const char *err)
79 errors_found |= ERROR_OBJECT;
80 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
81 fprintf_ln(stderr, _("error in %s %s: %s"),
82 printable_type(&obj->oid, obj->type),
83 describe_object(&obj->oid), err);
84 return -1;
87 static int fsck_error_func(struct fsck_options *o,
88 const struct object_id *oid,
89 enum object_type object_type,
90 enum fsck_msg_type msg_type,
91 enum fsck_msg_id msg_id,
92 const char *message)
94 switch (msg_type) {
95 case FSCK_WARN:
96 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
97 fprintf_ln(stderr, _("warning in %s %s: %s"),
98 printable_type(oid, object_type),
99 describe_object(oid), message);
100 return 0;
101 case FSCK_ERROR:
102 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
103 fprintf_ln(stderr, _("error in %s %s: %s"),
104 printable_type(oid, object_type),
105 describe_object(oid), message);
106 return 1;
107 default:
108 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
109 msg_type);
113 static struct object_array pending;
115 static int mark_object(struct object *obj, enum object_type type,
116 void *data, struct fsck_options *options)
118 struct object *parent = data;
121 * The only case data is NULL or type is OBJ_ANY is when
122 * mark_object_reachable() calls us. All the callers of
123 * that function has non-NULL obj hence ...
125 if (!obj) {
126 /* ... these references to parent->fld are safe here */
127 printf_ln(_("broken link from %7s %s"),
128 printable_type(&parent->oid, parent->type),
129 describe_object(&parent->oid));
130 printf_ln(_("broken link from %7s %s"),
131 (type == OBJ_ANY ? _("unknown") : type_name(type)),
132 _("unknown"));
133 errors_found |= ERROR_REACHABLE;
134 return 1;
137 if (type != OBJ_ANY && obj->type != type)
138 /* ... and the reference to parent is safe here */
139 objerror(parent, _("wrong object type in link"));
141 if (obj->flags & REACHABLE)
142 return 0;
143 obj->flags |= REACHABLE;
145 if (is_promisor_object(&obj->oid))
147 * Further recursion does not need to be performed on this
148 * object since it is a promisor object (so it does not need to
149 * be added to "pending").
151 return 0;
153 if (!(obj->flags & HAS_OBJ)) {
154 if (parent && !has_object(the_repository, &obj->oid, 1)) {
155 printf_ln(_("broken link from %7s %s\n"
156 " to %7s %s"),
157 printable_type(&parent->oid, parent->type),
158 describe_object(&parent->oid),
159 printable_type(&obj->oid, obj->type),
160 describe_object(&obj->oid));
161 errors_found |= ERROR_REACHABLE;
163 return 1;
166 add_object_array(obj, NULL, &pending);
167 return 0;
170 static void mark_object_reachable(struct object *obj)
172 mark_object(obj, OBJ_ANY, NULL, NULL);
175 static int traverse_one_object(struct object *obj)
177 int result = fsck_walk(obj, obj, &fsck_walk_options);
179 if (obj->type == OBJ_TREE) {
180 struct tree *tree = (struct tree *)obj;
181 free_tree_buffer(tree);
183 return result;
186 static int traverse_reachable(void)
188 struct progress *progress = NULL;
189 unsigned int nr = 0;
190 int result = 0;
191 if (show_progress)
192 progress = start_delayed_progress(_("Checking connectivity"), 0);
193 while (pending.nr) {
194 result |= traverse_one_object(object_array_pop(&pending));
195 display_progress(progress, ++nr);
197 stop_progress(&progress);
198 return !!result;
201 static int mark_used(struct object *obj, enum object_type object_type,
202 void *data, struct fsck_options *options)
204 if (!obj)
205 return 1;
206 obj->flags |= USED;
207 return 0;
210 static void mark_unreachable_referents(const struct object_id *oid)
212 struct fsck_options options = FSCK_OPTIONS_DEFAULT;
213 struct object *obj = lookup_object(the_repository, oid);
215 if (!obj || !(obj->flags & HAS_OBJ))
216 return; /* not part of our original set */
217 if (obj->flags & REACHABLE)
218 return; /* reachable objects already traversed */
221 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
222 * (and we want to avoid parsing blobs).
224 if (obj->type == OBJ_NONE) {
225 enum object_type type = oid_object_info(the_repository,
226 &obj->oid, NULL);
227 if (type > 0)
228 object_as_type(obj, type, 0);
231 options.walk = mark_used;
232 fsck_walk(obj, NULL, &options);
233 if (obj->type == OBJ_TREE)
234 free_tree_buffer((struct tree *)obj);
237 static int mark_loose_unreachable_referents(const struct object_id *oid,
238 const char *path UNUSED,
239 void *data UNUSED)
241 mark_unreachable_referents(oid);
242 return 0;
245 static int mark_packed_unreachable_referents(const struct object_id *oid,
246 struct packed_git *pack UNUSED,
247 uint32_t pos UNUSED,
248 void *data UNUSED)
250 mark_unreachable_referents(oid);
251 return 0;
255 * Check a single reachable object
257 static void check_reachable_object(struct object *obj)
260 * We obviously want the object to be parsed,
261 * except if it was in a pack-file and we didn't
262 * do a full fsck
264 if (!(obj->flags & HAS_OBJ)) {
265 if (is_promisor_object(&obj->oid))
266 return;
267 if (has_object_pack(&obj->oid))
268 return; /* it is in pack - forget about it */
269 printf_ln(_("missing %s %s"),
270 printable_type(&obj->oid, obj->type),
271 describe_object(&obj->oid));
272 errors_found |= ERROR_REACHABLE;
273 return;
278 * Check a single unreachable object
280 static void check_unreachable_object(struct object *obj)
283 * Missing unreachable object? Ignore it. It's not like
284 * we miss it (since it can't be reached), nor do we want
285 * to complain about it being unreachable (since it does
286 * not exist).
288 if (!(obj->flags & HAS_OBJ))
289 return;
292 * Unreachable object that exists? Show it if asked to,
293 * since this is something that is prunable.
295 if (show_unreachable) {
296 printf_ln(_("unreachable %s %s"),
297 printable_type(&obj->oid, obj->type),
298 describe_object(&obj->oid));
299 return;
303 * "!USED" means that nothing at all points to it, including
304 * other unreachable objects. In other words, it's the "tip"
305 * of some set of unreachable objects, usually a commit that
306 * got dropped.
308 * Such starting points are more interesting than some random
309 * set of unreachable objects, so we show them even if the user
310 * hasn't asked for _all_ unreachable objects. If you have
311 * deleted a branch by mistake, this is a prime candidate to
312 * start looking at, for example.
314 if (!(obj->flags & USED)) {
315 if (show_dangling)
316 printf_ln(_("dangling %s %s"),
317 printable_type(&obj->oid, obj->type),
318 describe_object(&obj->oid));
319 if (write_lost_and_found) {
320 char *filename = git_pathdup("lost-found/%s/%s",
321 obj->type == OBJ_COMMIT ? "commit" : "other",
322 describe_object(&obj->oid));
323 FILE *f;
325 if (safe_create_leading_directories_const(filename)) {
326 error(_("could not create lost-found"));
327 free(filename);
328 return;
330 f = xfopen(filename, "w");
331 if (obj->type == OBJ_BLOB) {
332 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
333 die_errno(_("could not write '%s'"), filename);
334 } else
335 fprintf(f, "%s\n", describe_object(&obj->oid));
336 if (fclose(f))
337 die_errno(_("could not finish '%s'"),
338 filename);
339 free(filename);
341 return;
345 * Otherwise? It's there, it's unreachable, and some other unreachable
346 * object points to it. Ignore it - it's not interesting, and we showed
347 * all the interesting cases above.
351 static void check_object(struct object *obj)
353 if (verbose)
354 fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid));
356 if (obj->flags & REACHABLE)
357 check_reachable_object(obj);
358 else
359 check_unreachable_object(obj);
362 static void check_connectivity(void)
364 int i, max;
366 /* Traverse the pending reachable objects */
367 traverse_reachable();
370 * With --connectivity-only, we won't have actually opened and marked
371 * unreachable objects with USED. Do that now to make --dangling, etc
372 * accurate.
374 if (connectivity_only && (show_dangling || write_lost_and_found)) {
376 * Even though we already have a "struct object" for each of
377 * these in memory, we must not iterate over the internal
378 * object hash as we do below. Our loop would potentially
379 * resize the hash, making our iteration invalid.
381 * Instead, we'll just go back to the source list of objects,
382 * and ignore any that weren't present in our earlier
383 * traversal.
385 for_each_loose_object(mark_loose_unreachable_referents, NULL, 0);
386 for_each_packed_object(mark_packed_unreachable_referents, NULL, 0);
389 /* Look up all the requirements, warn about missing objects.. */
390 max = get_max_object_index();
391 if (verbose)
392 fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max);
394 for (i = 0; i < max; i++) {
395 struct object *obj = get_indexed_object(i);
397 if (obj)
398 check_object(obj);
402 static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
404 int err;
406 if (obj->flags & SEEN)
407 return 0;
408 obj->flags |= SEEN;
410 if (verbose)
411 fprintf_ln(stderr, _("Checking %s %s"),
412 printable_type(&obj->oid, obj->type),
413 describe_object(&obj->oid));
415 if (fsck_walk(obj, NULL, &fsck_obj_options))
416 objerror(obj, _("broken links"));
417 err = fsck_object(obj, buffer, size, &fsck_obj_options);
418 if (err)
419 goto out;
421 if (obj->type == OBJ_COMMIT) {
422 struct commit *commit = (struct commit *) obj;
424 if (!commit->parents && show_root)
425 printf_ln(_("root %s"),
426 describe_object(&commit->object.oid));
429 if (obj->type == OBJ_TAG) {
430 struct tag *tag = (struct tag *) obj;
432 if (show_tags && tag->tagged) {
433 printf_ln(_("tagged %s %s (%s) in %s"),
434 printable_type(&tag->tagged->oid, tag->tagged->type),
435 describe_object(&tag->tagged->oid),
436 tag->tag,
437 describe_object(&tag->object.oid));
441 out:
442 if (obj->type == OBJ_TREE)
443 free_tree_buffer((struct tree *)obj);
444 return err;
447 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
448 unsigned long size, void *buffer, int *eaten)
451 * Note, buffer may be NULL if type is OBJ_BLOB. See
452 * verify_packfile(), data_valid variable for details.
454 struct object *obj;
455 obj = parse_object_buffer(the_repository, oid, type, size, buffer,
456 eaten);
457 if (!obj) {
458 errors_found |= ERROR_OBJECT;
459 return error(_("%s: object corrupt or missing"),
460 oid_to_hex(oid));
462 obj->flags &= ~(REACHABLE | SEEN);
463 obj->flags |= HAS_OBJ;
464 return fsck_obj(obj, buffer, size);
467 static int default_refs;
469 static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
470 timestamp_t timestamp)
472 struct object *obj;
474 if (!is_null_oid(oid)) {
475 obj = lookup_object(the_repository, oid);
476 if (obj && (obj->flags & HAS_OBJ)) {
477 if (timestamp)
478 fsck_put_object_name(&fsck_walk_options, oid,
479 "%s@{%"PRItime"}",
480 refname, timestamp);
481 obj->flags |= USED;
482 mark_object_reachable(obj);
483 } else if (!is_promisor_object(oid)) {
484 error(_("%s: invalid reflog entry %s"),
485 refname, oid_to_hex(oid));
486 errors_found |= ERROR_REACHABLE;
491 static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
492 const char *email UNUSED,
493 timestamp_t timestamp, int tz UNUSED,
494 const char *message UNUSED, void *cb_data)
496 const char *refname = cb_data;
498 if (verbose)
499 fprintf_ln(stderr, _("Checking reflog %s->%s"),
500 oid_to_hex(ooid), oid_to_hex(noid));
502 fsck_handle_reflog_oid(refname, ooid, 0);
503 fsck_handle_reflog_oid(refname, noid, timestamp);
504 return 0;
507 static int fsck_handle_reflog(const char *logname,
508 const struct object_id *oid UNUSED,
509 int flag UNUSED, void *cb_data)
511 struct strbuf refname = STRBUF_INIT;
513 strbuf_worktree_ref(cb_data, &refname, logname);
514 for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
515 strbuf_release(&refname);
516 return 0;
519 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
520 int flag UNUSED, void *cb_data UNUSED)
522 struct object *obj;
524 obj = parse_object(the_repository, oid);
525 if (!obj) {
526 if (is_promisor_object(oid)) {
528 * Increment default_refs anyway, because this is a
529 * valid ref.
531 default_refs++;
532 return 0;
534 error(_("%s: invalid sha1 pointer %s"),
535 refname, oid_to_hex(oid));
536 errors_found |= ERROR_REACHABLE;
537 /* We'll continue with the rest despite the error.. */
538 return 0;
540 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
541 error(_("%s: not a commit"), refname);
542 errors_found |= ERROR_REFS;
544 default_refs++;
545 obj->flags |= USED;
546 fsck_put_object_name(&fsck_walk_options,
547 oid, "%s", refname);
548 mark_object_reachable(obj);
550 return 0;
553 static int fsck_head_link(const char *head_ref_name,
554 const char **head_points_at,
555 struct object_id *head_oid);
557 static void get_default_heads(void)
559 struct worktree **worktrees, **p;
560 const char *head_points_at;
561 struct object_id head_oid;
563 for_each_rawref(fsck_handle_ref, NULL);
565 worktrees = get_worktrees();
566 for (p = worktrees; *p; p++) {
567 struct worktree *wt = *p;
568 struct strbuf ref = STRBUF_INIT;
570 strbuf_worktree_ref(wt, &ref, "HEAD");
571 fsck_head_link(ref.buf, &head_points_at, &head_oid);
572 if (head_points_at && !is_null_oid(&head_oid))
573 fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
574 strbuf_release(&ref);
576 if (include_reflogs)
577 refs_for_each_reflog(get_worktree_ref_store(wt),
578 fsck_handle_reflog, wt);
580 free_worktrees(worktrees);
583 * Not having any default heads isn't really fatal, but
584 * it does mean that "--unreachable" no longer makes any
585 * sense (since in this case everything will obviously
586 * be unreachable by definition.
588 * Showing dangling objects is valid, though (as those
589 * dangling objects are likely lost heads).
591 * So we just print a warning about it, and clear the
592 * "show_unreachable" flag.
594 if (!default_refs) {
595 fprintf_ln(stderr, _("notice: No default references"));
596 show_unreachable = 0;
600 struct for_each_loose_cb
602 struct progress *progress;
603 struct strbuf obj_type;
606 static int fsck_loose(const struct object_id *oid, const char *path, void *data)
608 struct for_each_loose_cb *cb_data = data;
609 struct object *obj;
610 enum object_type type = OBJ_NONE;
611 unsigned long size;
612 void *contents = NULL;
613 int eaten;
614 struct object_info oi = OBJECT_INFO_INIT;
615 struct object_id real_oid = *null_oid();
616 int err = 0;
618 strbuf_reset(&cb_data->obj_type);
619 oi.type_name = &cb_data->obj_type;
620 oi.sizep = &size;
621 oi.typep = &type;
623 if (read_loose_object(path, oid, &real_oid, &contents, &oi) < 0) {
624 if (contents && !oideq(&real_oid, oid))
625 err = error(_("%s: hash-path mismatch, found at: %s"),
626 oid_to_hex(&real_oid), path);
627 else
628 err = error(_("%s: object corrupt or missing: %s"),
629 oid_to_hex(oid), path);
631 if (type != OBJ_NONE && type < 0)
632 err = error(_("%s: object is of unknown type '%s': %s"),
633 oid_to_hex(&real_oid), cb_data->obj_type.buf,
634 path);
635 if (err < 0) {
636 errors_found |= ERROR_OBJECT;
637 free(contents);
638 return 0; /* keep checking other objects */
641 if (!contents && type != OBJ_BLOB)
642 BUG("read_loose_object streamed a non-blob");
644 obj = parse_object_buffer(the_repository, oid, type, size,
645 contents, &eaten);
647 if (!obj) {
648 errors_found |= ERROR_OBJECT;
649 error(_("%s: object could not be parsed: %s"),
650 oid_to_hex(oid), path);
651 if (!eaten)
652 free(contents);
653 return 0; /* keep checking other objects */
656 obj->flags &= ~(REACHABLE | SEEN);
657 obj->flags |= HAS_OBJ;
658 if (fsck_obj(obj, contents, size))
659 errors_found |= ERROR_OBJECT;
661 if (!eaten)
662 free(contents);
663 return 0; /* keep checking other objects, even if we saw an error */
666 static int fsck_cruft(const char *basename, const char *path,
667 void *data UNUSED)
669 if (!starts_with(basename, "tmp_obj_"))
670 fprintf_ln(stderr, _("bad sha1 file: %s"), path);
671 return 0;
674 static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data)
676 struct for_each_loose_cb *cb_data = data;
677 struct progress *progress = cb_data->progress;
678 display_progress(progress, nr + 1);
679 return 0;
682 static void fsck_object_dir(const char *path)
684 struct progress *progress = NULL;
685 struct for_each_loose_cb cb_data = {
686 .obj_type = STRBUF_INIT,
687 .progress = progress,
690 if (verbose)
691 fprintf_ln(stderr, _("Checking object directory"));
693 if (show_progress)
694 progress = start_progress(_("Checking object directories"), 256);
696 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
697 &cb_data);
698 display_progress(progress, 256);
699 stop_progress(&progress);
700 strbuf_release(&cb_data.obj_type);
703 static int fsck_head_link(const char *head_ref_name,
704 const char **head_points_at,
705 struct object_id *head_oid)
707 int null_is_error = 0;
709 if (verbose)
710 fprintf_ln(stderr, _("Checking %s link"), head_ref_name);
712 *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
713 if (!*head_points_at) {
714 errors_found |= ERROR_REFS;
715 return error(_("invalid %s"), head_ref_name);
717 if (!strcmp(*head_points_at, head_ref_name))
718 /* detached HEAD */
719 null_is_error = 1;
720 else if (!starts_with(*head_points_at, "refs/heads/")) {
721 errors_found |= ERROR_REFS;
722 return error(_("%s points to something strange (%s)"),
723 head_ref_name, *head_points_at);
725 if (is_null_oid(head_oid)) {
726 if (null_is_error) {
727 errors_found |= ERROR_REFS;
728 return error(_("%s: detached HEAD points at nothing"),
729 head_ref_name);
731 fprintf_ln(stderr,
732 _("notice: %s points to an unborn branch (%s)"),
733 head_ref_name, *head_points_at + 11);
735 return 0;
738 static int fsck_cache_tree(struct cache_tree *it, const char *index_path)
740 int i;
741 int err = 0;
743 if (verbose)
744 fprintf_ln(stderr, _("Checking cache tree of %s"), index_path);
746 if (0 <= it->entry_count) {
747 struct object *obj = parse_object(the_repository, &it->oid);
748 if (!obj) {
749 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
750 oid_to_hex(&it->oid), index_path);
751 errors_found |= ERROR_REFS;
752 return 1;
754 obj->flags |= USED;
755 fsck_put_object_name(&fsck_walk_options, &it->oid, ":");
756 mark_object_reachable(obj);
757 if (obj->type != OBJ_TREE)
758 err |= objerror(obj, _("non-tree in cache-tree"));
760 for (i = 0; i < it->subtree_nr; i++)
761 err |= fsck_cache_tree(it->down[i]->cache_tree, index_path);
762 return err;
765 static int fsck_resolve_undo(struct index_state *istate,
766 const char *index_path)
768 struct string_list_item *item;
769 struct string_list *resolve_undo = istate->resolve_undo;
771 if (!resolve_undo)
772 return 0;
774 for_each_string_list_item(item, resolve_undo) {
775 const char *path = item->string;
776 struct resolve_undo_info *ru = item->util;
777 int i;
779 if (!ru)
780 continue;
781 for (i = 0; i < 3; i++) {
782 struct object *obj;
784 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
785 continue;
787 obj = parse_object(the_repository, &ru->oid[i]);
788 if (!obj) {
789 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
790 oid_to_hex(&ru->oid[i]),
791 index_path);
792 errors_found |= ERROR_REFS;
793 continue;
795 obj->flags |= USED;
796 fsck_put_object_name(&fsck_walk_options, &ru->oid[i],
797 ":(%d):%s", i, path);
798 mark_object_reachable(obj);
801 return 0;
804 static void fsck_index(struct index_state *istate, const char *index_path,
805 int is_main_index)
807 unsigned int i;
809 /* TODO: audit for interaction with sparse-index. */
810 ensure_full_index(istate);
811 for (i = 0; i < istate->cache_nr; i++) {
812 unsigned int mode;
813 struct blob *blob;
814 struct object *obj;
816 mode = istate->cache[i]->ce_mode;
817 if (S_ISGITLINK(mode))
818 continue;
819 blob = lookup_blob(the_repository,
820 &istate->cache[i]->oid);
821 if (!blob)
822 continue;
823 obj = &blob->object;
824 obj->flags |= USED;
825 fsck_put_object_name(&fsck_walk_options, &obj->oid,
826 "%s:%s",
827 is_main_index ? "" : index_path,
828 istate->cache[i]->name);
829 mark_object_reachable(obj);
831 if (istate->cache_tree)
832 fsck_cache_tree(istate->cache_tree, index_path);
833 fsck_resolve_undo(istate, index_path);
836 static void mark_object_for_connectivity(const struct object_id *oid)
838 struct object *obj = lookup_unknown_object(the_repository, oid);
839 obj->flags |= HAS_OBJ;
842 static int mark_loose_for_connectivity(const struct object_id *oid,
843 const char *path UNUSED,
844 void *data UNUSED)
846 mark_object_for_connectivity(oid);
847 return 0;
850 static int mark_packed_for_connectivity(const struct object_id *oid,
851 struct packed_git *pack UNUSED,
852 uint32_t pos UNUSED,
853 void *data UNUSED)
855 mark_object_for_connectivity(oid);
856 return 0;
859 static char const * const fsck_usage[] = {
860 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
861 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
862 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
863 " [--[no-]name-objects] [<object>...]"),
864 NULL
867 static struct option fsck_opts[] = {
868 OPT__VERBOSE(&verbose, N_("be verbose")),
869 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
870 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
871 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
872 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
873 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
874 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
875 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
876 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
877 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
878 OPT_BOOL(0, "lost-found", &write_lost_and_found,
879 N_("write dangling objects in .git/lost-found")),
880 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
881 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
882 OPT_END(),
885 int cmd_fsck(int argc, const char **argv, const char *prefix)
887 int i;
888 struct object_directory *odb;
890 /* fsck knows how to handle missing promisor objects */
891 fetch_if_missing = 0;
893 errors_found = 0;
894 read_replace_refs = 0;
895 save_commit_buffer = 0;
897 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
899 fsck_walk_options.walk = mark_object;
900 fsck_obj_options.walk = mark_used;
901 fsck_obj_options.error_func = fsck_error_func;
902 if (check_strict)
903 fsck_obj_options.strict = 1;
905 if (show_progress == -1)
906 show_progress = isatty(2);
907 if (verbose)
908 show_progress = 0;
910 if (write_lost_and_found) {
911 check_full = 1;
912 include_reflogs = 0;
915 if (name_objects)
916 fsck_enable_object_names(&fsck_walk_options);
918 git_config(git_fsck_config, &fsck_obj_options);
919 prepare_repo_settings(the_repository);
921 if (connectivity_only) {
922 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
923 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
924 } else {
925 prepare_alt_odb(the_repository);
926 for (odb = the_repository->objects->odb; odb; odb = odb->next)
927 fsck_object_dir(odb->path);
929 if (check_full) {
930 struct packed_git *p;
931 uint32_t total = 0, count = 0;
932 struct progress *progress = NULL;
934 if (show_progress) {
935 for (p = get_all_packs(the_repository); p;
936 p = p->next) {
937 if (open_pack_index(p))
938 continue;
939 total += p->num_objects;
942 progress = start_progress(_("Checking objects"), total);
944 for (p = get_all_packs(the_repository); p;
945 p = p->next) {
946 /* verify gives error messages itself */
947 if (verify_pack(the_repository,
948 p, fsck_obj_buffer,
949 progress, count))
950 errors_found |= ERROR_PACK;
951 count += p->num_objects;
953 stop_progress(&progress);
956 if (fsck_finish(&fsck_obj_options))
957 errors_found |= ERROR_OBJECT;
960 for (i = 0; i < argc; i++) {
961 const char *arg = argv[i];
962 struct object_id oid;
963 if (!repo_get_oid(the_repository, arg, &oid)) {
964 struct object *obj = lookup_object(the_repository,
965 &oid);
967 if (!obj || !(obj->flags & HAS_OBJ)) {
968 if (is_promisor_object(&oid))
969 continue;
970 error(_("%s: object missing"), oid_to_hex(&oid));
971 errors_found |= ERROR_OBJECT;
972 continue;
975 obj->flags |= USED;
976 fsck_put_object_name(&fsck_walk_options, &oid,
977 "%s", arg);
978 mark_object_reachable(obj);
979 continue;
981 error(_("invalid parameter: expected sha1, got '%s'"), arg);
982 errors_found |= ERROR_OBJECT;
986 * If we've not been given any explicit head information, do the
987 * default ones from .git/refs. We also consider the index file
988 * in this case (ie this implies --cache).
990 if (!argc) {
991 get_default_heads();
992 keep_cache_objects = 1;
995 if (keep_cache_objects) {
996 struct worktree **worktrees, **p;
998 verify_index_checksum = 1;
999 verify_ce_order = 1;
1001 worktrees = get_worktrees();
1002 for (p = worktrees; *p; p++) {
1003 struct worktree *wt = *p;
1004 struct index_state istate =
1005 INDEX_STATE_INIT(the_repository);
1006 char *path;
1009 * Make a copy since the buffer is reusable
1010 * and may get overwritten by other calls
1011 * while we're examining the index.
1013 path = xstrdup(worktree_git_path(wt, "index"));
1014 read_index_from(&istate, path, get_worktree_git_dir(wt));
1015 fsck_index(&istate, path, wt->is_current);
1016 discard_index(&istate);
1017 free(path);
1019 free_worktrees(worktrees);
1022 check_connectivity();
1024 if (the_repository->settings.core_commit_graph) {
1025 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
1027 prepare_alt_odb(the_repository);
1028 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
1029 child_process_init(&commit_graph_verify);
1030 commit_graph_verify.git_cmd = 1;
1031 strvec_pushl(&commit_graph_verify.args, "commit-graph",
1032 "verify", "--object-dir", odb->path, NULL);
1033 if (run_command(&commit_graph_verify))
1034 errors_found |= ERROR_COMMIT_GRAPH;
1038 if (the_repository->settings.core_multi_pack_index) {
1039 struct child_process midx_verify = CHILD_PROCESS_INIT;
1041 prepare_alt_odb(the_repository);
1042 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
1043 child_process_init(&midx_verify);
1044 midx_verify.git_cmd = 1;
1045 strvec_pushl(&midx_verify.args, "multi-pack-index",
1046 "verify", "--object-dir", odb->path, NULL);
1047 if (run_command(&midx_verify))
1048 errors_found |= ERROR_MULTI_PACK_INDEX;
1052 return errors_found;