9 #include "cache-tree.h"
10 #include "tree-walk.h"
12 #include "parse-options.h"
16 #define REACHABLE 0x0001
21 static int show_unreachable
;
22 static int include_reflogs
= 1;
23 static int check_full
= 1;
24 static int check_strict
;
25 static int keep_cache_objects
;
26 static unsigned char head_sha1
[20];
27 static const char *head_points_at
;
28 static int errors_found
;
29 static int write_lost_and_found
;
31 static int show_progress
= -1;
32 #define ERROR_OBJECT 01
33 #define ERROR_REACHABLE 02
36 #ifdef NO_D_INO_IN_DIRENT
38 #define DIRENT_SORT_HINT(de) 0
41 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
44 static void objreport(struct object
*obj
, const char *severity
,
45 const char *err
, va_list params
)
47 fprintf(stderr
, "%s in %s %s: ",
48 severity
, typename(obj
->type
), sha1_to_hex(obj
->sha1
));
49 vfprintf(stderr
, err
, params
);
53 __attribute__((format (printf
, 2, 3)))
54 static int objerror(struct object
*obj
, const char *err
, ...)
57 va_start(params
, err
);
58 errors_found
|= ERROR_OBJECT
;
59 objreport(obj
, "error", err
, params
);
64 __attribute__((format (printf
, 3, 4)))
65 static int fsck_error_func(struct object
*obj
, int type
, const char *err
, ...)
68 va_start(params
, err
);
69 objreport(obj
, (type
== FSCK_WARN
) ? "warning" : "error", err
, params
);
71 return (type
== FSCK_WARN
) ? 0 : 1;
74 static struct object_array pending
;
76 static int mark_object(struct object
*obj
, int type
, void *data
)
78 struct object
*parent
= data
;
81 * The only case data is NULL or type is OBJ_ANY is when
82 * mark_object_reachable() calls us. All the callers of
83 * that function has non-NULL obj hence ...
86 /* ... these references to parent->fld are safe here */
87 printf("broken link from %7s %s\n",
88 typename(parent
->type
), sha1_to_hex(parent
->sha1
));
89 printf("broken link from %7s %s\n",
90 (type
== OBJ_ANY
? "unknown" : typename(type
)), "unknown");
91 errors_found
|= ERROR_REACHABLE
;
95 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
96 /* ... and the reference to parent is safe here */
97 objerror(parent
, "wrong object type in link");
99 if (obj
->flags
& REACHABLE
)
101 obj
->flags
|= REACHABLE
;
103 if (parent
&& !has_sha1_file(obj
->sha1
)) {
104 printf("broken link from %7s %s\n",
105 typename(parent
->type
), sha1_to_hex(parent
->sha1
));
106 printf(" to %7s %s\n",
107 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
108 errors_found
|= ERROR_REACHABLE
;
113 add_object_array(obj
, (void *) parent
, &pending
);
117 static void mark_object_reachable(struct object
*obj
)
119 mark_object(obj
, OBJ_ANY
, NULL
);
122 static int traverse_one_object(struct object
*obj
)
125 struct tree
*tree
= NULL
;
127 if (obj
->type
== OBJ_TREE
) {
129 tree
= (struct tree
*)obj
;
130 if (parse_tree(tree
) < 0)
131 return 1; /* error already displayed */
133 result
= fsck_walk(obj
, mark_object
, obj
);
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
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.
224 printf("dangling %s %s\n", typename(obj
->type
),
225 sha1_to_hex(obj
->sha1
));
226 if (write_lost_and_found
) {
227 char *filename
= git_path("lost-found/%s/%s",
228 obj
->type
== OBJ_COMMIT
? "commit" : "other",
229 sha1_to_hex(obj
->sha1
));
232 if (safe_create_leading_directories(filename
)) {
233 error("Could not create lost-found");
236 if (!(f
= fopen(filename
, "w")))
237 die_errno("Could not open '%s'", filename
);
238 if (obj
->type
== OBJ_BLOB
) {
239 enum object_type type
;
241 char *buf
= read_sha1_file(obj
->sha1
,
243 if (buf
&& fwrite(buf
, 1, size
, f
) != size
)
244 die_errno("Could not write '%s'", filename
);
247 fprintf(f
, "%s\n", sha1_to_hex(obj
->sha1
));
249 die_errno("Could not finish '%s'",
256 * Otherwise? It's there, it's unreachable, and some other unreachable
257 * object points to it. Ignore it - it's not interesting, and we showed
258 * all the interesting cases above.
262 static void check_object(struct object
*obj
)
265 fprintf(stderr
, "Checking %s\n", sha1_to_hex(obj
->sha1
));
267 if (obj
->flags
& REACHABLE
)
268 check_reachable_object(obj
);
270 check_unreachable_object(obj
);
273 static void check_connectivity(void)
277 /* Traverse the pending reachable objects */
278 traverse_reachable();
280 /* Look up all the requirements, warn about missing objects.. */
281 max
= get_max_object_index();
283 fprintf(stderr
, "Checking connectivity (%d objects)\n", max
);
285 for (i
= 0; i
< max
; i
++) {
286 struct object
*obj
= get_indexed_object(i
);
293 static int fsck_obj(struct object
*obj
)
295 if (obj
->flags
& SEEN
)
300 fprintf(stderr
, "Checking %s %s\n",
301 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
303 if (fsck_walk(obj
, mark_used
, NULL
))
304 objerror(obj
, "broken links");
305 if (fsck_object(obj
, check_strict
, fsck_error_func
))
308 if (obj
->type
== OBJ_TREE
) {
309 struct tree
*item
= (struct tree
*) obj
;
315 if (obj
->type
== OBJ_COMMIT
) {
316 struct commit
*commit
= (struct commit
*) obj
;
318 free(commit
->buffer
);
319 commit
->buffer
= NULL
;
321 if (!commit
->parents
&& show_root
)
322 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
325 if (obj
->type
== OBJ_TAG
) {
326 struct tag
*tag
= (struct tag
*) obj
;
328 if (show_tags
&& tag
->tagged
) {
329 printf("tagged %s %s", typename(tag
->tagged
->type
), sha1_to_hex(tag
->tagged
->sha1
));
330 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
337 static int fsck_sha1(const unsigned char *sha1
)
339 struct object
*obj
= parse_object(sha1
);
341 errors_found
|= ERROR_OBJECT
;
342 return error("%s: object corrupt or missing",
345 return fsck_obj(obj
);
348 static int fsck_obj_buffer(const unsigned char *sha1
, enum object_type type
,
349 unsigned long size
, void *buffer
, int *eaten
)
352 obj
= parse_object_buffer(sha1
, type
, size
, buffer
, eaten
);
354 errors_found
|= ERROR_OBJECT
;
355 return error("%s: object corrupt or missing", sha1_to_hex(sha1
));
357 return fsck_obj(obj
);
361 * This is the sorting chunk size: make it reasonably
362 * big so that we can sort well..
364 #define MAX_SHA1_ENTRIES (1024)
368 unsigned char sha1
[20];
373 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
376 static int ino_compare(const void *_a
, const void *_b
)
378 const struct sha1_entry
*a
= _a
, *b
= _b
;
379 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
380 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
383 static void fsck_sha1_list(void)
385 int i
, nr
= sha1_list
.nr
;
388 qsort(sha1_list
.entry
, nr
,
389 sizeof(struct sha1_entry
*), ino_compare
);
390 for (i
= 0; i
< nr
; i
++) {
391 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
392 unsigned char *sha1
= entry
->sha1
;
394 sha1_list
.entry
[i
] = NULL
;
401 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
403 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
407 hashcpy(entry
->sha1
, sha1
);
409 if (nr
== MAX_SHA1_ENTRIES
) {
413 sha1_list
.entry
[nr
] = entry
;
417 static inline int is_loose_object_file(struct dirent
*de
,
418 char *name
, unsigned char *sha1
)
420 if (strlen(de
->d_name
) != 38)
422 memcpy(name
+ 2, de
->d_name
, 39);
423 return !get_sha1_hex(name
, sha1
);
426 static void fsck_dir(int i
, char *path
)
428 DIR *dir
= opendir(path
);
436 fprintf(stderr
, "Checking directory %s\n", path
);
438 sprintf(name
, "%02x", i
);
439 while ((de
= readdir(dir
)) != NULL
) {
440 unsigned char sha1
[20];
442 if (is_dot_or_dotdot(de
->d_name
))
444 if (is_loose_object_file(de
, name
, sha1
)) {
445 add_sha1_list(sha1
, DIRENT_SORT_HINT(de
));
448 if (!prefixcmp(de
->d_name
, "tmp_obj_"))
450 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
455 static int default_refs
;
457 static int fsck_handle_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
458 const char *email
, unsigned long timestamp
, int tz
,
459 const char *message
, void *cb_data
)
464 fprintf(stderr
, "Checking reflog %s->%s\n",
465 sha1_to_hex(osha1
), sha1_to_hex(nsha1
));
467 if (!is_null_sha1(osha1
)) {
468 obj
= lookup_object(osha1
);
471 mark_object_reachable(obj
);
474 obj
= lookup_object(nsha1
);
477 mark_object_reachable(obj
);
482 static int fsck_handle_reflog(const char *logname
, const unsigned char *sha1
, int flag
, void *cb_data
)
484 for_each_reflog_ent(logname
, fsck_handle_reflog_ent
, NULL
);
488 static int is_branch(const char *refname
)
490 return !strcmp(refname
, "HEAD") || !prefixcmp(refname
, "refs/heads/");
493 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
497 obj
= parse_object(sha1
);
499 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
500 /* We'll continue with the rest despite the error.. */
503 if (obj
->type
!= OBJ_COMMIT
&& is_branch(refname
))
504 error("%s: not a commit", refname
);
507 mark_object_reachable(obj
);
512 static void get_default_heads(void)
514 if (head_points_at
&& !is_null_sha1(head_sha1
))
515 fsck_handle_ref("HEAD", head_sha1
, 0, NULL
);
516 for_each_ref(fsck_handle_ref
, NULL
);
518 for_each_reflog(fsck_handle_reflog
, NULL
);
521 * Not having any default heads isn't really fatal, but
522 * it does mean that "--unreachable" no longer makes any
523 * sense (since in this case everything will obviously
524 * be unreachable by definition.
526 * Showing dangling objects is valid, though (as those
527 * dangling objects are likely lost heads).
529 * So we just print a warning about it, and clear the
530 * "show_unreachable" flag.
533 fprintf(stderr
, "notice: No default references\n");
534 show_unreachable
= 0;
538 static void fsck_object_dir(const char *path
)
541 struct progress
*progress
= NULL
;
544 fprintf(stderr
, "Checking object directory\n");
547 progress
= start_progress("Checking object directories", 256);
548 for (i
= 0; i
< 256; i
++) {
549 static char dir
[4096];
550 sprintf(dir
, "%s/%02x", path
, i
);
552 display_progress(progress
, i
+1);
554 stop_progress(&progress
);
558 static int fsck_head_link(void)
561 int null_is_error
= 0;
564 fprintf(stderr
, "Checking HEAD link\n");
566 head_points_at
= resolve_ref_unsafe("HEAD", head_sha1
, 0, &flag
);
568 return error("Invalid HEAD");
569 if (!strcmp(head_points_at
, "HEAD"))
572 else if (prefixcmp(head_points_at
, "refs/heads/"))
573 return error("HEAD points to something strange (%s)",
575 if (is_null_sha1(head_sha1
)) {
577 return error("HEAD: detached HEAD points at nothing");
578 fprintf(stderr
, "notice: HEAD points to an unborn branch (%s)\n",
579 head_points_at
+ 11);
584 static int fsck_cache_tree(struct cache_tree
*it
)
590 fprintf(stderr
, "Checking cache tree\n");
592 if (0 <= it
->entry_count
) {
593 struct object
*obj
= parse_object(it
->sha1
);
595 error("%s: invalid sha1 pointer in cache-tree",
596 sha1_to_hex(it
->sha1
));
600 mark_object_reachable(obj
);
601 if (obj
->type
!= OBJ_TREE
)
602 err
|= objerror(obj
, "non-tree in cache-tree");
604 for (i
= 0; i
< it
->subtree_nr
; i
++)
605 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
);
609 static char const * const fsck_usage
[] = {
610 "git fsck [options] [<object>...]",
614 static struct option fsck_opts
[] = {
615 OPT__VERBOSE(&verbose
, "be verbose"),
616 OPT_BOOLEAN(0, "unreachable", &show_unreachable
, "show unreachable objects"),
617 OPT_BOOLEAN(0, "tags", &show_tags
, "report tags"),
618 OPT_BOOLEAN(0, "root", &show_root
, "report root nodes"),
619 OPT_BOOLEAN(0, "cache", &keep_cache_objects
, "make index objects head nodes"),
620 OPT_BOOLEAN(0, "reflogs", &include_reflogs
, "make reflogs head nodes (default)"),
621 OPT_BOOLEAN(0, "full", &check_full
, "also consider packs and alternate objects"),
622 OPT_BOOLEAN(0, "strict", &check_strict
, "enable more strict checking"),
623 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found
,
624 "write dangling objects in .git/lost-found"),
625 OPT_BOOL(0, "progress", &show_progress
, "show progress"),
629 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
632 struct alternate_object_database
*alt
;
635 read_replace_refs
= 0;
637 argc
= parse_options(argc
, argv
, prefix
, fsck_opts
, fsck_usage
, 0);
639 if (show_progress
== -1)
640 show_progress
= isatty(2);
644 if (write_lost_and_found
) {
650 fsck_object_dir(get_object_directory());
653 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
654 char namebuf
[PATH_MAX
];
655 int namelen
= alt
->name
- alt
->base
;
656 memcpy(namebuf
, alt
->base
, namelen
);
657 namebuf
[namelen
- 1] = 0;
658 fsck_object_dir(namebuf
);
662 struct packed_git
*p
;
663 uint32_t total
= 0, count
= 0;
664 struct progress
*progress
= NULL
;
666 prepare_packed_git();
669 for (p
= packed_git
; p
; p
= p
->next
) {
670 if (open_pack_index(p
))
672 total
+= p
->num_objects
;
675 progress
= start_progress("Checking objects", total
);
677 for (p
= packed_git
; p
; p
= p
->next
) {
678 /* verify gives error messages itself */
679 if (verify_pack(p
, fsck_obj_buffer
,
681 errors_found
|= ERROR_PACK
;
682 count
+= p
->num_objects
;
684 stop_progress(&progress
);
688 for (i
= 0; i
< argc
; i
++) {
689 const char *arg
= argv
[i
];
690 unsigned char sha1
[20];
691 if (!get_sha1(arg
, sha1
)) {
692 struct object
*obj
= lookup_object(sha1
);
694 /* Error is printed by lookup_object(). */
699 mark_object_reachable(obj
);
703 error("invalid parameter: expected sha1, got '%s'", arg
);
707 * If we've not been given any explicit head information, do the
708 * default ones from .git/refs. We also consider the index file
709 * in this case (ie this implies --cache).
713 keep_cache_objects
= 1;
716 if (keep_cache_objects
) {
718 for (i
= 0; i
< active_nr
; i
++) {
723 mode
= active_cache
[i
]->ce_mode
;
724 if (S_ISGITLINK(mode
))
726 blob
= lookup_blob(active_cache
[i
]->sha1
);
731 mark_object_reachable(obj
);
733 if (active_cache_tree
)
734 fsck_cache_tree(active_cache_tree
);
737 check_connectivity();