[PATCH] Show more details of packfile with verify-pack -v.
[alt-git.git] / fsck-cache.c
blob44ad5f169530fb7ab70a17f2e04b8367643faad6
1 #include <sys/types.h>
2 #include <dirent.h>
4 #include "cache.h"
5 #include "commit.h"
6 #include "tree.h"
7 #include "blob.h"
8 #include "tag.h"
9 #include "pack.h"
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)
23 int i;
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;
30 if (!obj->parsed) {
31 if (!standalone && has_sha1_file(obj->sha1))
32 ; /* it is in pack */
33 else
34 printf("missing %s %s\n",
35 obj->type, sha1_to_hex(obj->sha1));
36 continue;
39 for (refs = obj->refs; refs; refs = refs->next) {
40 if (refs->item->parsed ||
41 (!standalone && has_sha1_file(refs->item->sha1)))
42 continue;
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));
52 continue;
55 if (!obj->used) {
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;
78 unsigned char c1, c2;
79 int cmp;
81 cmp = memcmp(a->name, b->name, len);
82 if (cmp < 0)
83 return 0;
84 if (cmp > 0)
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.
92 c1 = a->name[len];
93 c2 = b->name[len];
94 if (!c1 && !c2)
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)
102 c1 = '/';
103 if (!c2 && b->directory)
104 c2 = '/';
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;
113 last = NULL;
114 for (entry = item->entries; entry; entry = entry->next) {
115 if (strchr(entry->name, '/'))
116 has_full_path = 1;
118 switch (entry->mode) {
120 * Standard modes..
122 case S_IFREG | 0755:
123 case S_IFREG | 0644:
124 case S_IFLNK:
125 case S_IFDIR:
126 break;
128 * This is nonstandard, but we had a few of these
129 * early on when we honored the full set of mode
130 * bits..
132 case S_IFREG | 0664:
133 break;
134 default:
135 printf("tree %s has entry %o %s\n",
136 sha1_to_hex(item->object.sha1),
137 entry->mode, entry->name);
140 if (last) {
141 switch (verify_ordered(last, entry)) {
142 case TREE_UNORDERED:
143 fprintf(stderr, "tree %s not ordered\n",
144 sha1_to_hex(item->object.sha1));
145 return -1;
146 case TREE_HAS_DUPS:
147 fprintf(stderr, "tree %s has duplicate entries for '%s'\n",
148 sha1_to_hex(item->object.sha1),
149 entry->name);
150 return -1;
151 default:
152 break;
156 last = entry;
159 if (has_full_path) {
160 fprintf(stderr, "warning: git-fsck-cache: tree %s "
161 "has full pathnames in it\n",
162 sha1_to_hex(item->object.sha1));
165 return 0;
168 static int fsck_commit(struct commit *commit)
170 free(commit->buffer);
171 commit->buffer = NULL;
172 if (!commit->tree)
173 return -1;
174 if (!commit->parents && show_root)
175 printf("root %s\n", sha1_to_hex(commit->object.sha1));
176 if (!commit->date)
177 printf("bad commit date in %s\n",
178 sha1_to_hex(commit->object.sha1));
179 return 0;
182 static int fsck_tag(struct tag *tag)
184 struct object *tagged = tag->tagged;
186 if (!tagged) {
187 printf("bad object in tag %s\n", sha1_to_hex(tag->object.sha1));
188 return -1;
190 if (!show_tags)
191 return 0;
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));
195 return 0;
198 static int fsck_sha1(unsigned char *sha1)
200 struct object *obj = parse_object(sha1);
201 if (!obj)
202 return -1;
203 if (obj->type == blob_type)
204 return 0;
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);
211 return -1;
215 * This is the sorting chunk size: make it reasonably
216 * big so that we can sort well..
218 #define MAX_SHA1_ENTRIES (1024)
220 struct sha1_entry {
221 unsigned long ino;
222 unsigned char sha1[20];
225 static struct {
226 unsigned long nr;
227 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
228 } sha1_list;
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));
249 free(entry);
251 sha1_list.nr = 0;
254 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
256 struct sha1_entry *entry = xmalloc(sizeof(*entry));
257 int nr;
259 entry->ino = ino;
260 memcpy(entry->sha1, sha1, 20);
261 nr = sha1_list.nr;
262 if (nr == MAX_SHA1_ENTRIES) {
263 fsck_sha1_list();
264 nr = 0;
266 sha1_list.entry[nr] = entry;
267 sha1_list.nr = ++nr;
270 static int fsck_dir(int i, char *path)
272 DIR *dir = opendir(path);
273 struct dirent *de;
275 if (!dir) {
276 return error("missing sha1 directory '%s'", path);
279 while ((de = readdir(dir)) != NULL) {
280 char name[100];
281 unsigned char sha1[20];
282 int len = strlen(de->d_name);
284 switch (len) {
285 case 2:
286 if (de->d_name[1] != '.')
287 break;
288 case 1:
289 if (de->d_name[0] != '.')
290 break;
291 continue;
292 case 38:
293 sprintf(name, "%02x", i);
294 memcpy(name+2, de->d_name, len+1);
295 if (get_sha1_hex(name, sha1) < 0)
296 break;
297 add_sha1_list(sha1, de->d_ino);
298 continue;
300 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
302 closedir(dir);
303 return 0;
306 static int read_sha1_reference(const char *path)
308 char hexname[60];
309 unsigned char sha1[20];
310 int fd = open(path, O_RDONLY), len;
311 struct object *obj;
313 if (fd < 0)
314 return -1;
316 len = read(fd, hexname, sizeof(hexname));
317 close(fd);
318 if (len < 40)
319 return -1;
321 if (get_sha1_hex(hexname, sha1) < 0)
322 return -1;
324 obj = lookup_object(sha1);
325 if (!obj) {
326 if (!standalone && has_sha1_file(sha1))
327 return 0; /* it is in pack */
328 return error("%s: invalid sha1 pointer %.40s", path, hexname);
331 obj->used = 1;
332 mark_reachable(obj, REACHABLE);
333 return 0;
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);
341 struct stat st;
343 memcpy(path, base, baselen);
344 path[baselen] = '/';
345 memcpy(path + baselen + 1, name, namelen+1);
346 if (stat(path, &st) < 0)
347 return 0;
350 * Recurse into directories
352 if (S_ISDIR(st.st_mode)) {
353 int count = 0;
354 DIR *dir = opendir(path);
355 if (dir) {
356 struct dirent *de;
357 while ((de = readdir(dir)) != NULL) {
358 if (de->d_name[0] == '.')
359 continue;
360 count += find_file_objects(path, de->d_name);
362 closedir(dir);
364 return count;
366 if (S_ISREG(st.st_mode))
367 return read_sha1_reference(path) == 0;
368 return 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");
375 if (!count)
376 die("No default references");
379 static void fsck_object_dir(const char *path)
381 int i;
382 for (i = 0; i < 256; i++) {
383 static char dir[4096];
384 sprintf(dir, "%s/%02x", path, i);
385 fsck_dir(i, dir);
387 fsck_sha1_list();
390 int main(int argc, char **argv)
392 int i, heads;
394 for (i = 1; i < argc; i++) {
395 const char *arg = argv[i];
397 if (!strcmp(arg, "--unreachable")) {
398 show_unreachable = 1;
399 continue;
401 if (!strcmp(arg, "--tags")) {
402 show_tags = 1;
403 continue;
405 if (!strcmp(arg, "--root")) {
406 show_root = 1;
407 continue;
409 if (!strcmp(arg, "--cache")) {
410 keep_cache_objects = 1;
411 continue;
413 if (!strcmp(arg, "--standalone")) {
414 standalone = 1;
415 continue;
417 if (!strcmp(arg, "--full")) {
418 check_full = 1;
419 continue;
421 if (*arg == '-')
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.");
427 if (standalone)
428 unsetenv("GIT_ALTERNATE_OBJECT_DIRECTORIES");
430 fsck_object_dir(get_object_directory());
431 if (check_full) {
432 int j;
433 struct packed_git *p;
434 prepare_alt_odb();
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 */
443 verify_pack(p, 0);
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));
457 heads = 0;
458 for (i = 1; i < argc; i++) {
459 const char *arg = argv[i];
461 if (*arg == '-')
462 continue;
464 if (!get_sha1(arg, head_sha1)) {
465 struct object *obj = lookup_object(head_sha1);
467 /* Error is printed by lookup_object(). */
468 if (!obj)
469 continue;
471 obj->used = 1;
472 mark_reachable(obj, REACHABLE);
473 heads++;
474 continue;
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).
484 if (!heads) {
485 get_default_heads();
486 keep_cache_objects = 1;
489 if (keep_cache_objects) {
490 int i;
491 read_cache();
492 for (i = 0; i < active_nr; i++) {
493 struct blob *blob = lookup_blob(active_cache[i]->sha1);
494 struct object *obj;
495 if (!blob)
496 continue;
497 obj = &blob->object;
498 obj->used = 1;
499 mark_reachable(obj, REACHABLE);
503 check_connectivity();
504 return 0;