12 #define REACHABLE 0x0001
14 static int show_root
= 0;
15 static int show_tags
= 0;
16 static int show_unreachable
= 0;
17 static int standalone
= 0;
18 static int check_full
= 0;
19 static int keep_cache_objects
= 0;
20 static unsigned char head_sha1
[20];
22 static void check_connectivity(void)
26 /* Look up all the requirements, warn about missing objects.. */
27 for (i
= 0; i
< nr_objs
; i
++) {
28 struct object
*obj
= objs
[i
];
29 struct object_list
*refs
;
32 if (!standalone
&& has_sha1_file(obj
->sha1
))
35 printf("missing %s %s\n",
36 obj
->type
, sha1_to_hex(obj
->sha1
));
40 for (refs
= obj
->refs
; refs
; refs
= refs
->next
) {
41 if (refs
->item
->parsed
||
42 (!standalone
&& has_sha1_file(refs
->item
->sha1
)))
44 printf("broken link from %7s %s\n",
45 obj
->type
, sha1_to_hex(obj
->sha1
));
46 printf(" to %7s %s\n",
47 refs
->item
->type
, sha1_to_hex(refs
->item
->sha1
));
50 if (show_unreachable
&& !(obj
->flags
& REACHABLE
)) {
51 printf("unreachable %s %s\n",
52 obj
->type
, sha1_to_hex(obj
->sha1
));
57 printf("dangling %s %s\n", obj
->type
,
58 sha1_to_hex(obj
->sha1
));
64 * The entries in a tree are ordered in the _path_ order,
65 * which means that a directory entry is ordered by adding
66 * a slash to the end of it.
68 * So a directory called "a" is ordered _after_ a file
69 * called "a.c", because "a/" sorts after "a.c".
71 #define TREE_UNORDERED (-1)
72 #define TREE_HAS_DUPS (-2)
74 static int verify_ordered(struct tree_entry_list
*a
, struct tree_entry_list
*b
)
76 int len1
= strlen(a
->name
);
77 int len2
= strlen(b
->name
);
78 int len
= len1
< len2
? len1
: len2
;
82 cmp
= memcmp(a
->name
, b
->name
, len
);
86 return TREE_UNORDERED
;
89 * Ok, the first <len> characters are the same.
90 * Now we need to order the next one, but turn
91 * a '\0' into a '/' for a directory entry.
97 * git-write-tree used to write out a nonsense tree that has
98 * entries with the same name, one blob and one tree. Make
99 * sure we do not have duplicate entries.
101 return TREE_HAS_DUPS
;
102 if (!c1
&& a
->directory
)
104 if (!c2
&& b
->directory
)
106 return c1
< c2
? 0 : TREE_UNORDERED
;
109 static int fsck_tree(struct tree
*item
)
111 int has_full_path
= 0;
112 struct tree_entry_list
*entry
, *last
;
115 for (entry
= item
->entries
; entry
; entry
= entry
->next
) {
116 if (strchr(entry
->name
, '/'))
119 switch (entry
->mode
) {
129 * This is nonstandard, but we had a few of these
130 * early on when we honored the full set of mode
136 printf("tree %s has entry %o %s\n",
137 sha1_to_hex(item
->object
.sha1
),
138 entry
->mode
, entry
->name
);
142 switch (verify_ordered(last
, entry
)) {
144 fprintf(stderr
, "tree %s not ordered\n",
145 sha1_to_hex(item
->object
.sha1
));
148 fprintf(stderr
, "tree %s has duplicate entries for '%s'\n",
149 sha1_to_hex(item
->object
.sha1
),
161 fprintf(stderr
, "warning: git-fsck-cache: tree %s "
162 "has full pathnames in it\n",
163 sha1_to_hex(item
->object
.sha1
));
169 static int fsck_commit(struct commit
*commit
)
171 free(commit
->buffer
);
172 commit
->buffer
= NULL
;
175 if (!commit
->parents
&& show_root
)
176 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
178 printf("bad commit date in %s\n",
179 sha1_to_hex(commit
->object
.sha1
));
183 static int fsck_tag(struct tag
*tag
)
185 struct object
*tagged
= tag
->tagged
;
188 printf("bad object in tag %s\n", sha1_to_hex(tag
->object
.sha1
));
194 printf("tagged %s %s", tagged
->type
, sha1_to_hex(tagged
->sha1
));
195 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
199 static int fsck_sha1(unsigned char *sha1
)
201 struct object
*obj
= parse_object(sha1
);
204 if (obj
->type
== blob_type
)
206 if (obj
->type
== tree_type
)
207 return fsck_tree((struct tree
*) obj
);
208 if (obj
->type
== commit_type
)
209 return fsck_commit((struct commit
*) obj
);
210 if (obj
->type
== tag_type
)
211 return fsck_tag((struct tag
*) obj
);
216 * This is the sorting chunk size: make it reasonably
217 * big so that we can sort well..
219 #define MAX_SHA1_ENTRIES (1024)
223 unsigned char sha1
[20];
228 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
231 static int ino_compare(const void *_a
, const void *_b
)
233 const struct sha1_entry
*a
= _a
, *b
= _b
;
234 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
235 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
238 static void fsck_sha1_list(void)
240 int i
, nr
= sha1_list
.nr
;
242 qsort(sha1_list
.entry
, nr
, sizeof(struct sha1_entry
*), ino_compare
);
243 for (i
= 0; i
< nr
; i
++) {
244 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
245 unsigned char *sha1
= entry
->sha1
;
247 sha1_list
.entry
[i
] = NULL
;
248 if (fsck_sha1(sha1
) < 0)
249 fprintf(stderr
, "bad sha1 entry '%s'\n", sha1_to_hex(sha1
));
255 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
257 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
261 memcpy(entry
->sha1
, sha1
, 20);
263 if (nr
== MAX_SHA1_ENTRIES
) {
267 sha1_list
.entry
[nr
] = entry
;
271 static int fsck_dir(int i
, char *path
)
273 DIR *dir
= opendir(path
);
277 return error("missing sha1 directory '%s'", path
);
280 while ((de
= readdir(dir
)) != NULL
) {
282 unsigned char sha1
[20];
283 int len
= strlen(de
->d_name
);
287 if (de
->d_name
[1] != '.')
290 if (de
->d_name
[0] != '.')
294 sprintf(name
, "%02x", i
);
295 memcpy(name
+2, de
->d_name
, len
+1);
296 if (get_sha1_hex(name
, sha1
) < 0)
298 add_sha1_list(sha1
, de
->d_ino
);
301 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
307 static int default_refs
= 0;
309 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
)
313 obj
= lookup_object(sha1
);
315 if (!standalone
&& has_sha1_file(sha1
)) {
317 return 0; /* it is in a pack */
319 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
320 /* We'll continue with the rest despite the error.. */
325 mark_reachable(obj
, REACHABLE
);
329 static void get_default_heads(void)
331 for_each_ref(fsck_handle_ref
);
333 die("No default references");
336 static void fsck_object_dir(const char *path
)
339 for (i
= 0; i
< 256; i
++) {
340 static char dir
[4096];
341 sprintf(dir
, "%s/%02x", path
, i
);
347 static int fsck_head_link(void)
351 unsigned char sha1
[20];
352 static char path
[PATH_MAX
], link
[PATH_MAX
];
353 const char *git_dir
= gitenv(GIT_DIR_ENVIRONMENT
) ? : DEFAULT_GIT_DIR_ENVIRONMENT
;
355 snprintf(path
, sizeof(path
), "%s/HEAD", git_dir
);
356 if (readlink(path
, link
, sizeof(link
)) < 0)
357 return error("HEAD is not a symlink");
358 if (strncmp("refs/heads/", link
, 11))
359 return error("HEAD points to something strange (%s)", link
);
360 fd
= open(path
, O_RDONLY
);
362 return error("HEAD: %s", strerror(errno
));
363 count
= read(fd
, hex
, sizeof(hex
));
366 return error("HEAD: %s", strerror(errno
));
367 if (count
< 40 || get_sha1_hex(hex
, sha1
))
368 return error("HEAD: not a valid git pointer");
372 int main(int argc
, char **argv
)
376 for (i
= 1; i
< argc
; i
++) {
377 const char *arg
= argv
[i
];
379 if (!strcmp(arg
, "--unreachable")) {
380 show_unreachable
= 1;
383 if (!strcmp(arg
, "--tags")) {
387 if (!strcmp(arg
, "--root")) {
391 if (!strcmp(arg
, "--cache")) {
392 keep_cache_objects
= 1;
395 if (!strcmp(arg
, "--standalone")) {
399 if (!strcmp(arg
, "--full")) {
404 usage("git-fsck-cache [--tags] [[--unreachable] [--cache] [--standalone | --full] <head-sha1>*]");
407 if (standalone
&& check_full
)
408 die("Only one of --standalone or --full can be used.");
410 unsetenv("GIT_ALTERNATE_OBJECT_DIRECTORIES");
413 fsck_object_dir(get_object_directory());
416 struct packed_git
*p
;
418 for (j
= 0; alt_odb
[j
].base
; j
++) {
419 alt_odb
[j
].name
[-1] = 0; /* was slash */
420 fsck_object_dir(alt_odb
[j
].base
);
421 alt_odb
[j
].name
[-1] = '/';
423 prepare_packed_git();
424 for (p
= packed_git
; p
; p
= p
->next
)
425 /* verify gives error messages itself */
428 for (p
= packed_git
; p
; p
= p
->next
) {
429 int num
= num_packed_objects(p
);
430 for (i
= 0; i
< num
; i
++) {
431 unsigned char sha1
[20];
432 nth_packed_object_sha1(p
, i
, sha1
);
433 if (fsck_sha1(sha1
) < 0)
434 fprintf(stderr
, "bad sha1 entry '%s'\n", sha1_to_hex(sha1
));
441 for (i
= 1; i
< argc
; i
++) {
442 const char *arg
= argv
[i
];
447 if (!get_sha1(arg
, head_sha1
)) {
448 struct object
*obj
= lookup_object(head_sha1
);
450 /* Error is printed by lookup_object(). */
455 mark_reachable(obj
, REACHABLE
);
459 error("expected sha1, got %s", arg
);
463 * If we've not been given any explicit head information, do the
464 * default ones from .git/refs. We also consider the index file
465 * in this case (ie this implies --cache).
469 keep_cache_objects
= 1;
472 if (keep_cache_objects
) {
475 for (i
= 0; i
< active_nr
; i
++) {
476 struct blob
*blob
= lookup_blob(active_cache
[i
]->sha1
);
482 mark_reachable(obj
, REACHABLE
);
486 check_connectivity();