svn tests: refactor away a "set -e" in test body
[git/debian.git] / builtin / fsck.c
blob70ff95837aee4f40271a4de596c63dd4fa02b18f
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
52 #define ERROR_MULTI_PACK_INDEX 040
54 static const char *describe_object(const struct object_id *oid)
56 return fsck_describe_object(&fsck_walk_options, oid);
59 static const char *printable_type(const struct object_id *oid,
60 enum object_type type)
62 const char *ret;
64 if (type == OBJ_NONE)
65 type = oid_object_info(the_repository, oid, NULL);
67 ret = type_name(type);
68 if (!ret)
69 ret = _("unknown");
71 return ret;
74 static int objerror(struct object *obj, const char *err)
76 errors_found |= ERROR_OBJECT;
77 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
78 fprintf_ln(stderr, _("error in %s %s: %s"),
79 printable_type(&obj->oid, obj->type),
80 describe_object(&obj->oid), err);
81 return -1;
84 static int fsck_error_func(struct fsck_options *o,
85 const struct object_id *oid,
86 enum object_type object_type,
87 enum fsck_msg_type msg_type,
88 enum fsck_msg_id msg_id,
89 const char *message)
91 switch (msg_type) {
92 case FSCK_WARN:
93 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
94 fprintf_ln(stderr, _("warning in %s %s: %s"),
95 printable_type(oid, object_type),
96 describe_object(oid), message);
97 return 0;
98 case FSCK_ERROR:
99 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
100 fprintf_ln(stderr, _("error in %s %s: %s"),
101 printable_type(oid, object_type),
102 describe_object(oid), message);
103 return 1;
104 default:
105 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
106 msg_type);
110 static struct object_array pending;
112 static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
114 struct object *parent = data;
117 * The only case data is NULL or type is OBJ_ANY is when
118 * mark_object_reachable() calls us. All the callers of
119 * that function has non-NULL obj hence ...
121 if (!obj) {
122 /* ... these references to parent->fld are safe here */
123 printf_ln(_("broken link from %7s %s"),
124 printable_type(&parent->oid, parent->type),
125 describe_object(&parent->oid));
126 printf_ln(_("broken link from %7s %s"),
127 (type == OBJ_ANY ? _("unknown") : type_name(type)),
128 _("unknown"));
129 errors_found |= ERROR_REACHABLE;
130 return 1;
133 if (type != OBJ_ANY && obj->type != type)
134 /* ... and the reference to parent is safe here */
135 objerror(parent, _("wrong object type in link"));
137 if (obj->flags & REACHABLE)
138 return 0;
139 obj->flags |= REACHABLE;
141 if (is_promisor_object(&obj->oid))
143 * Further recursion does not need to be performed on this
144 * object since it is a promisor object (so it does not need to
145 * be added to "pending").
147 return 0;
149 if (!(obj->flags & HAS_OBJ)) {
150 if (parent && !has_object(the_repository, &obj->oid, 1)) {
151 printf_ln(_("broken link from %7s %s\n"
152 " to %7s %s"),
153 printable_type(&parent->oid, parent->type),
154 describe_object(&parent->oid),
155 printable_type(&obj->oid, obj->type),
156 describe_object(&obj->oid));
157 errors_found |= ERROR_REACHABLE;
159 return 1;
162 add_object_array(obj, NULL, &pending);
163 return 0;
166 static void mark_object_reachable(struct object *obj)
168 mark_object(obj, OBJ_ANY, NULL, NULL);
171 static int traverse_one_object(struct object *obj)
173 int result = fsck_walk(obj, obj, &fsck_walk_options);
175 if (obj->type == OBJ_TREE) {
176 struct tree *tree = (struct tree *)obj;
177 free_tree_buffer(tree);
179 return result;
182 static int traverse_reachable(void)
184 struct progress *progress = NULL;
185 unsigned int nr = 0;
186 int result = 0;
187 if (show_progress)
188 progress = start_delayed_progress(_("Checking connectivity"), 0);
189 while (pending.nr) {
190 result |= traverse_one_object(object_array_pop(&pending));
191 display_progress(progress, ++nr);
193 stop_progress(&progress);
194 return !!result;
197 static int mark_used(struct object *obj, enum object_type object_type,
198 void *data, struct fsck_options *options)
200 if (!obj)
201 return 1;
202 obj->flags |= USED;
203 return 0;
206 static void mark_unreachable_referents(const struct object_id *oid)
208 struct fsck_options options = FSCK_OPTIONS_DEFAULT;
209 struct object *obj = lookup_object(the_repository, oid);
211 if (!obj || !(obj->flags & HAS_OBJ))
212 return; /* not part of our original set */
213 if (obj->flags & REACHABLE)
214 return; /* reachable objects already traversed */
217 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
218 * (and we want to avoid parsing blobs).
220 if (obj->type == OBJ_NONE) {
221 enum object_type type = oid_object_info(the_repository,
222 &obj->oid, NULL);
223 if (type > 0)
224 object_as_type(obj, type, 0);
227 options.walk = mark_used;
228 fsck_walk(obj, NULL, &options);
231 static int mark_loose_unreachable_referents(const struct object_id *oid,
232 const char *path,
233 void *data)
235 mark_unreachable_referents(oid);
236 return 0;
239 static int mark_packed_unreachable_referents(const struct object_id *oid,
240 struct packed_git *pack,
241 uint32_t pos,
242 void *data)
244 mark_unreachable_referents(oid);
245 return 0;
249 * Check a single reachable object
251 static void check_reachable_object(struct object *obj)
254 * We obviously want the object to be parsed,
255 * except if it was in a pack-file and we didn't
256 * do a full fsck
258 if (!(obj->flags & HAS_OBJ)) {
259 if (is_promisor_object(&obj->oid))
260 return;
261 if (has_object_pack(&obj->oid))
262 return; /* it is in pack - forget about it */
263 printf_ln(_("missing %s %s"),
264 printable_type(&obj->oid, obj->type),
265 describe_object(&obj->oid));
266 errors_found |= ERROR_REACHABLE;
267 return;
272 * Check a single unreachable object
274 static void check_unreachable_object(struct object *obj)
277 * Missing unreachable object? Ignore it. It's not like
278 * we miss it (since it can't be reached), nor do we want
279 * to complain about it being unreachable (since it does
280 * not exist).
282 if (!(obj->flags & HAS_OBJ))
283 return;
286 * Unreachable object that exists? Show it if asked to,
287 * since this is something that is prunable.
289 if (show_unreachable) {
290 printf_ln(_("unreachable %s %s"),
291 printable_type(&obj->oid, obj->type),
292 describe_object(&obj->oid));
293 return;
297 * "!USED" means that nothing at all points to it, including
298 * other unreachable objects. In other words, it's the "tip"
299 * of some set of unreachable objects, usually a commit that
300 * got dropped.
302 * Such starting points are more interesting than some random
303 * set of unreachable objects, so we show them even if the user
304 * hasn't asked for _all_ unreachable objects. If you have
305 * deleted a branch by mistake, this is a prime candidate to
306 * start looking at, for example.
308 if (!(obj->flags & USED)) {
309 if (show_dangling)
310 printf_ln(_("dangling %s %s"),
311 printable_type(&obj->oid, obj->type),
312 describe_object(&obj->oid));
313 if (write_lost_and_found) {
314 char *filename = git_pathdup("lost-found/%s/%s",
315 obj->type == OBJ_COMMIT ? "commit" : "other",
316 describe_object(&obj->oid));
317 FILE *f;
319 if (safe_create_leading_directories_const(filename)) {
320 error(_("could not create lost-found"));
321 free(filename);
322 return;
324 f = xfopen(filename, "w");
325 if (obj->type == OBJ_BLOB) {
326 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
327 die_errno(_("could not write '%s'"), filename);
328 } else
329 fprintf(f, "%s\n", describe_object(&obj->oid));
330 if (fclose(f))
331 die_errno(_("could not finish '%s'"),
332 filename);
333 free(filename);
335 return;
339 * Otherwise? It's there, it's unreachable, and some other unreachable
340 * object points to it. Ignore it - it's not interesting, and we showed
341 * all the interesting cases above.
345 static void check_object(struct object *obj)
347 if (verbose)
348 fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid));
350 if (obj->flags & REACHABLE)
351 check_reachable_object(obj);
352 else
353 check_unreachable_object(obj);
356 static void check_connectivity(void)
358 int i, max;
360 /* Traverse the pending reachable objects */
361 traverse_reachable();
364 * With --connectivity-only, we won't have actually opened and marked
365 * unreachable objects with USED. Do that now to make --dangling, etc
366 * accurate.
368 if (connectivity_only && (show_dangling || write_lost_and_found)) {
370 * Even though we already have a "struct object" for each of
371 * these in memory, we must not iterate over the internal
372 * object hash as we do below. Our loop would potentially
373 * resize the hash, making our iteration invalid.
375 * Instead, we'll just go back to the source list of objects,
376 * and ignore any that weren't present in our earlier
377 * traversal.
379 for_each_loose_object(mark_loose_unreachable_referents, NULL, 0);
380 for_each_packed_object(mark_packed_unreachable_referents, NULL, 0);
383 /* Look up all the requirements, warn about missing objects.. */
384 max = get_max_object_index();
385 if (verbose)
386 fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max);
388 for (i = 0; i < max; i++) {
389 struct object *obj = get_indexed_object(i);
391 if (obj)
392 check_object(obj);
396 static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
398 int err;
400 if (obj->flags & SEEN)
401 return 0;
402 obj->flags |= SEEN;
404 if (verbose)
405 fprintf_ln(stderr, _("Checking %s %s"),
406 printable_type(&obj->oid, obj->type),
407 describe_object(&obj->oid));
409 if (fsck_walk(obj, NULL, &fsck_obj_options))
410 objerror(obj, _("broken links"));
411 err = fsck_object(obj, buffer, size, &fsck_obj_options);
412 if (err)
413 goto out;
415 if (obj->type == OBJ_COMMIT) {
416 struct commit *commit = (struct commit *) obj;
418 if (!commit->parents && show_root)
419 printf_ln(_("root %s"),
420 describe_object(&commit->object.oid));
423 if (obj->type == OBJ_TAG) {
424 struct tag *tag = (struct tag *) obj;
426 if (show_tags && tag->tagged) {
427 printf_ln(_("tagged %s %s (%s) in %s"),
428 printable_type(&tag->tagged->oid, tag->tagged->type),
429 describe_object(&tag->tagged->oid),
430 tag->tag,
431 describe_object(&tag->object.oid));
435 out:
436 if (obj->type == OBJ_TREE)
437 free_tree_buffer((struct tree *)obj);
438 if (obj->type == OBJ_COMMIT)
439 free_commit_buffer(the_repository->parsed_objects,
440 (struct commit *)obj);
441 return err;
444 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
445 unsigned long size, void *buffer, int *eaten)
448 * Note, buffer may be NULL if type is OBJ_BLOB. See
449 * verify_packfile(), data_valid variable for details.
451 struct object *obj;
452 obj = parse_object_buffer(the_repository, oid, type, size, buffer,
453 eaten);
454 if (!obj) {
455 errors_found |= ERROR_OBJECT;
456 return error(_("%s: object corrupt or missing"),
457 oid_to_hex(oid));
459 obj->flags &= ~(REACHABLE | SEEN);
460 obj->flags |= HAS_OBJ;
461 return fsck_obj(obj, buffer, size);
464 static int default_refs;
466 static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
467 timestamp_t timestamp)
469 struct object *obj;
471 if (!is_null_oid(oid)) {
472 obj = lookup_object(the_repository, oid);
473 if (obj && (obj->flags & HAS_OBJ)) {
474 if (timestamp)
475 fsck_put_object_name(&fsck_walk_options, oid,
476 "%s@{%"PRItime"}",
477 refname, timestamp);
478 obj->flags |= USED;
479 mark_object_reachable(obj);
480 } else if (!is_promisor_object(oid)) {
481 error(_("%s: invalid reflog entry %s"),
482 refname, oid_to_hex(oid));
483 errors_found |= ERROR_REACHABLE;
488 static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
489 const char *email, timestamp_t timestamp, int tz,
490 const char *message, void *cb_data)
492 const char *refname = cb_data;
494 if (verbose)
495 fprintf_ln(stderr, _("Checking reflog %s->%s"),
496 oid_to_hex(ooid), oid_to_hex(noid));
498 fsck_handle_reflog_oid(refname, ooid, 0);
499 fsck_handle_reflog_oid(refname, noid, timestamp);
500 return 0;
503 static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
504 int flag, void *cb_data)
506 struct strbuf refname = STRBUF_INIT;
508 strbuf_worktree_ref(cb_data, &refname, logname);
509 for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
510 strbuf_release(&refname);
511 return 0;
514 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
515 int flag, void *cb_data)
517 struct object *obj;
519 obj = parse_object(the_repository, oid);
520 if (!obj) {
521 if (is_promisor_object(oid)) {
523 * Increment default_refs anyway, because this is a
524 * valid ref.
526 default_refs++;
527 return 0;
529 error(_("%s: invalid sha1 pointer %s"),
530 refname, oid_to_hex(oid));
531 errors_found |= ERROR_REACHABLE;
532 /* We'll continue with the rest despite the error.. */
533 return 0;
535 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
536 error(_("%s: not a commit"), refname);
537 errors_found |= ERROR_REFS;
539 default_refs++;
540 obj->flags |= USED;
541 fsck_put_object_name(&fsck_walk_options,
542 oid, "%s", refname);
543 mark_object_reachable(obj);
545 return 0;
548 static int fsck_head_link(const char *head_ref_name,
549 const char **head_points_at,
550 struct object_id *head_oid);
552 static void get_default_heads(void)
554 struct worktree **worktrees, **p;
555 const char *head_points_at;
556 struct object_id head_oid;
558 for_each_rawref(fsck_handle_ref, NULL);
560 worktrees = get_worktrees();
561 for (p = worktrees; *p; p++) {
562 struct worktree *wt = *p;
563 struct strbuf ref = STRBUF_INIT;
565 strbuf_worktree_ref(wt, &ref, "HEAD");
566 fsck_head_link(ref.buf, &head_points_at, &head_oid);
567 if (head_points_at && !is_null_oid(&head_oid))
568 fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
569 strbuf_release(&ref);
571 if (include_reflogs)
572 refs_for_each_reflog(get_worktree_ref_store(wt),
573 fsck_handle_reflog, wt);
575 free_worktrees(worktrees);
578 * Not having any default heads isn't really fatal, but
579 * it does mean that "--unreachable" no longer makes any
580 * sense (since in this case everything will obviously
581 * be unreachable by definition.
583 * Showing dangling objects is valid, though (as those
584 * dangling objects are likely lost heads).
586 * So we just print a warning about it, and clear the
587 * "show_unreachable" flag.
589 if (!default_refs) {
590 fprintf_ln(stderr, _("notice: No default references"));
591 show_unreachable = 0;
595 static int fsck_loose(const struct object_id *oid, const char *path, void *data)
597 struct object *obj;
598 enum object_type type;
599 unsigned long size;
600 void *contents;
601 int eaten;
603 if (read_loose_object(path, oid, &type, &size, &contents) < 0) {
604 errors_found |= ERROR_OBJECT;
605 error(_("%s: object corrupt or missing: %s"),
606 oid_to_hex(oid), path);
607 return 0; /* keep checking other objects */
610 if (!contents && type != OBJ_BLOB)
611 BUG("read_loose_object streamed a non-blob");
613 obj = parse_object_buffer(the_repository, oid, type, size,
614 contents, &eaten);
616 if (!obj) {
617 errors_found |= ERROR_OBJECT;
618 error(_("%s: object could not be parsed: %s"),
619 oid_to_hex(oid), path);
620 if (!eaten)
621 free(contents);
622 return 0; /* keep checking other objects */
625 obj->flags &= ~(REACHABLE | SEEN);
626 obj->flags |= HAS_OBJ;
627 if (fsck_obj(obj, contents, size))
628 errors_found |= ERROR_OBJECT;
630 if (!eaten)
631 free(contents);
632 return 0; /* keep checking other objects, even if we saw an error */
635 static int fsck_cruft(const char *basename, const char *path, void *data)
637 if (!starts_with(basename, "tmp_obj_"))
638 fprintf_ln(stderr, _("bad sha1 file: %s"), path);
639 return 0;
642 static int fsck_subdir(unsigned int nr, const char *path, void *progress)
644 display_progress(progress, nr + 1);
645 return 0;
648 static void fsck_object_dir(const char *path)
650 struct progress *progress = NULL;
652 if (verbose)
653 fprintf_ln(stderr, _("Checking object directory"));
655 if (show_progress)
656 progress = start_progress(_("Checking object directories"), 256);
658 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
659 progress);
660 display_progress(progress, 256);
661 stop_progress(&progress);
664 static int fsck_head_link(const char *head_ref_name,
665 const char **head_points_at,
666 struct object_id *head_oid)
668 int null_is_error = 0;
670 if (verbose)
671 fprintf_ln(stderr, _("Checking %s link"), head_ref_name);
673 *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
674 if (!*head_points_at) {
675 errors_found |= ERROR_REFS;
676 return error(_("invalid %s"), head_ref_name);
678 if (!strcmp(*head_points_at, head_ref_name))
679 /* detached HEAD */
680 null_is_error = 1;
681 else if (!starts_with(*head_points_at, "refs/heads/")) {
682 errors_found |= ERROR_REFS;
683 return error(_("%s points to something strange (%s)"),
684 head_ref_name, *head_points_at);
686 if (is_null_oid(head_oid)) {
687 if (null_is_error) {
688 errors_found |= ERROR_REFS;
689 return error(_("%s: detached HEAD points at nothing"),
690 head_ref_name);
692 fprintf_ln(stderr,
693 _("notice: %s points to an unborn branch (%s)"),
694 head_ref_name, *head_points_at + 11);
696 return 0;
699 static int fsck_cache_tree(struct cache_tree *it)
701 int i;
702 int err = 0;
704 if (verbose)
705 fprintf_ln(stderr, _("Checking cache tree"));
707 if (0 <= it->entry_count) {
708 struct object *obj = parse_object(the_repository, &it->oid);
709 if (!obj) {
710 error(_("%s: invalid sha1 pointer in cache-tree"),
711 oid_to_hex(&it->oid));
712 errors_found |= ERROR_REFS;
713 return 1;
715 obj->flags |= USED;
716 fsck_put_object_name(&fsck_walk_options, &it->oid, ":");
717 mark_object_reachable(obj);
718 if (obj->type != OBJ_TREE)
719 err |= objerror(obj, _("non-tree in cache-tree"));
721 for (i = 0; i < it->subtree_nr; i++)
722 err |= fsck_cache_tree(it->down[i]->cache_tree);
723 return err;
726 static void mark_object_for_connectivity(const struct object_id *oid)
728 struct object *obj = lookup_unknown_object(oid);
729 obj->flags |= HAS_OBJ;
732 static int mark_loose_for_connectivity(const struct object_id *oid,
733 const char *path,
734 void *data)
736 mark_object_for_connectivity(oid);
737 return 0;
740 static int mark_packed_for_connectivity(const struct object_id *oid,
741 struct packed_git *pack,
742 uint32_t pos,
743 void *data)
745 mark_object_for_connectivity(oid);
746 return 0;
749 static char const * const fsck_usage[] = {
750 N_("git fsck [<options>] [<object>...]"),
751 NULL
754 static struct option fsck_opts[] = {
755 OPT__VERBOSE(&verbose, N_("be verbose")),
756 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
757 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
758 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
759 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
760 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
761 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
762 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
763 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
764 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
765 OPT_BOOL(0, "lost-found", &write_lost_and_found,
766 N_("write dangling objects in .git/lost-found")),
767 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
768 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
769 OPT_END(),
772 int cmd_fsck(int argc, const char **argv, const char *prefix)
774 int i;
775 struct object_directory *odb;
777 /* fsck knows how to handle missing promisor objects */
778 fetch_if_missing = 0;
780 errors_found = 0;
781 read_replace_refs = 0;
783 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
785 fsck_walk_options.walk = mark_object;
786 fsck_obj_options.walk = mark_used;
787 fsck_obj_options.error_func = fsck_error_func;
788 if (check_strict)
789 fsck_obj_options.strict = 1;
791 if (show_progress == -1)
792 show_progress = isatty(2);
793 if (verbose)
794 show_progress = 0;
796 if (write_lost_and_found) {
797 check_full = 1;
798 include_reflogs = 0;
801 if (name_objects)
802 fsck_enable_object_names(&fsck_walk_options);
804 git_config(git_fsck_config, &fsck_obj_options);
806 if (connectivity_only) {
807 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
808 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
809 } else {
810 prepare_alt_odb(the_repository);
811 for (odb = the_repository->objects->odb; odb; odb = odb->next)
812 fsck_object_dir(odb->path);
814 if (check_full) {
815 struct packed_git *p;
816 uint32_t total = 0, count = 0;
817 struct progress *progress = NULL;
819 if (show_progress) {
820 for (p = get_all_packs(the_repository); p;
821 p = p->next) {
822 if (open_pack_index(p))
823 continue;
824 total += p->num_objects;
827 progress = start_progress(_("Checking objects"), total);
829 for (p = get_all_packs(the_repository); p;
830 p = p->next) {
831 /* verify gives error messages itself */
832 if (verify_pack(the_repository,
833 p, fsck_obj_buffer,
834 progress, count))
835 errors_found |= ERROR_PACK;
836 count += p->num_objects;
838 stop_progress(&progress);
841 if (fsck_finish(&fsck_obj_options))
842 errors_found |= ERROR_OBJECT;
845 for (i = 0; i < argc; i++) {
846 const char *arg = argv[i];
847 struct object_id oid;
848 if (!get_oid(arg, &oid)) {
849 struct object *obj = lookup_object(the_repository,
850 &oid);
852 if (!obj || !(obj->flags & HAS_OBJ)) {
853 if (is_promisor_object(&oid))
854 continue;
855 error(_("%s: object missing"), oid_to_hex(&oid));
856 errors_found |= ERROR_OBJECT;
857 continue;
860 obj->flags |= USED;
861 fsck_put_object_name(&fsck_walk_options, &oid,
862 "%s", arg);
863 mark_object_reachable(obj);
864 continue;
866 error(_("invalid parameter: expected sha1, got '%s'"), arg);
867 errors_found |= ERROR_OBJECT;
871 * If we've not been given any explicit head information, do the
872 * default ones from .git/refs. We also consider the index file
873 * in this case (ie this implies --cache).
875 if (!argc) {
876 get_default_heads();
877 keep_cache_objects = 1;
880 if (keep_cache_objects) {
881 verify_index_checksum = 1;
882 verify_ce_order = 1;
883 read_cache();
884 for (i = 0; i < active_nr; i++) {
885 unsigned int mode;
886 struct blob *blob;
887 struct object *obj;
889 mode = active_cache[i]->ce_mode;
890 if (S_ISGITLINK(mode))
891 continue;
892 blob = lookup_blob(the_repository,
893 &active_cache[i]->oid);
894 if (!blob)
895 continue;
896 obj = &blob->object;
897 obj->flags |= USED;
898 fsck_put_object_name(&fsck_walk_options, &obj->oid,
899 ":%s", active_cache[i]->name);
900 mark_object_reachable(obj);
902 if (active_cache_tree)
903 fsck_cache_tree(active_cache_tree);
906 check_connectivity();
908 if (!git_config_get_bool("core.commitgraph", &i) && i) {
909 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
910 const char *verify_argv[] = { "commit-graph", "verify", NULL, NULL, NULL };
912 prepare_alt_odb(the_repository);
913 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
914 child_process_init(&commit_graph_verify);
915 commit_graph_verify.argv = verify_argv;
916 commit_graph_verify.git_cmd = 1;
917 verify_argv[2] = "--object-dir";
918 verify_argv[3] = odb->path;
919 if (run_command(&commit_graph_verify))
920 errors_found |= ERROR_COMMIT_GRAPH;
924 if (!git_config_get_bool("core.multipackindex", &i) && i) {
925 struct child_process midx_verify = CHILD_PROCESS_INIT;
926 const char *midx_argv[] = { "multi-pack-index", "verify", NULL, NULL, NULL };
928 prepare_alt_odb(the_repository);
929 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
930 child_process_init(&midx_verify);
931 midx_verify.argv = midx_argv;
932 midx_verify.git_cmd = 1;
933 midx_argv[2] = "--object-dir";
934 midx_argv[3] = odb->path;
935 if (run_command(&midx_verify))
936 errors_found |= ERROR_MULTI_PACK_INDEX;
940 return errors_found;