10 #define REACHABLE 0x0001
12 static int show_root
= 0;
13 static int show_tags
= 0;
14 static int show_unreachable
= 0;
15 static int standalone
= 0;
16 static int keep_cache_objects
= 0;
17 static unsigned char head_sha1
[20];
19 static void check_connectivity(void)
23 /* Look up all the requirements, warn about missing objects.. */
24 for (i
= 0; i
< nr_objs
; i
++) {
25 struct object
*obj
= objs
[i
];
26 struct object_list
*refs
;
29 if (!standalone
&& has_sha1_file(obj
->sha1
))
32 printf("missing %s %s\n",
33 obj
->type
, sha1_to_hex(obj
->sha1
));
37 for (refs
= obj
->refs
; refs
; refs
= refs
->next
) {
38 if (refs
->item
->parsed
||
39 (!standalone
&& has_sha1_file(refs
->item
->sha1
)))
41 printf("broken link from %7s %s\n",
42 obj
->type
, sha1_to_hex(obj
->sha1
));
43 printf(" to %7s %s\n",
44 refs
->item
->type
, sha1_to_hex(refs
->item
->sha1
));
47 if (show_unreachable
&& !(obj
->flags
& REACHABLE
)) {
48 printf("unreachable %s %s\n",
49 obj
->type
, sha1_to_hex(obj
->sha1
));
54 printf("dangling %s %s\n", obj
->type
,
55 sha1_to_hex(obj
->sha1
));
61 * The entries in a tree are ordered in the _path_ order,
62 * which means that a directory entry is ordered by adding
63 * a slash to the end of it.
65 * So a directory called "a" is ordered _after_ a file
66 * called "a.c", because "a/" sorts after "a.c".
68 #define TREE_UNORDERED (-1)
69 #define TREE_HAS_DUPS (-2)
71 static int verify_ordered(struct tree_entry_list
*a
, struct tree_entry_list
*b
)
73 int len1
= strlen(a
->name
);
74 int len2
= strlen(b
->name
);
75 int len
= len1
< len2
? len1
: len2
;
79 cmp
= memcmp(a
->name
, b
->name
, len
);
83 return TREE_UNORDERED
;
86 * Ok, the first <len> characters are the same.
87 * Now we need to order the next one, but turn
88 * a '\0' into a '/' for a directory entry.
94 * git-write-tree used to write out a nonsense tree that has
95 * entries with the same name, one blob and one tree. Make
96 * sure we do not have duplicate entries.
99 if (!c1
&& a
->directory
)
101 if (!c2
&& b
->directory
)
103 return c1
< c2
? 0 : TREE_UNORDERED
;
106 static int fsck_tree(struct tree
*item
)
108 int has_full_path
= 0;
109 struct tree_entry_list
*entry
, *last
;
112 for (entry
= item
->entries
; entry
; entry
= entry
->next
) {
113 if (strchr(entry
->name
, '/'))
116 switch (entry
->mode
) {
126 * This is nonstandard, but we had a few of these
127 * early on when we honored the full set of mode
133 printf("tree %s has entry %o %s\n",
134 sha1_to_hex(item
->object
.sha1
),
135 entry
->mode
, entry
->name
);
139 switch (verify_ordered(last
, entry
)) {
141 fprintf(stderr
, "tree %s not ordered\n",
142 sha1_to_hex(item
->object
.sha1
));
145 fprintf(stderr
, "tree %s has duplicate entries for '%s'\n",
146 sha1_to_hex(item
->object
.sha1
),
158 fprintf(stderr
, "warning: git-fsck-cache: tree %s "
159 "has full pathnames in it\n",
160 sha1_to_hex(item
->object
.sha1
));
166 static int fsck_commit(struct commit
*commit
)
168 free(commit
->buffer
);
169 commit
->buffer
= NULL
;
172 if (!commit
->parents
&& show_root
)
173 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
175 printf("bad commit date in %s\n",
176 sha1_to_hex(commit
->object
.sha1
));
180 static int fsck_tag(struct tag
*tag
)
182 struct object
*tagged
= tag
->tagged
;
185 printf("bad object in tag %s\n", sha1_to_hex(tag
->object
.sha1
));
191 printf("tagged %s %s", tagged
->type
, sha1_to_hex(tagged
->sha1
));
192 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
196 static int fsck_sha1(unsigned char *sha1
)
198 struct object
*obj
= parse_object(sha1
);
201 if (obj
->type
== blob_type
)
203 if (obj
->type
== tree_type
)
204 return fsck_tree((struct tree
*) obj
);
205 if (obj
->type
== commit_type
)
206 return fsck_commit((struct commit
*) obj
);
207 if (obj
->type
== tag_type
)
208 return fsck_tag((struct tag
*) obj
);
213 * This is the sorting chunk size: make it reasonably
214 * big so that we can sort well..
216 #define MAX_SHA1_ENTRIES (1024)
220 unsigned char sha1
[20];
225 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
228 static int ino_compare(const void *_a
, const void *_b
)
230 const struct sha1_entry
*a
= _a
, *b
= _b
;
231 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
232 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
235 static void fsck_sha1_list(void)
237 int i
, nr
= sha1_list
.nr
;
239 qsort(sha1_list
.entry
, nr
, sizeof(struct sha1_entry
*), ino_compare
);
240 for (i
= 0; i
< nr
; i
++) {
241 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
242 unsigned char *sha1
= entry
->sha1
;
244 sha1_list
.entry
[i
] = NULL
;
245 if (fsck_sha1(sha1
) < 0)
246 fprintf(stderr
, "bad sha1 entry '%s'\n", sha1_to_hex(sha1
));
252 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
254 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
258 memcpy(entry
->sha1
, sha1
, 20);
260 if (nr
== MAX_SHA1_ENTRIES
) {
264 sha1_list
.entry
[nr
] = entry
;
268 static int fsck_dir(int i
, char *path
)
270 DIR *dir
= opendir(path
);
274 return error("missing sha1 directory '%s'", path
);
277 while ((de
= readdir(dir
)) != NULL
) {
279 unsigned char sha1
[20];
280 int len
= strlen(de
->d_name
);
284 if (de
->d_name
[1] != '.')
287 if (de
->d_name
[0] != '.')
291 sprintf(name
, "%02x", i
);
292 memcpy(name
+2, de
->d_name
, len
+1);
293 if (get_sha1_hex(name
, sha1
) < 0)
295 add_sha1_list(sha1
, de
->d_ino
);
298 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
304 static int read_sha1_reference(const char *path
)
307 unsigned char sha1
[20];
308 int fd
= open(path
, O_RDONLY
), len
;
314 len
= read(fd
, hexname
, sizeof(hexname
));
319 if (get_sha1_hex(hexname
, sha1
) < 0)
322 obj
= lookup_object(sha1
);
324 if (!standalone
&& has_sha1_file(sha1
))
325 return 0; /* it is in pack */
326 return error("%s: invalid sha1 pointer %.40s", path
, hexname
);
330 mark_reachable(obj
, REACHABLE
);
334 static int find_file_objects(const char *base
, const char *name
)
336 int baselen
= strlen(base
);
337 int namelen
= strlen(name
);
338 char *path
= xmalloc(baselen
+ namelen
+ 2);
341 memcpy(path
, base
, baselen
);
343 memcpy(path
+ baselen
+ 1, name
, namelen
+1);
344 if (stat(path
, &st
) < 0)
348 * Recurse into directories
350 if (S_ISDIR(st
.st_mode
)) {
352 DIR *dir
= opendir(path
);
355 while ((de
= readdir(dir
)) != NULL
) {
356 if (de
->d_name
[0] == '.')
358 count
+= find_file_objects(path
, de
->d_name
);
364 if (S_ISREG(st
.st_mode
))
365 return read_sha1_reference(path
) == 0;
369 static void get_default_heads(void)
371 char *git_dir
= gitenv(GIT_DIR_ENVIRONMENT
) ? : DEFAULT_GIT_DIR_ENVIRONMENT
;
372 int count
= find_file_objects(git_dir
, "refs");
374 die("No default references");
377 int main(int argc
, char **argv
)
382 for (i
= 1; i
< argc
; i
++) {
383 const char *arg
= argv
[i
];
385 if (!strcmp(arg
, "--unreachable")) {
386 show_unreachable
= 1;
389 if (!strcmp(arg
, "--tags")) {
393 if (!strcmp(arg
, "--root")) {
397 if (!strcmp(arg
, "--cache")) {
398 keep_cache_objects
= 1;
401 if (!strcmp(arg
, "--standalone")) {
406 usage("git-fsck-cache [--tags] [[--unreachable] [--cache] <head-sha1>*]");
409 sha1_dir
= get_object_directory();
410 for (i
= 0; i
< 256; i
++) {
411 static char dir
[4096];
412 sprintf(dir
, "%s/%02x", sha1_dir
, i
);
418 for (i
= 1; i
< argc
; i
++) {
419 const char *arg
= argv
[i
];
424 if (!get_sha1(arg
, head_sha1
)) {
425 struct object
*obj
= lookup_object(head_sha1
);
427 /* Error is printed by lookup_object(). */
432 mark_reachable(obj
, REACHABLE
);
436 error("expected sha1, got %s", arg
);
440 * If we've not been given any explicit head information, do the
441 * default ones from .git/refs. We also consider the index file
442 * in this case (ie this implies --cache).
446 keep_cache_objects
= 1;
449 if (keep_cache_objects
) {
452 for (i
= 0; i
< active_nr
; i
++) {
453 struct blob
*blob
= lookup_blob(active_cache
[i
]->sha1
);
459 mark_reachable(obj
, REACHABLE
);
463 check_connectivity();