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