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