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 check_full
= 0;
17 static int keep_cache_objects
= 0;
18 static unsigned char head_sha1
[20];
20 static void check_connectivity(void)
24 /* Look up all the requirements, warn about missing objects.. */
25 for (i
= 0; i
< nr_objs
; i
++) {
26 struct object
*obj
= objs
[i
];
27 struct object_list
*refs
;
30 if (!standalone
&& has_sha1_file(obj
->sha1
))
33 printf("missing %s %s\n",
34 obj
->type
, sha1_to_hex(obj
->sha1
));
38 for (refs
= obj
->refs
; refs
; refs
= refs
->next
) {
39 if (refs
->item
->parsed
||
40 (!standalone
&& has_sha1_file(refs
->item
->sha1
)))
42 printf("broken link from %7s %s\n",
43 obj
->type
, sha1_to_hex(obj
->sha1
));
44 printf(" to %7s %s\n",
45 refs
->item
->type
, sha1_to_hex(refs
->item
->sha1
));
48 if (show_unreachable
&& !(obj
->flags
& REACHABLE
)) {
49 printf("unreachable %s %s\n",
50 obj
->type
, sha1_to_hex(obj
->sha1
));
55 printf("dangling %s %s\n", obj
->type
,
56 sha1_to_hex(obj
->sha1
));
62 * The entries in a tree are ordered in the _path_ order,
63 * which means that a directory entry is ordered by adding
64 * a slash to the end of it.
66 * So a directory called "a" is ordered _after_ a file
67 * called "a.c", because "a/" sorts after "a.c".
69 #define TREE_UNORDERED (-1)
70 #define TREE_HAS_DUPS (-2)
72 static int verify_ordered(struct tree_entry_list
*a
, struct tree_entry_list
*b
)
74 int len1
= strlen(a
->name
);
75 int len2
= strlen(b
->name
);
76 int len
= len1
< len2
? len1
: len2
;
80 cmp
= memcmp(a
->name
, b
->name
, len
);
84 return TREE_UNORDERED
;
87 * Ok, the first <len> characters are the same.
88 * Now we need to order the next one, but turn
89 * a '\0' into a '/' for a directory entry.
95 * git-write-tree used to write out a nonsense tree that has
96 * entries with the same name, one blob and one tree. Make
97 * sure we do not have duplicate entries.
100 if (!c1
&& a
->directory
)
102 if (!c2
&& b
->directory
)
104 return c1
< c2
? 0 : TREE_UNORDERED
;
107 static int fsck_tree(struct tree
*item
)
109 int has_full_path
= 0;
110 struct tree_entry_list
*entry
, *last
;
113 for (entry
= item
->entries
; entry
; entry
= entry
->next
) {
114 if (strchr(entry
->name
, '/'))
117 switch (entry
->mode
) {
127 * This is nonstandard, but we had a few of these
128 * early on when we honored the full set of mode
134 printf("tree %s has entry %o %s\n",
135 sha1_to_hex(item
->object
.sha1
),
136 entry
->mode
, entry
->name
);
140 switch (verify_ordered(last
, entry
)) {
142 fprintf(stderr
, "tree %s not ordered\n",
143 sha1_to_hex(item
->object
.sha1
));
146 fprintf(stderr
, "tree %s has duplicate entries for '%s'\n",
147 sha1_to_hex(item
->object
.sha1
),
159 fprintf(stderr
, "warning: git-fsck-cache: tree %s "
160 "has full pathnames in it\n",
161 sha1_to_hex(item
->object
.sha1
));
167 static int fsck_commit(struct commit
*commit
)
169 free(commit
->buffer
);
170 commit
->buffer
= NULL
;
173 if (!commit
->parents
&& show_root
)
174 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
176 printf("bad commit date in %s\n",
177 sha1_to_hex(commit
->object
.sha1
));
181 static int fsck_tag(struct tag
*tag
)
183 struct object
*tagged
= tag
->tagged
;
186 printf("bad object in tag %s\n", sha1_to_hex(tag
->object
.sha1
));
192 printf("tagged %s %s", tagged
->type
, sha1_to_hex(tagged
->sha1
));
193 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
197 static int fsck_sha1(unsigned char *sha1
)
199 struct object
*obj
= parse_object(sha1
);
202 if (obj
->type
== blob_type
)
204 if (obj
->type
== tree_type
)
205 return fsck_tree((struct tree
*) obj
);
206 if (obj
->type
== commit_type
)
207 return fsck_commit((struct commit
*) obj
);
208 if (obj
->type
== tag_type
)
209 return fsck_tag((struct tag
*) obj
);
214 * This is the sorting chunk size: make it reasonably
215 * big so that we can sort well..
217 #define MAX_SHA1_ENTRIES (1024)
221 unsigned char sha1
[20];
226 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
229 static int ino_compare(const void *_a
, const void *_b
)
231 const struct sha1_entry
*a
= _a
, *b
= _b
;
232 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
233 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
236 static void fsck_sha1_list(void)
238 int i
, nr
= sha1_list
.nr
;
240 qsort(sha1_list
.entry
, nr
, sizeof(struct sha1_entry
*), ino_compare
);
241 for (i
= 0; i
< nr
; i
++) {
242 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
243 unsigned char *sha1
= entry
->sha1
;
245 sha1_list
.entry
[i
] = NULL
;
246 if (fsck_sha1(sha1
) < 0)
247 fprintf(stderr
, "bad sha1 entry '%s'\n", sha1_to_hex(sha1
));
253 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
255 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
259 memcpy(entry
->sha1
, sha1
, 20);
261 if (nr
== MAX_SHA1_ENTRIES
) {
265 sha1_list
.entry
[nr
] = entry
;
269 static int fsck_dir(int i
, char *path
)
271 DIR *dir
= opendir(path
);
275 return error("missing sha1 directory '%s'", path
);
278 while ((de
= readdir(dir
)) != NULL
) {
280 unsigned char sha1
[20];
281 int len
= strlen(de
->d_name
);
285 if (de
->d_name
[1] != '.')
288 if (de
->d_name
[0] != '.')
292 sprintf(name
, "%02x", i
);
293 memcpy(name
+2, de
->d_name
, len
+1);
294 if (get_sha1_hex(name
, sha1
) < 0)
296 add_sha1_list(sha1
, de
->d_ino
);
299 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
305 static int read_sha1_reference(const char *path
)
308 unsigned char sha1
[20];
309 int fd
= open(path
, O_RDONLY
), len
;
315 len
= read(fd
, hexname
, sizeof(hexname
));
320 if (get_sha1_hex(hexname
, sha1
) < 0)
323 obj
= lookup_object(sha1
);
325 if (!standalone
&& has_sha1_file(sha1
))
326 return 0; /* it is in pack */
327 return error("%s: invalid sha1 pointer %.40s", path
, hexname
);
331 mark_reachable(obj
, REACHABLE
);
335 static int find_file_objects(const char *base
, const char *name
)
337 int baselen
= strlen(base
);
338 int namelen
= strlen(name
);
339 char *path
= xmalloc(baselen
+ namelen
+ 2);
342 memcpy(path
, base
, baselen
);
344 memcpy(path
+ baselen
+ 1, name
, namelen
+1);
345 if (stat(path
, &st
) < 0)
349 * Recurse into directories
351 if (S_ISDIR(st
.st_mode
)) {
353 DIR *dir
= opendir(path
);
356 while ((de
= readdir(dir
)) != NULL
) {
357 if (de
->d_name
[0] == '.')
359 count
+= find_file_objects(path
, de
->d_name
);
365 if (S_ISREG(st
.st_mode
))
366 return read_sha1_reference(path
) == 0;
370 static void get_default_heads(void)
372 char *git_dir
= gitenv(GIT_DIR_ENVIRONMENT
) ? : DEFAULT_GIT_DIR_ENVIRONMENT
;
373 int count
= find_file_objects(git_dir
, "refs");
375 die("No default references");
378 static void fsck_object_dir(const char *path
)
381 for (i
= 0; i
< 256; i
++) {
382 static char dir
[4096];
383 sprintf(dir
, "%s/%02x", path
, i
);
389 int main(int argc
, char **argv
)
393 for (i
= 1; i
< argc
; i
++) {
394 const char *arg
= argv
[i
];
396 if (!strcmp(arg
, "--unreachable")) {
397 show_unreachable
= 1;
400 if (!strcmp(arg
, "--tags")) {
404 if (!strcmp(arg
, "--root")) {
408 if (!strcmp(arg
, "--cache")) {
409 keep_cache_objects
= 1;
412 if (!strcmp(arg
, "--standalone")) {
416 if (!strcmp(arg
, "--full")) {
421 usage("git-fsck-cache [--tags] [[--unreachable] [--cache] [--standalone | --full] <head-sha1>*]");
424 if (standalone
&& check_full
)
425 die("Only one of --standalone or --full can be used.");
427 unsetenv("GIT_ALTERNATE_OBJECT_DIRECTORIES");
429 fsck_object_dir(get_object_directory());
432 struct packed_git
*p
;
434 for (j
= 0; alt_odb
[j
].base
; j
++) {
435 alt_odb
[j
].name
[-1] = 0; /* was slash */
436 fsck_object_dir(alt_odb
[j
].base
);
437 alt_odb
[j
].name
[-1] = '/';
439 prepare_packed_git();
440 for (p
= packed_git
; p
; p
= p
->next
) {
441 int num
= num_packed_objects(p
);
442 for (i
= 0; i
< num
; i
++) {
443 unsigned char sha1
[20];
444 nth_packed_object_sha1(p
, i
, sha1
);
445 if (fsck_sha1(sha1
) < 0)
446 fprintf(stderr
, "bad sha1 entry '%s'\n", sha1_to_hex(sha1
));
453 for (i
= 1; i
< argc
; i
++) {
454 const char *arg
= argv
[i
];
459 if (!get_sha1(arg
, head_sha1
)) {
460 struct object
*obj
= lookup_object(head_sha1
);
462 /* Error is printed by lookup_object(). */
467 mark_reachable(obj
, REACHABLE
);
471 error("expected sha1, got %s", arg
);
475 * If we've not been given any explicit head information, do the
476 * default ones from .git/refs. We also consider the index file
477 * in this case (ie this implies --cache).
481 keep_cache_objects
= 1;
484 if (keep_cache_objects
) {
487 for (i
= 0; i
< active_nr
; i
++) {
488 struct blob
*blob
= lookup_blob(active_cache
[i
]->sha1
);
494 mark_reachable(obj
, REACHABLE
);
498 check_connectivity();