ci: refactor docker runner script
[git.git] / builtin / fsck.c
blob8d13794b1412c82f3a19331548b4b2cb26da349b
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 "run-command.h"
23 #include "worktree.h"
25 #define REACHABLE 0x0001
26 #define SEEN 0x0002
27 #define HAS_OBJ 0x0004
28 /* This flag is set if something points to this object. */
29 #define USED 0x0008
31 static int show_root;
32 static int show_tags;
33 static int show_unreachable;
34 static int include_reflogs = 1;
35 static int check_full = 1;
36 static int connectivity_only;
37 static int check_strict;
38 static int keep_cache_objects;
39 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
40 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
41 static int errors_found;
42 static int write_lost_and_found;
43 static int verbose;
44 static int show_progress = -1;
45 static int show_dangling = 1;
46 static int name_objects;
47 #define ERROR_OBJECT 01
48 #define ERROR_REACHABLE 02
49 #define ERROR_PACK 04
50 #define ERROR_REFS 010
51 #define ERROR_COMMIT_GRAPH 020
53 static const char *describe_object(const struct object_id *oid)
55 return fsck_describe_object(&fsck_walk_options, oid);
58 static const char *printable_type(const struct object_id *oid,
59 enum object_type type)
61 const char *ret;
63 if (type == OBJ_NONE)
64 type = oid_object_info(the_repository, oid, NULL);
66 ret = type_name(type);
67 if (!ret)
68 ret = _("unknown");
70 return ret;
73 static int fsck_config(const char *var, const char *value, void *cb)
75 if (strcmp(var, "fsck.skiplist") == 0) {
76 const char *path;
77 struct strbuf sb = STRBUF_INIT;
79 if (git_config_pathname(&path, var, value))
80 return 1;
81 strbuf_addf(&sb, "skiplist=%s", path);
82 free((char *)path);
83 fsck_set_msg_types(&fsck_obj_options, sb.buf);
84 strbuf_release(&sb);
85 return 0;
88 if (skip_prefix(var, "fsck.", &var)) {
89 fsck_set_msg_type(&fsck_obj_options, var, value);
90 return 0;
93 return git_default_config(var, value, cb);
96 static int objerror(struct object *obj, const char *err)
98 errors_found |= ERROR_OBJECT;
99 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
100 fprintf_ln(stderr, _("error in %s %s: %s"),
101 printable_type(&obj->oid, obj->type),
102 describe_object(&obj->oid), err);
103 return -1;
106 static int fsck_error_func(struct fsck_options *o,
107 const struct object_id *oid,
108 enum object_type object_type,
109 int msg_type, const char *message)
111 switch (msg_type) {
112 case FSCK_WARN:
113 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
114 fprintf_ln(stderr, _("warning in %s %s: %s"),
115 printable_type(oid, object_type),
116 describe_object(oid), message);
117 return 0;
118 case FSCK_ERROR:
119 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
120 fprintf_ln(stderr, _("error in %s %s: %s"),
121 printable_type(oid, object_type),
122 describe_object(oid), message);
123 return 1;
124 default:
125 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
126 msg_type);
130 static struct object_array pending;
132 static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
134 struct object *parent = data;
137 * The only case data is NULL or type is OBJ_ANY is when
138 * mark_object_reachable() calls us. All the callers of
139 * that function has non-NULL obj hence ...
141 if (!obj) {
142 /* ... these references to parent->fld are safe here */
143 printf_ln(_("broken link from %7s %s"),
144 printable_type(&parent->oid, parent->type),
145 describe_object(&parent->oid));
146 printf_ln(_("broken link from %7s %s"),
147 (type == OBJ_ANY ? _("unknown") : type_name(type)),
148 _("unknown"));
149 errors_found |= ERROR_REACHABLE;
150 return 1;
153 if (type != OBJ_ANY && obj->type != type)
154 /* ... and the reference to parent is safe here */
155 objerror(parent, _("wrong object type in link"));
157 if (obj->flags & REACHABLE)
158 return 0;
159 obj->flags |= REACHABLE;
161 if (is_promisor_object(&obj->oid))
163 * Further recursion does not need to be performed on this
164 * object since it is a promisor object (so it does not need to
165 * be added to "pending").
167 return 0;
169 if (!(obj->flags & HAS_OBJ)) {
170 if (parent && !has_object_file(&obj->oid)) {
171 printf_ln(_("broken link from %7s %s\n"
172 " to %7s %s"),
173 printable_type(&parent->oid, parent->type),
174 describe_object(&parent->oid),
175 printable_type(&obj->oid, obj->type),
176 describe_object(&obj->oid));
177 errors_found |= ERROR_REACHABLE;
179 return 1;
182 add_object_array(obj, NULL, &pending);
183 return 0;
186 static void mark_object_reachable(struct object *obj)
188 mark_object(obj, OBJ_ANY, NULL, NULL);
191 static int traverse_one_object(struct object *obj)
193 int result = fsck_walk(obj, obj, &fsck_walk_options);
195 if (obj->type == OBJ_TREE) {
196 struct tree *tree = (struct tree *)obj;
197 free_tree_buffer(tree);
199 return result;
202 static int traverse_reachable(void)
204 struct progress *progress = NULL;
205 unsigned int nr = 0;
206 int result = 0;
207 if (show_progress)
208 progress = start_delayed_progress(_("Checking connectivity"), 0);
209 while (pending.nr) {
210 result |= traverse_one_object(object_array_pop(&pending));
211 display_progress(progress, ++nr);
213 stop_progress(&progress);
214 return !!result;
217 static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
219 if (!obj)
220 return 1;
221 obj->flags |= USED;
222 return 0;
225 static void mark_unreachable_referents(const struct object_id *oid)
227 struct fsck_options options = FSCK_OPTIONS_DEFAULT;
228 struct object *obj = lookup_object(the_repository, oid);
230 if (!obj || !(obj->flags & HAS_OBJ))
231 return; /* not part of our original set */
232 if (obj->flags & REACHABLE)
233 return; /* reachable objects already traversed */
236 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
237 * (and we want to avoid parsing blobs).
239 if (obj->type == OBJ_NONE) {
240 enum object_type type = oid_object_info(the_repository,
241 &obj->oid, NULL);
242 if (type > 0)
243 object_as_type(the_repository, obj, type, 0);
246 options.walk = mark_used;
247 fsck_walk(obj, NULL, &options);
250 static int mark_loose_unreachable_referents(const struct object_id *oid,
251 const char *path,
252 void *data)
254 mark_unreachable_referents(oid);
255 return 0;
258 static int mark_packed_unreachable_referents(const struct object_id *oid,
259 struct packed_git *pack,
260 uint32_t pos,
261 void *data)
263 mark_unreachable_referents(oid);
264 return 0;
268 * Check a single reachable object
270 static void check_reachable_object(struct object *obj)
273 * We obviously want the object to be parsed,
274 * except if it was in a pack-file and we didn't
275 * do a full fsck
277 if (!(obj->flags & HAS_OBJ)) {
278 if (is_promisor_object(&obj->oid))
279 return;
280 if (has_object_pack(&obj->oid))
281 return; /* it is in pack - forget about it */
282 printf_ln(_("missing %s %s"),
283 printable_type(&obj->oid, obj->type),
284 describe_object(&obj->oid));
285 errors_found |= ERROR_REACHABLE;
286 return;
291 * Check a single unreachable object
293 static void check_unreachable_object(struct object *obj)
296 * Missing unreachable object? Ignore it. It's not like
297 * we miss it (since it can't be reached), nor do we want
298 * to complain about it being unreachable (since it does
299 * not exist).
301 if (!(obj->flags & HAS_OBJ))
302 return;
305 * Unreachable object that exists? Show it if asked to,
306 * since this is something that is prunable.
308 if (show_unreachable) {
309 printf_ln(_("unreachable %s %s"),
310 printable_type(&obj->oid, obj->type),
311 describe_object(&obj->oid));
312 return;
316 * "!USED" means that nothing at all points to it, including
317 * other unreachable objects. In other words, it's the "tip"
318 * of some set of unreachable objects, usually a commit that
319 * got dropped.
321 * Such starting points are more interesting than some random
322 * set of unreachable objects, so we show them even if the user
323 * hasn't asked for _all_ unreachable objects. If you have
324 * deleted a branch by mistake, this is a prime candidate to
325 * start looking at, for example.
327 if (!(obj->flags & USED)) {
328 if (show_dangling)
329 printf_ln(_("dangling %s %s"),
330 printable_type(&obj->oid, obj->type),
331 describe_object(&obj->oid));
332 if (write_lost_and_found) {
333 char *filename = git_pathdup("lost-found/%s/%s",
334 obj->type == OBJ_COMMIT ? "commit" : "other",
335 describe_object(&obj->oid));
336 FILE *f;
338 if (safe_create_leading_directories_const(filename)) {
339 error(_("could not create lost-found"));
340 free(filename);
341 return;
343 f = xfopen(filename, "w");
344 if (obj->type == OBJ_BLOB) {
345 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
346 die_errno(_("could not write '%s'"), filename);
347 } else
348 fprintf(f, "%s\n", describe_object(&obj->oid));
349 if (fclose(f))
350 die_errno(_("could not finish '%s'"),
351 filename);
352 free(filename);
354 return;
358 * Otherwise? It's there, it's unreachable, and some other unreachable
359 * object points to it. Ignore it - it's not interesting, and we showed
360 * all the interesting cases above.
364 static void check_object(struct object *obj)
366 if (verbose)
367 fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid));
369 if (obj->flags & REACHABLE)
370 check_reachable_object(obj);
371 else
372 check_unreachable_object(obj);
375 static void check_connectivity(void)
377 int i, max;
379 /* Traverse the pending reachable objects */
380 traverse_reachable();
383 * With --connectivity-only, we won't have actually opened and marked
384 * unreachable objects with USED. Do that now to make --dangling, etc
385 * accurate.
387 if (connectivity_only && (show_dangling || write_lost_and_found)) {
389 * Even though we already have a "struct object" for each of
390 * these in memory, we must not iterate over the internal
391 * object hash as we do below. Our loop would potentially
392 * resize the hash, making our iteration invalid.
394 * Instead, we'll just go back to the source list of objects,
395 * and ignore any that weren't present in our earlier
396 * traversal.
398 for_each_loose_object(mark_loose_unreachable_referents, NULL, 0);
399 for_each_packed_object(mark_packed_unreachable_referents, NULL, 0);
402 /* Look up all the requirements, warn about missing objects.. */
403 max = get_max_object_index();
404 if (verbose)
405 fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max);
407 for (i = 0; i < max; i++) {
408 struct object *obj = get_indexed_object(i);
410 if (obj)
411 check_object(obj);
415 static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
417 int err;
419 if (obj->flags & SEEN)
420 return 0;
421 obj->flags |= SEEN;
423 if (verbose)
424 fprintf_ln(stderr, _("Checking %s %s"),
425 printable_type(&obj->oid, obj->type),
426 describe_object(&obj->oid));
428 if (fsck_walk(obj, NULL, &fsck_obj_options))
429 objerror(obj, _("broken links"));
430 err = fsck_object(obj, buffer, size, &fsck_obj_options);
431 if (err)
432 goto out;
434 if (obj->type == OBJ_COMMIT) {
435 struct commit *commit = (struct commit *) obj;
437 if (!commit->parents && show_root)
438 printf_ln(_("root %s"),
439 describe_object(&commit->object.oid));
442 if (obj->type == OBJ_TAG) {
443 struct tag *tag = (struct tag *) obj;
445 if (show_tags && tag->tagged) {
446 printf_ln(_("tagged %s %s (%s) in %s"),
447 printable_type(&tag->tagged->oid, tag->tagged->type),
448 describe_object(&tag->tagged->oid),
449 tag->tag,
450 describe_object(&tag->object.oid));
454 out:
455 if (obj->type == OBJ_TREE)
456 free_tree_buffer((struct tree *)obj);
457 if (obj->type == OBJ_COMMIT)
458 free_commit_buffer(the_repository->parsed_objects,
459 (struct commit *)obj);
460 return err;
463 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
464 unsigned long size, void *buffer, int *eaten)
467 * Note, buffer may be NULL if type is OBJ_BLOB. See
468 * verify_packfile(), data_valid variable for details.
470 struct object *obj;
471 obj = parse_object_buffer(the_repository, oid, type, size, buffer,
472 eaten);
473 if (!obj) {
474 errors_found |= ERROR_OBJECT;
475 return error(_("%s: object corrupt or missing"),
476 oid_to_hex(oid));
478 obj->flags &= ~(REACHABLE | SEEN);
479 obj->flags |= HAS_OBJ;
480 return fsck_obj(obj, buffer, size);
483 static int default_refs;
485 static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
486 timestamp_t timestamp)
488 struct object *obj;
490 if (!is_null_oid(oid)) {
491 obj = lookup_object(the_repository, oid);
492 if (obj && (obj->flags & HAS_OBJ)) {
493 if (timestamp)
494 fsck_put_object_name(&fsck_walk_options, oid,
495 "%s@{%"PRItime"}",
496 refname, timestamp);
497 obj->flags |= USED;
498 mark_object_reachable(obj);
499 } else if (!is_promisor_object(oid)) {
500 error(_("%s: invalid reflog entry %s"),
501 refname, oid_to_hex(oid));
502 errors_found |= ERROR_REACHABLE;
507 static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
508 const char *email, timestamp_t timestamp, int tz,
509 const char *message, void *cb_data)
511 const char *refname = cb_data;
513 if (verbose)
514 fprintf_ln(stderr, _("Checking reflog %s->%s"),
515 oid_to_hex(ooid), oid_to_hex(noid));
517 fsck_handle_reflog_oid(refname, ooid, 0);
518 fsck_handle_reflog_oid(refname, noid, timestamp);
519 return 0;
522 static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
523 int flag, void *cb_data)
525 struct strbuf refname = STRBUF_INIT;
527 strbuf_worktree_ref(cb_data, &refname, logname);
528 for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
529 strbuf_release(&refname);
530 return 0;
533 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
534 int flag, void *cb_data)
536 struct object *obj;
538 obj = parse_object(the_repository, oid);
539 if (!obj) {
540 if (is_promisor_object(oid)) {
542 * Increment default_refs anyway, because this is a
543 * valid ref.
545 default_refs++;
546 return 0;
548 error(_("%s: invalid sha1 pointer %s"),
549 refname, oid_to_hex(oid));
550 errors_found |= ERROR_REACHABLE;
551 /* We'll continue with the rest despite the error.. */
552 return 0;
554 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
555 error(_("%s: not a commit"), refname);
556 errors_found |= ERROR_REFS;
558 default_refs++;
559 obj->flags |= USED;
560 fsck_put_object_name(&fsck_walk_options,
561 oid, "%s", refname);
562 mark_object_reachable(obj);
564 return 0;
567 static int fsck_head_link(const char *head_ref_name,
568 const char **head_points_at,
569 struct object_id *head_oid);
571 static void get_default_heads(void)
573 struct worktree **worktrees, **p;
574 const char *head_points_at;
575 struct object_id head_oid;
577 for_each_rawref(fsck_handle_ref, NULL);
579 worktrees = get_worktrees(0);
580 for (p = worktrees; *p; p++) {
581 struct worktree *wt = *p;
582 struct strbuf ref = STRBUF_INIT;
584 strbuf_worktree_ref(wt, &ref, "HEAD");
585 fsck_head_link(ref.buf, &head_points_at, &head_oid);
586 if (head_points_at && !is_null_oid(&head_oid))
587 fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
588 strbuf_release(&ref);
590 if (include_reflogs)
591 refs_for_each_reflog(get_worktree_ref_store(wt),
592 fsck_handle_reflog, wt);
594 free_worktrees(worktrees);
597 * Not having any default heads isn't really fatal, but
598 * it does mean that "--unreachable" no longer makes any
599 * sense (since in this case everything will obviously
600 * be unreachable by definition.
602 * Showing dangling objects is valid, though (as those
603 * dangling objects are likely lost heads).
605 * So we just print a warning about it, and clear the
606 * "show_unreachable" flag.
608 if (!default_refs) {
609 fprintf_ln(stderr, _("notice: No default references"));
610 show_unreachable = 0;
614 static int fsck_loose(const struct object_id *oid, const char *path, void *data)
616 struct object *obj;
617 enum object_type type;
618 unsigned long size;
619 void *contents;
620 int eaten;
622 if (read_loose_object(path, oid, &type, &size, &contents) < 0) {
623 errors_found |= ERROR_OBJECT;
624 error(_("%s: object corrupt or missing: %s"),
625 oid_to_hex(oid), path);
626 return 0; /* keep checking other objects */
629 if (!contents && type != OBJ_BLOB)
630 BUG("read_loose_object streamed a non-blob");
632 obj = parse_object_buffer(the_repository, oid, type, size,
633 contents, &eaten);
635 if (!obj) {
636 errors_found |= ERROR_OBJECT;
637 error(_("%s: object could not be parsed: %s"),
638 oid_to_hex(oid), path);
639 if (!eaten)
640 free(contents);
641 return 0; /* keep checking other objects */
644 obj->flags &= ~(REACHABLE | SEEN);
645 obj->flags |= HAS_OBJ;
646 if (fsck_obj(obj, contents, size))
647 errors_found |= ERROR_OBJECT;
649 if (!eaten)
650 free(contents);
651 return 0; /* keep checking other objects, even if we saw an error */
654 static int fsck_cruft(const char *basename, const char *path, void *data)
656 if (!starts_with(basename, "tmp_obj_"))
657 fprintf_ln(stderr, _("bad sha1 file: %s"), path);
658 return 0;
661 static int fsck_subdir(unsigned int nr, const char *path, void *progress)
663 display_progress(progress, nr + 1);
664 return 0;
667 static void fsck_object_dir(const char *path)
669 struct progress *progress = NULL;
671 if (verbose)
672 fprintf_ln(stderr, _("Checking object directory"));
674 if (show_progress)
675 progress = start_progress(_("Checking object directories"), 256);
677 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
678 progress);
679 display_progress(progress, 256);
680 stop_progress(&progress);
683 static int fsck_head_link(const char *head_ref_name,
684 const char **head_points_at,
685 struct object_id *head_oid)
687 int null_is_error = 0;
689 if (verbose)
690 fprintf_ln(stderr, _("Checking %s link"), head_ref_name);
692 *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
693 if (!*head_points_at) {
694 errors_found |= ERROR_REFS;
695 return error(_("invalid %s"), head_ref_name);
697 if (!strcmp(*head_points_at, head_ref_name))
698 /* detached HEAD */
699 null_is_error = 1;
700 else if (!starts_with(*head_points_at, "refs/heads/")) {
701 errors_found |= ERROR_REFS;
702 return error(_("%s points to something strange (%s)"),
703 head_ref_name, *head_points_at);
705 if (is_null_oid(head_oid)) {
706 if (null_is_error) {
707 errors_found |= ERROR_REFS;
708 return error(_("%s: detached HEAD points at nothing"),
709 head_ref_name);
711 fprintf_ln(stderr,
712 _("notice: %s points to an unborn branch (%s)"),
713 head_ref_name, *head_points_at + 11);
715 return 0;
718 static int fsck_cache_tree(struct cache_tree *it)
720 int i;
721 int err = 0;
723 if (verbose)
724 fprintf_ln(stderr, _("Checking cache tree"));
726 if (0 <= it->entry_count) {
727 struct object *obj = parse_object(the_repository, &it->oid);
728 if (!obj) {
729 error(_("%s: invalid sha1 pointer in cache-tree"),
730 oid_to_hex(&it->oid));
731 errors_found |= ERROR_REFS;
732 return 1;
734 obj->flags |= USED;
735 fsck_put_object_name(&fsck_walk_options, &it->oid, ":");
736 mark_object_reachable(obj);
737 if (obj->type != OBJ_TREE)
738 err |= objerror(obj, _("non-tree in cache-tree"));
740 for (i = 0; i < it->subtree_nr; i++)
741 err |= fsck_cache_tree(it->down[i]->cache_tree);
742 return err;
745 static void mark_object_for_connectivity(const struct object_id *oid)
747 struct object *obj = lookup_unknown_object(oid);
748 obj->flags |= HAS_OBJ;
751 static int mark_loose_for_connectivity(const struct object_id *oid,
752 const char *path,
753 void *data)
755 mark_object_for_connectivity(oid);
756 return 0;
759 static int mark_packed_for_connectivity(const struct object_id *oid,
760 struct packed_git *pack,
761 uint32_t pos,
762 void *data)
764 mark_object_for_connectivity(oid);
765 return 0;
768 static char const * const fsck_usage[] = {
769 N_("git fsck [<options>] [<object>...]"),
770 NULL
773 static struct option fsck_opts[] = {
774 OPT__VERBOSE(&verbose, N_("be verbose")),
775 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
776 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
777 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
778 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
779 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
780 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
781 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
782 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
783 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
784 OPT_BOOL(0, "lost-found", &write_lost_and_found,
785 N_("write dangling objects in .git/lost-found")),
786 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
787 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
788 OPT_END(),
791 int cmd_fsck(int argc, const char **argv, const char *prefix)
793 int i;
794 struct object_directory *odb;
796 /* fsck knows how to handle missing promisor objects */
797 fetch_if_missing = 0;
799 errors_found = 0;
800 read_replace_refs = 0;
802 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
804 fsck_walk_options.walk = mark_object;
805 fsck_obj_options.walk = mark_used;
806 fsck_obj_options.error_func = fsck_error_func;
807 if (check_strict)
808 fsck_obj_options.strict = 1;
810 if (show_progress == -1)
811 show_progress = isatty(2);
812 if (verbose)
813 show_progress = 0;
815 if (write_lost_and_found) {
816 check_full = 1;
817 include_reflogs = 0;
820 if (name_objects)
821 fsck_enable_object_names(&fsck_walk_options);
823 git_config(fsck_config, NULL);
825 if (connectivity_only) {
826 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
827 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
828 } else {
829 prepare_alt_odb(the_repository);
830 for (odb = the_repository->objects->odb; odb; odb = odb->next)
831 fsck_object_dir(odb->path);
833 if (check_full) {
834 struct packed_git *p;
835 uint32_t total = 0, count = 0;
836 struct progress *progress = NULL;
838 if (show_progress) {
839 for (p = get_all_packs(the_repository); p;
840 p = p->next) {
841 if (open_pack_index(p))
842 continue;
843 total += p->num_objects;
846 progress = start_progress(_("Checking objects"), total);
848 for (p = get_all_packs(the_repository); p;
849 p = p->next) {
850 /* verify gives error messages itself */
851 if (verify_pack(the_repository,
852 p, fsck_obj_buffer,
853 progress, count))
854 errors_found |= ERROR_PACK;
855 count += p->num_objects;
857 stop_progress(&progress);
860 if (fsck_finish(&fsck_obj_options))
861 errors_found |= ERROR_OBJECT;
864 for (i = 0; i < argc; i++) {
865 const char *arg = argv[i];
866 struct object_id oid;
867 if (!get_oid(arg, &oid)) {
868 struct object *obj = lookup_object(the_repository,
869 &oid);
871 if (!obj || !(obj->flags & HAS_OBJ)) {
872 if (is_promisor_object(&oid))
873 continue;
874 error(_("%s: object missing"), oid_to_hex(&oid));
875 errors_found |= ERROR_OBJECT;
876 continue;
879 obj->flags |= USED;
880 fsck_put_object_name(&fsck_walk_options, &oid,
881 "%s", arg);
882 mark_object_reachable(obj);
883 continue;
885 error(_("invalid parameter: expected sha1, got '%s'"), arg);
886 errors_found |= ERROR_OBJECT;
890 * If we've not been given any explicit head information, do the
891 * default ones from .git/refs. We also consider the index file
892 * in this case (ie this implies --cache).
894 if (!argc) {
895 get_default_heads();
896 keep_cache_objects = 1;
899 if (keep_cache_objects) {
900 verify_index_checksum = 1;
901 verify_ce_order = 1;
902 read_cache();
903 for (i = 0; i < active_nr; i++) {
904 unsigned int mode;
905 struct blob *blob;
906 struct object *obj;
908 mode = active_cache[i]->ce_mode;
909 if (S_ISGITLINK(mode))
910 continue;
911 blob = lookup_blob(the_repository,
912 &active_cache[i]->oid);
913 if (!blob)
914 continue;
915 obj = &blob->object;
916 obj->flags |= USED;
917 fsck_put_object_name(&fsck_walk_options, &obj->oid,
918 ":%s", active_cache[i]->name);
919 mark_object_reachable(obj);
921 if (active_cache_tree)
922 fsck_cache_tree(active_cache_tree);
925 check_connectivity();
927 if (!git_config_get_bool("core.commitgraph", &i) && i) {
928 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
929 const char *verify_argv[] = { "commit-graph", "verify", NULL, NULL, NULL };
931 prepare_alt_odb(the_repository);
932 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
933 child_process_init(&commit_graph_verify);
934 commit_graph_verify.argv = verify_argv;
935 commit_graph_verify.git_cmd = 1;
936 verify_argv[2] = "--object-dir";
937 verify_argv[3] = odb->path;
938 if (run_command(&commit_graph_verify))
939 errors_found |= ERROR_COMMIT_GRAPH;
943 if (!git_config_get_bool("core.multipackindex", &i) && i) {
944 struct child_process midx_verify = CHILD_PROCESS_INIT;
945 const char *midx_argv[] = { "multi-pack-index", "verify", NULL, NULL, NULL };
947 prepare_alt_odb(the_repository);
948 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
949 child_process_init(&midx_verify);
950 midx_verify.argv = midx_argv;
951 midx_verify.git_cmd = 1;
952 midx_argv[2] = "--object-dir";
953 midx_argv[3] = odb->path;
954 if (run_command(&midx_verify))
955 errors_found |= ERROR_COMMIT_GRAPH;
959 return errors_found;