]> git.ipfire.org Git - thirdparty/git.git/blame - fsck.c
fsck: accept an oid instead of a "struct blob" for fsck_blob()
[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,
b2f44feb 690 struct object *obj, struct fsck_options *options)
4d0d8975
JS
691{
692 const char *buffer = (const char *)data;
693 unsigned long i;
694
695 for (i = 0; i < size; i++) {
696 switch (buffer[i]) {
697 case '\0':
38370253 698 return report(options, &obj->oid, obj->type,
c99ba492
JS
699 FSCK_MSG_NUL_IN_HEADER,
700 "unterminated header: NUL at offset %ld", i);
4d0d8975
JS
701 case '\n':
702 if (i + 1 < size && buffer[i + 1] == '\n')
703 return 0;
704 }
705 }
706
84d18c0b
JH
707 /*
708 * We did not find double-LF that separates the header
709 * and the body. Not having a body is not a crime but
710 * we do want to see the terminating LF for the last header
711 * line.
712 */
713 if (size && buffer[size - 1] == '\n')
714 return 0;
715
38370253 716 return report(options, &obj->oid, obj->type,
c99ba492 717 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
4d0d8975
JS
718}
719
22410549 720static int fsck_ident(const char **ident, struct object *obj, struct fsck_options *options)
daae1922 721{
e6826e33 722 const char *p = *ident;
d4b8de04
JK
723 char *end;
724
e6826e33
JS
725 *ident = strchrnul(*ident, '\n');
726 if (**ident == '\n')
727 (*ident)++;
728
729 if (*p == '<')
38370253 730 return report(options, &obj->oid, obj->type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
731 p += strcspn(p, "<>\n");
732 if (*p == '>')
38370253 733 return report(options, &obj->oid, obj->type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
e6826e33 734 if (*p != '<')
38370253 735 return report(options, &obj->oid, obj->type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
e6826e33 736 if (p[-1] != ' ')
38370253 737 return report(options, &obj->oid, obj->type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
738 p++;
739 p += strcspn(p, "<>\n");
740 if (*p != '>')
38370253 741 return report(options, &obj->oid, obj->type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
e6826e33
JS
742 p++;
743 if (*p != ' ')
38370253 744 return report(options, &obj->oid, obj->type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
e6826e33
JS
745 p++;
746 if (*p == '0' && p[1] != ' ')
38370253 747 return report(options, &obj->oid, obj->type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
1aeb7e75 748 if (date_overflows(parse_timestamp(p, &end, 10)))
38370253 749 return report(options, &obj->oid, obj->type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
e6826e33 750 if ((end == p || *end != ' '))
38370253 751 return report(options, &obj->oid, obj->type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
e6826e33
JS
752 p = end + 1;
753 if ((*p != '+' && *p != '-') ||
754 !isdigit(p[1]) ||
755 !isdigit(p[2]) ||
756 !isdigit(p[3]) ||
757 !isdigit(p[4]) ||
758 (p[5] != '\n'))
38370253 759 return report(options, &obj->oid, obj->type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
e6826e33 760 p += 6;
daae1922
JN
761 return 0;
762}
763
23a173a7
JK
764static int fsck_commit(struct commit *commit, const char *buffer,
765 unsigned long size, struct fsck_options *options)
ba002f3b 766{
c54f5ca9 767 struct object_id tree_oid, oid;
ec652315 768 unsigned author_count;
daae1922 769 int err;
6d2d780f 770 const char *buffer_begin = buffer;
c54f5ca9 771 const char *p;
ba002f3b 772
b2f44feb 773 if (verify_headers(buffer, size, &commit->object, options))
4d0d8975
JS
774 return -1;
775
cf4fff57 776 if (!skip_prefix(buffer, "tree ", &buffer))
38370253 777 return report(options, &commit->object.oid, commit->object.type, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
c54f5ca9 778 if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
38370253 779 err = report(options, &commit->object.oid, commit->object.type, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
b3584761
JS
780 if (err)
781 return err;
782 }
c54f5ca9 783 buffer = p + 1;
cf4fff57 784 while (skip_prefix(buffer, "parent ", &buffer)) {
c54f5ca9 785 if (parse_oid_hex(buffer, &oid, &p) || *p != '\n') {
38370253 786 err = report(options, &commit->object.oid, commit->object.type, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
b3584761
JS
787 if (err)
788 return err;
789 }
c54f5ca9 790 buffer = p + 1;
ba002f3b 791 }
c9ad147f
JS
792 author_count = 0;
793 while (skip_prefix(buffer, "author ", &buffer)) {
794 author_count++;
795 err = fsck_ident(&buffer, &commit->object, options);
796 if (err)
797 return err;
ba002f3b 798 }
c9ad147f 799 if (author_count < 1)
38370253 800 err = report(options, &commit->object.oid, commit->object.type, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
c9ad147f 801 else if (author_count > 1)
38370253 802 err = report(options, &commit->object.oid, commit->object.type, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
daae1922
JN
803 if (err)
804 return err;
cf4fff57 805 if (!skip_prefix(buffer, "committer ", &buffer))
38370253 806 return report(options, &commit->object.oid, commit->object.type, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
22410549 807 err = fsck_ident(&buffer, &commit->object, options);
daae1922
JN
808 if (err)
809 return err;
6d2d780f 810 if (memchr(buffer_begin, '\0', size)) {
38370253 811 err = report(options, &commit->object.oid, commit->object.type, FSCK_MSG_NUL_IN_COMMIT,
6d2d780f
JH
812 "NUL byte in the commit object body");
813 if (err)
814 return err;
815 }
ba002f3b
MK
816 return 0;
817}
818
23a173a7 819static int fsck_tag(struct tag *tag, const char *buffer,
2175a0c6 820 unsigned long size, struct fsck_options *options)
cec097be 821{
c54f5ca9 822 struct object_id oid;
cec097be 823 int ret = 0;
23a173a7 824 char *eol;
cec097be 825 struct strbuf sb = STRBUF_INIT;
c54f5ca9 826 const char *p;
cec097be 827
8a272f29
RS
828 ret = verify_headers(buffer, size, &tag->object, options);
829 if (ret)
cec097be
JS
830 goto done;
831
832 if (!skip_prefix(buffer, "object ", &buffer)) {
38370253 833 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
cec097be
JS
834 goto done;
835 }
c54f5ca9 836 if (parse_oid_hex(buffer, &oid, &p) || *p != '\n') {
38370253 837 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
7d7d5b05
JS
838 if (ret)
839 goto done;
cec097be 840 }
c54f5ca9 841 buffer = p + 1;
cec097be
JS
842
843 if (!skip_prefix(buffer, "type ", &buffer)) {
38370253 844 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
cec097be
JS
845 goto done;
846 }
847 eol = strchr(buffer, '\n');
848 if (!eol) {
38370253 849 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
cec097be
JS
850 goto done;
851 }
852 if (type_from_string_gently(buffer, eol - buffer, 1) < 0)
38370253 853 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
cec097be
JS
854 if (ret)
855 goto done;
856 buffer = eol + 1;
857
858 if (!skip_prefix(buffer, "tag ", &buffer)) {
38370253 859 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
cec097be
JS
860 goto done;
861 }
862 eol = strchr(buffer, '\n');
863 if (!eol) {
38370253 864 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
cec097be
JS
865 goto done;
866 }
867 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
f27d05b1 868 if (check_refname_format(sb.buf, 0)) {
38370253
JK
869 ret = report(options, &tag->object.oid, tag->object.type,
870 FSCK_MSG_BAD_TAG_NAME,
871 "invalid 'tag' name: %.*s",
872 (int)(eol - buffer), buffer);
f27d05b1
JS
873 if (ret)
874 goto done;
875 }
cec097be
JS
876 buffer = eol + 1;
877
f27d05b1 878 if (!skip_prefix(buffer, "tagger ", &buffer)) {
cec097be 879 /* early tags do not contain 'tagger' lines; warn only */
38370253 880 ret = report(options, &tag->object.oid, tag->object.type, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
f27d05b1
JS
881 if (ret)
882 goto done;
883 }
cec097be 884 else
22410549 885 ret = fsck_ident(&buffer, &tag->object, options);
cec097be
JS
886
887done:
888 strbuf_release(&sb);
cec097be
JS
889 return ret;
890}
891
ed8b10f6 892struct fsck_gitmodules_data {
6da40b22 893 const struct object_id *oid;
ed8b10f6
JK
894 struct fsck_options *options;
895 int ret;
896};
897
898static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
899{
900 struct fsck_gitmodules_data *data = vdata;
901 const char *subsection, *key;
902 int subsection_len;
903 char *name;
904
905 if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
906 !subsection)
907 return 0;
908
909 name = xmemdupz(subsection, subsection_len);
910 if (check_submodule_name(name) < 0)
38370253 911 data->ret |= report(data->options,
6da40b22 912 data->oid, OBJ_BLOB,
ed8b10f6
JK
913 FSCK_MSG_GITMODULES_NAME,
914 "disallowed submodule name: %s",
915 name);
a124133e
JK
916 if (!strcmp(key, "url") && value &&
917 looks_like_command_line_option(value))
38370253 918 data->ret |= report(data->options,
6da40b22 919 data->oid, OBJ_BLOB,
a124133e
JK
920 FSCK_MSG_GITMODULES_URL,
921 "disallowed submodule url: %s",
922 value);
1a7fd1fb
JK
923 if (!strcmp(key, "path") && value &&
924 looks_like_command_line_option(value))
38370253 925 data->ret |= report(data->options,
6da40b22 926 data->oid, OBJ_BLOB,
1a7fd1fb
JK
927 FSCK_MSG_GITMODULES_PATH,
928 "disallowed submodule path: %s",
929 value);
ed8b10f6
JK
930 free(name);
931
932 return 0;
933}
934
6da40b22 935static int fsck_blob(const struct object_id *oid, const char *buf,
7ac4f3a0
JK
936 unsigned long size, struct fsck_options *options)
937{
ed8b10f6 938 struct fsck_gitmodules_data data;
de6bd9e3 939 struct config_options config_opts = { 0 };
ed8b10f6 940
6da40b22 941 if (!oidset_contains(&gitmodules_found, oid))
ed8b10f6 942 return 0;
6da40b22 943 oidset_insert(&gitmodules_done, oid);
ed8b10f6 944
6da40b22 945 if (object_on_skiplist(options, oid))
fb162877
RJ
946 return 0;
947
ed8b10f6
JK
948 if (!buf) {
949 /*
950 * A missing buffer here is a sign that the caller found the
951 * blob too gigantic to load into memory. Let's just consider
952 * that an error.
953 */
6da40b22 954 return report(options, oid, OBJ_BLOB,
0d68764d 955 FSCK_MSG_GITMODULES_LARGE,
ed8b10f6
JK
956 ".gitmodules too large to parse");
957 }
958
6da40b22 959 data.oid = oid;
ed8b10f6
JK
960 data.options = options;
961 data.ret = 0;
de6bd9e3 962 config_opts.error_action = CONFIG_ERROR_SILENT;
ed8b10f6 963 if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
de6bd9e3 964 ".gitmodules", buf, size, &data, &config_opts))
6da40b22 965 data.ret |= report(options, oid, OBJ_BLOB,
ed8b10f6
JK
966 FSCK_MSG_GITMODULES_PARSE,
967 "could not parse gitmodules blob");
968
969 return data.ret;
7ac4f3a0
JK
970}
971
90a398bb 972int fsck_object(struct object *obj, void *data, unsigned long size,
22410549 973 struct fsck_options *options)
ba002f3b
MK
974{
975 if (!obj)
38370253 976 return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
ba002f3b
MK
977
978 if (obj->type == OBJ_BLOB)
6da40b22 979 return fsck_blob(&obj->oid, data, size, options);
ba002f3b 980 if (obj->type == OBJ_TREE)
23a173a7 981 return fsck_tree((struct tree *) obj, data, size, options);
ba002f3b 982 if (obj->type == OBJ_COMMIT)
90a398bb 983 return fsck_commit((struct commit *) obj, (const char *) data,
22410549 984 size, options);
ba002f3b 985 if (obj->type == OBJ_TAG)
90a398bb 986 return fsck_tag((struct tag *) obj, (const char *) data,
22410549 987 size, options);
ba002f3b 988
38370253
JK
989 return report(options, &obj->oid, obj->type,
990 FSCK_MSG_UNKNOWN_TYPE,
991 "unknown type '%d' (internal fsck error)",
992 obj->type);
ba002f3b 993}
d6ffc8d7 994
1cd772cc 995int fsck_error_function(struct fsck_options *o,
5afc4b1d
JK
996 const struct object_id *oid,
997 enum object_type object_type,
998 int msg_type, const char *message)
d6ffc8d7 999{
0282f4dc 1000 if (msg_type == FSCK_WARN) {
5afc4b1d 1001 warning("object %s: %s", fsck_describe_object(o, oid), message);
0282f4dc
JS
1002 return 0;
1003 }
5afc4b1d 1004 error("object %s: %s", fsck_describe_object(o, oid), message);
d6ffc8d7
MK
1005 return 1;
1006}
159e7b08
JK
1007
1008int fsck_finish(struct fsck_options *options)
1009{
1010 int ret = 0;
1011 struct oidset_iter iter;
1012 const struct object_id *oid;
1013
1014 oidset_iter_init(&gitmodules_found, &iter);
1015 while ((oid = oidset_iter_next(&iter))) {
1016 struct blob *blob;
1017 enum object_type type;
1018 unsigned long size;
1019 char *buf;
1020
1021 if (oidset_contains(&gitmodules_done, oid))
1022 continue;
1023
da14a7ff 1024 blob = lookup_blob(the_repository, oid);
159e7b08 1025 if (!blob) {
0ebbcf70 1026 struct object *obj = lookup_unknown_object(oid);
38370253 1027 ret |= report(options, &obj->oid, obj->type,
159e7b08
JK
1028 FSCK_MSG_GITMODULES_BLOB,
1029 "non-blob found at .gitmodules");
1030 continue;
1031 }
1032
7913f53b 1033 buf = read_object_file(oid, &type, &size);
159e7b08 1034 if (!buf) {
27387444
JK
1035 if (is_promisor_object(&blob->object.oid))
1036 continue;
38370253
JK
1037 ret |= report(options,
1038 &blob->object.oid, blob->object.type,
159e7b08
JK
1039 FSCK_MSG_GITMODULES_MISSING,
1040 "unable to read .gitmodules blob");
1041 continue;
1042 }
1043
1044 if (type == OBJ_BLOB)
6da40b22 1045 ret |= fsck_blob(&blob->object.oid, buf, size, options);
159e7b08 1046 else
38370253
JK
1047 ret |= report(options,
1048 &blob->object.oid, blob->object.type,
159e7b08
JK
1049 FSCK_MSG_GITMODULES_BLOB,
1050 "non-blob found at .gitmodules");
1051 free(buf);
1052 }
1053
1054
1055 oidset_clear(&gitmodules_found);
1056 oidset_clear(&gitmodules_done);
1057 return ret;
1058}