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