Commit | Line | Data |
---|---|---|
20222118 LT |
1 | #include <sys/types.h> |
2 | #include <dirent.h> | |
3 | ||
4b182421 | 4 | #include "cache.h" |
ff5ebe39 DB |
5 | #include "commit.h" |
6 | #include "tree.h" | |
7 | #include "blob.h" | |
c418eda4 | 8 | #include "tag.h" |
ff5ebe39 DB |
9 | |
10 | #define REACHABLE 0x0001 | |
d9839e03 | 11 | |
ab7df187 | 12 | static int show_root = 0; |
889262ea | 13 | static int show_tags = 0; |
d9839e03 | 14 | static int show_unreachable = 0; |
d9839e03 LT |
15 | static unsigned char head_sha1[20]; |
16 | ||
8ba0bbb2 LT |
17 | static void check_connectivity(void) |
18 | { | |
19 | int i; | |
20 | ||
8ba0bbb2 | 21 | /* Look up all the requirements, warn about missing objects.. */ |
ff5ebe39 DB |
22 | for (i = 0; i < nr_objs; i++) { |
23 | struct object *obj = objs[i]; | |
c418eda4 | 24 | struct object_list *refs; |
8ba0bbb2 | 25 | |
ff5ebe39 | 26 | if (show_unreachable && !(obj->flags & REACHABLE)) { |
f43b8abc | 27 | printf("unreachable %s %s\n", obj->type, sha1_to_hex(obj->sha1)); |
8ba0bbb2 | 28 | continue; |
d9839e03 | 29 | } |
8ba0bbb2 | 30 | |
ff5ebe39 DB |
31 | if (!obj->parsed) { |
32 | printf("missing %s %s\n", obj->type, | |
33 | sha1_to_hex(obj->sha1)); | |
34 | } | |
35 | if (!obj->used) { | |
36 | printf("dangling %s %s\n", obj->type, | |
37 | sha1_to_hex(obj->sha1)); | |
d9839e03 | 38 | } |
c418eda4 DB |
39 | for (refs = obj->refs; refs; refs = refs->next) { |
40 | if (!refs->item->parsed) { | |
41 | printf("broken link from %s\n", | |
42 | sha1_to_hex(obj->sha1)); | |
43 | printf(" to %s\n", | |
44 | sha1_to_hex(refs->item->sha1)); | |
45 | } | |
46 | } | |
8ba0bbb2 LT |
47 | } |
48 | } | |
49 | ||
c418eda4 | 50 | static int fsck_tree(struct tree *item) |
20222118 | 51 | { |
ff5ebe39 DB |
52 | if (item->has_full_path) { |
53 | fprintf(stderr, "warning: fsck-cache: tree %s " | |
c418eda4 DB |
54 | "has full pathnames in it\n", |
55 | sha1_to_hex(item->object.sha1)); | |
1ea34e36 | 56 | } |
59c1e249 | 57 | return 0; |
1ea34e36 LT |
58 | } |
59 | ||
c418eda4 | 60 | static int fsck_commit(struct commit *commit) |
1ea34e36 | 61 | { |
ff5ebe39 | 62 | if (!commit->tree) |
1ea34e36 | 63 | return -1; |
ab7df187 | 64 | if (!commit->parents && show_root) |
c418eda4 | 65 | printf("root %s\n", sha1_to_hex(commit->object.sha1)); |
e6948b6d | 66 | if (!commit->date) |
c418eda4 DB |
67 | printf("bad commit date in %s\n", |
68 | sha1_to_hex(commit->object.sha1)); | |
4728b861 LT |
69 | return 0; |
70 | } | |
71 | ||
c418eda4 | 72 | static int fsck_tag(struct tag *tag) |
ec4465ad | 73 | { |
889262ea LT |
74 | if (!show_tags) |
75 | return 0; | |
76 | ||
c418eda4 DB |
77 | printf("tagged %s %s", |
78 | tag->tagged->type, | |
79 | sha1_to_hex(tag->tagged->sha1)); | |
80 | printf(" (%s) in %s\n", | |
81 | tag->tag, sha1_to_hex(tag->object.sha1)); | |
ff5ebe39 | 82 | return 0; |
20222118 LT |
83 | } |
84 | ||
85 | static int fsck_name(char *hex) | |
86 | { | |
87 | unsigned char sha1[20]; | |
88 | if (!get_sha1_hex(hex, sha1)) { | |
c418eda4 DB |
89 | struct object *obj = parse_object(sha1); |
90 | if (!obj) | |
91 | return -1; | |
92 | if (obj->type == blob_type) | |
93 | return 0; | |
94 | if (obj->type == tree_type) | |
95 | return fsck_tree((struct tree *) obj); | |
96 | if (obj->type == commit_type) | |
97 | return fsck_commit((struct commit *) obj); | |
98 | if (obj->type == tag_type) | |
99 | return fsck_tag((struct tag *) obj); | |
20222118 LT |
100 | } |
101 | return -1; | |
102 | } | |
103 | ||
104 | static int fsck_dir(int i, char *path) | |
105 | { | |
106 | DIR *dir = opendir(path); | |
107 | struct dirent *de; | |
108 | ||
109 | if (!dir) { | |
2de381f9 | 110 | return error("missing sha1 directory '%s'", path); |
20222118 LT |
111 | } |
112 | ||
113 | while ((de = readdir(dir)) != NULL) { | |
114 | char name[100]; | |
115 | int len = strlen(de->d_name); | |
116 | ||
117 | switch (len) { | |
118 | case 2: | |
119 | if (de->d_name[1] != '.') | |
120 | break; | |
121 | case 1: | |
122 | if (de->d_name[0] != '.') | |
123 | break; | |
124 | continue; | |
125 | case 38: | |
126 | sprintf(name, "%02x", i); | |
127 | memcpy(name+2, de->d_name, len+1); | |
128 | if (!fsck_name(name)) | |
129 | continue; | |
130 | } | |
131 | fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name); | |
132 | } | |
133 | closedir(dir); | |
134 | return 0; | |
135 | } | |
136 | ||
137 | int main(int argc, char **argv) | |
138 | { | |
bcee6fd8 | 139 | int i, heads; |
20222118 LT |
140 | char *sha1_dir; |
141 | ||
889262ea LT |
142 | for (i = 1; i < argc; i++) { |
143 | const char *arg = argv[i]; | |
144 | ||
145 | if (!strcmp(arg, "--unreachable")) { | |
146 | show_unreachable = 1; | |
147 | continue; | |
148 | } | |
149 | if (!strcmp(arg, "--tags")) { | |
150 | show_tags = 1; | |
151 | continue; | |
152 | } | |
ab7df187 LT |
153 | if (!strcmp(arg, "--root")) { |
154 | show_root = 1; | |
155 | continue; | |
156 | } | |
889262ea LT |
157 | if (*arg == '-') |
158 | usage("fsck-cache [--tags] [[--unreachable] <head-sha1>*]"); | |
159 | } | |
160 | ||
bcee6fd8 LT |
161 | sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT; |
162 | for (i = 0; i < 256; i++) { | |
163 | static char dir[4096]; | |
164 | sprintf(dir, "%s/%02x", sha1_dir, i); | |
165 | fsck_dir(i, dir); | |
166 | } | |
167 | ||
168 | heads = 0; | |
d9839e03 | 169 | for (i = 1; i < argc; i++) { |
889262ea LT |
170 | const char *arg = argv[i]; |
171 | ||
172 | if (*arg == '-') | |
d9839e03 | 173 | continue; |
889262ea LT |
174 | |
175 | if (!get_sha1_hex(arg, head_sha1)) { | |
e1a1388d JF |
176 | struct commit *commit = lookup_commit(head_sha1); |
177 | struct object *obj; | |
178 | ||
179 | /* Error is printed by lookup_commit(). */ | |
180 | if (!commit) | |
181 | continue; | |
182 | ||
183 | obj = &commit->object; | |
ff5ebe39 DB |
184 | obj->used = 1; |
185 | mark_reachable(obj, REACHABLE); | |
bcee6fd8 | 186 | heads++; |
d9839e03 LT |
187 | continue; |
188 | } | |
889262ea | 189 | error("expected sha1, got %s", arg); |
d9839e03 | 190 | } |
d9839e03 | 191 | |
bcee6fd8 LT |
192 | if (!heads) { |
193 | if (show_unreachable) { | |
194 | fprintf(stderr, "unable to do reachability without a head\n"); | |
195 | show_unreachable = 0; | |
196 | } | |
197 | fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n"); | |
20222118 | 198 | } |
bcee6fd8 | 199 | |
8ba0bbb2 | 200 | check_connectivity(); |
20222118 LT |
201 | return 0; |
202 | } |