9 #include "cache-tree.h"
10 #include "tree-walk.h"
12 #include "parse-options.h"
15 #include "streaming.h"
17 #define REACHABLE 0x0001
19 #define HAS_OBJ 0x0004
23 static int show_unreachable
;
24 static int include_reflogs
= 1;
25 static int check_full
= 1;
26 static int check_strict
;
27 static int keep_cache_objects
;
28 static unsigned char head_sha1
[20];
29 static const char *head_points_at
;
30 static int errors_found
;
31 static int write_lost_and_found
;
33 static int show_progress
= -1;
34 static int show_dangling
= 1;
35 #define ERROR_OBJECT 01
36 #define ERROR_REACHABLE 02
39 #ifdef NO_D_INO_IN_DIRENT
41 #define DIRENT_SORT_HINT(de) 0
44 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
47 static void objreport(struct object
*obj
, const char *severity
,
48 const char *err
, va_list params
)
50 fprintf(stderr
, "%s in %s %s: ",
51 severity
, typename(obj
->type
), sha1_to_hex(obj
->sha1
));
52 vfprintf(stderr
, err
, params
);
56 __attribute__((format (printf
, 2, 3)))
57 static int objerror(struct object
*obj
, const char *err
, ...)
60 va_start(params
, err
);
61 errors_found
|= ERROR_OBJECT
;
62 objreport(obj
, "error", err
, params
);
67 __attribute__((format (printf
, 3, 4)))
68 static int fsck_error_func(struct object
*obj
, int type
, const char *err
, ...)
71 va_start(params
, err
);
72 objreport(obj
, (type
== FSCK_WARN
) ? "warning" : "error", err
, params
);
74 return (type
== FSCK_WARN
) ? 0 : 1;
77 static struct object_array pending
;
79 static int mark_object(struct object
*obj
, int type
, void *data
)
81 struct object
*parent
= data
;
84 * The only case data is NULL or type is OBJ_ANY is when
85 * mark_object_reachable() calls us. All the callers of
86 * that function has non-NULL obj hence ...
89 /* ... these references to parent->fld are safe here */
90 printf("broken link from %7s %s\n",
91 typename(parent
->type
), sha1_to_hex(parent
->sha1
));
92 printf("broken link from %7s %s\n",
93 (type
== OBJ_ANY
? "unknown" : typename(type
)), "unknown");
94 errors_found
|= ERROR_REACHABLE
;
98 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
99 /* ... and the reference to parent is safe here */
100 objerror(parent
, "wrong object type in link");
102 if (obj
->flags
& REACHABLE
)
104 obj
->flags
|= REACHABLE
;
105 if (!(obj
->flags
& HAS_OBJ
)) {
106 if (parent
&& !has_sha1_file(obj
->sha1
)) {
107 printf("broken link from %7s %s\n",
108 typename(parent
->type
), sha1_to_hex(parent
->sha1
));
109 printf(" to %7s %s\n",
110 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
111 errors_found
|= ERROR_REACHABLE
;
116 add_object_array(obj
, NULL
, &pending
);
120 static void mark_object_reachable(struct object
*obj
)
122 mark_object(obj
, OBJ_ANY
, NULL
);
125 static int traverse_one_object(struct object
*obj
)
128 struct tree
*tree
= NULL
;
130 if (obj
->type
== OBJ_TREE
) {
131 tree
= (struct tree
*)obj
;
132 if (parse_tree(tree
) < 0)
133 return 1; /* error already displayed */
135 result
= fsck_walk(obj
, mark_object
, obj
);
137 free_tree_buffer(tree
);
141 static int traverse_reachable(void)
143 struct progress
*progress
= NULL
;
147 progress
= start_progress_delay(_("Checking connectivity"), 0, 0, 2);
149 struct object_array_entry
*entry
;
152 entry
= pending
.objects
+ --pending
.nr
;
154 result
|= traverse_one_object(obj
);
155 display_progress(progress
, ++nr
);
157 stop_progress(&progress
);
161 static int mark_used(struct object
*obj
, int type
, void *data
)
170 * Check a single reachable object
172 static void check_reachable_object(struct object
*obj
)
175 * We obviously want the object to be parsed,
176 * except if it was in a pack-file and we didn't
179 if (!(obj
->flags
& HAS_OBJ
)) {
180 if (has_sha1_pack(obj
->sha1
))
181 return; /* it is in pack - forget about it */
182 printf("missing %s %s\n", typename(obj
->type
), sha1_to_hex(obj
->sha1
));
183 errors_found
|= ERROR_REACHABLE
;
189 * Check a single unreachable object
191 static void check_unreachable_object(struct object
*obj
)
194 * Missing unreachable object? Ignore it. It's not like
195 * we miss it (since it can't be reached), nor do we want
196 * to complain about it being unreachable (since it does
203 * Unreachable object that exists? Show it if asked to,
204 * since this is something that is prunable.
206 if (show_unreachable
) {
207 printf("unreachable %s %s\n", typename(obj
->type
), sha1_to_hex(obj
->sha1
));
212 * "!used" means that nothing at all points to it, including
213 * other unreachable objects. In other words, it's the "tip"
214 * of some set of unreachable objects, usually a commit that
217 * Such starting points are more interesting than some random
218 * set of unreachable objects, so we show them even if the user
219 * hasn't asked for _all_ unreachable objects. If you have
220 * deleted a branch by mistake, this is a prime candidate to
221 * start looking at, for example.
225 printf("dangling %s %s\n", typename(obj
->type
),
226 sha1_to_hex(obj
->sha1
));
227 if (write_lost_and_found
) {
228 char *filename
= git_path("lost-found/%s/%s",
229 obj
->type
== OBJ_COMMIT
? "commit" : "other",
230 sha1_to_hex(obj
->sha1
));
233 if (safe_create_leading_directories(filename
)) {
234 error("Could not create lost-found");
237 if (!(f
= fopen(filename
, "w")))
238 die_errno("Could not open '%s'", filename
);
239 if (obj
->type
== OBJ_BLOB
) {
240 if (stream_blob_to_fd(fileno(f
), obj
->sha1
, NULL
, 1))
241 die_errno("Could not write '%s'", filename
);
243 fprintf(f
, "%s\n", sha1_to_hex(obj
->sha1
));
245 die_errno("Could not finish '%s'",
252 * Otherwise? It's there, it's unreachable, and some other unreachable
253 * object points to it. Ignore it - it's not interesting, and we showed
254 * all the interesting cases above.
258 static void check_object(struct object
*obj
)
261 fprintf(stderr
, "Checking %s\n", sha1_to_hex(obj
->sha1
));
263 if (obj
->flags
& REACHABLE
)
264 check_reachable_object(obj
);
266 check_unreachable_object(obj
);
269 static void check_connectivity(void)
273 /* Traverse the pending reachable objects */
274 traverse_reachable();
276 /* Look up all the requirements, warn about missing objects.. */
277 max
= get_max_object_index();
279 fprintf(stderr
, "Checking connectivity (%d objects)\n", max
);
281 for (i
= 0; i
< max
; i
++) {
282 struct object
*obj
= get_indexed_object(i
);
289 static int fsck_obj(struct object
*obj
)
291 if (obj
->flags
& SEEN
)
296 fprintf(stderr
, "Checking %s %s\n",
297 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
299 if (fsck_walk(obj
, mark_used
, NULL
))
300 objerror(obj
, "broken links");
301 if (fsck_object(obj
, check_strict
, fsck_error_func
))
304 if (obj
->type
== OBJ_TREE
) {
305 struct tree
*item
= (struct tree
*) obj
;
307 free_tree_buffer(item
);
310 if (obj
->type
== OBJ_COMMIT
) {
311 struct commit
*commit
= (struct commit
*) obj
;
313 free(commit
->buffer
);
314 commit
->buffer
= NULL
;
316 if (!commit
->parents
&& show_root
)
317 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
320 if (obj
->type
== OBJ_TAG
) {
321 struct tag
*tag
= (struct tag
*) obj
;
323 if (show_tags
&& tag
->tagged
) {
324 printf("tagged %s %s", typename(tag
->tagged
->type
), sha1_to_hex(tag
->tagged
->sha1
));
325 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
332 static int fsck_sha1(const unsigned char *sha1
)
334 struct object
*obj
= parse_object(sha1
);
336 errors_found
|= ERROR_OBJECT
;
337 return error("%s: object corrupt or missing",
340 obj
->flags
|= HAS_OBJ
;
341 return fsck_obj(obj
);
344 static int fsck_obj_buffer(const unsigned char *sha1
, enum object_type type
,
345 unsigned long size
, void *buffer
, int *eaten
)
348 obj
= parse_object_buffer(sha1
, type
, size
, buffer
, eaten
);
350 errors_found
|= ERROR_OBJECT
;
351 return error("%s: object corrupt or missing", sha1_to_hex(sha1
));
353 obj
->flags
= HAS_OBJ
;
354 return fsck_obj(obj
);
358 * This is the sorting chunk size: make it reasonably
359 * big so that we can sort well..
361 #define MAX_SHA1_ENTRIES (1024)
365 unsigned char sha1
[20];
370 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
373 static int ino_compare(const void *_a
, const void *_b
)
375 const struct sha1_entry
*a
= _a
, *b
= _b
;
376 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
377 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
380 static void fsck_sha1_list(void)
382 int i
, nr
= sha1_list
.nr
;
385 qsort(sha1_list
.entry
, nr
,
386 sizeof(struct sha1_entry
*), ino_compare
);
387 for (i
= 0; i
< nr
; i
++) {
388 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
389 unsigned char *sha1
= entry
->sha1
;
391 sha1_list
.entry
[i
] = NULL
;
398 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
400 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
404 hashcpy(entry
->sha1
, sha1
);
406 if (nr
== MAX_SHA1_ENTRIES
) {
410 sha1_list
.entry
[nr
] = entry
;
414 static inline int is_loose_object_file(struct dirent
*de
,
415 char *name
, unsigned char *sha1
)
417 if (strlen(de
->d_name
) != 38)
419 memcpy(name
+ 2, de
->d_name
, 39);
420 return !get_sha1_hex(name
, sha1
);
423 static void fsck_dir(int i
, char *path
)
425 DIR *dir
= opendir(path
);
433 fprintf(stderr
, "Checking directory %s\n", path
);
435 sprintf(name
, "%02x", i
);
436 while ((de
= readdir(dir
)) != NULL
) {
437 unsigned char sha1
[20];
439 if (is_dot_or_dotdot(de
->d_name
))
441 if (is_loose_object_file(de
, name
, sha1
)) {
442 add_sha1_list(sha1
, DIRENT_SORT_HINT(de
));
445 if (starts_with(de
->d_name
, "tmp_obj_"))
447 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
452 static int default_refs
;
454 static int fsck_handle_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
455 const char *email
, unsigned long timestamp
, int tz
,
456 const char *message
, void *cb_data
)
461 fprintf(stderr
, "Checking reflog %s->%s\n",
462 sha1_to_hex(osha1
), sha1_to_hex(nsha1
));
464 if (!is_null_sha1(osha1
)) {
465 obj
= lookup_object(osha1
);
468 mark_object_reachable(obj
);
471 obj
= lookup_object(nsha1
);
474 mark_object_reachable(obj
);
479 static int fsck_handle_reflog(const char *logname
, const unsigned char *sha1
, int flag
, void *cb_data
)
481 for_each_reflog_ent(logname
, fsck_handle_reflog_ent
, NULL
);
485 static int is_branch(const char *refname
)
487 return !strcmp(refname
, "HEAD") || starts_with(refname
, "refs/heads/");
490 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
494 obj
= parse_object(sha1
);
496 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
497 /* We'll continue with the rest despite the error.. */
500 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
))
501 error("%s: not a commit", refname
);
504 mark_object_reachable(obj
);
509 static void get_default_heads(void)
511 if (head_points_at
&& !is_null_sha1(head_sha1
))
512 fsck_handle_ref("HEAD", head_sha1
, 0, NULL
);
513 for_each_ref(fsck_handle_ref
, NULL
);
515 for_each_reflog(fsck_handle_reflog
, NULL
);
518 * Not having any default heads isn't really fatal, but
519 * it does mean that "--unreachable" no longer makes any
520 * sense (since in this case everything will obviously
521 * be unreachable by definition.
523 * Showing dangling objects is valid, though (as those
524 * dangling objects are likely lost heads).
526 * So we just print a warning about it, and clear the
527 * "show_unreachable" flag.
530 fprintf(stderr
, "notice: No default references\n");
531 show_unreachable
= 0;
535 static void fsck_object_dir(const char *path
)
538 struct progress
*progress
= NULL
;
541 fprintf(stderr
, "Checking object directory\n");
544 progress
= start_progress(_("Checking object directories"), 256);
545 for (i
= 0; i
< 256; i
++) {
546 static char dir
[4096];
547 sprintf(dir
, "%s/%02x", path
, i
);
549 display_progress(progress
, i
+1);
551 stop_progress(&progress
);
555 static int fsck_head_link(void)
558 int null_is_error
= 0;
561 fprintf(stderr
, "Checking HEAD link\n");
563 head_points_at
= resolve_ref_unsafe("HEAD", head_sha1
, 0, &flag
);
565 return error("Invalid HEAD");
566 if (!strcmp(head_points_at
, "HEAD"))
569 else if (!starts_with(head_points_at
, "refs/heads/"))
570 return error("HEAD points to something strange (%s)",
572 if (is_null_sha1(head_sha1
)) {
574 return error("HEAD: detached HEAD points at nothing");
575 fprintf(stderr
, "notice: HEAD points to an unborn branch (%s)\n",
576 head_points_at
+ 11);
581 static int fsck_cache_tree(struct cache_tree
*it
)
587 fprintf(stderr
, "Checking cache tree\n");
589 if (0 <= it
->entry_count
) {
590 struct object
*obj
= parse_object(it
->sha1
);
592 error("%s: invalid sha1 pointer in cache-tree",
593 sha1_to_hex(it
->sha1
));
597 mark_object_reachable(obj
);
598 if (obj
->type
!= OBJ_TREE
)
599 err
|= objerror(obj
, "non-tree in cache-tree");
601 for (i
= 0; i
< it
->subtree_nr
; i
++)
602 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
);
606 static char const * const fsck_usage
[] = {
607 N_("git fsck [options] [<object>...]"),
611 static struct option fsck_opts
[] = {
612 OPT__VERBOSE(&verbose
, N_("be verbose")),
613 OPT_BOOL(0, "unreachable", &show_unreachable
, N_("show unreachable objects")),
614 OPT_BOOL(0, "dangling", &show_dangling
, N_("show dangling objects")),
615 OPT_BOOL(0, "tags", &show_tags
, N_("report tags")),
616 OPT_BOOL(0, "root", &show_root
, N_("report root nodes")),
617 OPT_BOOL(0, "cache", &keep_cache_objects
, N_("make index objects head nodes")),
618 OPT_BOOL(0, "reflogs", &include_reflogs
, N_("make reflogs head nodes (default)")),
619 OPT_BOOL(0, "full", &check_full
, N_("also consider packs and alternate objects")),
620 OPT_BOOL(0, "strict", &check_strict
, N_("enable more strict checking")),
621 OPT_BOOL(0, "lost-found", &write_lost_and_found
,
622 N_("write dangling objects in .git/lost-found")),
623 OPT_BOOL(0, "progress", &show_progress
, N_("show progress")),
627 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
630 struct alternate_object_database
*alt
;
633 check_replace_refs
= 0;
635 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
637 if (show_progress
== -1)
638 show_progress
= isatty(2);
642 if (write_lost_and_found
) {
648 fsck_object_dir(get_object_directory());
651 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
652 char namebuf
[PATH_MAX
];
653 int namelen
= alt
->name
- alt
->base
;
654 memcpy(namebuf
, alt
->base
, namelen
);
655 namebuf
[namelen
- 1] = 0;
656 fsck_object_dir(namebuf
);
660 struct packed_git
*p
;
661 uint32_t total
= 0, count
= 0;
662 struct progress
*progress
= NULL
;
664 prepare_packed_git();
667 for (p
= packed_git
; p
; p
= p
->next
) {
668 if (open_pack_index(p
))
670 total
+= p
->num_objects
;
673 progress
= start_progress(_("Checking objects"), total
);
675 for (p
= packed_git
; p
; p
= p
->next
) {
676 /* verify gives error messages itself */
677 if (verify_pack(p
, fsck_obj_buffer
,
679 errors_found
|= ERROR_PACK
;
680 count
+= p
->num_objects
;
682 stop_progress(&progress
);
686 for (i
= 0; i
< argc
; i
++) {
687 const char *arg
= argv
[i
];
688 unsigned char sha1
[20];
689 if (!get_sha1(arg
, sha1
)) {
690 struct object
*obj
= lookup_object(sha1
);
692 /* Error is printed by lookup_object(). */
697 mark_object_reachable(obj
);
701 error("invalid parameter: expected sha1, got '%s'", arg
);
705 * If we've not been given any explicit head information, do the
706 * default ones from .git/refs. We also consider the index file
707 * in this case (ie this implies --cache).
711 keep_cache_objects
= 1;
714 if (keep_cache_objects
) {
716 for (i
= 0; i
< active_nr
; i
++) {
721 mode
= active_cache
[i
]->ce_mode
;
722 if (S_ISGITLINK(mode
))
724 blob
= lookup_blob(active_cache
[i
]->sha1
);
729 mark_object_reachable(obj
);
731 if (active_cache_tree
)
732 fsck_cache_tree(active_cache_tree
);
735 check_connectivity();