1#include "builtin.h"
2#include "cache.h"
3#include "repository.h"
4#include "config.h"
5#include "commit.h"
6#include "tree.h"
7#include "blob.h"
8#include "tag.h"
9#include "refs.h"
10#include "pack.h"
11#include "cache-tree.h"
12#include "tree-walk.h"
13#include "fsck.h"
14#include "parse-options.h"
15#include "dir.h"
16#include "progress.h"
17#include "streaming.h"
18#include "decorate.h"
19#include "packfile.h"
20#include "object-store.h"
21#include "run-command.h"
22#include "worktree.h"
23
24#define REACHABLE 0x0001
25#define SEEN 0x0002
26#define HAS_OBJ 0x0004
27/* This flag is set if something points to this object. */
28#define USED 0x0008
29
30static int show_root;
31static int show_tags;
32static int show_unreachable;
33static int include_reflogs = 1;
34static int check_full = 1;
35static int connectivity_only;
36static int check_strict;
37static int keep_cache_objects;
38static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
39static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
40static int errors_found;
41static int write_lost_and_found;
42static int verbose;
43static int show_progress = -1;
44static int show_dangling = 1;
45static int name_objects;
46#define ERROR_OBJECT 01
47#define ERROR_REACHABLE 02
48#define ERROR_PACK 04
49#define ERROR_REFS 010
50#define ERROR_COMMIT_GRAPH 020
51
52static const char *describe_object(struct object *obj)
53{
54 static struct strbuf buf = STRBUF_INIT;
55 char *name = name_objects ?
56 lookup_decoration(fsck_walk_options.object_names, obj) : NULL;
57
58 strbuf_reset(&buf);
59 strbuf_addstr(&buf, oid_to_hex(&obj->oid));
60 if (name)
61 strbuf_addf(&buf, " (%s)", name);
62
63 return buf.buf;
64}
65
66static const char *printable_type(struct object *obj)
67{
68 const char *ret;
69
70 if (obj->type == OBJ_NONE) {
71 enum object_type type = oid_object_info(the_repository,
72 &obj->oid, NULL);
73 if (type > 0)
74 object_as_type(the_repository, obj, type, 0);
75 }
76
77 ret = type_name(obj->type);
78 if (!ret)
79 ret = "unknown";
80
81 return ret;
82}
83
84static int fsck_config(const char *var, const char *value, void *cb)
85{
86 if (strcmp(var, "fsck.skiplist") == 0) {
87 const char *path;
88 struct strbuf sb = STRBUF_INIT;
89
90 if (git_config_pathname(&path, var, value))
91 return 1;
92 strbuf_addf(&sb, "skiplist=%s", path);
93 free((char *)path);
94 fsck_set_msg_types(&fsck_obj_options, sb.buf);
95 strbuf_release(&sb);
96 return 0;
97 }
98
99 if (skip_prefix(var, "fsck.", &var)) {
100 fsck_set_msg_type(&fsck_obj_options, var, value);
101 return 0;
102 }
103
104 return git_default_config(var, value, cb);
105}
106
107static void objreport(struct object *obj, const char *msg_type,
108 const char *err)
109{
110 fprintf(stderr, "%s in %s %s: %s\n",
111 msg_type, printable_type(obj), describe_object(obj), err);
112}
113
114static int objerror(struct object *obj, const char *err)
115{
116 errors_found |= ERROR_OBJECT;
117 objreport(obj, "error", err);
118 return -1;
119}
120
121static int fsck_error_func(struct fsck_options *o,
122 struct object *obj, int type, const char *message)
123{
124 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", message);
125 return (type == FSCK_WARN) ? 0 : 1;
126}
127
128static struct object_array pending;
129
130static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
131{
132 struct object *parent = data;
133
134 /*
135 * The only case data is NULL or type is OBJ_ANY is when
136 * mark_object_reachable() calls us. All the callers of
137 * that function has non-NULL obj hence ...
138 */
139 if (!obj) {
140 /* ... these references to parent->fld are safe here */
141 printf("broken link from %7s %s\n",
142 printable_type(parent), describe_object(parent));
143 printf("broken link from %7s %s\n",
144 (type == OBJ_ANY ? "unknown" : type_name(type)), "unknown");
145 errors_found |= ERROR_REACHABLE;
146 return 1;
147 }
148
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");
152
153 if (obj->flags & REACHABLE)
154 return 0;
155 obj->flags |= REACHABLE;
156
157 if (is_promisor_object(&obj->oid))
158 /*
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").
162 */
163 return 0;
164
165 if (!(obj->flags & HAS_OBJ)) {
166 if (parent && !has_object_file(&obj->oid)) {
167 printf("broken link from %7s %s\n",
168 printable_type(parent), describe_object(parent));
169 printf(" to %7s %s\n",
170 printable_type(obj), describe_object(obj));
171 errors_found |= ERROR_REACHABLE;
172 }
173 return 1;
174 }
175
176 add_object_array(obj, NULL, &pending);
177 return 0;
178}
179
180static void mark_object_reachable(struct object *obj)
181{
182 mark_object(obj, OBJ_ANY, NULL, NULL);
183}
184
185static int traverse_one_object(struct object *obj)
186{
187 int result = fsck_walk(obj, obj, &fsck_walk_options);
188
189 if (obj->type == OBJ_TREE) {
190 struct tree *tree = (struct tree *)obj;
191 free_tree_buffer(tree);
192 }
193 return result;
194}
195
196static int traverse_reachable(void)
197{
198 struct progress *progress = NULL;
199 unsigned int nr = 0;
200 int result = 0;
201 if (show_progress)
202 progress = start_delayed_progress(_("Checking connectivity"), 0);
203 while (pending.nr) {
204 result |= traverse_one_object(object_array_pop(&pending));
205 display_progress(progress, ++nr);
206 }
207 stop_progress(&progress);
208 return !!result;
209}
210
211static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
212{
213 if (!obj)
214 return 1;
215 obj->flags |= USED;
216 return 0;
217}
218
219/*
220 * Check a single reachable object
221 */
222static void check_reachable_object(struct object *obj)
223{
224 /*
225 * We obviously want the object to be parsed,
226 * except if it was in a pack-file and we didn't
227 * do a full fsck
228 */
229 if (!(obj->flags & HAS_OBJ)) {
230 if (is_promisor_object(&obj->oid))
231 return;
232 if (has_object_pack(&obj->oid))
233 return; /* it is in pack - forget about it */
234 printf("missing %s %s\n", printable_type(obj),
235 describe_object(obj));
236 errors_found |= ERROR_REACHABLE;
237 return;
238 }
239}
240
241/*
242 * Check a single unreachable object
243 */
244static void check_unreachable_object(struct object *obj)
245{
246 /*
247 * Missing unreachable object? Ignore it. It's not like
248 * we miss it (since it can't be reached), nor do we want
249 * to complain about it being unreachable (since it does
250 * not exist).
251 */
252 if (!(obj->flags & HAS_OBJ))
253 return;
254
255 /*
256 * Unreachable object that exists? Show it if asked to,
257 * since this is something that is prunable.
258 */
259 if (show_unreachable) {
260 printf("unreachable %s %s\n", printable_type(obj),
261 describe_object(obj));
262 return;
263 }
264
265 /*
266 * "!USED" means that nothing at all points to it, including
267 * other unreachable objects. In other words, it's the "tip"
268 * of some set of unreachable objects, usually a commit that
269 * got dropped.
270 *
271 * Such starting points are more interesting than some random
272 * set of unreachable objects, so we show them even if the user
273 * hasn't asked for _all_ unreachable objects. If you have
274 * deleted a branch by mistake, this is a prime candidate to
275 * start looking at, for example.
276 */
277 if (!(obj->flags & USED)) {
278 if (show_dangling)
279 printf("dangling %s %s\n", printable_type(obj),
280 describe_object(obj));
281 if (write_lost_and_found) {
282 char *filename = git_pathdup("lost-found/%s/%s",
283 obj->type == OBJ_COMMIT ? "commit" : "other",
284 describe_object(obj));
285 FILE *f;
286
287 if (safe_create_leading_directories_const(filename)) {
288 error("Could not create lost-found");
289 free(filename);
290 return;
291 }
292 f = xfopen(filename, "w");
293 if (obj->type == OBJ_BLOB) {
294 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
295 die_errno("Could not write '%s'", filename);
296 } else
297 fprintf(f, "%s\n", describe_object(obj));
298 if (fclose(f))
299 die_errno("Could not finish '%s'",
300 filename);
301 free(filename);
302 }
303 return;
304 }
305
306 /*
307 * Otherwise? It's there, it's unreachable, and some other unreachable
308 * object points to it. Ignore it - it's not interesting, and we showed
309 * all the interesting cases above.
310 */
311}
312
313static void check_object(struct object *obj)
314{
315 if (verbose)
316 fprintf(stderr, "Checking %s\n", describe_object(obj));
317
318 if (obj->flags & REACHABLE)
319 check_reachable_object(obj);
320 else
321 check_unreachable_object(obj);
322}
323
324static void check_connectivity(void)
325{
326 int i, max;
327
328 /* Traverse the pending reachable objects */
329 traverse_reachable();
330
331 /* Look up all the requirements, warn about missing objects.. */
332 max = get_max_object_index();
333 if (verbose)
334 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
335
336 for (i = 0; i < max; i++) {
337 struct object *obj = get_indexed_object(i);
338
339 if (obj)
340 check_object(obj);
341 }
342}
343
344static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
345{
346 int err;
347
348 if (obj->flags & SEEN)
349 return 0;
350 obj->flags |= SEEN;
351
352 if (verbose)
353 fprintf(stderr, "Checking %s %s\n",
354 printable_type(obj), describe_object(obj));
355
356 if (fsck_walk(obj, NULL, &fsck_obj_options))
357 objerror(obj, "broken links");
358 err = fsck_object(obj, buffer, size, &fsck_obj_options);
359 if (err)
360 goto out;
361
362 if (obj->type == OBJ_COMMIT) {
363 struct commit *commit = (struct commit *) obj;
364
365 if (!commit->parents && show_root)
366 printf("root %s\n", describe_object(&commit->object));
367 }
368
369 if (obj->type == OBJ_TAG) {
370 struct tag *tag = (struct tag *) obj;
371
372 if (show_tags && tag->tagged) {
373 printf("tagged %s %s", printable_type(tag->tagged),
374 describe_object(tag->tagged));
375 printf(" (%s) in %s\n", tag->tag,
376 describe_object(&tag->object));
377 }
378 }
379
380out:
381 if (obj->type == OBJ_TREE)
382 free_tree_buffer((struct tree *)obj);
383 if (obj->type == OBJ_COMMIT)
384 free_commit_buffer((struct commit *)obj);
385 return err;
386}
387
388static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
389 unsigned long size, void *buffer, int *eaten)
390{
391 /*
392 * Note, buffer may be NULL if type is OBJ_BLOB. See
393 * verify_packfile(), data_valid variable for details.
394 */
395 struct object *obj;
396 obj = parse_object_buffer(the_repository, oid, type, size, buffer,
397 eaten);
398 if (!obj) {
399 errors_found |= ERROR_OBJECT;
400 return error("%s: object corrupt or missing", oid_to_hex(oid));
401 }
402 obj->flags &= ~(REACHABLE | SEEN);
403 obj->flags |= HAS_OBJ;
404 return fsck_obj(obj, buffer, size);
405}
406
407static int default_refs;
408
409static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
410 timestamp_t timestamp)
411{
412 struct object *obj;
413
414 if (!is_null_oid(oid)) {
415 obj = lookup_object(the_repository, oid->hash);
416 if (obj && (obj->flags & HAS_OBJ)) {
417 if (timestamp && name_objects)
418 add_decoration(fsck_walk_options.object_names,
419 obj,
420 xstrfmt("%s@{%"PRItime"}", refname, timestamp));
421 obj->flags |= USED;
422 mark_object_reachable(obj);
423 } else if (!is_promisor_object(oid)) {
424 error("%s: invalid reflog entry %s", refname, oid_to_hex(oid));
425 errors_found |= ERROR_REACHABLE;
426 }
427 }
428}
429
430static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
431 const char *email, timestamp_t timestamp, int tz,
432 const char *message, void *cb_data)
433{
434 const char *refname = cb_data;
435
436 if (verbose)
437 fprintf(stderr, "Checking reflog %s->%s\n",
438 oid_to_hex(ooid), oid_to_hex(noid));
439
440 fsck_handle_reflog_oid(refname, ooid, 0);
441 fsck_handle_reflog_oid(refname, noid, timestamp);
442 return 0;
443}
444
445static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
446 int flag, void *cb_data)
447{
448 struct strbuf refname = STRBUF_INIT;
449
450 strbuf_worktree_ref(cb_data, &refname, logname);
451 for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
452 strbuf_release(&refname);
453 return 0;
454}
455
456static int fsck_handle_ref(const char *refname, const struct object_id *oid,
457 int flag, void *cb_data)
458{
459 struct object *obj;
460
461 obj = parse_object(the_repository, oid);
462 if (!obj) {
463 if (is_promisor_object(oid)) {
464 /*
465 * Increment default_refs anyway, because this is a
466 * valid ref.
467 */
468 default_refs++;
469 return 0;
470 }
471 error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
472 errors_found |= ERROR_REACHABLE;
473 /* We'll continue with the rest despite the error.. */
474 return 0;
475 }
476 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
477 error("%s: not a commit", refname);
478 errors_found |= ERROR_REFS;
479 }
480 default_refs++;
481 obj->flags |= USED;
482 if (name_objects)
483 add_decoration(fsck_walk_options.object_names,
484 obj, xstrdup(refname));
485 mark_object_reachable(obj);
486
487 return 0;
488}
489
490static int fsck_head_link(const char *head_ref_name,
491 const char **head_points_at,
492 struct object_id *head_oid);
493
494static void get_default_heads(void)
495{
496 struct worktree **worktrees, **p;
497 const char *head_points_at;
498 struct object_id head_oid;
499
500 for_each_rawref(fsck_handle_ref, NULL);
501
502 worktrees = get_worktrees(0);
503 for (p = worktrees; *p; p++) {
504 struct worktree *wt = *p;
505 struct strbuf ref = STRBUF_INIT;
506
507 strbuf_worktree_ref(wt, &ref, "HEAD");
508 fsck_head_link(ref.buf, &head_points_at, &head_oid);
509 if (head_points_at && !is_null_oid(&head_oid))
510 fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
511 strbuf_release(&ref);
512
513 if (include_reflogs)
514 refs_for_each_reflog(get_worktree_ref_store(wt),
515 fsck_handle_reflog, wt);
516 }
517 free_worktrees(worktrees);
518
519 /*
520 * Not having any default heads isn't really fatal, but
521 * it does mean that "--unreachable" no longer makes any
522 * sense (since in this case everything will obviously
523 * be unreachable by definition.
524 *
525 * Showing dangling objects is valid, though (as those
526 * dangling objects are likely lost heads).
527 *
528 * So we just print a warning about it, and clear the
529 * "show_unreachable" flag.
530 */
531 if (!default_refs) {
532 fprintf(stderr, "notice: No default references\n");
533 show_unreachable = 0;
534 }
535}
536
537static int fsck_loose(const struct object_id *oid, const char *path, void *data)
538{
539 struct object *obj;
540 enum object_type type;
541 unsigned long size;
542 void *contents;
543 int eaten;
544
545 if (read_loose_object(path, oid, &type, &size, &contents) < 0) {
546 errors_found |= ERROR_OBJECT;
547 error("%s: object corrupt or missing: %s",
548 oid_to_hex(oid), path);
549 return 0; /* keep checking other objects */
550 }
551
552 if (!contents && type != OBJ_BLOB)
553 BUG("read_loose_object streamed a non-blob");
554
555 obj = parse_object_buffer(the_repository, oid, type, size,
556 contents, &eaten);
557
558 if (!obj) {
559 errors_found |= ERROR_OBJECT;
560 error("%s: object could not be parsed: %s",
561 oid_to_hex(oid), path);
562 if (!eaten)
563 free(contents);
564 return 0; /* keep checking other objects */
565 }
566
567 obj->flags &= ~(REACHABLE | SEEN);
568 obj->flags |= HAS_OBJ;
569 if (fsck_obj(obj, contents, size))
570 errors_found |= ERROR_OBJECT;
571
572 if (!eaten)
573 free(contents);
574 return 0; /* keep checking other objects, even if we saw an error */
575}
576
577static int fsck_cruft(const char *basename, const char *path, void *data)
578{
579 if (!starts_with(basename, "tmp_obj_"))
580 fprintf(stderr, "bad sha1 file: %s\n", path);
581 return 0;
582}
583
584static int fsck_subdir(unsigned int nr, const char *path, void *progress)
585{
586 display_progress(progress, nr + 1);
587 return 0;
588}
589
590static void fsck_object_dir(const char *path)
591{
592 struct progress *progress = NULL;
593
594 if (verbose)
595 fprintf(stderr, "Checking object directory\n");
596
597 if (show_progress)
598 progress = start_progress(_("Checking object directories"), 256);
599
600 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
601 progress);
602 display_progress(progress, 256);
603 stop_progress(&progress);
604}
605
606static int fsck_head_link(const char *head_ref_name,
607 const char **head_points_at,
608 struct object_id *head_oid)
609{
610 int null_is_error = 0;
611
612 if (verbose)
613 fprintf(stderr, "Checking %s link\n", head_ref_name);
614
615 *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
616 if (!*head_points_at) {
617 errors_found |= ERROR_REFS;
618 return error("Invalid %s", head_ref_name);
619 }
620 if (!strcmp(*head_points_at, head_ref_name))
621 /* detached HEAD */
622 null_is_error = 1;
623 else if (!starts_with(*head_points_at, "refs/heads/")) {
624 errors_found |= ERROR_REFS;
625 return error("%s points to something strange (%s)",
626 head_ref_name, *head_points_at);
627 }
628 if (is_null_oid(head_oid)) {
629 if (null_is_error) {
630 errors_found |= ERROR_REFS;
631 return error("%s: detached HEAD points at nothing",
632 head_ref_name);
633 }
634 fprintf(stderr, "notice: %s points to an unborn branch (%s)\n",
635 head_ref_name, *head_points_at + 11);
636 }
637 return 0;
638}
639
640static int fsck_cache_tree(struct cache_tree *it)
641{
642 int i;
643 int err = 0;
644
645 if (verbose)
646 fprintf(stderr, "Checking cache tree\n");
647
648 if (0 <= it->entry_count) {
649 struct object *obj = parse_object(the_repository, &it->oid);
650 if (!obj) {
651 error("%s: invalid sha1 pointer in cache-tree",
652 oid_to_hex(&it->oid));
653 errors_found |= ERROR_REFS;
654 return 1;
655 }
656 obj->flags |= USED;
657 if (name_objects)
658 add_decoration(fsck_walk_options.object_names,
659 obj, xstrdup(":"));
660 mark_object_reachable(obj);
661 if (obj->type != OBJ_TREE)
662 err |= objerror(obj, "non-tree in cache-tree");
663 }
664 for (i = 0; i < it->subtree_nr; i++)
665 err |= fsck_cache_tree(it->down[i]->cache_tree);
666 return err;
667}
668
669static void mark_object_for_connectivity(const struct object_id *oid)
670{
671 struct object *obj = lookup_unknown_object(oid->hash);
672 obj->flags |= HAS_OBJ;
673}
674
675static int mark_loose_for_connectivity(const struct object_id *oid,
676 const char *path,
677 void *data)
678{
679 mark_object_for_connectivity(oid);
680 return 0;
681}
682
683static int mark_packed_for_connectivity(const struct object_id *oid,
684 struct packed_git *pack,
685 uint32_t pos,
686 void *data)
687{
688 mark_object_for_connectivity(oid);
689 return 0;
690}
691
692static char const * const fsck_usage[] = {
693 N_("git fsck [<options>] [<object>...]"),
694 NULL
695};
696
697static struct option fsck_opts[] = {
698 OPT__VERBOSE(&verbose, N_("be verbose")),
699 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
700 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
701 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
702 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
703 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
704 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
705 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
706 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
707 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
708 OPT_BOOL(0, "lost-found", &write_lost_and_found,
709 N_("write dangling objects in .git/lost-found")),
710 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
711 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
712 OPT_END(),
713};
714
715int cmd_fsck(int argc, const char **argv, const char *prefix)
716{
717 int i;
718 struct alternate_object_database *alt;
719
720 /* fsck knows how to handle missing promisor objects */
721 fetch_if_missing = 0;
722
723 errors_found = 0;
724 read_replace_refs = 0;
725
726 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
727
728 fsck_walk_options.walk = mark_object;
729 fsck_obj_options.walk = mark_used;
730 fsck_obj_options.error_func = fsck_error_func;
731 if (check_strict)
732 fsck_obj_options.strict = 1;
733
734 if (show_progress == -1)
735 show_progress = isatty(2);
736 if (verbose)
737 show_progress = 0;
738
739 if (write_lost_and_found) {
740 check_full = 1;
741 include_reflogs = 0;
742 }
743
744 if (name_objects)
745 fsck_walk_options.object_names =
746 xcalloc(1, sizeof(struct decoration));
747
748 git_config(fsck_config, NULL);
749
750 if (connectivity_only) {
751 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
752 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
753 } else {
754 struct alternate_object_database *alt_odb_list;
755
756 fsck_object_dir(get_object_directory());
757
758 prepare_alt_odb(the_repository);
759 alt_odb_list = the_repository->objects->alt_odb_list;
760 for (alt = alt_odb_list; alt; alt = alt->next)
761 fsck_object_dir(alt->path);
762
763 if (check_full) {
764 struct packed_git *p;
765 uint32_t total = 0, count = 0;
766 struct progress *progress = NULL;
767
768 if (show_progress) {
769 for (p = get_all_packs(the_repository); p;
770 p = p->next) {
771 if (open_pack_index(p))
772 continue;
773 total += p->num_objects;
774 }
775
776 progress = start_progress(_("Checking objects"), total);
777 }
778 for (p = get_all_packs(the_repository); p;
779 p = p->next) {
780 /* verify gives error messages itself */
781 if (verify_pack(p, fsck_obj_buffer,
782 progress, count))
783 errors_found |= ERROR_PACK;
784 count += p->num_objects;
785 }
786 stop_progress(&progress);
787 }
788
789 if (fsck_finish(&fsck_obj_options))
790 errors_found |= ERROR_OBJECT;
791 }
792
793 for (i = 0; i < argc; i++) {
794 const char *arg = argv[i];
795 struct object_id oid;
796 if (!get_oid(arg, &oid)) {
797 struct object *obj = lookup_object(the_repository,
798 oid.hash);
799
800 if (!obj || !(obj->flags & HAS_OBJ)) {
801 if (is_promisor_object(&oid))
802 continue;
803 error("%s: object missing", oid_to_hex(&oid));
804 errors_found |= ERROR_OBJECT;
805 continue;
806 }
807
808 obj->flags |= USED;
809 if (name_objects)
810 add_decoration(fsck_walk_options.object_names,
811 obj, xstrdup(arg));
812 mark_object_reachable(obj);
813 continue;
814 }
815 error("invalid parameter: expected sha1, got '%s'", arg);
816 errors_found |= ERROR_OBJECT;
817 }
818
819 /*
820 * If we've not been given any explicit head information, do the
821 * default ones from .git/refs. We also consider the index file
822 * in this case (ie this implies --cache).
823 */
824 if (!argc) {
825 get_default_heads();
826 keep_cache_objects = 1;
827 }
828
829 if (keep_cache_objects) {
830 verify_index_checksum = 1;
831 verify_ce_order = 1;
832 read_cache();
833 for (i = 0; i < active_nr; i++) {
834 unsigned int mode;
835 struct blob *blob;
836 struct object *obj;
837
838 mode = active_cache[i]->ce_mode;
839 if (S_ISGITLINK(mode))
840 continue;
841 blob = lookup_blob(the_repository,
842 &active_cache[i]->oid);
843 if (!blob)
844 continue;
845 obj = &blob->object;
846 obj->flags |= USED;
847 if (name_objects)
848 add_decoration(fsck_walk_options.object_names,
849 obj,
850 xstrfmt(":%s", active_cache[i]->name));
851 mark_object_reachable(obj);
852 }
853 if (active_cache_tree)
854 fsck_cache_tree(active_cache_tree);
855 }
856
857 check_connectivity();
858
859 if (!git_config_get_bool("core.commitgraph", &i) && i) {
860 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
861 const char *verify_argv[] = { "commit-graph", "verify", NULL, NULL, NULL };
862
863 commit_graph_verify.argv = verify_argv;
864 commit_graph_verify.git_cmd = 1;
865 if (run_command(&commit_graph_verify))
866 errors_found |= ERROR_COMMIT_GRAPH;
867
868 prepare_alt_odb(the_repository);
869 for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
870 verify_argv[2] = "--object-dir";
871 verify_argv[3] = alt->path;
872 if (run_command(&commit_graph_verify))
873 errors_found |= ERROR_COMMIT_GRAPH;
874 }
875 }
876
877 if (!git_config_get_bool("core.multipackindex", &i) && i) {
878 struct child_process midx_verify = CHILD_PROCESS_INIT;
879 const char *midx_argv[] = { "multi-pack-index", "verify", NULL, NULL, NULL };
880
881 midx_verify.argv = midx_argv;
882 midx_verify.git_cmd = 1;
883 if (run_command(&midx_verify))
884 errors_found |= ERROR_COMMIT_GRAPH;
885
886 prepare_alt_odb(the_repository);
887 for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
888 midx_argv[2] = "--object-dir";
889 midx_argv[3] = alt->path;
890 if (run_command(&midx_verify))
891 errors_found |= ERROR_COMMIT_GRAPH;
892 }
893 }
894
895 return errors_found;
896}