fsck: unify object-name code
[git.git] / builtin / fsck.c
blob237643cc1d9aade45c5b3885dadcf6701c119566
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(struct object *obj)
55 return fsck_describe_object(&fsck_walk_options, obj);
58 static const char *printable_type(struct object *obj)
60 const char *ret;
62 if (obj->type == OBJ_NONE) {
63 enum object_type type = oid_object_info(the_repository,
64 &obj->oid, NULL);
65 if (type > 0)
66 object_as_type(the_repository, obj, type, 0);
69 ret = type_name(obj->type);
70 if (!ret)
71 ret = _("unknown");
73 return ret;
76 static int fsck_config(const char *var, const char *value, void *cb)
78 if (strcmp(var, "fsck.skiplist") == 0) {
79 const char *path;
80 struct strbuf sb = STRBUF_INIT;
82 if (git_config_pathname(&path, var, value))
83 return 1;
84 strbuf_addf(&sb, "skiplist=%s", path);
85 free((char *)path);
86 fsck_set_msg_types(&fsck_obj_options, sb.buf);
87 strbuf_release(&sb);
88 return 0;
91 if (skip_prefix(var, "fsck.", &var)) {
92 fsck_set_msg_type(&fsck_obj_options, var, value);
93 return 0;
96 return git_default_config(var, value, cb);
99 static int objerror(struct object *obj, const char *err)
101 errors_found |= ERROR_OBJECT;
102 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
103 fprintf_ln(stderr, _("error in %s %s: %s"),
104 printable_type(obj), describe_object(obj), err);
105 return -1;
108 static int fsck_error_func(struct fsck_options *o,
109 struct object *obj, int type, const char *message)
111 switch (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(obj), describe_object(obj), message);
116 return 0;
117 case FSCK_ERROR:
118 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
119 fprintf_ln(stderr, _("error in %s %s: %s"),
120 printable_type(obj), describe_object(obj), message);
121 return 1;
122 default:
123 BUG("%d (FSCK_IGNORE?) should never trigger this callback", type);
127 static struct object_array pending;
129 static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
131 struct object *parent = data;
134 * The only case data is NULL or type is OBJ_ANY is when
135 * mark_object_reachable() calls us. All the callers of
136 * that function has non-NULL obj hence ...
138 if (!obj) {
139 /* ... these references to parent->fld are safe here */
140 printf_ln(_("broken link from %7s %s"),
141 printable_type(parent), describe_object(parent));
142 printf_ln(_("broken link from %7s %s"),
143 (type == OBJ_ANY ? _("unknown") : type_name(type)),
144 _("unknown"));
145 errors_found |= ERROR_REACHABLE;
146 return 1;
149 if (type != OBJ_ANY && obj->type != type)
150 /* ... and the reference to parent is safe here */
151 objerror(parent, _("wrong object type in link"));
153 if (obj->flags & REACHABLE)
154 return 0;
155 obj->flags |= REACHABLE;
157 if (is_promisor_object(&obj->oid))
159 * Further recursion does not need to be performed on this
160 * object since it is a promisor object (so it does not need to
161 * be added to "pending").
163 return 0;
165 if (!(obj->flags & HAS_OBJ)) {
166 if (parent && !has_object_file(&obj->oid)) {
167 printf_ln(_("broken link from %7s %s\n"
168 " to %7s %s"),
169 printable_type(parent),
170 describe_object(parent),
171 printable_type(obj),
172 describe_object(obj));
173 errors_found |= ERROR_REACHABLE;
175 return 1;
178 add_object_array(obj, NULL, &pending);
179 return 0;
182 static void mark_object_reachable(struct object *obj)
184 mark_object(obj, OBJ_ANY, NULL, NULL);
187 static int traverse_one_object(struct object *obj)
189 int result = fsck_walk(obj, obj, &fsck_walk_options);
191 if (obj->type == OBJ_TREE) {
192 struct tree *tree = (struct tree *)obj;
193 free_tree_buffer(tree);
195 return result;
198 static int traverse_reachable(void)
200 struct progress *progress = NULL;
201 unsigned int nr = 0;
202 int result = 0;
203 if (show_progress)
204 progress = start_delayed_progress(_("Checking connectivity"), 0);
205 while (pending.nr) {
206 result |= traverse_one_object(object_array_pop(&pending));
207 display_progress(progress, ++nr);
209 stop_progress(&progress);
210 return !!result;
213 static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
215 if (!obj)
216 return 1;
217 obj->flags |= USED;
218 return 0;
221 static void mark_unreachable_referents(const struct object_id *oid)
223 struct fsck_options options = FSCK_OPTIONS_DEFAULT;
224 struct object *obj = lookup_object(the_repository, oid);
226 if (!obj || !(obj->flags & HAS_OBJ))
227 return; /* not part of our original set */
228 if (obj->flags & REACHABLE)
229 return; /* reachable objects already traversed */
232 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
233 * (and we want to avoid parsing blobs).
235 if (obj->type == OBJ_NONE) {
236 enum object_type type = oid_object_info(the_repository,
237 &obj->oid, NULL);
238 if (type > 0)
239 object_as_type(the_repository, obj, type, 0);
242 options.walk = mark_used;
243 fsck_walk(obj, NULL, &options);
246 static int mark_loose_unreachable_referents(const struct object_id *oid,
247 const char *path,
248 void *data)
250 mark_unreachable_referents(oid);
251 return 0;
254 static int mark_packed_unreachable_referents(const struct object_id *oid,
255 struct packed_git *pack,
256 uint32_t pos,
257 void *data)
259 mark_unreachable_referents(oid);
260 return 0;
264 * Check a single reachable object
266 static void check_reachable_object(struct object *obj)
269 * We obviously want the object to be parsed,
270 * except if it was in a pack-file and we didn't
271 * do a full fsck
273 if (!(obj->flags & HAS_OBJ)) {
274 if (is_promisor_object(&obj->oid))
275 return;
276 if (has_object_pack(&obj->oid))
277 return; /* it is in pack - forget about it */
278 printf_ln(_("missing %s %s"), printable_type(obj),
279 describe_object(obj));
280 errors_found |= ERROR_REACHABLE;
281 return;
286 * Check a single unreachable object
288 static void check_unreachable_object(struct object *obj)
291 * Missing unreachable object? Ignore it. It's not like
292 * we miss it (since it can't be reached), nor do we want
293 * to complain about it being unreachable (since it does
294 * not exist).
296 if (!(obj->flags & HAS_OBJ))
297 return;
300 * Unreachable object that exists? Show it if asked to,
301 * since this is something that is prunable.
303 if (show_unreachable) {
304 printf_ln(_("unreachable %s %s"), printable_type(obj),
305 describe_object(obj));
306 return;
310 * "!USED" means that nothing at all points to it, including
311 * other unreachable objects. In other words, it's the "tip"
312 * of some set of unreachable objects, usually a commit that
313 * got dropped.
315 * Such starting points are more interesting than some random
316 * set of unreachable objects, so we show them even if the user
317 * hasn't asked for _all_ unreachable objects. If you have
318 * deleted a branch by mistake, this is a prime candidate to
319 * start looking at, for example.
321 if (!(obj->flags & USED)) {
322 if (show_dangling)
323 printf_ln(_("dangling %s %s"), printable_type(obj),
324 describe_object(obj));
325 if (write_lost_and_found) {
326 char *filename = git_pathdup("lost-found/%s/%s",
327 obj->type == OBJ_COMMIT ? "commit" : "other",
328 describe_object(obj));
329 FILE *f;
331 if (safe_create_leading_directories_const(filename)) {
332 error(_("could not create lost-found"));
333 free(filename);
334 return;
336 f = xfopen(filename, "w");
337 if (obj->type == OBJ_BLOB) {
338 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
339 die_errno(_("could not write '%s'"), filename);
340 } else
341 fprintf(f, "%s\n", describe_object(obj));
342 if (fclose(f))
343 die_errno(_("could not finish '%s'"),
344 filename);
345 free(filename);
347 return;
351 * Otherwise? It's there, it's unreachable, and some other unreachable
352 * object points to it. Ignore it - it's not interesting, and we showed
353 * all the interesting cases above.
357 static void check_object(struct object *obj)
359 if (verbose)
360 fprintf_ln(stderr, _("Checking %s"), describe_object(obj));
362 if (obj->flags & REACHABLE)
363 check_reachable_object(obj);
364 else
365 check_unreachable_object(obj);
368 static void check_connectivity(void)
370 int i, max;
372 /* Traverse the pending reachable objects */
373 traverse_reachable();
376 * With --connectivity-only, we won't have actually opened and marked
377 * unreachable objects with USED. Do that now to make --dangling, etc
378 * accurate.
380 if (connectivity_only && (show_dangling || write_lost_and_found)) {
382 * Even though we already have a "struct object" for each of
383 * these in memory, we must not iterate over the internal
384 * object hash as we do below. Our loop would potentially
385 * resize the hash, making our iteration invalid.
387 * Instead, we'll just go back to the source list of objects,
388 * and ignore any that weren't present in our earlier
389 * traversal.
391 for_each_loose_object(mark_loose_unreachable_referents, NULL, 0);
392 for_each_packed_object(mark_packed_unreachable_referents, NULL, 0);
395 /* Look up all the requirements, warn about missing objects.. */
396 max = get_max_object_index();
397 if (verbose)
398 fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max);
400 for (i = 0; i < max; i++) {
401 struct object *obj = get_indexed_object(i);
403 if (obj)
404 check_object(obj);
408 static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
410 int err;
412 if (obj->flags & SEEN)
413 return 0;
414 obj->flags |= SEEN;
416 if (verbose)
417 fprintf_ln(stderr, _("Checking %s %s"),
418 printable_type(obj), describe_object(obj));
420 if (fsck_walk(obj, NULL, &fsck_obj_options))
421 objerror(obj, _("broken links"));
422 err = fsck_object(obj, buffer, size, &fsck_obj_options);
423 if (err)
424 goto out;
426 if (obj->type == OBJ_COMMIT) {
427 struct commit *commit = (struct commit *) obj;
429 if (!commit->parents && show_root)
430 printf_ln(_("root %s"),
431 describe_object(&commit->object));
434 if (obj->type == OBJ_TAG) {
435 struct tag *tag = (struct tag *) obj;
437 if (show_tags && tag->tagged) {
438 printf_ln(_("tagged %s %s (%s) in %s"),
439 printable_type(tag->tagged),
440 describe_object(tag->tagged),
441 tag->tag,
442 describe_object(&tag->object));
446 out:
447 if (obj->type == OBJ_TREE)
448 free_tree_buffer((struct tree *)obj);
449 if (obj->type == OBJ_COMMIT)
450 free_commit_buffer(the_repository->parsed_objects,
451 (struct commit *)obj);
452 return err;
455 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
456 unsigned long size, void *buffer, int *eaten)
459 * Note, buffer may be NULL if type is OBJ_BLOB. See
460 * verify_packfile(), data_valid variable for details.
462 struct object *obj;
463 obj = parse_object_buffer(the_repository, oid, type, size, buffer,
464 eaten);
465 if (!obj) {
466 errors_found |= ERROR_OBJECT;
467 return error(_("%s: object corrupt or missing"),
468 oid_to_hex(oid));
470 obj->flags &= ~(REACHABLE | SEEN);
471 obj->flags |= HAS_OBJ;
472 return fsck_obj(obj, buffer, size);
475 static int default_refs;
477 static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
478 timestamp_t timestamp)
480 struct object *obj;
482 if (!is_null_oid(oid)) {
483 obj = lookup_object(the_repository, oid);
484 if (obj && (obj->flags & HAS_OBJ)) {
485 if (timestamp)
486 fsck_put_object_name(&fsck_walk_options, obj,
487 "%s@{%"PRItime"}",
488 refname, timestamp);
489 obj->flags |= USED;
490 mark_object_reachable(obj);
491 } else if (!is_promisor_object(oid)) {
492 error(_("%s: invalid reflog entry %s"),
493 refname, oid_to_hex(oid));
494 errors_found |= ERROR_REACHABLE;
499 static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
500 const char *email, timestamp_t timestamp, int tz,
501 const char *message, void *cb_data)
503 const char *refname = cb_data;
505 if (verbose)
506 fprintf_ln(stderr, _("Checking reflog %s->%s"),
507 oid_to_hex(ooid), oid_to_hex(noid));
509 fsck_handle_reflog_oid(refname, ooid, 0);
510 fsck_handle_reflog_oid(refname, noid, timestamp);
511 return 0;
514 static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
515 int flag, void *cb_data)
517 struct strbuf refname = STRBUF_INIT;
519 strbuf_worktree_ref(cb_data, &refname, logname);
520 for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
521 strbuf_release(&refname);
522 return 0;
525 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
526 int flag, void *cb_data)
528 struct object *obj;
530 obj = parse_object(the_repository, oid);
531 if (!obj) {
532 if (is_promisor_object(oid)) {
534 * Increment default_refs anyway, because this is a
535 * valid ref.
537 default_refs++;
538 return 0;
540 error(_("%s: invalid sha1 pointer %s"),
541 refname, oid_to_hex(oid));
542 errors_found |= ERROR_REACHABLE;
543 /* We'll continue with the rest despite the error.. */
544 return 0;
546 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
547 error(_("%s: not a commit"), refname);
548 errors_found |= ERROR_REFS;
550 default_refs++;
551 obj->flags |= USED;
552 fsck_put_object_name(&fsck_walk_options,
553 obj, "%s", refname);
554 mark_object_reachable(obj);
556 return 0;
559 static int fsck_head_link(const char *head_ref_name,
560 const char **head_points_at,
561 struct object_id *head_oid);
563 static void get_default_heads(void)
565 struct worktree **worktrees, **p;
566 const char *head_points_at;
567 struct object_id head_oid;
569 for_each_rawref(fsck_handle_ref, NULL);
571 worktrees = get_worktrees(0);
572 for (p = worktrees; *p; p++) {
573 struct worktree *wt = *p;
574 struct strbuf ref = STRBUF_INIT;
576 strbuf_worktree_ref(wt, &ref, "HEAD");
577 fsck_head_link(ref.buf, &head_points_at, &head_oid);
578 if (head_points_at && !is_null_oid(&head_oid))
579 fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
580 strbuf_release(&ref);
582 if (include_reflogs)
583 refs_for_each_reflog(get_worktree_ref_store(wt),
584 fsck_handle_reflog, wt);
586 free_worktrees(worktrees);
589 * Not having any default heads isn't really fatal, but
590 * it does mean that "--unreachable" no longer makes any
591 * sense (since in this case everything will obviously
592 * be unreachable by definition.
594 * Showing dangling objects is valid, though (as those
595 * dangling objects are likely lost heads).
597 * So we just print a warning about it, and clear the
598 * "show_unreachable" flag.
600 if (!default_refs) {
601 fprintf_ln(stderr, _("notice: No default references"));
602 show_unreachable = 0;
606 static int fsck_loose(const struct object_id *oid, const char *path, void *data)
608 struct object *obj;
609 enum object_type type;
610 unsigned long size;
611 void *contents;
612 int eaten;
614 if (read_loose_object(path, oid, &type, &size, &contents) < 0) {
615 errors_found |= ERROR_OBJECT;
616 error(_("%s: object corrupt or missing: %s"),
617 oid_to_hex(oid), path);
618 return 0; /* keep checking other objects */
621 if (!contents && type != OBJ_BLOB)
622 BUG("read_loose_object streamed a non-blob");
624 obj = parse_object_buffer(the_repository, oid, type, size,
625 contents, &eaten);
627 if (!obj) {
628 errors_found |= ERROR_OBJECT;
629 error(_("%s: object could not be parsed: %s"),
630 oid_to_hex(oid), path);
631 if (!eaten)
632 free(contents);
633 return 0; /* keep checking other objects */
636 obj->flags &= ~(REACHABLE | SEEN);
637 obj->flags |= HAS_OBJ;
638 if (fsck_obj(obj, contents, size))
639 errors_found |= ERROR_OBJECT;
641 if (!eaten)
642 free(contents);
643 return 0; /* keep checking other objects, even if we saw an error */
646 static int fsck_cruft(const char *basename, const char *path, void *data)
648 if (!starts_with(basename, "tmp_obj_"))
649 fprintf_ln(stderr, _("bad sha1 file: %s"), path);
650 return 0;
653 static int fsck_subdir(unsigned int nr, const char *path, void *progress)
655 display_progress(progress, nr + 1);
656 return 0;
659 static void fsck_object_dir(const char *path)
661 struct progress *progress = NULL;
663 if (verbose)
664 fprintf_ln(stderr, _("Checking object directory"));
666 if (show_progress)
667 progress = start_progress(_("Checking object directories"), 256);
669 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
670 progress);
671 display_progress(progress, 256);
672 stop_progress(&progress);
675 static int fsck_head_link(const char *head_ref_name,
676 const char **head_points_at,
677 struct object_id *head_oid)
679 int null_is_error = 0;
681 if (verbose)
682 fprintf_ln(stderr, _("Checking %s link"), head_ref_name);
684 *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
685 if (!*head_points_at) {
686 errors_found |= ERROR_REFS;
687 return error(_("invalid %s"), head_ref_name);
689 if (!strcmp(*head_points_at, head_ref_name))
690 /* detached HEAD */
691 null_is_error = 1;
692 else if (!starts_with(*head_points_at, "refs/heads/")) {
693 errors_found |= ERROR_REFS;
694 return error(_("%s points to something strange (%s)"),
695 head_ref_name, *head_points_at);
697 if (is_null_oid(head_oid)) {
698 if (null_is_error) {
699 errors_found |= ERROR_REFS;
700 return error(_("%s: detached HEAD points at nothing"),
701 head_ref_name);
703 fprintf_ln(stderr,
704 _("notice: %s points to an unborn branch (%s)"),
705 head_ref_name, *head_points_at + 11);
707 return 0;
710 static int fsck_cache_tree(struct cache_tree *it)
712 int i;
713 int err = 0;
715 if (verbose)
716 fprintf_ln(stderr, _("Checking cache tree"));
718 if (0 <= it->entry_count) {
719 struct object *obj = parse_object(the_repository, &it->oid);
720 if (!obj) {
721 error(_("%s: invalid sha1 pointer in cache-tree"),
722 oid_to_hex(&it->oid));
723 errors_found |= ERROR_REFS;
724 return 1;
726 obj->flags |= USED;
727 fsck_put_object_name(&fsck_walk_options, obj, ":");
728 mark_object_reachable(obj);
729 if (obj->type != OBJ_TREE)
730 err |= objerror(obj, _("non-tree in cache-tree"));
732 for (i = 0; i < it->subtree_nr; i++)
733 err |= fsck_cache_tree(it->down[i]->cache_tree);
734 return err;
737 static void mark_object_for_connectivity(const struct object_id *oid)
739 struct object *obj = lookup_unknown_object(oid);
740 obj->flags |= HAS_OBJ;
743 static int mark_loose_for_connectivity(const struct object_id *oid,
744 const char *path,
745 void *data)
747 mark_object_for_connectivity(oid);
748 return 0;
751 static int mark_packed_for_connectivity(const struct object_id *oid,
752 struct packed_git *pack,
753 uint32_t pos,
754 void *data)
756 mark_object_for_connectivity(oid);
757 return 0;
760 static char const * const fsck_usage[] = {
761 N_("git fsck [<options>] [<object>...]"),
762 NULL
765 static struct option fsck_opts[] = {
766 OPT__VERBOSE(&verbose, N_("be verbose")),
767 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
768 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
769 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
770 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
771 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
772 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
773 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
774 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
775 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
776 OPT_BOOL(0, "lost-found", &write_lost_and_found,
777 N_("write dangling objects in .git/lost-found")),
778 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
779 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
780 OPT_END(),
783 int cmd_fsck(int argc, const char **argv, const char *prefix)
785 int i;
786 struct object_directory *odb;
788 /* fsck knows how to handle missing promisor objects */
789 fetch_if_missing = 0;
791 errors_found = 0;
792 read_replace_refs = 0;
794 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
796 fsck_walk_options.walk = mark_object;
797 fsck_obj_options.walk = mark_used;
798 fsck_obj_options.error_func = fsck_error_func;
799 if (check_strict)
800 fsck_obj_options.strict = 1;
802 if (show_progress == -1)
803 show_progress = isatty(2);
804 if (verbose)
805 show_progress = 0;
807 if (write_lost_and_found) {
808 check_full = 1;
809 include_reflogs = 0;
812 if (name_objects)
813 fsck_enable_object_names(&fsck_walk_options);
815 git_config(fsck_config, NULL);
817 if (connectivity_only) {
818 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
819 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
820 } else {
821 prepare_alt_odb(the_repository);
822 for (odb = the_repository->objects->odb; odb; odb = odb->next)
823 fsck_object_dir(odb->path);
825 if (check_full) {
826 struct packed_git *p;
827 uint32_t total = 0, count = 0;
828 struct progress *progress = NULL;
830 if (show_progress) {
831 for (p = get_all_packs(the_repository); p;
832 p = p->next) {
833 if (open_pack_index(p))
834 continue;
835 total += p->num_objects;
838 progress = start_progress(_("Checking objects"), total);
840 for (p = get_all_packs(the_repository); p;
841 p = p->next) {
842 /* verify gives error messages itself */
843 if (verify_pack(the_repository,
844 p, fsck_obj_buffer,
845 progress, count))
846 errors_found |= ERROR_PACK;
847 count += p->num_objects;
849 stop_progress(&progress);
852 if (fsck_finish(&fsck_obj_options))
853 errors_found |= ERROR_OBJECT;
856 for (i = 0; i < argc; i++) {
857 const char *arg = argv[i];
858 struct object_id oid;
859 if (!get_oid(arg, &oid)) {
860 struct object *obj = lookup_object(the_repository,
861 &oid);
863 if (!obj || !(obj->flags & HAS_OBJ)) {
864 if (is_promisor_object(&oid))
865 continue;
866 error(_("%s: object missing"), oid_to_hex(&oid));
867 errors_found |= ERROR_OBJECT;
868 continue;
871 obj->flags |= USED;
872 fsck_put_object_name(&fsck_walk_options, obj,
873 "%s", arg);
874 mark_object_reachable(obj);
875 continue;
877 error(_("invalid parameter: expected sha1, got '%s'"), arg);
878 errors_found |= ERROR_OBJECT;
882 * If we've not been given any explicit head information, do the
883 * default ones from .git/refs. We also consider the index file
884 * in this case (ie this implies --cache).
886 if (!argc) {
887 get_default_heads();
888 keep_cache_objects = 1;
891 if (keep_cache_objects) {
892 verify_index_checksum = 1;
893 verify_ce_order = 1;
894 read_cache();
895 for (i = 0; i < active_nr; i++) {
896 unsigned int mode;
897 struct blob *blob;
898 struct object *obj;
900 mode = active_cache[i]->ce_mode;
901 if (S_ISGITLINK(mode))
902 continue;
903 blob = lookup_blob(the_repository,
904 &active_cache[i]->oid);
905 if (!blob)
906 continue;
907 obj = &blob->object;
908 obj->flags |= USED;
909 fsck_put_object_name(&fsck_walk_options, obj,
910 ":%s", active_cache[i]->name);
911 mark_object_reachable(obj);
913 if (active_cache_tree)
914 fsck_cache_tree(active_cache_tree);
917 check_connectivity();
919 if (!git_config_get_bool("core.commitgraph", &i) && i) {
920 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
921 const char *verify_argv[] = { "commit-graph", "verify", NULL, NULL, NULL };
923 prepare_alt_odb(the_repository);
924 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
925 child_process_init(&commit_graph_verify);
926 commit_graph_verify.argv = verify_argv;
927 commit_graph_verify.git_cmd = 1;
928 verify_argv[2] = "--object-dir";
929 verify_argv[3] = odb->path;
930 if (run_command(&commit_graph_verify))
931 errors_found |= ERROR_COMMIT_GRAPH;
935 if (!git_config_get_bool("core.multipackindex", &i) && i) {
936 struct child_process midx_verify = CHILD_PROCESS_INIT;
937 const char *midx_argv[] = { "multi-pack-index", "verify", NULL, NULL, NULL };
939 prepare_alt_odb(the_repository);
940 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
941 child_process_init(&midx_verify);
942 midx_verify.argv = midx_argv;
943 midx_verify.git_cmd = 1;
944 midx_argv[2] = "--object-dir";
945 midx_argv[3] = odb->path;
946 if (run_command(&midx_verify))
947 errors_found |= ERROR_COMMIT_GRAPH;
951 return errors_found;