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