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