at commit a51b77aa

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