at commit 301a1ce9

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