]> git.ipfire.org Git - thirdparty/git.git/blame - fsck-cache.c
Make "fsck-cache" print out all the root commits it finds.
[thirdparty/git.git] / fsck-cache.c
CommitLineData
20222118
LT
1#include "cache.h"
2
3#include <sys/types.h>
4#include <dirent.h>
5
6/*
1ea34e36 7 * These two functions should build up a graph in memory about
20222118
LT
8 * what objects we've referenced, and found, and types..
9 *
10 * Right now we don't do that kind of reachability checking. Yet.
11 */
1ea34e36 12static void mark_needs_sha1(unsigned char *parent, const char * type, unsigned char *child)
20222118
LT
13{
14}
15
1ea34e36 16static int mark_sha1_seen(unsigned char *sha1, char *tag)
20222118 17{
1ea34e36 18 return 0;
20222118
LT
19}
20
1ea34e36 21static int fsck_tree(unsigned char *sha1, void *data, unsigned long size)
20222118 22{
1ea34e36
LT
23 while (size) {
24 int len = 1+strlen(data);
25 unsigned char *file_sha1 = data + len;
26 char *path = strchr(data, ' ');
27 if (size < len + 20 || !path)
28 return -1;
29 data += len + 20;
30 size -= len + 20;
31 mark_needs_sha1(sha1, "blob", file_sha1);
32 }
59c1e249 33 return 0;
1ea34e36
LT
34}
35
36static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
37{
16d4d1ba 38 int parents;
1ea34e36
LT
39 unsigned char tree_sha1[20];
40 unsigned char parent_sha1[20];
41
42 if (memcmp(data, "tree ", 5))
43 return -1;
44 if (get_sha1_hex(data + 5, tree_sha1) < 0)
45 return -1;
46 mark_needs_sha1(sha1, "tree", tree_sha1);
47 data += 5 + 40 + 1; /* "tree " + <hex sha1> + '\n' */
16d4d1ba 48 parents = 0;
1ea34e36
LT
49 while (!memcmp(data, "parent ", 7)) {
50 if (get_sha1_hex(data + 7, parent_sha1) < 0)
51 return -1;
52 mark_needs_sha1(sha1, "commit", parent_sha1);
53 data += 7 + 40 + 1; /* "parent " + <hex sha1> + '\n' */
16d4d1ba 54 parents++;
1ea34e36 55 }
16d4d1ba
LT
56 if (!parents)
57 printf("root: %s\n", sha1_to_hex(sha1));
59c1e249 58 return 0;
20222118
LT
59}
60
61static int fsck_entry(unsigned char *sha1, char *tag, void *data, unsigned long size)
62{
1ea34e36 63 if (!strcmp(tag, "blob")) {
20222118 64 /* Nothing to check */;
1ea34e36
LT
65 } else if (!strcmp(tag, "tree")) {
66 if (fsck_tree(sha1, data, size) < 0)
67 return -1;
68 } else if (!strcmp(tag, "commit")) {
69 if (fsck_commit(sha1, data, size) < 0)
70 return -1;
71 } else
20222118
LT
72 return -1;
73 return mark_sha1_seen(sha1, tag);
74}
75
76static int fsck_name(char *hex)
77{
78 unsigned char sha1[20];
79 if (!get_sha1_hex(hex, sha1)) {
80 unsigned long mapsize;
81 void *map = map_sha1_file(sha1, &mapsize);
82 if (map) {
83 char type[100];
84 unsigned long size;
85 void *buffer = NULL;
86 if (!check_sha1_signature(sha1, map, mapsize))
87 buffer = unpack_sha1_file(map, mapsize, type, &size);
88 munmap(map, mapsize);
89 if (buffer && !fsck_entry(sha1, type, buffer, size))
90 return 0;
91 }
92 }
93 return -1;
94}
95
96static int fsck_dir(int i, char *path)
97{
98 DIR *dir = opendir(path);
99 struct dirent *de;
100
101 if (!dir) {
102 fprintf(stderr, "missing sha1 directory '%s'", path);
103 return -1;
104 }
105
106 while ((de = readdir(dir)) != NULL) {
107 char name[100];
108 int len = strlen(de->d_name);
109
110 switch (len) {
111 case 2:
112 if (de->d_name[1] != '.')
113 break;
114 case 1:
115 if (de->d_name[0] != '.')
116 break;
117 continue;
118 case 38:
119 sprintf(name, "%02x", i);
120 memcpy(name+2, de->d_name, len+1);
121 if (!fsck_name(name))
122 continue;
123 }
124 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
125 }
126 closedir(dir);
127 return 0;
128}
129
130int main(int argc, char **argv)
131{
132 int i;
133 char *sha1_dir;
134
135 if (argc != 1)
136 usage("fsck-cache");
137 sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
138 for (i = 0; i < 256; i++) {
139 static char dir[4096];
140 sprintf(dir, "%s/%02x", sha1_dir, i);
141 fsck_dir(i, dir);
142 }
143 return 0;
144}