[PATCH] Fix warning in convert-cache
[git/git.git] / fsck-cache.c
CommitLineData
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 12static int show_root = 0;
889262ea 13static int show_tags = 0;
d9839e03 14static int show_unreachable = 0;
d9839e03
LT
15static unsigned char head_sha1[20];
16
8ba0bbb2
LT
17static 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
3a6a23e6
LT
26 if (!obj->parsed) {
27 printf("missing %s %s\n", obj->type, sha1_to_hex(obj->sha1));
28 continue;
29 }
30
31 for (refs = obj->refs; refs; refs = refs->next) {
32 if (refs->item->parsed)
33 continue;
34 printf("broken link from %7s %s\n",
35 obj->type, sha1_to_hex(obj->sha1));
36 printf(" to %7s %s\n",
37 obj->type, sha1_to_hex(refs->item->sha1));
38 }
39
40 /* Don't bother with tag reachability. */
41 if (obj->type == tag_type)
42 continue;
43
ff5ebe39 44 if (show_unreachable && !(obj->flags & REACHABLE)) {
f43b8abc 45 printf("unreachable %s %s\n", obj->type, sha1_to_hex(obj->sha1));
8ba0bbb2 46 continue;
d9839e03 47 }
8ba0bbb2 48
ff5ebe39
DB
49 if (!obj->used) {
50 printf("dangling %s %s\n", obj->type,
51 sha1_to_hex(obj->sha1));
d9839e03 52 }
8ba0bbb2
LT
53 }
54}
55
c418eda4 56static int fsck_tree(struct tree *item)
20222118 57{
ff5ebe39
DB
58 if (item->has_full_path) {
59 fprintf(stderr, "warning: fsck-cache: tree %s "
c418eda4
DB
60 "has full pathnames in it\n",
61 sha1_to_hex(item->object.sha1));
1ea34e36 62 }
59c1e249 63 return 0;
1ea34e36
LT
64}
65
c418eda4 66static int fsck_commit(struct commit *commit)
1ea34e36 67{
ff5ebe39 68 if (!commit->tree)
1ea34e36 69 return -1;
ab7df187 70 if (!commit->parents && show_root)
c418eda4 71 printf("root %s\n", sha1_to_hex(commit->object.sha1));
e6948b6d 72 if (!commit->date)
c418eda4
DB
73 printf("bad commit date in %s\n",
74 sha1_to_hex(commit->object.sha1));
4728b861
LT
75 return 0;
76}
77
c418eda4 78static int fsck_tag(struct tag *tag)
ec4465ad 79{
889262ea
LT
80 if (!show_tags)
81 return 0;
82
c418eda4
DB
83 printf("tagged %s %s",
84 tag->tagged->type,
85 sha1_to_hex(tag->tagged->sha1));
86 printf(" (%s) in %s\n",
87 tag->tag, sha1_to_hex(tag->object.sha1));
ff5ebe39 88 return 0;
20222118
LT
89}
90
7e8c174a 91static int fsck_sha1(unsigned char *sha1)
20222118 92{
7e8c174a
LT
93 struct object *obj = parse_object(sha1);
94 if (!obj)
95 return -1;
96 if (obj->type == blob_type)
97 return 0;
98 if (obj->type == tree_type)
99 return fsck_tree((struct tree *) obj);
100 if (obj->type == commit_type)
101 return fsck_commit((struct commit *) obj);
102 if (obj->type == tag_type)
103 return fsck_tag((struct tag *) obj);
104 return -1;
105}
106
107/*
108 * This is the sorting chunk size: make it reasonably
109 * big so that we can sort well..
110 */
111#define MAX_SHA1_ENTRIES (1024)
112
113struct sha1_entry {
114 unsigned long ino;
20222118 115 unsigned char sha1[20];
7e8c174a
LT
116};
117
118static struct {
119 unsigned long nr;
120 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
121} sha1_list;
122
123static int ino_compare(const void *_a, const void *_b)
124{
125 const struct sha1_entry *a = _a, *b = _b;
126 unsigned long ino1 = a->ino, ino2 = b->ino;
127 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
128}
129
130static void fsck_sha1_list(void)
131{
132 int i, nr = sha1_list.nr;
133
134 qsort(sha1_list.entry, nr, sizeof(struct sha1_entry *), ino_compare);
135 for (i = 0; i < nr; i++) {
136 struct sha1_entry *entry = sha1_list.entry[i];
137 unsigned char *sha1 = entry->sha1;
138
139 sha1_list.entry[i] = NULL;
140 if (fsck_sha1(sha1) < 0)
141 fprintf(stderr, "bad sha1 entry '%s'\n", sha1_to_hex(sha1));
142 free(entry);
20222118 143 }
7e8c174a
LT
144 sha1_list.nr = 0;
145}
146
147static void add_sha1_list(unsigned char *sha1, unsigned long ino)
148{
149 struct sha1_entry *entry = xmalloc(sizeof(*entry));
150 int nr;
151
152 entry->ino = ino;
153 memcpy(entry->sha1, sha1, 20);
154 nr = sha1_list.nr;
155 if (nr == MAX_SHA1_ENTRIES) {
156 fsck_sha1_list();
157 nr = 0;
158 }
159 sha1_list.entry[nr] = entry;
160 sha1_list.nr = ++nr;
20222118
LT
161}
162
163static int fsck_dir(int i, char *path)
164{
165 DIR *dir = opendir(path);
166 struct dirent *de;
167
168 if (!dir) {
2de381f9 169 return error("missing sha1 directory '%s'", path);
20222118
LT
170 }
171
172 while ((de = readdir(dir)) != NULL) {
173 char name[100];
7e8c174a 174 unsigned char sha1[20];
20222118
LT
175 int len = strlen(de->d_name);
176
177 switch (len) {
178 case 2:
179 if (de->d_name[1] != '.')
180 break;
181 case 1:
182 if (de->d_name[0] != '.')
183 break;
184 continue;
185 case 38:
186 sprintf(name, "%02x", i);
187 memcpy(name+2, de->d_name, len+1);
7e8c174a
LT
188 if (get_sha1_hex(name, sha1) < 0)
189 break;
190 add_sha1_list(sha1, de->d_ino);
191 continue;
20222118
LT
192 }
193 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
194 }
195 closedir(dir);
196 return 0;
197}
198
199int main(int argc, char **argv)
200{
bcee6fd8 201 int i, heads;
20222118
LT
202 char *sha1_dir;
203
889262ea
LT
204 for (i = 1; i < argc; i++) {
205 const char *arg = argv[i];
206
207 if (!strcmp(arg, "--unreachable")) {
208 show_unreachable = 1;
209 continue;
210 }
211 if (!strcmp(arg, "--tags")) {
212 show_tags = 1;
213 continue;
214 }
ab7df187
LT
215 if (!strcmp(arg, "--root")) {
216 show_root = 1;
217 continue;
218 }
889262ea
LT
219 if (*arg == '-')
220 usage("fsck-cache [--tags] [[--unreachable] <head-sha1>*]");
221 }
222
bcee6fd8
LT
223 sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
224 for (i = 0; i < 256; i++) {
225 static char dir[4096];
226 sprintf(dir, "%s/%02x", sha1_dir, i);
227 fsck_dir(i, dir);
228 }
7e8c174a 229 fsck_sha1_list();
bcee6fd8
LT
230
231 heads = 0;
d9839e03 232 for (i = 1; i < argc; i++) {
889262ea
LT
233 const char *arg = argv[i];
234
235 if (*arg == '-')
d9839e03 236 continue;
889262ea 237
3c249c95 238 if (!get_sha1(arg, head_sha1)) {
e1a1388d
JF
239 struct commit *commit = lookup_commit(head_sha1);
240 struct object *obj;
241
242 /* Error is printed by lookup_commit(). */
243 if (!commit)
244 continue;
245
246 obj = &commit->object;
ff5ebe39
DB
247 obj->used = 1;
248 mark_reachable(obj, REACHABLE);
bcee6fd8 249 heads++;
d9839e03
LT
250 continue;
251 }
889262ea 252 error("expected sha1, got %s", arg);
d9839e03 253 }
d9839e03 254
bcee6fd8
LT
255 if (!heads) {
256 if (show_unreachable) {
257 fprintf(stderr, "unable to do reachability without a head\n");
258 show_unreachable = 0;
259 }
260 fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
20222118 261 }
bcee6fd8 262
8ba0bbb2 263 check_connectivity();
20222118
LT
264 return 0;
265}