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" |
Jonathan Nieder | a2b26ff | 2020-04-18 20:52:34 -0700 | [diff] [blame] | 12 | #include "url.h" |
Jeff King | a18fcc9 | 2014-12-15 18:21:57 -0500 | [diff] [blame] | 13 | #include "utf8.h" |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 14 | #include "decorate.h" |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 15 | #include "oidset.h" |
Jeff King | 2738744 | 2018-05-14 12:22:48 -0400 | [diff] [blame] | 16 | #include "packfile.h" |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 17 | #include "submodule-config.h" |
| 18 | #include "config.h" |
Jeff King | 07259e7 | 2020-03-11 18:48:24 -0400 | [diff] [blame] | 19 | #include "credential.h" |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 20 | #include "help.h" |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 21 | |
| 22 | static struct oidset gitmodules_found = OIDSET_INIT; |
| 23 | static struct oidset gitmodules_done = OIDSET_INIT; |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 24 | |
Johannes Schindelin | f50c440 | 2015-06-22 17:26:42 +0200 | [diff] [blame] | 25 | #define FSCK_FATAL -1 |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 26 | #define FSCK_INFO -2 |
Johannes Schindelin | f50c440 | 2015-06-22 17:26:42 +0200 | [diff] [blame] | 27 | |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 28 | #define FOREACH_MSG_ID(FUNC) \ |
Johannes Schindelin | f50c440 | 2015-06-22 17:26:42 +0200 | [diff] [blame] | 29 | /* fatal errors */ \ |
| 30 | FUNC(NUL_IN_HEADER, FATAL) \ |
| 31 | FUNC(UNTERMINATED_HEADER, FATAL) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 32 | /* errors */ \ |
| 33 | FUNC(BAD_DATE, ERROR) \ |
| 34 | FUNC(BAD_DATE_OVERFLOW, ERROR) \ |
| 35 | FUNC(BAD_EMAIL, ERROR) \ |
| 36 | FUNC(BAD_NAME, ERROR) \ |
| 37 | FUNC(BAD_OBJECT_SHA1, ERROR) \ |
| 38 | FUNC(BAD_PARENT_SHA1, ERROR) \ |
| 39 | FUNC(BAD_TAG_OBJECT, ERROR) \ |
| 40 | FUNC(BAD_TIMEZONE, ERROR) \ |
| 41 | FUNC(BAD_TREE, ERROR) \ |
| 42 | FUNC(BAD_TREE_SHA1, ERROR) \ |
| 43 | FUNC(BAD_TYPE, ERROR) \ |
| 44 | FUNC(DUPLICATE_ENTRIES, ERROR) \ |
| 45 | FUNC(MISSING_AUTHOR, ERROR) \ |
| 46 | FUNC(MISSING_COMMITTER, ERROR) \ |
| 47 | FUNC(MISSING_EMAIL, ERROR) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 48 | FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \ |
| 49 | FUNC(MISSING_OBJECT, ERROR) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 50 | FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \ |
| 51 | FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \ |
| 52 | FUNC(MISSING_TAG, ERROR) \ |
| 53 | FUNC(MISSING_TAG_ENTRY, ERROR) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 54 | FUNC(MISSING_TREE, ERROR) \ |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 55 | FUNC(MISSING_TREE_OBJECT, ERROR) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 56 | FUNC(MISSING_TYPE, ERROR) \ |
| 57 | FUNC(MISSING_TYPE_ENTRY, ERROR) \ |
Johannes Schindelin | c9ad147 | 2015-06-22 17:26:23 +0200 | [diff] [blame] | 58 | FUNC(MULTIPLE_AUTHORS, ERROR) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 59 | FUNC(TREE_NOT_SORTED, ERROR) \ |
| 60 | FUNC(UNKNOWN_TYPE, ERROR) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 61 | FUNC(ZERO_PADDED_DATE, ERROR) \ |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 62 | FUNC(GITMODULES_MISSING, ERROR) \ |
| 63 | FUNC(GITMODULES_BLOB, ERROR) \ |
Jeff King | 0d68764 | 2018-07-13 15:39:53 -0400 | [diff] [blame] | 64 | FUNC(GITMODULES_LARGE, ERROR) \ |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 65 | FUNC(GITMODULES_NAME, ERROR) \ |
Jeff King | b7b1fca | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 66 | FUNC(GITMODULES_SYMLINK, ERROR) \ |
Jeff King | a124133 | 2018-09-24 04:37:17 -0400 | [diff] [blame] | 67 | FUNC(GITMODULES_URL, ERROR) \ |
Jeff King | 1a7fd1f | 2018-09-24 04:42:19 -0400 | [diff] [blame] | 68 | FUNC(GITMODULES_PATH, ERROR) \ |
Jonathan Nieder | bb92255 | 2019-12-05 01:30:43 -0800 | [diff] [blame] | 69 | FUNC(GITMODULES_UPDATE, ERROR) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 70 | /* warnings */ \ |
| 71 | FUNC(BAD_FILEMODE, WARN) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 72 | FUNC(EMPTY_NAME, WARN) \ |
| 73 | FUNC(FULL_PATHNAME, WARN) \ |
| 74 | FUNC(HAS_DOT, WARN) \ |
| 75 | FUNC(HAS_DOTDOT, WARN) \ |
| 76 | FUNC(HAS_DOTGIT, WARN) \ |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 77 | FUNC(NULL_SHA1, WARN) \ |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 78 | FUNC(ZERO_PADDED_FILEMODE, WARN) \ |
Junio C Hamano | 6d2d780 | 2016-04-14 10:58:22 -0700 | [diff] [blame] | 79 | FUNC(NUL_IN_COMMIT, WARN) \ |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 80 | /* infos (reported as warnings, but ignored by default) */ \ |
Jeff King | 64eb14d | 2018-07-13 15:39:58 -0400 | [diff] [blame] | 81 | FUNC(GITMODULES_PARSE, INFO) \ |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 82 | FUNC(BAD_TAG_NAME, INFO) \ |
| 83 | FUNC(MISSING_TAGGER_ENTRY, INFO) |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 84 | |
| 85 | #define MSG_ID(id, msg_type) FSCK_MSG_##id, |
| 86 | enum fsck_msg_id { |
| 87 | FOREACH_MSG_ID(MSG_ID) |
| 88 | FSCK_MSG_MAX |
| 89 | }; |
| 90 | #undef MSG_ID |
| 91 | |
Johannes Schindelin | f417eed | 2015-06-22 17:25:14 +0200 | [diff] [blame] | 92 | #define STR(x) #x |
Nguyễn Thái Ngọc Duy | a4a9cc1 | 2018-05-26 15:55:25 +0200 | [diff] [blame] | 93 | #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] | 94 | static struct { |
Johannes Schindelin | f417eed | 2015-06-22 17:25:14 +0200 | [diff] [blame] | 95 | const char *id_string; |
| 96 | const char *downcased; |
Nguyễn Thái Ngọc Duy | a4a9cc1 | 2018-05-26 15:55:25 +0200 | [diff] [blame] | 97 | const char *camelcased; |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 98 | int msg_type; |
| 99 | } msg_id_info[FSCK_MSG_MAX + 1] = { |
| 100 | FOREACH_MSG_ID(MSG_ID) |
Nguyễn Thái Ngọc Duy | a4a9cc1 | 2018-05-26 15:55:25 +0200 | [diff] [blame] | 101 | { NULL, NULL, NULL, -1 } |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 102 | }; |
| 103 | #undef MSG_ID |
| 104 | |
Nguyễn Thái Ngọc Duy | a46baac | 2018-05-26 15:55:23 +0200 | [diff] [blame] | 105 | static void prepare_msg_ids(void) |
| 106 | { |
| 107 | int i; |
| 108 | |
| 109 | if (msg_id_info[0].downcased) |
| 110 | return; |
| 111 | |
| 112 | /* convert id_string to lower case, without underscores. */ |
| 113 | for (i = 0; i < FSCK_MSG_MAX; i++) { |
| 114 | const char *p = msg_id_info[i].id_string; |
| 115 | int len = strlen(p); |
| 116 | char *q = xmalloc(len); |
| 117 | |
| 118 | msg_id_info[i].downcased = q; |
| 119 | while (*p) |
| 120 | if (*p == '_') |
| 121 | p++; |
| 122 | else |
| 123 | *(q)++ = tolower(*(p)++); |
| 124 | *q = '\0'; |
Nguyễn Thái Ngọc Duy | a4a9cc1 | 2018-05-26 15:55:25 +0200 | [diff] [blame] | 125 | |
| 126 | p = msg_id_info[i].id_string; |
| 127 | q = xmalloc(len); |
| 128 | msg_id_info[i].camelcased = q; |
| 129 | while (*p) { |
| 130 | if (*p == '_') { |
| 131 | p++; |
| 132 | if (*p) |
| 133 | *q++ = *p++; |
| 134 | } else { |
| 135 | *q++ = tolower(*p++); |
| 136 | } |
| 137 | } |
| 138 | *q = '\0'; |
Nguyễn Thái Ngọc Duy | a46baac | 2018-05-26 15:55:23 +0200 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Johannes Schindelin | f417eed | 2015-06-22 17:25:14 +0200 | [diff] [blame] | 142 | static int parse_msg_id(const char *text) |
| 143 | { |
| 144 | int i; |
| 145 | |
Nguyễn Thái Ngọc Duy | a46baac | 2018-05-26 15:55:23 +0200 | [diff] [blame] | 146 | prepare_msg_ids(); |
Johannes Schindelin | f417eed | 2015-06-22 17:25:14 +0200 | [diff] [blame] | 147 | |
| 148 | for (i = 0; i < FSCK_MSG_MAX; i++) |
| 149 | if (!strcmp(text, msg_id_info[i].downcased)) |
| 150 | return i; |
| 151 | |
| 152 | return -1; |
| 153 | } |
| 154 | |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 155 | void list_config_fsck_msg_ids(struct string_list *list, const char *prefix) |
| 156 | { |
| 157 | int i; |
| 158 | |
| 159 | prepare_msg_ids(); |
| 160 | |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 161 | 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] | 162 | 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] | 163 | } |
| 164 | |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 165 | static int fsck_msg_type(enum fsck_msg_id msg_id, |
| 166 | struct fsck_options *options) |
| 167 | { |
| 168 | int msg_type; |
| 169 | |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 170 | assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX); |
| 171 | |
| 172 | if (options->msg_type) |
| 173 | msg_type = options->msg_type[msg_id]; |
| 174 | else { |
| 175 | msg_type = msg_id_info[msg_id].msg_type; |
| 176 | if (options->strict && msg_type == FSCK_WARN) |
| 177 | msg_type = FSCK_ERROR; |
| 178 | } |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 179 | |
| 180 | return msg_type; |
| 181 | } |
| 182 | |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 183 | static int parse_msg_type(const char *str) |
| 184 | { |
| 185 | if (!strcmp(str, "error")) |
| 186 | return FSCK_ERROR; |
| 187 | else if (!strcmp(str, "warn")) |
| 188 | return FSCK_WARN; |
Johannes Schindelin | efaba7c | 2015-06-22 17:26:48 +0200 | [diff] [blame] | 189 | else if (!strcmp(str, "ignore")) |
| 190 | return FSCK_IGNORE; |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 191 | else |
| 192 | die("Unknown fsck message type: '%s'", str); |
| 193 | } |
| 194 | |
Johannes Schindelin | 5d477a3 | 2015-06-22 17:25:31 +0200 | [diff] [blame] | 195 | int is_valid_msg_type(const char *msg_id, const char *msg_type) |
| 196 | { |
| 197 | if (parse_msg_id(msg_id) < 0) |
| 198 | return 0; |
| 199 | parse_msg_type(msg_type); |
| 200 | return 1; |
| 201 | } |
| 202 | |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 203 | void fsck_set_msg_type(struct fsck_options *options, |
| 204 | const char *msg_id, const char *msg_type) |
| 205 | { |
| 206 | int id = parse_msg_id(msg_id), type; |
| 207 | |
| 208 | if (id < 0) |
| 209 | die("Unhandled message id: %s", msg_id); |
| 210 | type = parse_msg_type(msg_type); |
| 211 | |
Johannes Schindelin | f50c440 | 2015-06-22 17:26:42 +0200 | [diff] [blame] | 212 | if (type != FSCK_ERROR && msg_id_info[id].msg_type == FSCK_FATAL) |
| 213 | die("Cannot demote %s to %s", msg_id, msg_type); |
| 214 | |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 215 | if (!options->msg_type) { |
| 216 | int i; |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 217 | int *msg_type; |
| 218 | ALLOC_ARRAY(msg_type, FSCK_MSG_MAX); |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 219 | for (i = 0; i < FSCK_MSG_MAX; i++) |
| 220 | msg_type[i] = fsck_msg_type(i, options); |
| 221 | options->msg_type = msg_type; |
| 222 | } |
| 223 | |
| 224 | options->msg_type[id] = type; |
| 225 | } |
| 226 | |
| 227 | void fsck_set_msg_types(struct fsck_options *options, const char *values) |
| 228 | { |
| 229 | char *buf = xstrdup(values), *to_free = buf; |
| 230 | int done = 0; |
| 231 | |
| 232 | while (!done) { |
| 233 | int len = strcspn(buf, " ,|"), equal; |
| 234 | |
| 235 | done = !buf[len]; |
| 236 | if (!len) { |
| 237 | buf++; |
| 238 | continue; |
| 239 | } |
| 240 | buf[len] = '\0'; |
| 241 | |
| 242 | for (equal = 0; |
| 243 | equal < len && buf[equal] != '=' && buf[equal] != ':'; |
| 244 | equal++) |
| 245 | buf[equal] = tolower(buf[equal]); |
| 246 | buf[equal] = '\0'; |
| 247 | |
Johannes Schindelin | cd94c6f | 2015-06-22 17:27:18 +0200 | [diff] [blame] | 248 | if (!strcmp(buf, "skiplist")) { |
| 249 | if (equal == len) |
| 250 | die("skiplist requires a path"); |
Barret Rhoden | 24eb33e | 2019-05-15 17:44:56 -0400 | [diff] [blame] | 251 | oidset_parse_file(&options->skiplist, buf + equal + 1); |
Johannes Schindelin | cd94c6f | 2015-06-22 17:27:18 +0200 | [diff] [blame] | 252 | buf += len + 1; |
| 253 | continue; |
| 254 | } |
| 255 | |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 256 | if (equal == len) |
| 257 | die("Missing '=': '%s'", buf); |
| 258 | |
| 259 | fsck_set_msg_type(options, buf, buf + equal + 1); |
| 260 | buf += len + 1; |
| 261 | } |
| 262 | free(to_free); |
| 263 | } |
| 264 | |
Johannes Schindelin | 71ab8fa | 2015-06-22 17:25:52 +0200 | [diff] [blame] | 265 | static void append_msg_id(struct strbuf *sb, const char *msg_id) |
| 266 | { |
| 267 | for (;;) { |
| 268 | char c = *(msg_id)++; |
| 269 | |
| 270 | if (!c) |
| 271 | break; |
| 272 | if (c != '_') |
| 273 | strbuf_addch(sb, tolower(c)); |
| 274 | else { |
| 275 | assert(*msg_id); |
| 276 | strbuf_addch(sb, *(msg_id)++); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | strbuf_addstr(sb, ": "); |
| 281 | } |
| 282 | |
Jeff King | f597937 | 2019-10-18 00:58:51 -0400 | [diff] [blame] | 283 | static int object_on_skiplist(struct fsck_options *opts, |
| 284 | const struct object_id *oid) |
Ramsay Jones | fb16287 | 2018-06-27 19:39:53 +0100 | [diff] [blame] | 285 | { |
Jeff King | f597937 | 2019-10-18 00:58:51 -0400 | [diff] [blame] | 286 | return opts && oid && oidset_contains(&opts->skiplist, oid); |
Ramsay Jones | fb16287 | 2018-06-27 19:39:53 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 289 | __attribute__((format (printf, 5, 6))) |
| 290 | static int report(struct fsck_options *options, |
| 291 | const struct object_id *oid, enum object_type object_type, |
| 292 | enum fsck_msg_id id, const char *fmt, ...) |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 293 | { |
| 294 | va_list ap; |
| 295 | struct strbuf sb = STRBUF_INIT; |
| 296 | int msg_type = fsck_msg_type(id, options), result; |
| 297 | |
Johannes Schindelin | efaba7c | 2015-06-22 17:26:48 +0200 | [diff] [blame] | 298 | if (msg_type == FSCK_IGNORE) |
| 299 | return 0; |
| 300 | |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 301 | if (object_on_skiplist(options, oid)) |
Johannes Schindelin | cd94c6f | 2015-06-22 17:27:18 +0200 | [diff] [blame] | 302 | return 0; |
| 303 | |
Johannes Schindelin | f50c440 | 2015-06-22 17:26:42 +0200 | [diff] [blame] | 304 | if (msg_type == FSCK_FATAL) |
| 305 | msg_type = FSCK_ERROR; |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 306 | else if (msg_type == FSCK_INFO) |
| 307 | msg_type = FSCK_WARN; |
Johannes Schindelin | f50c440 | 2015-06-22 17:26:42 +0200 | [diff] [blame] | 308 | |
Johannes Schindelin | 71ab8fa | 2015-06-22 17:25:52 +0200 | [diff] [blame] | 309 | append_msg_id(&sb, msg_id_info[id].id_string); |
| 310 | |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 311 | va_start(ap, fmt); |
| 312 | strbuf_vaddf(&sb, fmt, ap); |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 313 | result = options->error_func(options, oid, object_type, |
Jeff King | 5afc4b1 | 2019-10-18 00:58:40 -0400 | [diff] [blame] | 314 | msg_type, sb.buf); |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 315 | strbuf_release(&sb); |
| 316 | va_end(ap); |
| 317 | |
| 318 | return result; |
| 319 | } |
| 320 | |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 321 | void fsck_enable_object_names(struct fsck_options *options) |
| 322 | { |
| 323 | if (!options->object_names) |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 324 | options->object_names = kh_init_oid_map(); |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 325 | } |
| 326 | |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 327 | const char *fsck_get_object_name(struct fsck_options *options, |
| 328 | const struct object_id *oid) |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 329 | { |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 330 | khiter_t pos; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 331 | if (!options->object_names) |
| 332 | return NULL; |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 333 | pos = kh_get_oid_map(options->object_names, *oid); |
| 334 | if (pos >= kh_end(options->object_names)) |
| 335 | return NULL; |
| 336 | return kh_value(options->object_names, pos); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 337 | } |
| 338 | |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 339 | void fsck_put_object_name(struct fsck_options *options, |
| 340 | const struct object_id *oid, |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 341 | const char *fmt, ...) |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 342 | { |
| 343 | va_list ap; |
| 344 | struct strbuf buf = STRBUF_INIT; |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 345 | khiter_t pos; |
| 346 | int hashret; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 347 | |
| 348 | if (!options->object_names) |
| 349 | return; |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 350 | |
| 351 | pos = kh_put_oid_map(options->object_names, *oid, &hashret); |
| 352 | if (!hashret) |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 353 | return; |
| 354 | va_start(ap, fmt); |
| 355 | strbuf_vaddf(&buf, fmt, ap); |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 356 | kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 357 | va_end(ap); |
| 358 | } |
| 359 | |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 360 | const char *fsck_describe_object(struct fsck_options *options, |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 361 | const struct object_id *oid) |
Johannes Schindelin | 90cf590 | 2016-07-17 13:00:02 +0200 | [diff] [blame] | 362 | { |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 363 | static struct strbuf bufs[] = { |
| 364 | STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT |
| 365 | }; |
| 366 | static int b = 0; |
| 367 | struct strbuf *buf; |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 368 | const char *name = fsck_get_object_name(options, oid); |
Johannes Schindelin | 90cf590 | 2016-07-17 13:00:02 +0200 | [diff] [blame] | 369 | |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 370 | buf = bufs + b; |
| 371 | b = (b + 1) % ARRAY_SIZE(bufs); |
| 372 | strbuf_reset(buf); |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 373 | strbuf_addstr(buf, oid_to_hex(oid)); |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 374 | if (name) |
| 375 | strbuf_addf(buf, " (%s)", name); |
| 376 | |
| 377 | return buf->buf; |
Johannes Schindelin | 90cf590 | 2016-07-17 13:00:02 +0200 | [diff] [blame] | 378 | } |
| 379 | |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 380 | 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] | 381 | { |
| 382 | struct tree_desc desc; |
| 383 | struct name_entry entry; |
| 384 | int res = 0; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 385 | const char *name; |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 386 | |
| 387 | if (parse_tree(tree)) |
| 388 | return -1; |
| 389 | |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 390 | name = fsck_get_object_name(options, &tree->object.oid); |
David Turner | 8354fa3 | 2016-09-27 16:59:51 -0400 | [diff] [blame] | 391 | if (init_tree_desc_gently(&desc, tree->buffer, tree->size)) |
| 392 | return -1; |
| 393 | while (tree_entry_gently(&desc, &entry)) { |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 394 | struct object *obj; |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 395 | int result; |
| 396 | |
| 397 | if (S_ISGITLINK(entry.mode)) |
| 398 | continue; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 399 | |
| 400 | if (S_ISDIR(entry.mode)) { |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 401 | obj = (struct object *)lookup_tree(the_repository, &entry.oid); |
René Scharfe | 2720f6d | 2017-10-05 21:41:26 +0200 | [diff] [blame] | 402 | if (name && obj) |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 403 | fsck_put_object_name(options, &entry.oid, "%s%s/", |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 404 | name, entry.path); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 405 | result = options->walk(obj, OBJ_TREE, data, options); |
| 406 | } |
| 407 | else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) { |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 408 | obj = (struct object *)lookup_blob(the_repository, &entry.oid); |
René Scharfe | 2720f6d | 2017-10-05 21:41:26 +0200 | [diff] [blame] | 409 | if (name && obj) |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 410 | fsck_put_object_name(options, &entry.oid, "%s%s", |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 411 | name, entry.path); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 412 | result = options->walk(obj, OBJ_BLOB, data, options); |
| 413 | } |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 414 | else { |
Pete Wyckoff | 82247e9 | 2012-04-29 20:28:45 -0400 | [diff] [blame] | 415 | result = error("in tree %s: entry %s has bad mode %.6o", |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 416 | fsck_describe_object(options, &tree->object.oid), |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 417 | entry.path, entry.mode); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 418 | } |
| 419 | if (result < 0) |
| 420 | return result; |
| 421 | if (!res) |
| 422 | res = result; |
| 423 | } |
| 424 | return res; |
| 425 | } |
| 426 | |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 427 | 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] | 428 | { |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 429 | int counter = 0, generation = 0, name_prefix_len = 0; |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 430 | struct commit_list *parents; |
| 431 | int res; |
| 432 | int result; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 433 | const char *name; |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 434 | |
| 435 | if (parse_commit(commit)) |
| 436 | return -1; |
| 437 | |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 438 | name = fsck_get_object_name(options, &commit->object.oid); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 439 | if (name) |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 440 | fsck_put_object_name(options, get_commit_tree_oid(commit), |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 441 | "%s:", name); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 442 | |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 443 | result = options->walk((struct object *)get_commit_tree(commit), |
| 444 | OBJ_TREE, data, options); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 445 | if (result < 0) |
| 446 | return result; |
| 447 | res = result; |
| 448 | |
| 449 | parents = commit->parents; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 450 | if (name && parents) { |
| 451 | int len = strlen(name), power; |
| 452 | |
| 453 | if (len && name[len - 1] == '^') { |
| 454 | generation = 1; |
| 455 | name_prefix_len = len - 1; |
| 456 | } |
| 457 | else { /* parse ~<generation> suffix */ |
| 458 | for (generation = 0, power = 1; |
| 459 | len && isdigit(name[len - 1]); |
| 460 | power *= 10) |
| 461 | generation += power * (name[--len] - '0'); |
| 462 | if (power > 1 && len && name[len - 1] == '~') |
| 463 | name_prefix_len = len - 1; |
| 464 | } |
| 465 | } |
| 466 | |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 467 | while (parents) { |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 468 | if (name) { |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 469 | struct object_id *oid = &parents->item->object.oid; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 470 | |
Junio C Hamano | b84c7838 | 2018-10-24 10:25:12 +0900 | [diff] [blame] | 471 | if (counter++) |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 472 | fsck_put_object_name(options, oid, "%s^%d", |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 473 | name, counter); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 474 | else if (generation > 0) |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 475 | fsck_put_object_name(options, oid, "%.*s~%d", |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 476 | name_prefix_len, name, |
| 477 | generation + 1); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 478 | else |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 479 | fsck_put_object_name(options, oid, "%s^", name); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 480 | } |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 481 | result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 482 | if (result < 0) |
| 483 | return result; |
| 484 | if (!res) |
| 485 | res = result; |
| 486 | parents = parents->next; |
| 487 | } |
| 488 | return res; |
| 489 | } |
| 490 | |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 491 | 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] | 492 | { |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 493 | const char *name = fsck_get_object_name(options, &tag->object.oid); |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 494 | |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 495 | if (parse_tag(tag)) |
| 496 | return -1; |
Johannes Schindelin | 7b35efd | 2016-07-17 12:59:49 +0200 | [diff] [blame] | 497 | if (name) |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 498 | fsck_put_object_name(options, &tag->tagged->oid, "%s", name); |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 499 | return options->walk(tag->tagged, OBJ_ANY, data, options); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 502 | int fsck_walk(struct object *obj, void *data, struct fsck_options *options) |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 503 | { |
| 504 | if (!obj) |
| 505 | return -1; |
Jeff King | a2b2285 | 2017-01-25 23:12:07 -0500 | [diff] [blame] | 506 | |
| 507 | if (obj->type == OBJ_NONE) |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 508 | parse_object(the_repository, &obj->oid); |
Jeff King | a2b2285 | 2017-01-25 23:12:07 -0500 | [diff] [blame] | 509 | |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 510 | switch (obj->type) { |
| 511 | case OBJ_BLOB: |
| 512 | return 0; |
| 513 | case OBJ_TREE: |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 514 | return fsck_walk_tree((struct tree *)obj, data, options); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 515 | case OBJ_COMMIT: |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 516 | return fsck_walk_commit((struct commit *)obj, data, options); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 517 | case OBJ_TAG: |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 518 | return fsck_walk_tag((struct tag *)obj, data, options); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 519 | default: |
Jeff King | a59cfb3 | 2019-10-18 00:56:13 -0400 | [diff] [blame] | 520 | error("Unknown object type for %s", |
Jeff King | 7339029 | 2019-10-18 00:57:37 -0400 | [diff] [blame] | 521 | fsck_describe_object(options, &obj->oid)); |
Martin Koegler | 355885d | 2008-02-25 22:46:04 +0100 | [diff] [blame] | 522 | return -1; |
| 523 | } |
| 524 | } |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 525 | |
René Scharfe | 9068cfb | 2020-05-10 18:12:16 +0200 | [diff] [blame] | 526 | struct name_stack { |
| 527 | const char **names; |
| 528 | size_t nr, alloc; |
| 529 | }; |
| 530 | |
| 531 | static void name_stack_push(struct name_stack *stack, const char *name) |
| 532 | { |
| 533 | ALLOC_GROW(stack->names, stack->nr + 1, stack->alloc); |
| 534 | stack->names[stack->nr++] = name; |
| 535 | } |
| 536 | |
| 537 | static const char *name_stack_pop(struct name_stack *stack) |
| 538 | { |
| 539 | return stack->nr ? stack->names[--stack->nr] : NULL; |
| 540 | } |
| 541 | |
| 542 | static void name_stack_clear(struct name_stack *stack) |
| 543 | { |
| 544 | FREE_AND_NULL(stack->names); |
| 545 | stack->nr = stack->alloc = 0; |
| 546 | } |
| 547 | |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 548 | /* |
| 549 | * The entries in a tree are ordered in the _path_ order, |
| 550 | * which means that a directory entry is ordered by adding |
| 551 | * a slash to the end of it. |
| 552 | * |
| 553 | * So a directory called "a" is ordered _after_ a file |
| 554 | * called "a.c", because "a/" sorts after "a.c". |
| 555 | */ |
| 556 | #define TREE_UNORDERED (-1) |
| 557 | #define TREE_HAS_DUPS (-2) |
| 558 | |
René Scharfe | 9068cfb | 2020-05-10 18:12:16 +0200 | [diff] [blame] | 559 | static int is_less_than_slash(unsigned char c) |
| 560 | { |
| 561 | return '\0' < c && c < '/'; |
| 562 | } |
| 563 | |
| 564 | static int verify_ordered(unsigned mode1, const char *name1, |
| 565 | unsigned mode2, const char *name2, |
| 566 | struct name_stack *candidates) |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 567 | { |
| 568 | int len1 = strlen(name1); |
| 569 | int len2 = strlen(name2); |
| 570 | int len = len1 < len2 ? len1 : len2; |
| 571 | unsigned char c1, c2; |
| 572 | int cmp; |
| 573 | |
| 574 | cmp = memcmp(name1, name2, len); |
| 575 | if (cmp < 0) |
| 576 | return 0; |
| 577 | if (cmp > 0) |
| 578 | return TREE_UNORDERED; |
| 579 | |
| 580 | /* |
| 581 | * Ok, the first <len> characters are the same. |
| 582 | * Now we need to order the next one, but turn |
| 583 | * a '\0' into a '/' for a directory entry. |
| 584 | */ |
| 585 | c1 = name1[len]; |
| 586 | c2 = name2[len]; |
| 587 | if (!c1 && !c2) |
| 588 | /* |
| 589 | * git-write-tree used to write out a nonsense tree that has |
| 590 | * entries with the same name, one blob and one tree. Make |
| 591 | * sure we do not have duplicate entries. |
| 592 | */ |
| 593 | return TREE_HAS_DUPS; |
| 594 | if (!c1 && S_ISDIR(mode1)) |
| 595 | c1 = '/'; |
| 596 | if (!c2 && S_ISDIR(mode2)) |
| 597 | c2 = '/'; |
René Scharfe | 9068cfb | 2020-05-10 18:12:16 +0200 | [diff] [blame] | 598 | |
| 599 | /* |
| 600 | * There can be non-consecutive duplicates due to the implicitly |
| 601 | * add slash, e.g.: |
| 602 | * |
| 603 | * foo |
| 604 | * foo.bar |
| 605 | * foo.bar.baz |
| 606 | * foo.bar/ |
| 607 | * foo/ |
| 608 | * |
| 609 | * Record non-directory candidates (like "foo" and "foo.bar" in |
| 610 | * the example) on a stack and check directory candidates (like |
| 611 | * foo/" and "foo.bar/") against that stack. |
| 612 | */ |
| 613 | if (!c1 && is_less_than_slash(c2)) { |
| 614 | name_stack_push(candidates, name1); |
| 615 | } else if (c2 == '/' && is_less_than_slash(c1)) { |
| 616 | for (;;) { |
| 617 | const char *p; |
| 618 | const char *f_name = name_stack_pop(candidates); |
| 619 | |
| 620 | if (!f_name) |
| 621 | break; |
| 622 | if (!skip_prefix(name2, f_name, &p)) |
| 623 | break; |
| 624 | if (!*p) |
| 625 | return TREE_HAS_DUPS; |
| 626 | if (is_less_than_slash(*p)) { |
| 627 | name_stack_push(candidates, f_name); |
| 628 | break; |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 633 | return c1 < c2 ? 0 : TREE_UNORDERED; |
| 634 | } |
| 635 | |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 636 | static int fsck_tree(const struct object_id *oid, |
Jeff King | 23a173a | 2019-10-18 00:54:12 -0400 | [diff] [blame] | 637 | const char *buffer, unsigned long size, |
| 638 | struct fsck_options *options) |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 639 | { |
David Turner | 8354fa3 | 2016-09-27 16:59:51 -0400 | [diff] [blame] | 640 | int retval = 0; |
Jeff King | c479d14 | 2012-07-28 11:06:29 -0400 | [diff] [blame] | 641 | int has_null_sha1 = 0; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 642 | int has_full_path = 0; |
| 643 | int has_empty_name = 0; |
Jeff King | 5d34a43 | 2012-11-27 21:27:37 -0500 | [diff] [blame] | 644 | int has_dot = 0; |
| 645 | int has_dotdot = 0; |
Jeff King | 5c17f51 | 2012-11-28 16:35:29 -0500 | [diff] [blame] | 646 | int has_dotgit = 0; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 647 | int has_zero_pad = 0; |
| 648 | int has_bad_modes = 0; |
| 649 | int has_dup_entries = 0; |
| 650 | int not_properly_sorted = 0; |
| 651 | struct tree_desc desc; |
| 652 | unsigned o_mode; |
| 653 | const char *o_name; |
René Scharfe | 9068cfb | 2020-05-10 18:12:16 +0200 | [diff] [blame] | 654 | struct name_stack df_dup_candidates = { NULL }; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 655 | |
Jeff King | 23a173a | 2019-10-18 00:54:12 -0400 | [diff] [blame] | 656 | if (init_tree_desc_gently(&desc, buffer, size)) { |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 657 | 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] | 658 | return retval; |
| 659 | } |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 660 | |
| 661 | o_mode = 0; |
| 662 | o_name = NULL; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 663 | |
| 664 | while (desc.size) { |
Elijah Newren | 5ec1e72 | 2019-04-05 08:00:12 -0700 | [diff] [blame] | 665 | unsigned short mode; |
Johannes Schindelin | 288a74b | 2019-09-23 08:58:11 +0200 | [diff] [blame] | 666 | const char *name, *backslash; |
brian m. carlson | ce6663a | 2016-04-17 23:10:40 +0000 | [diff] [blame] | 667 | const struct object_id *oid; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 668 | |
brian m. carlson | ce6663a | 2016-04-17 23:10:40 +0000 | [diff] [blame] | 669 | oid = tree_entry_extract(&desc, &name, &mode); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 670 | |
brian m. carlson | ce6663a | 2016-04-17 23:10:40 +0000 | [diff] [blame] | 671 | has_null_sha1 |= is_null_oid(oid); |
Hiroyuki Sano | effd12e | 2014-03-20 08:02:04 +0900 | [diff] [blame] | 672 | has_full_path |= !!strchr(name, '/'); |
| 673 | has_empty_name |= !*name; |
| 674 | has_dot |= !strcmp(name, "."); |
| 675 | has_dotdot |= !strcmp(name, ".."); |
Jeff King | ed9c322 | 2018-05-13 12:35:37 -0400 | [diff] [blame] | 676 | has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 677 | has_zero_pad |= *(char *)desc.buffer == '0'; |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 678 | |
Jeff King | b7b1fca | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 679 | if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) { |
| 680 | if (!S_ISLNK(mode)) |
| 681 | oidset_insert(&gitmodules_found, oid); |
| 682 | else |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 683 | retval += report(options, |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 684 | oid, OBJ_TREE, |
Jeff King | b7b1fca | 2018-05-04 20:03:35 -0400 | [diff] [blame] | 685 | FSCK_MSG_GITMODULES_SYMLINK, |
| 686 | ".gitmodules is a symbolic link"); |
| 687 | } |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 688 | |
Johannes Schindelin | 288a74b | 2019-09-23 08:58:11 +0200 | [diff] [blame] | 689 | if ((backslash = strchr(name, '\\'))) { |
| 690 | while (backslash) { |
| 691 | backslash++; |
| 692 | has_dotgit |= is_ntfs_dotgit(backslash); |
Johannes Schindelin | bdfef04 | 2019-12-04 21:52:10 +0100 | [diff] [blame] | 693 | if (is_ntfs_dotgitmodules(backslash)) { |
| 694 | if (!S_ISLNK(mode)) |
| 695 | oidset_insert(&gitmodules_found, oid); |
| 696 | else |
Junio C Hamano | 7034cd0 | 2019-12-09 22:17:55 -0800 | [diff] [blame] | 697 | retval += report(options, oid, OBJ_TREE, |
Johannes Schindelin | bdfef04 | 2019-12-04 21:52:10 +0100 | [diff] [blame] | 698 | FSCK_MSG_GITMODULES_SYMLINK, |
| 699 | ".gitmodules is a symbolic link"); |
| 700 | } |
Johannes Schindelin | 288a74b | 2019-09-23 08:58:11 +0200 | [diff] [blame] | 701 | backslash = strchr(backslash, '\\'); |
| 702 | } |
| 703 | } |
| 704 | |
David Turner | 8354fa3 | 2016-09-27 16:59:51 -0400 | [diff] [blame] | 705 | if (update_tree_entry_gently(&desc)) { |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 706 | 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] | 707 | break; |
| 708 | } |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 709 | |
| 710 | switch (mode) { |
| 711 | /* |
| 712 | * Standard modes.. |
| 713 | */ |
| 714 | case S_IFREG | 0755: |
| 715 | case S_IFREG | 0644: |
| 716 | case S_IFLNK: |
| 717 | case S_IFDIR: |
| 718 | case S_IFGITLINK: |
| 719 | break; |
| 720 | /* |
| 721 | * This is nonstandard, but we had a few of these |
| 722 | * early on when we honored the full set of mode |
| 723 | * bits.. |
| 724 | */ |
| 725 | case S_IFREG | 0664: |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 726 | if (!options->strict) |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 727 | break; |
Jeff King | 1cf01a3 | 2017-09-21 02:25:41 -0400 | [diff] [blame] | 728 | /* fallthrough */ |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 729 | default: |
| 730 | has_bad_modes = 1; |
| 731 | } |
| 732 | |
| 733 | if (o_name) { |
René Scharfe | 9068cfb | 2020-05-10 18:12:16 +0200 | [diff] [blame] | 734 | switch (verify_ordered(o_mode, o_name, mode, name, |
| 735 | &df_dup_candidates)) { |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 736 | case TREE_UNORDERED: |
| 737 | not_properly_sorted = 1; |
| 738 | break; |
| 739 | case TREE_HAS_DUPS: |
| 740 | has_dup_entries = 1; |
| 741 | break; |
| 742 | default: |
| 743 | break; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | o_mode = mode; |
| 748 | o_name = name; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 749 | } |
| 750 | |
René Scharfe | 9068cfb | 2020-05-10 18:12:16 +0200 | [diff] [blame] | 751 | name_stack_clear(&df_dup_candidates); |
| 752 | |
Jeff King | c479d14 | 2012-07-28 11:06:29 -0400 | [diff] [blame] | 753 | if (has_null_sha1) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 754 | 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] | 755 | if (has_full_path) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 756 | 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] | 757 | if (has_empty_name) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 758 | 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] | 759 | if (has_dot) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 760 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOT, "contains '.'"); |
Jeff King | 5d34a43 | 2012-11-27 21:27:37 -0500 | [diff] [blame] | 761 | if (has_dotdot) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 762 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTDOT, "contains '..'"); |
Jeff King | 5c17f51 | 2012-11-28 16:35:29 -0500 | [diff] [blame] | 763 | if (has_dotgit) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 764 | retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTGIT, "contains '.git'"); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 765 | if (has_zero_pad) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 766 | 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] | 767 | if (has_bad_modes) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 768 | 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] | 769 | if (has_dup_entries) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 770 | 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] | 771 | if (not_properly_sorted) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 772 | 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] | 773 | return retval; |
| 774 | } |
| 775 | |
Junio C Hamano | 84d18c0 | 2015-06-28 11:18:31 -0700 | [diff] [blame] | 776 | static int verify_headers(const void *data, unsigned long size, |
Jeff King | cc57900 | 2019-10-18 01:00:50 -0400 | [diff] [blame] | 777 | const struct object_id *oid, enum object_type type, |
| 778 | struct fsck_options *options) |
Johannes Schindelin | 4d0d897 | 2014-09-11 16:26:33 +0200 | [diff] [blame] | 779 | { |
| 780 | const char *buffer = (const char *)data; |
| 781 | unsigned long i; |
| 782 | |
| 783 | for (i = 0; i < size; i++) { |
| 784 | switch (buffer[i]) { |
| 785 | case '\0': |
Jeff King | cc57900 | 2019-10-18 01:00:50 -0400 | [diff] [blame] | 786 | return report(options, oid, type, |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 787 | FSCK_MSG_NUL_IN_HEADER, |
| 788 | "unterminated header: NUL at offset %ld", i); |
Johannes Schindelin | 4d0d897 | 2014-09-11 16:26:33 +0200 | [diff] [blame] | 789 | case '\n': |
| 790 | if (i + 1 < size && buffer[i + 1] == '\n') |
| 791 | return 0; |
| 792 | } |
| 793 | } |
| 794 | |
Junio C Hamano | 84d18c0 | 2015-06-28 11:18:31 -0700 | [diff] [blame] | 795 | /* |
| 796 | * We did not find double-LF that separates the header |
| 797 | * and the body. Not having a body is not a crime but |
| 798 | * we do want to see the terminating LF for the last header |
| 799 | * line. |
| 800 | */ |
| 801 | if (size && buffer[size - 1] == '\n') |
| 802 | return 0; |
| 803 | |
Jeff King | cc57900 | 2019-10-18 01:00:50 -0400 | [diff] [blame] | 804 | return report(options, oid, type, |
Johannes Schindelin | c99ba49 | 2015-06-22 17:25:09 +0200 | [diff] [blame] | 805 | FSCK_MSG_UNTERMINATED_HEADER, "unterminated header"); |
Johannes Schindelin | 4d0d897 | 2014-09-11 16:26:33 +0200 | [diff] [blame] | 806 | } |
| 807 | |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 808 | static int fsck_ident(const char **ident, |
| 809 | const struct object_id *oid, enum object_type type, |
| 810 | struct fsck_options *options) |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 811 | { |
Johannes Schindelin | e6826e3 | 2015-06-22 17:26:03 +0200 | [diff] [blame] | 812 | const char *p = *ident; |
Jeff King | d4b8de0 | 2014-02-24 02:39:04 -0500 | [diff] [blame] | 813 | char *end; |
| 814 | |
Johannes Schindelin | e6826e3 | 2015-06-22 17:26:03 +0200 | [diff] [blame] | 815 | *ident = strchrnul(*ident, '\n'); |
| 816 | if (**ident == '\n') |
| 817 | (*ident)++; |
| 818 | |
| 819 | if (*p == '<') |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 820 | 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] | 821 | p += strcspn(p, "<>\n"); |
| 822 | if (*p == '>') |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 823 | 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] | 824 | if (*p != '<') |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 825 | 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] | 826 | if (p[-1] != ' ') |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 827 | 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] | 828 | p++; |
| 829 | p += strcspn(p, "<>\n"); |
| 830 | if (*p != '>') |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 831 | 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] | 832 | p++; |
| 833 | if (*p != ' ') |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 834 | 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] | 835 | p++; |
| 836 | if (*p == '0' && p[1] != ' ') |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 837 | 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] | 838 | if (date_overflows(parse_timestamp(p, &end, 10))) |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 839 | 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] | 840 | if ((end == p || *end != ' ')) |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 841 | 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] | 842 | p = end + 1; |
| 843 | if ((*p != '+' && *p != '-') || |
| 844 | !isdigit(p[1]) || |
| 845 | !isdigit(p[2]) || |
| 846 | !isdigit(p[3]) || |
| 847 | !isdigit(p[4]) || |
| 848 | (p[5] != '\n')) |
Jeff King | 7854399 | 2019-10-18 01:00:04 -0400 | [diff] [blame] | 849 | 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] | 850 | p += 6; |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 851 | return 0; |
| 852 | } |
| 853 | |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 854 | static int fsck_commit(const struct object_id *oid, |
| 855 | const char *buffer, unsigned long size, |
| 856 | struct fsck_options *options) |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 857 | { |
Jeff King | f648ee7 | 2019-10-18 01:00:59 -0400 | [diff] [blame] | 858 | struct object_id tree_oid, parent_oid; |
Jeff King | ec65231 | 2019-10-18 00:49:10 -0400 | [diff] [blame] | 859 | unsigned author_count; |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 860 | int err; |
Junio C Hamano | 6d2d780 | 2016-04-14 10:58:22 -0700 | [diff] [blame] | 861 | const char *buffer_begin = buffer; |
brian m. carlson | c54f5ca | 2018-05-02 00:25:41 +0000 | [diff] [blame] | 862 | const char *p; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 863 | |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 864 | if (verify_headers(buffer, size, oid, OBJ_COMMIT, options)) |
Johannes Schindelin | 4d0d897 | 2014-09-11 16:26:33 +0200 | [diff] [blame] | 865 | return -1; |
| 866 | |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 867 | if (!skip_prefix(buffer, "tree ", &buffer)) |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 868 | 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] | 869 | if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') { |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 870 | 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] | 871 | if (err) |
| 872 | return err; |
| 873 | } |
brian m. carlson | c54f5ca | 2018-05-02 00:25:41 +0000 | [diff] [blame] | 874 | buffer = p + 1; |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 875 | while (skip_prefix(buffer, "parent ", &buffer)) { |
Jeff King | f648ee7 | 2019-10-18 01:00:59 -0400 | [diff] [blame] | 876 | if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') { |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 877 | 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] | 878 | if (err) |
| 879 | return err; |
| 880 | } |
brian m. carlson | c54f5ca | 2018-05-02 00:25:41 +0000 | [diff] [blame] | 881 | buffer = p + 1; |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 882 | } |
Johannes Schindelin | c9ad147 | 2015-06-22 17:26:23 +0200 | [diff] [blame] | 883 | author_count = 0; |
| 884 | while (skip_prefix(buffer, "author ", &buffer)) { |
| 885 | author_count++; |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 886 | err = fsck_ident(&buffer, oid, OBJ_COMMIT, options); |
Johannes Schindelin | c9ad147 | 2015-06-22 17:26:23 +0200 | [diff] [blame] | 887 | if (err) |
| 888 | return err; |
| 889 | } |
| 890 | if (author_count < 1) |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 891 | 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] | 892 | else if (author_count > 1) |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 893 | 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] | 894 | if (err) |
| 895 | return err; |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 896 | if (!skip_prefix(buffer, "committer ", &buffer)) |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 897 | return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line"); |
| 898 | err = fsck_ident(&buffer, oid, OBJ_COMMIT, options); |
Jonathan Nieder | daae192 | 2010-04-24 11:06:08 -0500 | [diff] [blame] | 899 | if (err) |
| 900 | return err; |
Junio C Hamano | 6d2d780 | 2016-04-14 10:58:22 -0700 | [diff] [blame] | 901 | if (memchr(buffer_begin, '\0', size)) { |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 902 | err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT, |
Junio C Hamano | 6d2d780 | 2016-04-14 10:58:22 -0700 | [diff] [blame] | 903 | "NUL byte in the commit object body"); |
| 904 | if (err) |
| 905 | return err; |
| 906 | } |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 907 | return 0; |
| 908 | } |
| 909 | |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 910 | static int fsck_tag(const struct object_id *oid, const char *buffer, |
Jeff King | 2175a0c | 2019-10-18 00:51:19 -0400 | [diff] [blame] | 911 | unsigned long size, struct fsck_options *options) |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 912 | { |
Jeff King | f648ee7 | 2019-10-18 01:00:59 -0400 | [diff] [blame] | 913 | struct object_id tagged_oid; |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 914 | int ret = 0; |
Jeff King | 23a173a | 2019-10-18 00:54:12 -0400 | [diff] [blame] | 915 | char *eol; |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 916 | struct strbuf sb = STRBUF_INIT; |
brian m. carlson | c54f5ca | 2018-05-02 00:25:41 +0000 | [diff] [blame] | 917 | const char *p; |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 918 | |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 919 | ret = verify_headers(buffer, size, oid, OBJ_TAG, options); |
René Scharfe | 8a272f2 | 2015-11-19 17:25:31 +0100 | [diff] [blame] | 920 | if (ret) |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 921 | goto done; |
| 922 | |
| 923 | if (!skip_prefix(buffer, "object ", &buffer)) { |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 924 | 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] | 925 | goto done; |
| 926 | } |
Jeff King | f648ee7 | 2019-10-18 01:00:59 -0400 | [diff] [blame] | 927 | if (parse_oid_hex(buffer, &tagged_oid, &p) || *p != '\n') { |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 928 | 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] | 929 | if (ret) |
| 930 | goto done; |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 931 | } |
brian m. carlson | c54f5ca | 2018-05-02 00:25:41 +0000 | [diff] [blame] | 932 | buffer = p + 1; |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 933 | |
| 934 | if (!skip_prefix(buffer, "type ", &buffer)) { |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 935 | 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] | 936 | goto done; |
| 937 | } |
| 938 | eol = strchr(buffer, '\n'); |
| 939 | if (!eol) { |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 940 | 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] | 941 | goto done; |
| 942 | } |
| 943 | if (type_from_string_gently(buffer, eol - buffer, 1) < 0) |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 944 | 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] | 945 | if (ret) |
| 946 | goto done; |
| 947 | buffer = eol + 1; |
| 948 | |
| 949 | if (!skip_prefix(buffer, "tag ", &buffer)) { |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 950 | 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] | 951 | goto done; |
| 952 | } |
| 953 | eol = strchr(buffer, '\n'); |
| 954 | if (!eol) { |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 955 | 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] | 956 | goto done; |
| 957 | } |
| 958 | strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer); |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 959 | if (check_refname_format(sb.buf, 0)) { |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 960 | ret = report(options, oid, OBJ_TAG, |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 961 | FSCK_MSG_BAD_TAG_NAME, |
| 962 | "invalid 'tag' name: %.*s", |
| 963 | (int)(eol - buffer), buffer); |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 964 | if (ret) |
| 965 | goto done; |
| 966 | } |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 967 | buffer = eol + 1; |
| 968 | |
Johannes Schindelin | f27d05b | 2015-06-22 17:26:54 +0200 | [diff] [blame] | 969 | if (!skip_prefix(buffer, "tagger ", &buffer)) { |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 970 | /* early tags do not contain 'tagger' lines; warn only */ |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 971 | 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] | 972 | if (ret) |
| 973 | goto done; |
| 974 | } |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 975 | else |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 976 | ret = fsck_ident(&buffer, oid, OBJ_TAG, options); |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 977 | |
| 978 | done: |
| 979 | strbuf_release(&sb); |
Johannes Schindelin | cec097b | 2014-09-11 16:26:38 +0200 | [diff] [blame] | 980 | return ret; |
| 981 | } |
| 982 | |
Jonathan Nieder | a2b26ff | 2020-04-18 20:52:34 -0700 | [diff] [blame] | 983 | /* |
| 984 | * Like builtin/submodule--helper.c's starts_with_dot_slash, but without |
| 985 | * relying on the platform-dependent is_dir_sep helper. |
| 986 | * |
| 987 | * This is for use in checking whether a submodule URL is interpreted as |
| 988 | * relative to the current directory on any platform, since \ is a |
| 989 | * directory separator on Windows but not on other platforms. |
| 990 | */ |
| 991 | static int starts_with_dot_slash(const char *str) |
| 992 | { |
| 993 | return str[0] == '.' && (str[1] == '/' || str[1] == '\\'); |
| 994 | } |
| 995 | |
| 996 | /* |
| 997 | * Like starts_with_dot_slash, this is a variant of submodule--helper's |
| 998 | * helper of the same name with the twist that it accepts backslash as a |
| 999 | * directory separator even on non-Windows platforms. |
| 1000 | */ |
| 1001 | static int starts_with_dot_dot_slash(const char *str) |
| 1002 | { |
| 1003 | return str[0] == '.' && starts_with_dot_slash(str + 1); |
| 1004 | } |
| 1005 | |
| 1006 | static int submodule_url_is_relative(const char *url) |
| 1007 | { |
| 1008 | return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url); |
| 1009 | } |
| 1010 | |
| 1011 | /* |
Jonathan Nieder | c44088e | 2020-04-18 20:54:13 -0700 | [diff] [blame] | 1012 | * Count directory components that a relative submodule URL should chop |
| 1013 | * from the remote_url it is to be resolved against. |
| 1014 | * |
| 1015 | * In other words, this counts "../" components at the start of a |
| 1016 | * submodule URL. |
| 1017 | * |
| 1018 | * Returns the number of directory components to chop and writes a |
| 1019 | * pointer to the next character of url after all leading "./" and |
| 1020 | * "../" components to out. |
| 1021 | */ |
| 1022 | static int count_leading_dotdots(const char *url, const char **out) |
| 1023 | { |
| 1024 | int result = 0; |
| 1025 | while (1) { |
| 1026 | if (starts_with_dot_dot_slash(url)) { |
| 1027 | result++; |
| 1028 | url += strlen("../"); |
| 1029 | continue; |
| 1030 | } |
| 1031 | if (starts_with_dot_slash(url)) { |
| 1032 | url += strlen("./"); |
| 1033 | continue; |
| 1034 | } |
| 1035 | *out = url; |
| 1036 | return result; |
| 1037 | } |
| 1038 | } |
| 1039 | /* |
Jonathan Nieder | a2b26ff | 2020-04-18 20:52:34 -0700 | [diff] [blame] | 1040 | * Check whether a transport is implemented by git-remote-curl. |
| 1041 | * |
| 1042 | * If it is, returns 1 and writes the URL that would be passed to |
| 1043 | * git-remote-curl to the "out" parameter. |
| 1044 | * |
| 1045 | * Otherwise, returns 0 and leaves "out" untouched. |
| 1046 | * |
| 1047 | * Examples: |
| 1048 | * http::https://example.com/repo.git -> 1, https://example.com/repo.git |
| 1049 | * https://example.com/repo.git -> 1, https://example.com/repo.git |
| 1050 | * git://example.com/repo.git -> 0 |
| 1051 | * |
| 1052 | * This is for use in checking for previously exploitable bugs that |
| 1053 | * required a submodule URL to be passed to git-remote-curl. |
| 1054 | */ |
| 1055 | static int url_to_curl_url(const char *url, const char **out) |
| 1056 | { |
| 1057 | /* |
| 1058 | * We don't need to check for case-aliases, "http.exe", and so |
| 1059 | * on because in the default configuration, is_transport_allowed |
| 1060 | * prevents URLs with those schemes from being cloned |
| 1061 | * automatically. |
| 1062 | */ |
| 1063 | if (skip_prefix(url, "http::", out) || |
| 1064 | skip_prefix(url, "https::", out) || |
| 1065 | skip_prefix(url, "ftp::", out) || |
| 1066 | skip_prefix(url, "ftps::", out)) |
| 1067 | return 1; |
| 1068 | if (starts_with(url, "http://") || |
| 1069 | starts_with(url, "https://") || |
| 1070 | starts_with(url, "ftp://") || |
| 1071 | starts_with(url, "ftps://")) { |
| 1072 | *out = url; |
| 1073 | return 1; |
| 1074 | } |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
Jeff King | 07259e7 | 2020-03-11 18:48:24 -0400 | [diff] [blame] | 1078 | static int check_submodule_url(const char *url) |
| 1079 | { |
Jonathan Nieder | a2b26ff | 2020-04-18 20:52:34 -0700 | [diff] [blame] | 1080 | const char *curl_url; |
Jeff King | 07259e7 | 2020-03-11 18:48:24 -0400 | [diff] [blame] | 1081 | |
| 1082 | if (looks_like_command_line_option(url)) |
| 1083 | return -1; |
| 1084 | |
Jonathan Nieder | a2b26ff | 2020-04-18 20:52:34 -0700 | [diff] [blame] | 1085 | if (submodule_url_is_relative(url)) { |
Jonathan Nieder | c44088e | 2020-04-18 20:54:13 -0700 | [diff] [blame] | 1086 | char *decoded; |
| 1087 | const char *next; |
| 1088 | int has_nl; |
| 1089 | |
Jonathan Nieder | a2b26ff | 2020-04-18 20:52:34 -0700 | [diff] [blame] | 1090 | /* |
| 1091 | * This could be appended to an http URL and url-decoded; |
| 1092 | * check for malicious characters. |
| 1093 | */ |
Jonathan Nieder | c44088e | 2020-04-18 20:54:13 -0700 | [diff] [blame] | 1094 | decoded = url_decode(url); |
| 1095 | has_nl = !!strchr(decoded, '\n'); |
| 1096 | |
Jonathan Nieder | a2b26ff | 2020-04-18 20:52:34 -0700 | [diff] [blame] | 1097 | free(decoded); |
| 1098 | if (has_nl) |
| 1099 | return -1; |
Jonathan Nieder | c44088e | 2020-04-18 20:54:13 -0700 | [diff] [blame] | 1100 | |
| 1101 | /* |
| 1102 | * URLs which escape their root via "../" can overwrite |
| 1103 | * the host field and previous components, resolving to |
Jonathan Nieder | 1a3609e | 2020-04-18 20:57:22 -0700 | [diff] [blame] | 1104 | * URLs like https::example.com/submodule.git and |
| 1105 | * https:///example.com/submodule.git that were |
Jonathan Nieder | c44088e | 2020-04-18 20:54:13 -0700 | [diff] [blame] | 1106 | * susceptible to CVE-2020-11008. |
| 1107 | */ |
| 1108 | if (count_leading_dotdots(url, &next) > 0 && |
Jonathan Nieder | 1a3609e | 2020-04-18 20:57:22 -0700 | [diff] [blame] | 1109 | (*next == ':' || *next == '/')) |
Jonathan Nieder | c44088e | 2020-04-18 20:54:13 -0700 | [diff] [blame] | 1110 | return -1; |
Jonathan Nieder | a2b26ff | 2020-04-18 20:52:34 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | else if (url_to_curl_url(url, &curl_url)) { |
| 1114 | struct credential c = CREDENTIAL_INIT; |
Jonathan Nieder | 1a3609e | 2020-04-18 20:57:22 -0700 | [diff] [blame] | 1115 | int ret = 0; |
| 1116 | if (credential_from_url_gently(&c, curl_url, 1) || |
| 1117 | !*c.host) |
| 1118 | ret = -1; |
Jonathan Nieder | a2b26ff | 2020-04-18 20:52:34 -0700 | [diff] [blame] | 1119 | credential_clear(&c); |
| 1120 | return ret; |
| 1121 | } |
| 1122 | |
| 1123 | return 0; |
Jeff King | 07259e7 | 2020-03-11 18:48:24 -0400 | [diff] [blame] | 1124 | } |
| 1125 | |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 1126 | struct fsck_gitmodules_data { |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 1127 | const struct object_id *oid; |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 1128 | struct fsck_options *options; |
| 1129 | int ret; |
| 1130 | }; |
| 1131 | |
| 1132 | static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata) |
| 1133 | { |
| 1134 | struct fsck_gitmodules_data *data = vdata; |
| 1135 | const char *subsection, *key; |
Jeff King | f5914f4 | 2020-04-10 15:44:28 -0400 | [diff] [blame] | 1136 | size_t subsection_len; |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 1137 | char *name; |
| 1138 | |
| 1139 | if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 || |
| 1140 | !subsection) |
| 1141 | return 0; |
| 1142 | |
| 1143 | name = xmemdupz(subsection, subsection_len); |
| 1144 | if (check_submodule_name(name) < 0) |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 1145 | data->ret |= report(data->options, |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 1146 | data->oid, OBJ_BLOB, |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 1147 | FSCK_MSG_GITMODULES_NAME, |
| 1148 | "disallowed submodule name: %s", |
| 1149 | name); |
Jeff King | a124133 | 2018-09-24 04:37:17 -0400 | [diff] [blame] | 1150 | if (!strcmp(key, "url") && value && |
Jeff King | 07259e7 | 2020-03-11 18:48:24 -0400 | [diff] [blame] | 1151 | check_submodule_url(value) < 0) |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 1152 | data->ret |= report(data->options, |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 1153 | data->oid, OBJ_BLOB, |
Jeff King | a124133 | 2018-09-24 04:37:17 -0400 | [diff] [blame] | 1154 | FSCK_MSG_GITMODULES_URL, |
| 1155 | "disallowed submodule url: %s", |
| 1156 | value); |
Jeff King | 1a7fd1f | 2018-09-24 04:42:19 -0400 | [diff] [blame] | 1157 | if (!strcmp(key, "path") && value && |
| 1158 | looks_like_command_line_option(value)) |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 1159 | data->ret |= report(data->options, |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 1160 | data->oid, OBJ_BLOB, |
Jeff King | 1a7fd1f | 2018-09-24 04:42:19 -0400 | [diff] [blame] | 1161 | FSCK_MSG_GITMODULES_PATH, |
| 1162 | "disallowed submodule path: %s", |
| 1163 | value); |
Jonathan Nieder | bb92255 | 2019-12-05 01:30:43 -0800 | [diff] [blame] | 1164 | if (!strcmp(key, "update") && value && |
| 1165 | parse_submodule_update_type(value) == SM_UPDATE_COMMAND) |
Junio C Hamano | 7034cd0 | 2019-12-09 22:17:55 -0800 | [diff] [blame] | 1166 | data->ret |= report(data->options, data->oid, OBJ_BLOB, |
Jonathan Nieder | bb92255 | 2019-12-05 01:30:43 -0800 | [diff] [blame] | 1167 | FSCK_MSG_GITMODULES_UPDATE, |
| 1168 | "disallowed submodule update setting: %s", |
| 1169 | value); |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 1170 | free(name); |
| 1171 | |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 1175 | static int fsck_blob(const struct object_id *oid, const char *buf, |
Jeff King | 7ac4f3a | 2018-05-02 15:44:51 -0400 | [diff] [blame] | 1176 | unsigned long size, struct fsck_options *options) |
| 1177 | { |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 1178 | struct fsck_gitmodules_data data; |
Jeff King | de6bd9e | 2018-06-28 18:06:04 -0400 | [diff] [blame] | 1179 | struct config_options config_opts = { 0 }; |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 1180 | |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 1181 | if (!oidset_contains(&gitmodules_found, oid)) |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 1182 | return 0; |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 1183 | oidset_insert(&gitmodules_done, oid); |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 1184 | |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 1185 | if (object_on_skiplist(options, oid)) |
Ramsay Jones | fb16287 | 2018-06-27 19:39:53 +0100 | [diff] [blame] | 1186 | return 0; |
| 1187 | |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 1188 | if (!buf) { |
| 1189 | /* |
| 1190 | * A missing buffer here is a sign that the caller found the |
| 1191 | * blob too gigantic to load into memory. Let's just consider |
| 1192 | * that an error. |
| 1193 | */ |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 1194 | return report(options, oid, OBJ_BLOB, |
Jeff King | 0d68764 | 2018-07-13 15:39:53 -0400 | [diff] [blame] | 1195 | FSCK_MSG_GITMODULES_LARGE, |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 1196 | ".gitmodules too large to parse"); |
| 1197 | } |
| 1198 | |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 1199 | data.oid = oid; |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 1200 | data.options = options; |
| 1201 | data.ret = 0; |
Jeff King | de6bd9e | 2018-06-28 18:06:04 -0400 | [diff] [blame] | 1202 | config_opts.error_action = CONFIG_ERROR_SILENT; |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 1203 | if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB, |
Jeff King | de6bd9e | 2018-06-28 18:06:04 -0400 | [diff] [blame] | 1204 | ".gitmodules", buf, size, &data, &config_opts)) |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 1205 | data.ret |= report(options, oid, OBJ_BLOB, |
Jeff King | ed8b10f | 2018-05-02 17:25:27 -0400 | [diff] [blame] | 1206 | FSCK_MSG_GITMODULES_PARSE, |
| 1207 | "could not parse gitmodules blob"); |
| 1208 | |
| 1209 | return data.ret; |
Jeff King | 7ac4f3a | 2018-05-02 15:44:51 -0400 | [diff] [blame] | 1210 | } |
| 1211 | |
Johannes Schindelin | 90a398b | 2014-09-10 15:52:51 +0200 | [diff] [blame] | 1212 | int fsck_object(struct object *obj, void *data, unsigned long size, |
Johannes Schindelin | 2241054 | 2015-06-22 17:25:00 +0200 | [diff] [blame] | 1213 | struct fsck_options *options) |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 1214 | { |
| 1215 | if (!obj) |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 1216 | 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] | 1217 | |
| 1218 | if (obj->type == OBJ_BLOB) |
Jeff King | 6da40b2 | 2019-10-18 00:59:29 -0400 | [diff] [blame] | 1219 | return fsck_blob(&obj->oid, data, size, options); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 1220 | if (obj->type == OBJ_TREE) |
Jeff King | b2f2039 | 2019-10-18 01:02:08 -0400 | [diff] [blame] | 1221 | return fsck_tree(&obj->oid, data, size, options); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 1222 | if (obj->type == OBJ_COMMIT) |
Jeff King | c5b4269 | 2019-10-18 01:01:48 -0400 | [diff] [blame] | 1223 | return fsck_commit(&obj->oid, data, size, options); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 1224 | if (obj->type == OBJ_TAG) |
Jeff King | 103fb6d | 2019-10-18 01:01:26 -0400 | [diff] [blame] | 1225 | return fsck_tag(&obj->oid, data, size, options); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 1226 | |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 1227 | return report(options, &obj->oid, obj->type, |
| 1228 | FSCK_MSG_UNKNOWN_TYPE, |
| 1229 | "unknown type '%d' (internal fsck error)", |
| 1230 | obj->type); |
Martin Koegler | ba002f3 | 2008-02-25 22:46:08 +0100 | [diff] [blame] | 1231 | } |
Martin Koegler | d6ffc8d | 2008-02-25 22:46:09 +0100 | [diff] [blame] | 1232 | |
Johannes Schindelin | 1cd772c | 2016-07-17 12:59:57 +0200 | [diff] [blame] | 1233 | int fsck_error_function(struct fsck_options *o, |
Jeff King | 5afc4b1 | 2019-10-18 00:58:40 -0400 | [diff] [blame] | 1234 | const struct object_id *oid, |
| 1235 | enum object_type object_type, |
| 1236 | int msg_type, const char *message) |
Martin Koegler | d6ffc8d | 2008-02-25 22:46:09 +0100 | [diff] [blame] | 1237 | { |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 1238 | if (msg_type == FSCK_WARN) { |
Jeff King | 5afc4b1 | 2019-10-18 00:58:40 -0400 | [diff] [blame] | 1239 | warning("object %s: %s", fsck_describe_object(o, oid), message); |
Johannes Schindelin | 0282f4d | 2015-06-22 17:25:25 +0200 | [diff] [blame] | 1240 | return 0; |
| 1241 | } |
Jeff King | 5afc4b1 | 2019-10-18 00:58:40 -0400 | [diff] [blame] | 1242 | error("object %s: %s", fsck_describe_object(o, oid), message); |
Martin Koegler | d6ffc8d | 2008-02-25 22:46:09 +0100 | [diff] [blame] | 1243 | return 1; |
| 1244 | } |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 1245 | |
| 1246 | int fsck_finish(struct fsck_options *options) |
| 1247 | { |
| 1248 | int ret = 0; |
| 1249 | struct oidset_iter iter; |
| 1250 | const struct object_id *oid; |
| 1251 | |
| 1252 | oidset_iter_init(&gitmodules_found, &iter); |
| 1253 | while ((oid = oidset_iter_next(&iter))) { |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 1254 | enum object_type type; |
| 1255 | unsigned long size; |
| 1256 | char *buf; |
| 1257 | |
| 1258 | if (oidset_contains(&gitmodules_done, oid)) |
| 1259 | continue; |
| 1260 | |
Junio C Hamano | 7913f53 | 2018-05-29 17:09:58 +0900 | [diff] [blame] | 1261 | buf = read_object_file(oid, &type, &size); |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 1262 | if (!buf) { |
Jeff King | b8b00f1 | 2019-10-18 00:59:54 -0400 | [diff] [blame] | 1263 | if (is_promisor_object(oid)) |
Jeff King | 2738744 | 2018-05-14 12:22:48 -0400 | [diff] [blame] | 1264 | continue; |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 1265 | ret |= report(options, |
Jeff King | b8b00f1 | 2019-10-18 00:59:54 -0400 | [diff] [blame] | 1266 | oid, OBJ_BLOB, |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 1267 | FSCK_MSG_GITMODULES_MISSING, |
| 1268 | "unable to read .gitmodules blob"); |
| 1269 | continue; |
| 1270 | } |
| 1271 | |
| 1272 | if (type == OBJ_BLOB) |
Jeff King | b8b00f1 | 2019-10-18 00:59:54 -0400 | [diff] [blame] | 1273 | ret |= fsck_blob(oid, buf, size, options); |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 1274 | else |
Jeff King | 3837025 | 2019-10-18 00:59:15 -0400 | [diff] [blame] | 1275 | ret |= report(options, |
Jeff King | b8b00f1 | 2019-10-18 00:59:54 -0400 | [diff] [blame] | 1276 | oid, type, |
Jeff King | 159e7b0 | 2018-05-02 17:20:08 -0400 | [diff] [blame] | 1277 | FSCK_MSG_GITMODULES_BLOB, |
| 1278 | "non-blob found at .gitmodules"); |
| 1279 | free(buf); |
| 1280 | } |
| 1281 | |
| 1282 | |
| 1283 | oidset_clear(&gitmodules_found); |
| 1284 | oidset_clear(&gitmodules_done); |
| 1285 | return ret; |
| 1286 | } |