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 printf("broken link from %7s %s\n",
79 typename(parent
->type
), sha1_to_hex(parent
->sha1
));
80 printf("broken link from %7s %s\n",
81 (type
== OBJ_ANY
? "unknown" : typename(type
)), "unknown");
82 errors_found
|= ERROR_REACHABLE
;
86 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
87 objerror(parent
, "wrong object type in link");
89 if (obj
->flags
& REACHABLE
)
91 obj
->flags
|= REACHABLE
;
93 if (parent
&& !has_sha1_file(obj
->sha1
)) {
94 printf("broken link from %7s %s\n",
95 typename(parent
->type
), sha1_to_hex(parent
->sha1
));
96 printf(" to %7s %s\n",
97 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
98 errors_found
|= ERROR_REACHABLE
;
103 add_object_array(obj
, (void *) parent
, &pending
);
107 static void mark_object_reachable(struct object
*obj
)
109 mark_object(obj
, OBJ_ANY
, NULL
);
112 static int traverse_one_object(struct object
*obj
, struct object
*parent
)
115 struct tree
*tree
= NULL
;
117 if (obj
->type
== OBJ_TREE
) {
119 tree
= (struct tree
*)obj
;
120 if (parse_tree(tree
) < 0)
121 return 1; /* error already displayed */
123 result
= fsck_walk(obj
, mark_object
, obj
);
131 static int traverse_reachable(void)
135 struct object_array_entry
*entry
;
136 struct object
*obj
, *parent
;
138 entry
= pending
.objects
+ --pending
.nr
;
140 parent
= (struct object
*) entry
->name
;
141 result
|= traverse_one_object(obj
, parent
);
146 static int mark_used(struct object
*obj
, int type
, void *data
)
155 * Check a single reachable object
157 static void check_reachable_object(struct object
*obj
)
160 * We obviously want the object to be parsed,
161 * except if it was in a pack-file and we didn't
165 if (has_sha1_pack(obj
->sha1
))
166 return; /* it is in pack - forget about it */
167 printf("missing %s %s\n", typename(obj
->type
), sha1_to_hex(obj
->sha1
));
168 errors_found
|= ERROR_REACHABLE
;
174 * Check a single unreachable object
176 static void check_unreachable_object(struct object
*obj
)
179 * Missing unreachable object? Ignore it. It's not like
180 * we miss it (since it can't be reached), nor do we want
181 * to complain about it being unreachable (since it does
188 * Unreachable object that exists? Show it if asked to,
189 * since this is something that is prunable.
191 if (show_unreachable
) {
192 printf("unreachable %s %s\n", typename(obj
->type
), sha1_to_hex(obj
->sha1
));
197 * "!used" means that nothing at all points to it, including
198 * other unreachable objects. In other words, it's the "tip"
199 * of some set of unreachable objects, usually a commit that
202 * Such starting points are more interesting than some random
203 * set of unreachable objects, so we show them even if the user
204 * hasn't asked for _all_ unreachable objects. If you have
205 * deleted a branch by mistake, this is a prime candidate to
206 * start looking at, for example.
209 printf("dangling %s %s\n", typename(obj
->type
),
210 sha1_to_hex(obj
->sha1
));
211 if (write_lost_and_found
) {
212 char *filename
= git_path("lost-found/%s/%s",
213 obj
->type
== OBJ_COMMIT
? "commit" : "other",
214 sha1_to_hex(obj
->sha1
));
217 if (safe_create_leading_directories(filename
)) {
218 error("Could not create lost-found");
221 if (!(f
= fopen(filename
, "w")))
222 die_errno("Could not open '%s'", filename
);
223 if (obj
->type
== OBJ_BLOB
) {
224 enum object_type type
;
226 char *buf
= read_sha1_file(obj
->sha1
,
229 if (fwrite(buf
, size
, 1, f
) != 1)
230 die_errno("Could not write '%s'",
235 fprintf(f
, "%s\n", sha1_to_hex(obj
->sha1
));
237 die_errno("Could not finish '%s'",
244 * Otherwise? It's there, it's unreachable, and some other unreachable
245 * object points to it. Ignore it - it's not interesting, and we showed
246 * all the interesting cases above.
250 static void check_object(struct object
*obj
)
253 fprintf(stderr
, "Checking %s\n", sha1_to_hex(obj
->sha1
));
255 if (obj
->flags
& REACHABLE
)
256 check_reachable_object(obj
);
258 check_unreachable_object(obj
);
261 static void check_connectivity(void)
265 /* Traverse the pending reachable objects */
266 traverse_reachable();
268 /* Look up all the requirements, warn about missing objects.. */
269 max
= get_max_object_index();
271 fprintf(stderr
, "Checking connectivity (%d objects)\n", max
);
273 for (i
= 0; i
< max
; i
++) {
274 struct object
*obj
= get_indexed_object(i
);
281 static int fsck_sha1(const unsigned char *sha1
)
283 struct object
*obj
= parse_object(sha1
);
285 errors_found
|= ERROR_OBJECT
;
286 return error("%s: object corrupt or missing",
289 if (obj
->flags
& SEEN
)
294 fprintf(stderr
, "Checking %s %s\n",
295 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
297 if (fsck_walk(obj
, mark_used
, NULL
))
298 objerror(obj
, "broken links");
299 if (fsck_object(obj
, check_strict
, fsck_error_func
))
302 if (obj
->type
== OBJ_TREE
) {
303 struct tree
*item
= (struct tree
*) obj
;
309 if (obj
->type
== OBJ_COMMIT
) {
310 struct commit
*commit
= (struct commit
*) obj
;
312 free(commit
->buffer
);
313 commit
->buffer
= NULL
;
315 if (!commit
->parents
&& show_root
)
316 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
319 if (obj
->type
== OBJ_TAG
) {
320 struct tag
*tag
= (struct tag
*) obj
;
322 if (show_tags
&& tag
->tagged
) {
323 printf("tagged %s %s", typename(tag
->tagged
->type
), sha1_to_hex(tag
->tagged
->sha1
));
324 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
332 * This is the sorting chunk size: make it reasonably
333 * big so that we can sort well..
335 #define MAX_SHA1_ENTRIES (1024)
339 unsigned char sha1
[20];
344 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
347 static int ino_compare(const void *_a
, const void *_b
)
349 const struct sha1_entry
*a
= _a
, *b
= _b
;
350 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
351 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
354 static void fsck_sha1_list(void)
356 int i
, nr
= sha1_list
.nr
;
359 qsort(sha1_list
.entry
, nr
,
360 sizeof(struct sha1_entry
*), ino_compare
);
361 for (i
= 0; i
< nr
; i
++) {
362 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
363 unsigned char *sha1
= entry
->sha1
;
365 sha1_list
.entry
[i
] = NULL
;
372 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
374 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
378 hashcpy(entry
->sha1
, sha1
);
380 if (nr
== MAX_SHA1_ENTRIES
) {
384 sha1_list
.entry
[nr
] = entry
;
388 static void fsck_dir(int i
, char *path
)
390 DIR *dir
= opendir(path
);
397 fprintf(stderr
, "Checking directory %s\n", path
);
399 while ((de
= readdir(dir
)) != NULL
) {
401 unsigned char sha1
[20];
403 if (is_dot_or_dotdot(de
->d_name
))
405 if (strlen(de
->d_name
) == 38) {
406 sprintf(name
, "%02x", i
);
407 memcpy(name
+2, de
->d_name
, 39);
408 if (get_sha1_hex(name
, sha1
) < 0)
410 add_sha1_list(sha1
, DIRENT_SORT_HINT(de
));
413 if (!prefixcmp(de
->d_name
, "tmp_obj_"))
415 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
420 static int default_refs
;
422 static int fsck_handle_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
423 const char *email
, unsigned long timestamp
, int tz
,
424 const char *message
, void *cb_data
)
429 fprintf(stderr
, "Checking reflog %s->%s\n",
430 sha1_to_hex(osha1
), sha1_to_hex(nsha1
));
432 if (!is_null_sha1(osha1
)) {
433 obj
= lookup_object(osha1
);
436 mark_object_reachable(obj
);
439 obj
= lookup_object(nsha1
);
442 mark_object_reachable(obj
);
447 static int fsck_handle_reflog(const char *logname
, const unsigned char *sha1
, int flag
, void *cb_data
)
449 for_each_reflog_ent(logname
, fsck_handle_reflog_ent
, NULL
);
453 static int is_branch(const char *refname
)
455 return !strcmp(refname
, "HEAD") || !prefixcmp(refname
, "refs/heads/");
458 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
462 obj
= parse_object(sha1
);
464 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
465 /* We'll continue with the rest despite the error.. */
468 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
))
469 error("%s: not a commit", refname
);
472 mark_object_reachable(obj
);
477 static void get_default_heads(void)
479 if (head_points_at
&& !is_null_sha1(head_sha1
))
480 fsck_handle_ref("HEAD", head_sha1
, 0, NULL
);
481 for_each_ref(fsck_handle_ref
, NULL
);
483 for_each_reflog(fsck_handle_reflog
, NULL
);
486 * Not having any default heads isn't really fatal, but
487 * it does mean that "--unreachable" no longer makes any
488 * sense (since in this case everything will obviously
489 * be unreachable by definition.
491 * Showing dangling objects is valid, though (as those
492 * dangling objects are likely lost heads).
494 * So we just print a warning about it, and clear the
495 * "show_unreachable" flag.
498 fprintf(stderr
, "notice: No default references\n");
499 show_unreachable
= 0;
503 static void fsck_object_dir(const char *path
)
508 fprintf(stderr
, "Checking object directory\n");
510 for (i
= 0; i
< 256; i
++) {
511 static char dir
[4096];
512 sprintf(dir
, "%s/%02x", path
, i
);
518 static int fsck_head_link(void)
521 int null_is_error
= 0;
524 fprintf(stderr
, "Checking HEAD link\n");
526 head_points_at
= resolve_ref("HEAD", head_sha1
, 0, &flag
);
528 return error("Invalid HEAD");
529 if (!strcmp(head_points_at
, "HEAD"))
532 else if (prefixcmp(head_points_at
, "refs/heads/"))
533 return error("HEAD points to something strange (%s)",
535 if (is_null_sha1(head_sha1
)) {
537 return error("HEAD: detached HEAD points at nothing");
538 fprintf(stderr
, "notice: HEAD points to an unborn branch (%s)\n",
539 head_points_at
+ 11);
544 static int fsck_cache_tree(struct cache_tree
*it
)
550 fprintf(stderr
, "Checking cache tree\n");
552 if (0 <= it
->entry_count
) {
553 struct object
*obj
= parse_object(it
->sha1
);
555 error("%s: invalid sha1 pointer in cache-tree",
556 sha1_to_hex(it
->sha1
));
559 mark_object_reachable(obj
);
561 if (obj
->type
!= OBJ_TREE
)
562 err
|= objerror(obj
, "non-tree in cache-tree");
564 for (i
= 0; i
< it
->subtree_nr
; i
++)
565 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
);
569 static char const * const fsck_usage
[] = {
570 "git fsck [options] [<object>...]",
574 static struct option fsck_opts
[] = {
575 OPT__VERBOSE(&verbose
),
576 OPT_BOOLEAN(0, "unreachable", &show_unreachable
, "show unreachable objects"),
577 OPT_BOOLEAN(0, "tags", &show_tags
, "report tags"),
578 OPT_BOOLEAN(0, "root", &show_root
, "report root nodes"),
579 OPT_BOOLEAN(0, "cache", &keep_cache_objects
, "make index objects head nodes"),
580 OPT_BOOLEAN(0, "reflogs", &include_reflogs
, "make reflogs head nodes (default)"),
581 OPT_BOOLEAN(0, "full", &check_full
, "also consider alternate objects"),
582 OPT_BOOLEAN(0, "strict", &check_strict
, "enable more strict checking"),
583 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found
,
584 "write dangling objects in .git/lost-found"),
588 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
591 struct alternate_object_database
*alt
;
594 read_replace_refs
= 0;
596 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
597 if (write_lost_and_found
) {
603 fsck_object_dir(get_object_directory());
606 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
607 char namebuf
[PATH_MAX
];
608 int namelen
= alt
->name
- alt
->base
;
609 memcpy(namebuf
, alt
->base
, namelen
);
610 namebuf
[namelen
- 1] = 0;
611 fsck_object_dir(namebuf
);
615 struct packed_git
*p
;
617 prepare_packed_git();
618 for (p
= packed_git
; p
; p
= p
->next
)
619 /* verify gives error messages itself */
622 for (p
= packed_git
; p
; p
= p
->next
) {
624 if (open_pack_index(p
))
626 num
= p
->num_objects
;
627 for (j
= 0; j
< num
; j
++)
628 fsck_sha1(nth_packed_object_sha1(p
, j
));
633 for (i
= 0; i
< argc
; i
++) {
634 const char *arg
= argv
[i
];
635 unsigned char sha1
[20];
636 if (!get_sha1(arg
, sha1
)) {
637 struct object
*obj
= lookup_object(sha1
);
639 /* Error is printed by lookup_object(). */
644 mark_object_reachable(obj
);
648 error("invalid parameter: expected sha1, got '%s'", arg
);
652 * If we've not been given any explicit head information, do the
653 * default ones from .git/refs. We also consider the index file
654 * in this case (ie this implies --cache).
658 keep_cache_objects
= 1;
661 if (keep_cache_objects
) {
663 for (i
= 0; i
< active_nr
; i
++) {
668 mode
= active_cache
[i
]->ce_mode
;
669 if (S_ISGITLINK(mode
))
671 blob
= lookup_blob(active_cache
[i
]->sha1
);
676 mark_object_reachable(obj
);
678 if (active_cache_tree
)
679 fsck_cache_tree(active_cache_tree
);
682 check_connectivity();