at commit 0d5ebb32

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