2 #include "object-store.h"
3 #include "repository.h"
16 #include "submodule-config.h"
18 #include "credential.h"
21 static struct oidset gitmodules_found = OIDSET_INIT;
22 static struct oidset gitmodules_done = OIDSET_INIT;
27 #define FOREACH_MSG_ID(FUNC) \
29 FUNC(NUL_IN_HEADER, FATAL) \
30 FUNC(UNTERMINATED_HEADER, FATAL) \
32 FUNC(BAD_DATE, ERROR) \
33 FUNC(BAD_DATE_OVERFLOW, ERROR) \
34 FUNC(BAD_EMAIL, ERROR) \
35 FUNC(BAD_NAME, ERROR) \
36 FUNC(BAD_OBJECT_SHA1, ERROR) \
37 FUNC(BAD_PARENT_SHA1, ERROR) \
38 FUNC(BAD_TAG_OBJECT, ERROR) \
39 FUNC(BAD_TIMEZONE, ERROR) \
40 FUNC(BAD_TREE, ERROR) \
41 FUNC(BAD_TREE_SHA1, ERROR) \
42 FUNC(BAD_TYPE, ERROR) \
43 FUNC(DUPLICATE_ENTRIES, ERROR) \
44 FUNC(MISSING_AUTHOR, ERROR) \
45 FUNC(MISSING_COMMITTER, ERROR) \
46 FUNC(MISSING_EMAIL, ERROR) \
47 FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
48 FUNC(MISSING_OBJECT, ERROR) \
49 FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \
50 FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \
51 FUNC(MISSING_TAG, ERROR) \
52 FUNC(MISSING_TAG_ENTRY, ERROR) \
53 FUNC(MISSING_TREE, ERROR) \
54 FUNC(MISSING_TREE_OBJECT, ERROR) \
55 FUNC(MISSING_TYPE, ERROR) \
56 FUNC(MISSING_TYPE_ENTRY, ERROR) \
57 FUNC(MULTIPLE_AUTHORS, ERROR) \
58 FUNC(TREE_NOT_SORTED, ERROR) \
59 FUNC(UNKNOWN_TYPE, ERROR) \
60 FUNC(ZERO_PADDED_DATE, ERROR) \
61 FUNC(GITMODULES_MISSING, ERROR) \
62 FUNC(GITMODULES_BLOB, ERROR) \
63 FUNC(GITMODULES_LARGE, ERROR) \
64 FUNC(GITMODULES_NAME, ERROR) \
65 FUNC(GITMODULES_SYMLINK, ERROR) \
66 FUNC(GITMODULES_URL, ERROR) \
67 FUNC(GITMODULES_PATH, ERROR) \
68 FUNC(GITMODULES_UPDATE, ERROR) \
70 FUNC(BAD_FILEMODE, WARN) \
71 FUNC(EMPTY_NAME, WARN) \
72 FUNC(FULL_PATHNAME, WARN) \
74 FUNC(HAS_DOTDOT, WARN) \
75 FUNC(HAS_DOTGIT, WARN) \
76 FUNC(NULL_SHA1, WARN) \
77 FUNC(ZERO_PADDED_FILEMODE, WARN) \
78 FUNC(NUL_IN_COMMIT, WARN) \
79 /* infos (reported as warnings, but ignored by default) */ \
80 FUNC(GITMODULES_PARSE, INFO) \
81 FUNC(BAD_TAG_NAME, INFO) \
82 FUNC(MISSING_TAGGER_ENTRY, INFO)
84 #define MSG_ID(id, msg_type) FSCK_MSG_##id,
86 FOREACH_MSG_ID(MSG_ID)
92 #define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
94 const char *id_string;
95 const char *downcased;
96 const char *camelcased;
98 } msg_id_info[FSCK_MSG_MAX + 1] = {
99 FOREACH_MSG_ID(MSG_ID)
100 { NULL, NULL, NULL, -1 }
104 static void prepare_msg_ids(void)
108 if (msg_id_info[0].downcased)
111 /* convert id_string to lower case, without underscores. */
112 for (i = 0; i < FSCK_MSG_MAX; i++) {
113 const char *p = msg_id_info[i].id_string;
115 char *q = xmalloc(len);
117 msg_id_info[i].downcased = q;
122 *(q)++ = tolower(*(p)++);
125 p = msg_id_info[i].id_string;
127 msg_id_info[i].camelcased = q;
134 *q++ = tolower(*p++);
141 static int parse_msg_id(const char *text)
147 for (i = 0; i < FSCK_MSG_MAX; i++)
148 if (!strcmp(text, msg_id_info[i].downcased))
154 void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
160 for (i = 0; i < FSCK_MSG_MAX; i++)
161 list_config_item(list, prefix, msg_id_info[i].camelcased);
164 static int fsck_msg_type(enum fsck_msg_id msg_id,
165 struct fsck_options *options)
169 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
171 if (options->msg_type)
172 msg_type = options->msg_type[msg_id];
174 msg_type = msg_id_info[msg_id].msg_type;
175 if (options->strict && msg_type == FSCK_WARN)
176 msg_type = FSCK_ERROR;
182 static int parse_msg_type(const char *str)
184 if (!strcmp(str, "error"))
186 else if (!strcmp(str, "warn"))
188 else if (!strcmp(str, "ignore"))
191 die("Unknown fsck message type: '%s'", str);
194 int is_valid_msg_type(const char *msg_id, const char *msg_type)
196 if (parse_msg_id(msg_id) < 0)
198 parse_msg_type(msg_type);
202 void fsck_set_msg_type(struct fsck_options *options,
203 const char *msg_id, const char *msg_type)
205 int id = parse_msg_id(msg_id), type;
208 die("Unhandled message id: %s", msg_id);
209 type = parse_msg_type(msg_type);
211 if (type != FSCK_ERROR && msg_id_info[id].msg_type == FSCK_FATAL)
212 die("Cannot demote %s to %s", msg_id, msg_type);
214 if (!options->msg_type) {
217 ALLOC_ARRAY(msg_type, FSCK_MSG_MAX);
218 for (i = 0; i < FSCK_MSG_MAX; i++)
219 msg_type[i] = fsck_msg_type(i, options);
220 options->msg_type = msg_type;
223 options->msg_type[id] = type;
226 void fsck_set_msg_types(struct fsck_options *options, const char *values)
228 char *buf = xstrdup(values), *to_free = buf;
232 int len = strcspn(buf, " ,|"), equal;
242 equal < len && buf[equal] != '=' && buf[equal] != ':';
244 buf[equal] = tolower(buf[equal]);
247 if (!strcmp(buf, "skiplist")) {
249 die("skiplist requires a path");
250 oidset_parse_file(&options->skiplist, buf + equal + 1);
256 die("Missing '=': '%s'", buf);
258 fsck_set_msg_type(options, buf, buf + equal + 1);
264 static void append_msg_id(struct strbuf *sb, const char *msg_id)
267 char c = *(msg_id)++;
272 strbuf_addch(sb, tolower(c));
275 strbuf_addch(sb, *(msg_id)++);
279 strbuf_addstr(sb, ": ");
282 static int object_on_skiplist(struct fsck_options *opts,
283 const struct object_id *oid)
285 return opts && oid && oidset_contains(&opts->skiplist, oid);
288 __attribute__((format (printf, 5, 6)))
289 static int report(struct fsck_options *options,
290 const struct object_id *oid, enum object_type object_type,
291 enum fsck_msg_id id, const char *fmt, ...)
294 struct strbuf sb = STRBUF_INIT;
295 int msg_type = fsck_msg_type(id, options), result;
297 if (msg_type == FSCK_IGNORE)
300 if (object_on_skiplist(options, oid))
303 if (msg_type == FSCK_FATAL)
304 msg_type = FSCK_ERROR;
305 else if (msg_type == FSCK_INFO)
306 msg_type = FSCK_WARN;
308 append_msg_id(&sb, msg_id_info[id].id_string);
311 strbuf_vaddf(&sb, fmt, ap);
312 result = options->error_func(options, oid, object_type,
320 void fsck_enable_object_names(struct fsck_options *options)
322 if (!options->object_names)
323 options->object_names = kh_init_oid_map();
326 const char *fsck_get_object_name(struct fsck_options *options,
327 const struct object_id *oid)
330 if (!options->object_names)
332 pos = kh_get_oid_map(options->object_names, *oid);
333 if (pos >= kh_end(options->object_names))
335 return kh_value(options->object_names, pos);
338 void fsck_put_object_name(struct fsck_options *options,
339 const struct object_id *oid,
340 const char *fmt, ...)
343 struct strbuf buf = STRBUF_INIT;
347 if (!options->object_names)
350 pos = kh_put_oid_map(options->object_names, *oid, &hashret);
354 strbuf_vaddf(&buf, fmt, ap);
355 kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL);
359 const char *fsck_describe_object(struct fsck_options *options,
360 const struct object_id *oid)
362 static struct strbuf bufs[] = {
363 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
367 const char *name = fsck_get_object_name(options, oid);
370 b = (b + 1) % ARRAY_SIZE(bufs);
372 strbuf_addstr(buf, oid_to_hex(oid));
374 strbuf_addf(buf, " (%s)", name);
379 static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
381 struct tree_desc desc;
382 struct name_entry entry;
386 if (parse_tree(tree))
389 name = fsck_get_object_name(options, &tree->object.oid);
390 if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
392 while (tree_entry_gently(&desc, &entry)) {
396 if (S_ISGITLINK(entry.mode))
399 if (S_ISDIR(entry.mode)) {
400 obj = (struct object *)lookup_tree(the_repository, &entry.oid);
402 fsck_put_object_name(options, &entry.oid, "%s%s/",
404 result = options->walk(obj, OBJ_TREE, data, options);
406 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
407 obj = (struct object *)lookup_blob(the_repository, &entry.oid);
409 fsck_put_object_name(options, &entry.oid, "%s%s",
411 result = options->walk(obj, OBJ_BLOB, data, options);
414 result = error("in tree %s: entry %s has bad mode %.6o",
415 fsck_describe_object(options, &tree->object.oid),
416 entry.path, entry.mode);
426 static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
428 int counter = 0, generation = 0, name_prefix_len = 0;
429 struct commit_list *parents;
434 if (parse_commit(commit))
437 name = fsck_get_object_name(options, &commit->object.oid);
439 fsck_put_object_name(options, get_commit_tree_oid(commit),
442 result = options->walk((struct object *)get_commit_tree(commit),
443 OBJ_TREE, data, options);
448 parents = commit->parents;
449 if (name && parents) {
450 int len = strlen(name), power;
452 if (len && name[len - 1] == '^') {
454 name_prefix_len = len - 1;
456 else { /* parse ~<generation> suffix */
457 for (generation = 0, power = 1;
458 len && isdigit(name[len - 1]);
460 generation += power * (name[--len] - '0');
461 if (power > 1 && len && name[len - 1] == '~')
462 name_prefix_len = len - 1;
468 struct object_id *oid = &parents->item->object.oid;
471 fsck_put_object_name(options, oid, "%s^%d",
473 else if (generation > 0)
474 fsck_put_object_name(options, oid, "%.*s~%d",
475 name_prefix_len, name,
478 fsck_put_object_name(options, oid, "%s^", name);
480 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
485 parents = parents->next;
490 static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
492 const char *name = fsck_get_object_name(options, &tag->object.oid);
497 fsck_put_object_name(options, &tag->tagged->oid, "%s", name);
498 return options->walk(tag->tagged, OBJ_ANY, data, options);
501 int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
506 if (obj->type == OBJ_NONE)
507 parse_object(the_repository, &obj->oid);
513 return fsck_walk_tree((struct tree *)obj, data, options);
515 return fsck_walk_commit((struct commit *)obj, data, options);
517 return fsck_walk_tag((struct tag *)obj, data, options);
519 error("Unknown object type for %s",
520 fsck_describe_object(options, &obj->oid));
526 * The entries in a tree are ordered in the _path_ order,
527 * which means that a directory entry is ordered by adding
528 * a slash to the end of it.
530 * So a directory called "a" is ordered _after_ a file
531 * called "a.c", because "a/" sorts after "a.c".
533 #define TREE_UNORDERED (-1)
534 #define TREE_HAS_DUPS (-2)
536 static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
538 int len1 = strlen(name1);
539 int len2 = strlen(name2);
540 int len = len1 < len2 ? len1 : len2;
541 unsigned char c1, c2;
544 cmp = memcmp(name1, name2, len);
548 return TREE_UNORDERED;
551 * Ok, the first <len> characters are the same.
552 * Now we need to order the next one, but turn
553 * a '\0' into a '/' for a directory entry.
559 * git-write-tree used to write out a nonsense tree that has
560 * entries with the same name, one blob and one tree. Make
561 * sure we do not have duplicate entries.
563 return TREE_HAS_DUPS;
564 if (!c1 && S_ISDIR(mode1))
566 if (!c2 && S_ISDIR(mode2))
568 return c1 < c2 ? 0 : TREE_UNORDERED;
571 static int fsck_tree(const struct object_id *oid,
572 const char *buffer, unsigned long size,
573 struct fsck_options *options)
576 int has_null_sha1 = 0;
577 int has_full_path = 0;
578 int has_empty_name = 0;
582 int has_zero_pad = 0;
583 int has_bad_modes = 0;
584 int has_dup_entries = 0;
585 int not_properly_sorted = 0;
586 struct tree_desc desc;
590 if (init_tree_desc_gently(&desc, buffer, size)) {
591 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
600 const char *name, *backslash;
601 const struct object_id *oid;
603 oid = tree_entry_extract(&desc, &name, &mode);
605 has_null_sha1 |= is_null_oid(oid);
606 has_full_path |= !!strchr(name, '/');
607 has_empty_name |= !*name;
608 has_dot |= !strcmp(name, ".");
609 has_dotdot |= !strcmp(name, "..");
610 has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
611 has_zero_pad |= *(char *)desc.buffer == '0';
613 if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
615 oidset_insert(&gitmodules_found, oid);
617 retval += report(options,
619 FSCK_MSG_GITMODULES_SYMLINK,
620 ".gitmodules is a symbolic link");
623 if ((backslash = strchr(name, '\\'))) {
626 has_dotgit |= is_ntfs_dotgit(backslash);
627 if (is_ntfs_dotgitmodules(backslash)) {
629 oidset_insert(&gitmodules_found, oid);
631 retval += report(options, oid, OBJ_TREE,
632 FSCK_MSG_GITMODULES_SYMLINK,
633 ".gitmodules is a symbolic link");
635 backslash = strchr(backslash, '\\');
639 if (update_tree_entry_gently(&desc)) {
640 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
655 * This is nonstandard, but we had a few of these
656 * early on when we honored the full set of mode
660 if (!options->strict)
668 switch (verify_ordered(o_mode, o_name, mode, name)) {
670 not_properly_sorted = 1;
685 retval += report(options, oid, OBJ_TREE, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
687 retval += report(options, oid, OBJ_TREE, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
689 retval += report(options, oid, OBJ_TREE, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
691 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOT, "contains '.'");
693 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTDOT, "contains '..'");
695 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
697 retval += report(options, oid, OBJ_TREE, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
699 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
701 retval += report(options, oid, OBJ_TREE, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries");
702 if (not_properly_sorted)
703 retval += report(options, oid, OBJ_TREE, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted");
707 static int verify_headers(const void *data, unsigned long size,
708 const struct object_id *oid, enum object_type type,
709 struct fsck_options *options)
711 const char *buffer = (const char *)data;
714 for (i = 0; i < size; i++) {
717 return report(options, oid, type,
718 FSCK_MSG_NUL_IN_HEADER,
719 "unterminated header: NUL at offset %ld", i);
721 if (i + 1 < size && buffer[i + 1] == '\n')
727 * We did not find double-LF that separates the header
728 * and the body. Not having a body is not a crime but
729 * we do want to see the terminating LF for the last header
732 if (size && buffer[size - 1] == '\n')
735 return report(options, oid, type,
736 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
739 static int fsck_ident(const char **ident,
740 const struct object_id *oid, enum object_type type,
741 struct fsck_options *options)
743 const char *p = *ident;
746 *ident = strchrnul(*ident, '\n');
751 return report(options, oid, type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
752 p += strcspn(p, "<>\n");
754 return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
756 return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
758 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
760 p += strcspn(p, "<>\n");
762 return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
765 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
767 if (*p == '0' && p[1] != ' ')
768 return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
769 if (date_overflows(parse_timestamp(p, &end, 10)))
770 return report(options, oid, type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
771 if ((end == p || *end != ' '))
772 return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
774 if ((*p != '+' && *p != '-') ||
780 return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
785 static int fsck_commit(const struct object_id *oid,
786 const char *buffer, unsigned long size,
787 struct fsck_options *options)
789 struct object_id tree_oid, parent_oid;
790 unsigned author_count;
792 const char *buffer_begin = buffer;
795 if (verify_headers(buffer, size, oid, OBJ_COMMIT, options))
798 if (!skip_prefix(buffer, "tree ", &buffer))
799 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
800 if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
801 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
806 while (skip_prefix(buffer, "parent ", &buffer)) {
807 if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') {
808 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
815 while (skip_prefix(buffer, "author ", &buffer)) {
817 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
821 if (author_count < 1)
822 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
823 else if (author_count > 1)
824 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
827 if (!skip_prefix(buffer, "committer ", &buffer))
828 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
829 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
832 if (memchr(buffer_begin, '\0', size)) {
833 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT,
834 "NUL byte in the commit object body");
841 static int fsck_tag(const struct object_id *oid, const char *buffer,
842 unsigned long size, struct fsck_options *options)
844 struct object_id tagged_oid;
847 struct strbuf sb = STRBUF_INIT;
850 ret = verify_headers(buffer, size, oid, OBJ_TAG, options);
854 if (!skip_prefix(buffer, "object ", &buffer)) {
855 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
858 if (parse_oid_hex(buffer, &tagged_oid, &p) || *p != '\n') {
859 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
865 if (!skip_prefix(buffer, "type ", &buffer)) {
866 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
869 eol = strchr(buffer, '\n');
871 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
874 if (type_from_string_gently(buffer, eol - buffer, 1) < 0)
875 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
880 if (!skip_prefix(buffer, "tag ", &buffer)) {
881 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
884 eol = strchr(buffer, '\n');
886 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
889 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
890 if (check_refname_format(sb.buf, 0)) {
891 ret = report(options, oid, OBJ_TAG,
892 FSCK_MSG_BAD_TAG_NAME,
893 "invalid 'tag' name: %.*s",
894 (int)(eol - buffer), buffer);
900 if (!skip_prefix(buffer, "tagger ", &buffer)) {
901 /* early tags do not contain 'tagger' lines; warn only */
902 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
907 ret = fsck_ident(&buffer, oid, OBJ_TAG, options);
914 static int check_submodule_url(const char *url)
916 struct credential c = CREDENTIAL_INIT;
919 if (looks_like_command_line_option(url))
922 ret = credential_from_url_gently(&c, url, 1);
923 credential_clear(&c);
927 struct fsck_gitmodules_data {
928 const struct object_id *oid;
929 struct fsck_options *options;
933 static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
935 struct fsck_gitmodules_data *data = vdata;
936 const char *subsection, *key;
940 if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
944 name = xmemdupz(subsection, subsection_len);
945 if (check_submodule_name(name) < 0)
946 data->ret |= report(data->options,
948 FSCK_MSG_GITMODULES_NAME,
949 "disallowed submodule name: %s",
951 if (!strcmp(key, "url") && value &&
952 check_submodule_url(value) < 0)
953 data->ret |= report(data->options,
955 FSCK_MSG_GITMODULES_URL,
956 "disallowed submodule url: %s",
958 if (!strcmp(key, "path") && value &&
959 looks_like_command_line_option(value))
960 data->ret |= report(data->options,
962 FSCK_MSG_GITMODULES_PATH,
963 "disallowed submodule path: %s",
965 if (!strcmp(key, "update") && value &&
966 parse_submodule_update_type(value) == SM_UPDATE_COMMAND)
967 data->ret |= report(data->options, data->oid, OBJ_BLOB,
968 FSCK_MSG_GITMODULES_UPDATE,
969 "disallowed submodule update setting: %s",
976 static int fsck_blob(const struct object_id *oid, const char *buf,
977 unsigned long size, struct fsck_options *options)
979 struct fsck_gitmodules_data data;
980 struct config_options config_opts = { 0 };
982 if (!oidset_contains(&gitmodules_found, oid))
984 oidset_insert(&gitmodules_done, oid);
986 if (object_on_skiplist(options, oid))
991 * A missing buffer here is a sign that the caller found the
992 * blob too gigantic to load into memory. Let's just consider
995 return report(options, oid, OBJ_BLOB,
996 FSCK_MSG_GITMODULES_LARGE,
997 ".gitmodules too large to parse");
1001 data.options = options;
1003 config_opts.error_action = CONFIG_ERROR_SILENT;
1004 if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
1005 ".gitmodules", buf, size, &data, &config_opts))
1006 data.ret |= report(options, oid, OBJ_BLOB,
1007 FSCK_MSG_GITMODULES_PARSE,
1008 "could not parse gitmodules blob");
1013 int fsck_object(struct object *obj, void *data, unsigned long size,
1014 struct fsck_options *options)
1017 return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
1019 if (obj->type == OBJ_BLOB)
1020 return fsck_blob(&obj->oid, data, size, options);
1021 if (obj->type == OBJ_TREE)
1022 return fsck_tree(&obj->oid, data, size, options);
1023 if (obj->type == OBJ_COMMIT)
1024 return fsck_commit(&obj->oid, data, size, options);
1025 if (obj->type == OBJ_TAG)
1026 return fsck_tag(&obj->oid, data, size, options);
1028 return report(options, &obj->oid, obj->type,
1029 FSCK_MSG_UNKNOWN_TYPE,
1030 "unknown type '%d' (internal fsck error)",
1034 int fsck_error_function(struct fsck_options *o,
1035 const struct object_id *oid,
1036 enum object_type object_type,
1037 int msg_type, const char *message)
1039 if (msg_type == FSCK_WARN) {
1040 warning("object %s: %s", fsck_describe_object(o, oid), message);
1043 error("object %s: %s", fsck_describe_object(o, oid), message);
1047 int fsck_finish(struct fsck_options *options)
1050 struct oidset_iter iter;
1051 const struct object_id *oid;
1053 oidset_iter_init(&gitmodules_found, &iter);
1054 while ((oid = oidset_iter_next(&iter))) {
1055 enum object_type type;
1059 if (oidset_contains(&gitmodules_done, oid))
1062 buf = read_object_file(oid, &type, &size);
1064 if (is_promisor_object(oid))
1066 ret |= report(options,
1068 FSCK_MSG_GITMODULES_MISSING,
1069 "unable to read .gitmodules blob");
1073 if (type == OBJ_BLOB)
1074 ret |= fsck_blob(oid, buf, size, options);
1076 ret |= report(options,
1078 FSCK_MSG_GITMODULES_BLOB,
1079 "non-blob found at .gitmodules");
1084 oidset_clear(&gitmodules_found);
1085 oidset_clear(&gitmodules_done);