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