fsck-cache: only show tags if asked to do so with "--tags"
[git/git.git] / fsck-cache.c
CommitLineData
20222118
LT
1#include "cache.h"
2
3#include <sys/types.h>
4#include <dirent.h>
5
ff5ebe39
DB
6#include "commit.h"
7#include "tree.h"
8#include "blob.h"
9
10#define REACHABLE 0x0001
d9839e03 11
889262ea 12static int show_tags = 0;
d9839e03 13static int show_unreachable = 0;
d9839e03
LT
14static unsigned char head_sha1[20];
15
8ba0bbb2
LT
16static void check_connectivity(void)
17{
18 int i;
19
8ba0bbb2 20 /* Look up all the requirements, warn about missing objects.. */
ff5ebe39
DB
21 for (i = 0; i < nr_objs; i++) {
22 struct object *obj = objs[i];
8ba0bbb2 23
ff5ebe39 24 if (show_unreachable && !(obj->flags & REACHABLE)) {
f43b8abc 25 printf("unreachable %s %s\n", obj->type, sha1_to_hex(obj->sha1));
8ba0bbb2 26 continue;
d9839e03 27 }
8ba0bbb2 28
ff5ebe39
DB
29 if (!obj->parsed) {
30 printf("missing %s %s\n", obj->type,
31 sha1_to_hex(obj->sha1));
32 }
33 if (!obj->used) {
34 printf("dangling %s %s\n", obj->type,
35 sha1_to_hex(obj->sha1));
d9839e03 36 }
8ba0bbb2
LT
37 }
38}
39
1ea34e36 40static int fsck_tree(unsigned char *sha1, void *data, unsigned long size)
20222118 41{
ff5ebe39
DB
42 struct tree *item = lookup_tree(sha1);
43 if (parse_tree(item))
44 return -1;
45 if (item->has_full_path) {
46 fprintf(stderr, "warning: fsck-cache: tree %s "
47 "has full pathnames in it\n", sha1_to_hex(sha1));
1ea34e36 48 }
59c1e249 49 return 0;
1ea34e36
LT
50}
51
52static int fsck_commit(unsigned char *sha1, void *data, unsigned long size)
53{
ff5ebe39
DB
54 struct commit *commit = lookup_commit(sha1);
55 if (parse_commit(commit))
1ea34e36 56 return -1;
ff5ebe39 57 if (!commit->tree)
1ea34e36 58 return -1;
ff5ebe39 59 if (!commit->parents)
d9839e03 60 printf("root %s\n", sha1_to_hex(sha1));
e6948b6d
LT
61 if (!commit->date)
62 printf("bad commit date in %s\n", sha1_to_hex(sha1));
59c1e249 63 return 0;
20222118
LT
64}
65
4728b861
LT
66static int fsck_blob(unsigned char *sha1, void *data, unsigned long size)
67{
68 struct blob *blob = lookup_blob(sha1);
69 blob->object.parsed = 1;
70 return 0;
71}
72
ec4465ad
LT
73static int fsck_tag(unsigned char *sha1, void *data, unsigned long size)
74{
75 int typelen, taglen;
76 unsigned char object[20];
56ce69f7 77 char object_hex[60];
ec4465ad
LT
78 const char *type_line, *tag_line, *sig_line;
79
80 if (size < 64)
81 return -1;
82 if (memcmp("object ", data, 7) || get_sha1_hex(data + 7, object))
83 return -1;
84
85 type_line = data + 48;
86 if (memcmp("\ntype ", type_line-1, 6))
87 return -1;
88
89 tag_line = strchr(type_line, '\n');
90 if (!tag_line || memcmp("tag ", ++tag_line, 4))
91 return -1;
92
93 sig_line = strchr(tag_line, '\n');
94 if (!sig_line)
95 return -1;
96 sig_line++;
97
98 typelen = tag_line - type_line - strlen("type \n");
99 if (typelen >= 20)
100 return -1;
101 taglen = sig_line - tag_line - strlen("tag \n");
102
889262ea
LT
103 if (!show_tags)
104 return 0;
105
56ce69f7
LT
106 strcpy(object_hex, sha1_to_hex(object));
107 printf("tagged %.*s %s (%.*s) in %s\n",
ec4465ad 108 typelen, type_line + 5,
56ce69f7
LT
109 object_hex,
110 taglen, tag_line + 4,
111 sha1_to_hex(sha1));
ec4465ad
LT
112 return 0;
113}
114
ff5ebe39
DB
115static int fsck_entry(unsigned char *sha1, char *tag, void *data,
116 unsigned long size)
20222118 117{
1ea34e36 118 if (!strcmp(tag, "blob")) {
4728b861
LT
119 if (fsck_blob(sha1, data, size) < 0)
120 return -1;
1ea34e36
LT
121 } else if (!strcmp(tag, "tree")) {
122 if (fsck_tree(sha1, data, size) < 0)
123 return -1;
124 } else if (!strcmp(tag, "commit")) {
125 if (fsck_commit(sha1, data, size) < 0)
126 return -1;
ec4465ad
LT
127 } else if (!strcmp(tag, "tag")) {
128 if (fsck_tag(sha1, data, size) < 0)
129 return -1;
1ea34e36 130 } else
20222118 131 return -1;
ff5ebe39 132 return 0;
20222118
LT
133}
134
135static int fsck_name(char *hex)
136{
137 unsigned char sha1[20];
138 if (!get_sha1_hex(hex, sha1)) {
139 unsigned long mapsize;
140 void *map = map_sha1_file(sha1, &mapsize);
141 if (map) {
142 char type[100];
143 unsigned long size;
d98b46f8
LT
144 void *buffer = unpack_sha1_file(map, mapsize, type, &size);
145 if (!buffer)
146 return -1;
147 if (check_sha1_signature(sha1, buffer, size, type) < 0)
148 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
20222118 149 munmap(map, mapsize);
d98b46f8 150 if (!fsck_entry(sha1, type, buffer, size))
20222118
LT
151 return 0;
152 }
153 }
154 return -1;
155}
156
157static int fsck_dir(int i, char *path)
158{
159 DIR *dir = opendir(path);
160 struct dirent *de;
161
162 if (!dir) {
2de381f9 163 return error("missing sha1 directory '%s'", path);
20222118
LT
164 }
165
166 while ((de = readdir(dir)) != NULL) {
167 char name[100];
168 int len = strlen(de->d_name);
169
170 switch (len) {
171 case 2:
172 if (de->d_name[1] != '.')
173 break;
174 case 1:
175 if (de->d_name[0] != '.')
176 break;
177 continue;
178 case 38:
179 sprintf(name, "%02x", i);
180 memcpy(name+2, de->d_name, len+1);
181 if (!fsck_name(name))
182 continue;
183 }
184 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
185 }
186 closedir(dir);
187 return 0;
188}
189
190int main(int argc, char **argv)
191{
bcee6fd8 192 int i, heads;
20222118
LT
193 char *sha1_dir;
194
889262ea
LT
195 for (i = 1; i < argc; i++) {
196 const char *arg = argv[i];
197
198 if (!strcmp(arg, "--unreachable")) {
199 show_unreachable = 1;
200 continue;
201 }
202 if (!strcmp(arg, "--tags")) {
203 show_tags = 1;
204 continue;
205 }
206 if (*arg == '-')
207 usage("fsck-cache [--tags] [[--unreachable] <head-sha1>*]");
208 }
209
bcee6fd8
LT
210 sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
211 for (i = 0; i < 256; i++) {
212 static char dir[4096];
213 sprintf(dir, "%s/%02x", sha1_dir, i);
214 fsck_dir(i, dir);
215 }
216
217 heads = 0;
d9839e03 218 for (i = 1; i < argc; i++) {
889262ea
LT
219 const char *arg = argv[i];
220
221 if (*arg == '-')
d9839e03 222 continue;
889262ea
LT
223
224 if (!get_sha1_hex(arg, head_sha1)) {
b51ad431 225 struct object *obj = &lookup_commit(head_sha1)->object;
ff5ebe39
DB
226 obj->used = 1;
227 mark_reachable(obj, REACHABLE);
bcee6fd8 228 heads++;
d9839e03
LT
229 continue;
230 }
889262ea 231 error("expected sha1, got %s", arg);
d9839e03 232 }
d9839e03 233
bcee6fd8
LT
234 if (!heads) {
235 if (show_unreachable) {
236 fprintf(stderr, "unable to do reachability without a head\n");
237 show_unreachable = 0;
238 }
239 fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
20222118 240 }
bcee6fd8 241
8ba0bbb2 242 check_connectivity();
20222118
LT
243 return 0;
244}