at commit dbdea7a9

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_buffer(const struct object_id *oid, enum object_type type,
405 unsigned long size, void *buffer, int *eaten, void *cb_data)
406 {
407 struct repository *repo = cb_data;
408 struct object *obj;
409 int err;
410
411 /*
412 * Note, buffer may be NULL if type is OBJ_BLOB. See
413 * verify_packfile(), data_valid variable for details.
414 */
415 obj = parse_object_buffer(repo, oid, type, size, buffer, eaten);
416 if (!obj) {
417 errors_found |= ERROR_OBJECT;
418 err = error(_("%s: object corrupt or missing"),
419 oid_to_hex(oid));
420 goto out;
421 }
422
423 obj->flags &= ~REACHABLE;
424 obj->flags |= HAS_OBJ | SEEN;
425
426 if (verbose)
427 fprintf_ln(stderr, _("Checking %s %s"),
428 printable_type(repo, &obj->oid, obj->type),
429 describe_object(&obj->oid));
430
431 if (fsck_walk(obj, NULL, &fsck_obj_options))
432 objerror(repo, obj, _("broken links"));
433
434 err = fsck_object(obj, buffer, size, &fsck_obj_options);
435 if (err)
436 goto out;
437
438 if (obj->type == OBJ_COMMIT) {
439 struct commit *commit = (struct commit *) obj;
440
441 if (!commit->parents && show_root)
442 printf_ln(_("root %s"),
443 describe_object(&commit->object.oid));
444 }
445
446 if (obj->type == OBJ_TAG) {
447 struct tag *tag = (struct tag *) obj;
448
449 if (show_tags && tag->tagged) {
450 printf_ln(_("tagged %s %s (%s) in %s"),
451 printable_type(repo, &tag->tagged->oid, tag->tagged->type),
452 describe_object(&tag->tagged->oid),
453 tag->tag,
454 describe_object(&tag->object.oid));
455 }
456 }
457
458 out:
459 if (obj && obj->type == OBJ_TREE)
460 free_tree_buffer((struct tree *)obj);
461 return err;
462 }
463
464 static int default_refs;
465
466 static void fsck_handle_reflog_oid(struct repository *repo,
467 const char *refname, struct object_id *oid,
468 timestamp_t timestamp)
469 {
470 struct object *obj;
471
472 if (!is_null_oid(oid)) {
473 obj = lookup_object(repo, oid);
474 if (obj && (obj->flags & HAS_OBJ)) {
475 if (timestamp)
476 fsck_put_object_name(&fsck_walk_options, oid,
477 "%s@{%"PRItime"}",
478 refname, timestamp);
479 obj->flags |= USED;
480 mark_object_reachable(obj);
481 } else if (!is_promisor_object(repo, oid)) {
482 error(_("%s: invalid reflog entry %s"),
483 refname, oid_to_hex(oid));
484 errors_found |= ERROR_REACHABLE;
485 }
486 }
487 }
488
489 static int fsck_handle_reflog_ent(const char *refname,
490 struct object_id *ooid, struct object_id *noid,
491 const char *email UNUSED,
492 timestamp_t timestamp, int tz UNUSED,
493 const char *message UNUSED, void *cb_data)
494 {
495 struct repository *repo = cb_data;
496
497 if (now && timestamp > now)
498 return 0;
499
500 if (verbose)
501 fprintf_ln(stderr, _("Checking reflog %s->%s"),
502 oid_to_hex(ooid), oid_to_hex(noid));
503
504 fsck_handle_reflog_oid(repo, refname, ooid, 0);
505 fsck_handle_reflog_oid(repo, refname, noid, timestamp);
506 return 0;
507 }
508
509 static int fsck_handle_reflog(const char *logname, void *cb_data)
510 {
511 struct strbuf refname = STRBUF_INIT;
512 struct worktree *wt = cb_data;
513
514 strbuf_worktree_ref(wt, &refname, logname);
515 refs_for_each_reflog_ent(get_main_ref_store(wt->repo),
516 refname.buf, fsck_handle_reflog_ent,
517 wt->repo);
518 strbuf_release(&refname);
519 return 0;
520 }
521
522 struct ref_snapshot {
523 char *refname;
524 struct object_id oid;
525 /* TODO: Maybe supplement with latest reflog entry info too? */
526 };
527
528 struct snapshot {
529 size_t nr;
530 size_t alloc;
531 struct ref_snapshot *ref;
532 /* TODO: Consider also snapshotting the index of each worktree. */
533 };
534
535 struct snapshot_ref_data {
536 struct repository *repo;
537 struct snapshot *snap;
538 };
539
540 static int snapshot_ref(const struct reference *ref, void *cb_data)
541 {
542 struct snapshot_ref_data *data = cb_data;
543 struct snapshot *snap = data->snap;
544 struct object *obj;
545
546 obj = parse_object(data->repo, ref->oid);
547 if (!obj) {
548 if (is_promisor_object(data->repo, ref->oid)) {
549 /*
550 * Increment default_refs anyway, because this is a
551 * valid ref.
552 */
553 default_refs++;
554 return 0;
555 }
556 error(_("%s: invalid sha1 pointer %s"),
557 ref->name, oid_to_hex(ref->oid));
558 errors_found |= ERROR_REACHABLE;
559 /* We'll continue with the rest despite the error.. */
560 return 0;
561 }
562 if (obj->type != OBJ_COMMIT && is_branch(ref->name)) {
563 error(_("%s: not a commit"), ref->name);
564 errors_found |= ERROR_REFS;
565 }
566 default_refs++;
567
568 ALLOC_GROW(snap->ref, snap->nr + 1, snap->alloc);
569 snap->ref[snap->nr].refname = xstrdup(ref->name);
570 oidcpy(&snap->ref[snap->nr].oid, ref->oid);
571 snap->nr++;
572
573 return 0;
574 }
575
576 static int fsck_handle_ref(const struct reference *ref, void *cb_data)
577 {
578 struct repository *repo = cb_data;
579 struct object *obj;
580
581 obj = parse_object(repo, ref->oid);
582 obj->flags |= USED;
583 fsck_put_object_name(&fsck_walk_options,
584 ref->oid, "%s", ref->name);
585 mark_object_reachable(obj);
586
587 return 0;
588 }
589
590 static void snapshot_refs(struct repository *repo,
591 struct snapshot *snap, int argc, const char **argv)
592 {
593 struct refs_for_each_ref_options opts = {
594 .flags = REFS_FOR_EACH_INCLUDE_BROKEN,
595 };
596 struct snapshot_ref_data data = {
597 .repo = repo,
598 .snap = snap,
599 };
600 struct worktree **worktrees, **p;
601 const char *head_points_at;
602 struct object_id head_oid;
603
604 for (int i = 0; i < argc; i++) {
605 const char *arg = argv[i];
606 struct object_id oid;
607 if (!repo_get_oid(repo, arg, &oid)) {
608 struct reference ref = {
609 .name = arg,
610 .oid = &oid,
611 };
612
613 snapshot_ref(&ref, &data);
614 continue;
615 }
616 error(_("invalid parameter: expected sha1, got '%s'"), arg);
617 errors_found |= ERROR_OBJECT;
618 }
619
620 if (argc) {
621 include_reflogs = 0;
622 return;
623 }
624
625 refs_for_each_ref_ext(get_main_ref_store(repo),
626 snapshot_ref, &data, &opts);
627
628 worktrees = get_worktrees(repo);
629 for (p = worktrees; *p; p++) {
630 struct worktree *wt = *p;
631 struct strbuf refname = STRBUF_INIT;
632
633 strbuf_worktree_ref(wt, &refname, "HEAD");
634
635 head_points_at = refs_resolve_ref_unsafe(get_main_ref_store(repo),
636 refname.buf, 0, &head_oid, NULL);
637
638 if (head_points_at && !is_null_oid(&head_oid)) {
639 struct reference ref = {
640 .name = refname.buf,
641 .oid = &head_oid,
642 };
643
644 snapshot_ref(&ref, &data);
645 }
646 strbuf_release(&refname);
647
648 /*
649 * TODO: Could use refs_for_each_reflog(...) to find
650 * latest entry instead of using a global 'now' for that
651 * purpose.
652 */
653 }
654 free_worktrees(worktrees);
655
656 /* Ignore reflogs newer than now */
657 now = time(NULL);
658 }
659
660
661 static void free_snapshot_refs(struct snapshot *snap)
662 {
663 for (size_t i = 0; i < snap->nr; i++)
664 free(snap->ref[i].refname);
665 free(snap->ref);
666 }
667
668 static void process_refs(struct repository *repo, struct snapshot *snap)
669 {
670 struct worktree **worktrees, **p;
671
672 for (size_t i = 0; i < snap->nr; i++) {
673 struct reference ref = {
674 .name = snap->ref[i].refname,
675 .oid = &snap->ref[i].oid,
676 };
677 fsck_handle_ref(&ref, repo);
678 }
679
680 if (include_reflogs) {
681 worktrees = get_worktrees(repo);
682 for (p = worktrees; *p; p++) {
683 struct worktree *wt = *p;
684
685 refs_for_each_reflog(get_worktree_ref_store(wt),
686 fsck_handle_reflog, wt);
687 }
688 free_worktrees(worktrees);
689 }
690
691 /*
692 * Not having any default heads isn't really fatal, but
693 * it does mean that "--unreachable" no longer makes any
694 * sense (since in this case everything will obviously
695 * be unreachable by definition.
696 *
697 * Showing dangling objects is valid, though (as those
698 * dangling objects are likely lost heads).
699 *
700 * So we just print a warning about it, and clear the
701 * "show_unreachable" flag.
702 */
703 if (!default_refs) {
704 fprintf_ln(stderr, _("notice: No default references"));
705 show_unreachable = 0;
706 }
707 }
708
709 struct for_each_loose_cb {
710 struct repository *repo;
711 struct progress *progress;
712 };
713
714 static int fsck_loose(const struct object_id *oid, const char *path,
715 void *cb_data)
716 {
717 struct for_each_loose_cb *data = cb_data;
718 enum object_type type = OBJ_NONE;
719 size_t size;
720 void *contents = NULL;
721 int eaten;
722 struct object_info oi = OBJECT_INFO_INIT;
723 struct object_id real_oid = *null_oid(data->repo->hash_algo);
724 int err = 0;
725
726 oi.sizep = &size;
727 oi.typep = &type;
728
729 if (read_loose_object(data->repo, path, oid, &real_oid, &contents, &oi) < 0) {
730 if (contents && !oideq(&real_oid, oid))
731 err = error(_("%s: hash-path mismatch, found at: %s"),
732 oid_to_hex(&real_oid), path);
733 else
734 err = error(_("%s: object corrupt or missing: %s"),
735 oid_to_hex(oid), path);
736 }
737 if (err < 0) {
738 errors_found |= ERROR_OBJECT;
739 free(contents);
740 return 0; /* keep checking other objects */
741 }
742
743 if (!contents && type != OBJ_BLOB)
744 BUG("read_loose_object streamed a non-blob");
745
746 if (fsck_obj_buffer(oid, type, size, contents, &eaten, data->repo))
747 errors_found |= ERROR_OBJECT;
748
749 if (!eaten)
750 free(contents);
751 return 0; /* keep checking other objects, even if we saw an error */
752 }
753
754 static int fsck_cruft(const char *basename, const char *path,
755 void *data UNUSED)
756 {
757 if (!starts_with(basename, "tmp_obj_"))
758 fprintf_ln(stderr, _("bad sha1 file: %s"), path);
759 return 0;
760 }
761
762 static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data)
763 {
764 struct for_each_loose_cb *cb_data = data;
765 struct progress *progress = cb_data->progress;
766 display_progress(progress, nr + 1);
767 return 0;
768 }
769
770 static void fsck_source(struct repository *repo, struct odb_source *source)
771 {
772 struct progress *progress = NULL;
773 struct for_each_loose_cb cb_data = {
774 .repo = source->odb->repo,
775 .progress = progress,
776 };
777
778 if (verbose)
779 fprintf_ln(stderr, _("Checking object directory"));
780
781 if (show_progress)
782 progress = start_progress(repo,
783 _("Checking object directories"), 256);
784
785 for_each_loose_file_in_source(source, fsck_loose,
786 fsck_cruft, fsck_subdir, &cb_data);
787 display_progress(progress, 256);
788 stop_progress(&progress);
789 }
790
791 static int fsck_cache_tree(struct repository *repo, struct cache_tree *it, const char *index_path)
792 {
793 int i;
794 int err = 0;
795
796 if (verbose)
797 fprintf_ln(stderr, _("Checking cache tree of %s"), index_path);
798
799 if (0 <= it->entry_count) {
800 struct object *obj = parse_object(repo, &it->oid);
801 if (!obj) {
802 error(_("%s: invalid sha1 pointer in cache-tree of %s"),
803 oid_to_hex(&it->oid), index_path);
804 errors_found |= ERROR_REFS;
805 return 1;
806 }
807 obj->flags |= USED;
808 fsck_put_object_name(&fsck_walk_options, &it->oid, ":");
809 mark_object_reachable(obj);
810 if (obj->type != OBJ_TREE)
811 err |= objerror(repo, obj, _("non-tree in cache-tree"));
812 }
813 for (i = 0; i < it->subtree_nr; i++)
814 err |= fsck_cache_tree(repo, it->down[i]->cache_tree, index_path);
815 return err;
816 }
817
818 static int fsck_resolve_undo(struct index_state *istate,
819 const char *index_path)
820 {
821 struct string_list_item *item;
822 struct string_list *resolve_undo = istate->resolve_undo;
823
824 if (!resolve_undo)
825 return 0;
826
827 for_each_string_list_item(item, resolve_undo) {
828 const char *path = item->string;
829 struct resolve_undo_info *ru = item->util;
830 int i;
831
832 if (!ru)
833 continue;
834 for (i = 0; i < 3; i++) {
835 struct object *obj;
836
837 if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
838 continue;
839
840 obj = parse_object(istate->repo, &ru->oid[i]);
841 if (!obj) {
842 error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
843 oid_to_hex(&ru->oid[i]),
844 index_path);
845 errors_found |= ERROR_REFS;
846 continue;
847 }
848 obj->flags |= USED;
849 fsck_put_object_name(&fsck_walk_options, &ru->oid[i],
850 ":(%d):%s", i, path);
851 mark_object_reachable(obj);
852 }
853 }
854 return 0;
855 }
856
857 static void fsck_index(struct index_state *istate, const char *index_path,
858 int is_current_worktree)
859 {
860 unsigned int i;
861
862 /* TODO: audit for interaction with sparse-index. */
863 ensure_full_index(istate);
864 for (i = 0; i < istate->cache_nr; i++) {
865 unsigned int mode;
866 struct blob *blob;
867 struct object *obj;
868
869 mode = istate->cache[i]->ce_mode;
870 if (S_ISGITLINK(mode))
871 continue;
872 blob = lookup_blob(istate->repo,
873 &istate->cache[i]->oid);
874 if (!blob)
875 continue;
876 obj = &blob->object;
877 obj->flags |= USED;
878 fsck_put_object_name(&fsck_walk_options, &obj->oid,
879 "%s:%s",
880 is_current_worktree ? "" : index_path,
881 istate->cache[i]->name);
882 mark_object_reachable(obj);
883 }
884 if (istate->cache_tree)
885 fsck_cache_tree(istate->repo, istate->cache_tree, index_path);
886 fsck_resolve_undo(istate, index_path);
887 }
888
889 static int mark_object_for_connectivity(const struct object_id *oid,
890 struct object_info *oi UNUSED,
891 void *cb_data)
892 {
893 struct repository *repo = cb_data;
894 struct object *obj = lookup_unknown_object(repo, oid);
895 obj->flags |= HAS_OBJ;
896 return 0;
897 }
898
899 static int check_pack_rev_indexes(struct repository *r, int show_progress)
900 {
901 struct progress *progress = NULL;
902 struct packed_git *p;
903 uint32_t pack_count = 0;
904 int res = 0;
905
906 if (show_progress) {
907 repo_for_each_pack(r, p)
908 pack_count++;
909 progress = start_delayed_progress(r,
910 "Verifying reverse pack-indexes", pack_count);
911 pack_count = 0;
912 }
913
914 repo_for_each_pack(r, p) {
915 int load_error = load_pack_revindex_from_disk(p);
916
917 if (load_error < 0) {
918 error(_("unable to load rev-index for pack '%s'"), p->pack_name);
919 res = ERROR_PACK_REV_INDEX;
920 } else if (!load_error &&
921 !load_pack_revindex(r, p) &&
922 verify_pack_revindex(p)) {
923 error(_("invalid rev-index for pack '%s'"), p->pack_name);
924 res = ERROR_PACK_REV_INDEX;
925 }
926 display_progress(progress, ++pack_count);
927 }
928 stop_progress(&progress);
929
930 return res;
931 }
932
933 static void fsck_refs(struct repository *r)
934 {
935 struct child_process refs_verify = CHILD_PROCESS_INIT;
936 struct progress *progress = NULL;
937
938 if (show_progress)
939 progress = start_progress(r, _("Checking ref database"), 1);
940
941 if (verbose)
942 fprintf_ln(stderr, _("Checking ref database"));
943
944 child_process_init(&refs_verify);
945 refs_verify.git_cmd = 1;
946 strvec_pushl(&refs_verify.args, "refs", "verify", NULL);
947 if (verbose)
948 strvec_push(&refs_verify.args, "--verbose");
949 if (check_strict)
950 strvec_push(&refs_verify.args, "--strict");
951
952 if (run_command(&refs_verify))
953 errors_found |= ERROR_REFS;
954
955 display_progress(progress, 1);
956 stop_progress(&progress);
957 }
958
959 static char const * const fsck_usage[] = {
960 N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n"
961 " [--[no-]full] [--strict] [--verbose] [--lost-found]\n"
962 " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n"
963 " [--[no-]name-objects] [--[no-]references] [<object>...]"),
964 NULL
965 };
966
967 static struct option fsck_opts[] = {
968 OPT__VERBOSE(&verbose, N_("be verbose")),
969 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
970 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
971 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
972 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
973 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
974 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
975 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
976 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
977 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
978 OPT_BOOL(0, "lost-found", &write_lost_and_found,
979 N_("write dangling objects in .git/lost-found")),
980 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
981 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
982 OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")),
983 OPT_END(),
984 };
985
986 int cmd_fsck(int argc,
987 const char **argv,
988 const char *prefix,
989 struct repository *repo)
990 {
991 struct odb_source *source;
992 struct snapshot snap = {
993 .nr = 0,
994 .alloc = 0,
995 .ref = NULL
996 };
997
998 /* fsck knows how to handle missing promisor objects */
999 fetch_if_missing = 0;
1000
1001 errors_found = 0;
1002 disable_replace_refs();
1003 save_commit_buffer = 0;
1004
1005 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
1006
1007 fsck_options_init(&fsck_walk_options, repo, FSCK_OPTIONS_DEFAULT);
1008 fsck_walk_options.walk = mark_object;
1009
1010 fsck_options_init(&fsck_obj_options, repo, FSCK_OPTIONS_DEFAULT);
1011 fsck_obj_options.walk = mark_used;
1012 fsck_obj_options.error_func = fsck_objects_error_func;
1013 if (check_strict)
1014 fsck_obj_options.strict = 1;
1015
1016 if (show_progress == -1)
1017 show_progress = isatty(2);
1018 if (verbose)
1019 show_progress = 0;
1020
1021 if (write_lost_and_found) {
1022 check_full = 1;
1023 include_reflogs = 0;
1024 }
1025
1026 if (name_objects)
1027 fsck_enable_object_names(&fsck_walk_options);
1028
1029 repo_config(repo, git_fsck_config, &fsck_obj_options);
1030 prepare_repo_settings(repo);
1031
1032 if (check_references)
1033 fsck_refs(repo);
1034
1035 /*
1036 * Take a snapshot of the refs before walking objects to avoid looking
1037 * at a set of refs that may be changed by the user while we are walking
1038 * objects. We can still walk over new objects that are added during the
1039 * execution of fsck but won't miss any objects that were reachable.
1040 */
1041 snapshot_refs(repo, &snap, argc, argv);
1042
1043 /* Ensure we get a "fresh" view of the odb */
1044 odb_reprepare(repo->objects);
1045
1046 if (connectivity_only) {
1047 odb_for_each_object(repo->objects, NULL,
1048 mark_object_for_connectivity, repo, 0);
1049 } else {
1050 for (source = repo->objects->sources; source; source = source->next)
1051 fsck_source(repo, source);
1052
1053 if (check_full) {
1054 struct packed_git *p;
1055 uint32_t total = 0, count = 0;
1056 struct progress *progress = NULL;
1057
1058 if (show_progress) {
1059 repo_for_each_pack(repo, p) {
1060 if (open_pack_index(p))
1061 continue;
1062 total += p->num_objects;
1063 }
1064
1065 progress = start_progress(repo,
1066 _("Checking objects"), total);
1067 }
1068
1069 repo_for_each_pack(repo, p) {
1070 /* verify gives error messages itself */
1071 if (verify_pack(repo,
1072 p, fsck_obj_buffer, repo,
1073 progress, count))
1074 errors_found |= ERROR_PACK;
1075 count += p->num_objects;
1076 }
1077 stop_progress(&progress);
1078 }
1079
1080 if (fsck_finish(&fsck_obj_options))
1081 errors_found |= ERROR_OBJECT;
1082 }
1083
1084 /* Process the snapshotted refs and the reflogs. */
1085 process_refs(repo, &snap);
1086
1087 /* If not given any explicit objects, process index files too. */
1088 if (!argc)
1089 keep_cache_objects = 1;
1090 if (keep_cache_objects) {
1091 /*
1092 * TODO: Consider first walking these indexes in snapshot_refs,
1093 * to snapshot where the index entries used to point, and then
1094 * check those snapshotted locations here.
1095 */
1096 struct worktree **worktrees, **p;
1097
1098 verify_index_checksum = 1;
1099 verify_ce_order = 1;
1100
1101 worktrees = get_worktrees(repo);
1102 for (p = worktrees; *p; p++) {
1103 struct worktree *wt = *p;
1104 struct index_state istate =
1105 INDEX_STATE_INIT(repo);
1106 char *path, *wt_gitdir;
1107
1108 /*
1109 * Make a copy since the buffer is reusable
1110 * and may get overwritten by other calls
1111 * while we're examining the index.
1112 */
1113 path = xstrdup(worktree_git_path(wt, "index"));
1114 wt_gitdir = get_worktree_git_dir(wt);
1115
1116 read_index_from(&istate, path, wt_gitdir);
1117 fsck_index(&istate, path, wt->is_current);
1118
1119 discard_index(&istate);
1120 free(wt_gitdir);
1121 free(path);
1122 }
1123 free_worktrees(worktrees);
1124 }
1125
1126 errors_found |= check_pack_rev_indexes(repo, show_progress);
1127 if (verify_bitmap_files(repo))
1128 errors_found |= ERROR_BITMAP;
1129
1130 check_connectivity(repo);
1131
1132 if (repo->settings.core_commit_graph) {
1133 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
1134
1135 for (source = repo->objects->sources; source; source = source->next) {
1136 child_process_init(&commit_graph_verify);
1137 commit_graph_verify.git_cmd = 1;
1138 strvec_pushl(&commit_graph_verify.args, "commit-graph",
1139 "verify", "--object-dir", source->path, NULL);
1140 if (show_progress)
1141 strvec_push(&commit_graph_verify.args, "--progress");
1142 else
1143 strvec_push(&commit_graph_verify.args, "--no-progress");
1144 if (run_command(&commit_graph_verify))
1145 errors_found |= ERROR_COMMIT_GRAPH;
1146 }
1147 }
1148
1149 if (repo->settings.core_multi_pack_index) {
1150 struct child_process midx_verify = CHILD_PROCESS_INIT;
1151
1152 for (source = repo->objects->sources; source; source = source->next) {
1153 child_process_init(&midx_verify);
1154 midx_verify.git_cmd = 1;
1155 strvec_pushl(&midx_verify.args, "multi-pack-index",
1156 "verify", "--object-dir", source->path, NULL);
1157 if (show_progress)
1158 strvec_push(&midx_verify.args, "--progress");
1159 else
1160 strvec_push(&midx_verify.args, "--no-progress");
1161 if (run_command(&midx_verify))
1162 errors_found |= ERROR_MULTI_PACK_INDEX;
1163 }
1164 }
1165
1166 free_snapshot_refs(&snap);
1167 return errors_found;
1168 }