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