at commit 6bc77804

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