Raw
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();
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();
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 struct object *obj;
726 enum object_type type = OBJ_NONE;
727 unsigned long size;
728 void *contents = NULL;
729 int eaten;
730 struct object_info oi = OBJECT_INFO_INIT;
731 struct object_id real_oid = *null_oid(data->repo->hash_algo);
732 int err = 0;
733
734 oi.sizep = &size;
735 oi.typep = &type;
736
737 if (read_loose_object(data->repo, path, oid, &real_oid, &contents, &oi) < 0) {
738 if (contents && !oideq(&real_oid, oid))
739 err = error(_("%s: hash-path mismatch, found at: %s"),
740 oid_to_hex(&real_oid), path);
741 else
742 err = error(_("%s: object corrupt or missing: %s"),
743 oid_to_hex(oid), path);
744 }
745 if (err < 0) {
746 errors_found |= ERROR_OBJECT;
747 free(contents);
748 return 0; /* keep checking other objects */
749 }
750
751 if (!contents && type != OBJ_BLOB)
752 BUG("read_loose_object streamed a non-blob");
753
754 obj = parse_object_buffer(data->repo, oid, type, size,
755 contents, &eaten);
756
757 if (!obj) {
758 errors_found |= ERROR_OBJECT;
759 error(_("%s: object could not be parsed: %s"),
760 oid_to_hex(oid), path);
761 if (!eaten)
762 free(contents);
763 return 0; /* keep checking other objects */
764 }
765
766 obj->flags &= ~(REACHABLE | SEEN);
767 obj->flags |= HAS_OBJ;
768 if (fsck_obj(data->repo, obj, contents, size))
769 errors_found |= ERROR_OBJECT;
770
771 if (!eaten)
772 free(contents);
773 return 0; /* keep checking other objects, even if we saw an error */
774 }
775
776 static int fsck_cruft(const char *basename, const char *path,
777 void *data UNUSED)
778 {
779 if (!starts_with(basename, "tmp_obj_"))
780 fprintf_ln(stderr, _("bad sha1 file: %s"), path);
781 return 0;
782 }
783
784 static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data)
785 {
786 struct for_each_loose_cb *cb_data = data;
787 struct progress *progress = cb_data->progress;
788 display_progress(progress, nr + 1);
789 return 0;
790 }
791
792 static void fsck_source(struct repository *repo, struct odb_source *source)
793 {
794 struct progress *progress = NULL;
795 struct for_each_loose_cb cb_data = {
796 .repo = source->odb->repo,
797 .progress = progress,
798 };
799
800 if (verbose)
801 fprintf_ln(stderr, _("Checking object directory"));
802
803 if (show_progress)
804 progress = start_progress(repo,
805 _("Checking object directories"), 256);
806
807 for_each_loose_file_in_source(source, fsck_loose,
808 fsck_cruft, fsck_subdir, &cb_data);
809 display_progress(progress, 256);
810 stop_progress(&progress);
811 }
812
813 static int fsck_cache_tree(struct repository *repo, struct cache_tree *it, const char *index_path)
814 {
815 int i;
816 int err = 0;
817
818 if (verbose)
819 fprintf_ln(stderr, _("Checking cache tree of %s"), index_path);
820
821 if (0 <= it->entry_count) {
822 struct object *obj = parse_object(repo, &it->oid);
823 if (!obj) {
824 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
825 oid_to_hex(&it->oid), index_path);
826 errors_found |= ERROR_REFS;
827 return 1;
828 }
829 obj->flags |= USED;
830 fsck_put_object_name(&fsck_walk_options, &it->oid, ":");
831 mark_object_reachable(obj);
832 if (obj->type != OBJ_TREE)
833 err |= objerror(repo, obj, _("non-tree in cache-tree"));
834 }
835 for (i = 0; i < it->subtree_nr; i++)
836 err |= fsck_cache_tree(repo, it->down[i]->cache_tree, index_path);
837 return err;
838 }
839
840 static int fsck_resolve_undo(struct index_state *istate,
841 const char *index_path)
842 {
843 struct string_list_item *item;
844 struct string_list *resolve_undo = istate->resolve_undo;
845
846 if (!resolve_undo)
847 return 0;
848
849 for_each_string_list_item(item, resolve_undo) {
850 const char *path = item->string;
851 struct resolve_undo_info *ru = item->util;
852 int i;
853
854 if (!ru)
855 continue;
856 for (i = 0; i < 3; i++) {
857 struct object *obj;
858
859 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
860 continue;
861
862 obj = parse_object(istate->repo, &ru->oid[i]);
863 if (!obj) {
864 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
865 oid_to_hex(&ru->oid[i]),
866 index_path);
867 errors_found |= ERROR_REFS;
868 continue;
869 }
870 obj->flags |= USED;
871 fsck_put_object_name(&fsck_walk_options, &ru->oid[i],
872 ":(%d):%s", i, path);
873 mark_object_reachable(obj);
874 }
875 }
876 return 0;
877 }
878
879 static void fsck_index(struct index_state *istate, const char *index_path,
880 int is_current_worktree)
881 {
882 unsigned int i;
883
884 /* TODO: audit for interaction with sparse-index. */
885 ensure_full_index(istate);
886 for (i = 0; i < istate->cache_nr; i++) {
887 unsigned int mode;
888 struct blob *blob;
889 struct object *obj;
890
891 mode = istate->cache[i]->ce_mode;
892 if (S_ISGITLINK(mode))
893 continue;
894 blob = lookup_blob(istate->repo,
895 &istate->cache[i]->oid);
896 if (!blob)
897 continue;
898 obj = &blob->object;
899 obj->flags |= USED;
900 fsck_put_object_name(&fsck_walk_options, &obj->oid,
901 "%s:%s",
902 is_current_worktree ? "" : index_path,
903 istate->cache[i]->name);
904 mark_object_reachable(obj);
905 }
906 if (istate->cache_tree)
907 fsck_cache_tree(istate->repo, istate->cache_tree, index_path);
908 fsck_resolve_undo(istate, index_path);
909 }
910
911 static int mark_object_for_connectivity(const struct object_id *oid,
912 struct object_info *oi UNUSED,
913 void *cb_data)
914 {
915 struct repository *repo = cb_data;
916 struct object *obj = lookup_unknown_object(repo, oid);
917 obj->flags |= HAS_OBJ;
918 return 0;
919 }
920
921 static int check_pack_rev_indexes(struct repository *r, int show_progress)
922 {
923 struct progress *progress = NULL;
924 struct packed_git *p;
925 uint32_t pack_count = 0;
926 int res = 0;
927
928 if (show_progress) {
929 repo_for_each_pack(r, p)
930 pack_count++;
931 progress = start_delayed_progress(r,
932 "Verifying reverse pack-indexes", pack_count);
933 pack_count = 0;
934 }
935
936 repo_for_each_pack(r, p) {
937 int load_error = load_pack_revindex_from_disk(p);
938
939 if (load_error < 0) {
940 error(_("unable to load rev-index for pack '%s'"), p->pack_name);
941 res = ERROR_PACK_REV_INDEX;
942 } else if (!load_error &&
943 !load_pack_revindex(r, p) &&
944 verify_pack_revindex(p)) {
945 error(_("invalid rev-index for pack '%s'"), p->pack_name);
946 res = ERROR_PACK_REV_INDEX;
947 }
948 display_progress(progress, ++pack_count);
949 }
950 stop_progress(&progress);
951
952 return res;
953 }
954
955 static void fsck_refs(struct repository *r)
956 {
957 struct child_process refs_verify = CHILD_PROCESS_INIT;
958 struct progress *progress = NULL;
959
960 if (show_progress)
961 progress = start_progress(r, _("Checking ref database"), 1);
962
963 if (verbose)
964 fprintf_ln(stderr, _("Checking ref database"));
965
966 child_process_init(&refs_verify);
967 refs_verify.git_cmd = 1;
968 strvec_pushl(&refs_verify.args, "refs", "verify", NULL);
969 if (verbose)
970 strvec_push(&refs_verify.args, "--verbose");
971 if (check_strict)
972 strvec_push(&refs_verify.args, "--strict");
973
974 if (run_command(&refs_verify))
975 errors_found |= ERROR_REFS;
976
977 display_progress(progress, 1);
978 stop_progress(&progress);
979 }
980
981 static char const * const fsck_usage[] = {
982 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
983 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
984 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
985 " [--[no-]name-objects] [--[no-]references] [<object>...]"),
986 NULL
987 };
988
989 static struct option fsck_opts[] = {
990 OPT__VERBOSE(&verbose, N_("be verbose")),
991 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
992 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
993 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
994 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
995 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
996 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
997 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
998 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
999 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
1000 OPT_BOOL(0, "lost-found", &write_lost_and_found,
1001 N_("write dangling objects in .git/lost-found")),
1002 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
1003 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
1004 OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")),
1005 OPT_END(),
1006 };
1007
1008 int cmd_fsck(int argc,
1009 const char **argv,
1010 const char *prefix,
1011 struct repository *repo)
1012 {
1013 struct odb_source *source;
1014 struct snapshot snap = {
1015 .nr = 0,
1016 .alloc = 0,
1017 .ref = NULL
1018 };
1019
1020 /* fsck knows how to handle missing promisor objects */
1021 fetch_if_missing = 0;
1022
1023 errors_found = 0;
1024 disable_replace_refs();
1025 save_commit_buffer = 0;
1026
1027 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
1028
1029 fsck_options_init(&fsck_walk_options, repo, FSCK_OPTIONS_DEFAULT);
1030 fsck_walk_options.walk = mark_object;
1031
1032 fsck_options_init(&fsck_obj_options, repo, FSCK_OPTIONS_DEFAULT);
1033 fsck_obj_options.walk = mark_used;
1034 fsck_obj_options.error_func = fsck_objects_error_func;
1035 if (check_strict)
1036 fsck_obj_options.strict = 1;
1037
1038 if (show_progress == -1)
1039 show_progress = isatty(2);
1040 if (verbose)
1041 show_progress = 0;
1042
1043 if (write_lost_and_found) {
1044 check_full = 1;
1045 include_reflogs = 0;
1046 }
1047
1048 if (name_objects)
1049 fsck_enable_object_names(&fsck_walk_options);
1050
1051 repo_config(repo, git_fsck_config, &fsck_obj_options);
1052 prepare_repo_settings(repo);
1053
1054 if (check_references)
1055 fsck_refs(repo);
1056
1057 /*
1058 * Take a snapshot of the refs before walking objects to avoid looking
1059 * at a set of refs that may be changed by the user while we are walking
1060 * objects. We can still walk over new objects that are added during the
1061 * execution of fsck but won't miss any objects that were reachable.
1062 */
1063 snapshot_refs(repo, &snap, argc, argv);
1064
1065 /* Ensure we get a "fresh" view of the odb */
1066 odb_reprepare(repo->objects);
1067
1068 if (connectivity_only) {
1069 odb_for_each_object(repo->objects, NULL,
1070 mark_object_for_connectivity, repo, 0);
1071 } else {
1072 odb_prepare_alternates(repo->objects);
1073 for (source = repo->objects->sources; source; source = source->next)
1074 fsck_source(repo, source);
1075
1076 if (check_full) {
1077 struct packed_git *p;
1078 uint32_t total = 0, count = 0;
1079 struct progress *progress = NULL;
1080
1081 if (show_progress) {
1082 repo_for_each_pack(repo, p) {
1083 if (open_pack_index(p))
1084 continue;
1085 total += p->num_objects;
1086 }
1087
1088 progress = start_progress(repo,
1089 _("Checking objects"), total);
1090 }
1091
1092 repo_for_each_pack(repo, p) {
1093 /* verify gives error messages itself */
1094 if (verify_pack(repo,
1095 p, fsck_obj_buffer, repo,
1096 progress, count))
1097 errors_found |= ERROR_PACK;
1098 count += p->num_objects;
1099 }
1100 stop_progress(&progress);
1101 }
1102
1103 if (fsck_finish(&fsck_obj_options))
1104 errors_found |= ERROR_OBJECT;
1105 }
1106
1107 /* Process the snapshotted refs and the reflogs. */
1108 process_refs(repo, &snap);
1109
1110 /* If not given any explicit objects, process index files too. */
1111 if (!argc)
1112 keep_cache_objects = 1;
1113 if (keep_cache_objects) {
1114 /*
1115 * TODO: Consider first walking these indexes in snapshot_refs,
1116 * to snapshot where the index entries used to point, and then
1117 * check those snapshotted locations here.
1118 */
1119 struct worktree **worktrees, **p;
1120
1121 verify_index_checksum = 1;
1122 verify_ce_order = 1;
1123
1124 worktrees = get_worktrees();
1125 for (p = worktrees; *p; p++) {
1126 struct worktree *wt = *p;
1127 struct index_state istate =
1128 INDEX_STATE_INIT(repo);
1129 char *path, *wt_gitdir;
1130
1131 /*
1132 * Make a copy since the buffer is reusable
1133 * and may get overwritten by other calls
1134 * while we're examining the index.
1135 */
1136 path = xstrdup(worktree_git_path(wt, "index"));
1137 wt_gitdir = get_worktree_git_dir(wt);
1138
1139 read_index_from(&istate, path, wt_gitdir);
1140 fsck_index(&istate, path, wt->is_current);
1141
1142 discard_index(&istate);
1143 free(wt_gitdir);
1144 free(path);
1145 }
1146 free_worktrees(worktrees);
1147 }
1148
1149 errors_found |= check_pack_rev_indexes(repo, show_progress);
1150 if (verify_bitmap_files(repo))
1151 errors_found |= ERROR_BITMAP;
1152
1153 check_connectivity(repo);
1154
1155 if (repo->settings.core_commit_graph) {
1156 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
1157
1158 odb_prepare_alternates(repo->objects);
1159 for (source = repo->objects->sources; source; source = source->next) {
1160 child_process_init(&commit_graph_verify);
1161 commit_graph_verify.git_cmd = 1;
1162 strvec_pushl(&commit_graph_verify.args, "commit-graph",
1163 "verify", "--object-dir", source->path, NULL);
1164 if (show_progress)
1165 strvec_push(&commit_graph_verify.args, "--progress");
1166 else
1167 strvec_push(&commit_graph_verify.args, "--no-progress");
1168 if (run_command(&commit_graph_verify))
1169 errors_found |= ERROR_COMMIT_GRAPH;
1170 }
1171 }
1172
1173 if (repo->settings.core_multi_pack_index) {
1174 struct child_process midx_verify = CHILD_PROCESS_INIT;
1175
1176 odb_prepare_alternates(repo->objects);
1177 for (source = repo->objects->sources; source; source = source->next) {
1178 child_process_init(&midx_verify);
1179 midx_verify.git_cmd = 1;
1180 strvec_pushl(&midx_verify.args, "multi-pack-index",
1181 "verify", "--object-dir", source->path, NULL);
1182 if (show_progress)
1183 strvec_push(&midx_verify.args, "--progress");
1184 else
1185 strvec_push(&midx_verify.args, "--no-progress");
1186 if (run_command(&midx_verify))
1187 errors_found |= ERROR_MULTI_PACK_INDEX;
1188 }
1189 }
1190
1191 free_snapshot_refs(&snap);
1192 return errors_found;
1193 }