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