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