8 #include "cache-tree.h"
11 #define REACHABLE 0x0001
16 static int show_unreachable
;
17 static int include_reflogs
= 1;
18 static int check_full
;
19 static int check_strict
;
20 static int keep_cache_objects
;
21 static unsigned char head_sha1
[20];
22 static int errors_found
;
23 static int write_lost_and_found
;
25 #define ERROR_OBJECT 01
26 #define ERROR_REACHABLE 02
28 #ifdef NO_D_INO_IN_DIRENT
30 #define DIRENT_SORT_HINT(de) 0
33 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
36 static void objreport(struct object
*obj
, const char *severity
,
37 const char *err
, va_list params
)
39 fprintf(stderr
, "%s in %s %s: ",
40 severity
, typename(obj
->type
), sha1_to_hex(obj
->sha1
));
41 vfprintf(stderr
, err
, params
);
45 static int objerror(struct object
*obj
, const char *err
, ...)
48 va_start(params
, err
);
49 errors_found
|= ERROR_OBJECT
;
50 objreport(obj
, "error", err
, params
);
55 static int objwarning(struct object
*obj
, const char *err
, ...)
58 va_start(params
, err
);
59 objreport(obj
, "warning", err
, params
);
65 * Check a single reachable object
67 static void check_reachable_object(struct object
*obj
)
69 const struct object_refs
*refs
;
72 * We obviously want the object to be parsed,
73 * except if it was in a pack-file and we didn't
77 if (has_sha1_pack(obj
->sha1
, NULL
))
78 return; /* it is in pack - forget about it */
79 printf("missing %s %s\n", typename(obj
->type
), sha1_to_hex(obj
->sha1
));
80 errors_found
|= ERROR_REACHABLE
;
85 * Check that everything that we try to reference is also good.
87 refs
= lookup_object_refs(obj
);
90 for (j
= 0; j
< refs
->count
; j
++) {
91 struct object
*ref
= refs
->ref
[j
];
93 (has_sha1_file(ref
->sha1
)))
95 printf("broken link from %7s %s\n",
96 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
97 printf(" to %7s %s\n",
98 typename(ref
->type
), sha1_to_hex(ref
->sha1
));
99 errors_found
|= ERROR_REACHABLE
;
105 * Check a single unreachable object
107 static void check_unreachable_object(struct object
*obj
)
110 * Missing unreachable object? Ignore it. It's not like
111 * we miss it (since it can't be reached), nor do we want
112 * to complain about it being unreachable (since it does
119 * Unreachable object that exists? Show it if asked to,
120 * since this is something that is prunable.
122 if (show_unreachable
) {
123 printf("unreachable %s %s\n", typename(obj
->type
), sha1_to_hex(obj
->sha1
));
128 * "!used" means that nothing at all points to it, including
129 * other unreachable objects. In other words, it's the "tip"
130 * of some set of unreachable objects, usually a commit that
133 * Such starting points are more interesting than some random
134 * set of unreachable objects, so we show them even if the user
135 * hasn't asked for _all_ unreachable objects. If you have
136 * deleted a branch by mistake, this is a prime candidate to
137 * start looking at, for example.
140 printf("dangling %s %s\n", typename(obj
->type
),
141 sha1_to_hex(obj
->sha1
));
142 if (write_lost_and_found
) {
143 char *filename
= git_path("lost-found/%s/%s",
144 obj
->type
== OBJ_COMMIT
? "commit" : "other",
145 sha1_to_hex(obj
->sha1
));
148 if (safe_create_leading_directories(filename
)) {
149 error("Could not create lost-found");
152 if (!(f
= fopen(filename
, "w")))
153 die("Could not open %s", filename
);
154 fprintf(f
, "%s\n", sha1_to_hex(obj
->sha1
));
161 * Otherwise? It's there, it's unreachable, and some other unreachable
162 * object points to it. Ignore it - it's not interesting, and we showed
163 * all the interesting cases above.
167 static void check_object(struct object
*obj
)
170 fprintf(stderr
, "Checking %s\n", sha1_to_hex(obj
->sha1
));
172 if (obj
->flags
& REACHABLE
)
173 check_reachable_object(obj
);
175 check_unreachable_object(obj
);
178 static void check_connectivity(void)
182 /* Look up all the requirements, warn about missing objects.. */
183 max
= get_max_object_index();
185 fprintf(stderr
, "Checking connectivity (%d objects)\n", max
);
187 for (i
= 0; i
< max
; i
++) {
188 struct object
*obj
= get_indexed_object(i
);
196 * The entries in a tree are ordered in the _path_ order,
197 * which means that a directory entry is ordered by adding
198 * a slash to the end of it.
200 * So a directory called "a" is ordered _after_ a file
201 * called "a.c", because "a/" sorts after "a.c".
203 #define TREE_UNORDERED (-1)
204 #define TREE_HAS_DUPS (-2)
206 static int verify_ordered(unsigned mode1
, const char *name1
, unsigned mode2
, const char *name2
)
208 int len1
= strlen(name1
);
209 int len2
= strlen(name2
);
210 int len
= len1
< len2
? len1
: len2
;
211 unsigned char c1
, c2
;
214 cmp
= memcmp(name1
, name2
, len
);
218 return TREE_UNORDERED
;
221 * Ok, the first <len> characters are the same.
222 * Now we need to order the next one, but turn
223 * a '\0' into a '/' for a directory entry.
229 * git-write-tree used to write out a nonsense tree that has
230 * entries with the same name, one blob and one tree. Make
231 * sure we do not have duplicate entries.
233 return TREE_HAS_DUPS
;
234 if (!c1
&& S_ISDIR(mode1
))
236 if (!c2
&& S_ISDIR(mode2
))
238 return c1
< c2
? 0 : TREE_UNORDERED
;
241 static int fsck_tree(struct tree
*item
)
244 int has_full_path
= 0;
245 int has_empty_name
= 0;
246 int has_zero_pad
= 0;
247 int has_bad_modes
= 0;
248 int has_dup_entries
= 0;
249 int not_properly_sorted
= 0;
250 struct tree_desc desc
;
253 const unsigned char *o_sha1
;
256 fprintf(stderr
, "Checking tree %s\n",
257 sha1_to_hex(item
->object
.sha1
));
259 init_tree_desc(&desc
, item
->buffer
, item
->size
);
267 const unsigned char *sha1
;
269 sha1
= tree_entry_extract(&desc
, &name
, &mode
);
271 if (strchr(name
, '/'))
275 has_zero_pad
|= *(char *)desc
.buffer
== '0';
276 update_tree_entry(&desc
);
289 * This is nonstandard, but we had a few of these
290 * early on when we honored the full set of mode
301 switch (verify_ordered(o_mode
, o_name
, mode
, name
)) {
303 not_properly_sorted
= 1;
322 objwarning(&item
->object
, "contains full pathnames");
324 if (has_empty_name
) {
325 objwarning(&item
->object
, "contains empty pathname");
328 objwarning(&item
->object
, "contains zero-padded file modes");
331 objwarning(&item
->object
, "contains bad file modes");
333 if (has_dup_entries
) {
334 retval
= objerror(&item
->object
, "contains duplicate file entries");
336 if (not_properly_sorted
) {
337 retval
= objerror(&item
->object
, "not properly sorted");
342 static int fsck_commit(struct commit
*commit
)
344 char *buffer
= commit
->buffer
;
345 unsigned char tree_sha1
[20], sha1
[20];
348 fprintf(stderr
, "Checking commit %s\n",
349 sha1_to_hex(commit
->object
.sha1
));
351 if (memcmp(buffer
, "tree ", 5))
352 return objerror(&commit
->object
, "invalid format - expected 'tree' line");
353 if (get_sha1_hex(buffer
+5, tree_sha1
) || buffer
[45] != '\n')
354 return objerror(&commit
->object
, "invalid 'tree' line format - bad sha1");
356 while (!memcmp(buffer
, "parent ", 7)) {
357 if (get_sha1_hex(buffer
+7, sha1
) || buffer
[47] != '\n')
358 return objerror(&commit
->object
, "invalid 'parent' line format - bad sha1");
361 if (memcmp(buffer
, "author ", 7))
362 return objerror(&commit
->object
, "invalid format - expected 'author' line");
363 free(commit
->buffer
);
364 commit
->buffer
= NULL
;
366 return objerror(&commit
->object
, "could not load commit's tree %s", tree_sha1
);
367 if (!commit
->parents
&& show_root
)
368 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
370 printf("bad commit date in %s\n",
371 sha1_to_hex(commit
->object
.sha1
));
375 static int fsck_tag(struct tag
*tag
)
377 struct object
*tagged
= tag
->tagged
;
380 fprintf(stderr
, "Checking tag %s\n",
381 sha1_to_hex(tag
->object
.sha1
));
384 return objerror(&tag
->object
, "could not load tagged object");
389 printf("tagged %s %s", typename(tagged
->type
), sha1_to_hex(tagged
->sha1
));
390 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
394 static int fsck_sha1(const unsigned char *sha1
)
396 struct object
*obj
= parse_object(sha1
);
398 errors_found
|= ERROR_OBJECT
;
399 return error("%s: object corrupt or missing",
402 if (obj
->flags
& SEEN
)
405 if (obj
->type
== OBJ_BLOB
)
407 if (obj
->type
== OBJ_TREE
)
408 return fsck_tree((struct tree
*) obj
);
409 if (obj
->type
== OBJ_COMMIT
)
410 return fsck_commit((struct commit
*) obj
);
411 if (obj
->type
== OBJ_TAG
)
412 return fsck_tag((struct tag
*) obj
);
414 /* By now, parse_object() would've returned NULL instead. */
415 return objerror(obj
, "unknown type '%d' (internal fsck error)",
420 * This is the sorting chunk size: make it reasonably
421 * big so that we can sort well..
423 #define MAX_SHA1_ENTRIES (1024)
427 unsigned char sha1
[20];
432 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
435 static int ino_compare(const void *_a
, const void *_b
)
437 const struct sha1_entry
*a
= _a
, *b
= _b
;
438 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
439 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
442 static void fsck_sha1_list(void)
444 int i
, nr
= sha1_list
.nr
;
447 qsort(sha1_list
.entry
, nr
,
448 sizeof(struct sha1_entry
*), ino_compare
);
449 for (i
= 0; i
< nr
; i
++) {
450 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
451 unsigned char *sha1
= entry
->sha1
;
453 sha1_list
.entry
[i
] = NULL
;
460 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
462 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
466 hashcpy(entry
->sha1
, sha1
);
468 if (nr
== MAX_SHA1_ENTRIES
) {
472 sha1_list
.entry
[nr
] = entry
;
476 static void fsck_dir(int i
, char *path
)
478 DIR *dir
= opendir(path
);
485 fprintf(stderr
, "Checking directory %s\n", path
);
487 while ((de
= readdir(dir
)) != NULL
) {
489 unsigned char sha1
[20];
490 int len
= strlen(de
->d_name
);
494 if (de
->d_name
[1] != '.')
497 if (de
->d_name
[0] != '.')
501 sprintf(name
, "%02x", i
);
502 memcpy(name
+2, de
->d_name
, len
+1);
503 if (get_sha1_hex(name
, sha1
) < 0)
505 add_sha1_list(sha1
, DIRENT_SORT_HINT(de
));
508 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
513 static int default_refs
;
515 static int fsck_handle_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
516 const char *email
, unsigned long timestamp
, int tz
,
517 const char *message
, void *cb_data
)
522 fprintf(stderr
, "Checking reflog %s->%s\n",
523 sha1_to_hex(osha1
), sha1_to_hex(nsha1
));
525 if (!is_null_sha1(osha1
)) {
526 obj
= lookup_object(osha1
);
529 mark_reachable(obj
, REACHABLE
);
532 obj
= lookup_object(nsha1
);
535 mark_reachable(obj
, REACHABLE
);
540 static int fsck_handle_reflog(const char *logname
, const unsigned char *sha1
, int flag
, void *cb_data
)
542 for_each_reflog_ent(logname
, fsck_handle_reflog_ent
, NULL
);
546 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
550 obj
= lookup_object(sha1
);
552 if (has_sha1_file(sha1
)) {
554 return 0; /* it is in a pack */
556 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
557 /* We'll continue with the rest despite the error.. */
562 mark_reachable(obj
, REACHABLE
);
567 static void get_default_heads(void)
569 for_each_ref(fsck_handle_ref
, NULL
);
571 for_each_reflog(fsck_handle_reflog
, NULL
);
574 * Not having any default heads isn't really fatal, but
575 * it does mean that "--unreachable" no longer makes any
576 * sense (since in this case everything will obviously
577 * be unreachable by definition.
579 * Showing dangling objects is valid, though (as those
580 * dangling objects are likely lost heads).
582 * So we just print a warning about it, and clear the
583 * "show_unreachable" flag.
586 fprintf(stderr
, "notice: No default references\n");
587 show_unreachable
= 0;
591 static void fsck_object_dir(const char *path
)
596 fprintf(stderr
, "Checking object directory\n");
598 for (i
= 0; i
< 256; i
++) {
599 static char dir
[4096];
600 sprintf(dir
, "%s/%02x", path
, i
);
606 static int fsck_head_link(void)
608 unsigned char sha1
[20];
610 int null_is_error
= 0;
611 const char *head_points_at
= resolve_ref("HEAD", sha1
, 0, &flag
);
614 fprintf(stderr
, "Checking HEAD link\n");
617 return error("Invalid HEAD");
618 if (!strcmp(head_points_at
, "HEAD"))
621 else if (prefixcmp(head_points_at
, "refs/heads/"))
622 return error("HEAD points to something strange (%s)",
624 if (is_null_sha1(sha1
)) {
626 return error("HEAD: detached HEAD points at nothing");
627 fprintf(stderr
, "notice: HEAD points to an unborn branch (%s)\n",
628 head_points_at
+ 11);
633 static int fsck_cache_tree(struct cache_tree
*it
)
639 fprintf(stderr
, "Checking cache tree\n");
641 if (0 <= it
->entry_count
) {
642 struct object
*obj
= parse_object(it
->sha1
);
644 error("%s: invalid sha1 pointer in cache-tree",
645 sha1_to_hex(it
->sha1
));
648 mark_reachable(obj
, REACHABLE
);
650 if (obj
->type
!= OBJ_TREE
)
651 err
|= objerror(obj
, "non-tree in cache-tree");
653 for (i
= 0; i
< it
->subtree_nr
; i
++)
654 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
);
658 static const char fsck_usage
[] =
659 "git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] "
660 "[--strict] [--verbose] <head-sha1>*]";
662 int cmd_fsck(int argc
, char **argv
, const char *prefix
)
666 track_object_refs
= 1;
669 for (i
= 1; i
< argc
; i
++) {
670 const char *arg
= argv
[i
];
672 if (!strcmp(arg
, "--unreachable")) {
673 show_unreachable
= 1;
676 if (!strcmp(arg
, "--tags")) {
680 if (!strcmp(arg
, "--root")) {
684 if (!strcmp(arg
, "--cache")) {
685 keep_cache_objects
= 1;
688 if (!strcmp(arg
, "--no-reflogs")) {
692 if (!strcmp(arg
, "--full")) {
696 if (!strcmp(arg
, "--strict")) {
700 if (!strcmp(arg
, "--verbose")) {
704 if (!strcmp(arg
, "--lost-found")) {
707 write_lost_and_found
= 1;
715 fsck_object_dir(get_object_directory());
717 struct alternate_object_database
*alt
;
718 struct packed_git
*p
;
720 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
721 char namebuf
[PATH_MAX
];
722 int namelen
= alt
->name
- alt
->base
;
723 memcpy(namebuf
, alt
->base
, namelen
);
724 namebuf
[namelen
- 1] = 0;
725 fsck_object_dir(namebuf
);
727 prepare_packed_git();
728 for (p
= packed_git
; p
; p
= p
->next
)
729 /* verify gives error messages itself */
732 for (p
= packed_git
; p
; p
= p
->next
) {
734 if (open_pack_index(p
))
736 num
= p
->num_objects
;
737 for (i
= 0; i
< num
; i
++)
738 fsck_sha1(nth_packed_object_sha1(p
, i
));
743 for (i
= 1; i
< argc
; i
++) {
744 const char *arg
= argv
[i
];
749 if (!get_sha1(arg
, head_sha1
)) {
750 struct object
*obj
= lookup_object(head_sha1
);
752 /* Error is printed by lookup_object(). */
757 mark_reachable(obj
, REACHABLE
);
761 error("invalid parameter: expected sha1, got '%s'", arg
);
765 * If we've not been given any explicit head information, do the
766 * default ones from .git/refs. We also consider the index file
767 * in this case (ie this implies --cache).
771 keep_cache_objects
= 1;
774 if (keep_cache_objects
) {
777 for (i
= 0; i
< active_nr
; i
++) {
782 mode
= ntohl(active_cache
[i
]->ce_mode
);
783 if (S_ISGITLINK(mode
))
785 blob
= lookup_blob(active_cache
[i
]->sha1
);
790 mark_reachable(obj
, REACHABLE
);
792 if (active_cache_tree
)
793 fsck_cache_tree(active_cache_tree
);
796 check_connectivity();