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