[PATCH] Expose packed_git and alt_odb.
[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;
ae7c0c92 15static int keep_cache_objects = 0;
d9839e03
LT
16static unsigned char head_sha1[20];
17
8ba0bbb2
LT
18static void check_connectivity(void)
19{
20 int i;
21
8ba0bbb2 22 /* Look up all the requirements, warn about missing objects.. */
ff5ebe39
DB
23 for (i = 0; i < nr_objs; i++) {
24 struct object *obj = objs[i];
c418eda4 25 struct object_list *refs;
8ba0bbb2 26
3a6a23e6 27 if (!obj->parsed) {
c4584ae3
JH
28 printf("missing %s %s\n",
29 obj->type, sha1_to_hex(obj->sha1));
3a6a23e6
LT
30 continue;
31 }
32
33 for (refs = obj->refs; refs; refs = refs->next) {
34 if (refs->item->parsed)
35 continue;
36 printf("broken link from %7s %s\n",
37 obj->type, sha1_to_hex(obj->sha1));
38 printf(" to %7s %s\n",
aa034134 39 refs->item->type, sha1_to_hex(refs->item->sha1));
3a6a23e6
LT
40 }
41
ff5ebe39 42 if (show_unreachable && !(obj->flags & REACHABLE)) {
c4584ae3
JH
43 printf("unreachable %s %s\n",
44 obj->type, sha1_to_hex(obj->sha1));
8ba0bbb2 45 continue;
d9839e03 46 }
8ba0bbb2 47
ff5ebe39
DB
48 if (!obj->used) {
49 printf("dangling %s %s\n", obj->type,
50 sha1_to_hex(obj->sha1));
d9839e03 51 }
8ba0bbb2
LT
52 }
53}
54
85003492
LT
55/*
56 * The entries in a tree are ordered in the _path_ order,
57 * which means that a directory entry is ordered by adding
58 * a slash to the end of it.
59 *
60 * So a directory called "a" is ordered _after_ a file
61 * called "a.c", because "a/" sorts after "a.c".
62 */
a4f35a2d
JH
63#define TREE_UNORDERED (-1)
64#define TREE_HAS_DUPS (-2)
65
85003492
LT
66static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
67{
68 int len1 = strlen(a->name);
69 int len2 = strlen(b->name);
70 int len = len1 < len2 ? len1 : len2;
71 unsigned char c1, c2;
72 int cmp;
73
74 cmp = memcmp(a->name, b->name, len);
75 if (cmp < 0)
76 return 0;
77 if (cmp > 0)
a4f35a2d 78 return TREE_UNORDERED;
85003492
LT
79
80 /*
81 * Ok, the first <len> characters are the same.
82 * Now we need to order the next one, but turn
83 * a '\0' into a '/' for a directory entry.
84 */
85 c1 = a->name[len];
86 c2 = b->name[len];
a4f35a2d
JH
87 if (!c1 && !c2)
88 /*
89 * git-write-tree used to write out a nonsense tree that has
90 * entries with the same name, one blob and one tree. Make
91 * sure we do not have duplicate entries.
92 */
93 return TREE_HAS_DUPS;
85003492
LT
94 if (!c1 && a->directory)
95 c1 = '/';
96 if (!c2 && b->directory)
97 c2 = '/';
a4f35a2d 98 return c1 < c2 ? 0 : TREE_UNORDERED;
85003492
LT
99}
100
c418eda4 101static int fsck_tree(struct tree *item)
20222118 102{
85003492
LT
103 int has_full_path = 0;
104 struct tree_entry_list *entry, *last;
105
106 last = NULL;
107 for (entry = item->entries; entry; entry = entry->next) {
108 if (strchr(entry->name, '/'))
109 has_full_path = 1;
110
42ea9cb2
LT
111 switch (entry->mode) {
112 /*
113 * Standard modes..
114 */
115 case S_IFREG | 0755:
116 case S_IFREG | 0644:
117 case S_IFLNK:
118 case S_IFDIR:
119 break;
120 /*
121 * This is nonstandard, but we had a few of these
122 * early on when we honored the full set of mode
123 * bits..
124 */
125 case S_IFREG | 0664:
126 break;
127 default:
128 printf("tree %s has entry %o %s\n",
129 sha1_to_hex(item->object.sha1),
130 entry->mode, entry->name);
131 }
132
85003492 133 if (last) {
a4f35a2d
JH
134 switch (verify_ordered(last, entry)) {
135 case TREE_UNORDERED:
85003492
LT
136 fprintf(stderr, "tree %s not ordered\n",
137 sha1_to_hex(item->object.sha1));
138 return -1;
a4f35a2d
JH
139 case TREE_HAS_DUPS:
140 fprintf(stderr, "tree %s has duplicate entries for '%s'\n",
141 sha1_to_hex(item->object.sha1),
142 entry->name);
143 return -1;
144 default:
145 break;
85003492
LT
146 }
147 }
148
149 last = entry;
150 }
151
152 if (has_full_path) {
667bb59b 153 fprintf(stderr, "warning: git-fsck-cache: tree %s "
c418eda4
DB
154 "has full pathnames in it\n",
155 sha1_to_hex(item->object.sha1));
1ea34e36 156 }
85003492 157
59c1e249 158 return 0;
1ea34e36
LT
159}
160
c418eda4 161static int fsck_commit(struct commit *commit)
1ea34e36 162{
bd1e17e2
LT
163 free(commit->buffer);
164 commit->buffer = NULL;
ff5ebe39 165 if (!commit->tree)
1ea34e36 166 return -1;
ab7df187 167 if (!commit->parents && show_root)
c418eda4 168 printf("root %s\n", sha1_to_hex(commit->object.sha1));
e6948b6d 169 if (!commit->date)
c418eda4
DB
170 printf("bad commit date in %s\n",
171 sha1_to_hex(commit->object.sha1));
4728b861
LT
172 return 0;
173}
174
c418eda4 175static int fsck_tag(struct tag *tag)
ec4465ad 176{
92d4c85d
LT
177 struct object *tagged = tag->tagged;
178
179 if (!tagged) {
180 printf("bad object in tag %s\n", sha1_to_hex(tag->object.sha1));
181 return -1;
182 }
889262ea
LT
183 if (!show_tags)
184 return 0;
185
92d4c85d
LT
186 printf("tagged %s %s", tagged->type, sha1_to_hex(tagged->sha1));
187 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
ff5ebe39 188 return 0;
20222118
LT
189}
190
7e8c174a 191static int fsck_sha1(unsigned char *sha1)
20222118 192{
7e8c174a
LT
193 struct object *obj = parse_object(sha1);
194 if (!obj)
195 return -1;
196 if (obj->type == blob_type)
197 return 0;
198 if (obj->type == tree_type)
199 return fsck_tree((struct tree *) obj);
200 if (obj->type == commit_type)
201 return fsck_commit((struct commit *) obj);
202 if (obj->type == tag_type)
203 return fsck_tag((struct tag *) obj);
204 return -1;
205}
206
207/*
208 * This is the sorting chunk size: make it reasonably
209 * big so that we can sort well..
210 */
211#define MAX_SHA1_ENTRIES (1024)
212
213struct sha1_entry {
214 unsigned long ino;
20222118 215 unsigned char sha1[20];
7e8c174a
LT
216};
217
218static struct {
219 unsigned long nr;
220 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
221} sha1_list;
222
223static int ino_compare(const void *_a, const void *_b)
224{
225 const struct sha1_entry *a = _a, *b = _b;
226 unsigned long ino1 = a->ino, ino2 = b->ino;
227 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
228}
229
230static void fsck_sha1_list(void)
231{
232 int i, nr = sha1_list.nr;
233
234 qsort(sha1_list.entry, nr, sizeof(struct sha1_entry *), ino_compare);
235 for (i = 0; i < nr; i++) {
236 struct sha1_entry *entry = sha1_list.entry[i];
237 unsigned char *sha1 = entry->sha1;
238
239 sha1_list.entry[i] = NULL;
240 if (fsck_sha1(sha1) < 0)
241 fprintf(stderr, "bad sha1 entry '%s'\n", sha1_to_hex(sha1));
242 free(entry);
20222118 243 }
7e8c174a
LT
244 sha1_list.nr = 0;
245}
246
247static void add_sha1_list(unsigned char *sha1, unsigned long ino)
248{
249 struct sha1_entry *entry = xmalloc(sizeof(*entry));
250 int nr;
251
252 entry->ino = ino;
253 memcpy(entry->sha1, sha1, 20);
254 nr = sha1_list.nr;
255 if (nr == MAX_SHA1_ENTRIES) {
256 fsck_sha1_list();
257 nr = 0;
258 }
259 sha1_list.entry[nr] = entry;
260 sha1_list.nr = ++nr;
20222118
LT
261}
262
263static int fsck_dir(int i, char *path)
264{
265 DIR *dir = opendir(path);
266 struct dirent *de;
267
268 if (!dir) {
2de381f9 269 return error("missing sha1 directory '%s'", path);
20222118
LT
270 }
271
272 while ((de = readdir(dir)) != NULL) {
273 char name[100];
7e8c174a 274 unsigned char sha1[20];
20222118
LT
275 int len = strlen(de->d_name);
276
277 switch (len) {
278 case 2:
279 if (de->d_name[1] != '.')
280 break;
281 case 1:
282 if (de->d_name[0] != '.')
283 break;
284 continue;
285 case 38:
286 sprintf(name, "%02x", i);
287 memcpy(name+2, de->d_name, len+1);
7e8c174a
LT
288 if (get_sha1_hex(name, sha1) < 0)
289 break;
290 add_sha1_list(sha1, de->d_ino);
291 continue;
20222118
LT
292 }
293 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
294 }
295 closedir(dir);
296 return 0;
297}
298
7c4d07c7 299static int read_sha1_reference(const char *path)
1024932f
LT
300{
301 char hexname[60];
302 unsigned char sha1[20];
303 int fd = open(path, O_RDONLY), len;
304 struct object *obj;
305
306 if (fd < 0)
7c4d07c7 307 return -1;
1024932f
LT
308
309 len = read(fd, hexname, sizeof(hexname));
310 close(fd);
311 if (len < 40)
7c4d07c7 312 return -1;
1024932f
LT
313
314 if (get_sha1_hex(hexname, sha1) < 0)
7c4d07c7 315 return -1;
1024932f
LT
316
317 obj = lookup_object(sha1);
7c4d07c7
LT
318 if (!obj)
319 return error("%s: invalid sha1 pointer %.40s", path, hexname);
320
1024932f
LT
321 obj->used = 1;
322 mark_reachable(obj, REACHABLE);
7c4d07c7 323 return 0;
1024932f
LT
324}
325
477606f5 326static int find_file_objects(const char *base, const char *name)
1024932f
LT
327{
328 int baselen = strlen(base);
329 int namelen = strlen(name);
330 char *path = xmalloc(baselen + namelen + 2);
331 struct stat st;
332
333 memcpy(path, base, baselen);
334 path[baselen] = '/';
335 memcpy(path + baselen + 1, name, namelen+1);
336 if (stat(path, &st) < 0)
477606f5 337 return 0;
1024932f
LT
338
339 /*
340 * Recurse into directories
341 */
342 if (S_ISDIR(st.st_mode)) {
477606f5 343 int count = 0;
1024932f
LT
344 DIR *dir = opendir(path);
345 if (dir) {
346 struct dirent *de;
347 while ((de = readdir(dir)) != NULL) {
348 if (de->d_name[0] == '.')
349 continue;
477606f5 350 count += find_file_objects(path, de->d_name);
1024932f
LT
351 }
352 closedir(dir);
353 }
477606f5 354 return count;
1024932f 355 }
477606f5
LT
356 if (S_ISREG(st.st_mode))
357 return read_sha1_reference(path) == 0;
358 return 0;
1024932f
LT
359}
360
361static void get_default_heads(void)
362{
363 char *git_dir = gitenv(GIT_DIR_ENVIRONMENT) ? : DEFAULT_GIT_DIR_ENVIRONMENT;
477606f5
LT
364 int count = find_file_objects(git_dir, "refs");
365 if (!count)
366 die("No default references");
1024932f
LT
367}
368
20222118
LT
369int main(int argc, char **argv)
370{
bcee6fd8 371 int i, heads;
20222118
LT
372 char *sha1_dir;
373
889262ea
LT
374 for (i = 1; i < argc; i++) {
375 const char *arg = argv[i];
376
377 if (!strcmp(arg, "--unreachable")) {
378 show_unreachable = 1;
379 continue;
380 }
381 if (!strcmp(arg, "--tags")) {
382 show_tags = 1;
383 continue;
384 }
ab7df187
LT
385 if (!strcmp(arg, "--root")) {
386 show_root = 1;
387 continue;
388 }
ae7c0c92
JH
389 if (!strcmp(arg, "--cache")) {
390 keep_cache_objects = 1;
391 continue;
392 }
889262ea 393 if (*arg == '-')
667bb59b 394 usage("git-fsck-cache [--tags] [[--unreachable] [--cache] <head-sha1>*]");
889262ea
LT
395 }
396
ace1534d 397 sha1_dir = get_object_directory();
bcee6fd8
LT
398 for (i = 0; i < 256; i++) {
399 static char dir[4096];
400 sprintf(dir, "%s/%02x", sha1_dir, i);
401 fsck_dir(i, dir);
402 }
7e8c174a 403 fsck_sha1_list();
bcee6fd8
LT
404
405 heads = 0;
d9839e03 406 for (i = 1; i < argc; i++) {
889262ea
LT
407 const char *arg = argv[i];
408
409 if (*arg == '-')
d9839e03 410 continue;
889262ea 411
3c249c95 412 if (!get_sha1(arg, head_sha1)) {
770896e5 413 struct object *obj = lookup_object(head_sha1);
e1a1388d 414
770896e5
LT
415 /* Error is printed by lookup_object(). */
416 if (!obj)
e1a1388d
JF
417 continue;
418
ff5ebe39
DB
419 obj->used = 1;
420 mark_reachable(obj, REACHABLE);
bcee6fd8 421 heads++;
d9839e03
LT
422 continue;
423 }
889262ea 424 error("expected sha1, got %s", arg);
d9839e03 425 }
d9839e03 426
1024932f 427 /*
d1af002d 428 * If we've not been given any explicit head information, do the
e7bd907d
LT
429 * default ones from .git/refs. We also consider the index file
430 * in this case (ie this implies --cache).
1024932f 431 */
e7bd907d 432 if (!heads) {
1024932f
LT
433 get_default_heads();
434 keep_cache_objects = 1;
435 }
436
ae7c0c92
JH
437 if (keep_cache_objects) {
438 int i;
439 read_cache();
440 for (i = 0; i < active_nr; i++) {
441 struct blob *blob = lookup_blob(active_cache[i]->sha1);
442 struct object *obj;
443 if (!blob)
444 continue;
445 obj = &blob->object;
446 obj->used = 1;
447 mark_reachable(obj, REACHABLE);
448 }
449 }
450
8ba0bbb2 451 check_connectivity();
20222118
LT
452 return 0;
453}