]> git.ipfire.org Git - thirdparty/git.git/blame - fsck.c
Merge branch 'js/remove-cruft-files'
[thirdparty/git.git] / fsck.c
CommitLineData
d812c3b6 1#include "git-compat-util.h"
d4a4f929 2#include "date.h"
a034e910 3#include "dir.h"
41771fa4 4#include "hex.h"
a034e910 5#include "object-store-ll.h"
c339932b 6#include "path.h"
109cd76d 7#include "repository.h"
355885d5 8#include "object.h"
27ab4784 9#include "attr.h"
355885d5
MK
10#include "blob.h"
11#include "tree.h"
12#include "tree-walk.h"
13#include "commit.h"
14#include "tag.h"
15#include "fsck.h"
cec097be 16#include "refs.h"
a2b26ffb 17#include "url.h"
a18fcc9f 18#include "utf8.h"
159e7b08 19#include "oidset.h"
27387444 20#include "packfile.h"
ed8b10f6
JK
21#include "submodule-config.h"
22#include "config.h"
3ac68a93 23#include "help.h"
159e7b08 24
0fbcaef6
JK
25static ssize_t max_tree_entry_len = 4096;
26
f417eed8 27#define STR(x) #x
a4a9cc19 28#define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
c99ba492 29static struct {
f417eed8
JS
30 const char *id_string;
31 const char *downcased;
a4a9cc19 32 const char *camelcased;
1b32b59f 33 enum fsck_msg_type msg_type;
c99ba492 34} msg_id_info[FSCK_MSG_MAX + 1] = {
901f2f67 35 FOREACH_FSCK_MSG_ID(MSG_ID)
a4a9cc19 36 { NULL, NULL, NULL, -1 }
c99ba492
JS
37};
38#undef MSG_ID
b5495024 39#undef STR
c99ba492 40
a46baac6 41static void prepare_msg_ids(void)
f417eed8
JS
42{
43 int i;
44
a46baac6
NTND
45 if (msg_id_info[0].downcased)
46 return;
47
48 /* convert id_string to lower case, without underscores. */
49 for (i = 0; i < FSCK_MSG_MAX; i++) {
50 const char *p = msg_id_info[i].id_string;
51 int len = strlen(p);
52 char *q = xmalloc(len);
53
54 msg_id_info[i].downcased = q;
55 while (*p)
56 if (*p == '_')
57 p++;
58 else
59 *(q)++ = tolower(*(p)++);
60 *q = '\0';
a4a9cc19
NTND
61
62 p = msg_id_info[i].id_string;
63 q = xmalloc(len);
64 msg_id_info[i].camelcased = q;
65 while (*p) {
66 if (*p == '_') {
67 p++;
68 if (*p)
69 *q++ = *p++;
70 } else {
71 *q++ = tolower(*p++);
72 }
f417eed8 73 }
a4a9cc19 74 *q = '\0';
f417eed8 75 }
a46baac6
NTND
76}
77
78static int parse_msg_id(const char *text)
79{
80 int i;
81
82 prepare_msg_ids();
f417eed8
JS
83
84 for (i = 0; i < FSCK_MSG_MAX; i++)
85 if (!strcmp(text, msg_id_info[i].downcased))
86 return i;
87
88 return -1;
89}
90
3ac68a93
NTND
91void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
92{
93 int i;
94
95 prepare_msg_ids();
96
3ac68a93 97 for (i = 0; i < FSCK_MSG_MAX; i++)
a4a9cc19 98 list_config_item(list, prefix, msg_id_info[i].camelcased);
3ac68a93
NTND
99}
100
1b32b59f 101static enum fsck_msg_type fsck_msg_type(enum fsck_msg_id msg_id,
c99ba492
JS
102 struct fsck_options *options)
103{
0282f4dc
JS
104 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
105
e35d65a7 106 if (!options->msg_type) {
1b32b59f 107 enum fsck_msg_type msg_type = msg_id_info[msg_id].msg_type;
e35d65a7 108
0282f4dc
JS
109 if (options->strict && msg_type == FSCK_WARN)
110 msg_type = FSCK_ERROR;
e35d65a7 111 return msg_type;
0282f4dc 112 }
c99ba492 113
e35d65a7 114 return options->msg_type[msg_id];
c99ba492
JS
115}
116
1b32b59f 117static enum fsck_msg_type parse_msg_type(const char *str)
0282f4dc
JS
118{
119 if (!strcmp(str, "error"))
120 return FSCK_ERROR;
121 else if (!strcmp(str, "warn"))
122 return FSCK_WARN;
efaba7cc
JS
123 else if (!strcmp(str, "ignore"))
124 return FSCK_IGNORE;
0282f4dc
JS
125 else
126 die("Unknown fsck message type: '%s'", str);
127}
128
5d477a33
JS
129int is_valid_msg_type(const char *msg_id, const char *msg_type)
130{
131 if (parse_msg_id(msg_id) < 0)
132 return 0;
133 parse_msg_type(msg_type);
134 return 1;
135}
136
53692df2
ÆAB
137void fsck_set_msg_type_from_ids(struct fsck_options *options,
138 enum fsck_msg_id msg_id,
139 enum fsck_msg_type msg_type)
140{
141 if (!options->msg_type) {
142 int i;
143 enum fsck_msg_type *severity;
144 ALLOC_ARRAY(severity, FSCK_MSG_MAX);
145 for (i = 0; i < FSCK_MSG_MAX; i++)
146 severity[i] = fsck_msg_type(i, options);
147 options->msg_type = severity;
148 }
149
150 options->msg_type[msg_id] = msg_type;
151}
152
0282f4dc 153void fsck_set_msg_type(struct fsck_options *options,
f1abc2d0 154 const char *msg_id_str, const char *msg_type_str)
0282f4dc 155{
1b32b59f 156 int msg_id = parse_msg_id(msg_id_str);
0fbcaef6
JK
157 char *to_free = NULL;
158 enum fsck_msg_type msg_type;
0282f4dc 159
f1abc2d0
ÆAB
160 if (msg_id < 0)
161 die("Unhandled message id: %s", msg_id_str);
0282f4dc 162
0fbcaef6
JK
163 if (msg_id == FSCK_MSG_LARGE_PATHNAME) {
164 const char *colon = strchr(msg_type_str, ':');
165 if (colon) {
166 msg_type_str = to_free =
167 xmemdupz(msg_type_str, colon - msg_type_str);
168 colon++;
169 if (!git_parse_ssize_t(colon, &max_tree_entry_len))
170 die("unable to parse max tree entry len: %s", colon);
171 }
172 }
173 msg_type = parse_msg_type(msg_type_str);
174
f1abc2d0
ÆAB
175 if (msg_type != FSCK_ERROR && msg_id_info[msg_id].msg_type == FSCK_FATAL)
176 die("Cannot demote %s to %s", msg_id_str, msg_type_str);
f50c4407 177
53692df2 178 fsck_set_msg_type_from_ids(options, msg_id, msg_type);
0fbcaef6 179 free(to_free);
0282f4dc
JS
180}
181
182void fsck_set_msg_types(struct fsck_options *options, const char *values)
183{
184 char *buf = xstrdup(values), *to_free = buf;
185 int done = 0;
186
187 while (!done) {
188 int len = strcspn(buf, " ,|"), equal;
189
190 done = !buf[len];
191 if (!len) {
192 buf++;
193 continue;
194 }
195 buf[len] = '\0';
196
197 for (equal = 0;
198 equal < len && buf[equal] != '=' && buf[equal] != ':';
199 equal++)
200 buf[equal] = tolower(buf[equal]);
201 buf[equal] = '\0';
202
cd94c6f9
JS
203 if (!strcmp(buf, "skiplist")) {
204 if (equal == len)
205 die("skiplist requires a path");
24eb33eb 206 oidset_parse_file(&options->skiplist, buf + equal + 1);
cd94c6f9
JS
207 buf += len + 1;
208 continue;
209 }
210
0282f4dc
JS
211 if (equal == len)
212 die("Missing '=': '%s'", buf);
355885d5 213
0282f4dc
JS
214 fsck_set_msg_type(options, buf, buf + equal + 1);
215 buf += len + 1;
216 }
217 free(to_free);
218}
219
f5979376
JK
220static int object_on_skiplist(struct fsck_options *opts,
221 const struct object_id *oid)
fb162877 222{
f5979376 223 return opts && oid && oidset_contains(&opts->skiplist, oid);
fb162877
RJ
224}
225
38370253
JK
226__attribute__((format (printf, 5, 6)))
227static int report(struct fsck_options *options,
228 const struct object_id *oid, enum object_type object_type,
35af754b 229 enum fsck_msg_id msg_id, const char *fmt, ...)
c99ba492
JS
230{
231 va_list ap;
232 struct strbuf sb = STRBUF_INIT;
1b32b59f
ÆAB
233 enum fsck_msg_type msg_type = fsck_msg_type(msg_id, options);
234 int result;
c99ba492 235
efaba7cc
JS
236 if (msg_type == FSCK_IGNORE)
237 return 0;
238
38370253 239 if (object_on_skiplist(options, oid))
cd94c6f9
JS
240 return 0;
241
f50c4407
JS
242 if (msg_type == FSCK_FATAL)
243 msg_type = FSCK_ERROR;
f27d05b1
JS
244 else if (msg_type == FSCK_INFO)
245 msg_type = FSCK_WARN;
f50c4407 246
034a7b7b 247 prepare_msg_ids();
35af754b 248 strbuf_addf(&sb, "%s: ", msg_id_info[msg_id].camelcased);
71ab8fa8 249
c99ba492
JS
250 va_start(ap, fmt);
251 strbuf_vaddf(&sb, fmt, ap);
38370253 252 result = options->error_func(options, oid, object_type,
394d5d31 253 msg_type, msg_id, sb.buf);
c99ba492
JS
254 strbuf_release(&sb);
255 va_end(ap);
256
257 return result;
258}
259
a59cfb32
JK
260void fsck_enable_object_names(struct fsck_options *options)
261{
262 if (!options->object_names)
73390290 263 options->object_names = kh_init_oid_map();
a59cfb32
JK
264}
265
73390290
JK
266const char *fsck_get_object_name(struct fsck_options *options,
267 const struct object_id *oid)
7b35efd7 268{
73390290 269 khiter_t pos;
7b35efd7
JS
270 if (!options->object_names)
271 return NULL;
73390290
JK
272 pos = kh_get_oid_map(options->object_names, *oid);
273 if (pos >= kh_end(options->object_names))
274 return NULL;
275 return kh_value(options->object_names, pos);
7b35efd7
JS
276}
277
73390290
JK
278void fsck_put_object_name(struct fsck_options *options,
279 const struct object_id *oid,
a59cfb32 280 const char *fmt, ...)
7b35efd7
JS
281{
282 va_list ap;
283 struct strbuf buf = STRBUF_INIT;
73390290
JK
284 khiter_t pos;
285 int hashret;
7b35efd7
JS
286
287 if (!options->object_names)
288 return;
73390290
JK
289
290 pos = kh_put_oid_map(options->object_names, *oid, &hashret);
291 if (!hashret)
7b35efd7
JS
292 return;
293 va_start(ap, fmt);
294 strbuf_vaddf(&buf, fmt, ap);
73390290 295 kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL);
7b35efd7
JS
296 va_end(ap);
297}
298
a59cfb32 299const char *fsck_describe_object(struct fsck_options *options,
73390290 300 const struct object_id *oid)
90cf590f 301{
a59cfb32
JK
302 static struct strbuf bufs[] = {
303 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
304 };
305 static int b = 0;
306 struct strbuf *buf;
73390290 307 const char *name = fsck_get_object_name(options, oid);
a59cfb32
JK
308
309 buf = bufs + b;
310 b = (b + 1) % ARRAY_SIZE(bufs);
311 strbuf_reset(buf);
73390290 312 strbuf_addstr(buf, oid_to_hex(oid));
a59cfb32
JK
313 if (name)
314 strbuf_addf(buf, " (%s)", name);
90cf590f 315
a59cfb32 316 return buf->buf;
90cf590f
JS
317}
318
22410549 319static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
355885d5
MK
320{
321 struct tree_desc desc;
322 struct name_entry entry;
323 int res = 0;
7b35efd7 324 const char *name;
355885d5
MK
325
326 if (parse_tree(tree))
327 return -1;
328
73390290 329 name = fsck_get_object_name(options, &tree->object.oid);
ec18b10b 330 if (init_tree_desc_gently(&desc, tree->buffer, tree->size, 0))
8354fa3d
DT
331 return -1;
332 while (tree_entry_gently(&desc, &entry)) {
7b35efd7 333 struct object *obj;
355885d5
MK
334 int result;
335
336 if (S_ISGITLINK(entry.mode))
337 continue;
7b35efd7
JS
338
339 if (S_ISDIR(entry.mode)) {
ea82b2a0 340 obj = (struct object *)lookup_tree(the_repository, &entry.oid);
2720f6db 341 if (name && obj)
73390290 342 fsck_put_object_name(options, &entry.oid, "%s%s/",
a59cfb32 343 name, entry.path);
7b35efd7
JS
344 result = options->walk(obj, OBJ_TREE, data, options);
345 }
346 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
ea82b2a0 347 obj = (struct object *)lookup_blob(the_repository, &entry.oid);
2720f6db 348 if (name && obj)
73390290 349 fsck_put_object_name(options, &entry.oid, "%s%s",
a59cfb32 350 name, entry.path);
7b35efd7
JS
351 result = options->walk(obj, OBJ_BLOB, data, options);
352 }
355885d5 353 else {
82247e9b 354 result = error("in tree %s: entry %s has bad mode %.6o",
73390290 355 fsck_describe_object(options, &tree->object.oid),
a59cfb32 356 entry.path, entry.mode);
355885d5
MK
357 }
358 if (result < 0)
359 return result;
360 if (!res)
361 res = result;
362 }
363 return res;
364}
365
22410549 366static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
355885d5 367{
7b35efd7 368 int counter = 0, generation = 0, name_prefix_len = 0;
355885d5
MK
369 struct commit_list *parents;
370 int res;
371 int result;
7b35efd7 372 const char *name;
355885d5 373
ecb5091f 374 if (repo_parse_commit(the_repository, commit))
355885d5
MK
375 return -1;
376
73390290 377 name = fsck_get_object_name(options, &commit->object.oid);
7b35efd7 378 if (name)
73390290 379 fsck_put_object_name(options, get_commit_tree_oid(commit),
a59cfb32 380 "%s:", name);
7b35efd7 381
ecb5091f 382 result = options->walk((struct object *) repo_get_commit_tree(the_repository, commit),
2e27bd77 383 OBJ_TREE, data, options);
355885d5
MK
384 if (result < 0)
385 return result;
386 res = result;
387
388 parents = commit->parents;
7b35efd7
JS
389 if (name && parents) {
390 int len = strlen(name), power;
391
392 if (len && name[len - 1] == '^') {
393 generation = 1;
394 name_prefix_len = len - 1;
395 }
396 else { /* parse ~<generation> suffix */
397 for (generation = 0, power = 1;
398 len && isdigit(name[len - 1]);
399 power *= 10)
400 generation += power * (name[--len] - '0');
401 if (power > 1 && len && name[len - 1] == '~')
402 name_prefix_len = len - 1;
e89f8936
JS
403 else {
404 /* Maybe a non-first parent, e.g. HEAD^2 */
405 generation = 0;
406 name_prefix_len = len;
407 }
7b35efd7
JS
408 }
409 }
410
355885d5 411 while (parents) {
7b35efd7 412 if (name) {
73390290 413 struct object_id *oid = &parents->item->object.oid;
7b35efd7 414
b84c7838 415 if (counter++)
73390290 416 fsck_put_object_name(options, oid, "%s^%d",
a59cfb32 417 name, counter);
7b35efd7 418 else if (generation > 0)
73390290 419 fsck_put_object_name(options, oid, "%.*s~%d",
a59cfb32
JK
420 name_prefix_len, name,
421 generation + 1);
7b35efd7 422 else
73390290 423 fsck_put_object_name(options, oid, "%s^", name);
7b35efd7 424 }
22410549 425 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
355885d5
MK
426 if (result < 0)
427 return result;
428 if (!res)
429 res = result;
430 parents = parents->next;
431 }
432 return res;
433}
434
22410549 435static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
355885d5 436{
73390290 437 const char *name = fsck_get_object_name(options, &tag->object.oid);
7b35efd7 438
355885d5
MK
439 if (parse_tag(tag))
440 return -1;
7b35efd7 441 if (name)
73390290 442 fsck_put_object_name(options, &tag->tagged->oid, "%s", name);
22410549 443 return options->walk(tag->tagged, OBJ_ANY, data, options);
355885d5
MK
444}
445
22410549 446int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
355885d5
MK
447{
448 if (!obj)
449 return -1;
a2b22854
JK
450
451 if (obj->type == OBJ_NONE)
109cd76d 452 parse_object(the_repository, &obj->oid);
a2b22854 453
355885d5
MK
454 switch (obj->type) {
455 case OBJ_BLOB:
456 return 0;
457 case OBJ_TREE:
22410549 458 return fsck_walk_tree((struct tree *)obj, data, options);
355885d5 459 case OBJ_COMMIT:
22410549 460 return fsck_walk_commit((struct commit *)obj, data, options);
355885d5 461 case OBJ_TAG:
22410549 462 return fsck_walk_tag((struct tag *)obj, data, options);
355885d5 463 default:
a59cfb32 464 error("Unknown object type for %s",
73390290 465 fsck_describe_object(options, &obj->oid));
355885d5
MK
466 return -1;
467 }
468}
ba002f3b 469
9068cfb2
RS
470struct name_stack {
471 const char **names;
472 size_t nr, alloc;
473};
474
475static void name_stack_push(struct name_stack *stack, const char *name)
476{
477 ALLOC_GROW(stack->names, stack->nr + 1, stack->alloc);
478 stack->names[stack->nr++] = name;
479}
480
481static const char *name_stack_pop(struct name_stack *stack)
482{
483 return stack->nr ? stack->names[--stack->nr] : NULL;
484}
485
486static void name_stack_clear(struct name_stack *stack)
487{
488 FREE_AND_NULL(stack->names);
489 stack->nr = stack->alloc = 0;
490}
491
ba002f3b
MK
492/*
493 * The entries in a tree are ordered in the _path_ order,
494 * which means that a directory entry is ordered by adding
495 * a slash to the end of it.
496 *
497 * So a directory called "a" is ordered _after_ a file
498 * called "a.c", because "a/" sorts after "a.c".
499 */
500#define TREE_UNORDERED (-1)
501#define TREE_HAS_DUPS (-2)
502
9068cfb2
RS
503static int is_less_than_slash(unsigned char c)
504{
505 return '\0' < c && c < '/';
506}
507
508static int verify_ordered(unsigned mode1, const char *name1,
509 unsigned mode2, const char *name2,
510 struct name_stack *candidates)
ba002f3b
MK
511{
512 int len1 = strlen(name1);
513 int len2 = strlen(name2);
514 int len = len1 < len2 ? len1 : len2;
515 unsigned char c1, c2;
516 int cmp;
517
518 cmp = memcmp(name1, name2, len);
519 if (cmp < 0)
520 return 0;
521 if (cmp > 0)
522 return TREE_UNORDERED;
523
524 /*
525 * Ok, the first <len> characters are the same.
526 * Now we need to order the next one, but turn
527 * a '\0' into a '/' for a directory entry.
528 */
529 c1 = name1[len];
530 c2 = name2[len];
531 if (!c1 && !c2)
532 /*
533 * git-write-tree used to write out a nonsense tree that has
534 * entries with the same name, one blob and one tree. Make
535 * sure we do not have duplicate entries.
536 */
537 return TREE_HAS_DUPS;
538 if (!c1 && S_ISDIR(mode1))
539 c1 = '/';
540 if (!c2 && S_ISDIR(mode2))
541 c2 = '/';
9068cfb2
RS
542
543 /*
544 * There can be non-consecutive duplicates due to the implicitly
86715592 545 * added slash, e.g.:
9068cfb2
RS
546 *
547 * foo
548 * foo.bar
549 * foo.bar.baz
550 * foo.bar/
551 * foo/
552 *
553 * Record non-directory candidates (like "foo" and "foo.bar" in
554 * the example) on a stack and check directory candidates (like
555 * foo/" and "foo.bar/") against that stack.
556 */
557 if (!c1 && is_less_than_slash(c2)) {
558 name_stack_push(candidates, name1);
559 } else if (c2 == '/' && is_less_than_slash(c1)) {
560 for (;;) {
561 const char *p;
562 const char *f_name = name_stack_pop(candidates);
563
564 if (!f_name)
565 break;
566 if (!skip_prefix(name2, f_name, &p))
fe747043 567 continue;
9068cfb2
RS
568 if (!*p)
569 return TREE_HAS_DUPS;
570 if (is_less_than_slash(*p)) {
571 name_stack_push(candidates, f_name);
572 break;
573 }
574 }
575 }
576
ba002f3b
MK
577 return c1 < c2 ? 0 : TREE_UNORDERED;
578}
579
9e1947cb 580static int fsck_tree(const struct object_id *tree_oid,
23a173a7
JK
581 const char *buffer, unsigned long size,
582 struct fsck_options *options)
ba002f3b 583{
8354fa3d 584 int retval = 0;
c479d14a 585 int has_null_sha1 = 0;
ba002f3b
MK
586 int has_full_path = 0;
587 int has_empty_name = 0;
5d34a435
JK
588 int has_dot = 0;
589 int has_dotdot = 0;
5c17f512 590 int has_dotgit = 0;
ba002f3b
MK
591 int has_zero_pad = 0;
592 int has_bad_modes = 0;
593 int has_dup_entries = 0;
594 int not_properly_sorted = 0;
0fbcaef6 595 int has_large_name = 0;
ba002f3b
MK
596 struct tree_desc desc;
597 unsigned o_mode;
598 const char *o_name;
9068cfb2 599 struct name_stack df_dup_candidates = { NULL };
ba002f3b 600
53602a93 601 if (init_tree_desc_gently(&desc, buffer, size, TREE_DESC_RAW_MODES)) {
0282f679
JK
602 retval += report(options, tree_oid, OBJ_TREE,
603 FSCK_MSG_BAD_TREE,
604 "cannot be parsed as a tree");
8354fa3d
DT
605 return retval;
606 }
ba002f3b
MK
607
608 o_mode = 0;
609 o_name = NULL;
ba002f3b
MK
610
611 while (desc.size) {
5ec1e728 612 unsigned short mode;
288a74bc 613 const char *name, *backslash;
9e1947cb 614 const struct object_id *entry_oid;
ba002f3b 615
9e1947cb 616 entry_oid = tree_entry_extract(&desc, &name, &mode);
ba002f3b 617
9e1947cb 618 has_null_sha1 |= is_null_oid(entry_oid);
effd12ec
HS
619 has_full_path |= !!strchr(name, '/');
620 has_empty_name |= !*name;
621 has_dot |= !strcmp(name, ".");
622 has_dotdot |= !strcmp(name, "..");
ed9c3220 623 has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
ba002f3b 624 has_zero_pad |= *(char *)desc.buffer == '0';
0fbcaef6 625 has_large_name |= tree_entry_len(&desc.entry) > max_tree_entry_len;
159e7b08 626
b7b1fca1
JK
627 if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
628 if (!S_ISLNK(mode))
9e1947cb
JK
629 oidset_insert(&options->gitmodules_found,
630 entry_oid);
b7b1fca1 631 else
38370253 632 retval += report(options,
9e1947cb 633 tree_oid, OBJ_TREE,
b7b1fca1
JK
634 FSCK_MSG_GITMODULES_SYMLINK,
635 ".gitmodules is a symbolic link");
636 }
159e7b08 637
f8587c31 638 if (is_hfs_dotgitattributes(name) || is_ntfs_dotgitattributes(name)) {
27ab4784
PS
639 if (!S_ISLNK(mode))
640 oidset_insert(&options->gitattributes_found,
641 entry_oid);
642 else
f8587c31
PS
643 retval += report(options, tree_oid, OBJ_TREE,
644 FSCK_MSG_GITATTRIBUTES_SYMLINK,
645 ".gitattributes is a symlink");
646 }
647
bb6832d5
JK
648 if (S_ISLNK(mode)) {
649 if (is_hfs_dotgitignore(name) ||
650 is_ntfs_dotgitignore(name))
651 retval += report(options, tree_oid, OBJ_TREE,
652 FSCK_MSG_GITIGNORE_SYMLINK,
653 ".gitignore is a symlink");
bb6832d5
JK
654 if (is_hfs_dotmailmap(name) ||
655 is_ntfs_dotmailmap(name))
656 retval += report(options, tree_oid, OBJ_TREE,
657 FSCK_MSG_MAILMAP_SYMLINK,
658 ".mailmap is a symlink");
659 }
660
288a74bc
JS
661 if ((backslash = strchr(name, '\\'))) {
662 while (backslash) {
663 backslash++;
664 has_dotgit |= is_ntfs_dotgit(backslash);
bdfef049
JS
665 if (is_ntfs_dotgitmodules(backslash)) {
666 if (!S_ISLNK(mode))
9e1947cb
JK
667 oidset_insert(&options->gitmodules_found,
668 entry_oid);
bdfef049 669 else
9e1947cb 670 retval += report(options, tree_oid, OBJ_TREE,
bdfef049
JS
671 FSCK_MSG_GITMODULES_SYMLINK,
672 ".gitmodules is a symbolic link");
673 }
288a74bc
JS
674 backslash = strchr(backslash, '\\');
675 }
676 }
677
8354fa3d 678 if (update_tree_entry_gently(&desc)) {
0282f679
JK
679 retval += report(options, tree_oid, OBJ_TREE,
680 FSCK_MSG_BAD_TREE,
681 "cannot be parsed as a tree");
8354fa3d
DT
682 break;
683 }
ba002f3b
MK
684
685 switch (mode) {
686 /*
687 * Standard modes..
688 */
689 case S_IFREG | 0755:
690 case S_IFREG | 0644:
691 case S_IFLNK:
692 case S_IFDIR:
693 case S_IFGITLINK:
694 break;
695 /*
696 * This is nonstandard, but we had a few of these
697 * early on when we honored the full set of mode
698 * bits..
699 */
700 case S_IFREG | 0664:
22410549 701 if (!options->strict)
ba002f3b 702 break;
1cf01a34 703 /* fallthrough */
ba002f3b
MK
704 default:
705 has_bad_modes = 1;
706 }
707
708 if (o_name) {
9068cfb2
RS
709 switch (verify_ordered(o_mode, o_name, mode, name,
710 &df_dup_candidates)) {
ba002f3b
MK
711 case TREE_UNORDERED:
712 not_properly_sorted = 1;
713 break;
714 case TREE_HAS_DUPS:
715 has_dup_entries = 1;
716 break;
717 default:
718 break;
719 }
720 }
721
722 o_mode = mode;
723 o_name = name;
ba002f3b
MK
724 }
725
9068cfb2
RS
726 name_stack_clear(&df_dup_candidates);
727
c479d14a 728 if (has_null_sha1)
0282f679
JK
729 retval += report(options, tree_oid, OBJ_TREE,
730 FSCK_MSG_NULL_SHA1,
731 "contains entries pointing to null sha1");
ba002f3b 732 if (has_full_path)
0282f679
JK
733 retval += report(options, tree_oid, OBJ_TREE,
734 FSCK_MSG_FULL_PATHNAME,
735 "contains full pathnames");
ba002f3b 736 if (has_empty_name)
0282f679
JK
737 retval += report(options, tree_oid, OBJ_TREE,
738 FSCK_MSG_EMPTY_NAME,
739 "contains empty pathname");
5d34a435 740 if (has_dot)
0282f679
JK
741 retval += report(options, tree_oid, OBJ_TREE,
742 FSCK_MSG_HAS_DOT,
743 "contains '.'");
5d34a435 744 if (has_dotdot)
0282f679
JK
745 retval += report(options, tree_oid, OBJ_TREE,
746 FSCK_MSG_HAS_DOTDOT,
747 "contains '..'");
5c17f512 748 if (has_dotgit)
0282f679
JK
749 retval += report(options, tree_oid, OBJ_TREE,
750 FSCK_MSG_HAS_DOTGIT,
751 "contains '.git'");
ba002f3b 752 if (has_zero_pad)
0282f679
JK
753 retval += report(options, tree_oid, OBJ_TREE,
754 FSCK_MSG_ZERO_PADDED_FILEMODE,
755 "contains zero-padded file modes");
ba002f3b 756 if (has_bad_modes)
0282f679
JK
757 retval += report(options, tree_oid, OBJ_TREE,
758 FSCK_MSG_BAD_FILEMODE,
759 "contains bad file modes");
ba002f3b 760 if (has_dup_entries)
0282f679
JK
761 retval += report(options, tree_oid, OBJ_TREE,
762 FSCK_MSG_DUPLICATE_ENTRIES,
763 "contains duplicate file entries");
ba002f3b 764 if (not_properly_sorted)
0282f679
JK
765 retval += report(options, tree_oid, OBJ_TREE,
766 FSCK_MSG_TREE_NOT_SORTED,
767 "not properly sorted");
0fbcaef6
JK
768 if (has_large_name)
769 retval += report(options, tree_oid, OBJ_TREE,
770 FSCK_MSG_LARGE_PATHNAME,
771 "contains excessively large pathname");
ba002f3b
MK
772 return retval;
773}
774
8e430903
JK
775/*
776 * Confirm that the headers of a commit or tag object end in a reasonable way,
777 * either with the usual "\n\n" separator, or at least with a trailing newline
778 * on the final header line.
779 *
780 * This property is important for the memory safety of our callers. It allows
781 * them to scan the buffer linewise without constantly checking the remaining
782 * size as long as:
783 *
784 * - they check that there are bytes left in the buffer at the start of any
785 * line (i.e., that the last newline they saw was not the final one we
786 * found here)
787 *
788 * - any intra-line scanning they do will stop at a newline, which will worst
789 * case hit the newline we found here as the end-of-header. This makes it
790 * OK for them to use helpers like parse_oid_hex(), or even skip_prefix().
791 */
84d18c0b 792static int verify_headers(const void *data, unsigned long size,
cc579000
JK
793 const struct object_id *oid, enum object_type type,
794 struct fsck_options *options)
4d0d8975
JS
795{
796 const char *buffer = (const char *)data;
797 unsigned long i;
798
799 for (i = 0; i < size; i++) {
800 switch (buffer[i]) {
801 case '\0':
cc579000 802 return report(options, oid, type,
c99ba492
JS
803 FSCK_MSG_NUL_IN_HEADER,
804 "unterminated header: NUL at offset %ld", i);
4d0d8975
JS
805 case '\n':
806 if (i + 1 < size && buffer[i + 1] == '\n')
807 return 0;
808 }
809 }
810
84d18c0b
JH
811 /*
812 * We did not find double-LF that separates the header
813 * and the body. Not having a body is not a crime but
814 * we do want to see the terminating LF for the last header
815 * line.
816 */
817 if (size && buffer[size - 1] == '\n')
818 return 0;
819
cc579000 820 return report(options, oid, type,
c99ba492 821 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
4d0d8975
JS
822}
823
78543993
JK
824static int fsck_ident(const char **ident,
825 const struct object_id *oid, enum object_type type,
826 struct fsck_options *options)
daae1922 827{
e6826e33 828 const char *p = *ident;
d4b8de04
JK
829 char *end;
830
e6826e33
JS
831 *ident = strchrnul(*ident, '\n');
832 if (**ident == '\n')
833 (*ident)++;
834
835 if (*p == '<')
78543993 836 return report(options, oid, type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
837 p += strcspn(p, "<>\n");
838 if (*p == '>')
78543993 839 return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
e6826e33 840 if (*p != '<')
78543993 841 return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
e6826e33 842 if (p[-1] != ' ')
78543993 843 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
844 p++;
845 p += strcspn(p, "<>\n");
846 if (*p != '>')
78543993 847 return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
e6826e33
JS
848 p++;
849 if (*p != ' ')
78543993 850 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
e6826e33 851 p++;
8e430903
JK
852 /*
853 * Our timestamp parser is based on the C strto*() functions, which
854 * will happily eat whitespace, including the newline that is supposed
855 * to prevent us walking past the end of the buffer. So do our own
856 * scan, skipping linear whitespace but not newlines, and then
857 * confirming we found a digit. We _could_ be even more strict here,
858 * as we really expect only a single space, but since we have
859 * traditionally allowed extra whitespace, we'll continue to do so.
860 */
861 while (*p == ' ' || *p == '\t')
862 p++;
863 if (!isdigit(*p))
864 return report(options, oid, type, FSCK_MSG_BAD_DATE,
865 "invalid author/committer line - bad date");
e6826e33 866 if (*p == '0' && p[1] != ' ')
78543993 867 return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
1aeb7e75 868 if (date_overflows(parse_timestamp(p, &end, 10)))
78543993 869 return report(options, oid, type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
e6826e33 870 if ((end == p || *end != ' '))
78543993 871 return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
e6826e33
JS
872 p = end + 1;
873 if ((*p != '+' && *p != '-') ||
874 !isdigit(p[1]) ||
875 !isdigit(p[2]) ||
876 !isdigit(p[3]) ||
877 !isdigit(p[4]) ||
878 (p[5] != '\n'))
78543993 879 return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
e6826e33 880 p += 6;
daae1922
JN
881 return 0;
882}
883
c5b4269b
JK
884static int fsck_commit(const struct object_id *oid,
885 const char *buffer, unsigned long size,
886 struct fsck_options *options)
ba002f3b 887{
f648ee70 888 struct object_id tree_oid, parent_oid;
ec652315 889 unsigned author_count;
daae1922 890 int err;
6d2d780f 891 const char *buffer_begin = buffer;
8e430903 892 const char *buffer_end = buffer + size;
c54f5ca9 893 const char *p;
ba002f3b 894
8e430903
JK
895 /*
896 * We _must_ stop parsing immediately if this reports failure, as the
897 * memory safety of the rest of the function depends on it. See the
898 * comment above the definition of verify_headers() for more details.
899 */
c5b4269b 900 if (verify_headers(buffer, size, oid, OBJ_COMMIT, options))
4d0d8975
JS
901 return -1;
902
8e430903 903 if (buffer >= buffer_end || !skip_prefix(buffer, "tree ", &buffer))
c5b4269b 904 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
c54f5ca9 905 if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
c5b4269b 906 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
b3584761
JS
907 if (err)
908 return err;
909 }
c54f5ca9 910 buffer = p + 1;
8e430903 911 while (buffer < buffer_end && skip_prefix(buffer, "parent ", &buffer)) {
f648ee70 912 if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') {
c5b4269b 913 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
b3584761
JS
914 if (err)
915 return err;
916 }
c54f5ca9 917 buffer = p + 1;
ba002f3b 918 }
c9ad147f 919 author_count = 0;
8e430903 920 while (buffer < buffer_end && skip_prefix(buffer, "author ", &buffer)) {
c9ad147f 921 author_count++;
c5b4269b 922 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
c9ad147f
JS
923 if (err)
924 return err;
ba002f3b 925 }
c9ad147f 926 if (author_count < 1)
c5b4269b 927 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
c9ad147f 928 else if (author_count > 1)
c5b4269b 929 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
daae1922
JN
930 if (err)
931 return err;
8e430903 932 if (buffer >= buffer_end || !skip_prefix(buffer, "committer ", &buffer))
c5b4269b
JK
933 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
934 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
daae1922
JN
935 if (err)
936 return err;
6d2d780f 937 if (memchr(buffer_begin, '\0', size)) {
c5b4269b 938 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT,
6d2d780f
JH
939 "NUL byte in the commit object body");
940 if (err)
941 return err;
942 }
ba002f3b
MK
943 return 0;
944}
945
103fb6d4 946static int fsck_tag(const struct object_id *oid, const char *buffer,
2175a0c6 947 unsigned long size, struct fsck_options *options)
cec097be 948{
f648ee70 949 struct object_id tagged_oid;
acf9de4c
ÆAB
950 int tagged_type;
951 return fsck_tag_standalone(oid, buffer, size, options, &tagged_oid,
952 &tagged_type);
953}
954
955int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
956 unsigned long size, struct fsck_options *options,
957 struct object_id *tagged_oid,
958 int *tagged_type)
959{
cec097be 960 int ret = 0;
23a173a7 961 char *eol;
cec097be 962 struct strbuf sb = STRBUF_INIT;
8e430903 963 const char *buffer_end = buffer + size;
c54f5ca9 964 const char *p;
cec097be 965
8e430903
JK
966 /*
967 * We _must_ stop parsing immediately if this reports failure, as the
968 * memory safety of the rest of the function depends on it. See the
969 * comment above the definition of verify_headers() for more details.
970 */
103fb6d4 971 ret = verify_headers(buffer, size, oid, OBJ_TAG, options);
8a272f29 972 if (ret)
cec097be
JS
973 goto done;
974
8e430903 975 if (buffer >= buffer_end || !skip_prefix(buffer, "object ", &buffer)) {
103fb6d4 976 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
cec097be
JS
977 goto done;
978 }
acf9de4c 979 if (parse_oid_hex(buffer, tagged_oid, &p) || *p != '\n') {
103fb6d4 980 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
7d7d5b05
JS
981 if (ret)
982 goto done;
cec097be 983 }
c54f5ca9 984 buffer = p + 1;
cec097be 985
8e430903 986 if (buffer >= buffer_end || !skip_prefix(buffer, "type ", &buffer)) {
103fb6d4 987 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
cec097be
JS
988 goto done;
989 }
8e430903 990 eol = memchr(buffer, '\n', buffer_end - buffer);
cec097be 991 if (!eol) {
103fb6d4 992 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
cec097be
JS
993 goto done;
994 }
acf9de4c
ÆAB
995 *tagged_type = type_from_string_gently(buffer, eol - buffer, 1);
996 if (*tagged_type < 0)
103fb6d4 997 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
cec097be
JS
998 if (ret)
999 goto done;
1000 buffer = eol + 1;
1001
8e430903 1002 if (buffer >= buffer_end || !skip_prefix(buffer, "tag ", &buffer)) {
103fb6d4 1003 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
cec097be
JS
1004 goto done;
1005 }
8e430903 1006 eol = memchr(buffer, '\n', buffer_end - buffer);
cec097be 1007 if (!eol) {
103fb6d4 1008 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
cec097be
JS
1009 goto done;
1010 }
1011 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
f27d05b1 1012 if (check_refname_format(sb.buf, 0)) {
103fb6d4 1013 ret = report(options, oid, OBJ_TAG,
38370253
JK
1014 FSCK_MSG_BAD_TAG_NAME,
1015 "invalid 'tag' name: %.*s",
1016 (int)(eol - buffer), buffer);
f27d05b1
JS
1017 if (ret)
1018 goto done;
1019 }
cec097be
JS
1020 buffer = eol + 1;
1021
8e430903 1022 if (buffer >= buffer_end || !skip_prefix(buffer, "tagger ", &buffer)) {
cec097be 1023 /* early tags do not contain 'tagger' lines; warn only */
103fb6d4 1024 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
f27d05b1
JS
1025 if (ret)
1026 goto done;
1027 }
cec097be 1028 else
103fb6d4 1029 ret = fsck_ident(&buffer, oid, OBJ_TAG, options);
cec097be 1030
8e430903 1031 if (buffer < buffer_end && !starts_with(buffer, "\n")) {
acf9de4c
ÆAB
1032 /*
1033 * The verify_headers() check will allow
1034 * e.g. "[...]tagger <tagger>\nsome
1035 * garbage\n\nmessage" to pass, thinking "some
1036 * garbage" could be a custom header. E.g. "mktag"
1037 * doesn't want any unknown headers.
1038 */
1039 ret = report(options, oid, OBJ_TAG, FSCK_MSG_EXTRA_HEADER_ENTRY, "invalid format - extra header(s) after 'tagger'");
1040 if (ret)
1041 goto done;
1042 }
1043
cec097be
JS
1044done:
1045 strbuf_release(&sb);
cec097be
JS
1046 return ret;
1047}
1048
ed8b10f6 1049struct fsck_gitmodules_data {
6da40b22 1050 const struct object_id *oid;
ed8b10f6
JK
1051 struct fsck_options *options;
1052 int ret;
1053};
1054
a4e7e317
GC
1055static int fsck_gitmodules_fn(const char *var, const char *value,
1056 const struct config_context *ctx UNUSED,
1057 void *vdata)
ed8b10f6
JK
1058{
1059 struct fsck_gitmodules_data *data = vdata;
1060 const char *subsection, *key;
f5914f4b 1061 size_t subsection_len;
ed8b10f6
JK
1062 char *name;
1063
1064 if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
1065 !subsection)
1066 return 0;
1067
1068 name = xmemdupz(subsection, subsection_len);
1069 if (check_submodule_name(name) < 0)
38370253 1070 data->ret |= report(data->options,
6da40b22 1071 data->oid, OBJ_BLOB,
ed8b10f6
JK
1072 FSCK_MSG_GITMODULES_NAME,
1073 "disallowed submodule name: %s",
1074 name);
a124133e 1075 if (!strcmp(key, "url") && value &&
07259e74 1076 check_submodule_url(value) < 0)
38370253 1077 data->ret |= report(data->options,
6da40b22 1078 data->oid, OBJ_BLOB,
a124133e
JK
1079 FSCK_MSG_GITMODULES_URL,
1080 "disallowed submodule url: %s",
1081 value);
1a7fd1fb
JK
1082 if (!strcmp(key, "path") && value &&
1083 looks_like_command_line_option(value))
38370253 1084 data->ret |= report(data->options,
6da40b22 1085 data->oid, OBJ_BLOB,
1a7fd1fb
JK
1086 FSCK_MSG_GITMODULES_PATH,
1087 "disallowed submodule path: %s",
1088 value);
bb92255e
JN
1089 if (!strcmp(key, "update") && value &&
1090 parse_submodule_update_type(value) == SM_UPDATE_COMMAND)
7034cd09 1091 data->ret |= report(data->options, data->oid, OBJ_BLOB,
bb92255e
JN
1092 FSCK_MSG_GITMODULES_UPDATE,
1093 "disallowed submodule update setting: %s",
1094 value);
ed8b10f6
JK
1095 free(name);
1096
1097 return 0;
1098}
1099
6da40b22 1100static int fsck_blob(const struct object_id *oid, const char *buf,
7ac4f3a0
JK
1101 unsigned long size, struct fsck_options *options)
1102{
bb3a9265 1103 int ret = 0;
ed8b10f6 1104
6da40b22 1105 if (object_on_skiplist(options, oid))
fb162877
RJ
1106 return 0;
1107
bb3a9265
PS
1108 if (oidset_contains(&options->gitmodules_found, oid)) {
1109 struct config_options config_opts = { 0 };
1110 struct fsck_gitmodules_data data;
1111
1112 oidset_insert(&options->gitmodules_done, oid);
1113
1114 if (!buf) {
1115 /*
1116 * A missing buffer here is a sign that the caller found the
1117 * blob too gigantic to load into memory. Let's just consider
1118 * that an error.
1119 */
1120 return report(options, oid, OBJ_BLOB,
1121 FSCK_MSG_GITMODULES_LARGE,
1122 ".gitmodules too large to parse");
1123 }
1124
1125 data.oid = oid;
1126 data.options = options;
1127 data.ret = 0;
1128 config_opts.error_action = CONFIG_ERROR_SILENT;
1129 if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
809d8680
GC
1130 ".gitmodules", buf, size, &data,
1131 CONFIG_SCOPE_UNKNOWN, &config_opts))
bb3a9265
PS
1132 data.ret |= report(options, oid, OBJ_BLOB,
1133 FSCK_MSG_GITMODULES_PARSE,
1134 "could not parse gitmodules blob");
1135 ret |= data.ret;
ed8b10f6
JK
1136 }
1137
27ab4784
PS
1138 if (oidset_contains(&options->gitattributes_found, oid)) {
1139 const char *ptr;
1140
1141 oidset_insert(&options->gitattributes_done, oid);
1142
1143 if (!buf || size > ATTR_MAX_FILE_SIZE) {
1144 /*
1145 * A missing buffer here is a sign that the caller found the
1146 * blob too gigantic to load into memory. Let's just consider
1147 * that an error.
1148 */
1149 return report(options, oid, OBJ_BLOB,
1150 FSCK_MSG_GITATTRIBUTES_LARGE,
1151 ".gitattributes too large to parse");
1152 }
1153
1154 for (ptr = buf; *ptr; ) {
1155 const char *eol = strchrnul(ptr, '\n');
1156 if (eol - ptr >= ATTR_MAX_LINE_LENGTH) {
1157 ret |= report(options, oid, OBJ_BLOB,
1158 FSCK_MSG_GITATTRIBUTES_LINE_LENGTH,
1159 ".gitattributes has too long lines to parse");
1160 break;
1161 }
1162
1163 ptr = *eol ? eol + 1 : eol;
1164 }
1165 }
1166
bb3a9265 1167 return ret;
7ac4f3a0
JK
1168}
1169
90a398bb 1170int fsck_object(struct object *obj, void *data, unsigned long size,
22410549 1171 struct fsck_options *options)
ba002f3b
MK
1172{
1173 if (!obj)
38370253 1174 return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
ba002f3b 1175
35ff327e
JK
1176 return fsck_buffer(&obj->oid, obj->type, data, size, options);
1177}
1178
1179int fsck_buffer(const struct object_id *oid, enum object_type type,
1180 void *data, unsigned long size,
1181 struct fsck_options *options)
1182{
1183 if (type == OBJ_BLOB)
1184 return fsck_blob(oid, data, size, options);
1185 if (type == OBJ_TREE)
1186 return fsck_tree(oid, data, size, options);
1187 if (type == OBJ_COMMIT)
1188 return fsck_commit(oid, data, size, options);
1189 if (type == OBJ_TAG)
1190 return fsck_tag(oid, data, size, options);
1191
1192 return report(options, oid, type,
38370253
JK
1193 FSCK_MSG_UNKNOWN_TYPE,
1194 "unknown type '%d' (internal fsck error)",
35ff327e 1195 type);
ba002f3b 1196}
d6ffc8d7 1197
1cd772cc 1198int fsck_error_function(struct fsck_options *o,
5afc4b1d 1199 const struct object_id *oid,
0b4e9013 1200 enum object_type object_type UNUSED,
394d5d31 1201 enum fsck_msg_type msg_type,
0b4e9013 1202 enum fsck_msg_id msg_id UNUSED,
394d5d31 1203 const char *message)
d6ffc8d7 1204{
0282f4dc 1205 if (msg_type == FSCK_WARN) {
5afc4b1d 1206 warning("object %s: %s", fsck_describe_object(o, oid), message);
0282f4dc
JS
1207 return 0;
1208 }
5afc4b1d 1209 error("object %s: %s", fsck_describe_object(o, oid), message);
d6ffc8d7
MK
1210 return 1;
1211}
159e7b08 1212
a59a8c68
PS
1213static int fsck_blobs(struct oidset *blobs_found, struct oidset *blobs_done,
1214 enum fsck_msg_id msg_missing, enum fsck_msg_id msg_type,
1215 struct fsck_options *options, const char *blob_type)
159e7b08
JK
1216{
1217 int ret = 0;
1218 struct oidset_iter iter;
1219 const struct object_id *oid;
1220
a59a8c68 1221 oidset_iter_init(blobs_found, &iter);
159e7b08 1222 while ((oid = oidset_iter_next(&iter))) {
159e7b08
JK
1223 enum object_type type;
1224 unsigned long size;
1225 char *buf;
1226
a59a8c68 1227 if (oidset_contains(blobs_done, oid))
159e7b08
JK
1228 continue;
1229
bc726bd0 1230 buf = repo_read_object_file(the_repository, oid, &type, &size);
159e7b08 1231 if (!buf) {
b8b00f16 1232 if (is_promisor_object(oid))
27387444 1233 continue;
38370253 1234 ret |= report(options,
a59a8c68
PS
1235 oid, OBJ_BLOB, msg_missing,
1236 "unable to read %s blob", blob_type);
159e7b08
JK
1237 continue;
1238 }
1239
1240 if (type == OBJ_BLOB)
b8b00f16 1241 ret |= fsck_blob(oid, buf, size, options);
159e7b08 1242 else
a59a8c68
PS
1243 ret |= report(options, oid, type, msg_type,
1244 "non-blob found at %s", blob_type);
159e7b08
JK
1245 free(buf);
1246 }
1247
a59a8c68
PS
1248 oidset_clear(blobs_found);
1249 oidset_clear(blobs_done);
1250
1251 return ret;
1252}
1253
1254int fsck_finish(struct fsck_options *options)
1255{
1256 int ret = 0;
1257
1258 ret |= fsck_blobs(&options->gitmodules_found, &options->gitmodules_done,
1259 FSCK_MSG_GITMODULES_MISSING, FSCK_MSG_GITMODULES_BLOB,
1260 options, ".gitmodules");
27ab4784
PS
1261 ret |= fsck_blobs(&options->gitattributes_found, &options->gitattributes_done,
1262 FSCK_MSG_GITATTRIBUTES_MISSING, FSCK_MSG_GITATTRIBUTES_BLOB,
1263 options, ".gitattributes");
159e7b08 1264
159e7b08
JK
1265 return ret;
1266}
1f3299fd 1267
a4e7e317
GC
1268int git_fsck_config(const char *var, const char *value,
1269 const struct config_context *ctx, void *cb)
1f3299fd 1270{
fb79f5bf 1271 struct fsck_options *options = cb;
d49cb162
JK
1272 const char *msg_id;
1273
1f3299fd
ÆAB
1274 if (strcmp(var, "fsck.skiplist") == 0) {
1275 const char *path;
1276 struct strbuf sb = STRBUF_INIT;
1277
1278 if (git_config_pathname(&path, var, value))
1279 return 1;
1280 strbuf_addf(&sb, "skiplist=%s", path);
1281 free((char *)path);
1282 fsck_set_msg_types(options, sb.buf);
1283 strbuf_release(&sb);
1284 return 0;
1285 }
1286
d49cb162
JK
1287 if (skip_prefix(var, "fsck.", &msg_id)) {
1288 if (!value)
1289 return config_error_nonbool(var);
1290 fsck_set_msg_type(options, msg_id, value);
1f3299fd
ÆAB
1291 return 0;
1292 }
1293
a4e7e317 1294 return git_default_config(var, value, ctx, cb);
1f3299fd 1295}
3745e269
ÆAB
1296
1297/*
1298 * Custom error callbacks that are used in more than one place.
1299 */
1300
1301int fsck_error_cb_print_missing_gitmodules(struct fsck_options *o,
1302 const struct object_id *oid,
1303 enum object_type object_type,
1304 enum fsck_msg_type msg_type,
1305 enum fsck_msg_id msg_id,
1306 const char *message)
1307{
1308 if (msg_id == FSCK_MSG_GITMODULES_MISSING) {
1309 puts(oid_to_hex(oid));
1310 return 0;
1311 }
1312 return fsck_error_function(o, oid, object_type, msg_type, msg_id, message);
1313}