]> git.ipfire.org Git - thirdparty/git.git/blame - fsck.c
Git 2.31
[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;
e89f8936
JS
466 else {
467 /* Maybe a non-first parent, e.g. HEAD^2 */
468 generation = 0;
469 name_prefix_len = len;
470 }
7b35efd7
JS
471 }
472 }
473
355885d5 474 while (parents) {
7b35efd7 475 if (name) {
73390290 476 struct object_id *oid = &parents->item->object.oid;
7b35efd7 477
b84c7838 478 if (counter++)
73390290 479 fsck_put_object_name(options, oid, "%s^%d",
a59cfb32 480 name, counter);
7b35efd7 481 else if (generation > 0)
73390290 482 fsck_put_object_name(options, oid, "%.*s~%d",
a59cfb32
JK
483 name_prefix_len, name,
484 generation + 1);
7b35efd7 485 else
73390290 486 fsck_put_object_name(options, oid, "%s^", name);
7b35efd7 487 }
22410549 488 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
355885d5
MK
489 if (result < 0)
490 return result;
491 if (!res)
492 res = result;
493 parents = parents->next;
494 }
495 return res;
496}
497
22410549 498static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
355885d5 499{
73390290 500 const char *name = fsck_get_object_name(options, &tag->object.oid);
7b35efd7 501
355885d5
MK
502 if (parse_tag(tag))
503 return -1;
7b35efd7 504 if (name)
73390290 505 fsck_put_object_name(options, &tag->tagged->oid, "%s", name);
22410549 506 return options->walk(tag->tagged, OBJ_ANY, data, options);
355885d5
MK
507}
508
22410549 509int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
355885d5
MK
510{
511 if (!obj)
512 return -1;
a2b22854
JK
513
514 if (obj->type == OBJ_NONE)
109cd76d 515 parse_object(the_repository, &obj->oid);
a2b22854 516
355885d5
MK
517 switch (obj->type) {
518 case OBJ_BLOB:
519 return 0;
520 case OBJ_TREE:
22410549 521 return fsck_walk_tree((struct tree *)obj, data, options);
355885d5 522 case OBJ_COMMIT:
22410549 523 return fsck_walk_commit((struct commit *)obj, data, options);
355885d5 524 case OBJ_TAG:
22410549 525 return fsck_walk_tag((struct tag *)obj, data, options);
355885d5 526 default:
a59cfb32 527 error("Unknown object type for %s",
73390290 528 fsck_describe_object(options, &obj->oid));
355885d5
MK
529 return -1;
530 }
531}
ba002f3b 532
9068cfb2
RS
533struct name_stack {
534 const char **names;
535 size_t nr, alloc;
536};
537
538static void name_stack_push(struct name_stack *stack, const char *name)
539{
540 ALLOC_GROW(stack->names, stack->nr + 1, stack->alloc);
541 stack->names[stack->nr++] = name;
542}
543
544static const char *name_stack_pop(struct name_stack *stack)
545{
546 return stack->nr ? stack->names[--stack->nr] : NULL;
547}
548
549static void name_stack_clear(struct name_stack *stack)
550{
551 FREE_AND_NULL(stack->names);
552 stack->nr = stack->alloc = 0;
553}
554
ba002f3b
MK
555/*
556 * The entries in a tree are ordered in the _path_ order,
557 * which means that a directory entry is ordered by adding
558 * a slash to the end of it.
559 *
560 * So a directory called "a" is ordered _after_ a file
561 * called "a.c", because "a/" sorts after "a.c".
562 */
563#define TREE_UNORDERED (-1)
564#define TREE_HAS_DUPS (-2)
565
9068cfb2
RS
566static int is_less_than_slash(unsigned char c)
567{
568 return '\0' < c && c < '/';
569}
570
571static int verify_ordered(unsigned mode1, const char *name1,
572 unsigned mode2, const char *name2,
573 struct name_stack *candidates)
ba002f3b
MK
574{
575 int len1 = strlen(name1);
576 int len2 = strlen(name2);
577 int len = len1 < len2 ? len1 : len2;
578 unsigned char c1, c2;
579 int cmp;
580
581 cmp = memcmp(name1, name2, len);
582 if (cmp < 0)
583 return 0;
584 if (cmp > 0)
585 return TREE_UNORDERED;
586
587 /*
588 * Ok, the first <len> characters are the same.
589 * Now we need to order the next one, but turn
590 * a '\0' into a '/' for a directory entry.
591 */
592 c1 = name1[len];
593 c2 = name2[len];
594 if (!c1 && !c2)
595 /*
596 * git-write-tree used to write out a nonsense tree that has
597 * entries with the same name, one blob and one tree. Make
598 * sure we do not have duplicate entries.
599 */
600 return TREE_HAS_DUPS;
601 if (!c1 && S_ISDIR(mode1))
602 c1 = '/';
603 if (!c2 && S_ISDIR(mode2))
604 c2 = '/';
9068cfb2
RS
605
606 /*
607 * There can be non-consecutive duplicates due to the implicitly
86715592 608 * added slash, e.g.:
9068cfb2
RS
609 *
610 * foo
611 * foo.bar
612 * foo.bar.baz
613 * foo.bar/
614 * foo/
615 *
616 * Record non-directory candidates (like "foo" and "foo.bar" in
617 * the example) on a stack and check directory candidates (like
618 * foo/" and "foo.bar/") against that stack.
619 */
620 if (!c1 && is_less_than_slash(c2)) {
621 name_stack_push(candidates, name1);
622 } else if (c2 == '/' && is_less_than_slash(c1)) {
623 for (;;) {
624 const char *p;
625 const char *f_name = name_stack_pop(candidates);
626
627 if (!f_name)
628 break;
629 if (!skip_prefix(name2, f_name, &p))
fe747043 630 continue;
9068cfb2
RS
631 if (!*p)
632 return TREE_HAS_DUPS;
633 if (is_less_than_slash(*p)) {
634 name_stack_push(candidates, f_name);
635 break;
636 }
637 }
638 }
639
ba002f3b
MK
640 return c1 < c2 ? 0 : TREE_UNORDERED;
641}
642
b2f2039c 643static int fsck_tree(const struct object_id *oid,
23a173a7
JK
644 const char *buffer, unsigned long size,
645 struct fsck_options *options)
ba002f3b 646{
8354fa3d 647 int retval = 0;
c479d14a 648 int has_null_sha1 = 0;
ba002f3b
MK
649 int has_full_path = 0;
650 int has_empty_name = 0;
5d34a435
JK
651 int has_dot = 0;
652 int has_dotdot = 0;
5c17f512 653 int has_dotgit = 0;
ba002f3b
MK
654 int has_zero_pad = 0;
655 int has_bad_modes = 0;
656 int has_dup_entries = 0;
657 int not_properly_sorted = 0;
658 struct tree_desc desc;
659 unsigned o_mode;
660 const char *o_name;
9068cfb2 661 struct name_stack df_dup_candidates = { NULL };
ba002f3b 662
23a173a7 663 if (init_tree_desc_gently(&desc, buffer, size)) {
b2f2039c 664 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
8354fa3d
DT
665 return retval;
666 }
ba002f3b
MK
667
668 o_mode = 0;
669 o_name = NULL;
ba002f3b
MK
670
671 while (desc.size) {
5ec1e728 672 unsigned short mode;
288a74bc 673 const char *name, *backslash;
ce6663a9 674 const struct object_id *oid;
ba002f3b 675
ce6663a9 676 oid = tree_entry_extract(&desc, &name, &mode);
ba002f3b 677
ce6663a9 678 has_null_sha1 |= is_null_oid(oid);
effd12ec
HS
679 has_full_path |= !!strchr(name, '/');
680 has_empty_name |= !*name;
681 has_dot |= !strcmp(name, ".");
682 has_dotdot |= !strcmp(name, "..");
ed9c3220 683 has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
ba002f3b 684 has_zero_pad |= *(char *)desc.buffer == '0';
159e7b08 685
b7b1fca1
JK
686 if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
687 if (!S_ISLNK(mode))
688 oidset_insert(&gitmodules_found, oid);
689 else
38370253 690 retval += report(options,
b2f2039c 691 oid, OBJ_TREE,
b7b1fca1
JK
692 FSCK_MSG_GITMODULES_SYMLINK,
693 ".gitmodules is a symbolic link");
694 }
159e7b08 695
288a74bc
JS
696 if ((backslash = strchr(name, '\\'))) {
697 while (backslash) {
698 backslash++;
699 has_dotgit |= is_ntfs_dotgit(backslash);
bdfef049
JS
700 if (is_ntfs_dotgitmodules(backslash)) {
701 if (!S_ISLNK(mode))
702 oidset_insert(&gitmodules_found, oid);
703 else
7034cd09 704 retval += report(options, oid, OBJ_TREE,
bdfef049
JS
705 FSCK_MSG_GITMODULES_SYMLINK,
706 ".gitmodules is a symbolic link");
707 }
288a74bc
JS
708 backslash = strchr(backslash, '\\');
709 }
710 }
711
8354fa3d 712 if (update_tree_entry_gently(&desc)) {
b2f2039c 713 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
8354fa3d
DT
714 break;
715 }
ba002f3b
MK
716
717 switch (mode) {
718 /*
719 * Standard modes..
720 */
721 case S_IFREG | 0755:
722 case S_IFREG | 0644:
723 case S_IFLNK:
724 case S_IFDIR:
725 case S_IFGITLINK:
726 break;
727 /*
728 * This is nonstandard, but we had a few of these
729 * early on when we honored the full set of mode
730 * bits..
731 */
732 case S_IFREG | 0664:
22410549 733 if (!options->strict)
ba002f3b 734 break;
1cf01a34 735 /* fallthrough */
ba002f3b
MK
736 default:
737 has_bad_modes = 1;
738 }
739
740 if (o_name) {
9068cfb2
RS
741 switch (verify_ordered(o_mode, o_name, mode, name,
742 &df_dup_candidates)) {
ba002f3b
MK
743 case TREE_UNORDERED:
744 not_properly_sorted = 1;
745 break;
746 case TREE_HAS_DUPS:
747 has_dup_entries = 1;
748 break;
749 default:
750 break;
751 }
752 }
753
754 o_mode = mode;
755 o_name = name;
ba002f3b
MK
756 }
757
9068cfb2
RS
758 name_stack_clear(&df_dup_candidates);
759
c479d14a 760 if (has_null_sha1)
b2f2039c 761 retval += report(options, oid, OBJ_TREE, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
ba002f3b 762 if (has_full_path)
b2f2039c 763 retval += report(options, oid, OBJ_TREE, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
ba002f3b 764 if (has_empty_name)
b2f2039c 765 retval += report(options, oid, OBJ_TREE, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
5d34a435 766 if (has_dot)
b2f2039c 767 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOT, "contains '.'");
5d34a435 768 if (has_dotdot)
b2f2039c 769 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTDOT, "contains '..'");
5c17f512 770 if (has_dotgit)
b2f2039c 771 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
ba002f3b 772 if (has_zero_pad)
b2f2039c 773 retval += report(options, oid, OBJ_TREE, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
ba002f3b 774 if (has_bad_modes)
b2f2039c 775 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
ba002f3b 776 if (has_dup_entries)
b2f2039c 777 retval += report(options, oid, OBJ_TREE, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries");
ba002f3b 778 if (not_properly_sorted)
b2f2039c 779 retval += report(options, oid, OBJ_TREE, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted");
ba002f3b
MK
780 return retval;
781}
782
84d18c0b 783static int verify_headers(const void *data, unsigned long size,
cc579000
JK
784 const struct object_id *oid, enum object_type type,
785 struct fsck_options *options)
4d0d8975
JS
786{
787 const char *buffer = (const char *)data;
788 unsigned long i;
789
790 for (i = 0; i < size; i++) {
791 switch (buffer[i]) {
792 case '\0':
cc579000 793 return report(options, oid, type,
c99ba492
JS
794 FSCK_MSG_NUL_IN_HEADER,
795 "unterminated header: NUL at offset %ld", i);
4d0d8975
JS
796 case '\n':
797 if (i + 1 < size && buffer[i + 1] == '\n')
798 return 0;
799 }
800 }
801
84d18c0b
JH
802 /*
803 * We did not find double-LF that separates the header
804 * and the body. Not having a body is not a crime but
805 * we do want to see the terminating LF for the last header
806 * line.
807 */
808 if (size && buffer[size - 1] == '\n')
809 return 0;
810
cc579000 811 return report(options, oid, type,
c99ba492 812 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
4d0d8975
JS
813}
814
78543993
JK
815static int fsck_ident(const char **ident,
816 const struct object_id *oid, enum object_type type,
817 struct fsck_options *options)
daae1922 818{
e6826e33 819 const char *p = *ident;
d4b8de04
JK
820 char *end;
821
e6826e33
JS
822 *ident = strchrnul(*ident, '\n');
823 if (**ident == '\n')
824 (*ident)++;
825
826 if (*p == '<')
78543993 827 return report(options, oid, type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
828 p += strcspn(p, "<>\n");
829 if (*p == '>')
78543993 830 return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
e6826e33 831 if (*p != '<')
78543993 832 return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
e6826e33 833 if (p[-1] != ' ')
78543993 834 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
835 p++;
836 p += strcspn(p, "<>\n");
837 if (*p != '>')
78543993 838 return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
e6826e33
JS
839 p++;
840 if (*p != ' ')
78543993 841 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
e6826e33
JS
842 p++;
843 if (*p == '0' && p[1] != ' ')
78543993 844 return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
1aeb7e75 845 if (date_overflows(parse_timestamp(p, &end, 10)))
78543993 846 return report(options, oid, type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
e6826e33 847 if ((end == p || *end != ' '))
78543993 848 return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
e6826e33
JS
849 p = end + 1;
850 if ((*p != '+' && *p != '-') ||
851 !isdigit(p[1]) ||
852 !isdigit(p[2]) ||
853 !isdigit(p[3]) ||
854 !isdigit(p[4]) ||
855 (p[5] != '\n'))
78543993 856 return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
e6826e33 857 p += 6;
daae1922
JN
858 return 0;
859}
860
c5b4269b
JK
861static int fsck_commit(const struct object_id *oid,
862 const char *buffer, unsigned long size,
863 struct fsck_options *options)
ba002f3b 864{
f648ee70 865 struct object_id tree_oid, parent_oid;
ec652315 866 unsigned author_count;
daae1922 867 int err;
6d2d780f 868 const char *buffer_begin = buffer;
c54f5ca9 869 const char *p;
ba002f3b 870
c5b4269b 871 if (verify_headers(buffer, size, oid, OBJ_COMMIT, options))
4d0d8975
JS
872 return -1;
873
cf4fff57 874 if (!skip_prefix(buffer, "tree ", &buffer))
c5b4269b 875 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
c54f5ca9 876 if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
c5b4269b 877 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
b3584761
JS
878 if (err)
879 return err;
880 }
c54f5ca9 881 buffer = p + 1;
cf4fff57 882 while (skip_prefix(buffer, "parent ", &buffer)) {
f648ee70 883 if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') {
c5b4269b 884 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
b3584761
JS
885 if (err)
886 return err;
887 }
c54f5ca9 888 buffer = p + 1;
ba002f3b 889 }
c9ad147f
JS
890 author_count = 0;
891 while (skip_prefix(buffer, "author ", &buffer)) {
892 author_count++;
c5b4269b 893 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
c9ad147f
JS
894 if (err)
895 return err;
ba002f3b 896 }
c9ad147f 897 if (author_count < 1)
c5b4269b 898 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
c9ad147f 899 else if (author_count > 1)
c5b4269b 900 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
daae1922
JN
901 if (err)
902 return err;
cf4fff57 903 if (!skip_prefix(buffer, "committer ", &buffer))
c5b4269b
JK
904 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
905 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
daae1922
JN
906 if (err)
907 return err;
6d2d780f 908 if (memchr(buffer_begin, '\0', size)) {
c5b4269b 909 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT,
6d2d780f
JH
910 "NUL byte in the commit object body");
911 if (err)
912 return err;
913 }
ba002f3b
MK
914 return 0;
915}
916
103fb6d4 917static int fsck_tag(const struct object_id *oid, const char *buffer,
2175a0c6 918 unsigned long size, struct fsck_options *options)
cec097be 919{
f648ee70 920 struct object_id tagged_oid;
acf9de4c
ÆAB
921 int tagged_type;
922 return fsck_tag_standalone(oid, buffer, size, options, &tagged_oid,
923 &tagged_type);
924}
925
926int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
927 unsigned long size, struct fsck_options *options,
928 struct object_id *tagged_oid,
929 int *tagged_type)
930{
cec097be 931 int ret = 0;
23a173a7 932 char *eol;
cec097be 933 struct strbuf sb = STRBUF_INIT;
c54f5ca9 934 const char *p;
cec097be 935
103fb6d4 936 ret = verify_headers(buffer, size, oid, OBJ_TAG, options);
8a272f29 937 if (ret)
cec097be
JS
938 goto done;
939
940 if (!skip_prefix(buffer, "object ", &buffer)) {
103fb6d4 941 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
cec097be
JS
942 goto done;
943 }
acf9de4c 944 if (parse_oid_hex(buffer, tagged_oid, &p) || *p != '\n') {
103fb6d4 945 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
7d7d5b05
JS
946 if (ret)
947 goto done;
cec097be 948 }
c54f5ca9 949 buffer = p + 1;
cec097be
JS
950
951 if (!skip_prefix(buffer, "type ", &buffer)) {
103fb6d4 952 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
cec097be
JS
953 goto done;
954 }
955 eol = strchr(buffer, '\n');
956 if (!eol) {
103fb6d4 957 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
cec097be
JS
958 goto done;
959 }
acf9de4c
ÆAB
960 *tagged_type = type_from_string_gently(buffer, eol - buffer, 1);
961 if (*tagged_type < 0)
103fb6d4 962 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
cec097be
JS
963 if (ret)
964 goto done;
965 buffer = eol + 1;
966
967 if (!skip_prefix(buffer, "tag ", &buffer)) {
103fb6d4 968 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
cec097be
JS
969 goto done;
970 }
971 eol = strchr(buffer, '\n');
972 if (!eol) {
103fb6d4 973 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
cec097be
JS
974 goto done;
975 }
976 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
f27d05b1 977 if (check_refname_format(sb.buf, 0)) {
103fb6d4 978 ret = report(options, oid, OBJ_TAG,
38370253
JK
979 FSCK_MSG_BAD_TAG_NAME,
980 "invalid 'tag' name: %.*s",
981 (int)(eol - buffer), buffer);
f27d05b1
JS
982 if (ret)
983 goto done;
984 }
cec097be
JS
985 buffer = eol + 1;
986
f27d05b1 987 if (!skip_prefix(buffer, "tagger ", &buffer)) {
cec097be 988 /* early tags do not contain 'tagger' lines; warn only */
103fb6d4 989 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
f27d05b1
JS
990 if (ret)
991 goto done;
992 }
cec097be 993 else
103fb6d4 994 ret = fsck_ident(&buffer, oid, OBJ_TAG, options);
9a1a3a4d
ÆAB
995 if (!*buffer)
996 goto done;
cec097be 997
acf9de4c
ÆAB
998 if (!starts_with(buffer, "\n")) {
999 /*
1000 * The verify_headers() check will allow
1001 * e.g. "[...]tagger <tagger>\nsome
1002 * garbage\n\nmessage" to pass, thinking "some
1003 * garbage" could be a custom header. E.g. "mktag"
1004 * doesn't want any unknown headers.
1005 */
1006 ret = report(options, oid, OBJ_TAG, FSCK_MSG_EXTRA_HEADER_ENTRY, "invalid format - extra header(s) after 'tagger'");
1007 if (ret)
1008 goto done;
1009 }
1010
cec097be
JS
1011done:
1012 strbuf_release(&sb);
cec097be
JS
1013 return ret;
1014}
1015
a2b26ffb
JN
1016/*
1017 * Like builtin/submodule--helper.c's starts_with_dot_slash, but without
1018 * relying on the platform-dependent is_dir_sep helper.
1019 *
1020 * This is for use in checking whether a submodule URL is interpreted as
1021 * relative to the current directory on any platform, since \ is a
1022 * directory separator on Windows but not on other platforms.
1023 */
1024static int starts_with_dot_slash(const char *str)
1025{
1026 return str[0] == '.' && (str[1] == '/' || str[1] == '\\');
1027}
1028
1029/*
1030 * Like starts_with_dot_slash, this is a variant of submodule--helper's
1031 * helper of the same name with the twist that it accepts backslash as a
1032 * directory separator even on non-Windows platforms.
1033 */
1034static int starts_with_dot_dot_slash(const char *str)
1035{
1036 return str[0] == '.' && starts_with_dot_slash(str + 1);
1037}
1038
1039static int submodule_url_is_relative(const char *url)
1040{
1041 return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url);
1042}
1043
c44088ec
JN
1044/*
1045 * Count directory components that a relative submodule URL should chop
1046 * from the remote_url it is to be resolved against.
1047 *
1048 * In other words, this counts "../" components at the start of a
1049 * submodule URL.
1050 *
1051 * Returns the number of directory components to chop and writes a
1052 * pointer to the next character of url after all leading "./" and
1053 * "../" components to out.
1054 */
1055static int count_leading_dotdots(const char *url, const char **out)
1056{
1057 int result = 0;
1058 while (1) {
1059 if (starts_with_dot_dot_slash(url)) {
1060 result++;
1061 url += strlen("../");
1062 continue;
1063 }
1064 if (starts_with_dot_slash(url)) {
1065 url += strlen("./");
1066 continue;
1067 }
1068 *out = url;
1069 return result;
1070 }
1071}
a2b26ffb
JN
1072/*
1073 * Check whether a transport is implemented by git-remote-curl.
1074 *
1075 * If it is, returns 1 and writes the URL that would be passed to
1076 * git-remote-curl to the "out" parameter.
1077 *
1078 * Otherwise, returns 0 and leaves "out" untouched.
1079 *
1080 * Examples:
1081 * http::https://example.com/repo.git -> 1, https://example.com/repo.git
1082 * https://example.com/repo.git -> 1, https://example.com/repo.git
1083 * git://example.com/repo.git -> 0
1084 *
1085 * This is for use in checking for previously exploitable bugs that
1086 * required a submodule URL to be passed to git-remote-curl.
1087 */
1088static int url_to_curl_url(const char *url, const char **out)
1089{
1090 /*
1091 * We don't need to check for case-aliases, "http.exe", and so
1092 * on because in the default configuration, is_transport_allowed
1093 * prevents URLs with those schemes from being cloned
1094 * automatically.
1095 */
1096 if (skip_prefix(url, "http::", out) ||
1097 skip_prefix(url, "https::", out) ||
1098 skip_prefix(url, "ftp::", out) ||
1099 skip_prefix(url, "ftps::", out))
1100 return 1;
1101 if (starts_with(url, "http://") ||
1102 starts_with(url, "https://") ||
1103 starts_with(url, "ftp://") ||
1104 starts_with(url, "ftps://")) {
1105 *out = url;
1106 return 1;
1107 }
1108 return 0;
1109}
1110
07259e74
JK
1111static int check_submodule_url(const char *url)
1112{
a2b26ffb 1113 const char *curl_url;
07259e74
JK
1114
1115 if (looks_like_command_line_option(url))
1116 return -1;
1117
6aed5673 1118 if (submodule_url_is_relative(url) || starts_with(url, "git://")) {
c44088ec
JN
1119 char *decoded;
1120 const char *next;
1121 int has_nl;
1122
a2b26ffb
JN
1123 /*
1124 * This could be appended to an http URL and url-decoded;
1125 * check for malicious characters.
1126 */
c44088ec
JN
1127 decoded = url_decode(url);
1128 has_nl = !!strchr(decoded, '\n');
1129
a2b26ffb
JN
1130 free(decoded);
1131 if (has_nl)
1132 return -1;
c44088ec
JN
1133
1134 /*
1135 * URLs which escape their root via "../" can overwrite
1136 * the host field and previous components, resolving to
1a3609e4
JN
1137 * URLs like https::example.com/submodule.git and
1138 * https:///example.com/submodule.git that were
c44088ec
JN
1139 * susceptible to CVE-2020-11008.
1140 */
1141 if (count_leading_dotdots(url, &next) > 0 &&
1a3609e4 1142 (*next == ':' || *next == '/'))
c44088ec 1143 return -1;
a2b26ffb
JN
1144 }
1145
1146 else if (url_to_curl_url(url, &curl_url)) {
1147 struct credential c = CREDENTIAL_INIT;
1a3609e4
JN
1148 int ret = 0;
1149 if (credential_from_url_gently(&c, curl_url, 1) ||
1150 !*c.host)
1151 ret = -1;
a2b26ffb
JN
1152 credential_clear(&c);
1153 return ret;
1154 }
1155
1156 return 0;
07259e74
JK
1157}
1158
ed8b10f6 1159struct fsck_gitmodules_data {
6da40b22 1160 const struct object_id *oid;
ed8b10f6
JK
1161 struct fsck_options *options;
1162 int ret;
1163};
1164
1165static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
1166{
1167 struct fsck_gitmodules_data *data = vdata;
1168 const char *subsection, *key;
f5914f4b 1169 size_t subsection_len;
ed8b10f6
JK
1170 char *name;
1171
1172 if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
1173 !subsection)
1174 return 0;
1175
1176 name = xmemdupz(subsection, subsection_len);
1177 if (check_submodule_name(name) < 0)
38370253 1178 data->ret |= report(data->options,
6da40b22 1179 data->oid, OBJ_BLOB,
ed8b10f6
JK
1180 FSCK_MSG_GITMODULES_NAME,
1181 "disallowed submodule name: %s",
1182 name);
a124133e 1183 if (!strcmp(key, "url") && value &&
07259e74 1184 check_submodule_url(value) < 0)
38370253 1185 data->ret |= report(data->options,
6da40b22 1186 data->oid, OBJ_BLOB,
a124133e
JK
1187 FSCK_MSG_GITMODULES_URL,
1188 "disallowed submodule url: %s",
1189 value);
1a7fd1fb
JK
1190 if (!strcmp(key, "path") && value &&
1191 looks_like_command_line_option(value))
38370253 1192 data->ret |= report(data->options,
6da40b22 1193 data->oid, OBJ_BLOB,
1a7fd1fb
JK
1194 FSCK_MSG_GITMODULES_PATH,
1195 "disallowed submodule path: %s",
1196 value);
bb92255e
JN
1197 if (!strcmp(key, "update") && value &&
1198 parse_submodule_update_type(value) == SM_UPDATE_COMMAND)
7034cd09 1199 data->ret |= report(data->options, data->oid, OBJ_BLOB,
bb92255e
JN
1200 FSCK_MSG_GITMODULES_UPDATE,
1201 "disallowed submodule update setting: %s",
1202 value);
ed8b10f6
JK
1203 free(name);
1204
1205 return 0;
1206}
1207
6da40b22 1208static int fsck_blob(const struct object_id *oid, const char *buf,
7ac4f3a0
JK
1209 unsigned long size, struct fsck_options *options)
1210{
ed8b10f6 1211 struct fsck_gitmodules_data data;
de6bd9e3 1212 struct config_options config_opts = { 0 };
ed8b10f6 1213
6da40b22 1214 if (!oidset_contains(&gitmodules_found, oid))
ed8b10f6 1215 return 0;
6da40b22 1216 oidset_insert(&gitmodules_done, oid);
ed8b10f6 1217
6da40b22 1218 if (object_on_skiplist(options, oid))
fb162877
RJ
1219 return 0;
1220
ed8b10f6
JK
1221 if (!buf) {
1222 /*
1223 * A missing buffer here is a sign that the caller found the
1224 * blob too gigantic to load into memory. Let's just consider
1225 * that an error.
1226 */
6da40b22 1227 return report(options, oid, OBJ_BLOB,
0d68764d 1228 FSCK_MSG_GITMODULES_LARGE,
ed8b10f6
JK
1229 ".gitmodules too large to parse");
1230 }
1231
6da40b22 1232 data.oid = oid;
ed8b10f6
JK
1233 data.options = options;
1234 data.ret = 0;
de6bd9e3 1235 config_opts.error_action = CONFIG_ERROR_SILENT;
ed8b10f6 1236 if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
de6bd9e3 1237 ".gitmodules", buf, size, &data, &config_opts))
6da40b22 1238 data.ret |= report(options, oid, OBJ_BLOB,
ed8b10f6
JK
1239 FSCK_MSG_GITMODULES_PARSE,
1240 "could not parse gitmodules blob");
1241
1242 return data.ret;
7ac4f3a0
JK
1243}
1244
90a398bb 1245int fsck_object(struct object *obj, void *data, unsigned long size,
22410549 1246 struct fsck_options *options)
ba002f3b
MK
1247{
1248 if (!obj)
38370253 1249 return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
ba002f3b
MK
1250
1251 if (obj->type == OBJ_BLOB)
6da40b22 1252 return fsck_blob(&obj->oid, data, size, options);
ba002f3b 1253 if (obj->type == OBJ_TREE)
b2f2039c 1254 return fsck_tree(&obj->oid, data, size, options);
ba002f3b 1255 if (obj->type == OBJ_COMMIT)
c5b4269b 1256 return fsck_commit(&obj->oid, data, size, options);
ba002f3b 1257 if (obj->type == OBJ_TAG)
103fb6d4 1258 return fsck_tag(&obj->oid, data, size, options);
ba002f3b 1259
38370253
JK
1260 return report(options, &obj->oid, obj->type,
1261 FSCK_MSG_UNKNOWN_TYPE,
1262 "unknown type '%d' (internal fsck error)",
1263 obj->type);
ba002f3b 1264}
d6ffc8d7 1265
1cd772cc 1266int fsck_error_function(struct fsck_options *o,
5afc4b1d
JK
1267 const struct object_id *oid,
1268 enum object_type object_type,
1269 int msg_type, const char *message)
d6ffc8d7 1270{
0282f4dc 1271 if (msg_type == FSCK_WARN) {
5afc4b1d 1272 warning("object %s: %s", fsck_describe_object(o, oid), message);
0282f4dc
JS
1273 return 0;
1274 }
5afc4b1d 1275 error("object %s: %s", fsck_describe_object(o, oid), message);
d6ffc8d7
MK
1276 return 1;
1277}
159e7b08 1278
5476e1ef
JT
1279void register_found_gitmodules(const struct object_id *oid)
1280{
1281 oidset_insert(&gitmodules_found, oid);
1282}
1283
159e7b08
JK
1284int fsck_finish(struct fsck_options *options)
1285{
1286 int ret = 0;
1287 struct oidset_iter iter;
1288 const struct object_id *oid;
1289
1290 oidset_iter_init(&gitmodules_found, &iter);
1291 while ((oid = oidset_iter_next(&iter))) {
159e7b08
JK
1292 enum object_type type;
1293 unsigned long size;
1294 char *buf;
1295
1296 if (oidset_contains(&gitmodules_done, oid))
1297 continue;
1298
7913f53b 1299 buf = read_object_file(oid, &type, &size);
159e7b08 1300 if (!buf) {
b8b00f16 1301 if (is_promisor_object(oid))
27387444 1302 continue;
38370253 1303 ret |= report(options,
b8b00f16 1304 oid, OBJ_BLOB,
159e7b08
JK
1305 FSCK_MSG_GITMODULES_MISSING,
1306 "unable to read .gitmodules blob");
1307 continue;
1308 }
1309
1310 if (type == OBJ_BLOB)
b8b00f16 1311 ret |= fsck_blob(oid, buf, size, options);
159e7b08 1312 else
38370253 1313 ret |= report(options,
b8b00f16 1314 oid, type,
159e7b08
JK
1315 FSCK_MSG_GITMODULES_BLOB,
1316 "non-blob found at .gitmodules");
1317 free(buf);
1318 }
1319
1320
1321 oidset_clear(&gitmodules_found);
1322 oidset_clear(&gitmodules_done);
1323 return ret;
1324}
1f3299fd
ÆAB
1325
1326int fsck_config_internal(const char *var, const char *value, void *cb,
1327 struct fsck_options *options)
1328{
1329 if (strcmp(var, "fsck.skiplist") == 0) {
1330 const char *path;
1331 struct strbuf sb = STRBUF_INIT;
1332
1333 if (git_config_pathname(&path, var, value))
1334 return 1;
1335 strbuf_addf(&sb, "skiplist=%s", path);
1336 free((char *)path);
1337 fsck_set_msg_types(options, sb.buf);
1338 strbuf_release(&sb);
1339 return 0;
1340 }
1341
1342 if (skip_prefix(var, "fsck.", &var)) {
1343 fsck_set_msg_type(options, var, value);
1344 return 0;
1345 }
1346
1347 return git_default_config(var, value, cb);
1348}