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