at commit ae7a0ffc

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