Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 1 | #include "cache.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 2 | #include "object-store.h" |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 3 | #include "repository.h" |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 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" |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 11 | #include "refs.h" |
Jeff King | a18fcc9 | 2014-12-15 18:21:57 -0500 | [diff] [blame] | 12 | #include "utf8.h" |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 13 | #include "decorate.h" |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 14 | #include "oidset.h" |
Jeff King | 2738744 | 2018-05-14 12:22:48 -0400 | [diff] [blame] | 15 | #include "packfile.h" |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 16 | #include "submodule-config.h" |
| 17 | #include "config.h" |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 18 | #include "help.h" |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 19 | |
| 20 | static struct oidset gitmodules_found = OIDSET_INIT; |
| 21 | static struct oidset gitmodules_done = OIDSET_INIT; |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 22 | |
Johannes Schindelin | f50c440 | 2015-06-22 17:26:42 +0200 | [diff] [blame] | 23 | #define FSCK_FATAL -1 |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 24 | #define FSCK_INFO -2 |
Johannes Schindelin | f50c440 | 2015-06-22 17:26:42 +0200 | [diff] [blame] | 25 | |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 26 | #define FOREACH_MSG_ID(FUNC) \ |
Johannes Schindelin | f50c440 | 2015-06-22 17:26:42 +0200 | [diff] [blame] | 27 | /* fatal errors */ \ |
| 28 | FUNC(NUL_IN_HEADER, FATAL) \ |
| 29 | FUNC(UNTERMINATED_HEADER, FATAL) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 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) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 46 | FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \ |
| 47 | FUNC(MISSING_OBJECT, ERROR) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 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) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 52 | FUNC(MISSING_TREE, ERROR) \ |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 53 | FUNC(MISSING_TREE_OBJECT, ERROR) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 54 | FUNC(MISSING_TYPE, ERROR) \ |
| 55 | FUNC(MISSING_TYPE_ENTRY, ERROR) \ |
Johannes Schindelin | c9ad147 | 2015-06-22 17:26:23 +0200 | [diff] [blame] | 56 | FUNC(MULTIPLE_AUTHORS, ERROR) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 57 | FUNC(TREE_NOT_SORTED, ERROR) \ |
| 58 | FUNC(UNKNOWN_TYPE, ERROR) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 59 | FUNC(ZERO_PADDED_DATE, ERROR) \ |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 60 | FUNC(GITMODULES_MISSING, ERROR) \ |
| 61 | FUNC(GITMODULES_BLOB, ERROR) \ |
Jeff King | 0d68764 | 2018-07-13 15:39:53 -0400 | [diff] [blame] | 62 | FUNC(GITMODULES_LARGE, ERROR) \ |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 63 | FUNC(GITMODULES_NAME, ERROR) \ |
Jeff King | b7b1fca | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 64 | FUNC(GITMODULES_SYMLINK, ERROR) \ |
Jeff King | a124133 | 2018-09-24 04:37:17 -0400 | [diff] [blame] | 65 | FUNC(GITMODULES_URL, ERROR) \ |
Jeff King | 1a7fd1f | 2018-09-24 04:42:19 -0400 | [diff] [blame] | 66 | FUNC(GITMODULES_PATH, ERROR) \ |
Jonathan Nieder | bb92255 | 2019-12-05 01:30:43 -0800 | [diff] [blame] | 67 | FUNC(GITMODULES_UPDATE, ERROR) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 68 | /* warnings */ \ |
| 69 | FUNC(BAD_FILEMODE, WARN) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 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) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 75 | FUNC(NULL_SHA1, WARN) \ |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 76 | FUNC(ZERO_PADDED_FILEMODE, WARN) \ |
Junio C Hamano | 6d2d780 | 2016-04-14 10:58:22 -0700 | [diff] [blame] | 77 | FUNC(NUL_IN_COMMIT, WARN) \ |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 78 | /* infos (reported as warnings, but ignored by default) */ \ |
Jeff King | 64eb14d | 2018-07-13 15:39:58 -0400 | [diff] [blame] | 79 | FUNC(GITMODULES_PARSE, INFO) \ |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 80 | FUNC(BAD_TAG_NAME, INFO) \ |
| 81 | FUNC(MISSING_TAGGER_ENTRY, INFO) |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 82 | |
| 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 |
| 87 | }; |
| 88 | #undef MSG_ID |
| 89 | |
Johannes Schindelin | f417eed | 2015-06-22 17:25:14 +0200 | [diff] [blame] | 90 | #define STR(x) #x |
Nguyễn Thái Ngọc Duy | a4a9cc1 | 2018-05-26 15:55:25 +0200 | [diff] [blame] | 91 | #define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type }, |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 92 | static struct { |
Johannes Schindelin | f417eed | 2015-06-22 17:25:14 +0200 | [diff] [blame] | 93 | const char *id_string; |
| 94 | const char *downcased; |
Nguyễn Thái Ngọc Duy | a4a9cc1 | 2018-05-26 15:55:25 +0200 | [diff] [blame] | 95 | const char *camelcased; |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 96 | int msg_type; |
| 97 | } msg_id_info[FSCK_MSG_MAX + 1] = { |
| 98 | FOREACH_MSG_ID(MSG_ID) |
Nguyễn Thái Ngọc Duy | a4a9cc1 | 2018-05-26 15:55:25 +0200 | [diff] [blame] | 99 | { NULL, NULL, NULL, -1 } |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 100 | }; |
| 101 | #undef MSG_ID |
| 102 | |
Nguyễn Thái Ngọc Duy | a46baac | 2018-05-26 15:55:23 +0200 | [diff] [blame] | 103 | static void prepare_msg_ids(void) |
| 104 | { |
| 105 | int i; |
| 106 | |
| 107 | if (msg_id_info[0].downcased) |
| 108 | return; |
| 109 | |
| 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); |
| 115 | |
| 116 | msg_id_info[i].downcased = q; |
| 117 | while (*p) |
| 118 | if (*p == '_') |
| 119 | p++; |
| 120 | else |
| 121 | *(q)++ = tolower(*(p)++); |
| 122 | *q = '\0'; |
Nguyễn Thái Ngọc Duy | a4a9cc1 | 2018-05-26 15:55:25 +0200 | [diff] [blame] | 123 | |
| 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++); |
| 134 | } |
| 135 | } |
| 136 | *q = '\0'; |
Nguyễn Thái Ngọc Duy | a46baac | 2018-05-26 15:55:23 +0200 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Johannes Schindelin | f417eed | 2015-06-22 17:25:14 +0200 | [diff] [blame] | 140 | static int parse_msg_id(const char *text) |
| 141 | { |
| 142 | int i; |
| 143 | |
Nguyễn Thái Ngọc Duy | a46baac | 2018-05-26 15:55:23 +0200 | [diff] [blame] | 144 | prepare_msg_ids(); |
Johannes Schindelin | f417eed | 2015-06-22 17:25:14 +0200 | [diff] [blame] | 145 | |
| 146 | for (i = 0; i < FSCK_MSG_MAX; i++) |
| 147 | if (!strcmp(text, msg_id_info[i].downcased)) |
| 148 | return i; |
| 149 | |
| 150 | return -1; |
| 151 | } |
| 152 | |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 153 | void list_config_fsck_msg_ids(struct string_list *list, const char *prefix) |
| 154 | { |
| 155 | int i; |
| 156 | |
| 157 | prepare_msg_ids(); |
| 158 | |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 159 | for (i = 0; i < FSCK_MSG_MAX; i++) |
Nguyễn Thái Ngọc Duy | a4a9cc1 | 2018-05-26 15:55:25 +0200 | [diff] [blame] | 160 | list_config_item(list, prefix, msg_id_info[i].camelcased); |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 163 | static int fsck_msg_type(enum fsck_msg_id msg_id, |
| 164 | struct fsck_options *options) |
| 165 | { |
| 166 | int msg_type; |
| 167 | |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 168 | assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX); |
| 169 | |
| 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; |
| 176 | } |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 177 | |
| 178 | return msg_type; |
| 179 | } |
| 180 | |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 181 | static int parse_msg_type(const char *str) |
| 182 | { |
| 183 | if (!strcmp(str, "error")) |
| 184 | return FSCK_ERROR; |
| 185 | else if (!strcmp(str, "warn")) |
| 186 | return FSCK_WARN; |
Johannes Schindelin | efaba7c | 2015-06-22 17:26:48 +0200 | [diff] [blame] | 187 | else if (!strcmp(str, "ignore")) |
| 188 | return FSCK_IGNORE; |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 189 | else |
| 190 | die("Unknown fsck message type: '%s'", str); |
| 191 | } |
| 192 | |
Johannes Schindelin | 5d477a3 | 2015-06-22 17:25:31 +0200 | [diff] [blame] | 193 | int is_valid_msg_type(const char *msg_id, const char *msg_type) |
| 194 | { |
| 195 | if (parse_msg_id(msg_id) < 0) |
| 196 | return 0; |
| 197 | parse_msg_type(msg_type); |
| 198 | return 1; |
| 199 | } |
| 200 | |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 201 | void fsck_set_msg_type(struct fsck_options *options, |
| 202 | const char *msg_id, const char *msg_type) |
| 203 | { |
| 204 | int id = parse_msg_id(msg_id), type; |
| 205 | |
| 206 | if (id < 0) |
| 207 | die("Unhandled message id: %s", msg_id); |
| 208 | type = parse_msg_type(msg_type); |
| 209 | |
Johannes Schindelin | f50c440 | 2015-06-22 17:26:42 +0200 | [diff] [blame] | 210 | if (type != FSCK_ERROR && msg_id_info[id].msg_type == FSCK_FATAL) |
| 211 | die("Cannot demote %s to %s", msg_id, msg_type); |
| 212 | |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 213 | if (!options->msg_type) { |
| 214 | int i; |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 215 | int *msg_type; |
| 216 | ALLOC_ARRAY(msg_type, FSCK_MSG_MAX); |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 217 | for (i = 0; i < FSCK_MSG_MAX; i++) |
| 218 | msg_type[i] = fsck_msg_type(i, options); |
| 219 | options->msg_type = msg_type; |
| 220 | } |
| 221 | |
| 222 | options->msg_type[id] = type; |
| 223 | } |
| 224 | |
| 225 | void fsck_set_msg_types(struct fsck_options *options, const char *values) |
| 226 | { |
| 227 | char *buf = xstrdup(values), *to_free = buf; |
| 228 | int done = 0; |
| 229 | |
| 230 | while (!done) { |
| 231 | int len = strcspn(buf, " ,|"), equal; |
| 232 | |
| 233 | done = !buf[len]; |
| 234 | if (!len) { |
| 235 | buf++; |
| 236 | continue; |
| 237 | } |
| 238 | buf[len] = '\0'; |
| 239 | |
| 240 | for (equal = 0; |
| 241 | equal < len && buf[equal] != '=' && buf[equal] != ':'; |
| 242 | equal++) |
| 243 | buf[equal] = tolower(buf[equal]); |
| 244 | buf[equal] = '\0'; |
| 245 | |
Johannes Schindelin | cd94c6f | 2015-06-22 17:27:18 +0200 | [diff] [blame] | 246 | if (!strcmp(buf, "skiplist")) { |
| 247 | if (equal == len) |
| 248 | die("skiplist requires a path"); |
Barret Rhoden | 24eb33e | 2019-05-15 17:44:56 -0400 | [diff] [blame] | 249 | oidset_parse_file(&options->skiplist, buf + equal + 1); |
Johannes Schindelin | cd94c6f | 2015-06-22 17:27:18 +0200 | [diff] [blame] | 250 | buf += len + 1; |
| 251 | continue; |
| 252 | } |
| 253 | |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 254 | if (equal == len) |
| 255 | die("Missing '=': '%s'", buf); |
| 256 | |
| 257 | fsck_set_msg_type(options, buf, buf + equal + 1); |
| 258 | buf += len + 1; |
| 259 | } |
| 260 | free(to_free); |
| 261 | } |
| 262 | |
Johannes Schindelin | 71ab8fa | 2015-06-22 17:25:52 +0200 | [diff] [blame] | 263 | static void append_msg_id(struct strbuf *sb, const char *msg_id) |
| 264 | { |
| 265 | for (;;) { |
| 266 | char c = *(msg_id)++; |
| 267 | |
| 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)++); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | strbuf_addstr(sb, ": "); |
| 279 | } |
| 280 | |
Jeff King | f597937 | 2019-10-18 00:58:51 -0400 | [diff] [blame] | 281 | static int object_on_skiplist(struct fsck_options *opts, |
| 282 | const struct object_id *oid) |
Ramsay Jones | fb16287 | 2018-06-27 19:39:53 +0100 | [diff] [blame] | 283 | { |
Jeff King | f597937 | 2019-10-18 00:58:51 -0400 | [diff] [blame] | 284 | return opts && oid && oidset_contains(&opts->skiplist, oid); |
Ramsay Jones | fb16287 | 2018-06-27 19:39:53 +0100 | [diff] [blame] | 285 | } |
| 286 | |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 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, ...) |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 291 | { |
| 292 | va_list ap; |
| 293 | struct strbuf sb = STRBUF_INIT; |
| 294 | int msg_type = fsck_msg_type(id, options), result; |
| 295 | |
Johannes Schindelin | efaba7c | 2015-06-22 17:26:48 +0200 | [diff] [blame] | 296 | if (msg_type == FSCK_IGNORE) |
| 297 | return 0; |
| 298 | |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 299 | if (object_on_skiplist(options, oid)) |
Johannes Schindelin | cd94c6f | 2015-06-22 17:27:18 +0200 | [diff] [blame] | 300 | return 0; |
| 301 | |
Johannes Schindelin | f50c440 | 2015-06-22 17:26:42 +0200 | [diff] [blame] | 302 | if (msg_type == FSCK_FATAL) |
| 303 | msg_type = FSCK_ERROR; |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 304 | else if (msg_type == FSCK_INFO) |
| 305 | msg_type = FSCK_WARN; |
Johannes Schindelin | f50c440 | 2015-06-22 17:26:42 +0200 | [diff] [blame] | 306 | |
Johannes Schindelin | 71ab8fa | 2015-06-22 17:25:52 +0200 | [diff] [blame] | 307 | append_msg_id(&sb, msg_id_info[id].id_string); |
| 308 | |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 309 | va_start(ap, fmt); |
| 310 | strbuf_vaddf(&sb, fmt, ap); |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 311 | result = options->error_func(options, oid, object_type, |
Jeff King | 5afc4b1 | 2019-10-18 00:58:40 -0400 | [diff] [blame] | 312 | msg_type, sb.buf); |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 313 | strbuf_release(&sb); |
| 314 | va_end(ap); |
| 315 | |
| 316 | return result; |
| 317 | } |
| 318 | |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 319 | void fsck_enable_object_names(struct fsck_options *options) |
| 320 | { |
| 321 | if (!options->object_names) |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 322 | options->object_names = kh_init_oid_map(); |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 323 | } |
| 324 | |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 325 | const char *fsck_get_object_name(struct fsck_options *options, |
| 326 | const struct object_id *oid) |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 327 | { |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 328 | khiter_t pos; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 329 | if (!options->object_names) |
| 330 | return NULL; |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 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); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 337 | void fsck_put_object_name(struct fsck_options *options, |
| 338 | const struct object_id *oid, |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 339 | const char *fmt, ...) |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 340 | { |
| 341 | va_list ap; |
| 342 | struct strbuf buf = STRBUF_INIT; |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 343 | khiter_t pos; |
| 344 | int hashret; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 345 | |
| 346 | if (!options->object_names) |
| 347 | return; |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 348 | |
| 349 | pos = kh_put_oid_map(options->object_names, *oid, &hashret); |
| 350 | if (!hashret) |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 351 | return; |
| 352 | va_start(ap, fmt); |
| 353 | strbuf_vaddf(&buf, fmt, ap); |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 354 | kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 355 | va_end(ap); |
| 356 | } |
| 357 | |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 358 | const char *fsck_describe_object(struct fsck_options *options, |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 359 | const struct object_id *oid) |
Johannes Schindelin | 90cf590 | 2016-07-17 13:00:02 +0200 | [diff] [blame] | 360 | { |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 361 | static struct strbuf bufs[] = { |
| 362 | STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT |
| 363 | }; |
| 364 | static int b = 0; |
| 365 | struct strbuf *buf; |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 366 | const char *name = fsck_get_object_name(options, oid); |
Johannes Schindelin | 90cf590 | 2016-07-17 13:00:02 +0200 | [diff] [blame] | 367 | |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 368 | buf = bufs + b; |
| 369 | b = (b + 1) % ARRAY_SIZE(bufs); |
| 370 | strbuf_reset(buf); |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 371 | strbuf_addstr(buf, oid_to_hex(oid)); |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 372 | if (name) |
| 373 | strbuf_addf(buf, " (%s)", name); |
| 374 | |
| 375 | return buf->buf; |
Johannes Schindelin | 90cf590 | 2016-07-17 13:00:02 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 378 | static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options) |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 379 | { |
| 380 | struct tree_desc desc; |
| 381 | struct name_entry entry; |
| 382 | int res = 0; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 383 | const char *name; |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 384 | |
| 385 | if (parse_tree(tree)) |
| 386 | return -1; |
| 387 | |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 388 | name = fsck_get_object_name(options, &tree->object.oid); |
David Turner | 8354fa3 | 2016-09-27 16:59:51 -0400 | [diff] [blame] | 389 | if (init_tree_desc_gently(&desc, tree->buffer, tree->size)) |
| 390 | return -1; |
| 391 | while (tree_entry_gently(&desc, &entry)) { |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 392 | struct object *obj; |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 393 | int result; |
| 394 | |
| 395 | if (S_ISGITLINK(entry.mode)) |
| 396 | continue; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 397 | |
| 398 | if (S_ISDIR(entry.mode)) { |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 399 | obj = (struct object *)lookup_tree(the_repository, &entry.oid); |
René Scharfe | 2720f6d | 2017-10-05 21:41:26 +0200 | [diff] [blame] | 400 | if (name && obj) |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 401 | fsck_put_object_name(options, &entry.oid, "%s%s/", |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 402 | name, entry.path); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 403 | result = options->walk(obj, OBJ_TREE, data, options); |
| 404 | } |
| 405 | else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) { |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 406 | obj = (struct object *)lookup_blob(the_repository, &entry.oid); |
René Scharfe | 2720f6d | 2017-10-05 21:41:26 +0200 | [diff] [blame] | 407 | if (name && obj) |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 408 | fsck_put_object_name(options, &entry.oid, "%s%s", |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 409 | name, entry.path); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 410 | result = options->walk(obj, OBJ_BLOB, data, options); |
| 411 | } |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 412 | else { |
Pete Wyckoff | 82247e9 | 2012-04-29 20:28:45 -0400 | [diff] [blame] | 413 | result = error("in tree %s: entry %s has bad mode %.6o", |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 414 | fsck_describe_object(options, &tree->object.oid), |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 415 | entry.path, entry.mode); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 416 | } |
| 417 | if (result < 0) |
| 418 | return result; |
| 419 | if (!res) |
| 420 | res = result; |
| 421 | } |
| 422 | return res; |
| 423 | } |
| 424 | |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 425 | static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options) |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 426 | { |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 427 | int counter = 0, generation = 0, name_prefix_len = 0; |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 428 | struct commit_list *parents; |
| 429 | int res; |
| 430 | int result; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 431 | const char *name; |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 432 | |
| 433 | if (parse_commit(commit)) |
| 434 | return -1; |
| 435 | |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 436 | name = fsck_get_object_name(options, &commit->object.oid); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 437 | if (name) |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 438 | fsck_put_object_name(options, get_commit_tree_oid(commit), |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 439 | "%s:", name); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 440 | |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 441 | result = options->walk((struct object *)get_commit_tree(commit), |
| 442 | OBJ_TREE, data, options); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 443 | if (result < 0) |
| 444 | return result; |
| 445 | res = result; |
| 446 | |
| 447 | parents = commit->parents; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 448 | if (name && parents) { |
| 449 | int len = strlen(name), power; |
| 450 | |
| 451 | if (len && name[len - 1] == '^') { |
| 452 | generation = 1; |
| 453 | name_prefix_len = len - 1; |
| 454 | } |
| 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; |
| 462 | } |
| 463 | } |
| 464 | |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 465 | while (parents) { |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 466 | if (name) { |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 467 | struct object_id *oid = &parents->item->object.oid; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 468 | |
Junio C Hamano | b84c7838 | 2018-10-24 10:25:12 +0900 | [diff] [blame] | 469 | if (counter++) |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 470 | fsck_put_object_name(options, oid, "%s^%d", |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 471 | name, counter); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 472 | else if (generation > 0) |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 473 | fsck_put_object_name(options, oid, "%.*s~%d", |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 474 | name_prefix_len, name, |
| 475 | generation + 1); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 476 | else |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 477 | fsck_put_object_name(options, oid, "%s^", name); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 478 | } |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 479 | result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 480 | if (result < 0) |
| 481 | return result; |
| 482 | if (!res) |
| 483 | res = result; |
| 484 | parents = parents->next; |
| 485 | } |
| 486 | return res; |
| 487 | } |
| 488 | |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 489 | static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options) |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 490 | { |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 491 | const char *name = fsck_get_object_name(options, &tag->object.oid); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 492 | |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 493 | if (parse_tag(tag)) |
| 494 | return -1; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 495 | if (name) |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 496 | fsck_put_object_name(options, &tag->tagged->oid, "%s", name); |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 497 | return options->walk(tag->tagged, OBJ_ANY, data, options); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 498 | } |
| 499 | |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 500 | int fsck_walk(struct object *obj, void *data, struct fsck_options *options) |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 501 | { |
| 502 | if (!obj) |
| 503 | return -1; |
Jeff King | a2b2285 | 2017-01-25 23:12:07 -0500 | [diff] [blame] | 504 | |
| 505 | if (obj->type == OBJ_NONE) |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 506 | parse_object(the_repository, &obj->oid); |
Jeff King | a2b2285 | 2017-01-25 23:12:07 -0500 | [diff] [blame] | 507 | |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 508 | switch (obj->type) { |
| 509 | case OBJ_BLOB: |
| 510 | return 0; |
| 511 | case OBJ_TREE: |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 512 | return fsck_walk_tree((struct tree *)obj, data, options); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 513 | case OBJ_COMMIT: |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 514 | return fsck_walk_commit((struct commit *)obj, data, options); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 515 | case OBJ_TAG: |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 516 | return fsck_walk_tag((struct tag *)obj, data, options); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 517 | default: |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 518 | error("Unknown object type for %s", |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 519 | fsck_describe_object(options, &obj->oid)); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 520 | return -1; |
| 521 | } |
| 522 | } |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 523 | |
| 524 | /* |
| 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. |
| 528 | * |
| 529 | * So a directory called "a" is ordered _after_ a file |
| 530 | * called "a.c", because "a/" sorts after "a.c". |
| 531 | */ |
| 532 | #define TREE_UNORDERED (-1) |
| 533 | #define TREE_HAS_DUPS (-2) |
| 534 | |
| 535 | static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2) |
| 536 | { |
| 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; |
| 542 | |
| 543 | cmp = memcmp(name1, name2, len); |
| 544 | if (cmp < 0) |
| 545 | return 0; |
| 546 | if (cmp > 0) |
| 547 | return TREE_UNORDERED; |
| 548 | |
| 549 | /* |
| 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. |
| 553 | */ |
| 554 | c1 = name1[len]; |
| 555 | c2 = name2[len]; |
| 556 | if (!c1 && !c2) |
| 557 | /* |
| 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. |
| 561 | */ |
| 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; |
| 568 | } |
| 569 | |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 570 | static int fsck_tree(const struct object_id *oid, |
Jeff King | 23a173a | 2019-10-18 00:54:12 -0400 | [diff] [blame] | 571 | const char *buffer, unsigned long size, |
| 572 | struct fsck_options *options) |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 573 | { |
David Turner | 8354fa3 | 2016-09-27 16:59:51 -0400 | [diff] [blame] | 574 | int retval = 0; |
Jeff King | c479d14 | 2012-07-28 11:06:29 -0400 | [diff] [blame] | 575 | int has_null_sha1 = 0; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 576 | int has_full_path = 0; |
| 577 | int has_empty_name = 0; |
Jeff King | 5d34a43 | 2012-11-27 21:27:37 -0500 | [diff] [blame] | 578 | int has_dot = 0; |
| 579 | int has_dotdot = 0; |
Jeff King | 5c17f51 | 2012-11-28 16:35:29 -0500 | [diff] [blame] | 580 | int has_dotgit = 0; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 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; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 588 | |
Jeff King | 23a173a | 2019-10-18 00:54:12 -0400 | [diff] [blame] | 589 | if (init_tree_desc_gently(&desc, buffer, size)) { |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 590 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree"); |
David Turner | 8354fa3 | 2016-09-27 16:59:51 -0400 | [diff] [blame] | 591 | return retval; |
| 592 | } |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 593 | |
| 594 | o_mode = 0; |
| 595 | o_name = NULL; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 596 | |
| 597 | while (desc.size) { |
Elijah Newren | 5ec1e72 | 2019-04-05 08:00:12 -0700 | [diff] [blame] | 598 | unsigned short mode; |
Johannes Schindelin | 288a74b | 2019-09-23 08:58:11 +0200 | [diff] [blame] | 599 | const char *name, *backslash; |
brian m. carlson | ce6663a | 2016-04-17 23:10:40 +0000 | [diff] [blame] | 600 | const struct object_id *oid; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 601 | |
brian m. carlson | ce6663a | 2016-04-17 23:10:40 +0000 | [diff] [blame] | 602 | oid = tree_entry_extract(&desc, &name, &mode); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 603 | |
brian m. carlson | ce6663a | 2016-04-17 23:10:40 +0000 | [diff] [blame] | 604 | has_null_sha1 |= is_null_oid(oid); |
Hiroyuki Sano | effd12e | 2014-03-20 08:02:04 +0900 | [diff] [blame] | 605 | has_full_path |= !!strchr(name, '/'); |
| 606 | has_empty_name |= !*name; |
| 607 | has_dot |= !strcmp(name, "."); |
| 608 | has_dotdot |= !strcmp(name, ".."); |
Jeff King | ed9c322 | 2018-05-13 12:35:37 -0400 | [diff] [blame] | 609 | has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 610 | has_zero_pad |= *(char *)desc.buffer == '0'; |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 611 | |
Jeff King | b7b1fca | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 612 | if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) { |
| 613 | if (!S_ISLNK(mode)) |
| 614 | oidset_insert(&gitmodules_found, oid); |
| 615 | else |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 616 | retval += report(options, |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 617 | oid, OBJ_TREE, |
Jeff King | b7b1fca | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 618 | FSCK_MSG_GITMODULES_SYMLINK, |
| 619 | ".gitmodules is a symbolic link"); |
| 620 | } |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 621 | |
Johannes Schindelin | 288a74b | 2019-09-23 08:58:11 +0200 | [diff] [blame] | 622 | if ((backslash = strchr(name, '\\'))) { |
| 623 | while (backslash) { |
| 624 | backslash++; |
| 625 | has_dotgit |= is_ntfs_dotgit(backslash); |
Johannes Schindelin | bdfef04 | 2019-12-04 21:52:10 +0100 | [diff] [blame] | 626 | if (is_ntfs_dotgitmodules(backslash)) { |
| 627 | if (!S_ISLNK(mode)) |
| 628 | oidset_insert(&gitmodules_found, oid); |
| 629 | else |
Junio C Hamano | 7034cd0 | 2019-12-09 22:17:55 -0800 | [diff] [blame] | 630 | retval += report(options, oid, OBJ_TREE, |
Johannes Schindelin | bdfef04 | 2019-12-04 21:52:10 +0100 | [diff] [blame] | 631 | FSCK_MSG_GITMODULES_SYMLINK, |
| 632 | ".gitmodules is a symbolic link"); |
| 633 | } |
Johannes Schindelin | 288a74b | 2019-09-23 08:58:11 +0200 | [diff] [blame] | 634 | backslash = strchr(backslash, '\\'); |
| 635 | } |
| 636 | } |
| 637 | |
David Turner | 8354fa3 | 2016-09-27 16:59:51 -0400 | [diff] [blame] | 638 | if (update_tree_entry_gently(&desc)) { |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 639 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree"); |
David Turner | 8354fa3 | 2016-09-27 16:59:51 -0400 | [diff] [blame] | 640 | break; |
| 641 | } |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 642 | |
| 643 | switch (mode) { |
| 644 | /* |
| 645 | * Standard modes.. |
| 646 | */ |
| 647 | case S_IFREG | 0755: |
| 648 | case S_IFREG | 0644: |
| 649 | case S_IFLNK: |
| 650 | case S_IFDIR: |
| 651 | case S_IFGITLINK: |
| 652 | break; |
| 653 | /* |
| 654 | * This is nonstandard, but we had a few of these |
| 655 | * early on when we honored the full set of mode |
| 656 | * bits.. |
| 657 | */ |
| 658 | case S_IFREG | 0664: |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 659 | if (!options->strict) |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 660 | break; |
Jeff King | 1cf01a3 | 2017-09-21 02:25:41 -0400 | [diff] [blame] | 661 | /* fallthrough */ |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 662 | default: |
| 663 | has_bad_modes = 1; |
| 664 | } |
| 665 | |
| 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; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | o_mode = mode; |
| 680 | o_name = name; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Jeff King | c479d14 | 2012-07-28 11:06:29 -0400 | [diff] [blame] | 683 | if (has_null_sha1) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 684 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1"); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 685 | if (has_full_path) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 686 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_FULL_PATHNAME, "contains full pathnames"); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 687 | if (has_empty_name) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 688 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_EMPTY_NAME, "contains empty pathname"); |
Jeff King | 5d34a43 | 2012-11-27 21:27:37 -0500 | [diff] [blame] | 689 | if (has_dot) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 690 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOT, "contains '.'"); |
Jeff King | 5d34a43 | 2012-11-27 21:27:37 -0500 | [diff] [blame] | 691 | if (has_dotdot) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 692 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTDOT, "contains '..'"); |
Jeff King | 5c17f51 | 2012-11-28 16:35:29 -0500 | [diff] [blame] | 693 | if (has_dotgit) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 694 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTGIT, "contains '.git'"); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 695 | if (has_zero_pad) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 696 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes"); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 697 | if (has_bad_modes) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 698 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_FILEMODE, "contains bad file modes"); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 699 | if (has_dup_entries) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 700 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries"); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 701 | if (not_properly_sorted) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 702 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted"); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 703 | return retval; |
| 704 | } |
| 705 | |
Junio C Hamano | 84d18c0 | 2015-06-28 11:18:31 -0700 | [diff] [blame] | 706 | static int verify_headers(const void *data, unsigned long size, |
Jeff King | cc57900 | 2019-10-18 01:00:50 -0400 | [diff] [blame] | 707 | const struct object_id *oid, enum object_type type, |
| 708 | struct fsck_options *options) |
Johannes Schindelin | 4d0d897 | 2014-09-11 16:26:33 +0200 | [diff] [blame] | 709 | { |
| 710 | const char *buffer = (const char *)data; |
| 711 | unsigned long i; |
| 712 | |
| 713 | for (i = 0; i < size; i++) { |
| 714 | switch (buffer[i]) { |
| 715 | case '\0': |
Jeff King | cc57900 | 2019-10-18 01:00:50 -0400 | [diff] [blame] | 716 | return report(options, oid, type, |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 717 | FSCK_MSG_NUL_IN_HEADER, |
| 718 | "unterminated header: NUL at offset %ld", i); |
Johannes Schindelin | 4d0d897 | 2014-09-11 16:26:33 +0200 | [diff] [blame] | 719 | case '\n': |
| 720 | if (i + 1 < size && buffer[i + 1] == '\n') |
| 721 | return 0; |
| 722 | } |
| 723 | } |
| 724 | |
Junio C Hamano | 84d18c0 | 2015-06-28 11:18:31 -0700 | [diff] [blame] | 725 | /* |
| 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. |
| 730 | */ |
| 731 | if (size && buffer[size - 1] == '\n') |
| 732 | return 0; |
| 733 | |
Jeff King | cc57900 | 2019-10-18 01:00:50 -0400 | [diff] [blame] | 734 | return report(options, oid, type, |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 735 | FSCK_MSG_UNTERMINATED_HEADER, "unterminated header"); |
Johannes Schindelin | 4d0d897 | 2014-09-11 16:26:33 +0200 | [diff] [blame] | 736 | } |
| 737 | |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 738 | static int fsck_ident(const char **ident, |
| 739 | const struct object_id *oid, enum object_type type, |
| 740 | struct fsck_options *options) |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 741 | { |
Johannes Schindelin | e6826e3 | 2015-06-22 17:26:03 +0200 | [diff] [blame] | 742 | const char *p = *ident; |
Jeff King | d4b8de0 | 2014-02-24 02:39:04 -0500 | [diff] [blame] | 743 | char *end; |
| 744 | |
Johannes Schindelin | e6826e3 | 2015-06-22 17:26:03 +0200 | [diff] [blame] | 745 | *ident = strchrnul(*ident, '\n'); |
| 746 | if (**ident == '\n') |
| 747 | (*ident)++; |
| 748 | |
| 749 | if (*p == '<') |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 750 | return report(options, oid, type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email"); |
Johannes Schindelin | e6826e3 | 2015-06-22 17:26:03 +0200 | [diff] [blame] | 751 | p += strcspn(p, "<>\n"); |
| 752 | if (*p == '>') |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 753 | return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name"); |
Johannes Schindelin | e6826e3 | 2015-06-22 17:26:03 +0200 | [diff] [blame] | 754 | if (*p != '<') |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 755 | return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email"); |
Johannes Schindelin | e6826e3 | 2015-06-22 17:26:03 +0200 | [diff] [blame] | 756 | if (p[-1] != ' ') |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 757 | return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email"); |
Johannes Schindelin | e6826e3 | 2015-06-22 17:26:03 +0200 | [diff] [blame] | 758 | p++; |
| 759 | p += strcspn(p, "<>\n"); |
| 760 | if (*p != '>') |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 761 | return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email"); |
Johannes Schindelin | e6826e3 | 2015-06-22 17:26:03 +0200 | [diff] [blame] | 762 | p++; |
| 763 | if (*p != ' ') |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 764 | return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date"); |
Johannes Schindelin | e6826e3 | 2015-06-22 17:26:03 +0200 | [diff] [blame] | 765 | p++; |
| 766 | if (*p == '0' && p[1] != ' ') |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 767 | return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date"); |
Johannes Schindelin | 1aeb7e7 | 2017-04-21 12:45:44 +0200 | [diff] [blame] | 768 | if (date_overflows(parse_timestamp(p, &end, 10))) |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 769 | return report(options, oid, type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow"); |
Johannes Schindelin | e6826e3 | 2015-06-22 17:26:03 +0200 | [diff] [blame] | 770 | if ((end == p || *end != ' ')) |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 771 | return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date"); |
Johannes Schindelin | e6826e3 | 2015-06-22 17:26:03 +0200 | [diff] [blame] | 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')) |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 779 | return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone"); |
Johannes Schindelin | e6826e3 | 2015-06-22 17:26:03 +0200 | [diff] [blame] | 780 | p += 6; |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 781 | return 0; |
| 782 | } |
| 783 | |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 784 | static int fsck_commit(const struct object_id *oid, |
| 785 | const char *buffer, unsigned long size, |
| 786 | struct fsck_options *options) |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 787 | { |
Jeff King | f648ee7 | 2019-10-18 01:00:59 -0400 | [diff] [blame] | 788 | struct object_id tree_oid, parent_oid; |
Jeff King | ec65231 | 2019-10-18 00:49:10 -0400 | [diff] [blame] | 789 | unsigned author_count; |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 790 | int err; |
Junio C Hamano | 6d2d780 | 2016-04-14 10:58:22 -0700 | [diff] [blame] | 791 | const char *buffer_begin = buffer; |
brian m. carlson | c54f5ca | 2018-05-02 00:25:41 +0000 | [diff] [blame] | 792 | const char *p; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 793 | |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 794 | if (verify_headers(buffer, size, oid, OBJ_COMMIT, options)) |
Johannes Schindelin | 4d0d897 | 2014-09-11 16:26:33 +0200 | [diff] [blame] | 795 | return -1; |
| 796 | |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 797 | if (!skip_prefix(buffer, "tree ", &buffer)) |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 798 | return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line"); |
brian m. carlson | c54f5ca | 2018-05-02 00:25:41 +0000 | [diff] [blame] | 799 | if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') { |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 800 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1"); |
Johannes Schindelin | b358476 | 2015-06-22 17:26:11 +0200 | [diff] [blame] | 801 | if (err) |
| 802 | return err; |
| 803 | } |
brian m. carlson | c54f5ca | 2018-05-02 00:25:41 +0000 | [diff] [blame] | 804 | buffer = p + 1; |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 805 | while (skip_prefix(buffer, "parent ", &buffer)) { |
Jeff King | f648ee7 | 2019-10-18 01:00:59 -0400 | [diff] [blame] | 806 | if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') { |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 807 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1"); |
Johannes Schindelin | b358476 | 2015-06-22 17:26:11 +0200 | [diff] [blame] | 808 | if (err) |
| 809 | return err; |
| 810 | } |
brian m. carlson | c54f5ca | 2018-05-02 00:25:41 +0000 | [diff] [blame] | 811 | buffer = p + 1; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 812 | } |
Johannes Schindelin | c9ad147 | 2015-06-22 17:26:23 +0200 | [diff] [blame] | 813 | author_count = 0; |
| 814 | while (skip_prefix(buffer, "author ", &buffer)) { |
| 815 | author_count++; |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 816 | err = fsck_ident(&buffer, oid, OBJ_COMMIT, options); |
Johannes Schindelin | c9ad147 | 2015-06-22 17:26:23 +0200 | [diff] [blame] | 817 | if (err) |
| 818 | return err; |
| 819 | } |
| 820 | if (author_count < 1) |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 821 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line"); |
Johannes Schindelin | c9ad147 | 2015-06-22 17:26:23 +0200 | [diff] [blame] | 822 | else if (author_count > 1) |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 823 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines"); |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 824 | if (err) |
| 825 | return err; |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 826 | if (!skip_prefix(buffer, "committer ", &buffer)) |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 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); |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 829 | if (err) |
| 830 | return err; |
Junio C Hamano | 6d2d780 | 2016-04-14 10:58:22 -0700 | [diff] [blame] | 831 | if (memchr(buffer_begin, '\0', size)) { |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 832 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT, |
Junio C Hamano | 6d2d780 | 2016-04-14 10:58:22 -0700 | [diff] [blame] | 833 | "NUL byte in the commit object body"); |
| 834 | if (err) |
| 835 | return err; |
| 836 | } |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 837 | return 0; |
| 838 | } |
| 839 | |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 840 | static int fsck_tag(const struct object_id *oid, const char *buffer, |
Jeff King | 2175a0c | 2019-10-18 00:51:19 -0400 | [diff] [blame] | 841 | unsigned long size, struct fsck_options *options) |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 842 | { |
Jeff King | f648ee7 | 2019-10-18 01:00:59 -0400 | [diff] [blame] | 843 | struct object_id tagged_oid; |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 844 | int ret = 0; |
Jeff King | 23a173a | 2019-10-18 00:54:12 -0400 | [diff] [blame] | 845 | char *eol; |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 846 | struct strbuf sb = STRBUF_INIT; |
brian m. carlson | c54f5ca | 2018-05-02 00:25:41 +0000 | [diff] [blame] | 847 | const char *p; |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 848 | |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 849 | ret = verify_headers(buffer, size, oid, OBJ_TAG, options); |
René Scharfe | 8a272f2 | 2015-11-19 17:25:31 +0100 | [diff] [blame] | 850 | if (ret) |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 851 | goto done; |
| 852 | |
| 853 | if (!skip_prefix(buffer, "object ", &buffer)) { |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 854 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line"); |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 855 | goto done; |
| 856 | } |
Jeff King | f648ee7 | 2019-10-18 01:00:59 -0400 | [diff] [blame] | 857 | if (parse_oid_hex(buffer, &tagged_oid, &p) || *p != '\n') { |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 858 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1"); |
Johannes Schindelin | 7d7d5b0 | 2015-06-22 17:26:30 +0200 | [diff] [blame] | 859 | if (ret) |
| 860 | goto done; |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 861 | } |
brian m. carlson | c54f5ca | 2018-05-02 00:25:41 +0000 | [diff] [blame] | 862 | buffer = p + 1; |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 863 | |
| 864 | if (!skip_prefix(buffer, "type ", &buffer)) { |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 865 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line"); |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 866 | goto done; |
| 867 | } |
| 868 | eol = strchr(buffer, '\n'); |
| 869 | if (!eol) { |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 870 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line"); |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 871 | goto done; |
| 872 | } |
| 873 | if (type_from_string_gently(buffer, eol - buffer, 1) < 0) |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 874 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value"); |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 875 | if (ret) |
| 876 | goto done; |
| 877 | buffer = eol + 1; |
| 878 | |
| 879 | if (!skip_prefix(buffer, "tag ", &buffer)) { |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 880 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line"); |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 881 | goto done; |
| 882 | } |
| 883 | eol = strchr(buffer, '\n'); |
| 884 | if (!eol) { |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 885 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line"); |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 886 | goto done; |
| 887 | } |
| 888 | strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer); |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 889 | if (check_refname_format(sb.buf, 0)) { |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 890 | ret = report(options, oid, OBJ_TAG, |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 891 | FSCK_MSG_BAD_TAG_NAME, |
| 892 | "invalid 'tag' name: %.*s", |
| 893 | (int)(eol - buffer), buffer); |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 894 | if (ret) |
| 895 | goto done; |
| 896 | } |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 897 | buffer = eol + 1; |
| 898 | |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 899 | if (!skip_prefix(buffer, "tagger ", &buffer)) { |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 900 | /* early tags do not contain 'tagger' lines; warn only */ |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 901 | ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line"); |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 902 | if (ret) |
| 903 | goto done; |
| 904 | } |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 905 | else |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 906 | ret = fsck_ident(&buffer, oid, OBJ_TAG, options); |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 907 | |
| 908 | done: |
| 909 | strbuf_release(&sb); |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 910 | return ret; |
| 911 | } |
| 912 | |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 913 | struct fsck_gitmodules_data { |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 914 | const struct object_id *oid; |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 915 | struct fsck_options *options; |
| 916 | int ret; |
| 917 | }; |
| 918 | |
| 919 | static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata) |
| 920 | { |
| 921 | struct fsck_gitmodules_data *data = vdata; |
| 922 | const char *subsection, *key; |
| 923 | int subsection_len; |
| 924 | char *name; |
| 925 | |
| 926 | if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 || |
| 927 | !subsection) |
| 928 | return 0; |
| 929 | |
| 930 | name = xmemdupz(subsection, subsection_len); |
| 931 | if (check_submodule_name(name) < 0) |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 932 | data->ret |= report(data->options, |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 933 | data->oid, OBJ_BLOB, |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 934 | FSCK_MSG_GITMODULES_NAME, |
| 935 | "disallowed submodule name: %s", |
| 936 | name); |
Jeff King | a124133 | 2018-09-24 04:37:17 -0400 | [diff] [blame] | 937 | if (!strcmp(key, "url") && value && |
| 938 | looks_like_command_line_option(value)) |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 939 | data->ret |= report(data->options, |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 940 | data->oid, OBJ_BLOB, |
Jeff King | a124133 | 2018-09-24 04:37:17 -0400 | [diff] [blame] | 941 | FSCK_MSG_GITMODULES_URL, |
| 942 | "disallowed submodule url: %s", |
| 943 | value); |
Jeff King | 1a7fd1f | 2018-09-24 04:42:19 -0400 | [diff] [blame] | 944 | if (!strcmp(key, "path") && value && |
| 945 | looks_like_command_line_option(value)) |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 946 | data->ret |= report(data->options, |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 947 | data->oid, OBJ_BLOB, |
Jeff King | 1a7fd1f | 2018-09-24 04:42:19 -0400 | [diff] [blame] | 948 | FSCK_MSG_GITMODULES_PATH, |
| 949 | "disallowed submodule path: %s", |
| 950 | value); |
Jonathan Nieder | bb92255 | 2019-12-05 01:30:43 -0800 | [diff] [blame] | 951 | if (!strcmp(key, "update") && value && |
| 952 | parse_submodule_update_type(value) == SM_UPDATE_COMMAND) |
Junio C Hamano | 7034cd0 | 2019-12-09 22:17:55 -0800 | [diff] [blame] | 953 | data->ret |= report(data->options, data->oid, OBJ_BLOB, |
Jonathan Nieder | bb92255 | 2019-12-05 01:30:43 -0800 | [diff] [blame] | 954 | FSCK_MSG_GITMODULES_UPDATE, |
| 955 | "disallowed submodule update setting: %s", |
| 956 | value); |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 957 | free(name); |
| 958 | |
| 959 | return 0; |
| 960 | } |
| 961 | |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 962 | static int fsck_blob(const struct object_id *oid, const char *buf, |
Jeff King | 7ac4f3a | 2018-05-02 15:44:51 -0400 | [diff] [blame] | 963 | unsigned long size, struct fsck_options *options) |
| 964 | { |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 965 | struct fsck_gitmodules_data data; |
Jeff King | de6bd9e | 2018-06-28 18:06:04 -0400 | [diff] [blame] | 966 | struct config_options config_opts = { 0 }; |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 967 | |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 968 | if (!oidset_contains(&gitmodules_found, oid)) |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 969 | return 0; |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 970 | oidset_insert(&gitmodules_done, oid); |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 971 | |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 972 | if (object_on_skiplist(options, oid)) |
Ramsay Jones | fb16287 | 2018-06-27 19:39:53 +0100 | [diff] [blame] | 973 | return 0; |
| 974 | |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 975 | if (!buf) { |
| 976 | /* |
| 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. |
| 980 | */ |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 981 | return report(options, oid, OBJ_BLOB, |
Jeff King | 0d68764 | 2018-07-13 15:39:53 -0400 | [diff] [blame] | 982 | FSCK_MSG_GITMODULES_LARGE, |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 983 | ".gitmodules too large to parse"); |
| 984 | } |
| 985 | |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 986 | data.oid = oid; |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 987 | data.options = options; |
| 988 | data.ret = 0; |
Jeff King | de6bd9e | 2018-06-28 18:06:04 -0400 | [diff] [blame] | 989 | config_opts.error_action = CONFIG_ERROR_SILENT; |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 990 | if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB, |
Jeff King | de6bd9e | 2018-06-28 18:06:04 -0400 | [diff] [blame] | 991 | ".gitmodules", buf, size, &data, &config_opts)) |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 992 | data.ret |= report(options, oid, OBJ_BLOB, |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 993 | FSCK_MSG_GITMODULES_PARSE, |
| 994 | "could not parse gitmodules blob"); |
| 995 | |
| 996 | return data.ret; |
Jeff King | 7ac4f3a | 2018-05-02 15:44:51 -0400 | [diff] [blame] | 997 | } |
| 998 | |
Johannes Schindelin | 90a398b | 2014-09-10 15:52:51 +0200 | [diff] [blame] | 999 | int fsck_object(struct object *obj, void *data, unsigned long size, |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 1000 | struct fsck_options *options) |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 1001 | { |
| 1002 | if (!obj) |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 1003 | return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck"); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 1004 | |
| 1005 | if (obj->type == OBJ_BLOB) |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 1006 | return fsck_blob(&obj->oid, data, size, options); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 1007 | if (obj->type == OBJ_TREE) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 1008 | return fsck_tree(&obj->oid, data, size, options); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 1009 | if (obj->type == OBJ_COMMIT) |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 1010 | return fsck_commit(&obj->oid, data, size, options); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 1011 | if (obj->type == OBJ_TAG) |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 1012 | return fsck_tag(&obj->oid, data, size, options); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 1013 | |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 1014 | return report(options, &obj->oid, obj->type, |
| 1015 | FSCK_MSG_UNKNOWN_TYPE, |
| 1016 | "unknown type '%d' (internal fsck error)", |
| 1017 | obj->type); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 1018 | } |
Martin Koegler | d6ffc8d | 2008-02-25 22:46:09 +0100 | [diff] [blame] | 1019 | |
Johannes Schindelin | 1cd772c | 2016-07-17 12:59:57 +0200 | [diff] [blame] | 1020 | int fsck_error_function(struct fsck_options *o, |
Jeff King | 5afc4b1 | 2019-10-18 00:58:40 -0400 | [diff] [blame] | 1021 | const struct object_id *oid, |
| 1022 | enum object_type object_type, |
| 1023 | int msg_type, const char *message) |
Martin Koegler | d6ffc8d | 2008-02-25 22:46:09 +0100 | [diff] [blame] | 1024 | { |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 1025 | if (msg_type == FSCK_WARN) { |
Jeff King | 5afc4b1 | 2019-10-18 00:58:40 -0400 | [diff] [blame] | 1026 | warning("object %s: %s", fsck_describe_object(o, oid), message); |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 1027 | return 0; |
| 1028 | } |
Jeff King | 5afc4b1 | 2019-10-18 00:58:40 -0400 | [diff] [blame] | 1029 | error("object %s: %s", fsck_describe_object(o, oid), message); |
Martin Koegler | d6ffc8d | 2008-02-25 22:46:09 +0100 | [diff] [blame] | 1030 | return 1; |
| 1031 | } |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 1032 | |
| 1033 | int fsck_finish(struct fsck_options *options) |
| 1034 | { |
| 1035 | int ret = 0; |
| 1036 | struct oidset_iter iter; |
| 1037 | const struct object_id *oid; |
| 1038 | |
| 1039 | oidset_iter_init(&gitmodules_found, &iter); |
| 1040 | while ((oid = oidset_iter_next(&iter))) { |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 1041 | enum object_type type; |
| 1042 | unsigned long size; |
| 1043 | char *buf; |
| 1044 | |
| 1045 | if (oidset_contains(&gitmodules_done, oid)) |
| 1046 | continue; |
| 1047 | |
Junio C Hamano | 7913f53 | 2018-05-29 17:09:58 +0900 | [diff] [blame] | 1048 | buf = read_object_file(oid, &type, &size); |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 1049 | if (!buf) { |
Jeff King | b8b00f1 | 2019-10-18 00:59:54 -0400 | [diff] [blame] | 1050 | if (is_promisor_object(oid)) |
Jeff King | 2738744 | 2018-05-14 12:22:48 -0400 | [diff] [blame] | 1051 | continue; |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 1052 | ret |= report(options, |
Jeff King | b8b00f1 | 2019-10-18 00:59:54 -0400 | [diff] [blame] | 1053 | oid, OBJ_BLOB, |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 1054 | FSCK_MSG_GITMODULES_MISSING, |
| 1055 | "unable to read .gitmodules blob"); |
| 1056 | continue; |
| 1057 | } |
| 1058 | |
| 1059 | if (type == OBJ_BLOB) |
Jeff King | b8b00f1 | 2019-10-18 00:59:54 -0400 | [diff] [blame] | 1060 | ret |= fsck_blob(oid, buf, size, options); |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 1061 | else |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 1062 | ret |= report(options, |
Jeff King | b8b00f1 | 2019-10-18 00:59:54 -0400 | [diff] [blame] | 1063 | oid, type, |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 1064 | FSCK_MSG_GITMODULES_BLOB, |
| 1065 | "non-blob found at .gitmodules"); |
| 1066 | free(buf); |
| 1067 | } |
| 1068 | |
| 1069 | |
| 1070 | oidset_clear(&gitmodules_found); |
| 1071 | oidset_clear(&gitmodules_done); |
| 1072 | return ret; |
| 1073 | } |