The first batch post 2.25 cycle
[git.git] / fsck.c
blob640d813d8430c6835ab1068c0c4512560e91c2b8
1 #include "cache.h"
2 #include "object-store.h"
3 #include "repository.h"
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"
11 #include "refs.h"
12 #include "utf8.h"
13 #include "decorate.h"
14 #include "oidset.h"
15 #include "packfile.h"
16 #include "submodule-config.h"
17 #include "config.h"
18 #include "help.h"
20 static struct oidset gitmodules_found = OIDSET_INIT;
21 static struct oidset gitmodules_done = OIDSET_INIT;
23 #define FSCK_FATAL -1
24 #define FSCK_INFO -2
26 #define FOREACH_MSG_ID(FUNC) \
27 /* fatal errors */ \
28 FUNC(NUL_IN_HEADER, FATAL) \
29 FUNC(UNTERMINATED_HEADER, FATAL) \
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) \
46 FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
47 FUNC(MISSING_OBJECT, 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_TREE, ERROR) \
53 FUNC(MISSING_TREE_OBJECT, ERROR) \
54 FUNC(MISSING_TYPE, ERROR) \
55 FUNC(MISSING_TYPE_ENTRY, ERROR) \
56 FUNC(MULTIPLE_AUTHORS, ERROR) \
57 FUNC(TREE_NOT_SORTED, ERROR) \
58 FUNC(UNKNOWN_TYPE, ERROR) \
59 FUNC(ZERO_PADDED_DATE, ERROR) \
60 FUNC(GITMODULES_MISSING, ERROR) \
61 FUNC(GITMODULES_BLOB, ERROR) \
62 FUNC(GITMODULES_LARGE, ERROR) \
63 FUNC(GITMODULES_NAME, ERROR) \
64 FUNC(GITMODULES_SYMLINK, ERROR) \
65 FUNC(GITMODULES_URL, ERROR) \
66 FUNC(GITMODULES_PATH, ERROR) \
67 FUNC(GITMODULES_UPDATE, ERROR) \
68 /* warnings */ \
69 FUNC(BAD_FILEMODE, WARN) \
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) \
75 FUNC(NULL_SHA1, WARN) \
76 FUNC(ZERO_PADDED_FILEMODE, WARN) \
77 FUNC(NUL_IN_COMMIT, WARN) \
78 /* infos (reported as warnings, but ignored by default) */ \
79 FUNC(GITMODULES_PARSE, INFO) \
80 FUNC(BAD_TAG_NAME, INFO) \
81 FUNC(MISSING_TAGGER_ENTRY, INFO)
83 #define MSG_ID(id, msg_type) FSCK_MSG_##id,
84 enum fsck_msg_id {
85 FOREACH_MSG_ID(MSG_ID)
86 FSCK_MSG_MAX
88 #undef MSG_ID
90 #define STR(x) #x
91 #define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
92 static struct {
93 const char *id_string;
94 const char *downcased;
95 const char *camelcased;
96 int msg_type;
97 } msg_id_info[FSCK_MSG_MAX + 1] = {
98 FOREACH_MSG_ID(MSG_ID)
99 { NULL, NULL, NULL, -1 }
101 #undef MSG_ID
103 static void prepare_msg_ids(void)
105 int i;
107 if (msg_id_info[0].downcased)
108 return;
110 /* convert id_string to lower case, without underscores. */
111 for (i = 0; i < FSCK_MSG_MAX; i++) {
112 const char *p = msg_id_info[i].id_string;
113 int len = strlen(p);
114 char *q = xmalloc(len);
116 msg_id_info[i].downcased = q;
117 while (*p)
118 if (*p == '_')
119 p++;
120 else
121 *(q)++ = tolower(*(p)++);
122 *q = '\0';
124 p = msg_id_info[i].id_string;
125 q = xmalloc(len);
126 msg_id_info[i].camelcased = q;
127 while (*p) {
128 if (*p == '_') {
129 p++;
130 if (*p)
131 *q++ = *p++;
132 } else {
133 *q++ = tolower(*p++);
136 *q = '\0';
140 static int parse_msg_id(const char *text)
142 int i;
144 prepare_msg_ids();
146 for (i = 0; i < FSCK_MSG_MAX; i++)
147 if (!strcmp(text, msg_id_info[i].downcased))
148 return i;
150 return -1;
153 void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
155 int i;
157 prepare_msg_ids();
159 for (i = 0; i < FSCK_MSG_MAX; i++)
160 list_config_item(list, prefix, msg_id_info[i].camelcased);
163 static int fsck_msg_type(enum fsck_msg_id msg_id,
164 struct fsck_options *options)
166 int msg_type;
168 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
170 if (options->msg_type)
171 msg_type = options->msg_type[msg_id];
172 else {
173 msg_type = msg_id_info[msg_id].msg_type;
174 if (options->strict && msg_type == FSCK_WARN)
175 msg_type = FSCK_ERROR;
178 return msg_type;
181 static int parse_msg_type(const char *str)
183 if (!strcmp(str, "error"))
184 return FSCK_ERROR;
185 else if (!strcmp(str, "warn"))
186 return FSCK_WARN;
187 else if (!strcmp(str, "ignore"))
188 return FSCK_IGNORE;
189 else
190 die("Unknown fsck message type: '%s'", str);
193 int is_valid_msg_type(const char *msg_id, const char *msg_type)
195 if (parse_msg_id(msg_id) < 0)
196 return 0;
197 parse_msg_type(msg_type);
198 return 1;
201 void fsck_set_msg_type(struct fsck_options *options,
202 const char *msg_id, const char *msg_type)
204 int id = parse_msg_id(msg_id), type;
206 if (id < 0)
207 die("Unhandled message id: %s", msg_id);
208 type = parse_msg_type(msg_type);
210 if (type != FSCK_ERROR && msg_id_info[id].msg_type == FSCK_FATAL)
211 die("Cannot demote %s to %s", msg_id, msg_type);
213 if (!options->msg_type) {
214 int i;
215 int *msg_type;
216 ALLOC_ARRAY(msg_type, FSCK_MSG_MAX);
217 for (i = 0; i < FSCK_MSG_MAX; i++)
218 msg_type[i] = fsck_msg_type(i, options);
219 options->msg_type = msg_type;
222 options->msg_type[id] = type;
225 void fsck_set_msg_types(struct fsck_options *options, const char *values)
227 char *buf = xstrdup(values), *to_free = buf;
228 int done = 0;
230 while (!done) {
231 int len = strcspn(buf, " ,|"), equal;
233 done = !buf[len];
234 if (!len) {
235 buf++;
236 continue;
238 buf[len] = '\0';
240 for (equal = 0;
241 equal < len && buf[equal] != '=' && buf[equal] != ':';
242 equal++)
243 buf[equal] = tolower(buf[equal]);
244 buf[equal] = '\0';
246 if (!strcmp(buf, "skiplist")) {
247 if (equal == len)
248 die("skiplist requires a path");
249 oidset_parse_file(&options->skiplist, buf + equal + 1);
250 buf += len + 1;
251 continue;
254 if (equal == len)
255 die("Missing '=': '%s'", buf);
257 fsck_set_msg_type(options, buf, buf + equal + 1);
258 buf += len + 1;
260 free(to_free);
263 static void append_msg_id(struct strbuf *sb, const char *msg_id)
265 for (;;) {
266 char c = *(msg_id)++;
268 if (!c)
269 break;
270 if (c != '_')
271 strbuf_addch(sb, tolower(c));
272 else {
273 assert(*msg_id);
274 strbuf_addch(sb, *(msg_id)++);
278 strbuf_addstr(sb, ": ");
281 static int object_on_skiplist(struct fsck_options *opts,
282 const struct object_id *oid)
284 return opts && oid && oidset_contains(&opts->skiplist, oid);
287 __attribute__((format (printf, 5, 6)))
288 static int report(struct fsck_options *options,
289 const struct object_id *oid, enum object_type object_type,
290 enum fsck_msg_id id, const char *fmt, ...)
292 va_list ap;
293 struct strbuf sb = STRBUF_INIT;
294 int msg_type = fsck_msg_type(id, options), result;
296 if (msg_type == FSCK_IGNORE)
297 return 0;
299 if (object_on_skiplist(options, oid))
300 return 0;
302 if (msg_type == FSCK_FATAL)
303 msg_type = FSCK_ERROR;
304 else if (msg_type == FSCK_INFO)
305 msg_type = FSCK_WARN;
307 append_msg_id(&sb, msg_id_info[id].id_string);
309 va_start(ap, fmt);
310 strbuf_vaddf(&sb, fmt, ap);
311 result = options->error_func(options, oid, object_type,
312 msg_type, sb.buf);
313 strbuf_release(&sb);
314 va_end(ap);
316 return result;
319 void fsck_enable_object_names(struct fsck_options *options)
321 if (!options->object_names)
322 options->object_names = kh_init_oid_map();
325 const char *fsck_get_object_name(struct fsck_options *options,
326 const struct object_id *oid)
328 khiter_t pos;
329 if (!options->object_names)
330 return NULL;
331 pos = kh_get_oid_map(options->object_names, *oid);
332 if (pos >= kh_end(options->object_names))
333 return NULL;
334 return kh_value(options->object_names, pos);
337 void fsck_put_object_name(struct fsck_options *options,
338 const struct object_id *oid,
339 const char *fmt, ...)
341 va_list ap;
342 struct strbuf buf = STRBUF_INIT;
343 khiter_t pos;
344 int hashret;
346 if (!options->object_names)
347 return;
349 pos = kh_put_oid_map(options->object_names, *oid, &hashret);
350 if (!hashret)
351 return;
352 va_start(ap, fmt);
353 strbuf_vaddf(&buf, fmt, ap);
354 kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL);
355 va_end(ap);
358 const char *fsck_describe_object(struct fsck_options *options,
359 const struct object_id *oid)
361 static struct strbuf bufs[] = {
362 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
364 static int b = 0;
365 struct strbuf *buf;
366 const char *name = fsck_get_object_name(options, oid);
368 buf = bufs + b;
369 b = (b + 1) % ARRAY_SIZE(bufs);
370 strbuf_reset(buf);
371 strbuf_addstr(buf, oid_to_hex(oid));
372 if (name)
373 strbuf_addf(buf, " (%s)", name);
375 return buf->buf;
378 static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
380 struct tree_desc desc;
381 struct name_entry entry;
382 int res = 0;
383 const char *name;
385 if (parse_tree(tree))
386 return -1;
388 name = fsck_get_object_name(options, &tree->object.oid);
389 if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
390 return -1;
391 while (tree_entry_gently(&desc, &entry)) {
392 struct object *obj;
393 int result;
395 if (S_ISGITLINK(entry.mode))
396 continue;
398 if (S_ISDIR(entry.mode)) {
399 obj = (struct object *)lookup_tree(the_repository, &entry.oid);
400 if (name && obj)
401 fsck_put_object_name(options, &entry.oid, "%s%s/",
402 name, entry.path);
403 result = options->walk(obj, OBJ_TREE, data, options);
405 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
406 obj = (struct object *)lookup_blob(the_repository, &entry.oid);
407 if (name && obj)
408 fsck_put_object_name(options, &entry.oid, "%s%s",
409 name, entry.path);
410 result = options->walk(obj, OBJ_BLOB, data, options);
412 else {
413 result = error("in tree %s: entry %s has bad mode %.6o",
414 fsck_describe_object(options, &tree->object.oid),
415 entry.path, entry.mode);
417 if (result < 0)
418 return result;
419 if (!res)
420 res = result;
422 return res;
425 static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
427 int counter = 0, generation = 0, name_prefix_len = 0;
428 struct commit_list *parents;
429 int res;
430 int result;
431 const char *name;
433 if (parse_commit(commit))
434 return -1;
436 name = fsck_get_object_name(options, &commit->object.oid);
437 if (name)
438 fsck_put_object_name(options, get_commit_tree_oid(commit),
439 "%s:", name);
441 result = options->walk((struct object *)get_commit_tree(commit),
442 OBJ_TREE, data, options);
443 if (result < 0)
444 return result;
445 res = result;
447 parents = commit->parents;
448 if (name && parents) {
449 int len = strlen(name), power;
451 if (len && name[len - 1] == '^') {
452 generation = 1;
453 name_prefix_len = len - 1;
455 else { /* parse ~<generation> suffix */
456 for (generation = 0, power = 1;
457 len && isdigit(name[len - 1]);
458 power *= 10)
459 generation += power * (name[--len] - '0');
460 if (power > 1 && len && name[len - 1] == '~')
461 name_prefix_len = len - 1;
465 while (parents) {
466 if (name) {
467 struct object_id *oid = &parents->item->object.oid;
469 if (counter++)
470 fsck_put_object_name(options, oid, "%s^%d",
471 name, counter);
472 else if (generation > 0)
473 fsck_put_object_name(options, oid, "%.*s~%d",
474 name_prefix_len, name,
475 generation + 1);
476 else
477 fsck_put_object_name(options, oid, "%s^", name);
479 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
480 if (result < 0)
481 return result;
482 if (!res)
483 res = result;
484 parents = parents->next;
486 return res;
489 static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
491 const char *name = fsck_get_object_name(options, &tag->object.oid);
493 if (parse_tag(tag))
494 return -1;
495 if (name)
496 fsck_put_object_name(options, &tag->tagged->oid, "%s", name);
497 return options->walk(tag->tagged, OBJ_ANY, data, options);
500 int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
502 if (!obj)
503 return -1;
505 if (obj->type == OBJ_NONE)
506 parse_object(the_repository, &obj->oid);
508 switch (obj->type) {
509 case OBJ_BLOB:
510 return 0;
511 case OBJ_TREE:
512 return fsck_walk_tree((struct tree *)obj, data, options);
513 case OBJ_COMMIT:
514 return fsck_walk_commit((struct commit *)obj, data, options);
515 case OBJ_TAG:
516 return fsck_walk_tag((struct tag *)obj, data, options);
517 default:
518 error("Unknown object type for %s",
519 fsck_describe_object(options, &obj->oid));
520 return -1;
525 * The entries in a tree are ordered in the _path_ order,
526 * which means that a directory entry is ordered by adding
527 * a slash to the end of it.
529 * So a directory called "a" is ordered _after_ a file
530 * called "a.c", because "a/" sorts after "a.c".
532 #define TREE_UNORDERED (-1)
533 #define TREE_HAS_DUPS (-2)
535 static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
537 int len1 = strlen(name1);
538 int len2 = strlen(name2);
539 int len = len1 < len2 ? len1 : len2;
540 unsigned char c1, c2;
541 int cmp;
543 cmp = memcmp(name1, name2, len);
544 if (cmp < 0)
545 return 0;
546 if (cmp > 0)
547 return TREE_UNORDERED;
550 * Ok, the first <len> characters are the same.
551 * Now we need to order the next one, but turn
552 * a '\0' into a '/' for a directory entry.
554 c1 = name1[len];
555 c2 = name2[len];
556 if (!c1 && !c2)
558 * git-write-tree used to write out a nonsense tree that has
559 * entries with the same name, one blob and one tree. Make
560 * sure we do not have duplicate entries.
562 return TREE_HAS_DUPS;
563 if (!c1 && S_ISDIR(mode1))
564 c1 = '/';
565 if (!c2 && S_ISDIR(mode2))
566 c2 = '/';
567 return c1 < c2 ? 0 : TREE_UNORDERED;
570 static int fsck_tree(const struct object_id *oid,
571 const char *buffer, unsigned long size,
572 struct fsck_options *options)
574 int retval = 0;
575 int has_null_sha1 = 0;
576 int has_full_path = 0;
577 int has_empty_name = 0;
578 int has_dot = 0;
579 int has_dotdot = 0;
580 int has_dotgit = 0;
581 int has_zero_pad = 0;
582 int has_bad_modes = 0;
583 int has_dup_entries = 0;
584 int not_properly_sorted = 0;
585 struct tree_desc desc;
586 unsigned o_mode;
587 const char *o_name;
589 if (init_tree_desc_gently(&desc, buffer, size)) {
590 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
591 return retval;
594 o_mode = 0;
595 o_name = NULL;
597 while (desc.size) {
598 unsigned short mode;
599 const char *name, *backslash;
600 const struct object_id *oid;
602 oid = tree_entry_extract(&desc, &name, &mode);
604 has_null_sha1 |= is_null_oid(oid);
605 has_full_path |= !!strchr(name, '/');
606 has_empty_name |= !*name;
607 has_dot |= !strcmp(name, ".");
608 has_dotdot |= !strcmp(name, "..");
609 has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
610 has_zero_pad |= *(char *)desc.buffer == '0';
612 if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
613 if (!S_ISLNK(mode))
614 oidset_insert(&gitmodules_found, oid);
615 else
616 retval += report(options,
617 oid, OBJ_TREE,
618 FSCK_MSG_GITMODULES_SYMLINK,
619 ".gitmodules is a symbolic link");
622 if ((backslash = strchr(name, '\\'))) {
623 while (backslash) {
624 backslash++;
625 has_dotgit |= is_ntfs_dotgit(backslash);
626 if (is_ntfs_dotgitmodules(backslash)) {
627 if (!S_ISLNK(mode))
628 oidset_insert(&gitmodules_found, oid);
629 else
630 retval += report(options, oid, OBJ_TREE,
631 FSCK_MSG_GITMODULES_SYMLINK,
632 ".gitmodules is a symbolic link");
634 backslash = strchr(backslash, '\\');
638 if (update_tree_entry_gently(&desc)) {
639 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
640 break;
643 switch (mode) {
645 * Standard modes..
647 case S_IFREG | 0755:
648 case S_IFREG | 0644:
649 case S_IFLNK:
650 case S_IFDIR:
651 case S_IFGITLINK:
652 break;
654 * This is nonstandard, but we had a few of these
655 * early on when we honored the full set of mode
656 * bits..
658 case S_IFREG | 0664:
659 if (!options->strict)
660 break;
661 /* fallthrough */
662 default:
663 has_bad_modes = 1;
666 if (o_name) {
667 switch (verify_ordered(o_mode, o_name, mode, name)) {
668 case TREE_UNORDERED:
669 not_properly_sorted = 1;
670 break;
671 case TREE_HAS_DUPS:
672 has_dup_entries = 1;
673 break;
674 default:
675 break;
679 o_mode = mode;
680 o_name = name;
683 if (has_null_sha1)
684 retval += report(options, oid, OBJ_TREE, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
685 if (has_full_path)
686 retval += report(options, oid, OBJ_TREE, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
687 if (has_empty_name)
688 retval += report(options, oid, OBJ_TREE, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
689 if (has_dot)
690 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOT, "contains '.'");
691 if (has_dotdot)
692 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTDOT, "contains '..'");
693 if (has_dotgit)
694 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
695 if (has_zero_pad)
696 retval += report(options, oid, OBJ_TREE, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
697 if (has_bad_modes)
698 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
699 if (has_dup_entries)
700 retval += report(options, oid, OBJ_TREE, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries");
701 if (not_properly_sorted)
702 retval += report(options, oid, OBJ_TREE, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted");
703 return retval;
706 static int verify_headers(const void *data, unsigned long size,
707 const struct object_id *oid, enum object_type type,
708 struct fsck_options *options)
710 const char *buffer = (const char *)data;
711 unsigned long i;
713 for (i = 0; i < size; i++) {
714 switch (buffer[i]) {
715 case '\0':
716 return report(options, oid, type,
717 FSCK_MSG_NUL_IN_HEADER,
718 "unterminated header: NUL at offset %ld", i);
719 case '\n':
720 if (i + 1 < size && buffer[i + 1] == '\n')
721 return 0;
726 * We did not find double-LF that separates the header
727 * and the body. Not having a body is not a crime but
728 * we do want to see the terminating LF for the last header
729 * line.
731 if (size && buffer[size - 1] == '\n')
732 return 0;
734 return report(options, oid, type,
735 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
738 static int fsck_ident(const char **ident,
739 const struct object_id *oid, enum object_type type,
740 struct fsck_options *options)
742 const char *p = *ident;
743 char *end;
745 *ident = strchrnul(*ident, '\n');
746 if (**ident == '\n')
747 (*ident)++;
749 if (*p == '<')
750 return report(options, oid, type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
751 p += strcspn(p, "<>\n");
752 if (*p == '>')
753 return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
754 if (*p != '<')
755 return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
756 if (p[-1] != ' ')
757 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
758 p++;
759 p += strcspn(p, "<>\n");
760 if (*p != '>')
761 return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
762 p++;
763 if (*p != ' ')
764 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
765 p++;
766 if (*p == '0' && p[1] != ' ')
767 return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
768 if (date_overflows(parse_timestamp(p, &end, 10)))
769 return report(options, oid, type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
770 if ((end == p || *end != ' '))
771 return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
772 p = end + 1;
773 if ((*p != '+' && *p != '-') ||
774 !isdigit(p[1]) ||
775 !isdigit(p[2]) ||
776 !isdigit(p[3]) ||
777 !isdigit(p[4]) ||
778 (p[5] != '\n'))
779 return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
780 p += 6;
781 return 0;
784 static int fsck_commit(const struct object_id *oid,
785 const char *buffer, unsigned long size,
786 struct fsck_options *options)
788 struct object_id tree_oid, parent_oid;
789 unsigned author_count;
790 int err;
791 const char *buffer_begin = buffer;
792 const char *p;
794 if (verify_headers(buffer, size, oid, OBJ_COMMIT, options))
795 return -1;
797 if (!skip_prefix(buffer, "tree ", &buffer))
798 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
799 if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
800 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
801 if (err)
802 return err;
804 buffer = p + 1;
805 while (skip_prefix(buffer, "parent ", &buffer)) {
806 if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') {
807 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
808 if (err)
809 return err;
811 buffer = p + 1;
813 author_count = 0;
814 while (skip_prefix(buffer, "author ", &buffer)) {
815 author_count++;
816 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
817 if (err)
818 return err;
820 if (author_count < 1)
821 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
822 else if (author_count > 1)
823 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
824 if (err)
825 return err;
826 if (!skip_prefix(buffer, "committer ", &buffer))
827 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
828 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
829 if (err)
830 return err;
831 if (memchr(buffer_begin, '\0', size)) {
832 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT,
833 "NUL byte in the commit object body");
834 if (err)
835 return err;
837 return 0;
840 static int fsck_tag(const struct object_id *oid, const char *buffer,
841 unsigned long size, struct fsck_options *options)
843 struct object_id tagged_oid;
844 int ret = 0;
845 char *eol;
846 struct strbuf sb = STRBUF_INIT;
847 const char *p;
849 ret = verify_headers(buffer, size, oid, OBJ_TAG, options);
850 if (ret)
851 goto done;
853 if (!skip_prefix(buffer, "object ", &buffer)) {
854 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
855 goto done;
857 if (parse_oid_hex(buffer, &tagged_oid, &p) || *p != '\n') {
858 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
859 if (ret)
860 goto done;
862 buffer = p + 1;
864 if (!skip_prefix(buffer, "type ", &buffer)) {
865 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
866 goto done;
868 eol = strchr(buffer, '\n');
869 if (!eol) {
870 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
871 goto done;
873 if (type_from_string_gently(buffer, eol - buffer, 1) < 0)
874 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
875 if (ret)
876 goto done;
877 buffer = eol + 1;
879 if (!skip_prefix(buffer, "tag ", &buffer)) {
880 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
881 goto done;
883 eol = strchr(buffer, '\n');
884 if (!eol) {
885 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
886 goto done;
888 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
889 if (check_refname_format(sb.buf, 0)) {
890 ret = report(options, oid, OBJ_TAG,
891 FSCK_MSG_BAD_TAG_NAME,
892 "invalid 'tag' name: %.*s",
893 (int)(eol - buffer), buffer);
894 if (ret)
895 goto done;
897 buffer = eol + 1;
899 if (!skip_prefix(buffer, "tagger ", &buffer)) {
900 /* early tags do not contain 'tagger' lines; warn only */
901 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
902 if (ret)
903 goto done;
905 else
906 ret = fsck_ident(&buffer, oid, OBJ_TAG, options);
908 done:
909 strbuf_release(&sb);
910 return ret;
913 struct fsck_gitmodules_data {
914 const struct object_id *oid;
915 struct fsck_options *options;
916 int ret;
919 static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
921 struct fsck_gitmodules_data *data = vdata;
922 const char *subsection, *key;
923 int subsection_len;
924 char *name;
926 if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
927 !subsection)
928 return 0;
930 name = xmemdupz(subsection, subsection_len);
931 if (check_submodule_name(name) < 0)
932 data->ret |= report(data->options,
933 data->oid, OBJ_BLOB,
934 FSCK_MSG_GITMODULES_NAME,
935 "disallowed submodule name: %s",
936 name);
937 if (!strcmp(key, "url") && value &&
938 looks_like_command_line_option(value))
939 data->ret |= report(data->options,
940 data->oid, OBJ_BLOB,
941 FSCK_MSG_GITMODULES_URL,
942 "disallowed submodule url: %s",
943 value);
944 if (!strcmp(key, "path") && value &&
945 looks_like_command_line_option(value))
946 data->ret |= report(data->options,
947 data->oid, OBJ_BLOB,
948 FSCK_MSG_GITMODULES_PATH,
949 "disallowed submodule path: %s",
950 value);
951 if (!strcmp(key, "update") && value &&
952 parse_submodule_update_type(value) == SM_UPDATE_COMMAND)
953 data->ret |= report(data->options, data->oid, OBJ_BLOB,
954 FSCK_MSG_GITMODULES_UPDATE,
955 "disallowed submodule update setting: %s",
956 value);
957 free(name);
959 return 0;
962 static int fsck_blob(const struct object_id *oid, const char *buf,
963 unsigned long size, struct fsck_options *options)
965 struct fsck_gitmodules_data data;
966 struct config_options config_opts = { 0 };
968 if (!oidset_contains(&gitmodules_found, oid))
969 return 0;
970 oidset_insert(&gitmodules_done, oid);
972 if (object_on_skiplist(options, oid))
973 return 0;
975 if (!buf) {
977 * A missing buffer here is a sign that the caller found the
978 * blob too gigantic to load into memory. Let's just consider
979 * that an error.
981 return report(options, oid, OBJ_BLOB,
982 FSCK_MSG_GITMODULES_LARGE,
983 ".gitmodules too large to parse");
986 data.oid = oid;
987 data.options = options;
988 data.ret = 0;
989 config_opts.error_action = CONFIG_ERROR_SILENT;
990 if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
991 ".gitmodules", buf, size, &data, &config_opts))
992 data.ret |= report(options, oid, OBJ_BLOB,
993 FSCK_MSG_GITMODULES_PARSE,
994 "could not parse gitmodules blob");
996 return data.ret;
999 int fsck_object(struct object *obj, void *data, unsigned long size,
1000 struct fsck_options *options)
1002 if (!obj)
1003 return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
1005 if (obj->type == OBJ_BLOB)
1006 return fsck_blob(&obj->oid, data, size, options);
1007 if (obj->type == OBJ_TREE)
1008 return fsck_tree(&obj->oid, data, size, options);
1009 if (obj->type == OBJ_COMMIT)
1010 return fsck_commit(&obj->oid, data, size, options);
1011 if (obj->type == OBJ_TAG)
1012 return fsck_tag(&obj->oid, data, size, options);
1014 return report(options, &obj->oid, obj->type,
1015 FSCK_MSG_UNKNOWN_TYPE,
1016 "unknown type '%d' (internal fsck error)",
1017 obj->type);
1020 int fsck_error_function(struct fsck_options *o,
1021 const struct object_id *oid,
1022 enum object_type object_type,
1023 int msg_type, const char *message)
1025 if (msg_type == FSCK_WARN) {
1026 warning("object %s: %s", fsck_describe_object(o, oid), message);
1027 return 0;
1029 error("object %s: %s", fsck_describe_object(o, oid), message);
1030 return 1;
1033 int fsck_finish(struct fsck_options *options)
1035 int ret = 0;
1036 struct oidset_iter iter;
1037 const struct object_id *oid;
1039 oidset_iter_init(&gitmodules_found, &iter);
1040 while ((oid = oidset_iter_next(&iter))) {
1041 enum object_type type;
1042 unsigned long size;
1043 char *buf;
1045 if (oidset_contains(&gitmodules_done, oid))
1046 continue;
1048 buf = read_object_file(oid, &type, &size);
1049 if (!buf) {
1050 if (is_promisor_object(oid))
1051 continue;
1052 ret |= report(options,
1053 oid, OBJ_BLOB,
1054 FSCK_MSG_GITMODULES_MISSING,
1055 "unable to read .gitmodules blob");
1056 continue;
1059 if (type == OBJ_BLOB)
1060 ret |= fsck_blob(oid, buf, size, options);
1061 else
1062 ret |= report(options,
1063 oid, type,
1064 FSCK_MSG_GITMODULES_BLOB,
1065 "non-blob found at .gitmodules");
1066 free(buf);
1070 oidset_clear(&gitmodules_found);
1071 oidset_clear(&gitmodules_done);
1072 return ret;