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
34 #ifdef NO_D_INO_IN_DIRENT
36 #define DIRENT_SORT_HINT(de) 0
39 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
42 static void objreport(struct object
*obj
, const char *severity
,
43 const char *err
, va_list params
)
45 fprintf(stderr
, "%s in %s %s: ",
46 severity
, typename(obj
->type
), sha1_to_hex(obj
->sha1
));
47 vfprintf(stderr
, err
, params
);
51 __attribute__((format (printf
, 2, 3)))
52 static int objerror(struct object
*obj
, const char *err
, ...)
55 va_start(params
, err
);
56 errors_found
|= ERROR_OBJECT
;
57 objreport(obj
, "error", err
, params
);
62 __attribute__((format (printf
, 3, 4)))
63 static int fsck_error_func(struct object
*obj
, int type
, const char *err
, ...)
66 va_start(params
, err
);
67 objreport(obj
, (type
== FSCK_WARN
) ? "warning" : "error", err
, params
);
69 return (type
== FSCK_WARN
) ? 0 : 1;
72 static struct object_array pending
;
74 static int mark_object(struct object
*obj
, int type
, void *data
)
76 struct object
*parent
= data
;
79 * The only case data is NULL or type is OBJ_ANY is when
80 * mark_object_reachable() calls us. All the callers of
81 * that function has non-NULL obj hence ...
84 /* ... these references to parent->fld are safe here */
85 printf("broken link from %7s %s\n",
86 typename(parent
->type
), sha1_to_hex(parent
->sha1
));
87 printf("broken link from %7s %s\n",
88 (type
== OBJ_ANY
? "unknown" : typename(type
)), "unknown");
89 errors_found
|= ERROR_REACHABLE
;
93 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
94 /* ... and the reference to parent is safe here */
95 objerror(parent
, "wrong object type in link");
97 if (obj
->flags
& REACHABLE
)
99 obj
->flags
|= REACHABLE
;
101 if (parent
&& !has_sha1_file(obj
->sha1
)) {
102 printf("broken link from %7s %s\n",
103 typename(parent
->type
), sha1_to_hex(parent
->sha1
));
104 printf(" to %7s %s\n",
105 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
106 errors_found
|= ERROR_REACHABLE
;
111 add_object_array(obj
, (void *) parent
, &pending
);
115 static void mark_object_reachable(struct object
*obj
)
117 mark_object(obj
, OBJ_ANY
, NULL
);
120 static int traverse_one_object(struct object
*obj
)
123 struct tree
*tree
= NULL
;
125 if (obj
->type
== OBJ_TREE
) {
127 tree
= (struct tree
*)obj
;
128 if (parse_tree(tree
) < 0)
129 return 1; /* error already displayed */
131 result
= fsck_walk(obj
, mark_object
, obj
);
139 static int traverse_reachable(void)
143 struct object_array_entry
*entry
;
146 entry
= pending
.objects
+ --pending
.nr
;
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
,
235 if (buf
&& fwrite(buf
, 1, size
, f
) != size
)
236 die_errno("Could not write '%s'", filename
);
239 fprintf(f
, "%s\n", sha1_to_hex(obj
->sha1
));
241 die_errno("Could not finish '%s'",
248 * Otherwise? It's there, it's unreachable, and some other unreachable
249 * object points to it. Ignore it - it's not interesting, and we showed
250 * all the interesting cases above.
254 static void check_object(struct object
*obj
)
257 fprintf(stderr
, "Checking %s\n", sha1_to_hex(obj
->sha1
));
259 if (obj
->flags
& REACHABLE
)
260 check_reachable_object(obj
);
262 check_unreachable_object(obj
);
265 static void check_connectivity(void)
269 /* Traverse the pending reachable objects */
270 traverse_reachable();
272 /* Look up all the requirements, warn about missing objects.. */
273 max
= get_max_object_index();
275 fprintf(stderr
, "Checking connectivity (%d objects)\n", max
);
277 for (i
= 0; i
< max
; i
++) {
278 struct object
*obj
= get_indexed_object(i
);
285 static int fsck_obj(struct object
*obj
)
287 if (obj
->flags
& SEEN
)
292 fprintf(stderr
, "Checking %s %s\n",
293 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
295 if (fsck_walk(obj
, mark_used
, NULL
))
296 objerror(obj
, "broken links");
297 if (fsck_object(obj
, check_strict
, fsck_error_func
))
300 if (obj
->type
== OBJ_TREE
) {
301 struct tree
*item
= (struct tree
*) obj
;
307 if (obj
->type
== OBJ_COMMIT
) {
308 struct commit
*commit
= (struct commit
*) obj
;
310 free(commit
->buffer
);
311 commit
->buffer
= NULL
;
313 if (!commit
->parents
&& show_root
)
314 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
317 if (obj
->type
== OBJ_TAG
) {
318 struct tag
*tag
= (struct tag
*) obj
;
320 if (show_tags
&& tag
->tagged
) {
321 printf("tagged %s %s", typename(tag
->tagged
->type
), sha1_to_hex(tag
->tagged
->sha1
));
322 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
329 static int fsck_sha1(const unsigned char *sha1
)
331 struct object
*obj
= parse_object(sha1
);
333 errors_found
|= ERROR_OBJECT
;
334 return error("%s: object corrupt or missing",
337 return fsck_obj(obj
);
340 static int fsck_obj_buffer(const unsigned char *sha1
, enum object_type type
,
341 unsigned long size
, void *buffer
, int *eaten
)
344 obj
= parse_object_buffer(sha1
, type
, size
, buffer
, eaten
);
346 errors_found
|= ERROR_OBJECT
;
347 return error("%s: object corrupt or missing", sha1_to_hex(sha1
));
349 return fsck_obj(obj
);
353 * This is the sorting chunk size: make it reasonably
354 * big so that we can sort well..
356 #define MAX_SHA1_ENTRIES (1024)
360 unsigned char sha1
[20];
365 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
368 static int ino_compare(const void *_a
, const void *_b
)
370 const struct sha1_entry
*a
= _a
, *b
= _b
;
371 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
372 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
375 static void fsck_sha1_list(void)
377 int i
, nr
= sha1_list
.nr
;
380 qsort(sha1_list
.entry
, nr
,
381 sizeof(struct sha1_entry
*), ino_compare
);
382 for (i
= 0; i
< nr
; i
++) {
383 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
384 unsigned char *sha1
= entry
->sha1
;
386 sha1_list
.entry
[i
] = NULL
;
393 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
395 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
399 hashcpy(entry
->sha1
, sha1
);
401 if (nr
== MAX_SHA1_ENTRIES
) {
405 sha1_list
.entry
[nr
] = entry
;
409 static inline int is_loose_object_file(struct dirent
*de
,
410 char *name
, unsigned char *sha1
)
412 if (strlen(de
->d_name
) != 38)
414 memcpy(name
+ 2, de
->d_name
, 39);
415 return !get_sha1_hex(name
, sha1
);
418 static void fsck_dir(int i
, char *path
)
420 DIR *dir
= opendir(path
);
428 fprintf(stderr
, "Checking directory %s\n", path
);
430 sprintf(name
, "%02x", i
);
431 while ((de
= readdir(dir
)) != NULL
) {
432 unsigned char sha1
[20];
434 if (is_dot_or_dotdot(de
->d_name
))
436 if (is_loose_object_file(de
, name
, sha1
)) {
437 add_sha1_list(sha1
, DIRENT_SORT_HINT(de
));
440 if (!prefixcmp(de
->d_name
, "tmp_obj_"))
442 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
447 static int default_refs
;
449 static int fsck_handle_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
450 const char *email
, unsigned long timestamp
, int tz
,
451 const char *message
, void *cb_data
)
456 fprintf(stderr
, "Checking reflog %s->%s\n",
457 sha1_to_hex(osha1
), sha1_to_hex(nsha1
));
459 if (!is_null_sha1(osha1
)) {
460 obj
= lookup_object(osha1
);
463 mark_object_reachable(obj
);
466 obj
= lookup_object(nsha1
);
469 mark_object_reachable(obj
);
474 static int fsck_handle_reflog(const char *logname
, const unsigned char *sha1
, int flag
, void *cb_data
)
476 for_each_reflog_ent(logname
, fsck_handle_reflog_ent
, NULL
);
480 static int is_branch(const char *refname
)
482 return !strcmp(refname
, "HEAD") || !prefixcmp(refname
, "refs/heads/");
485 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
489 obj
= parse_object(sha1
);
491 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
492 /* We'll continue with the rest despite the error.. */
495 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
))
496 error("%s: not a commit", refname
);
499 mark_object_reachable(obj
);
504 static void get_default_heads(void)
506 if (head_points_at
&& !is_null_sha1(head_sha1
))
507 fsck_handle_ref("HEAD", head_sha1
, 0, NULL
);
508 for_each_ref(fsck_handle_ref
, NULL
);
510 for_each_reflog(fsck_handle_reflog
, NULL
);
513 * Not having any default heads isn't really fatal, but
514 * it does mean that "--unreachable" no longer makes any
515 * sense (since in this case everything will obviously
516 * be unreachable by definition.
518 * Showing dangling objects is valid, though (as those
519 * dangling objects are likely lost heads).
521 * So we just print a warning about it, and clear the
522 * "show_unreachable" flag.
525 fprintf(stderr
, "notice: No default references\n");
526 show_unreachable
= 0;
530 static void fsck_object_dir(const char *path
)
535 fprintf(stderr
, "Checking object directory\n");
537 for (i
= 0; i
< 256; i
++) {
538 static char dir
[4096];
539 sprintf(dir
, "%s/%02x", path
, i
);
545 static int fsck_head_link(void)
548 int null_is_error
= 0;
551 fprintf(stderr
, "Checking HEAD link\n");
553 head_points_at
= resolve_ref("HEAD", head_sha1
, 0, &flag
);
555 return error("Invalid HEAD");
556 if (!strcmp(head_points_at
, "HEAD"))
559 else if (prefixcmp(head_points_at
, "refs/heads/"))
560 return error("HEAD points to something strange (%s)",
562 if (is_null_sha1(head_sha1
)) {
564 return error("HEAD: detached HEAD points at nothing");
565 fprintf(stderr
, "notice: HEAD points to an unborn branch (%s)\n",
566 head_points_at
+ 11);
571 static int fsck_cache_tree(struct cache_tree
*it
)
577 fprintf(stderr
, "Checking cache tree\n");
579 if (0 <= it
->entry_count
) {
580 struct object
*obj
= parse_object(it
->sha1
);
582 error("%s: invalid sha1 pointer in cache-tree",
583 sha1_to_hex(it
->sha1
));
587 mark_object_reachable(obj
);
588 if (obj
->type
!= OBJ_TREE
)
589 err
|= objerror(obj
, "non-tree in cache-tree");
591 for (i
= 0; i
< it
->subtree_nr
; i
++)
592 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
);
596 static char const * const fsck_usage
[] = {
597 "git fsck [options] [<object>...]",
601 static struct option fsck_opts
[] = {
602 OPT__VERBOSE(&verbose
, "be verbose"),
603 OPT_BOOLEAN(0, "unreachable", &show_unreachable
, "show unreachable objects"),
604 OPT_BOOLEAN(0, "tags", &show_tags
, "report tags"),
605 OPT_BOOLEAN(0, "root", &show_root
, "report root nodes"),
606 OPT_BOOLEAN(0, "cache", &keep_cache_objects
, "make index objects head nodes"),
607 OPT_BOOLEAN(0, "reflogs", &include_reflogs
, "make reflogs head nodes (default)"),
608 OPT_BOOLEAN(0, "full", &check_full
, "also consider packs and alternate objects"),
609 OPT_BOOLEAN(0, "strict", &check_strict
, "enable more strict checking"),
610 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found
,
611 "write dangling objects in .git/lost-found"),
615 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
618 struct alternate_object_database
*alt
;
621 read_replace_refs
= 0;
623 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
624 if (write_lost_and_found
) {
630 fsck_object_dir(get_object_directory());
633 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
634 char namebuf
[PATH_MAX
];
635 int namelen
= alt
->name
- alt
->base
;
636 memcpy(namebuf
, alt
->base
, namelen
);
637 namebuf
[namelen
- 1] = 0;
638 fsck_object_dir(namebuf
);
642 struct packed_git
*p
;
644 prepare_packed_git();
645 for (p
= packed_git
; p
; p
= p
->next
)
646 /* verify gives error messages itself */
647 if (verify_pack(p
, fsck_obj_buffer
))
648 errors_found
|= ERROR_PACK
;
652 for (i
= 0; i
< argc
; i
++) {
653 const char *arg
= argv
[i
];
654 unsigned char sha1
[20];
655 if (!get_sha1(arg
, sha1
)) {
656 struct object
*obj
= lookup_object(sha1
);
658 /* Error is printed by lookup_object(). */
663 mark_object_reachable(obj
);
667 error("invalid parameter: expected sha1, got '%s'", arg
);
671 * If we've not been given any explicit head information, do the
672 * default ones from .git/refs. We also consider the index file
673 * in this case (ie this implies --cache).
677 keep_cache_objects
= 1;
680 if (keep_cache_objects
) {
682 for (i
= 0; i
< active_nr
; i
++) {
687 mode
= active_cache
[i
]->ce_mode
;
688 if (S_ISGITLINK(mode
))
690 blob
= lookup_blob(active_cache
[i
]->sha1
);
695 mark_object_reachable(obj
);
697 if (active_cache_tree
)
698 fsck_cache_tree(active_cache_tree
);
701 check_connectivity();