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