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