Git 2.25.3
[alt-git.git] / fsck.c
blobdc6f6ac3de193dec0854b8b2242de1544abd1de0
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 "credential.h"
19 #include "help.h"
21 static struct oidset gitmodules_found = OIDSET_INIT;
22 static struct oidset gitmodules_done = OIDSET_INIT;
24 #define FSCK_FATAL -1
25 #define FSCK_INFO -2
27 #define FOREACH_MSG_ID(FUNC) \
28 /* fatal errors */ \
29 FUNC(NUL_IN_HEADER, FATAL) \
30 FUNC(UNTERMINATED_HEADER, FATAL) \
31 /* errors */ \
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) \
69 /* warnings */ \
70 FUNC(BAD_FILEMODE, WARN) \
71 FUNC(EMPTY_NAME, WARN) \
72 FUNC(FULL_PATHNAME, WARN) \
73 FUNC(HAS_DOT, 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,
85 enum fsck_msg_id {
86 FOREACH_MSG_ID(MSG_ID)
87 FSCK_MSG_MAX
89 #undef MSG_ID
91 #define STR(x) #x
92 #define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
93 static struct {
94 const char *id_string;
95 const char *downcased;
96 const char *camelcased;
97 int msg_type;
98 } msg_id_info[FSCK_MSG_MAX + 1] = {
99 FOREACH_MSG_ID(MSG_ID)
100 { NULL, NULL, NULL, -1 }
102 #undef MSG_ID
104 static void prepare_msg_ids(void)
106 int i;
108 if (msg_id_info[0].downcased)
109 return;
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;
114 int len = strlen(p);
115 char *q = xmalloc(len);
117 msg_id_info[i].downcased = q;
118 while (*p)
119 if (*p == '_')
120 p++;
121 else
122 *(q)++ = tolower(*(p)++);
123 *q = '\0';
125 p = msg_id_info[i].id_string;
126 q = xmalloc(len);
127 msg_id_info[i].camelcased = q;
128 while (*p) {
129 if (*p == '_') {
130 p++;
131 if (*p)
132 *q++ = *p++;
133 } else {
134 *q++ = tolower(*p++);
137 *q = '\0';
141 static int parse_msg_id(const char *text)
143 int i;
145 prepare_msg_ids();
147 for (i = 0; i < FSCK_MSG_MAX; i++)
148 if (!strcmp(text, msg_id_info[i].downcased))
149 return i;
151 return -1;
154 void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
156 int i;
158 prepare_msg_ids();
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)
167 int msg_type;
169 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
171 if (options->msg_type)
172 msg_type = options->msg_type[msg_id];
173 else {
174 msg_type = msg_id_info[msg_id].msg_type;
175 if (options->strict && msg_type == FSCK_WARN)
176 msg_type = FSCK_ERROR;
179 return msg_type;
182 static int parse_msg_type(const char *str)
184 if (!strcmp(str, "error"))
185 return FSCK_ERROR;
186 else if (!strcmp(str, "warn"))
187 return FSCK_WARN;
188 else if (!strcmp(str, "ignore"))
189 return FSCK_IGNORE;
190 else
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)
197 return 0;
198 parse_msg_type(msg_type);
199 return 1;
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;
207 if (id < 0)
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) {
215 int i;
216 int *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;
229 int done = 0;
231 while (!done) {
232 int len = strcspn(buf, " ,|"), equal;
234 done = !buf[len];
235 if (!len) {
236 buf++;
237 continue;
239 buf[len] = '\0';
241 for (equal = 0;
242 equal < len && buf[equal] != '=' && buf[equal] != ':';
243 equal++)
244 buf[equal] = tolower(buf[equal]);
245 buf[equal] = '\0';
247 if (!strcmp(buf, "skiplist")) {
248 if (equal == len)
249 die("skiplist requires a path");
250 oidset_parse_file(&options->skiplist, buf + equal + 1);
251 buf += len + 1;
252 continue;
255 if (equal == len)
256 die("Missing '=': '%s'", buf);
258 fsck_set_msg_type(options, buf, buf + equal + 1);
259 buf += len + 1;
261 free(to_free);
264 static void append_msg_id(struct strbuf *sb, const char *msg_id)
266 for (;;) {
267 char c = *(msg_id)++;
269 if (!c)
270 break;
271 if (c != '_')
272 strbuf_addch(sb, tolower(c));
273 else {
274 assert(*msg_id);
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, ...)
293 va_list ap;
294 struct strbuf sb = STRBUF_INIT;
295 int msg_type = fsck_msg_type(id, options), result;
297 if (msg_type == FSCK_IGNORE)
298 return 0;
300 if (object_on_skiplist(options, oid))
301 return 0;
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);
310 va_start(ap, fmt);
311 strbuf_vaddf(&sb, fmt, ap);
312 result = options->error_func(options, oid, object_type,
313 msg_type, sb.buf);
314 strbuf_release(&sb);
315 va_end(ap);
317 return result;
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)
329 khiter_t pos;
330 if (!options->object_names)
331 return NULL;
332 pos = kh_get_oid_map(options->object_names, *oid);
333 if (pos >= kh_end(options->object_names))
334 return NULL;
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, ...)
342 va_list ap;
343 struct strbuf buf = STRBUF_INIT;
344 khiter_t pos;
345 int hashret;
347 if (!options->object_names)
348 return;
350 pos = kh_put_oid_map(options->object_names, *oid, &hashret);
351 if (!hashret)
352 return;
353 va_start(ap, fmt);
354 strbuf_vaddf(&buf, fmt, ap);
355 kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL);
356 va_end(ap);
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
365 static int b = 0;
366 struct strbuf *buf;
367 const char *name = fsck_get_object_name(options, oid);
369 buf = bufs + b;
370 b = (b + 1) % ARRAY_SIZE(bufs);
371 strbuf_reset(buf);
372 strbuf_addstr(buf, oid_to_hex(oid));
373 if (name)
374 strbuf_addf(buf, " (%s)", name);
376 return buf->buf;
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;
383 int res = 0;
384 const char *name;
386 if (parse_tree(tree))
387 return -1;
389 name = fsck_get_object_name(options, &tree->object.oid);
390 if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
391 return -1;
392 while (tree_entry_gently(&desc, &entry)) {
393 struct object *obj;
394 int result;
396 if (S_ISGITLINK(entry.mode))
397 continue;
399 if (S_ISDIR(entry.mode)) {
400 obj = (struct object *)lookup_tree(the_repository, &entry.oid);
401 if (name && obj)
402 fsck_put_object_name(options, &entry.oid, "%s%s/",
403 name, entry.path);
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);
408 if (name && obj)
409 fsck_put_object_name(options, &entry.oid, "%s%s",
410 name, entry.path);
411 result = options->walk(obj, OBJ_BLOB, data, options);
413 else {
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);
418 if (result < 0)
419 return result;
420 if (!res)
421 res = result;
423 return res;
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;
430 int res;
431 int result;
432 const char *name;
434 if (parse_commit(commit))
435 return -1;
437 name = fsck_get_object_name(options, &commit->object.oid);
438 if (name)
439 fsck_put_object_name(options, get_commit_tree_oid(commit),
440 "%s:", name);
442 result = options->walk((struct object *)get_commit_tree(commit),
443 OBJ_TREE, data, options);
444 if (result < 0)
445 return result;
446 res = result;
448 parents = commit->parents;
449 if (name && parents) {
450 int len = strlen(name), power;
452 if (len && name[len - 1] == '^') {
453 generation = 1;
454 name_prefix_len = len - 1;
456 else { /* parse ~<generation> suffix */
457 for (generation = 0, power = 1;
458 len && isdigit(name[len - 1]);
459 power *= 10)
460 generation += power * (name[--len] - '0');
461 if (power > 1 && len && name[len - 1] == '~')
462 name_prefix_len = len - 1;
466 while (parents) {
467 if (name) {
468 struct object_id *oid = &parents->item->object.oid;
470 if (counter++)
471 fsck_put_object_name(options, oid, "%s^%d",
472 name, counter);
473 else if (generation > 0)
474 fsck_put_object_name(options, oid, "%.*s~%d",
475 name_prefix_len, name,
476 generation + 1);
477 else
478 fsck_put_object_name(options, oid, "%s^", name);
480 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
481 if (result < 0)
482 return result;
483 if (!res)
484 res = result;
485 parents = parents->next;
487 return res;
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);
494 if (parse_tag(tag))
495 return -1;
496 if (name)
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)
503 if (!obj)
504 return -1;
506 if (obj->type == OBJ_NONE)
507 parse_object(the_repository, &obj->oid);
509 switch (obj->type) {
510 case OBJ_BLOB:
511 return 0;
512 case OBJ_TREE:
513 return fsck_walk_tree((struct tree *)obj, data, options);
514 case OBJ_COMMIT:
515 return fsck_walk_commit((struct commit *)obj, data, options);
516 case OBJ_TAG:
517 return fsck_walk_tag((struct tag *)obj, data, options);
518 default:
519 error("Unknown object type for %s",
520 fsck_describe_object(options, &obj->oid));
521 return -1;
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;
542 int cmp;
544 cmp = memcmp(name1, name2, len);
545 if (cmp < 0)
546 return 0;
547 if (cmp > 0)
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.
555 c1 = name1[len];
556 c2 = name2[len];
557 if (!c1 && !c2)
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))
565 c1 = '/';
566 if (!c2 && S_ISDIR(mode2))
567 c2 = '/';
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)
575 int retval = 0;
576 int has_null_sha1 = 0;
577 int has_full_path = 0;
578 int has_empty_name = 0;
579 int has_dot = 0;
580 int has_dotdot = 0;
581 int has_dotgit = 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;
587 unsigned o_mode;
588 const char *o_name;
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");
592 return retval;
595 o_mode = 0;
596 o_name = NULL;
598 while (desc.size) {
599 unsigned short mode;
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)) {
614 if (!S_ISLNK(mode))
615 oidset_insert(&gitmodules_found, oid);
616 else
617 retval += report(options,
618 oid, OBJ_TREE,
619 FSCK_MSG_GITMODULES_SYMLINK,
620 ".gitmodules is a symbolic link");
623 if ((backslash = strchr(name, '\\'))) {
624 while (backslash) {
625 backslash++;
626 has_dotgit |= is_ntfs_dotgit(backslash);
627 if (is_ntfs_dotgitmodules(backslash)) {
628 if (!S_ISLNK(mode))
629 oidset_insert(&gitmodules_found, oid);
630 else
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");
641 break;
644 switch (mode) {
646 * Standard modes..
648 case S_IFREG | 0755:
649 case S_IFREG | 0644:
650 case S_IFLNK:
651 case S_IFDIR:
652 case S_IFGITLINK:
653 break;
655 * This is nonstandard, but we had a few of these
656 * early on when we honored the full set of mode
657 * bits..
659 case S_IFREG | 0664:
660 if (!options->strict)
661 break;
662 /* fallthrough */
663 default:
664 has_bad_modes = 1;
667 if (o_name) {
668 switch (verify_ordered(o_mode, o_name, mode, name)) {
669 case TREE_UNORDERED:
670 not_properly_sorted = 1;
671 break;
672 case TREE_HAS_DUPS:
673 has_dup_entries = 1;
674 break;
675 default:
676 break;
680 o_mode = mode;
681 o_name = name;
684 if (has_null_sha1)
685 retval += report(options, oid, OBJ_TREE, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
686 if (has_full_path)
687 retval += report(options, oid, OBJ_TREE, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
688 if (has_empty_name)
689 retval += report(options, oid, OBJ_TREE, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
690 if (has_dot)
691 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOT, "contains '.'");
692 if (has_dotdot)
693 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTDOT, "contains '..'");
694 if (has_dotgit)
695 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
696 if (has_zero_pad)
697 retval += report(options, oid, OBJ_TREE, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
698 if (has_bad_modes)
699 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
700 if (has_dup_entries)
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");
704 return retval;
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;
712 unsigned long i;
714 for (i = 0; i < size; i++) {
715 switch (buffer[i]) {
716 case '\0':
717 return report(options, oid, type,
718 FSCK_MSG_NUL_IN_HEADER,
719 "unterminated header: NUL at offset %ld", i);
720 case '\n':
721 if (i + 1 < size && buffer[i + 1] == '\n')
722 return 0;
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
730 * line.
732 if (size && buffer[size - 1] == '\n')
733 return 0;
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;
744 char *end;
746 *ident = strchrnul(*ident, '\n');
747 if (**ident == '\n')
748 (*ident)++;
750 if (*p == '<')
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");
753 if (*p == '>')
754 return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
755 if (*p != '<')
756 return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
757 if (p[-1] != ' ')
758 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
759 p++;
760 p += strcspn(p, "<>\n");
761 if (*p != '>')
762 return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
763 p++;
764 if (*p != ' ')
765 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
766 p++;
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");
773 p = end + 1;
774 if ((*p != '+' && *p != '-') ||
775 !isdigit(p[1]) ||
776 !isdigit(p[2]) ||
777 !isdigit(p[3]) ||
778 !isdigit(p[4]) ||
779 (p[5] != '\n'))
780 return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
781 p += 6;
782 return 0;
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;
791 int err;
792 const char *buffer_begin = buffer;
793 const char *p;
795 if (verify_headers(buffer, size, oid, OBJ_COMMIT, options))
796 return -1;
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");
802 if (err)
803 return err;
805 buffer = p + 1;
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");
809 if (err)
810 return err;
812 buffer = p + 1;
814 author_count = 0;
815 while (skip_prefix(buffer, "author ", &buffer)) {
816 author_count++;
817 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
818 if (err)
819 return err;
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");
825 if (err)
826 return err;
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);
830 if (err)
831 return err;
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");
835 if (err)
836 return err;
838 return 0;
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;
845 int ret = 0;
846 char *eol;
847 struct strbuf sb = STRBUF_INIT;
848 const char *p;
850 ret = verify_headers(buffer, size, oid, OBJ_TAG, options);
851 if (ret)
852 goto done;
854 if (!skip_prefix(buffer, "object ", &buffer)) {
855 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
856 goto done;
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");
860 if (ret)
861 goto done;
863 buffer = p + 1;
865 if (!skip_prefix(buffer, "type ", &buffer)) {
866 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
867 goto done;
869 eol = strchr(buffer, '\n');
870 if (!eol) {
871 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
872 goto done;
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");
876 if (ret)
877 goto done;
878 buffer = eol + 1;
880 if (!skip_prefix(buffer, "tag ", &buffer)) {
881 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
882 goto done;
884 eol = strchr(buffer, '\n');
885 if (!eol) {
886 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
887 goto done;
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);
895 if (ret)
896 goto done;
898 buffer = eol + 1;
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");
903 if (ret)
904 goto done;
906 else
907 ret = fsck_ident(&buffer, oid, OBJ_TAG, options);
909 done:
910 strbuf_release(&sb);
911 return ret;
914 static int check_submodule_url(const char *url)
916 struct credential c = CREDENTIAL_INIT;
917 int ret;
919 if (looks_like_command_line_option(url))
920 return -1;
922 ret = credential_from_url_gently(&c, url, 1);
923 credential_clear(&c);
924 return ret;
927 struct fsck_gitmodules_data {
928 const struct object_id *oid;
929 struct fsck_options *options;
930 int ret;
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;
937 int subsection_len;
938 char *name;
940 if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
941 !subsection)
942 return 0;
944 name = xmemdupz(subsection, subsection_len);
945 if (check_submodule_name(name) < 0)
946 data->ret |= report(data->options,
947 data->oid, OBJ_BLOB,
948 FSCK_MSG_GITMODULES_NAME,
949 "disallowed submodule name: %s",
950 name);
951 if (!strcmp(key, "url") && value &&
952 check_submodule_url(value) < 0)
953 data->ret |= report(data->options,
954 data->oid, OBJ_BLOB,
955 FSCK_MSG_GITMODULES_URL,
956 "disallowed submodule url: %s",
957 value);
958 if (!strcmp(key, "path") && value &&
959 looks_like_command_line_option(value))
960 data->ret |= report(data->options,
961 data->oid, OBJ_BLOB,
962 FSCK_MSG_GITMODULES_PATH,
963 "disallowed submodule path: %s",
964 value);
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",
970 value);
971 free(name);
973 return 0;
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))
983 return 0;
984 oidset_insert(&gitmodules_done, oid);
986 if (object_on_skiplist(options, oid))
987 return 0;
989 if (!buf) {
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
993 * that an error.
995 return report(options, oid, OBJ_BLOB,
996 FSCK_MSG_GITMODULES_LARGE,
997 ".gitmodules too large to parse");
1000 data.oid = oid;
1001 data.options = options;
1002 data.ret = 0;
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");
1010 return data.ret;
1013 int fsck_object(struct object *obj, void *data, unsigned long size,
1014 struct fsck_options *options)
1016 if (!obj)
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)",
1031 obj->type);
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);
1041 return 0;
1043 error("object %s: %s", fsck_describe_object(o, oid), message);
1044 return 1;
1047 int fsck_finish(struct fsck_options *options)
1049 int ret = 0;
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;
1056 unsigned long size;
1057 char *buf;
1059 if (oidset_contains(&gitmodules_done, oid))
1060 continue;
1062 buf = read_object_file(oid, &type, &size);
1063 if (!buf) {
1064 if (is_promisor_object(oid))
1065 continue;
1066 ret |= report(options,
1067 oid, OBJ_BLOB,
1068 FSCK_MSG_GITMODULES_MISSING,
1069 "unable to read .gitmodules blob");
1070 continue;
1073 if (type == OBJ_BLOB)
1074 ret |= fsck_blob(oid, buf, size, options);
1075 else
1076 ret |= report(options,
1077 oid, type,
1078 FSCK_MSG_GITMODULES_BLOB,
1079 "non-blob found at .gitmodules");
1080 free(buf);
1084 oidset_clear(&gitmodules_found);
1085 oidset_clear(&gitmodules_done);
1086 return ret;