11 #define REACHABLE 0x0001
13 static int show_root
= 0;
14 static int show_tags
= 0;
15 static int show_unreachable
= 0;
16 static int standalone
= 0;
17 static int check_full
= 0;
18 static int keep_cache_objects
= 0;
19 static unsigned char head_sha1
[20];
21 static void check_connectivity(void)
25 /* Look up all the requirements, warn about missing objects.. */
26 for (i
= 0; i
< nr_objs
; i
++) {
27 struct object
*obj
= objs
[i
];
28 struct object_list
*refs
;
31 if (!standalone
&& has_sha1_file(obj
->sha1
))
34 printf("missing %s %s\n",
35 obj
->type
, sha1_to_hex(obj
->sha1
));
39 for (refs
= obj
->refs
; refs
; refs
= refs
->next
) {
40 if (refs
->item
->parsed
||
41 (!standalone
&& has_sha1_file(refs
->item
->sha1
)))
43 printf("broken link from %7s %s\n",
44 obj
->type
, sha1_to_hex(obj
->sha1
));
45 printf(" to %7s %s\n",
46 refs
->item
->type
, sha1_to_hex(refs
->item
->sha1
));
49 if (show_unreachable
&& !(obj
->flags
& REACHABLE
)) {
50 printf("unreachable %s %s\n",
51 obj
->type
, sha1_to_hex(obj
->sha1
));
56 printf("dangling %s %s\n", obj
->type
,
57 sha1_to_hex(obj
->sha1
));
63 * The entries in a tree are ordered in the _path_ order,
64 * which means that a directory entry is ordered by adding
65 * a slash to the end of it.
67 * So a directory called "a" is ordered _after_ a file
68 * called "a.c", because "a/" sorts after "a.c".
70 #define TREE_UNORDERED (-1)
71 #define TREE_HAS_DUPS (-2)
73 static int verify_ordered(struct tree_entry_list
*a
, struct tree_entry_list
*b
)
75 int len1
= strlen(a
->name
);
76 int len2
= strlen(b
->name
);
77 int len
= len1
< len2
? len1
: len2
;
81 cmp
= memcmp(a
->name
, b
->name
, len
);
85 return TREE_UNORDERED
;
88 * Ok, the first <len> characters are the same.
89 * Now we need to order the next one, but turn
90 * a '\0' into a '/' for a directory entry.
96 * git-write-tree used to write out a nonsense tree that has
97 * entries with the same name, one blob and one tree. Make
98 * sure we do not have duplicate entries.
100 return TREE_HAS_DUPS
;
101 if (!c1
&& a
->directory
)
103 if (!c2
&& b
->directory
)
105 return c1
< c2
? 0 : TREE_UNORDERED
;
108 static int fsck_tree(struct tree
*item
)
110 int has_full_path
= 0;
111 struct tree_entry_list
*entry
, *last
;
114 for (entry
= item
->entries
; entry
; entry
= entry
->next
) {
115 if (strchr(entry
->name
, '/'))
118 switch (entry
->mode
) {
128 * This is nonstandard, but we had a few of these
129 * early on when we honored the full set of mode
135 printf("tree %s has entry %o %s\n",
136 sha1_to_hex(item
->object
.sha1
),
137 entry
->mode
, entry
->name
);
141 switch (verify_ordered(last
, entry
)) {
143 fprintf(stderr
, "tree %s not ordered\n",
144 sha1_to_hex(item
->object
.sha1
));
147 fprintf(stderr
, "tree %s has duplicate entries for '%s'\n",
148 sha1_to_hex(item
->object
.sha1
),
160 fprintf(stderr
, "warning: git-fsck-cache: tree %s "
161 "has full pathnames in it\n",
162 sha1_to_hex(item
->object
.sha1
));
168 static int fsck_commit(struct commit
*commit
)
170 free(commit
->buffer
);
171 commit
->buffer
= NULL
;
174 if (!commit
->parents
&& show_root
)
175 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
177 printf("bad commit date in %s\n",
178 sha1_to_hex(commit
->object
.sha1
));
182 static int fsck_tag(struct tag
*tag
)
184 struct object
*tagged
= tag
->tagged
;
187 printf("bad object in tag %s\n", sha1_to_hex(tag
->object
.sha1
));
193 printf("tagged %s %s", tagged
->type
, sha1_to_hex(tagged
->sha1
));
194 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
198 static int fsck_sha1(unsigned char *sha1
)
200 struct object
*obj
= parse_object(sha1
);
203 if (obj
->type
== blob_type
)
205 if (obj
->type
== tree_type
)
206 return fsck_tree((struct tree
*) obj
);
207 if (obj
->type
== commit_type
)
208 return fsck_commit((struct commit
*) obj
);
209 if (obj
->type
== tag_type
)
210 return fsck_tag((struct tag
*) obj
);
215 * This is the sorting chunk size: make it reasonably
216 * big so that we can sort well..
218 #define MAX_SHA1_ENTRIES (1024)
222 unsigned char sha1
[20];
227 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
230 static int ino_compare(const void *_a
, const void *_b
)
232 const struct sha1_entry
*a
= _a
, *b
= _b
;
233 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
234 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
237 static void fsck_sha1_list(void)
239 int i
, nr
= sha1_list
.nr
;
241 qsort(sha1_list
.entry
, nr
, sizeof(struct sha1_entry
*), ino_compare
);
242 for (i
= 0; i
< nr
; i
++) {
243 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
244 unsigned char *sha1
= entry
->sha1
;
246 sha1_list
.entry
[i
] = NULL
;
247 if (fsck_sha1(sha1
) < 0)
248 fprintf(stderr
, "bad sha1 entry '%s'\n", sha1_to_hex(sha1
));
254 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
256 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
260 memcpy(entry
->sha1
, sha1
, 20);
262 if (nr
== MAX_SHA1_ENTRIES
) {
266 sha1_list
.entry
[nr
] = entry
;
270 static int fsck_dir(int i
, char *path
)
272 DIR *dir
= opendir(path
);
276 return error("missing sha1 directory '%s'", path
);
279 while ((de
= readdir(dir
)) != NULL
) {
281 unsigned char sha1
[20];
282 int len
= strlen(de
->d_name
);
286 if (de
->d_name
[1] != '.')
289 if (de
->d_name
[0] != '.')
293 sprintf(name
, "%02x", i
);
294 memcpy(name
+2, de
->d_name
, len
+1);
295 if (get_sha1_hex(name
, sha1
) < 0)
297 add_sha1_list(sha1
, de
->d_ino
);
300 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
306 static int read_sha1_reference(const char *path
)
309 unsigned char sha1
[20];
310 int fd
= open(path
, O_RDONLY
), len
;
316 len
= read(fd
, hexname
, sizeof(hexname
));
321 if (get_sha1_hex(hexname
, sha1
) < 0)
324 obj
= lookup_object(sha1
);
326 if (!standalone
&& has_sha1_file(sha1
))
327 return 0; /* it is in pack */
328 return error("%s: invalid sha1 pointer %.40s", path
, hexname
);
332 mark_reachable(obj
, REACHABLE
);
336 static int find_file_objects(const char *base
, const char *name
)
338 int baselen
= strlen(base
);
339 int namelen
= strlen(name
);
340 char *path
= xmalloc(baselen
+ namelen
+ 2);
343 memcpy(path
, base
, baselen
);
345 memcpy(path
+ baselen
+ 1, name
, namelen
+1);
346 if (stat(path
, &st
) < 0)
350 * Recurse into directories
352 if (S_ISDIR(st
.st_mode
)) {
354 DIR *dir
= opendir(path
);
357 while ((de
= readdir(dir
)) != NULL
) {
358 if (de
->d_name
[0] == '.')
360 count
+= find_file_objects(path
, de
->d_name
);
366 if (S_ISREG(st
.st_mode
))
367 return read_sha1_reference(path
) == 0;
371 static void get_default_heads(void)
373 char *git_dir
= gitenv(GIT_DIR_ENVIRONMENT
) ? : DEFAULT_GIT_DIR_ENVIRONMENT
;
374 int count
= find_file_objects(git_dir
, "refs");
376 die("No default references");
379 static void fsck_object_dir(const char *path
)
382 for (i
= 0; i
< 256; i
++) {
383 static char dir
[4096];
384 sprintf(dir
, "%s/%02x", path
, i
);
390 int main(int argc
, char **argv
)
394 for (i
= 1; i
< argc
; i
++) {
395 const char *arg
= argv
[i
];
397 if (!strcmp(arg
, "--unreachable")) {
398 show_unreachable
= 1;
401 if (!strcmp(arg
, "--tags")) {
405 if (!strcmp(arg
, "--root")) {
409 if (!strcmp(arg
, "--cache")) {
410 keep_cache_objects
= 1;
413 if (!strcmp(arg
, "--standalone")) {
417 if (!strcmp(arg
, "--full")) {
422 usage("git-fsck-cache [--tags] [[--unreachable] [--cache] [--standalone | --full] <head-sha1>*]");
425 if (standalone
&& check_full
)
426 die("Only one of --standalone or --full can be used.");
428 unsetenv("GIT_ALTERNATE_OBJECT_DIRECTORIES");
430 fsck_object_dir(get_object_directory());
433 struct packed_git
*p
;
435 for (j
= 0; alt_odb
[j
].base
; j
++) {
436 alt_odb
[j
].name
[-1] = 0; /* was slash */
437 fsck_object_dir(alt_odb
[j
].base
);
438 alt_odb
[j
].name
[-1] = '/';
440 prepare_packed_git();
441 for (p
= packed_git
; p
; p
= p
->next
)
442 /* verify gives error messages itself */
445 for (p
= packed_git
; p
; p
= p
->next
) {
446 int num
= num_packed_objects(p
);
447 for (i
= 0; i
< num
; i
++) {
448 unsigned char sha1
[20];
449 nth_packed_object_sha1(p
, i
, sha1
);
450 if (fsck_sha1(sha1
) < 0)
451 fprintf(stderr
, "bad sha1 entry '%s'\n", sha1_to_hex(sha1
));
458 for (i
= 1; i
< argc
; i
++) {
459 const char *arg
= argv
[i
];
464 if (!get_sha1(arg
, head_sha1
)) {
465 struct object
*obj
= lookup_object(head_sha1
);
467 /* Error is printed by lookup_object(). */
472 mark_reachable(obj
, REACHABLE
);
476 error("expected sha1, got %s", arg
);
480 * If we've not been given any explicit head information, do the
481 * default ones from .git/refs. We also consider the index file
482 * in this case (ie this implies --cache).
486 keep_cache_objects
= 1;
489 if (keep_cache_objects
) {
492 for (i
= 0; i
< active_nr
; i
++) {
493 struct blob
*blob
= lookup_blob(active_cache
[i
]->sha1
);
499 mark_reachable(obj
, REACHABLE
);
503 check_connectivity();