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