Merge branch 'es/chainlint'
[git/debian.git] / builtin / fsck.c
blob9e54892311d5a698d51abb66ac73892c739732a1
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, enum object_type type,
113 void *data, struct fsck_options *options)
115 struct object *parent = data;
118 * The only case data is NULL or type is OBJ_ANY is when
119 * mark_object_reachable() calls us. All the callers of
120 * that function has non-NULL obj hence ...
122 if (!obj) {
123 /* ... these references to parent->fld are safe here */
124 printf_ln(_("broken link from %7s %s"),
125 printable_type(&parent->oid, parent->type),
126 describe_object(&parent->oid));
127 printf_ln(_("broken link from %7s %s"),
128 (type == OBJ_ANY ? _("unknown") : type_name(type)),
129 _("unknown"));
130 errors_found |= ERROR_REACHABLE;
131 return 1;
134 if (type != OBJ_ANY && obj->type != type)
135 /* ... and the reference to parent is safe here */
136 objerror(parent, _("wrong object type in link"));
138 if (obj->flags & REACHABLE)
139 return 0;
140 obj->flags |= REACHABLE;
142 if (is_promisor_object(&obj->oid))
144 * Further recursion does not need to be performed on this
145 * object since it is a promisor object (so it does not need to
146 * be added to "pending").
148 return 0;
150 if (!(obj->flags & HAS_OBJ)) {
151 if (parent && !has_object(the_repository, &obj->oid, 1)) {
152 printf_ln(_("broken link from %7s %s\n"
153 " to %7s %s"),
154 printable_type(&parent->oid, parent->type),
155 describe_object(&parent->oid),
156 printable_type(&obj->oid, obj->type),
157 describe_object(&obj->oid));
158 errors_found |= ERROR_REACHABLE;
160 return 1;
163 add_object_array(obj, NULL, &pending);
164 return 0;
167 static void mark_object_reachable(struct object *obj)
169 mark_object(obj, OBJ_ANY, NULL, NULL);
172 static int traverse_one_object(struct object *obj)
174 int result = fsck_walk(obj, obj, &fsck_walk_options);
176 if (obj->type == OBJ_TREE) {
177 struct tree *tree = (struct tree *)obj;
178 free_tree_buffer(tree);
180 return result;
183 static int traverse_reachable(void)
185 struct progress *progress = NULL;
186 unsigned int nr = 0;
187 int result = 0;
188 if (show_progress)
189 progress = start_delayed_progress(_("Checking connectivity"), 0);
190 while (pending.nr) {
191 result |= traverse_one_object(object_array_pop(&pending));
192 display_progress(progress, ++nr);
194 stop_progress(&progress);
195 return !!result;
198 static int mark_used(struct object *obj, enum object_type object_type,
199 void *data, struct fsck_options *options)
201 if (!obj)
202 return 1;
203 obj->flags |= USED;
204 return 0;
207 static void mark_unreachable_referents(const struct object_id *oid)
209 struct fsck_options options = FSCK_OPTIONS_DEFAULT;
210 struct object *obj = lookup_object(the_repository, oid);
212 if (!obj || !(obj->flags & HAS_OBJ))
213 return; /* not part of our original set */
214 if (obj->flags & REACHABLE)
215 return; /* reachable objects already traversed */
218 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
219 * (and we want to avoid parsing blobs).
221 if (obj->type == OBJ_NONE) {
222 enum object_type type = oid_object_info(the_repository,
223 &obj->oid, NULL);
224 if (type > 0)
225 object_as_type(obj, type, 0);
228 options.walk = mark_used;
229 fsck_walk(obj, NULL, &options);
232 static int mark_loose_unreachable_referents(const struct object_id *oid,
233 const char *path,
234 void *data)
236 mark_unreachable_referents(oid);
237 return 0;
240 static int mark_packed_unreachable_referents(const struct object_id *oid,
241 struct packed_git *pack,
242 uint32_t pos,
243 void *data)
245 mark_unreachable_referents(oid);
246 return 0;
250 * Check a single reachable object
252 static void check_reachable_object(struct object *obj)
255 * We obviously want the object to be parsed,
256 * except if it was in a pack-file and we didn't
257 * do a full fsck
259 if (!(obj->flags & HAS_OBJ)) {
260 if (is_promisor_object(&obj->oid))
261 return;
262 if (has_object_pack(&obj->oid))
263 return; /* it is in pack - forget about it */
264 printf_ln(_("missing %s %s"),
265 printable_type(&obj->oid, obj->type),
266 describe_object(&obj->oid));
267 errors_found |= ERROR_REACHABLE;
268 return;
273 * Check a single unreachable object
275 static void check_unreachable_object(struct object *obj)
278 * Missing unreachable object? Ignore it. It's not like
279 * we miss it (since it can't be reached), nor do we want
280 * to complain about it being unreachable (since it does
281 * not exist).
283 if (!(obj->flags & HAS_OBJ))
284 return;
287 * Unreachable object that exists? Show it if asked to,
288 * since this is something that is prunable.
290 if (show_unreachable) {
291 printf_ln(_("unreachable %s %s"),
292 printable_type(&obj->oid, obj->type),
293 describe_object(&obj->oid));
294 return;
298 * "!USED" means that nothing at all points to it, including
299 * other unreachable objects. In other words, it's the "tip"
300 * of some set of unreachable objects, usually a commit that
301 * got dropped.
303 * Such starting points are more interesting than some random
304 * set of unreachable objects, so we show them even if the user
305 * hasn't asked for _all_ unreachable objects. If you have
306 * deleted a branch by mistake, this is a prime candidate to
307 * start looking at, for example.
309 if (!(obj->flags & USED)) {
310 if (show_dangling)
311 printf_ln(_("dangling %s %s"),
312 printable_type(&obj->oid, obj->type),
313 describe_object(&obj->oid));
314 if (write_lost_and_found) {
315 char *filename = git_pathdup("lost-found/%s/%s",
316 obj->type == OBJ_COMMIT ? "commit" : "other",
317 describe_object(&obj->oid));
318 FILE *f;
320 if (safe_create_leading_directories_const(filename)) {
321 error(_("could not create lost-found"));
322 free(filename);
323 return;
325 f = xfopen(filename, "w");
326 if (obj->type == OBJ_BLOB) {
327 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
328 die_errno(_("could not write '%s'"), filename);
329 } else
330 fprintf(f, "%s\n", describe_object(&obj->oid));
331 if (fclose(f))
332 die_errno(_("could not finish '%s'"),
333 filename);
334 free(filename);
336 return;
340 * Otherwise? It's there, it's unreachable, and some other unreachable
341 * object points to it. Ignore it - it's not interesting, and we showed
342 * all the interesting cases above.
346 static void check_object(struct object *obj)
348 if (verbose)
349 fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid));
351 if (obj->flags & REACHABLE)
352 check_reachable_object(obj);
353 else
354 check_unreachable_object(obj);
357 static void check_connectivity(void)
359 int i, max;
361 /* Traverse the pending reachable objects */
362 traverse_reachable();
365 * With --connectivity-only, we won't have actually opened and marked
366 * unreachable objects with USED. Do that now to make --dangling, etc
367 * accurate.
369 if (connectivity_only && (show_dangling || write_lost_and_found)) {
371 * Even though we already have a "struct object" for each of
372 * these in memory, we must not iterate over the internal
373 * object hash as we do below. Our loop would potentially
374 * resize the hash, making our iteration invalid.
376 * Instead, we'll just go back to the source list of objects,
377 * and ignore any that weren't present in our earlier
378 * traversal.
380 for_each_loose_object(mark_loose_unreachable_referents, NULL, 0);
381 for_each_packed_object(mark_packed_unreachable_referents, NULL, 0);
384 /* Look up all the requirements, warn about missing objects.. */
385 max = get_max_object_index();
386 if (verbose)
387 fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max);
389 for (i = 0; i < max; i++) {
390 struct object *obj = get_indexed_object(i);
392 if (obj)
393 check_object(obj);
397 static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
399 int err;
401 if (obj->flags & SEEN)
402 return 0;
403 obj->flags |= SEEN;
405 if (verbose)
406 fprintf_ln(stderr, _("Checking %s %s"),
407 printable_type(&obj->oid, obj->type),
408 describe_object(&obj->oid));
410 if (fsck_walk(obj, NULL, &fsck_obj_options))
411 objerror(obj, _("broken links"));
412 err = fsck_object(obj, buffer, size, &fsck_obj_options);
413 if (err)
414 goto out;
416 if (obj->type == OBJ_COMMIT) {
417 struct commit *commit = (struct commit *) obj;
419 if (!commit->parents && show_root)
420 printf_ln(_("root %s"),
421 describe_object(&commit->object.oid));
424 if (obj->type == OBJ_TAG) {
425 struct tag *tag = (struct tag *) obj;
427 if (show_tags && tag->tagged) {
428 printf_ln(_("tagged %s %s (%s) in %s"),
429 printable_type(&tag->tagged->oid, tag->tagged->type),
430 describe_object(&tag->tagged->oid),
431 tag->tag,
432 describe_object(&tag->object.oid));
436 out:
437 if (obj->type == OBJ_TREE)
438 free_tree_buffer((struct tree *)obj);
439 if (obj->type == OBJ_COMMIT)
440 free_commit_buffer(the_repository->parsed_objects,
441 (struct commit *)obj);
442 return err;
445 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
446 unsigned long size, void *buffer, int *eaten)
449 * Note, buffer may be NULL if type is OBJ_BLOB. See
450 * verify_packfile(), data_valid variable for details.
452 struct object *obj;
453 obj = parse_object_buffer(the_repository, oid, type, size, buffer,
454 eaten);
455 if (!obj) {
456 errors_found |= ERROR_OBJECT;
457 return error(_("%s: object corrupt or missing"),
458 oid_to_hex(oid));
460 obj->flags &= ~(REACHABLE | SEEN);
461 obj->flags |= HAS_OBJ;
462 return fsck_obj(obj, buffer, size);
465 static int default_refs;
467 static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
468 timestamp_t timestamp)
470 struct object *obj;
472 if (!is_null_oid(oid)) {
473 obj = lookup_object(the_repository, oid);
474 if (obj && (obj->flags & HAS_OBJ)) {
475 if (timestamp)
476 fsck_put_object_name(&fsck_walk_options, oid,
477 "%s@{%"PRItime"}",
478 refname, timestamp);
479 obj->flags |= USED;
480 mark_object_reachable(obj);
481 } else if (!is_promisor_object(oid)) {
482 error(_("%s: invalid reflog entry %s"),
483 refname, oid_to_hex(oid));
484 errors_found |= ERROR_REACHABLE;
489 static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
490 const char *email, timestamp_t timestamp, int tz,
491 const char *message, void *cb_data)
493 const char *refname = cb_data;
495 if (verbose)
496 fprintf_ln(stderr, _("Checking reflog %s->%s"),
497 oid_to_hex(ooid), oid_to_hex(noid));
499 fsck_handle_reflog_oid(refname, ooid, 0);
500 fsck_handle_reflog_oid(refname, noid, timestamp);
501 return 0;
504 static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
505 int flag, void *cb_data)
507 struct strbuf refname = STRBUF_INIT;
509 strbuf_worktree_ref(cb_data, &refname, logname);
510 for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
511 strbuf_release(&refname);
512 return 0;
515 static int fsck_handle_ref(const char *refname, const struct object_id *oid,
516 int flag, void *cb_data)
518 struct object *obj;
520 obj = parse_object(the_repository, oid);
521 if (!obj) {
522 if (is_promisor_object(oid)) {
524 * Increment default_refs anyway, because this is a
525 * valid ref.
527 default_refs++;
528 return 0;
530 error(_("%s: invalid sha1 pointer %s"),
531 refname, oid_to_hex(oid));
532 errors_found |= ERROR_REACHABLE;
533 /* We'll continue with the rest despite the error.. */
534 return 0;
536 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
537 error(_("%s: not a commit"), refname);
538 errors_found |= ERROR_REFS;
540 default_refs++;
541 obj->flags |= USED;
542 fsck_put_object_name(&fsck_walk_options,
543 oid, "%s", refname);
544 mark_object_reachable(obj);
546 return 0;
549 static int fsck_head_link(const char *head_ref_name,
550 const char **head_points_at,
551 struct object_id *head_oid);
553 static void get_default_heads(void)
555 struct worktree **worktrees, **p;
556 const char *head_points_at;
557 struct object_id head_oid;
559 for_each_rawref(fsck_handle_ref, NULL);
561 worktrees = get_worktrees();
562 for (p = worktrees; *p; p++) {
563 struct worktree *wt = *p;
564 struct strbuf ref = STRBUF_INIT;
566 strbuf_worktree_ref(wt, &ref, "HEAD");
567 fsck_head_link(ref.buf, &head_points_at, &head_oid);
568 if (head_points_at && !is_null_oid(&head_oid))
569 fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
570 strbuf_release(&ref);
572 if (include_reflogs)
573 refs_for_each_reflog(get_worktree_ref_store(wt),
574 fsck_handle_reflog, wt);
576 free_worktrees(worktrees);
579 * Not having any default heads isn't really fatal, but
580 * it does mean that "--unreachable" no longer makes any
581 * sense (since in this case everything will obviously
582 * be unreachable by definition.
584 * Showing dangling objects is valid, though (as those
585 * dangling objects are likely lost heads).
587 * So we just print a warning about it, and clear the
588 * "show_unreachable" flag.
590 if (!default_refs) {
591 fprintf_ln(stderr, _("notice: No default references"));
592 show_unreachable = 0;
596 struct for_each_loose_cb
598 struct progress *progress;
599 struct strbuf obj_type;
602 static int fsck_loose(const struct object_id *oid, const char *path, void *data)
604 struct for_each_loose_cb *cb_data = data;
605 struct object *obj;
606 enum object_type type = OBJ_NONE;
607 unsigned long size;
608 void *contents = NULL;
609 int eaten;
610 struct object_info oi = OBJECT_INFO_INIT;
611 struct object_id real_oid = *null_oid();
612 int err = 0;
614 strbuf_reset(&cb_data->obj_type);
615 oi.type_name = &cb_data->obj_type;
616 oi.sizep = &size;
617 oi.typep = &type;
619 if (read_loose_object(path, oid, &real_oid, &contents, &oi) < 0) {
620 if (contents && !oideq(&real_oid, oid))
621 err = error(_("%s: hash-path mismatch, found at: %s"),
622 oid_to_hex(&real_oid), path);
623 else
624 err = error(_("%s: object corrupt or missing: %s"),
625 oid_to_hex(oid), path);
627 if (type != OBJ_NONE && type < 0)
628 err = error(_("%s: object is of unknown type '%s': %s"),
629 oid_to_hex(&real_oid), cb_data->obj_type.buf,
630 path);
631 if (err < 0) {
632 errors_found |= ERROR_OBJECT;
633 free(contents);
634 return 0; /* keep checking other objects */
637 if (!contents && type != OBJ_BLOB)
638 BUG("read_loose_object streamed a non-blob");
640 obj = parse_object_buffer(the_repository, oid, type, size,
641 contents, &eaten);
643 if (!obj) {
644 errors_found |= ERROR_OBJECT;
645 error(_("%s: object could not be parsed: %s"),
646 oid_to_hex(oid), path);
647 if (!eaten)
648 free(contents);
649 return 0; /* keep checking other objects */
652 obj->flags &= ~(REACHABLE | SEEN);
653 obj->flags |= HAS_OBJ;
654 if (fsck_obj(obj, contents, size))
655 errors_found |= ERROR_OBJECT;
657 if (!eaten)
658 free(contents);
659 return 0; /* keep checking other objects, even if we saw an error */
662 static int fsck_cruft(const char *basename, const char *path, void *data)
664 if (!starts_with(basename, "tmp_obj_"))
665 fprintf_ln(stderr, _("bad sha1 file: %s"), path);
666 return 0;
669 static int fsck_subdir(unsigned int nr, const char *path, void *data)
671 struct for_each_loose_cb *cb_data = data;
672 struct progress *progress = cb_data->progress;
673 display_progress(progress, nr + 1);
674 return 0;
677 static void fsck_object_dir(const char *path)
679 struct progress *progress = NULL;
680 struct for_each_loose_cb cb_data = {
681 .obj_type = STRBUF_INIT,
682 .progress = progress,
685 if (verbose)
686 fprintf_ln(stderr, _("Checking object directory"));
688 if (show_progress)
689 progress = start_progress(_("Checking object directories"), 256);
691 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
692 &cb_data);
693 display_progress(progress, 256);
694 stop_progress(&progress);
695 strbuf_release(&cb_data.obj_type);
698 static int fsck_head_link(const char *head_ref_name,
699 const char **head_points_at,
700 struct object_id *head_oid)
702 int null_is_error = 0;
704 if (verbose)
705 fprintf_ln(stderr, _("Checking %s link"), head_ref_name);
707 *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
708 if (!*head_points_at) {
709 errors_found |= ERROR_REFS;
710 return error(_("invalid %s"), head_ref_name);
712 if (!strcmp(*head_points_at, head_ref_name))
713 /* detached HEAD */
714 null_is_error = 1;
715 else if (!starts_with(*head_points_at, "refs/heads/")) {
716 errors_found |= ERROR_REFS;
717 return error(_("%s points to something strange (%s)"),
718 head_ref_name, *head_points_at);
720 if (is_null_oid(head_oid)) {
721 if (null_is_error) {
722 errors_found |= ERROR_REFS;
723 return error(_("%s: detached HEAD points at nothing"),
724 head_ref_name);
726 fprintf_ln(stderr,
727 _("notice: %s points to an unborn branch (%s)"),
728 head_ref_name, *head_points_at + 11);
730 return 0;
733 static int fsck_cache_tree(struct cache_tree *it)
735 int i;
736 int err = 0;
738 if (verbose)
739 fprintf_ln(stderr, _("Checking cache tree"));
741 if (0 <= it->entry_count) {
742 struct object *obj = parse_object(the_repository, &it->oid);
743 if (!obj) {
744 error(_("%s: invalid sha1 pointer in cache-tree"),
745 oid_to_hex(&it->oid));
746 errors_found |= ERROR_REFS;
747 return 1;
749 obj->flags |= USED;
750 fsck_put_object_name(&fsck_walk_options, &it->oid, ":");
751 mark_object_reachable(obj);
752 if (obj->type != OBJ_TREE)
753 err |= objerror(obj, _("non-tree in cache-tree"));
755 for (i = 0; i < it->subtree_nr; i++)
756 err |= fsck_cache_tree(it->down[i]->cache_tree);
757 return err;
760 static void mark_object_for_connectivity(const struct object_id *oid)
762 struct object *obj = lookup_unknown_object(the_repository, oid);
763 obj->flags |= HAS_OBJ;
766 static int mark_loose_for_connectivity(const struct object_id *oid,
767 const char *path,
768 void *data)
770 mark_object_for_connectivity(oid);
771 return 0;
774 static int mark_packed_for_connectivity(const struct object_id *oid,
775 struct packed_git *pack,
776 uint32_t pos,
777 void *data)
779 mark_object_for_connectivity(oid);
780 return 0;
783 static char const * const fsck_usage[] = {
784 N_("git fsck [<options>] [<object>...]"),
785 NULL
788 static struct option fsck_opts[] = {
789 OPT__VERBOSE(&verbose, N_("be verbose")),
790 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
791 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
792 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
793 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
794 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
795 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
796 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
797 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
798 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
799 OPT_BOOL(0, "lost-found", &write_lost_and_found,
800 N_("write dangling objects in .git/lost-found")),
801 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
802 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
803 OPT_END(),
806 int cmd_fsck(int argc, const char **argv, const char *prefix)
808 int i;
809 struct object_directory *odb;
811 /* fsck knows how to handle missing promisor objects */
812 fetch_if_missing = 0;
814 errors_found = 0;
815 read_replace_refs = 0;
817 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
819 fsck_walk_options.walk = mark_object;
820 fsck_obj_options.walk = mark_used;
821 fsck_obj_options.error_func = fsck_error_func;
822 if (check_strict)
823 fsck_obj_options.strict = 1;
825 if (show_progress == -1)
826 show_progress = isatty(2);
827 if (verbose)
828 show_progress = 0;
830 if (write_lost_and_found) {
831 check_full = 1;
832 include_reflogs = 0;
835 if (name_objects)
836 fsck_enable_object_names(&fsck_walk_options);
838 git_config(git_fsck_config, &fsck_obj_options);
839 prepare_repo_settings(the_repository);
841 if (connectivity_only) {
842 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
843 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
844 } else {
845 prepare_alt_odb(the_repository);
846 for (odb = the_repository->objects->odb; odb; odb = odb->next)
847 fsck_object_dir(odb->path);
849 if (check_full) {
850 struct packed_git *p;
851 uint32_t total = 0, count = 0;
852 struct progress *progress = NULL;
854 if (show_progress) {
855 for (p = get_all_packs(the_repository); p;
856 p = p->next) {
857 if (open_pack_index(p))
858 continue;
859 total += p->num_objects;
862 progress = start_progress(_("Checking objects"), total);
864 for (p = get_all_packs(the_repository); p;
865 p = p->next) {
866 /* verify gives error messages itself */
867 if (verify_pack(the_repository,
868 p, fsck_obj_buffer,
869 progress, count))
870 errors_found |= ERROR_PACK;
871 count += p->num_objects;
873 stop_progress(&progress);
876 if (fsck_finish(&fsck_obj_options))
877 errors_found |= ERROR_OBJECT;
880 for (i = 0; i < argc; i++) {
881 const char *arg = argv[i];
882 struct object_id oid;
883 if (!get_oid(arg, &oid)) {
884 struct object *obj = lookup_object(the_repository,
885 &oid);
887 if (!obj || !(obj->flags & HAS_OBJ)) {
888 if (is_promisor_object(&oid))
889 continue;
890 error(_("%s: object missing"), oid_to_hex(&oid));
891 errors_found |= ERROR_OBJECT;
892 continue;
895 obj->flags |= USED;
896 fsck_put_object_name(&fsck_walk_options, &oid,
897 "%s", arg);
898 mark_object_reachable(obj);
899 continue;
901 error(_("invalid parameter: expected sha1, got '%s'"), arg);
902 errors_found |= ERROR_OBJECT;
906 * If we've not been given any explicit head information, do the
907 * default ones from .git/refs. We also consider the index file
908 * in this case (ie this implies --cache).
910 if (!argc) {
911 get_default_heads();
912 keep_cache_objects = 1;
915 if (keep_cache_objects) {
916 verify_index_checksum = 1;
917 verify_ce_order = 1;
918 read_cache();
919 /* TODO: audit for interaction with sparse-index. */
920 ensure_full_index(&the_index);
921 for (i = 0; i < active_nr; i++) {
922 unsigned int mode;
923 struct blob *blob;
924 struct object *obj;
926 mode = active_cache[i]->ce_mode;
927 if (S_ISGITLINK(mode))
928 continue;
929 blob = lookup_blob(the_repository,
930 &active_cache[i]->oid);
931 if (!blob)
932 continue;
933 obj = &blob->object;
934 obj->flags |= USED;
935 fsck_put_object_name(&fsck_walk_options, &obj->oid,
936 ":%s", active_cache[i]->name);
937 mark_object_reachable(obj);
939 if (active_cache_tree)
940 fsck_cache_tree(active_cache_tree);
943 check_connectivity();
945 if (the_repository->settings.core_commit_graph) {
946 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
948 prepare_alt_odb(the_repository);
949 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
950 child_process_init(&commit_graph_verify);
951 commit_graph_verify.git_cmd = 1;
952 strvec_pushl(&commit_graph_verify.args, "commit-graph",
953 "verify", "--object-dir", odb->path, NULL);
954 if (run_command(&commit_graph_verify))
955 errors_found |= ERROR_COMMIT_GRAPH;
959 if (the_repository->settings.core_multi_pack_index) {
960 struct child_process midx_verify = CHILD_PROCESS_INIT;
962 prepare_alt_odb(the_repository);
963 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
964 child_process_init(&midx_verify);
965 midx_verify.git_cmd = 1;
966 strvec_pushl(&midx_verify.args, "multi-pack-index",
967 "verify", "--object-dir", odb->path, NULL);
968 if (run_command(&midx_verify))
969 errors_found |= ERROR_MULTI_PACK_INDEX;
973 return errors_found;