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