at commit 426d291b

1 #include "builtin.h"
2 #include "gettext.h"
3 #include "hex.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 "cache-tree.h"
11 #include "fsck.h"
12 #include "parse-options.h"
13 #include "progress.h"
14 #include "packfile.h"
15 #include "object-file.h"
16 #include "object-name.h"
17 #include "odb.h"
18 #include "odb/streaming.h"
19 #include "path.h"
20 #include "read-cache-ll.h"
21 #include "replace-object.h"
22 #include "resolve-undo.h"
23 #include "run-command.h"
24 #include "sparse-index.h"
25 #include "worktree.h"
26 #include "pack-bitmap.h"
27
28 #define REACHABLE 0x0001
29 #define SEEN 0x0002
30 #define HAS_OBJ 0x0004
31 /* This flag is set if something points to this object. */
32 #define USED 0x0008
33
34 static int show_root;
35 static int show_tags;
36 static int show_unreachable;
37 static int include_reflogs = 1;
38 static int connectivity_only;
39 static int check_strict;
40 static struct fsck_options fsck_walk_options;
41 static struct fsck_options fsck_obj_options;
42 static int errors_found;
43 static int write_lost_and_found;
44 static int verbose;
45 static int show_progress = -1;
46 static int show_dangling = 1;
47 static timestamp_t now;
48 #define ERROR_OBJECT 01
49 #define ERROR_REACHABLE 02
50 #define ERROR_REFS 010
51 #define ERROR_COMMIT_GRAPH 020
52 #define ERROR_MULTI_PACK_INDEX 040
53 #define ERROR_BITMAP 0200
54
55 static const char *describe_object(const struct object_id *oid)
56 {
57 return fsck_describe_object(&fsck_walk_options, oid);
58 }
59
60 static const char *printable_type(struct repository *repo,
61 const struct object_id *oid,
62 enum object_type type)
63 {
64 const char *ret;
65
66 if (type == OBJ_NONE)
67 type = odb_read_object_info(repo->objects, oid, NULL);
68
69 ret = type_name(type);
70 if (!ret)
71 ret = _("unknown");
72
73 return ret;
74 }
75
76 static int objerror(struct repository *repo, struct object *obj, const char *err)
77 {
78 errors_found |= ERROR_OBJECT;
79 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
80 fprintf_ln(stderr, _("error in %s %s: %s"),
81 printable_type(repo, &obj->oid, obj->type),
82 describe_object(&obj->oid), err);
83 return -1;
84 }
85
86 static int fsck_objects_error_func(struct fsck_options *o,
87 void *fsck_report,
88 enum fsck_msg_type msg_type,
89 enum fsck_msg_id msg_id UNUSED,
90 const char *message)
91 {
92 struct fsck_object_report *report = fsck_report;
93 const struct object_id *oid = report->oid;
94 enum object_type object_type = report->object_type;
95
96 switch (msg_type) {
97 case FSCK_WARN:
98 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
99 fprintf_ln(stderr, _("warning in %s %s: %s"),
100 printable_type(o->repo, oid, object_type),
101 describe_object(oid), message);
102 return 0;
103 case FSCK_ERROR:
104 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
105 fprintf_ln(stderr, _("error in %s %s: %s"),
106 printable_type(o->repo, oid, object_type),
107 describe_object(oid), message);
108 return 1;
109 default:
110 BUG("%d (FSCK_IGNORE?) should never trigger this callback",
111 msg_type);
112 }
113 }
114
115 static struct object_array pending;
116
117 static int mark_object(struct object *obj, enum object_type type,
118 void *data, struct fsck_options *options)
119 {
120 struct object *parent = data;
121
122 /*
123 * The only case data is NULL or type is OBJ_ANY is when
124 * mark_object_reachable() calls us. All the callers of
125 * that function has non-NULL obj hence ...
126 */
127 if (!obj) {
128 /* ... these references to parent->fld are safe here */
129 printf_ln(_("broken link from %7s %s"),
130 printable_type(options->repo, &parent->oid, parent->type),
131 describe_object(&parent->oid));
132 printf_ln(_("broken link from %7s %s"),
133 (type == OBJ_ANY ? _("unknown") : type_name(type)),
134 _("unknown"));
135 errors_found |= ERROR_REACHABLE;
136 return 1;
137 }
138
139 if (type != OBJ_ANY && obj->type != type)
140 /* ... and the reference to parent is safe here */
141 objerror(options->repo, parent, _("wrong object type in link"));
142
143 if (obj->flags & REACHABLE)
144 return 0;
145 obj->flags |= REACHABLE;
146
147 if (is_promisor_object(options->repo, &obj->oid))
148 /*
149 * Further recursion does not need to be performed on this
150 * object since it is a promisor object (so it does not need to
151 * be added to "pending").
152 */
153 return 0;
154
155 if (!(obj->flags & HAS_OBJ)) {
156 if (parent && !odb_has_object(options->repo->objects, &obj->oid,
157 ODB_HAS_OBJECT_RECHECK_PACKED)) {
158 printf_ln(_("broken link from %7s %s\n"
159 " to %7s %s"),
160 printable_type(options->repo, &parent->oid, parent->type),
161 describe_object(&parent->oid),
162 printable_type(options->repo, &obj->oid, obj->type),
163 describe_object(&obj->oid));
164 errors_found |= ERROR_REACHABLE;
165 }
166 return 1;
167 }
168
169 add_object_array(obj, NULL, &pending);
170 return 0;
171 }
172
173 static void mark_object_reachable(struct object *obj)
174 {
175 mark_object(obj, OBJ_ANY, NULL, &fsck_walk_options);
176 }
177
178 static int traverse_one_object(struct object *obj)
179 {
180 int result = fsck_walk(obj, obj, &fsck_walk_options);
181
182 if (obj->type == OBJ_TREE) {
183 struct tree *tree = (struct tree *)obj;
184 free_tree_buffer(tree);
185 }
186 return result;
187 }
188
189 static int traverse_reachable(struct repository *repo)
190 {
191 struct progress *progress = NULL;
192 unsigned int nr = 0;
193 int result = 0;
194 if (show_progress)
195 progress = start_delayed_progress(repo,
196 _("Checking connectivity"), 0);
197 while (pending.nr) {
198 result |= traverse_one_object(object_array_pop(&pending));
199 display_progress(progress, ++nr);
200 }
201 stop_progress(&progress);
202 return !!result;
203 }
204
205 static int mark_used(struct object *obj, enum object_type type UNUSED,
206 void *data UNUSED, struct fsck_options *options UNUSED)
207 {
208 if (!obj)
209 return 1;
210 obj->flags |= USED;
211 return 0;
212 }
213
214 static int mark_unreachable_referents(const struct object_id *oid,
215 struct object_info *oi UNUSED,
216 void *data)
217 {
218 struct repository *repo = data;
219 struct fsck_options options;
220 struct object *obj = lookup_object(data, oid);
221
222 if (!obj || !(obj->flags & HAS_OBJ))
223 return 0; /* not part of our original set */
224 if (obj->flags & REACHABLE)
225 return 0; /* reachable objects already traversed */
226
227 /*
228 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
229 * (and we want to avoid parsing blobs).
230 */
231 if (obj->type == OBJ_NONE) {
232 enum object_type type = odb_read_object_info(repo->objects,
233 &obj->oid, NULL);
234 if (type > 0)
235 object_as_type(obj, type, 0);
236 }
237
238 fsck_options_init(&options, repo, FSCK_OPTIONS_DEFAULT);
239 options.walk = mark_used;
240 fsck_walk(obj, NULL, &options);
241 if (obj->type == OBJ_TREE)
242 free_tree_buffer((struct tree *)obj);
243
244 return 0;
245 }
246
247 /*
248 * Check a single reachable object
249 */
250 static void check_reachable_object(struct repository *repo, struct object *obj)
251 {
252 /*
253 * We obviously want the object to be parsed,
254 * except if it was in a pack-file and we didn't
255 * do a full fsck
256 */
257 if (!(obj->flags & HAS_OBJ)) {
258 if (is_promisor_object(repo, &obj->oid))
259 return;
260 if (has_object_pack(repo, &obj->oid))
261 return; /* it is in pack - forget about it */
262 printf_ln(_("missing %s %s"),
263 printable_type(repo, &obj->oid, obj->type),
264 describe_object(&obj->oid));
265 errors_found |= ERROR_REACHABLE;
266 return;
267 }
268 }
269
270 /*
271 * Check a single unreachable object
272 */
273 static void check_unreachable_object(struct repository *repo, struct object *obj)
274 {
275 /*
276 * Missing unreachable object? Ignore it. It's not like
277 * we miss it (since it can't be reached), nor do we want
278 * to complain about it being unreachable (since it does
279 * not exist).
280 */
281 if (!(obj->flags & HAS_OBJ))
282 return;
283
284 /*
285 * Unreachable object that exists? Show it if asked to,
286 * since this is something that is prunable.
287 */
288 if (show_unreachable) {
289 printf_ln(_("unreachable %s %s"),
290 printable_type(repo, &obj->oid, obj->type),
291 describe_object(&obj->oid));
292 return;
293 }
294
295 /*
296 * "!USED" means that nothing at all points to it, including
297 * other unreachable objects. In other words, it's the "tip"
298 * of some set of unreachable objects, usually a commit that
299 * got dropped.
300 *
301 * Such starting points are more interesting than some random
302 * set of unreachable objects, so we show them even if the user
303 * hasn't asked for _all_ unreachable objects. If you have
304 * deleted a branch by mistake, this is a prime candidate to
305 * start looking at, for example.
306 */
307 if (!(obj->flags & USED)) {
308 if (show_dangling)
309 printf_ln(_("dangling %s %s"),
310 printable_type(repo, &obj->oid, obj->type),
311 describe_object(&obj->oid));
312 if (write_lost_and_found) {
313 char *filename = repo_git_path(repo, "lost-found/%s/%s",
314 obj->type == OBJ_COMMIT ? "commit" : "other",
315 describe_object(&obj->oid));
316 FILE *f;
317
318 if (safe_create_leading_directories_const(repo, filename)) {
319 error(_("could not create lost-found"));
320 free(filename);
321 return;
322 }
323 f = xfopen(filename, "w");
324 if (obj->type == OBJ_BLOB) {
325 if (odb_stream_blob_to_fd(repo->objects, fileno(f),
326 &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);
334 }
335 return;
336 }
337
338 /*
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.
342 */
343 }
344
345 static void check_object(struct repository *repo, struct object *obj)
346 {
347 if (verbose)
348 fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid));
349
350 if (obj->flags & REACHABLE)
351 check_reachable_object(repo, obj);
352 else
353 check_unreachable_object(repo, obj);
354 }
355
356 static void check_connectivity(struct repository *repo)
357 {
358 int i, max;
359
360 /* Traverse the pending reachable objects */
361 traverse_reachable(repo);
362
363 /*
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.
367 */
368 if (connectivity_only && (show_dangling || write_lost_and_found)) {
369 /*
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.
374 *
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.
378 */
379 odb_for_each_object(repo->objects, NULL,
380 mark_unreachable_referents, repo, 0);
381 }
382
383 /* Look up all the requirements, warn about missing objects.. */
384 max = get_max_object_index(repo);
385 if (verbose)
386 fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max);
387
388 for (i = 0; i < max; i++) {
389 struct object *obj = get_indexed_object(repo, i);
390
391 if (obj)
392 check_object(repo, obj);
393 }
394 }
395
396 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
397 unsigned long size, void *buffer, int *eaten, void *cb_data)
398 {
399 struct repository *repo = cb_data;
400 struct object *obj;
401 int err;
402
403 /*
404 * Note, buffer may be NULL if type is OBJ_BLOB. See
405 * verify_packfile(), data_valid variable for details.
406 */
407 obj = parse_object_buffer(repo, oid, type, size, buffer, eaten);
408 if (!obj) {
409 errors_found |= ERROR_OBJECT;
410 err = error(_("%s: object corrupt or missing"),
411 oid_to_hex(oid));
412 goto out;
413 }
414
415 obj->flags &= ~REACHABLE;
416 obj->flags |= HAS_OBJ | SEEN;
417
418 if (verbose)
419 fprintf_ln(stderr, _("Checking %s %s"),
420 printable_type(repo, &obj->oid, obj->type),
421 describe_object(&obj->oid));
422
423 if (fsck_walk(obj, NULL, &fsck_obj_options))
424 objerror(repo, obj, _("broken links"));
425
426 err = fsck_object(obj, buffer, size, &fsck_obj_options);
427 if (err)
428 goto out;
429
430 if (obj->type == OBJ_COMMIT) {
431 struct commit *commit = (struct commit *) obj;
432
433 if (!commit->parents && show_root)
434 printf_ln(_("root %s"),
435 describe_object(&commit->object.oid));
436 }
437
438 if (obj->type == OBJ_TAG) {
439 struct tag *tag = (struct tag *) obj;
440
441 if (show_tags && tag->tagged) {
442 printf_ln(_("tagged %s %s (%s) in %s"),
443 printable_type(repo, &tag->tagged->oid, tag->tagged->type),
444 describe_object(&tag->tagged->oid),
445 tag->tag,
446 describe_object(&tag->object.oid));
447 }
448 }
449
450 out:
451 if (obj && obj->type == OBJ_TREE)
452 free_tree_buffer((struct tree *)obj);
453 return err;
454 }
455
456 static int default_refs;
457
458 static void fsck_handle_reflog_oid(struct repository *repo,
459 const char *refname, struct object_id *oid,
460 timestamp_t timestamp)
461 {
462 struct object *obj;
463
464 if (!is_null_oid(oid)) {
465 obj = lookup_object(repo, oid);
466 if (obj && (obj->flags & HAS_OBJ)) {
467 if (timestamp)
468 fsck_put_object_name(&fsck_walk_options, oid,
469 "%s@{%"PRItime"}",
470 refname, timestamp);
471 obj->flags |= USED;
472 mark_object_reachable(obj);
473 } else if (!is_promisor_object(repo, oid)) {
474 error(_("%s: invalid reflog entry %s"),
475 refname, oid_to_hex(oid));
476 errors_found |= ERROR_REACHABLE;
477 }
478 }
479 }
480
481 static int fsck_handle_reflog_ent(const char *refname,
482 struct object_id *ooid, struct object_id *noid,
483 const char *email UNUSED,
484 timestamp_t timestamp, int tz UNUSED,
485 const char *message UNUSED, void *cb_data)
486 {
487 struct repository *repo = cb_data;
488
489 if (now && timestamp > now)
490 return 0;
491
492 if (verbose)
493 fprintf_ln(stderr, _("Checking reflog %s->%s"),
494 oid_to_hex(ooid), oid_to_hex(noid));
495
496 fsck_handle_reflog_oid(repo, refname, ooid, 0);
497 fsck_handle_reflog_oid(repo, refname, noid, timestamp);
498 return 0;
499 }
500
501 static int fsck_handle_reflog(const char *logname, void *cb_data)
502 {
503 struct strbuf refname = STRBUF_INIT;
504 struct worktree *wt = cb_data;
505
506 strbuf_worktree_ref(wt, &refname, logname);
507 refs_for_each_reflog_ent(get_main_ref_store(wt->repo),
508 refname.buf, fsck_handle_reflog_ent,
509 wt->repo);
510 strbuf_release(&refname);
511 return 0;
512 }
513
514 struct ref_snapshot {
515 char *refname;
516 struct object_id oid;
517 /* TODO: Maybe supplement with latest reflog entry info too? */
518 };
519
520 struct snapshot {
521 size_t nr;
522 size_t alloc;
523 struct ref_snapshot *ref;
524 /* TODO: Consider also snapshotting the index of each worktree. */
525 };
526
527 struct snapshot_ref_data {
528 struct repository *repo;
529 struct snapshot *snap;
530 };
531
532 static int snapshot_ref(const struct reference *ref, void *cb_data)
533 {
534 struct snapshot_ref_data *data = cb_data;
535 struct snapshot *snap = data->snap;
536 struct object *obj;
537
538 obj = parse_object(data->repo, ref->oid);
539 if (!obj) {
540 if (is_promisor_object(data->repo, ref->oid)) {
541 /*
542 * Increment default_refs anyway, because this is a
543 * valid ref.
544 */
545 default_refs++;
546 return 0;
547 }
548 error(_("%s: invalid sha1 pointer %s"),
549 ref->name, oid_to_hex(ref->oid));
550 errors_found |= ERROR_REACHABLE;
551 /* We'll continue with the rest despite the error.. */
552 return 0;
553 }
554 if (obj->type != OBJ_COMMIT && is_branch(ref->name)) {
555 error(_("%s: not a commit"), ref->name);
556 errors_found |= ERROR_REFS;
557 }
558 default_refs++;
559
560 ALLOC_GROW(snap->ref, snap->nr + 1, snap->alloc);
561 snap->ref[snap->nr].refname = xstrdup(ref->name);
562 oidcpy(&snap->ref[snap->nr].oid, ref->oid);
563 snap->nr++;
564
565 return 0;
566 }
567
568 static int fsck_handle_ref(const struct reference *ref, void *cb_data)
569 {
570 struct repository *repo = cb_data;
571 struct object *obj;
572
573 obj = parse_object(repo, ref->oid);
574 obj->flags |= USED;
575 fsck_put_object_name(&fsck_walk_options,
576 ref->oid, "%s", ref->name);
577 mark_object_reachable(obj);
578
579 return 0;
580 }
581
582 static void snapshot_refs(struct repository *repo,
583 struct snapshot *snap, int argc, const char **argv)
584 {
585 struct refs_for_each_ref_options opts = {
586 .flags = REFS_FOR_EACH_INCLUDE_BROKEN,
587 };
588 struct snapshot_ref_data data = {
589 .repo = repo,
590 .snap = snap,
591 };
592 struct worktree **worktrees, **p;
593 const char *head_points_at;
594 struct object_id head_oid;
595
596 for (int i = 0; i < argc; i++) {
597 const char *arg = argv[i];
598 struct object_id oid;
599 if (!repo_get_oid(repo, arg, &oid)) {
600 struct reference ref = {
601 .name = arg,
602 .oid = &oid,
603 };
604
605 snapshot_ref(&ref, &data);
606 continue;
607 }
608 error(_("invalid parameter: expected sha1, got '%s'"), arg);
609 errors_found |= ERROR_OBJECT;
610 }
611
612 if (argc) {
613 include_reflogs = 0;
614 return;
615 }
616
617 refs_for_each_ref_ext(get_main_ref_store(repo),
618 snapshot_ref, &data, &opts);
619
620 worktrees = get_worktrees(repo);
621 for (p = worktrees; *p; p++) {
622 struct worktree *wt = *p;
623 struct strbuf refname = STRBUF_INIT;
624
625 strbuf_worktree_ref(wt, &refname, "HEAD");
626
627 head_points_at = refs_resolve_ref_unsafe(get_main_ref_store(repo),
628 refname.buf, 0, &head_oid, NULL);
629
630 if (head_points_at && !is_null_oid(&head_oid)) {
631 struct reference ref = {
632 .name = refname.buf,
633 .oid = &head_oid,
634 };
635
636 snapshot_ref(&ref, &data);
637 }
638 strbuf_release(&refname);
639
640 /*
641 * TODO: Could use refs_for_each_reflog(...) to find
642 * latest entry instead of using a global 'now' for that
643 * purpose.
644 */
645 }
646 free_worktrees(worktrees);
647
648 /* Ignore reflogs newer than now */
649 now = time(NULL);
650 }
651
652
653 static void free_snapshot_refs(struct snapshot *snap)
654 {
655 for (size_t i = 0; i < snap->nr; i++)
656 free(snap->ref[i].refname);
657 free(snap->ref);
658 }
659
660 static void process_refs(struct repository *repo, struct snapshot *snap)
661 {
662 struct worktree **worktrees, **p;
663
664 for (size_t i = 0; i < snap->nr; i++) {
665 struct reference ref = {
666 .name = snap->ref[i].refname,
667 .oid = &snap->ref[i].oid,
668 };
669 fsck_handle_ref(&ref, repo);
670 }
671
672 if (include_reflogs) {
673 worktrees = get_worktrees(repo);
674 for (p = worktrees; *p; p++) {
675 struct worktree *wt = *p;
676
677 refs_for_each_reflog(get_worktree_ref_store(wt),
678 fsck_handle_reflog, wt);
679 }
680 free_worktrees(worktrees);
681 }
682
683 /*
684 * Not having any default heads isn't really fatal, but
685 * it does mean that "--unreachable" no longer makes any
686 * sense (since in this case everything will obviously
687 * be unreachable by definition.
688 *
689 * Showing dangling objects is valid, though (as those
690 * dangling objects are likely lost heads).
691 *
692 * So we just print a warning about it, and clear the
693 * "show_unreachable" flag.
694 */
695 if (!default_refs) {
696 fprintf_ln(stderr, _("notice: No default references"));
697 show_unreachable = 0;
698 }
699 }
700
701 struct for_each_loose_cb {
702 struct repository *repo;
703 struct progress *progress;
704 };
705
706 static int fsck_loose(const struct object_id *oid, const char *path,
707 void *cb_data)
708 {
709 struct for_each_loose_cb *data = cb_data;
710 enum object_type type = OBJ_NONE;
711 size_t size;
712 void *contents = NULL;
713 int eaten;
714 struct object_info oi = OBJECT_INFO_INIT;
715 struct object_id real_oid = *null_oid(data->repo->hash_algo);
716 int err = 0;
717
718 oi.sizep = &size;
719 oi.typep = &type;
720
721 if (read_loose_object(data->repo, path, oid, &real_oid, &contents, &oi) < 0) {
722 if (contents && !oideq(&real_oid, oid))
723 err = error(_("%s: hash-path mismatch, found at: %s"),
724 oid_to_hex(&real_oid), path);
725 else
726 err = error(_("%s: object corrupt or missing: %s"),
727 oid_to_hex(oid), path);
728 }
729 if (err < 0) {
730 errors_found |= ERROR_OBJECT;
731 free(contents);
732 return 0; /* keep checking other objects */
733 }
734
735 if (!contents && type != OBJ_BLOB)
736 BUG("read_loose_object streamed a non-blob");
737
738 if (fsck_obj_buffer(oid, type, size, contents, &eaten, data->repo))
739 errors_found |= ERROR_OBJECT;
740
741 if (!eaten)
742 free(contents);
743 return 0; /* keep checking other objects, even if we saw an error */
744 }
745
746 static int fsck_cruft(const char *basename, const char *path,
747 void *data UNUSED)
748 {
749 if (!starts_with(basename, "tmp_obj_"))
750 fprintf_ln(stderr, _("bad sha1 file: %s"), path);
751 return 0;
752 }
753
754 static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data)
755 {
756 struct for_each_loose_cb *cb_data = data;
757 struct progress *progress = cb_data->progress;
758 display_progress(progress, nr + 1);
759 return 0;
760 }
761
762 static void fsck_source(struct repository *repo, struct odb_source *source)
763 {
764 struct progress *progress = NULL;
765 struct for_each_loose_cb cb_data = {
766 .repo = source->odb->repo,
767 .progress = progress,
768 };
769
770 if (verbose)
771 fprintf_ln(stderr, _("Checking object directory"));
772
773 if (show_progress)
774 progress = start_progress(repo,
775 _("Checking object directories"), 256);
776
777 for_each_loose_file_in_source(source, fsck_loose,
778 fsck_cruft, fsck_subdir, &cb_data);
779 display_progress(progress, 256);
780 stop_progress(&progress);
781 }
782
783 static int fsck_cache_tree(struct repository *repo, struct cache_tree *it, const char *index_path)
784 {
785 int i;
786 int err = 0;
787
788 if (verbose)
789 fprintf_ln(stderr, _("Checking cache tree of %s"), index_path);
790
791 if (0 <= it->entry_count) {
792 struct object *obj = parse_object(repo, &it->oid);
793 if (!obj) {
794 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
795 oid_to_hex(&it->oid), index_path);
796 errors_found |= ERROR_REFS;
797 return 1;
798 }
799 obj->flags |= USED;
800 fsck_put_object_name(&fsck_walk_options, &it->oid, ":");
801 mark_object_reachable(obj);
802 if (obj->type != OBJ_TREE)
803 err |= objerror(repo, obj, _("non-tree in cache-tree"));
804 }
805 for (i = 0; i < it->subtree_nr; i++)
806 err |= fsck_cache_tree(repo, it->down[i]->cache_tree, index_path);
807 return err;
808 }
809
810 static int fsck_resolve_undo(struct index_state *istate,
811 const char *index_path)
812 {
813 struct string_list_item *item;
814 struct string_list *resolve_undo = istate->resolve_undo;
815
816 if (!resolve_undo)
817 return 0;
818
819 for_each_string_list_item(item, resolve_undo) {
820 const char *path = item->string;
821 struct resolve_undo_info *ru = item->util;
822 int i;
823
824 if (!ru)
825 continue;
826 for (i = 0; i < 3; i++) {
827 struct object *obj;
828
829 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
830 continue;
831
832 obj = parse_object(istate->repo, &ru->oid[i]);
833 if (!obj) {
834 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
835 oid_to_hex(&ru->oid[i]),
836 index_path);
837 errors_found |= ERROR_REFS;
838 continue;
839 }
840 obj->flags |= USED;
841 fsck_put_object_name(&fsck_walk_options, &ru->oid[i],
842 ":(%d):%s", i, path);
843 mark_object_reachable(obj);
844 }
845 }
846 return 0;
847 }
848
849 static void fsck_index(struct index_state *istate, const char *index_path,
850 int is_current_worktree)
851 {
852 unsigned int i;
853
854 /* TODO: audit for interaction with sparse-index. */
855 ensure_full_index(istate);
856 for (i = 0; i < istate->cache_nr; i++) {
857 unsigned int mode;
858 struct blob *blob;
859 struct object *obj;
860
861 mode = istate->cache[i]->ce_mode;
862 if (S_ISGITLINK(mode))
863 continue;
864 blob = lookup_blob(istate->repo,
865 &istate->cache[i]->oid);
866 if (!blob)
867 continue;
868 obj = &blob->object;
869 obj->flags |= USED;
870 fsck_put_object_name(&fsck_walk_options, &obj->oid,
871 "%s:%s",
872 is_current_worktree ? "" : index_path,
873 istate->cache[i]->name);
874 mark_object_reachable(obj);
875 }
876 if (istate->cache_tree)
877 fsck_cache_tree(istate->repo, istate->cache_tree, index_path);
878 fsck_resolve_undo(istate, index_path);
879 }
880
881 static int mark_object_for_connectivity(const struct object_id *oid,
882 struct object_info *oi UNUSED,
883 void *cb_data)
884 {
885 struct repository *repo = cb_data;
886 struct object *obj = lookup_unknown_object(repo, oid);
887 obj->flags |= HAS_OBJ;
888 return 0;
889 }
890
891 static void fsck_refs(struct repository *r)
892 {
893 struct child_process refs_verify = CHILD_PROCESS_INIT;
894 struct progress *progress = NULL;
895
896 if (show_progress)
897 progress = start_progress(r, _("Checking ref database"), 1);
898
899 if (verbose)
900 fprintf_ln(stderr, _("Checking ref database"));
901
902 child_process_init(&refs_verify);
903 refs_verify.git_cmd = 1;
904 strvec_pushl(&refs_verify.args, "refs", "verify", NULL);
905 if (verbose)
906 strvec_push(&refs_verify.args, "--verbose");
907 if (check_strict)
908 strvec_push(&refs_verify.args, "--strict");
909
910 if (run_command(&refs_verify))
911 errors_found |= ERROR_REFS;
912
913 display_progress(progress, 1);
914 stop_progress(&progress);
915 }
916
917 static char const * const fsck_usage[] = {
918 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
919 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
920 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
921 " [--[no-]name-objects] [--[no-]references] [<object>...]"),
922 NULL
923 };
924
925 int cmd_fsck(int argc,
926 const char **argv,
927 const char *prefix,
928 struct repository *repo)
929 {
930 struct odb_fsck_options odb_fsck_opts = {
931 .flags = ODB_FSCK_FULL,
932 .object_cb = fsck_obj_buffer,
933 .object_payload = repo,
934 };
935 int keep_cache_objects = 0;
936 int name_objects = 0;
937 int check_references = 1;
938 struct option fsck_opts[] = {
939 OPT__VERBOSE(&verbose, N_("be verbose")),
940 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
941 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
942 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
943 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
944 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
945 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
946 OPT_BIT(0, "full", &odb_fsck_opts.flags,
947 N_("also consider packs and alternate objects"), ODB_FSCK_FULL),
948 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
949 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
950 OPT_BOOL(0, "lost-found", &write_lost_and_found,
951 N_("write dangling objects in .git/lost-found")),
952 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
953 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
954 OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")),
955 OPT_END(),
956 };
957 struct odb_source *source;
958 struct snapshot snap = {
959 .nr = 0,
960 .alloc = 0,
961 .ref = NULL
962 };
963
964 /* fsck knows how to handle missing promisor objects */
965 fetch_if_missing = 0;
966
967 errors_found = 0;
968 disable_replace_refs();
969 save_commit_buffer = 0;
970
971 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
972
973 fsck_options_init(&fsck_walk_options, repo, FSCK_OPTIONS_DEFAULT);
974 fsck_walk_options.walk = mark_object;
975
976 fsck_options_init(&fsck_obj_options, repo, FSCK_OPTIONS_DEFAULT);
977 fsck_obj_options.walk = mark_used;
978 fsck_obj_options.error_func = fsck_objects_error_func;
979 if (check_strict)
980 fsck_obj_options.strict = 1;
981
982 if (show_progress == -1)
983 show_progress = isatty(2);
984 if (verbose)
985 show_progress = 0;
986 if (show_progress)
987 odb_fsck_opts.flags |= ODB_FSCK_PROGRESS;
988
989 if (write_lost_and_found) {
990 odb_fsck_opts.flags |= ODB_FSCK_FULL;
991 include_reflogs = 0;
992 }
993
994 if (name_objects)
995 fsck_enable_object_names(&fsck_walk_options);
996
997 repo_config(repo, git_fsck_config, &fsck_obj_options);
998 prepare_repo_settings(repo);
999
1000 if (check_references)
1001 fsck_refs(repo);
1002
1003 /*
1004 * Take a snapshot of the refs before walking objects to avoid looking
1005 * at a set of refs that may be changed by the user while we are walking
1006 * objects. We can still walk over new objects that are added during the
1007 * execution of fsck but won't miss any objects that were reachable.
1008 */
1009 snapshot_refs(repo, &snap, argc, argv);
1010
1011 /* Ensure we get a "fresh" view of the odb */
1012 odb_reprepare(repo->objects);
1013
1014 if (connectivity_only) {
1015 odb_for_each_object(repo->objects, NULL,
1016 mark_object_for_connectivity, repo, 0);
1017 } else {
1018 for (source = repo->objects->sources; source; source = source->next)
1019 if ((odb_fsck_opts.flags & ODB_FSCK_FULL) || source->local)
1020 fsck_source(repo, source);
1021
1022 if (odb_fsck(repo->objects, &odb_fsck_opts) < 0)
1023 errors_found |= ERROR_OBJECT;
1024
1025 if (fsck_finish(&fsck_obj_options))
1026 errors_found |= ERROR_OBJECT;
1027 }
1028
1029 /* Process the snapshotted refs and the reflogs. */
1030 process_refs(repo, &snap);
1031
1032 /* If not given any explicit objects, process index files too. */
1033 if (!argc)
1034 keep_cache_objects = 1;
1035 if (keep_cache_objects) {
1036 /*
1037 * TODO: Consider first walking these indexes in snapshot_refs,
1038 * to snapshot where the index entries used to point, and then
1039 * check those snapshotted locations here.
1040 */
1041 struct worktree **worktrees, **p;
1042
1043 verify_index_checksum = 1;
1044 verify_ce_order = 1;
1045
1046 worktrees = get_worktrees(repo);
1047 for (p = worktrees; *p; p++) {
1048 struct worktree *wt = *p;
1049 struct index_state istate =
1050 INDEX_STATE_INIT(repo);
1051 char *path, *wt_gitdir;
1052
1053 /*
1054 * Make a copy since the buffer is reusable
1055 * and may get overwritten by other calls
1056 * while we're examining the index.
1057 */
1058 path = xstrdup(worktree_git_path(wt, "index"));
1059 wt_gitdir = get_worktree_git_dir(wt);
1060
1061 read_index_from(&istate, path, wt_gitdir);
1062 fsck_index(&istate, path, wt->is_current);
1063
1064 discard_index(&istate);
1065 free(wt_gitdir);
1066 free(path);
1067 }
1068 free_worktrees(worktrees);
1069 }
1070
1071 if (verify_bitmap_files(repo))
1072 errors_found |= ERROR_BITMAP;
1073
1074 check_connectivity(repo);
1075
1076 if (repo->settings.core_commit_graph) {
1077 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
1078
1079 for (source = repo->objects->sources; source; source = source->next) {
1080 child_process_init(&commit_graph_verify);
1081 commit_graph_verify.git_cmd = 1;
1082 strvec_pushl(&commit_graph_verify.args, "commit-graph",
1083 "verify", "--object-dir", source->path, NULL);
1084 if (show_progress)
1085 strvec_push(&commit_graph_verify.args, "--progress");
1086 else
1087 strvec_push(&commit_graph_verify.args, "--no-progress");
1088 if (run_command(&commit_graph_verify))
1089 errors_found |= ERROR_COMMIT_GRAPH;
1090 }
1091 }
1092
1093 if (repo->settings.core_multi_pack_index) {
1094 struct child_process midx_verify = CHILD_PROCESS_INIT;
1095
1096 for (source = repo->objects->sources; source; source = source->next) {
1097 child_process_init(&midx_verify);
1098 midx_verify.git_cmd = 1;
1099 strvec_pushl(&midx_verify.args, "multi-pack-index",
1100 "verify", "--object-dir", source->path, NULL);
1101 if (show_progress)
1102 strvec_push(&midx_verify.args, "--progress");
1103 else
1104 strvec_push(&midx_verify.args, "--no-progress");
1105 if (run_command(&midx_verify))
1106 errors_found |= ERROR_MULTI_PACK_INDEX;
1107 }
1108 }
1109
1110 free_snapshot_refs(&snap);
1111 return errors_found;
1112 }