]> git.ipfire.org Git - thirdparty/git.git/blame - fsck.c
fsck: don't require an object struct in verify_headers()
[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"
a18fcc9f 12#include "utf8.h"
7b35efd7 13#include "decorate.h"
159e7b08 14#include "oidset.h"
27387444 15#include "packfile.h"
ed8b10f6
JK
16#include "submodule-config.h"
17#include "config.h"
3ac68a93 18#include "help.h"
159e7b08
JK
19
20static struct oidset gitmodules_found = OIDSET_INIT;
21static struct oidset gitmodules_done = OIDSET_INIT;
355885d5 22
f50c4407 23#define FSCK_FATAL -1
f27d05b1 24#define FSCK_INFO -2
f50c4407 25
c99ba492 26#define FOREACH_MSG_ID(FUNC) \
f50c4407
JS
27 /* fatal errors */ \
28 FUNC(NUL_IN_HEADER, FATAL) \
29 FUNC(UNTERMINATED_HEADER, FATAL) \
c99ba492
JS
30 /* errors */ \
31 FUNC(BAD_DATE, ERROR) \
32 FUNC(BAD_DATE_OVERFLOW, ERROR) \
33 FUNC(BAD_EMAIL, ERROR) \
34 FUNC(BAD_NAME, ERROR) \
35 FUNC(BAD_OBJECT_SHA1, ERROR) \
36 FUNC(BAD_PARENT_SHA1, ERROR) \
37 FUNC(BAD_TAG_OBJECT, ERROR) \
38 FUNC(BAD_TIMEZONE, ERROR) \
39 FUNC(BAD_TREE, ERROR) \
40 FUNC(BAD_TREE_SHA1, ERROR) \
41 FUNC(BAD_TYPE, ERROR) \
42 FUNC(DUPLICATE_ENTRIES, ERROR) \
43 FUNC(MISSING_AUTHOR, ERROR) \
44 FUNC(MISSING_COMMITTER, ERROR) \
45 FUNC(MISSING_EMAIL, ERROR) \
c99ba492
JS
46 FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
47 FUNC(MISSING_OBJECT, ERROR) \
c99ba492
JS
48 FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \
49 FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \
50 FUNC(MISSING_TAG, ERROR) \
51 FUNC(MISSING_TAG_ENTRY, ERROR) \
c99ba492 52 FUNC(MISSING_TREE, ERROR) \
159e7b08 53 FUNC(MISSING_TREE_OBJECT, ERROR) \
c99ba492
JS
54 FUNC(MISSING_TYPE, ERROR) \
55 FUNC(MISSING_TYPE_ENTRY, ERROR) \
c9ad147f 56 FUNC(MULTIPLE_AUTHORS, ERROR) \
c99ba492
JS
57 FUNC(TREE_NOT_SORTED, ERROR) \
58 FUNC(UNKNOWN_TYPE, ERROR) \
c99ba492 59 FUNC(ZERO_PADDED_DATE, ERROR) \
159e7b08
JK
60 FUNC(GITMODULES_MISSING, ERROR) \
61 FUNC(GITMODULES_BLOB, ERROR) \
0d68764d 62 FUNC(GITMODULES_LARGE, ERROR) \
ed8b10f6 63 FUNC(GITMODULES_NAME, ERROR) \
b7b1fca1 64 FUNC(GITMODULES_SYMLINK, ERROR) \
a124133e 65 FUNC(GITMODULES_URL, ERROR) \
1a7fd1fb 66 FUNC(GITMODULES_PATH, 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
JS
79 FUNC(BAD_TAG_NAME, INFO) \
80 FUNC(MISSING_TAGGER_ENTRY, INFO)
c99ba492
JS
81
82#define MSG_ID(id, msg_type) FSCK_MSG_##id,
83enum fsck_msg_id {
84 FOREACH_MSG_ID(MSG_ID)
85 FSCK_MSG_MAX
86};
87#undef MSG_ID
88
f417eed8 89#define STR(x) #x
a4a9cc19 90#define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
c99ba492 91static struct {
f417eed8
JS
92 const char *id_string;
93 const char *downcased;
a4a9cc19 94 const char *camelcased;
c99ba492
JS
95 int msg_type;
96} msg_id_info[FSCK_MSG_MAX + 1] = {
97 FOREACH_MSG_ID(MSG_ID)
a4a9cc19 98 { NULL, NULL, NULL, -1 }
c99ba492
JS
99};
100#undef MSG_ID
101
a46baac6 102static void prepare_msg_ids(void)
f417eed8
JS
103{
104 int i;
105
a46baac6
NTND
106 if (msg_id_info[0].downcased)
107 return;
108
109 /* convert id_string to lower case, without underscores. */
110 for (i = 0; i < FSCK_MSG_MAX; i++) {
111 const char *p = msg_id_info[i].id_string;
112 int len = strlen(p);
113 char *q = xmalloc(len);
114
115 msg_id_info[i].downcased = q;
116 while (*p)
117 if (*p == '_')
118 p++;
119 else
120 *(q)++ = tolower(*(p)++);
121 *q = '\0';
a4a9cc19
NTND
122
123 p = msg_id_info[i].id_string;
124 q = xmalloc(len);
125 msg_id_info[i].camelcased = q;
126 while (*p) {
127 if (*p == '_') {
128 p++;
129 if (*p)
130 *q++ = *p++;
131 } else {
132 *q++ = tolower(*p++);
133 }
f417eed8 134 }
a4a9cc19 135 *q = '\0';
f417eed8 136 }
a46baac6
NTND
137}
138
139static int parse_msg_id(const char *text)
140{
141 int i;
142
143 prepare_msg_ids();
f417eed8
JS
144
145 for (i = 0; i < FSCK_MSG_MAX; i++)
146 if (!strcmp(text, msg_id_info[i].downcased))
147 return i;
148
149 return -1;
150}
151
3ac68a93
NTND
152void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
153{
154 int i;
155
156 prepare_msg_ids();
157
3ac68a93 158 for (i = 0; i < FSCK_MSG_MAX; i++)
a4a9cc19 159 list_config_item(list, prefix, msg_id_info[i].camelcased);
3ac68a93
NTND
160}
161
c99ba492
JS
162static int fsck_msg_type(enum fsck_msg_id msg_id,
163 struct fsck_options *options)
164{
165 int msg_type;
166
0282f4dc
JS
167 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
168
169 if (options->msg_type)
170 msg_type = options->msg_type[msg_id];
171 else {
172 msg_type = msg_id_info[msg_id].msg_type;
173 if (options->strict && msg_type == FSCK_WARN)
174 msg_type = FSCK_ERROR;
175 }
c99ba492
JS
176
177 return msg_type;
178}
179
0282f4dc
JS
180static int parse_msg_type(const char *str)
181{
182 if (!strcmp(str, "error"))
183 return FSCK_ERROR;
184 else if (!strcmp(str, "warn"))
185 return FSCK_WARN;
efaba7cc
JS
186 else if (!strcmp(str, "ignore"))
187 return FSCK_IGNORE;
0282f4dc
JS
188 else
189 die("Unknown fsck message type: '%s'", str);
190}
191
5d477a33
JS
192int is_valid_msg_type(const char *msg_id, const char *msg_type)
193{
194 if (parse_msg_id(msg_id) < 0)
195 return 0;
196 parse_msg_type(msg_type);
197 return 1;
198}
199
0282f4dc
JS
200void fsck_set_msg_type(struct fsck_options *options,
201 const char *msg_id, const char *msg_type)
202{
203 int id = parse_msg_id(msg_id), type;
204
205 if (id < 0)
206 die("Unhandled message id: %s", msg_id);
207 type = parse_msg_type(msg_type);
208
f50c4407
JS
209 if (type != FSCK_ERROR && msg_id_info[id].msg_type == FSCK_FATAL)
210 die("Cannot demote %s to %s", msg_id, msg_type);
211
0282f4dc
JS
212 if (!options->msg_type) {
213 int i;
b32fa95f
JK
214 int *msg_type;
215 ALLOC_ARRAY(msg_type, FSCK_MSG_MAX);
0282f4dc
JS
216 for (i = 0; i < FSCK_MSG_MAX; i++)
217 msg_type[i] = fsck_msg_type(i, options);
218 options->msg_type = msg_type;
219 }
220
221 options->msg_type[id] = type;
222}
223
224void fsck_set_msg_types(struct fsck_options *options, const char *values)
225{
226 char *buf = xstrdup(values), *to_free = buf;
227 int done = 0;
228
229 while (!done) {
230 int len = strcspn(buf, " ,|"), equal;
231
232 done = !buf[len];
233 if (!len) {
234 buf++;
235 continue;
236 }
237 buf[len] = '\0';
238
239 for (equal = 0;
240 equal < len && buf[equal] != '=' && buf[equal] != ':';
241 equal++)
242 buf[equal] = tolower(buf[equal]);
243 buf[equal] = '\0';
244
cd94c6f9
JS
245 if (!strcmp(buf, "skiplist")) {
246 if (equal == len)
247 die("skiplist requires a path");
24eb33eb 248 oidset_parse_file(&options->skiplist, buf + equal + 1);
cd94c6f9
JS
249 buf += len + 1;
250 continue;
251 }
252
0282f4dc
JS
253 if (equal == len)
254 die("Missing '=': '%s'", buf);
355885d5 255
0282f4dc
JS
256 fsck_set_msg_type(options, buf, buf + equal + 1);
257 buf += len + 1;
258 }
259 free(to_free);
260}
261
71ab8fa8
JS
262static void append_msg_id(struct strbuf *sb, const char *msg_id)
263{
264 for (;;) {
265 char c = *(msg_id)++;
266
267 if (!c)
268 break;
269 if (c != '_')
270 strbuf_addch(sb, tolower(c));
271 else {
272 assert(*msg_id);
273 strbuf_addch(sb, *(msg_id)++);
274 }
275 }
276
277 strbuf_addstr(sb, ": ");
278}
279
f5979376
JK
280static int object_on_skiplist(struct fsck_options *opts,
281 const struct object_id *oid)
fb162877 282{
f5979376 283 return opts && oid && oidset_contains(&opts->skiplist, oid);
fb162877
RJ
284}
285
38370253
JK
286__attribute__((format (printf, 5, 6)))
287static int report(struct fsck_options *options,
288 const struct object_id *oid, enum object_type object_type,
289 enum fsck_msg_id id, const char *fmt, ...)
c99ba492
JS
290{
291 va_list ap;
292 struct strbuf sb = STRBUF_INIT;
293 int msg_type = fsck_msg_type(id, options), result;
294
efaba7cc
JS
295 if (msg_type == FSCK_IGNORE)
296 return 0;
297
38370253 298 if (object_on_skiplist(options, oid))
cd94c6f9
JS
299 return 0;
300
f50c4407
JS
301 if (msg_type == FSCK_FATAL)
302 msg_type = FSCK_ERROR;
f27d05b1
JS
303 else if (msg_type == FSCK_INFO)
304 msg_type = FSCK_WARN;
f50c4407 305
71ab8fa8
JS
306 append_msg_id(&sb, msg_id_info[id].id_string);
307
c99ba492
JS
308 va_start(ap, fmt);
309 strbuf_vaddf(&sb, fmt, ap);
38370253 310 result = options->error_func(options, oid, object_type,
5afc4b1d 311 msg_type, sb.buf);
c99ba492
JS
312 strbuf_release(&sb);
313 va_end(ap);
314
315 return result;
316}
317
a59cfb32
JK
318void fsck_enable_object_names(struct fsck_options *options)
319{
320 if (!options->object_names)
73390290 321 options->object_names = kh_init_oid_map();
a59cfb32
JK
322}
323
73390290
JK
324const char *fsck_get_object_name(struct fsck_options *options,
325 const struct object_id *oid)
7b35efd7 326{
73390290 327 khiter_t pos;
7b35efd7
JS
328 if (!options->object_names)
329 return NULL;
73390290
JK
330 pos = kh_get_oid_map(options->object_names, *oid);
331 if (pos >= kh_end(options->object_names))
332 return NULL;
333 return kh_value(options->object_names, pos);
7b35efd7
JS
334}
335
73390290
JK
336void fsck_put_object_name(struct fsck_options *options,
337 const struct object_id *oid,
a59cfb32 338 const char *fmt, ...)
7b35efd7
JS
339{
340 va_list ap;
341 struct strbuf buf = STRBUF_INIT;
73390290
JK
342 khiter_t pos;
343 int hashret;
7b35efd7
JS
344
345 if (!options->object_names)
346 return;
73390290
JK
347
348 pos = kh_put_oid_map(options->object_names, *oid, &hashret);
349 if (!hashret)
7b35efd7
JS
350 return;
351 va_start(ap, fmt);
352 strbuf_vaddf(&buf, fmt, ap);
73390290 353 kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL);
7b35efd7
JS
354 va_end(ap);
355}
356
a59cfb32 357const char *fsck_describe_object(struct fsck_options *options,
73390290 358 const struct object_id *oid)
90cf590f 359{
a59cfb32
JK
360 static struct strbuf bufs[] = {
361 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
362 };
363 static int b = 0;
364 struct strbuf *buf;
73390290 365 const char *name = fsck_get_object_name(options, oid);
a59cfb32
JK
366
367 buf = bufs + b;
368 b = (b + 1) % ARRAY_SIZE(bufs);
369 strbuf_reset(buf);
73390290 370 strbuf_addstr(buf, oid_to_hex(oid));
a59cfb32
JK
371 if (name)
372 strbuf_addf(buf, " (%s)", name);
90cf590f 373
a59cfb32 374 return buf->buf;
90cf590f
JS
375}
376
22410549 377static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
355885d5
MK
378{
379 struct tree_desc desc;
380 struct name_entry entry;
381 int res = 0;
7b35efd7 382 const char *name;
355885d5
MK
383
384 if (parse_tree(tree))
385 return -1;
386
73390290 387 name = fsck_get_object_name(options, &tree->object.oid);
8354fa3d
DT
388 if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
389 return -1;
390 while (tree_entry_gently(&desc, &entry)) {
7b35efd7 391 struct object *obj;
355885d5
MK
392 int result;
393
394 if (S_ISGITLINK(entry.mode))
395 continue;
7b35efd7
JS
396
397 if (S_ISDIR(entry.mode)) {
ea82b2a0 398 obj = (struct object *)lookup_tree(the_repository, &entry.oid);
2720f6db 399 if (name && obj)
73390290 400 fsck_put_object_name(options, &entry.oid, "%s%s/",
a59cfb32 401 name, entry.path);
7b35efd7
JS
402 result = options->walk(obj, OBJ_TREE, data, options);
403 }
404 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
ea82b2a0 405 obj = (struct object *)lookup_blob(the_repository, &entry.oid);
2720f6db 406 if (name && obj)
73390290 407 fsck_put_object_name(options, &entry.oid, "%s%s",
a59cfb32 408 name, entry.path);
7b35efd7
JS
409 result = options->walk(obj, OBJ_BLOB, data, options);
410 }
355885d5 411 else {
82247e9b 412 result = error("in tree %s: entry %s has bad mode %.6o",
73390290 413 fsck_describe_object(options, &tree->object.oid),
a59cfb32 414 entry.path, entry.mode);
355885d5
MK
415 }
416 if (result < 0)
417 return result;
418 if (!res)
419 res = result;
420 }
421 return res;
422}
423
22410549 424static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
355885d5 425{
7b35efd7 426 int counter = 0, generation = 0, name_prefix_len = 0;
355885d5
MK
427 struct commit_list *parents;
428 int res;
429 int result;
7b35efd7 430 const char *name;
355885d5
MK
431
432 if (parse_commit(commit))
433 return -1;
434
73390290 435 name = fsck_get_object_name(options, &commit->object.oid);
7b35efd7 436 if (name)
73390290 437 fsck_put_object_name(options, get_commit_tree_oid(commit),
a59cfb32 438 "%s:", name);
7b35efd7 439
2e27bd77
DS
440 result = options->walk((struct object *)get_commit_tree(commit),
441 OBJ_TREE, data, options);
355885d5
MK
442 if (result < 0)
443 return result;
444 res = result;
445
446 parents = commit->parents;
7b35efd7
JS
447 if (name && parents) {
448 int len = strlen(name), power;
449
450 if (len && name[len - 1] == '^') {
451 generation = 1;
452 name_prefix_len = len - 1;
453 }
454 else { /* parse ~<generation> suffix */
455 for (generation = 0, power = 1;
456 len && isdigit(name[len - 1]);
457 power *= 10)
458 generation += power * (name[--len] - '0');
459 if (power > 1 && len && name[len - 1] == '~')
460 name_prefix_len = len - 1;
461 }
462 }
463
355885d5 464 while (parents) {
7b35efd7 465 if (name) {
73390290 466 struct object_id *oid = &parents->item->object.oid;
7b35efd7 467
b84c7838 468 if (counter++)
73390290 469 fsck_put_object_name(options, oid, "%s^%d",
a59cfb32 470 name, counter);
7b35efd7 471 else if (generation > 0)
73390290 472 fsck_put_object_name(options, oid, "%.*s~%d",
a59cfb32
JK
473 name_prefix_len, name,
474 generation + 1);
7b35efd7 475 else
73390290 476 fsck_put_object_name(options, oid, "%s^", name);
7b35efd7 477 }
22410549 478 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
355885d5
MK
479 if (result < 0)
480 return result;
481 if (!res)
482 res = result;
483 parents = parents->next;
484 }
485 return res;
486}
487
22410549 488static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
355885d5 489{
73390290 490 const char *name = fsck_get_object_name(options, &tag->object.oid);
7b35efd7 491
355885d5
MK
492 if (parse_tag(tag))
493 return -1;
7b35efd7 494 if (name)
73390290 495 fsck_put_object_name(options, &tag->tagged->oid, "%s", name);
22410549 496 return options->walk(tag->tagged, OBJ_ANY, data, options);
355885d5
MK
497}
498
22410549 499int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
355885d5
MK
500{
501 if (!obj)
502 return -1;
a2b22854
JK
503
504 if (obj->type == OBJ_NONE)
109cd76d 505 parse_object(the_repository, &obj->oid);
a2b22854 506
355885d5
MK
507 switch (obj->type) {
508 case OBJ_BLOB:
509 return 0;
510 case OBJ_TREE:
22410549 511 return fsck_walk_tree((struct tree *)obj, data, options);
355885d5 512 case OBJ_COMMIT:
22410549 513 return fsck_walk_commit((struct commit *)obj, data, options);
355885d5 514 case OBJ_TAG:
22410549 515 return fsck_walk_tag((struct tag *)obj, data, options);
355885d5 516 default:
a59cfb32 517 error("Unknown object type for %s",
73390290 518 fsck_describe_object(options, &obj->oid));
355885d5
MK
519 return -1;
520 }
521}
ba002f3b
MK
522
523/*
524 * The entries in a tree are ordered in the _path_ order,
525 * which means that a directory entry is ordered by adding
526 * a slash to the end of it.
527 *
528 * So a directory called "a" is ordered _after_ a file
529 * called "a.c", because "a/" sorts after "a.c".
530 */
531#define TREE_UNORDERED (-1)
532#define TREE_HAS_DUPS (-2)
533
534static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
535{
536 int len1 = strlen(name1);
537 int len2 = strlen(name2);
538 int len = len1 < len2 ? len1 : len2;
539 unsigned char c1, c2;
540 int cmp;
541
542 cmp = memcmp(name1, name2, len);
543 if (cmp < 0)
544 return 0;
545 if (cmp > 0)
546 return TREE_UNORDERED;
547
548 /*
549 * Ok, the first <len> characters are the same.
550 * Now we need to order the next one, but turn
551 * a '\0' into a '/' for a directory entry.
552 */
553 c1 = name1[len];
554 c2 = name2[len];
555 if (!c1 && !c2)
556 /*
557 * git-write-tree used to write out a nonsense tree that has
558 * entries with the same name, one blob and one tree. Make
559 * sure we do not have duplicate entries.
560 */
561 return TREE_HAS_DUPS;
562 if (!c1 && S_ISDIR(mode1))
563 c1 = '/';
564 if (!c2 && S_ISDIR(mode2))
565 c2 = '/';
566 return c1 < c2 ? 0 : TREE_UNORDERED;
567}
568
23a173a7
JK
569static int fsck_tree(struct tree *item,
570 const char *buffer, unsigned long size,
571 struct fsck_options *options)
ba002f3b 572{
8354fa3d 573 int retval = 0;
c479d14a 574 int has_null_sha1 = 0;
ba002f3b
MK
575 int has_full_path = 0;
576 int has_empty_name = 0;
5d34a435
JK
577 int has_dot = 0;
578 int has_dotdot = 0;
5c17f512 579 int has_dotgit = 0;
ba002f3b
MK
580 int has_zero_pad = 0;
581 int has_bad_modes = 0;
582 int has_dup_entries = 0;
583 int not_properly_sorted = 0;
584 struct tree_desc desc;
585 unsigned o_mode;
586 const char *o_name;
ba002f3b 587
23a173a7 588 if (init_tree_desc_gently(&desc, buffer, size)) {
38370253 589 retval += report(options, &item->object.oid, item->object.type, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
8354fa3d
DT
590 return retval;
591 }
ba002f3b
MK
592
593 o_mode = 0;
594 o_name = NULL;
ba002f3b
MK
595
596 while (desc.size) {
5ec1e728 597 unsigned short mode;
ba002f3b 598 const char *name;
ce6663a9 599 const struct object_id *oid;
ba002f3b 600
ce6663a9 601 oid = tree_entry_extract(&desc, &name, &mode);
ba002f3b 602
ce6663a9 603 has_null_sha1 |= is_null_oid(oid);
effd12ec
HS
604 has_full_path |= !!strchr(name, '/');
605 has_empty_name |= !*name;
606 has_dot |= !strcmp(name, ".");
607 has_dotdot |= !strcmp(name, "..");
ed9c3220 608 has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
ba002f3b 609 has_zero_pad |= *(char *)desc.buffer == '0';
159e7b08 610
b7b1fca1
JK
611 if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
612 if (!S_ISLNK(mode))
613 oidset_insert(&gitmodules_found, oid);
614 else
38370253
JK
615 retval += report(options,
616 &item->object.oid, item->object.type,
b7b1fca1
JK
617 FSCK_MSG_GITMODULES_SYMLINK,
618 ".gitmodules is a symbolic link");
619 }
159e7b08 620
8354fa3d 621 if (update_tree_entry_gently(&desc)) {
38370253 622 retval += report(options, &item->object.oid, item->object.type, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
8354fa3d
DT
623 break;
624 }
ba002f3b
MK
625
626 switch (mode) {
627 /*
628 * Standard modes..
629 */
630 case S_IFREG | 0755:
631 case S_IFREG | 0644:
632 case S_IFLNK:
633 case S_IFDIR:
634 case S_IFGITLINK:
635 break;
636 /*
637 * This is nonstandard, but we had a few of these
638 * early on when we honored the full set of mode
639 * bits..
640 */
641 case S_IFREG | 0664:
22410549 642 if (!options->strict)
ba002f3b 643 break;
1cf01a34 644 /* fallthrough */
ba002f3b
MK
645 default:
646 has_bad_modes = 1;
647 }
648
649 if (o_name) {
650 switch (verify_ordered(o_mode, o_name, mode, name)) {
651 case TREE_UNORDERED:
652 not_properly_sorted = 1;
653 break;
654 case TREE_HAS_DUPS:
655 has_dup_entries = 1;
656 break;
657 default:
658 break;
659 }
660 }
661
662 o_mode = mode;
663 o_name = name;
ba002f3b
MK
664 }
665
c479d14a 666 if (has_null_sha1)
38370253 667 retval += report(options, &item->object.oid, item->object.type, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
ba002f3b 668 if (has_full_path)
38370253 669 retval += report(options, &item->object.oid, item->object.type, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
ba002f3b 670 if (has_empty_name)
38370253 671 retval += report(options, &item->object.oid, item->object.type, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
5d34a435 672 if (has_dot)
38370253 673 retval += report(options, &item->object.oid, item->object.type, FSCK_MSG_HAS_DOT, "contains '.'");
5d34a435 674 if (has_dotdot)
38370253 675 retval += report(options, &item->object.oid, item->object.type, FSCK_MSG_HAS_DOTDOT, "contains '..'");
5c17f512 676 if (has_dotgit)
38370253 677 retval += report(options, &item->object.oid, item->object.type, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
ba002f3b 678 if (has_zero_pad)
38370253 679 retval += report(options, &item->object.oid, item->object.type, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
ba002f3b 680 if (has_bad_modes)
38370253 681 retval += report(options, &item->object.oid, item->object.type, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
ba002f3b 682 if (has_dup_entries)
38370253 683 retval += report(options, &item->object.oid, item->object.type, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries");
ba002f3b 684 if (not_properly_sorted)
38370253 685 retval += report(options, &item->object.oid, item->object.type, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted");
ba002f3b
MK
686 return retval;
687}
688
84d18c0b 689static int verify_headers(const void *data, unsigned long size,
cc579000
JK
690 const struct object_id *oid, enum object_type type,
691 struct fsck_options *options)
4d0d8975
JS
692{
693 const char *buffer = (const char *)data;
694 unsigned long i;
695
696 for (i = 0; i < size; i++) {
697 switch (buffer[i]) {
698 case '\0':
cc579000 699 return report(options, oid, type,
c99ba492
JS
700 FSCK_MSG_NUL_IN_HEADER,
701 "unterminated header: NUL at offset %ld", i);
4d0d8975
JS
702 case '\n':
703 if (i + 1 < size && buffer[i + 1] == '\n')
704 return 0;
705 }
706 }
707
84d18c0b
JH
708 /*
709 * We did not find double-LF that separates the header
710 * and the body. Not having a body is not a crime but
711 * we do want to see the terminating LF for the last header
712 * line.
713 */
714 if (size && buffer[size - 1] == '\n')
715 return 0;
716
cc579000 717 return report(options, oid, type,
c99ba492 718 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
4d0d8975
JS
719}
720
78543993
JK
721static int fsck_ident(const char **ident,
722 const struct object_id *oid, enum object_type type,
723 struct fsck_options *options)
daae1922 724{
e6826e33 725 const char *p = *ident;
d4b8de04
JK
726 char *end;
727
e6826e33
JS
728 *ident = strchrnul(*ident, '\n');
729 if (**ident == '\n')
730 (*ident)++;
731
732 if (*p == '<')
78543993 733 return report(options, oid, type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
734 p += strcspn(p, "<>\n");
735 if (*p == '>')
78543993 736 return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
e6826e33 737 if (*p != '<')
78543993 738 return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
e6826e33 739 if (p[-1] != ' ')
78543993 740 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
741 p++;
742 p += strcspn(p, "<>\n");
743 if (*p != '>')
78543993 744 return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
e6826e33
JS
745 p++;
746 if (*p != ' ')
78543993 747 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
e6826e33
JS
748 p++;
749 if (*p == '0' && p[1] != ' ')
78543993 750 return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
1aeb7e75 751 if (date_overflows(parse_timestamp(p, &end, 10)))
78543993 752 return report(options, oid, type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
e6826e33 753 if ((end == p || *end != ' '))
78543993 754 return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
e6826e33
JS
755 p = end + 1;
756 if ((*p != '+' && *p != '-') ||
757 !isdigit(p[1]) ||
758 !isdigit(p[2]) ||
759 !isdigit(p[3]) ||
760 !isdigit(p[4]) ||
761 (p[5] != '\n'))
78543993 762 return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
e6826e33 763 p += 6;
daae1922
JN
764 return 0;
765}
766
23a173a7
JK
767static int fsck_commit(struct commit *commit, const char *buffer,
768 unsigned long size, struct fsck_options *options)
ba002f3b 769{
c54f5ca9 770 struct object_id tree_oid, oid;
ec652315 771 unsigned author_count;
daae1922 772 int err;
6d2d780f 773 const char *buffer_begin = buffer;
c54f5ca9 774 const char *p;
ba002f3b 775
cc579000
JK
776 if (verify_headers(buffer, size, &commit->object.oid,
777 commit->object.type, options))
4d0d8975
JS
778 return -1;
779
cf4fff57 780 if (!skip_prefix(buffer, "tree ", &buffer))
38370253 781 return report(options, &commit->object.oid, commit->object.type, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
c54f5ca9 782 if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
38370253 783 err = report(options, &commit->object.oid, commit->object.type, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
b3584761
JS
784 if (err)
785 return err;
786 }
c54f5ca9 787 buffer = p + 1;
cf4fff57 788 while (skip_prefix(buffer, "parent ", &buffer)) {
c54f5ca9 789 if (parse_oid_hex(buffer, &oid, &p) || *p != '\n') {
38370253 790 err = report(options, &commit->object.oid, commit->object.type, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
b3584761
JS
791 if (err)
792 return err;
793 }
c54f5ca9 794 buffer = p + 1;
ba002f3b 795 }
c9ad147f
JS
796 author_count = 0;
797 while (skip_prefix(buffer, "author ", &buffer)) {
798 author_count++;
78543993 799 err = fsck_ident(&buffer, &commit->object.oid, commit->object.type, options);
c9ad147f
JS
800 if (err)
801 return err;
ba002f3b 802 }
c9ad147f 803 if (author_count < 1)
38370253 804 err = report(options, &commit->object.oid, commit->object.type, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
c9ad147f 805 else if (author_count > 1)
38370253 806 err = report(options, &commit->object.oid, commit->object.type, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
daae1922
JN
807 if (err)
808 return err;
cf4fff57 809 if (!skip_prefix(buffer, "committer ", &buffer))
38370253 810 return report(options, &commit->object.oid, commit->object.type, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
78543993 811 err = fsck_ident(&buffer, &commit->object.oid, commit->object.type, options);
daae1922
JN
812 if (err)
813 return err;
6d2d780f 814 if (memchr(buffer_begin, '\0', size)) {
38370253 815 err = report(options, &commit->object.oid, commit->object.type, FSCK_MSG_NUL_IN_COMMIT,
6d2d780f
JH
816 "NUL byte in the commit object body");
817 if (err)
818 return err;
819 }
ba002f3b
MK
820 return 0;
821}
822
23a173a7 823static int fsck_tag(struct tag *tag, const char *buffer,
2175a0c6 824 unsigned long size, struct fsck_options *options)
cec097be 825{
c54f5ca9 826 struct object_id oid;
cec097be 827 int ret = 0;
23a173a7 828 char *eol;
cec097be 829 struct strbuf sb = STRBUF_INIT;
c54f5ca9 830 const char *p;
cec097be 831
cc579000 832 ret = verify_headers(buffer, size, &tag->object.oid, tag->object.type, options);
8a272f29 833 if (ret)
cec097be
JS
834 goto done;
835
836 if (!skip_prefix(buffer, "object ", &buffer)) {
38370253 837 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
cec097be
JS
838 goto done;
839 }
c54f5ca9 840 if (parse_oid_hex(buffer, &oid, &p) || *p != '\n') {
38370253 841 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
7d7d5b05
JS
842 if (ret)
843 goto done;
cec097be 844 }
c54f5ca9 845 buffer = p + 1;
cec097be
JS
846
847 if (!skip_prefix(buffer, "type ", &buffer)) {
38370253 848 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
cec097be
JS
849 goto done;
850 }
851 eol = strchr(buffer, '\n');
852 if (!eol) {
38370253 853 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
cec097be
JS
854 goto done;
855 }
856 if (type_from_string_gently(buffer, eol - buffer, 1) < 0)
38370253 857 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
cec097be
JS
858 if (ret)
859 goto done;
860 buffer = eol + 1;
861
862 if (!skip_prefix(buffer, "tag ", &buffer)) {
38370253 863 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
cec097be
JS
864 goto done;
865 }
866 eol = strchr(buffer, '\n');
867 if (!eol) {
38370253 868 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
cec097be
JS
869 goto done;
870 }
871 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
f27d05b1 872 if (check_refname_format(sb.buf, 0)) {
38370253
JK
873 ret = report(options, &tag->object.oid, tag->object.type,
874 FSCK_MSG_BAD_TAG_NAME,
875 "invalid 'tag' name: %.*s",
876 (int)(eol - buffer), buffer);
f27d05b1
JS
877 if (ret)
878 goto done;
879 }
cec097be
JS
880 buffer = eol + 1;
881
f27d05b1 882 if (!skip_prefix(buffer, "tagger ", &buffer)) {
cec097be 883 /* early tags do not contain 'tagger' lines; warn only */
38370253 884 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
f27d05b1
JS
885 if (ret)
886 goto done;
887 }
cec097be 888 else
78543993 889 ret = fsck_ident(&buffer, &tag->object.oid, tag->object.type, options);
cec097be
JS
890
891done:
892 strbuf_release(&sb);
cec097be
JS
893 return ret;
894}
895
ed8b10f6 896struct fsck_gitmodules_data {
6da40b22 897 const struct object_id *oid;
ed8b10f6
JK
898 struct fsck_options *options;
899 int ret;
900};
901
902static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
903{
904 struct fsck_gitmodules_data *data = vdata;
905 const char *subsection, *key;
906 int subsection_len;
907 char *name;
908
909 if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
910 !subsection)
911 return 0;
912
913 name = xmemdupz(subsection, subsection_len);
914 if (check_submodule_name(name) < 0)
38370253 915 data->ret |= report(data->options,
6da40b22 916 data->oid, OBJ_BLOB,
ed8b10f6
JK
917 FSCK_MSG_GITMODULES_NAME,
918 "disallowed submodule name: %s",
919 name);
a124133e
JK
920 if (!strcmp(key, "url") && value &&
921 looks_like_command_line_option(value))
38370253 922 data->ret |= report(data->options,
6da40b22 923 data->oid, OBJ_BLOB,
a124133e
JK
924 FSCK_MSG_GITMODULES_URL,
925 "disallowed submodule url: %s",
926 value);
1a7fd1fb
JK
927 if (!strcmp(key, "path") && value &&
928 looks_like_command_line_option(value))
38370253 929 data->ret |= report(data->options,
6da40b22 930 data->oid, OBJ_BLOB,
1a7fd1fb
JK
931 FSCK_MSG_GITMODULES_PATH,
932 "disallowed submodule path: %s",
933 value);
ed8b10f6
JK
934 free(name);
935
936 return 0;
937}
938
6da40b22 939static int fsck_blob(const struct object_id *oid, const char *buf,
7ac4f3a0
JK
940 unsigned long size, struct fsck_options *options)
941{
ed8b10f6 942 struct fsck_gitmodules_data data;
de6bd9e3 943 struct config_options config_opts = { 0 };
ed8b10f6 944
6da40b22 945 if (!oidset_contains(&gitmodules_found, oid))
ed8b10f6 946 return 0;
6da40b22 947 oidset_insert(&gitmodules_done, oid);
ed8b10f6 948
6da40b22 949 if (object_on_skiplist(options, oid))
fb162877
RJ
950 return 0;
951
ed8b10f6
JK
952 if (!buf) {
953 /*
954 * A missing buffer here is a sign that the caller found the
955 * blob too gigantic to load into memory. Let's just consider
956 * that an error.
957 */
6da40b22 958 return report(options, oid, OBJ_BLOB,
0d68764d 959 FSCK_MSG_GITMODULES_LARGE,
ed8b10f6
JK
960 ".gitmodules too large to parse");
961 }
962
6da40b22 963 data.oid = oid;
ed8b10f6
JK
964 data.options = options;
965 data.ret = 0;
de6bd9e3 966 config_opts.error_action = CONFIG_ERROR_SILENT;
ed8b10f6 967 if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
de6bd9e3 968 ".gitmodules", buf, size, &data, &config_opts))
6da40b22 969 data.ret |= report(options, oid, OBJ_BLOB,
ed8b10f6
JK
970 FSCK_MSG_GITMODULES_PARSE,
971 "could not parse gitmodules blob");
972
973 return data.ret;
7ac4f3a0
JK
974}
975
90a398bb 976int fsck_object(struct object *obj, void *data, unsigned long size,
22410549 977 struct fsck_options *options)
ba002f3b
MK
978{
979 if (!obj)
38370253 980 return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
ba002f3b
MK
981
982 if (obj->type == OBJ_BLOB)
6da40b22 983 return fsck_blob(&obj->oid, data, size, options);
ba002f3b 984 if (obj->type == OBJ_TREE)
23a173a7 985 return fsck_tree((struct tree *) obj, data, size, options);
ba002f3b 986 if (obj->type == OBJ_COMMIT)
90a398bb 987 return fsck_commit((struct commit *) obj, (const char *) data,
22410549 988 size, options);
ba002f3b 989 if (obj->type == OBJ_TAG)
90a398bb 990 return fsck_tag((struct tag *) obj, (const char *) data,
22410549 991 size, options);
ba002f3b 992
38370253
JK
993 return report(options, &obj->oid, obj->type,
994 FSCK_MSG_UNKNOWN_TYPE,
995 "unknown type '%d' (internal fsck error)",
996 obj->type);
ba002f3b 997}
d6ffc8d7 998
1cd772cc 999int fsck_error_function(struct fsck_options *o,
5afc4b1d
JK
1000 const struct object_id *oid,
1001 enum object_type object_type,
1002 int msg_type, const char *message)
d6ffc8d7 1003{
0282f4dc 1004 if (msg_type == FSCK_WARN) {
5afc4b1d 1005 warning("object %s: %s", fsck_describe_object(o, oid), message);
0282f4dc
JS
1006 return 0;
1007 }
5afc4b1d 1008 error("object %s: %s", fsck_describe_object(o, oid), message);
d6ffc8d7
MK
1009 return 1;
1010}
159e7b08
JK
1011
1012int fsck_finish(struct fsck_options *options)
1013{
1014 int ret = 0;
1015 struct oidset_iter iter;
1016 const struct object_id *oid;
1017
1018 oidset_iter_init(&gitmodules_found, &iter);
1019 while ((oid = oidset_iter_next(&iter))) {
159e7b08
JK
1020 enum object_type type;
1021 unsigned long size;
1022 char *buf;
1023
1024 if (oidset_contains(&gitmodules_done, oid))
1025 continue;
1026
7913f53b 1027 buf = read_object_file(oid, &type, &size);
159e7b08 1028 if (!buf) {
b8b00f16 1029 if (is_promisor_object(oid))
27387444 1030 continue;
38370253 1031 ret |= report(options,
b8b00f16 1032 oid, OBJ_BLOB,
159e7b08
JK
1033 FSCK_MSG_GITMODULES_MISSING,
1034 "unable to read .gitmodules blob");
1035 continue;
1036 }
1037
1038 if (type == OBJ_BLOB)
b8b00f16 1039 ret |= fsck_blob(oid, buf, size, options);
159e7b08 1040 else
38370253 1041 ret |= report(options,
b8b00f16 1042 oid, type,
159e7b08
JK
1043 FSCK_MSG_GITMODULES_BLOB,
1044 "non-blob found at .gitmodules");
1045 free(buf);
1046 }
1047
1048
1049 oidset_clear(&gitmodules_found);
1050 oidset_clear(&gitmodules_done);
1051 return ret;
1052}