pager.h: move declarations for pager.c functions from cache.h
[git.git] / builtin / fsck.c
blob35a6de3cdb56e2444d50e73c4a8dbe917a8e68fb
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-file.h"
23 #include "object-name.h"
24 #include "object-store.h"
25 #include "replace-object.h"
26 #include "resolve-undo.h"
27 #include "run-command.h"
28 #include "worktree.h"
30 #define REACHABLE 0x0001
31 #define SEEN 0x0002
32 #define HAS_OBJ 0x0004
33 /* This flag is set if something points to this object. */
34 #define USED 0x0008
36 static int show_root;
37 static int show_tags;
38 static int show_unreachable;
39 static int include_reflogs = 1;
40 static int check_full = 1;
41 static int connectivity_only;
42 static int check_strict;
43 static int keep_cache_objects;
44 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
45 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
46 static int errors_found;
47 static int write_lost_and_found;
48 static int verbose;
49 static int show_progress = -1;
50 static int show_dangling = 1;
51 static int name_objects;
52 #define ERROR_OBJECT 01
53 #define ERROR_REACHABLE 02
54 #define ERROR_PACK 04
55 #define ERROR_REFS 010
56 #define ERROR_COMMIT_GRAPH 020
57 #define ERROR_MULTI_PACK_INDEX 040
59 static const char *describe_object(const struct object_id *oid)
61 return fsck_describe_object(&fsck_walk_options, oid);
64 static const char *printable_type(const struct object_id *oid,
65 enum object_type type)
67 const char *ret;
69 if (type == OBJ_NONE)
70 type = oid_object_info(the_repository, oid, NULL);
72 ret = type_name(type);
73 if (!ret)
74 ret = _("unknown");
76 return ret;
79 static int objerror(struct object *obj, const char *err)
81 errors_found |= ERROR_OBJECT;
82 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
83 fprintf_ln(stderr, _("error in %s %s: %s"),
84 printable_type(&obj->oid, obj->type),
85 describe_object(&obj->oid), err);
86 return -1;
89 static int fsck_error_func(struct fsck_options *o,
90 const struct object_id *oid,
91 enum object_type object_type,
92 enum fsck_msg_type msg_type,
93 enum fsck_msg_id msg_id,
94 const char *message)
96 switch (msg_type) {
97 case FSCK_WARN:
98 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
99 fprintf_ln(stderr, _("warning in %s %s: %s"),
100 printable_type(oid, object_type),
101 describe_object(oid), message);
102 return 0;
103 case FSCK_ERROR:
104 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
105 fprintf_ln(stderr, _("error in %s %s: %s"),
106 printable_type(oid, object_type),
107 describe_object(oid), message);
108 return 1;
109 default:
110 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
111 msg_type);
115 static struct object_array pending;
117 static int mark_object(struct object *obj, enum object_type type,
118 void *data, struct fsck_options *options)
120 struct object *parent = data;
123 * The only case data is NULL or type is OBJ_ANY is when
124 * mark_object_reachable() calls us. All the callers of
125 * that function has non-NULL obj hence ...
127 if (!obj) {
128 /* ... these references to parent->fld are safe here */
129 printf_ln(_("broken link from %7s %s"),
130 printable_type(&parent->oid, parent->type),
131 describe_object(&parent->oid));
132 printf_ln(_("broken link from %7s %s"),
133 (type == OBJ_ANY ? _("unknown") : type_name(type)),
134 _("unknown"));
135 errors_found |= ERROR_REACHABLE;
136 return 1;
139 if (type != OBJ_ANY && obj->type != type)
140 /* ... and the reference to parent is safe here */
141 objerror(parent, _("wrong object type in link"));
143 if (obj->flags & REACHABLE)
144 return 0;
145 obj->flags |= REACHABLE;
147 if (is_promisor_object(&obj->oid))
149 * Further recursion does not need to be performed on this
150 * object since it is a promisor object (so it does not need to
151 * be added to "pending").
153 return 0;
155 if (!(obj->flags & HAS_OBJ)) {
156 if (parent && !has_object(the_repository, &obj->oid, 1)) {
157 printf_ln(_("broken link from %7s %s\n"
158 " to %7s %s"),
159 printable_type(&parent->oid, parent->type),
160 describe_object(&parent->oid),
161 printable_type(&obj->oid, obj->type),
162 describe_object(&obj->oid));
163 errors_found |= ERROR_REACHABLE;
165 return 1;
168 add_object_array(obj, NULL, &pending);
169 return 0;
172 static void mark_object_reachable(struct object *obj)
174 mark_object(obj, OBJ_ANY, NULL, NULL);
177 static int traverse_one_object(struct object *obj)
179 int result = fsck_walk(obj, obj, &fsck_walk_options);
181 if (obj->type == OBJ_TREE) {
182 struct tree *tree = (struct tree *)obj;
183 free_tree_buffer(tree);
185 return result;
188 static int traverse_reachable(void)
190 struct progress *progress = NULL;
191 unsigned int nr = 0;
192 int result = 0;
193 if (show_progress)
194 progress = start_delayed_progress(_("Checking connectivity"), 0);
195 while (pending.nr) {
196 result |= traverse_one_object(object_array_pop(&pending));
197 display_progress(progress, ++nr);
199 stop_progress(&progress);
200 return !!result;
203 static int mark_used(struct object *obj, enum object_type object_type,
204 void *data, struct fsck_options *options)
206 if (!obj)
207 return 1;
208 obj->flags |= USED;
209 return 0;
212 static void mark_unreachable_referents(const struct object_id *oid)
214 struct fsck_options options = FSCK_OPTIONS_DEFAULT;
215 struct object *obj = lookup_object(the_repository, oid);
217 if (!obj || !(obj->flags & HAS_OBJ))
218 return; /* not part of our original set */
219 if (obj->flags & REACHABLE)
220 return; /* reachable objects already traversed */
223 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
224 * (and we want to avoid parsing blobs).
226 if (obj->type == OBJ_NONE) {
227 enum object_type type = oid_object_info(the_repository,
228 &obj->oid, NULL);
229 if (type > 0)
230 object_as_type(obj, type, 0);
233 options.walk = mark_used;
234 fsck_walk(obj, NULL, &options);
235 if (obj->type == OBJ_TREE)
236 free_tree_buffer((struct tree *)obj);
239 static int mark_loose_unreachable_referents(const struct object_id *oid,
240 const char *path UNUSED,
241 void *data UNUSED)
243 mark_unreachable_referents(oid);
244 return 0;
247 static int mark_packed_unreachable_referents(const struct object_id *oid,
248 struct packed_git *pack UNUSED,
249 uint32_t pos UNUSED,
250 void *data UNUSED)
252 mark_unreachable_referents(oid);
253 return 0;
257 * Check a single reachable object
259 static void check_reachable_object(struct object *obj)
262 * We obviously want the object to be parsed,
263 * except if it was in a pack-file and we didn't
264 * do a full fsck
266 if (!(obj->flags & HAS_OBJ)) {
267 if (is_promisor_object(&obj->oid))
268 return;
269 if (has_object_pack(&obj->oid))
270 return; /* it is in pack - forget about it */
271 printf_ln(_("missing %s %s"),
272 printable_type(&obj->oid, obj->type),
273 describe_object(&obj->oid));
274 errors_found |= ERROR_REACHABLE;
275 return;
280 * Check a single unreachable object
282 static void check_unreachable_object(struct object *obj)
285 * Missing unreachable object? Ignore it. It's not like
286 * we miss it (since it can't be reached), nor do we want
287 * to complain about it being unreachable (since it does
288 * not exist).
290 if (!(obj->flags & HAS_OBJ))
291 return;
294 * Unreachable object that exists? Show it if asked to,
295 * since this is something that is prunable.
297 if (show_unreachable) {
298 printf_ln(_("unreachable %s %s"),
299 printable_type(&obj->oid, obj->type),
300 describe_object(&obj->oid));
301 return;
305 * "!USED" means that nothing at all points to it, including
306 * other unreachable objects. In other words, it's the "tip"
307 * of some set of unreachable objects, usually a commit that
308 * got dropped.
310 * Such starting points are more interesting than some random
311 * set of unreachable objects, so we show them even if the user
312 * hasn't asked for _all_ unreachable objects. If you have
313 * deleted a branch by mistake, this is a prime candidate to
314 * start looking at, for example.
316 if (!(obj->flags & USED)) {
317 if (show_dangling)
318 printf_ln(_("dangling %s %s"),
319 printable_type(&obj->oid, obj->type),
320 describe_object(&obj->oid));
321 if (write_lost_and_found) {
322 char *filename = git_pathdup("lost-found/%s/%s",
323 obj->type == OBJ_COMMIT ? "commit" : "other",
324 describe_object(&obj->oid));
325 FILE *f;
327 if (safe_create_leading_directories_const(filename)) {
328 error(_("could not create lost-found"));
329 free(filename);
330 return;
332 f = xfopen(filename, "w");
333 if (obj->type == OBJ_BLOB) {
334 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
335 die_errno(_("could not write '%s'"), filename);
336 } else
337 fprintf(f, "%s\n", describe_object(&obj->oid));
338 if (fclose(f))
339 die_errno(_("could not finish '%s'"),
340 filename);
341 free(filename);
343 return;
347 * Otherwise? It's there, it's unreachable, and some other unreachable
348 * object points to it. Ignore it - it's not interesting, and we showed
349 * all the interesting cases above.
353 static void check_object(struct object *obj)
355 if (verbose)
356 fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid));
358 if (obj->flags & REACHABLE)
359 check_reachable_object(obj);
360 else
361 check_unreachable_object(obj);
364 static void check_connectivity(void)
366 int i, max;
368 /* Traverse the pending reachable objects */
369 traverse_reachable();
372 * With --connectivity-only, we won't have actually opened and marked
373 * unreachable objects with USED. Do that now to make --dangling, etc
374 * accurate.
376 if (connectivity_only && (show_dangling || write_lost_and_found)) {
378 * Even though we already have a "struct object" for each of
379 * these in memory, we must not iterate over the internal
380 * object hash as we do below. Our loop would potentially
381 * resize the hash, making our iteration invalid.
383 * Instead, we'll just go back to the source list of objects,
384 * and ignore any that weren't present in our earlier
385 * traversal.
387 for_each_loose_object(mark_loose_unreachable_referents, NULL, 0);
388 for_each_packed_object(mark_packed_unreachable_referents, NULL, 0);
391 /* Look up all the requirements, warn about missing objects.. */
392 max = get_max_object_index();
393 if (verbose)
394 fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max);
396 for (i = 0; i < max; i++) {
397 struct object *obj = get_indexed_object(i);
399 if (obj)
400 check_object(obj);
404 static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
406 int err;
408 if (obj->flags & SEEN)
409 return 0;
410 obj->flags |= SEEN;
412 if (verbose)
413 fprintf_ln(stderr, _("Checking %s %s"),
414 printable_type(&obj->oid, obj->type),
415 describe_object(&obj->oid));
417 if (fsck_walk(obj, NULL, &fsck_obj_options))
418 objerror(obj, _("broken links"));
419 err = fsck_object(obj, buffer, size, &fsck_obj_options);
420 if (err)
421 goto out;
423 if (obj->type == OBJ_COMMIT) {
424 struct commit *commit = (struct commit *) obj;
426 if (!commit->parents && show_root)
427 printf_ln(_("root %s"),
428 describe_object(&commit->object.oid));
431 if (obj->type == OBJ_TAG) {
432 struct tag *tag = (struct tag *) obj;
434 if (show_tags && tag->tagged) {
435 printf_ln(_("tagged %s %s (%s) in %s"),
436 printable_type(&tag->tagged->oid, tag->tagged->type),
437 describe_object(&tag->tagged->oid),
438 tag->tag,
439 describe_object(&tag->object.oid));
443 out:
444 if (obj->type == OBJ_TREE)
445 free_tree_buffer((struct tree *)obj);
446 return err;
449 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
450 unsigned long size, void *buffer, int *eaten)
453 * Note, buffer may be NULL if type is OBJ_BLOB. See
454 * verify_packfile(), data_valid variable for details.
456 struct object *obj;
457 obj = parse_object_buffer(the_repository, oid, type, size, buffer,
458 eaten);
459 if (!obj) {
460 errors_found |= ERROR_OBJECT;
461 return error(_("%s: object corrupt or missing"),
462 oid_to_hex(oid));
464 obj->flags &= ~(REACHABLE | SEEN);
465 obj->flags |= HAS_OBJ;
466 return fsck_obj(obj, buffer, size);
469 static int default_refs;
471 static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
472 timestamp_t timestamp)
474 struct object *obj;
476 if (!is_null_oid(oid)) {
477 obj = lookup_object(the_repository, oid);
478 if (obj && (obj->flags & HAS_OBJ)) {
479 if (timestamp)
480 fsck_put_object_name(&fsck_walk_options, oid,
481 "%s@{%"PRItime"}",
482 refname, timestamp);
483 obj->flags |= USED;
484 mark_object_reachable(obj);
485 } else if (!is_promisor_object(oid)) {
486 error(_("%s: invalid reflog entry %s"),
487 refname, oid_to_hex(oid));
488 errors_found |= ERROR_REACHABLE;
493 static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
494 const char *email UNUSED,
495 timestamp_t timestamp, int tz UNUSED,
496 const char *message UNUSED, void *cb_data)
498 const char *refname = cb_data;
500 if (verbose)
501 fprintf_ln(stderr, _("Checking reflog %s->%s"),
502 oid_to_hex(ooid), oid_to_hex(noid));
504 fsck_handle_reflog_oid(refname, ooid, 0);
505 fsck_handle_reflog_oid(refname, noid, timestamp);
506 return 0;
509 static int fsck_handle_reflog(const char *logname,
510 const struct object_id *oid UNUSED,
511 int flag UNUSED, void *cb_data)
513 struct strbuf refname = STRBUF_INIT;
515 strbuf_worktree_ref(cb_data, &refname, logname);
516 for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
517 strbuf_release(&refname);
518 return 0;
521 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
522 int flag UNUSED, void *cb_data UNUSED)
524 struct object *obj;
526 obj = parse_object(the_repository, oid);
527 if (!obj) {
528 if (is_promisor_object(oid)) {
530 * Increment default_refs anyway, because this is a
531 * valid ref.
533 default_refs++;
534 return 0;
536 error(_("%s: invalid sha1 pointer %s"),
537 refname, oid_to_hex(oid));
538 errors_found |= ERROR_REACHABLE;
539 /* We'll continue with the rest despite the error.. */
540 return 0;
542 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
543 error(_("%s: not a commit"), refname);
544 errors_found |= ERROR_REFS;
546 default_refs++;
547 obj->flags |= USED;
548 fsck_put_object_name(&fsck_walk_options,
549 oid, "%s", refname);
550 mark_object_reachable(obj);
552 return 0;
555 static int fsck_head_link(const char *head_ref_name,
556 const char **head_points_at,
557 struct object_id *head_oid);
559 static void get_default_heads(void)
561 struct worktree **worktrees, **p;
562 const char *head_points_at;
563 struct object_id head_oid;
565 for_each_rawref(fsck_handle_ref, NULL);
567 worktrees = get_worktrees();
568 for (p = worktrees; *p; p++) {
569 struct worktree *wt = *p;
570 struct strbuf ref = STRBUF_INIT;
572 strbuf_worktree_ref(wt, &ref, "HEAD");
573 fsck_head_link(ref.buf, &head_points_at, &head_oid);
574 if (head_points_at && !is_null_oid(&head_oid))
575 fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
576 strbuf_release(&ref);
578 if (include_reflogs)
579 refs_for_each_reflog(get_worktree_ref_store(wt),
580 fsck_handle_reflog, wt);
582 free_worktrees(worktrees);
585 * Not having any default heads isn't really fatal, but
586 * it does mean that "--unreachable" no longer makes any
587 * sense (since in this case everything will obviously
588 * be unreachable by definition.
590 * Showing dangling objects is valid, though (as those
591 * dangling objects are likely lost heads).
593 * So we just print a warning about it, and clear the
594 * "show_unreachable" flag.
596 if (!default_refs) {
597 fprintf_ln(stderr, _("notice: No default references"));
598 show_unreachable = 0;
602 struct for_each_loose_cb
604 struct progress *progress;
605 struct strbuf obj_type;
608 static int fsck_loose(const struct object_id *oid, const char *path, void *data)
610 struct for_each_loose_cb *cb_data = data;
611 struct object *obj;
612 enum object_type type = OBJ_NONE;
613 unsigned long size;
614 void *contents = NULL;
615 int eaten;
616 struct object_info oi = OBJECT_INFO_INIT;
617 struct object_id real_oid = *null_oid();
618 int err = 0;
620 strbuf_reset(&cb_data->obj_type);
621 oi.type_name = &cb_data->obj_type;
622 oi.sizep = &size;
623 oi.typep = &type;
625 if (read_loose_object(path, oid, &real_oid, &contents, &oi) < 0) {
626 if (contents && !oideq(&real_oid, oid))
627 err = error(_("%s: hash-path mismatch, found at: %s"),
628 oid_to_hex(&real_oid), path);
629 else
630 err = error(_("%s: object corrupt or missing: %s"),
631 oid_to_hex(oid), path);
633 if (type != OBJ_NONE && type < 0)
634 err = error(_("%s: object is of unknown type '%s': %s"),
635 oid_to_hex(&real_oid), cb_data->obj_type.buf,
636 path);
637 if (err < 0) {
638 errors_found |= ERROR_OBJECT;
639 free(contents);
640 return 0; /* keep checking other objects */
643 if (!contents && type != OBJ_BLOB)
644 BUG("read_loose_object streamed a non-blob");
646 obj = parse_object_buffer(the_repository, oid, type, size,
647 contents, &eaten);
649 if (!obj) {
650 errors_found |= ERROR_OBJECT;
651 error(_("%s: object could not be parsed: %s"),
652 oid_to_hex(oid), path);
653 if (!eaten)
654 free(contents);
655 return 0; /* keep checking other objects */
658 obj->flags &= ~(REACHABLE | SEEN);
659 obj->flags |= HAS_OBJ;
660 if (fsck_obj(obj, contents, size))
661 errors_found |= ERROR_OBJECT;
663 if (!eaten)
664 free(contents);
665 return 0; /* keep checking other objects, even if we saw an error */
668 static int fsck_cruft(const char *basename, const char *path,
669 void *data UNUSED)
671 if (!starts_with(basename, "tmp_obj_"))
672 fprintf_ln(stderr, _("bad sha1 file: %s"), path);
673 return 0;
676 static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data)
678 struct for_each_loose_cb *cb_data = data;
679 struct progress *progress = cb_data->progress;
680 display_progress(progress, nr + 1);
681 return 0;
684 static void fsck_object_dir(const char *path)
686 struct progress *progress = NULL;
687 struct for_each_loose_cb cb_data = {
688 .obj_type = STRBUF_INIT,
689 .progress = progress,
692 if (verbose)
693 fprintf_ln(stderr, _("Checking object directory"));
695 if (show_progress)
696 progress = start_progress(_("Checking object directories"), 256);
698 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
699 &cb_data);
700 display_progress(progress, 256);
701 stop_progress(&progress);
702 strbuf_release(&cb_data.obj_type);
705 static int fsck_head_link(const char *head_ref_name,
706 const char **head_points_at,
707 struct object_id *head_oid)
709 int null_is_error = 0;
711 if (verbose)
712 fprintf_ln(stderr, _("Checking %s link"), head_ref_name);
714 *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
715 if (!*head_points_at) {
716 errors_found |= ERROR_REFS;
717 return error(_("invalid %s"), head_ref_name);
719 if (!strcmp(*head_points_at, head_ref_name))
720 /* detached HEAD */
721 null_is_error = 1;
722 else if (!starts_with(*head_points_at, "refs/heads/")) {
723 errors_found |= ERROR_REFS;
724 return error(_("%s points to something strange (%s)"),
725 head_ref_name, *head_points_at);
727 if (is_null_oid(head_oid)) {
728 if (null_is_error) {
729 errors_found |= ERROR_REFS;
730 return error(_("%s: detached HEAD points at nothing"),
731 head_ref_name);
733 fprintf_ln(stderr,
734 _("notice: %s points to an unborn branch (%s)"),
735 head_ref_name, *head_points_at + 11);
737 return 0;
740 static int fsck_cache_tree(struct cache_tree *it, const char *index_path)
742 int i;
743 int err = 0;
745 if (verbose)
746 fprintf_ln(stderr, _("Checking cache tree of %s"), index_path);
748 if (0 <= it->entry_count) {
749 struct object *obj = parse_object(the_repository, &it->oid);
750 if (!obj) {
751 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
752 oid_to_hex(&it->oid), index_path);
753 errors_found |= ERROR_REFS;
754 return 1;
756 obj->flags |= USED;
757 fsck_put_object_name(&fsck_walk_options, &it->oid, ":");
758 mark_object_reachable(obj);
759 if (obj->type != OBJ_TREE)
760 err |= objerror(obj, _("non-tree in cache-tree"));
762 for (i = 0; i < it->subtree_nr; i++)
763 err |= fsck_cache_tree(it->down[i]->cache_tree, index_path);
764 return err;
767 static int fsck_resolve_undo(struct index_state *istate,
768 const char *index_path)
770 struct string_list_item *item;
771 struct string_list *resolve_undo = istate->resolve_undo;
773 if (!resolve_undo)
774 return 0;
776 for_each_string_list_item(item, resolve_undo) {
777 const char *path = item->string;
778 struct resolve_undo_info *ru = item->util;
779 int i;
781 if (!ru)
782 continue;
783 for (i = 0; i < 3; i++) {
784 struct object *obj;
786 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
787 continue;
789 obj = parse_object(the_repository, &ru->oid[i]);
790 if (!obj) {
791 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
792 oid_to_hex(&ru->oid[i]),
793 index_path);
794 errors_found |= ERROR_REFS;
795 continue;
797 obj->flags |= USED;
798 fsck_put_object_name(&fsck_walk_options, &ru->oid[i],
799 ":(%d):%s", i, path);
800 mark_object_reachable(obj);
803 return 0;
806 static void fsck_index(struct index_state *istate, const char *index_path,
807 int is_main_index)
809 unsigned int i;
811 /* TODO: audit for interaction with sparse-index. */
812 ensure_full_index(istate);
813 for (i = 0; i < istate->cache_nr; i++) {
814 unsigned int mode;
815 struct blob *blob;
816 struct object *obj;
818 mode = istate->cache[i]->ce_mode;
819 if (S_ISGITLINK(mode))
820 continue;
821 blob = lookup_blob(the_repository,
822 &istate->cache[i]->oid);
823 if (!blob)
824 continue;
825 obj = &blob->object;
826 obj->flags |= USED;
827 fsck_put_object_name(&fsck_walk_options, &obj->oid,
828 "%s:%s",
829 is_main_index ? "" : index_path,
830 istate->cache[i]->name);
831 mark_object_reachable(obj);
833 if (istate->cache_tree)
834 fsck_cache_tree(istate->cache_tree, index_path);
835 fsck_resolve_undo(istate, index_path);
838 static void mark_object_for_connectivity(const struct object_id *oid)
840 struct object *obj = lookup_unknown_object(the_repository, oid);
841 obj->flags |= HAS_OBJ;
844 static int mark_loose_for_connectivity(const struct object_id *oid,
845 const char *path UNUSED,
846 void *data UNUSED)
848 mark_object_for_connectivity(oid);
849 return 0;
852 static int mark_packed_for_connectivity(const struct object_id *oid,
853 struct packed_git *pack UNUSED,
854 uint32_t pos UNUSED,
855 void *data UNUSED)
857 mark_object_for_connectivity(oid);
858 return 0;
861 static char const * const fsck_usage[] = {
862 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
863 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
864 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
865 " [--[no-]name-objects] [<object>...]"),
866 NULL
869 static struct option fsck_opts[] = {
870 OPT__VERBOSE(&verbose, N_("be verbose")),
871 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
872 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
873 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
874 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
875 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
876 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
877 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
878 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
879 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
880 OPT_BOOL(0, "lost-found", &write_lost_and_found,
881 N_("write dangling objects in .git/lost-found")),
882 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
883 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
884 OPT_END(),
887 int cmd_fsck(int argc, const char **argv, const char *prefix)
889 int i;
890 struct object_directory *odb;
892 /* fsck knows how to handle missing promisor objects */
893 fetch_if_missing = 0;
895 errors_found = 0;
896 read_replace_refs = 0;
897 save_commit_buffer = 0;
899 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
901 fsck_walk_options.walk = mark_object;
902 fsck_obj_options.walk = mark_used;
903 fsck_obj_options.error_func = fsck_error_func;
904 if (check_strict)
905 fsck_obj_options.strict = 1;
907 if (show_progress == -1)
908 show_progress = isatty(2);
909 if (verbose)
910 show_progress = 0;
912 if (write_lost_and_found) {
913 check_full = 1;
914 include_reflogs = 0;
917 if (name_objects)
918 fsck_enable_object_names(&fsck_walk_options);
920 git_config(git_fsck_config, &fsck_obj_options);
921 prepare_repo_settings(the_repository);
923 if (connectivity_only) {
924 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
925 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
926 } else {
927 prepare_alt_odb(the_repository);
928 for (odb = the_repository->objects->odb; odb; odb = odb->next)
929 fsck_object_dir(odb->path);
931 if (check_full) {
932 struct packed_git *p;
933 uint32_t total = 0, count = 0;
934 struct progress *progress = NULL;
936 if (show_progress) {
937 for (p = get_all_packs(the_repository); p;
938 p = p->next) {
939 if (open_pack_index(p))
940 continue;
941 total += p->num_objects;
944 progress = start_progress(_("Checking objects"), total);
946 for (p = get_all_packs(the_repository); p;
947 p = p->next) {
948 /* verify gives error messages itself */
949 if (verify_pack(the_repository,
950 p, fsck_obj_buffer,
951 progress, count))
952 errors_found |= ERROR_PACK;
953 count += p->num_objects;
955 stop_progress(&progress);
958 if (fsck_finish(&fsck_obj_options))
959 errors_found |= ERROR_OBJECT;
962 for (i = 0; i < argc; i++) {
963 const char *arg = argv[i];
964 struct object_id oid;
965 if (!repo_get_oid(the_repository, arg, &oid)) {
966 struct object *obj = lookup_object(the_repository,
967 &oid);
969 if (!obj || !(obj->flags & HAS_OBJ)) {
970 if (is_promisor_object(&oid))
971 continue;
972 error(_("%s: object missing"), oid_to_hex(&oid));
973 errors_found |= ERROR_OBJECT;
974 continue;
977 obj->flags |= USED;
978 fsck_put_object_name(&fsck_walk_options, &oid,
979 "%s", arg);
980 mark_object_reachable(obj);
981 continue;
983 error(_("invalid parameter: expected sha1, got '%s'"), arg);
984 errors_found |= ERROR_OBJECT;
988 * If we've not been given any explicit head information, do the
989 * default ones from .git/refs. We also consider the index file
990 * in this case (ie this implies --cache).
992 if (!argc) {
993 get_default_heads();
994 keep_cache_objects = 1;
997 if (keep_cache_objects) {
998 struct worktree **worktrees, **p;
1000 verify_index_checksum = 1;
1001 verify_ce_order = 1;
1003 worktrees = get_worktrees();
1004 for (p = worktrees; *p; p++) {
1005 struct worktree *wt = *p;
1006 struct index_state istate =
1007 INDEX_STATE_INIT(the_repository);
1008 char *path;
1011 * Make a copy since the buffer is reusable
1012 * and may get overwritten by other calls
1013 * while we're examining the index.
1015 path = xstrdup(worktree_git_path(wt, "index"));
1016 read_index_from(&istate, path, get_worktree_git_dir(wt));
1017 fsck_index(&istate, path, wt->is_current);
1018 discard_index(&istate);
1019 free(path);
1021 free_worktrees(worktrees);
1024 check_connectivity();
1026 if (the_repository->settings.core_commit_graph) {
1027 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
1029 prepare_alt_odb(the_repository);
1030 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
1031 child_process_init(&commit_graph_verify);
1032 commit_graph_verify.git_cmd = 1;
1033 strvec_pushl(&commit_graph_verify.args, "commit-graph",
1034 "verify", "--object-dir", odb->path, NULL);
1035 if (run_command(&commit_graph_verify))
1036 errors_found |= ERROR_COMMIT_GRAPH;
1040 if (the_repository->settings.core_multi_pack_index) {
1041 struct child_process midx_verify = CHILD_PROCESS_INIT;
1043 prepare_alt_odb(the_repository);
1044 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
1045 child_process_init(&midx_verify);
1046 midx_verify.git_cmd = 1;
1047 strvec_pushl(&midx_verify.args, "multi-pack-index",
1048 "verify", "--object-dir", odb->path, NULL);
1049 if (run_command(&midx_verify))
1050 errors_found |= ERROR_MULTI_PACK_INDEX;
1054 return errors_found;