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