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