9 #include "cache-tree.h"
10 #include "tree-walk.h"
12 #include "parse-options.h"
15 #define REACHABLE 0x0001
20 static int show_unreachable
;
21 static int include_reflogs
= 1;
22 static int check_full
= 1;
23 static int check_strict
;
24 static int keep_cache_objects
;
25 static unsigned char head_sha1
[20];
26 static const char *head_points_at
;
27 static int errors_found
;
28 static int write_lost_and_found
;
30 #define ERROR_OBJECT 01
31 #define ERROR_REACHABLE 02
33 #ifdef NO_D_INO_IN_DIRENT
35 #define DIRENT_SORT_HINT(de) 0
38 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
41 static void objreport(struct object
*obj
, const char *severity
,
42 const char *err
, va_list params
)
44 fprintf(stderr
, "%s in %s %s: ",
45 severity
, typename(obj
->type
), sha1_to_hex(obj
->sha1
));
46 vfprintf(stderr
, err
, params
);
50 __attribute__((format (printf
, 2, 3)))
51 static int objerror(struct object
*obj
, const char *err
, ...)
54 va_start(params
, err
);
55 errors_found
|= ERROR_OBJECT
;
56 objreport(obj
, "error", err
, params
);
61 __attribute__((format (printf
, 3, 4)))
62 static int fsck_error_func(struct object
*obj
, int type
, const char *err
, ...)
65 va_start(params
, err
);
66 objreport(obj
, (type
== FSCK_WARN
) ? "warning" : "error", err
, params
);
68 return (type
== FSCK_WARN
) ? 0 : 1;
71 static struct object_array pending
;
73 static int mark_object(struct object
*obj
, int type
, void *data
)
75 struct object
*parent
= data
;
78 * The only case data is NULL or type is OBJ_ANY is when
79 * mark_object_reachable() calls us. All the callers of
80 * that function has non-NULL obj hence ...
83 /* ... these references to parent->fld are safe here */
84 printf("broken link from %7s %s\n",
85 typename(parent
->type
), sha1_to_hex(parent
->sha1
));
86 printf("broken link from %7s %s\n",
87 (type
== OBJ_ANY
? "unknown" : typename(type
)), "unknown");
88 errors_found
|= ERROR_REACHABLE
;
92 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
93 /* ... and the reference to parent is safe here */
94 objerror(parent
, "wrong object type in link");
96 if (obj
->flags
& REACHABLE
)
98 obj
->flags
|= REACHABLE
;
100 if (parent
&& !has_sha1_file(obj
->sha1
)) {
101 printf("broken link from %7s %s\n",
102 typename(parent
->type
), sha1_to_hex(parent
->sha1
));
103 printf(" to %7s %s\n",
104 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
105 errors_found
|= ERROR_REACHABLE
;
110 add_object_array(obj
, (void *) parent
, &pending
);
114 static void mark_object_reachable(struct object
*obj
)
116 mark_object(obj
, OBJ_ANY
, NULL
);
119 static int traverse_one_object(struct object
*obj
)
122 struct tree
*tree
= NULL
;
124 if (obj
->type
== OBJ_TREE
) {
126 tree
= (struct tree
*)obj
;
127 if (parse_tree(tree
) < 0)
128 return 1; /* error already displayed */
130 result
= fsck_walk(obj
, mark_object
, obj
);
138 static int traverse_reachable(void)
142 struct object_array_entry
*entry
;
143 struct object
*obj
, *parent
;
145 entry
= pending
.objects
+ --pending
.nr
;
147 parent
= (struct object
*) entry
->name
;
148 result
|= traverse_one_object(obj
);
153 static int mark_used(struct object
*obj
, int type
, void *data
)
162 * Check a single reachable object
164 static void check_reachable_object(struct object
*obj
)
167 * We obviously want the object to be parsed,
168 * except if it was in a pack-file and we didn't
172 if (has_sha1_pack(obj
->sha1
))
173 return; /* it is in pack - forget about it */
174 printf("missing %s %s\n", typename(obj
->type
), sha1_to_hex(obj
->sha1
));
175 errors_found
|= ERROR_REACHABLE
;
181 * Check a single unreachable object
183 static void check_unreachable_object(struct object
*obj
)
186 * Missing unreachable object? Ignore it. It's not like
187 * we miss it (since it can't be reached), nor do we want
188 * to complain about it being unreachable (since it does
195 * Unreachable object that exists? Show it if asked to,
196 * since this is something that is prunable.
198 if (show_unreachable
) {
199 printf("unreachable %s %s\n", typename(obj
->type
), sha1_to_hex(obj
->sha1
));
204 * "!used" means that nothing at all points to it, including
205 * other unreachable objects. In other words, it's the "tip"
206 * of some set of unreachable objects, usually a commit that
209 * Such starting points are more interesting than some random
210 * set of unreachable objects, so we show them even if the user
211 * hasn't asked for _all_ unreachable objects. If you have
212 * deleted a branch by mistake, this is a prime candidate to
213 * start looking at, for example.
216 printf("dangling %s %s\n", typename(obj
->type
),
217 sha1_to_hex(obj
->sha1
));
218 if (write_lost_and_found
) {
219 char *filename
= git_path("lost-found/%s/%s",
220 obj
->type
== OBJ_COMMIT
? "commit" : "other",
221 sha1_to_hex(obj
->sha1
));
224 if (safe_create_leading_directories(filename
)) {
225 error("Could not create lost-found");
228 if (!(f
= fopen(filename
, "w")))
229 die_errno("Could not open '%s'", filename
);
230 if (obj
->type
== OBJ_BLOB
) {
231 enum object_type type
;
233 char *buf
= read_sha1_file(obj
->sha1
,
236 if (fwrite(buf
, size
, 1, f
) != 1)
237 die_errno("Could not write '%s'",
242 fprintf(f
, "%s\n", sha1_to_hex(obj
->sha1
));
244 die_errno("Could not finish '%s'",
251 * Otherwise? It's there, it's unreachable, and some other unreachable
252 * object points to it. Ignore it - it's not interesting, and we showed
253 * all the interesting cases above.
257 static void check_object(struct object
*obj
)
260 fprintf(stderr
, "Checking %s\n", sha1_to_hex(obj
->sha1
));
262 if (obj
->flags
& REACHABLE
)
263 check_reachable_object(obj
);
265 check_unreachable_object(obj
);
268 static void check_connectivity(void)
272 /* Traverse the pending reachable objects */
273 traverse_reachable();
275 /* Look up all the requirements, warn about missing objects.. */
276 max
= get_max_object_index();
278 fprintf(stderr
, "Checking connectivity (%d objects)\n", max
);
280 for (i
= 0; i
< max
; i
++) {
281 struct object
*obj
= get_indexed_object(i
);
288 static int fsck_sha1(const unsigned char *sha1
)
290 struct object
*obj
= parse_object(sha1
);
292 errors_found
|= ERROR_OBJECT
;
293 return error("%s: object corrupt or missing",
296 if (obj
->flags
& SEEN
)
301 fprintf(stderr
, "Checking %s %s\n",
302 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
304 if (fsck_walk(obj
, mark_used
, NULL
))
305 objerror(obj
, "broken links");
306 if (fsck_object(obj
, check_strict
, fsck_error_func
))
309 if (obj
->type
== OBJ_TREE
) {
310 struct tree
*item
= (struct tree
*) obj
;
316 if (obj
->type
== OBJ_COMMIT
) {
317 struct commit
*commit
= (struct commit
*) obj
;
319 free(commit
->buffer
);
320 commit
->buffer
= NULL
;
322 if (!commit
->parents
&& show_root
)
323 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
326 if (obj
->type
== OBJ_TAG
) {
327 struct tag
*tag
= (struct tag
*) obj
;
329 if (show_tags
&& tag
->tagged
) {
330 printf("tagged %s %s", typename(tag
->tagged
->type
), sha1_to_hex(tag
->tagged
->sha1
));
331 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
339 * This is the sorting chunk size: make it reasonably
340 * big so that we can sort well..
342 #define MAX_SHA1_ENTRIES (1024)
346 unsigned char sha1
[20];
351 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
354 static int ino_compare(const void *_a
, const void *_b
)
356 const struct sha1_entry
*a
= _a
, *b
= _b
;
357 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
358 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
361 static void fsck_sha1_list(void)
363 int i
, nr
= sha1_list
.nr
;
366 qsort(sha1_list
.entry
, nr
,
367 sizeof(struct sha1_entry
*), ino_compare
);
368 for (i
= 0; i
< nr
; i
++) {
369 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
370 unsigned char *sha1
= entry
->sha1
;
372 sha1_list
.entry
[i
] = NULL
;
379 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
381 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
385 hashcpy(entry
->sha1
, sha1
);
387 if (nr
== MAX_SHA1_ENTRIES
) {
391 sha1_list
.entry
[nr
] = entry
;
395 static inline int is_loose_object_file(struct dirent
*de
,
396 char *name
, unsigned char *sha1
)
398 if (strlen(de
->d_name
) != 38)
400 memcpy(name
+ 2, de
->d_name
, 39);
401 return !get_sha1_hex(name
, sha1
);
404 static void fsck_dir(int i
, char *path
)
406 DIR *dir
= opendir(path
);
414 fprintf(stderr
, "Checking directory %s\n", path
);
416 sprintf(name
, "%02x", i
);
417 while ((de
= readdir(dir
)) != NULL
) {
418 unsigned char sha1
[20];
420 if (is_dot_or_dotdot(de
->d_name
))
422 if (is_loose_object_file(de
, name
, sha1
)) {
423 add_sha1_list(sha1
, DIRENT_SORT_HINT(de
));
426 if (!prefixcmp(de
->d_name
, "tmp_obj_"))
428 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
433 static int default_refs
;
435 static int fsck_handle_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
436 const char *email
, unsigned long timestamp
, int tz
,
437 const char *message
, void *cb_data
)
442 fprintf(stderr
, "Checking reflog %s->%s\n",
443 sha1_to_hex(osha1
), sha1_to_hex(nsha1
));
445 if (!is_null_sha1(osha1
)) {
446 obj
= lookup_object(osha1
);
449 mark_object_reachable(obj
);
452 obj
= lookup_object(nsha1
);
455 mark_object_reachable(obj
);
460 static int fsck_handle_reflog(const char *logname
, const unsigned char *sha1
, int flag
, void *cb_data
)
462 for_each_reflog_ent(logname
, fsck_handle_reflog_ent
, NULL
);
466 static int is_branch(const char *refname
)
468 return !strcmp(refname
, "HEAD") || !prefixcmp(refname
, "refs/heads/");
471 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
475 obj
= parse_object(sha1
);
477 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
478 /* We'll continue with the rest despite the error.. */
481 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
))
482 error("%s: not a commit", refname
);
485 mark_object_reachable(obj
);
490 static void get_default_heads(void)
492 if (head_points_at
&& !is_null_sha1(head_sha1
))
493 fsck_handle_ref("HEAD", head_sha1
, 0, NULL
);
494 for_each_ref(fsck_handle_ref
, NULL
);
496 for_each_reflog(fsck_handle_reflog
, NULL
);
499 * Not having any default heads isn't really fatal, but
500 * it does mean that "--unreachable" no longer makes any
501 * sense (since in this case everything will obviously
502 * be unreachable by definition.
504 * Showing dangling objects is valid, though (as those
505 * dangling objects are likely lost heads).
507 * So we just print a warning about it, and clear the
508 * "show_unreachable" flag.
511 fprintf(stderr
, "notice: No default references\n");
512 show_unreachable
= 0;
516 static void fsck_object_dir(const char *path
)
521 fprintf(stderr
, "Checking object directory\n");
523 for (i
= 0; i
< 256; i
++) {
524 static char dir
[4096];
525 sprintf(dir
, "%s/%02x", path
, i
);
531 static int fsck_head_link(void)
534 int null_is_error
= 0;
537 fprintf(stderr
, "Checking HEAD link\n");
539 head_points_at
= resolve_ref("HEAD", head_sha1
, 0, &flag
);
541 return error("Invalid HEAD");
542 if (!strcmp(head_points_at
, "HEAD"))
545 else if (prefixcmp(head_points_at
, "refs/heads/"))
546 return error("HEAD points to something strange (%s)",
548 if (is_null_sha1(head_sha1
)) {
550 return error("HEAD: detached HEAD points at nothing");
551 fprintf(stderr
, "notice: HEAD points to an unborn branch (%s)\n",
552 head_points_at
+ 11);
557 static int fsck_cache_tree(struct cache_tree
*it
)
563 fprintf(stderr
, "Checking cache tree\n");
565 if (0 <= it
->entry_count
) {
566 struct object
*obj
= parse_object(it
->sha1
);
568 error("%s: invalid sha1 pointer in cache-tree",
569 sha1_to_hex(it
->sha1
));
573 mark_object_reachable(obj
);
574 if (obj
->type
!= OBJ_TREE
)
575 err
|= objerror(obj
, "non-tree in cache-tree");
577 for (i
= 0; i
< it
->subtree_nr
; i
++)
578 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
);
582 static char const * const fsck_usage
[] = {
583 "git fsck [options] [<object>...]",
587 static struct option fsck_opts
[] = {
588 OPT__VERBOSE(&verbose
, "be verbose"),
589 OPT_BOOLEAN(0, "unreachable", &show_unreachable
, "show unreachable objects"),
590 OPT_BOOLEAN(0, "tags", &show_tags
, "report tags"),
591 OPT_BOOLEAN(0, "root", &show_root
, "report root nodes"),
592 OPT_BOOLEAN(0, "cache", &keep_cache_objects
, "make index objects head nodes"),
593 OPT_BOOLEAN(0, "reflogs", &include_reflogs
, "make reflogs head nodes (default)"),
594 OPT_BOOLEAN(0, "full", &check_full
, "also consider packs and alternate objects"),
595 OPT_BOOLEAN(0, "strict", &check_strict
, "enable more strict checking"),
596 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found
,
597 "write dangling objects in .git/lost-found"),
601 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
604 struct alternate_object_database
*alt
;
607 read_replace_refs
= 0;
609 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
610 if (write_lost_and_found
) {
616 fsck_object_dir(get_object_directory());
619 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
620 char namebuf
[PATH_MAX
];
621 int namelen
= alt
->name
- alt
->base
;
622 memcpy(namebuf
, alt
->base
, namelen
);
623 namebuf
[namelen
- 1] = 0;
624 fsck_object_dir(namebuf
);
628 struct packed_git
*p
;
630 prepare_packed_git();
631 for (p
= packed_git
; p
; p
= p
->next
)
632 /* verify gives error messages itself */
635 for (p
= packed_git
; p
; p
= p
->next
) {
637 if (open_pack_index(p
))
639 num
= p
->num_objects
;
640 for (j
= 0; j
< num
; j
++)
641 fsck_sha1(nth_packed_object_sha1(p
, j
));
646 for (i
= 0; i
< argc
; i
++) {
647 const char *arg
= argv
[i
];
648 unsigned char sha1
[20];
649 if (!get_sha1(arg
, sha1
)) {
650 struct object
*obj
= lookup_object(sha1
);
652 /* Error is printed by lookup_object(). */
657 mark_object_reachable(obj
);
661 error("invalid parameter: expected sha1, got '%s'", arg
);
665 * If we've not been given any explicit head information, do the
666 * default ones from .git/refs. We also consider the index file
667 * in this case (ie this implies --cache).
671 keep_cache_objects
= 1;
674 if (keep_cache_objects
) {
676 for (i
= 0; i
< active_nr
; i
++) {
681 mode
= active_cache
[i
]->ce_mode
;
682 if (S_ISGITLINK(mode
))
684 blob
= lookup_blob(active_cache
[i
]->sha1
);
689 mark_object_reachable(obj
);
691 if (active_cache_tree
)
692 fsck_cache_tree(active_cache_tree
);
695 check_connectivity();