at commit 1bbb92d5

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