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