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