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 show_max_delta_depth
= 0;
17 static int keep_cache_objects
= 0;
18 static unsigned char head_sha1
[20];
20 static void expand_deltas(void)
25 * To be as efficient as possible we look for delta heads and
26 * recursively process them going backward, and parsing
27 * resulting objects along the way. This allows for processing
28 * each delta objects only once regardless of the delta depth.
30 for (i
= 0; i
< nr_objs
; i
++) {
31 struct object
*obj
= objs
[i
];
32 if (obj
->parsed
&& !obj
->delta
&& obj
->attached_deltas
) {
36 void *buf
= read_sha1_file(obj
->sha1
, type
, &size
);
39 depth
= process_deltas(buf
, size
, obj
->type
,
40 obj
->attached_deltas
);
41 if (max_depth
< depth
)
45 if (show_max_delta_depth
)
46 printf("maximum delta depth = %d\n", max_depth
);
49 static void check_connectivity(void)
53 /* Look up all the requirements, warn about missing objects.. */
54 for (i
= 0; i
< nr_objs
; i
++) {
55 struct object
*obj
= objs
[i
];
56 struct object_list
*refs
;
60 printf("unresolved delta %s\n",
61 sha1_to_hex(obj
->sha1
));
63 printf("missing %s %s\n",
64 obj
->type
, sha1_to_hex(obj
->sha1
));
68 for (refs
= obj
->refs
; refs
; refs
= refs
->next
) {
69 if (refs
->item
->parsed
)
71 printf("broken link from %7s %s\n",
72 obj
->type
, sha1_to_hex(obj
->sha1
));
73 printf(" to %7s %s\n",
74 refs
->item
->type
, sha1_to_hex(refs
->item
->sha1
));
77 /* Don't bother with tag reachability. */
78 if (obj
->type
== tag_type
)
81 if (show_unreachable
&& !(obj
->flags
& REACHABLE
)) {
82 if (obj
->attached_deltas
)
83 printf("foreign delta reference %s\n",
84 sha1_to_hex(obj
->sha1
));
86 printf("unreachable %s %s\n",
87 obj
->type
, sha1_to_hex(obj
->sha1
));
92 printf("dangling %s %s\n", obj
->type
,
93 sha1_to_hex(obj
->sha1
));
99 * The entries in a tree are ordered in the _path_ order,
100 * which means that a directory entry is ordered by adding
101 * a slash to the end of it.
103 * So a directory called "a" is ordered _after_ a file
104 * called "a.c", because "a/" sorts after "a.c".
106 #define TREE_UNORDERED (-1)
107 #define TREE_HAS_DUPS (-2)
109 static int verify_ordered(struct tree_entry_list
*a
, struct tree_entry_list
*b
)
111 int len1
= strlen(a
->name
);
112 int len2
= strlen(b
->name
);
113 int len
= len1
< len2
? len1
: len2
;
114 unsigned char c1
, c2
;
117 cmp
= memcmp(a
->name
, b
->name
, len
);
121 return TREE_UNORDERED
;
124 * Ok, the first <len> characters are the same.
125 * Now we need to order the next one, but turn
126 * a '\0' into a '/' for a directory entry.
132 * git-write-tree used to write out a nonsense tree that has
133 * entries with the same name, one blob and one tree. Make
134 * sure we do not have duplicate entries.
136 return TREE_HAS_DUPS
;
137 if (!c1
&& a
->directory
)
139 if (!c2
&& b
->directory
)
141 return c1
< c2
? 0 : TREE_UNORDERED
;
144 static int fsck_tree(struct tree
*item
)
146 int has_full_path
= 0;
147 struct tree_entry_list
*entry
, *last
;
150 for (entry
= item
->entries
; entry
; entry
= entry
->next
) {
151 if (strchr(entry
->name
, '/'))
154 switch (entry
->mode
) {
164 * This is nonstandard, but we had a few of these
165 * early on when we honored the full set of mode
171 printf("tree %s has entry %o %s\n",
172 sha1_to_hex(item
->object
.sha1
),
173 entry
->mode
, entry
->name
);
177 switch (verify_ordered(last
, entry
)) {
179 fprintf(stderr
, "tree %s not ordered\n",
180 sha1_to_hex(item
->object
.sha1
));
183 fprintf(stderr
, "tree %s has duplicate entries for '%s'\n",
184 sha1_to_hex(item
->object
.sha1
),
196 fprintf(stderr
, "warning: git-fsck-cache: tree %s "
197 "has full pathnames in it\n",
198 sha1_to_hex(item
->object
.sha1
));
204 static int fsck_commit(struct commit
*commit
)
206 free(commit
->buffer
);
207 commit
->buffer
= NULL
;
210 if (!commit
->parents
&& show_root
)
211 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
213 printf("bad commit date in %s\n",
214 sha1_to_hex(commit
->object
.sha1
));
218 static int fsck_tag(struct tag
*tag
)
220 struct object
*tagged
= tag
->tagged
;
223 printf("bad object in tag %s\n", sha1_to_hex(tag
->object
.sha1
));
229 printf("tagged %s %s", tagged
->type
, sha1_to_hex(tagged
->sha1
));
230 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
234 static int fsck_sha1(unsigned char *sha1
)
236 struct object
*obj
= parse_object(sha1
);
239 if (obj
->type
== blob_type
)
241 if (obj
->type
== tree_type
)
242 return fsck_tree((struct tree
*) obj
);
243 if (obj
->type
== commit_type
)
244 return fsck_commit((struct commit
*) obj
);
245 if (obj
->type
== tag_type
)
246 return fsck_tag((struct tag
*) obj
);
247 if (!obj
->type
&& obj
->delta
)
253 * This is the sorting chunk size: make it reasonably
254 * big so that we can sort well..
256 #define MAX_SHA1_ENTRIES (1024)
260 unsigned char sha1
[20];
265 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
268 static int ino_compare(const void *_a
, const void *_b
)
270 const struct sha1_entry
*a
= _a
, *b
= _b
;
271 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
272 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
275 static void fsck_sha1_list(void)
277 int i
, nr
= sha1_list
.nr
;
279 qsort(sha1_list
.entry
, nr
, sizeof(struct sha1_entry
*), ino_compare
);
280 for (i
= 0; i
< nr
; i
++) {
281 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
282 unsigned char *sha1
= entry
->sha1
;
284 sha1_list
.entry
[i
] = NULL
;
285 if (fsck_sha1(sha1
) < 0)
286 fprintf(stderr
, "bad sha1 entry '%s'\n", sha1_to_hex(sha1
));
292 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
294 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
298 memcpy(entry
->sha1
, sha1
, 20);
300 if (nr
== MAX_SHA1_ENTRIES
) {
304 sha1_list
.entry
[nr
] = entry
;
308 static int fsck_dir(int i
, char *path
)
310 DIR *dir
= opendir(path
);
314 return error("missing sha1 directory '%s'", path
);
317 while ((de
= readdir(dir
)) != NULL
) {
319 unsigned char sha1
[20];
320 int len
= strlen(de
->d_name
);
324 if (de
->d_name
[1] != '.')
327 if (de
->d_name
[0] != '.')
331 sprintf(name
, "%02x", i
);
332 memcpy(name
+2, de
->d_name
, len
+1);
333 if (get_sha1_hex(name
, sha1
) < 0)
335 add_sha1_list(sha1
, de
->d_ino
);
338 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
344 static int read_sha1_reference(const char *path
)
347 unsigned char sha1
[20];
348 int fd
= open(path
, O_RDONLY
), len
;
354 len
= read(fd
, hexname
, sizeof(hexname
));
359 if (get_sha1_hex(hexname
, sha1
) < 0)
362 obj
= lookup_object(sha1
);
364 return error("%s: invalid sha1 pointer %.40s", path
, hexname
);
367 mark_reachable(obj
, REACHABLE
);
371 static int find_file_objects(const char *base
, const char *name
)
373 int baselen
= strlen(base
);
374 int namelen
= strlen(name
);
375 char *path
= xmalloc(baselen
+ namelen
+ 2);
378 memcpy(path
, base
, baselen
);
380 memcpy(path
+ baselen
+ 1, name
, namelen
+1);
381 if (stat(path
, &st
) < 0)
385 * Recurse into directories
387 if (S_ISDIR(st
.st_mode
)) {
389 DIR *dir
= opendir(path
);
392 while ((de
= readdir(dir
)) != NULL
) {
393 if (de
->d_name
[0] == '.')
395 count
+= find_file_objects(path
, de
->d_name
);
401 if (S_ISREG(st
.st_mode
))
402 return read_sha1_reference(path
) == 0;
406 static void get_default_heads(void)
408 char *git_dir
= gitenv(GIT_DIR_ENVIRONMENT
) ? : DEFAULT_GIT_DIR_ENVIRONMENT
;
409 int count
= find_file_objects(git_dir
, "refs");
411 die("No default references");
414 int main(int argc
, char **argv
)
419 for (i
= 1; i
< argc
; i
++) {
420 const char *arg
= argv
[i
];
422 if (!strcmp(arg
, "--unreachable")) {
423 show_unreachable
= 1;
426 if (!strcmp(arg
, "--tags")) {
430 if (!strcmp(arg
, "--root")) {
434 if (!strcmp(arg
, "--delta-depth")) {
435 show_max_delta_depth
= 1;
438 if (!strcmp(arg
, "--cache")) {
439 keep_cache_objects
= 1;
443 usage("git-fsck-cache [--tags] [[--unreachable] [--cache] <head-sha1>*]");
446 sha1_dir
= get_object_directory();
447 for (i
= 0; i
< 256; i
++) {
448 static char dir
[4096];
449 sprintf(dir
, "%s/%02x", sha1_dir
, i
);
457 for (i
= 1; i
< argc
; i
++) {
458 const char *arg
= argv
[i
];
463 if (!get_sha1(arg
, head_sha1
)) {
464 struct object
*obj
= lookup_object(head_sha1
);
466 /* Error is printed by lookup_object(). */
471 mark_reachable(obj
, REACHABLE
);
475 error("expected sha1, got %s", arg
);
479 * If we've not been given any explicit head information, do the
480 * default ones from .git/refs. We also consider the index file
481 * in this case (ie this implies --cache).
485 keep_cache_objects
= 1;
488 if (keep_cache_objects
) {
491 for (i
= 0; i
< active_nr
; i
++) {
492 struct blob
*blob
= lookup_blob(active_cache
[i
]->sha1
);
498 mark_reachable(obj
, REACHABLE
);
502 check_connectivity();