blob: 640d813d8430c6835ab1068c0c4512560e91c2b8 [file] [log] [blame]
Martin Koegler355885d2008-02-25 22:46:04 +01001#include "cache.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07002#include "object-store.h"
Stefan Beller109cd762018-06-28 18:21:51 -07003#include "repository.h"
Martin Koegler355885d2008-02-25 22:46:04 +01004#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 Schindelincec097b2014-09-11 16:26:38 +020011#include "refs.h"
Jeff Kinga18fcc92014-12-15 18:21:57 -050012#include "utf8.h"
Johannes Schindelin7b35efd2016-07-17 12:59:49 +020013#include "decorate.h"
Jeff King159e7b02018-05-02 17:20:08 -040014#include "oidset.h"
Jeff King27387442018-05-14 12:22:48 -040015#include "packfile.h"
Jeff Kinged8b10f2018-05-02 17:25:27 -040016#include "submodule-config.h"
17#include "config.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020018#include "help.h"
Jeff King159e7b02018-05-02 17:20:08 -040019
20static struct oidset gitmodules_found = OIDSET_INIT;
21static struct oidset gitmodules_done = OIDSET_INIT;
Martin Koegler355885d2008-02-25 22:46:04 +010022
Johannes Schindelinf50c4402015-06-22 17:26:42 +020023#define FSCK_FATAL -1
Johannes Schindelinf27d05b2015-06-22 17:26:54 +020024#define FSCK_INFO -2
Johannes Schindelinf50c4402015-06-22 17:26:42 +020025
Johannes Schindelinc99ba492015-06-22 17:25:09 +020026#define FOREACH_MSG_ID(FUNC) \
Johannes Schindelinf50c4402015-06-22 17:26:42 +020027 /* fatal errors */ \
28 FUNC(NUL_IN_HEADER, FATAL) \
29 FUNC(UNTERMINATED_HEADER, FATAL) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020030 /* 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 Schindelinc99ba492015-06-22 17:25:09 +020046 FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
47 FUNC(MISSING_OBJECT, ERROR) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020048 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 Schindelinc99ba492015-06-22 17:25:09 +020052 FUNC(MISSING_TREE, ERROR) \
Jeff King159e7b02018-05-02 17:20:08 -040053 FUNC(MISSING_TREE_OBJECT, ERROR) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020054 FUNC(MISSING_TYPE, ERROR) \
55 FUNC(MISSING_TYPE_ENTRY, ERROR) \
Johannes Schindelinc9ad1472015-06-22 17:26:23 +020056 FUNC(MULTIPLE_AUTHORS, ERROR) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020057 FUNC(TREE_NOT_SORTED, ERROR) \
58 FUNC(UNKNOWN_TYPE, ERROR) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020059 FUNC(ZERO_PADDED_DATE, ERROR) \
Jeff King159e7b02018-05-02 17:20:08 -040060 FUNC(GITMODULES_MISSING, ERROR) \
61 FUNC(GITMODULES_BLOB, ERROR) \
Jeff King0d687642018-07-13 15:39:53 -040062 FUNC(GITMODULES_LARGE, ERROR) \
Jeff Kinged8b10f2018-05-02 17:25:27 -040063 FUNC(GITMODULES_NAME, ERROR) \
Jeff Kingb7b1fca2018-05-04 20:03:35 -040064 FUNC(GITMODULES_SYMLINK, ERROR) \
Jeff Kinga1241332018-09-24 04:37:17 -040065 FUNC(GITMODULES_URL, ERROR) \
Jeff King1a7fd1f2018-09-24 04:42:19 -040066 FUNC(GITMODULES_PATH, ERROR) \
Jonathan Niederbb922552019-12-05 01:30:43 -080067 FUNC(GITMODULES_UPDATE, ERROR) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020068 /* warnings */ \
69 FUNC(BAD_FILEMODE, WARN) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020070 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 Schindelinc99ba492015-06-22 17:25:09 +020075 FUNC(NULL_SHA1, WARN) \
Johannes Schindelinf27d05b2015-06-22 17:26:54 +020076 FUNC(ZERO_PADDED_FILEMODE, WARN) \
Junio C Hamano6d2d7802016-04-14 10:58:22 -070077 FUNC(NUL_IN_COMMIT, WARN) \
Johannes Schindelinf27d05b2015-06-22 17:26:54 +020078 /* infos (reported as warnings, but ignored by default) */ \
Jeff King64eb14d2018-07-13 15:39:58 -040079 FUNC(GITMODULES_PARSE, INFO) \
Johannes Schindelinf27d05b2015-06-22 17:26:54 +020080 FUNC(BAD_TAG_NAME, INFO) \
81 FUNC(MISSING_TAGGER_ENTRY, INFO)
Johannes Schindelinc99ba492015-06-22 17:25:09 +020082
83#define MSG_ID(id, msg_type) FSCK_MSG_##id,
84enum fsck_msg_id {
85 FOREACH_MSG_ID(MSG_ID)
86 FSCK_MSG_MAX
87};
88#undef MSG_ID
89
Johannes Schindelinf417eed2015-06-22 17:25:14 +020090#define STR(x) #x
Nguyễn Thái Ngọc Duya4a9cc12018-05-26 15:55:25 +020091#define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
Johannes Schindelinc99ba492015-06-22 17:25:09 +020092static struct {
Johannes Schindelinf417eed2015-06-22 17:25:14 +020093 const char *id_string;
94 const char *downcased;
Nguyễn Thái Ngọc Duya4a9cc12018-05-26 15:55:25 +020095 const char *camelcased;
Johannes Schindelinc99ba492015-06-22 17:25:09 +020096 int msg_type;
97} msg_id_info[FSCK_MSG_MAX + 1] = {
98 FOREACH_MSG_ID(MSG_ID)
Nguyễn Thái Ngọc Duya4a9cc12018-05-26 15:55:25 +020099 { NULL, NULL, NULL, -1 }
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200100};
101#undef MSG_ID
102
Nguyễn Thái Ngọc Duya46baac2018-05-26 15:55:23 +0200103static 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 Duya4a9cc12018-05-26 15:55:25 +0200123
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 Duya46baac2018-05-26 15:55:23 +0200137 }
138}
139
Johannes Schindelinf417eed2015-06-22 17:25:14 +0200140static int parse_msg_id(const char *text)
141{
142 int i;
143
Nguyễn Thái Ngọc Duya46baac2018-05-26 15:55:23 +0200144 prepare_msg_ids();
Johannes Schindelinf417eed2015-06-22 17:25:14 +0200145
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 Duy3ac68a92018-05-26 15:55:24 +0200153void 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 Duy3ac68a92018-05-26 15:55:24 +0200159 for (i = 0; i < FSCK_MSG_MAX; i++)
Nguyễn Thái Ngọc Duya4a9cc12018-05-26 15:55:25 +0200160 list_config_item(list, prefix, msg_id_info[i].camelcased);
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +0200161}
162
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200163static int fsck_msg_type(enum fsck_msg_id msg_id,
164 struct fsck_options *options)
165{
166 int msg_type;
167
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200168 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 Schindelinc99ba492015-06-22 17:25:09 +0200177
178 return msg_type;
179}
180
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200181static 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 Schindelinefaba7c2015-06-22 17:26:48 +0200187 else if (!strcmp(str, "ignore"))
188 return FSCK_IGNORE;
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200189 else
190 die("Unknown fsck message type: '%s'", str);
191}
192
Johannes Schindelin5d477a32015-06-22 17:25:31 +0200193int 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 Schindelin0282f4d2015-06-22 17:25:25 +0200201void 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 Schindelinf50c4402015-06-22 17:26:42 +0200210 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 Schindelin0282f4d2015-06-22 17:25:25 +0200213 if (!options->msg_type) {
214 int i;
Jeff Kingb32fa952016-02-22 17:44:25 -0500215 int *msg_type;
216 ALLOC_ARRAY(msg_type, FSCK_MSG_MAX);
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200217 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
225void 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 Schindelincd94c6f2015-06-22 17:27:18 +0200246 if (!strcmp(buf, "skiplist")) {
247 if (equal == len)
248 die("skiplist requires a path");
Barret Rhoden24eb33e2019-05-15 17:44:56 -0400249 oidset_parse_file(&options->skiplist, buf + equal + 1);
Johannes Schindelincd94c6f2015-06-22 17:27:18 +0200250 buf += len + 1;
251 continue;
252 }
253
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200254 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 Schindelin71ab8fa2015-06-22 17:25:52 +0200263static 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 Kingf5979372019-10-18 00:58:51 -0400281static int object_on_skiplist(struct fsck_options *opts,
282 const struct object_id *oid)
Ramsay Jonesfb162872018-06-27 19:39:53 +0100283{
Jeff Kingf5979372019-10-18 00:58:51 -0400284 return opts && oid && oidset_contains(&opts->skiplist, oid);
Ramsay Jonesfb162872018-06-27 19:39:53 +0100285}
286
Jeff King38370252019-10-18 00:59:15 -0400287__attribute__((format (printf, 5, 6)))
288static 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 Schindelinc99ba492015-06-22 17:25:09 +0200291{
292 va_list ap;
293 struct strbuf sb = STRBUF_INIT;
294 int msg_type = fsck_msg_type(id, options), result;
295
Johannes Schindelinefaba7c2015-06-22 17:26:48 +0200296 if (msg_type == FSCK_IGNORE)
297 return 0;
298
Jeff King38370252019-10-18 00:59:15 -0400299 if (object_on_skiplist(options, oid))
Johannes Schindelincd94c6f2015-06-22 17:27:18 +0200300 return 0;
301
Johannes Schindelinf50c4402015-06-22 17:26:42 +0200302 if (msg_type == FSCK_FATAL)
303 msg_type = FSCK_ERROR;
Johannes Schindelinf27d05b2015-06-22 17:26:54 +0200304 else if (msg_type == FSCK_INFO)
305 msg_type = FSCK_WARN;
Johannes Schindelinf50c4402015-06-22 17:26:42 +0200306
Johannes Schindelin71ab8fa2015-06-22 17:25:52 +0200307 append_msg_id(&sb, msg_id_info[id].id_string);
308
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200309 va_start(ap, fmt);
310 strbuf_vaddf(&sb, fmt, ap);
Jeff King38370252019-10-18 00:59:15 -0400311 result = options->error_func(options, oid, object_type,
Jeff King5afc4b12019-10-18 00:58:40 -0400312 msg_type, sb.buf);
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200313 strbuf_release(&sb);
314 va_end(ap);
315
316 return result;
317}
318
Jeff Kinga59cfb32019-10-18 00:56:13 -0400319void fsck_enable_object_names(struct fsck_options *options)
320{
321 if (!options->object_names)
Jeff King73390292019-10-18 00:57:37 -0400322 options->object_names = kh_init_oid_map();
Jeff Kinga59cfb32019-10-18 00:56:13 -0400323}
324
Jeff King73390292019-10-18 00:57:37 -0400325const char *fsck_get_object_name(struct fsck_options *options,
326 const struct object_id *oid)
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200327{
Jeff King73390292019-10-18 00:57:37 -0400328 khiter_t pos;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200329 if (!options->object_names)
330 return NULL;
Jeff King73390292019-10-18 00:57:37 -0400331 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 Schindelin7b35efd2016-07-17 12:59:49 +0200335}
336
Jeff King73390292019-10-18 00:57:37 -0400337void fsck_put_object_name(struct fsck_options *options,
338 const struct object_id *oid,
Jeff Kinga59cfb32019-10-18 00:56:13 -0400339 const char *fmt, ...)
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200340{
341 va_list ap;
342 struct strbuf buf = STRBUF_INIT;
Jeff King73390292019-10-18 00:57:37 -0400343 khiter_t pos;
344 int hashret;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200345
346 if (!options->object_names)
347 return;
Jeff King73390292019-10-18 00:57:37 -0400348
349 pos = kh_put_oid_map(options->object_names, *oid, &hashret);
350 if (!hashret)
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200351 return;
352 va_start(ap, fmt);
353 strbuf_vaddf(&buf, fmt, ap);
Jeff King73390292019-10-18 00:57:37 -0400354 kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200355 va_end(ap);
356}
357
Jeff Kinga59cfb32019-10-18 00:56:13 -0400358const char *fsck_describe_object(struct fsck_options *options,
Jeff King73390292019-10-18 00:57:37 -0400359 const struct object_id *oid)
Johannes Schindelin90cf5902016-07-17 13:00:02 +0200360{
Jeff Kinga59cfb32019-10-18 00:56:13 -0400361 static struct strbuf bufs[] = {
362 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
363 };
364 static int b = 0;
365 struct strbuf *buf;
Jeff King73390292019-10-18 00:57:37 -0400366 const char *name = fsck_get_object_name(options, oid);
Johannes Schindelin90cf5902016-07-17 13:00:02 +0200367
Jeff Kinga59cfb32019-10-18 00:56:13 -0400368 buf = bufs + b;
369 b = (b + 1) % ARRAY_SIZE(bufs);
370 strbuf_reset(buf);
Jeff King73390292019-10-18 00:57:37 -0400371 strbuf_addstr(buf, oid_to_hex(oid));
Jeff Kinga59cfb32019-10-18 00:56:13 -0400372 if (name)
373 strbuf_addf(buf, " (%s)", name);
374
375 return buf->buf;
Johannes Schindelin90cf5902016-07-17 13:00:02 +0200376}
377
Johannes Schindelin22410542015-06-22 17:25:00 +0200378static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
Martin Koegler355885d2008-02-25 22:46:04 +0100379{
380 struct tree_desc desc;
381 struct name_entry entry;
382 int res = 0;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200383 const char *name;
Martin Koegler355885d2008-02-25 22:46:04 +0100384
385 if (parse_tree(tree))
386 return -1;
387
Jeff King73390292019-10-18 00:57:37 -0400388 name = fsck_get_object_name(options, &tree->object.oid);
David Turner8354fa32016-09-27 16:59:51 -0400389 if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
390 return -1;
391 while (tree_entry_gently(&desc, &entry)) {
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200392 struct object *obj;
Martin Koegler355885d2008-02-25 22:46:04 +0100393 int result;
394
395 if (S_ISGITLINK(entry.mode))
396 continue;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200397
398 if (S_ISDIR(entry.mode)) {
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000399 obj = (struct object *)lookup_tree(the_repository, &entry.oid);
René Scharfe2720f6d2017-10-05 21:41:26 +0200400 if (name && obj)
Jeff King73390292019-10-18 00:57:37 -0400401 fsck_put_object_name(options, &entry.oid, "%s%s/",
Jeff Kinga59cfb32019-10-18 00:56:13 -0400402 name, entry.path);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200403 result = options->walk(obj, OBJ_TREE, data, options);
404 }
405 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000406 obj = (struct object *)lookup_blob(the_repository, &entry.oid);
René Scharfe2720f6d2017-10-05 21:41:26 +0200407 if (name && obj)
Jeff King73390292019-10-18 00:57:37 -0400408 fsck_put_object_name(options, &entry.oid, "%s%s",
Jeff Kinga59cfb32019-10-18 00:56:13 -0400409 name, entry.path);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200410 result = options->walk(obj, OBJ_BLOB, data, options);
411 }
Martin Koegler355885d2008-02-25 22:46:04 +0100412 else {
Pete Wyckoff82247e92012-04-29 20:28:45 -0400413 result = error("in tree %s: entry %s has bad mode %.6o",
Jeff King73390292019-10-18 00:57:37 -0400414 fsck_describe_object(options, &tree->object.oid),
Jeff Kinga59cfb32019-10-18 00:56:13 -0400415 entry.path, entry.mode);
Martin Koegler355885d2008-02-25 22:46:04 +0100416 }
417 if (result < 0)
418 return result;
419 if (!res)
420 res = result;
421 }
422 return res;
423}
424
Johannes Schindelin22410542015-06-22 17:25:00 +0200425static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
Martin Koegler355885d2008-02-25 22:46:04 +0100426{
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200427 int counter = 0, generation = 0, name_prefix_len = 0;
Martin Koegler355885d2008-02-25 22:46:04 +0100428 struct commit_list *parents;
429 int res;
430 int result;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200431 const char *name;
Martin Koegler355885d2008-02-25 22:46:04 +0100432
433 if (parse_commit(commit))
434 return -1;
435
Jeff King73390292019-10-18 00:57:37 -0400436 name = fsck_get_object_name(options, &commit->object.oid);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200437 if (name)
Jeff King73390292019-10-18 00:57:37 -0400438 fsck_put_object_name(options, get_commit_tree_oid(commit),
Jeff Kinga59cfb32019-10-18 00:56:13 -0400439 "%s:", name);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200440
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000441 result = options->walk((struct object *)get_commit_tree(commit),
442 OBJ_TREE, data, options);
Martin Koegler355885d2008-02-25 22:46:04 +0100443 if (result < 0)
444 return result;
445 res = result;
446
447 parents = commit->parents;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200448 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 Koegler355885d2008-02-25 22:46:04 +0100465 while (parents) {
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200466 if (name) {
Jeff King73390292019-10-18 00:57:37 -0400467 struct object_id *oid = &parents->item->object.oid;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200468
Junio C Hamanob84c78382018-10-24 10:25:12 +0900469 if (counter++)
Jeff King73390292019-10-18 00:57:37 -0400470 fsck_put_object_name(options, oid, "%s^%d",
Jeff Kinga59cfb32019-10-18 00:56:13 -0400471 name, counter);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200472 else if (generation > 0)
Jeff King73390292019-10-18 00:57:37 -0400473 fsck_put_object_name(options, oid, "%.*s~%d",
Jeff Kinga59cfb32019-10-18 00:56:13 -0400474 name_prefix_len, name,
475 generation + 1);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200476 else
Jeff King73390292019-10-18 00:57:37 -0400477 fsck_put_object_name(options, oid, "%s^", name);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200478 }
Johannes Schindelin22410542015-06-22 17:25:00 +0200479 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
Martin Koegler355885d2008-02-25 22:46:04 +0100480 if (result < 0)
481 return result;
482 if (!res)
483 res = result;
484 parents = parents->next;
485 }
486 return res;
487}
488
Johannes Schindelin22410542015-06-22 17:25:00 +0200489static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
Martin Koegler355885d2008-02-25 22:46:04 +0100490{
Jeff King73390292019-10-18 00:57:37 -0400491 const char *name = fsck_get_object_name(options, &tag->object.oid);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200492
Martin Koegler355885d2008-02-25 22:46:04 +0100493 if (parse_tag(tag))
494 return -1;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200495 if (name)
Jeff King73390292019-10-18 00:57:37 -0400496 fsck_put_object_name(options, &tag->tagged->oid, "%s", name);
Johannes Schindelin22410542015-06-22 17:25:00 +0200497 return options->walk(tag->tagged, OBJ_ANY, data, options);
Martin Koegler355885d2008-02-25 22:46:04 +0100498}
499
Johannes Schindelin22410542015-06-22 17:25:00 +0200500int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
Martin Koegler355885d2008-02-25 22:46:04 +0100501{
502 if (!obj)
503 return -1;
Jeff Kinga2b22852017-01-25 23:12:07 -0500504
505 if (obj->type == OBJ_NONE)
Stefan Beller109cd762018-06-28 18:21:51 -0700506 parse_object(the_repository, &obj->oid);
Jeff Kinga2b22852017-01-25 23:12:07 -0500507
Martin Koegler355885d2008-02-25 22:46:04 +0100508 switch (obj->type) {
509 case OBJ_BLOB:
510 return 0;
511 case OBJ_TREE:
Johannes Schindelin22410542015-06-22 17:25:00 +0200512 return fsck_walk_tree((struct tree *)obj, data, options);
Martin Koegler355885d2008-02-25 22:46:04 +0100513 case OBJ_COMMIT:
Johannes Schindelin22410542015-06-22 17:25:00 +0200514 return fsck_walk_commit((struct commit *)obj, data, options);
Martin Koegler355885d2008-02-25 22:46:04 +0100515 case OBJ_TAG:
Johannes Schindelin22410542015-06-22 17:25:00 +0200516 return fsck_walk_tag((struct tag *)obj, data, options);
Martin Koegler355885d2008-02-25 22:46:04 +0100517 default:
Jeff Kinga59cfb32019-10-18 00:56:13 -0400518 error("Unknown object type for %s",
Jeff King73390292019-10-18 00:57:37 -0400519 fsck_describe_object(options, &obj->oid));
Martin Koegler355885d2008-02-25 22:46:04 +0100520 return -1;
521 }
522}
Martin Koeglerba002f32008-02-25 22:46:08 +0100523
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
535static 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 Kingb2f20392019-10-18 01:02:08 -0400570static int fsck_tree(const struct object_id *oid,
Jeff King23a173a2019-10-18 00:54:12 -0400571 const char *buffer, unsigned long size,
572 struct fsck_options *options)
Martin Koeglerba002f32008-02-25 22:46:08 +0100573{
David Turner8354fa32016-09-27 16:59:51 -0400574 int retval = 0;
Jeff Kingc479d142012-07-28 11:06:29 -0400575 int has_null_sha1 = 0;
Martin Koeglerba002f32008-02-25 22:46:08 +0100576 int has_full_path = 0;
577 int has_empty_name = 0;
Jeff King5d34a432012-11-27 21:27:37 -0500578 int has_dot = 0;
579 int has_dotdot = 0;
Jeff King5c17f512012-11-28 16:35:29 -0500580 int has_dotgit = 0;
Martin Koeglerba002f32008-02-25 22:46:08 +0100581 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 Koeglerba002f32008-02-25 22:46:08 +0100588
Jeff King23a173a2019-10-18 00:54:12 -0400589 if (init_tree_desc_gently(&desc, buffer, size)) {
Jeff Kingb2f20392019-10-18 01:02:08 -0400590 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
David Turner8354fa32016-09-27 16:59:51 -0400591 return retval;
592 }
Martin Koeglerba002f32008-02-25 22:46:08 +0100593
594 o_mode = 0;
595 o_name = NULL;
Martin Koeglerba002f32008-02-25 22:46:08 +0100596
597 while (desc.size) {
Elijah Newren5ec1e722019-04-05 08:00:12 -0700598 unsigned short mode;
Johannes Schindelin288a74b2019-09-23 08:58:11 +0200599 const char *name, *backslash;
brian m. carlsonce6663a2016-04-17 23:10:40 +0000600 const struct object_id *oid;
Martin Koeglerba002f32008-02-25 22:46:08 +0100601
brian m. carlsonce6663a2016-04-17 23:10:40 +0000602 oid = tree_entry_extract(&desc, &name, &mode);
Martin Koeglerba002f32008-02-25 22:46:08 +0100603
brian m. carlsonce6663a2016-04-17 23:10:40 +0000604 has_null_sha1 |= is_null_oid(oid);
Hiroyuki Sanoeffd12e2014-03-20 08:02:04 +0900605 has_full_path |= !!strchr(name, '/');
606 has_empty_name |= !*name;
607 has_dot |= !strcmp(name, ".");
608 has_dotdot |= !strcmp(name, "..");
Jeff Kinged9c3222018-05-13 12:35:37 -0400609 has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
Martin Koeglerba002f32008-02-25 22:46:08 +0100610 has_zero_pad |= *(char *)desc.buffer == '0';
Jeff King159e7b02018-05-02 17:20:08 -0400611
Jeff Kingb7b1fca2018-05-04 20:03:35 -0400612 if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
613 if (!S_ISLNK(mode))
614 oidset_insert(&gitmodules_found, oid);
615 else
Jeff King38370252019-10-18 00:59:15 -0400616 retval += report(options,
Jeff Kingb2f20392019-10-18 01:02:08 -0400617 oid, OBJ_TREE,
Jeff Kingb7b1fca2018-05-04 20:03:35 -0400618 FSCK_MSG_GITMODULES_SYMLINK,
619 ".gitmodules is a symbolic link");
620 }
Jeff King159e7b02018-05-02 17:20:08 -0400621
Johannes Schindelin288a74b2019-09-23 08:58:11 +0200622 if ((backslash = strchr(name, '\\'))) {
623 while (backslash) {
624 backslash++;
625 has_dotgit |= is_ntfs_dotgit(backslash);
Johannes Schindelinbdfef042019-12-04 21:52:10 +0100626 if (is_ntfs_dotgitmodules(backslash)) {
627 if (!S_ISLNK(mode))
628 oidset_insert(&gitmodules_found, oid);
629 else
Junio C Hamano7034cd02019-12-09 22:17:55 -0800630 retval += report(options, oid, OBJ_TREE,
Johannes Schindelinbdfef042019-12-04 21:52:10 +0100631 FSCK_MSG_GITMODULES_SYMLINK,
632 ".gitmodules is a symbolic link");
633 }
Johannes Schindelin288a74b2019-09-23 08:58:11 +0200634 backslash = strchr(backslash, '\\');
635 }
636 }
637
David Turner8354fa32016-09-27 16:59:51 -0400638 if (update_tree_entry_gently(&desc)) {
Jeff Kingb2f20392019-10-18 01:02:08 -0400639 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
David Turner8354fa32016-09-27 16:59:51 -0400640 break;
641 }
Martin Koeglerba002f32008-02-25 22:46:08 +0100642
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 Schindelin22410542015-06-22 17:25:00 +0200659 if (!options->strict)
Martin Koeglerba002f32008-02-25 22:46:08 +0100660 break;
Jeff King1cf01a32017-09-21 02:25:41 -0400661 /* fallthrough */
Martin Koeglerba002f32008-02-25 22:46:08 +0100662 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 Koeglerba002f32008-02-25 22:46:08 +0100681 }
682
Jeff Kingc479d142012-07-28 11:06:29 -0400683 if (has_null_sha1)
Jeff Kingb2f20392019-10-18 01:02:08 -0400684 retval += report(options, oid, OBJ_TREE, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
Martin Koeglerba002f32008-02-25 22:46:08 +0100685 if (has_full_path)
Jeff Kingb2f20392019-10-18 01:02:08 -0400686 retval += report(options, oid, OBJ_TREE, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
Martin Koeglerba002f32008-02-25 22:46:08 +0100687 if (has_empty_name)
Jeff Kingb2f20392019-10-18 01:02:08 -0400688 retval += report(options, oid, OBJ_TREE, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
Jeff King5d34a432012-11-27 21:27:37 -0500689 if (has_dot)
Jeff Kingb2f20392019-10-18 01:02:08 -0400690 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOT, "contains '.'");
Jeff King5d34a432012-11-27 21:27:37 -0500691 if (has_dotdot)
Jeff Kingb2f20392019-10-18 01:02:08 -0400692 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTDOT, "contains '..'");
Jeff King5c17f512012-11-28 16:35:29 -0500693 if (has_dotgit)
Jeff Kingb2f20392019-10-18 01:02:08 -0400694 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
Martin Koeglerba002f32008-02-25 22:46:08 +0100695 if (has_zero_pad)
Jeff Kingb2f20392019-10-18 01:02:08 -0400696 retval += report(options, oid, OBJ_TREE, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
Martin Koeglerba002f32008-02-25 22:46:08 +0100697 if (has_bad_modes)
Jeff Kingb2f20392019-10-18 01:02:08 -0400698 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
Martin Koeglerba002f32008-02-25 22:46:08 +0100699 if (has_dup_entries)
Jeff Kingb2f20392019-10-18 01:02:08 -0400700 retval += report(options, oid, OBJ_TREE, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries");
Martin Koeglerba002f32008-02-25 22:46:08 +0100701 if (not_properly_sorted)
Jeff Kingb2f20392019-10-18 01:02:08 -0400702 retval += report(options, oid, OBJ_TREE, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted");
Martin Koeglerba002f32008-02-25 22:46:08 +0100703 return retval;
704}
705
Junio C Hamano84d18c02015-06-28 11:18:31 -0700706static int verify_headers(const void *data, unsigned long size,
Jeff Kingcc579002019-10-18 01:00:50 -0400707 const struct object_id *oid, enum object_type type,
708 struct fsck_options *options)
Johannes Schindelin4d0d8972014-09-11 16:26:33 +0200709{
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 Kingcc579002019-10-18 01:00:50 -0400716 return report(options, oid, type,
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200717 FSCK_MSG_NUL_IN_HEADER,
718 "unterminated header: NUL at offset %ld", i);
Johannes Schindelin4d0d8972014-09-11 16:26:33 +0200719 case '\n':
720 if (i + 1 < size && buffer[i + 1] == '\n')
721 return 0;
722 }
723 }
724
Junio C Hamano84d18c02015-06-28 11:18:31 -0700725 /*
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 Kingcc579002019-10-18 01:00:50 -0400734 return report(options, oid, type,
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200735 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
Johannes Schindelin4d0d8972014-09-11 16:26:33 +0200736}
737
Jeff King78543992019-10-18 01:00:04 -0400738static int fsck_ident(const char **ident,
739 const struct object_id *oid, enum object_type type,
740 struct fsck_options *options)
Jonathan Niederdaae1922010-04-24 11:06:08 -0500741{
Johannes Schindeline6826e32015-06-22 17:26:03 +0200742 const char *p = *ident;
Jeff Kingd4b8de02014-02-24 02:39:04 -0500743 char *end;
744
Johannes Schindeline6826e32015-06-22 17:26:03 +0200745 *ident = strchrnul(*ident, '\n');
746 if (**ident == '\n')
747 (*ident)++;
748
749 if (*p == '<')
Jeff King78543992019-10-18 01:00:04 -0400750 return report(options, oid, type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
Johannes Schindeline6826e32015-06-22 17:26:03 +0200751 p += strcspn(p, "<>\n");
752 if (*p == '>')
Jeff King78543992019-10-18 01:00:04 -0400753 return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
Johannes Schindeline6826e32015-06-22 17:26:03 +0200754 if (*p != '<')
Jeff King78543992019-10-18 01:00:04 -0400755 return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
Johannes Schindeline6826e32015-06-22 17:26:03 +0200756 if (p[-1] != ' ')
Jeff King78543992019-10-18 01:00:04 -0400757 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
Johannes Schindeline6826e32015-06-22 17:26:03 +0200758 p++;
759 p += strcspn(p, "<>\n");
760 if (*p != '>')
Jeff King78543992019-10-18 01:00:04 -0400761 return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
Johannes Schindeline6826e32015-06-22 17:26:03 +0200762 p++;
763 if (*p != ' ')
Jeff King78543992019-10-18 01:00:04 -0400764 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
Johannes Schindeline6826e32015-06-22 17:26:03 +0200765 p++;
766 if (*p == '0' && p[1] != ' ')
Jeff King78543992019-10-18 01:00:04 -0400767 return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200768 if (date_overflows(parse_timestamp(p, &end, 10)))
Jeff King78543992019-10-18 01:00:04 -0400769 return report(options, oid, type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
Johannes Schindeline6826e32015-06-22 17:26:03 +0200770 if ((end == p || *end != ' '))
Jeff King78543992019-10-18 01:00:04 -0400771 return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
Johannes Schindeline6826e32015-06-22 17:26:03 +0200772 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 King78543992019-10-18 01:00:04 -0400779 return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
Johannes Schindeline6826e32015-06-22 17:26:03 +0200780 p += 6;
Jonathan Niederdaae1922010-04-24 11:06:08 -0500781 return 0;
782}
783
Jeff Kingc5b42692019-10-18 01:01:48 -0400784static int fsck_commit(const struct object_id *oid,
785 const char *buffer, unsigned long size,
786 struct fsck_options *options)
Martin Koeglerba002f32008-02-25 22:46:08 +0100787{
Jeff Kingf648ee72019-10-18 01:00:59 -0400788 struct object_id tree_oid, parent_oid;
Jeff Kingec652312019-10-18 00:49:10 -0400789 unsigned author_count;
Jonathan Niederdaae1922010-04-24 11:06:08 -0500790 int err;
Junio C Hamano6d2d7802016-04-14 10:58:22 -0700791 const char *buffer_begin = buffer;
brian m. carlsonc54f5ca2018-05-02 00:25:41 +0000792 const char *p;
Martin Koeglerba002f32008-02-25 22:46:08 +0100793
Jeff Kingc5b42692019-10-18 01:01:48 -0400794 if (verify_headers(buffer, size, oid, OBJ_COMMIT, options))
Johannes Schindelin4d0d8972014-09-11 16:26:33 +0200795 return -1;
796
Jeff Kingcf4fff52014-06-18 15:44:19 -0400797 if (!skip_prefix(buffer, "tree ", &buffer))
Jeff Kingc5b42692019-10-18 01:01:48 -0400798 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
brian m. carlsonc54f5ca2018-05-02 00:25:41 +0000799 if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
Jeff Kingc5b42692019-10-18 01:01:48 -0400800 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
Johannes Schindelinb3584762015-06-22 17:26:11 +0200801 if (err)
802 return err;
803 }
brian m. carlsonc54f5ca2018-05-02 00:25:41 +0000804 buffer = p + 1;
Jeff Kingcf4fff52014-06-18 15:44:19 -0400805 while (skip_prefix(buffer, "parent ", &buffer)) {
Jeff Kingf648ee72019-10-18 01:00:59 -0400806 if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') {
Jeff Kingc5b42692019-10-18 01:01:48 -0400807 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
Johannes Schindelinb3584762015-06-22 17:26:11 +0200808 if (err)
809 return err;
810 }
brian m. carlsonc54f5ca2018-05-02 00:25:41 +0000811 buffer = p + 1;
Martin Koeglerba002f32008-02-25 22:46:08 +0100812 }
Johannes Schindelinc9ad1472015-06-22 17:26:23 +0200813 author_count = 0;
814 while (skip_prefix(buffer, "author ", &buffer)) {
815 author_count++;
Jeff Kingc5b42692019-10-18 01:01:48 -0400816 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
Johannes Schindelinc9ad1472015-06-22 17:26:23 +0200817 if (err)
818 return err;
819 }
820 if (author_count < 1)
Jeff Kingc5b42692019-10-18 01:01:48 -0400821 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
Johannes Schindelinc9ad1472015-06-22 17:26:23 +0200822 else if (author_count > 1)
Jeff Kingc5b42692019-10-18 01:01:48 -0400823 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
Jonathan Niederdaae1922010-04-24 11:06:08 -0500824 if (err)
825 return err;
Jeff Kingcf4fff52014-06-18 15:44:19 -0400826 if (!skip_prefix(buffer, "committer ", &buffer))
Jeff Kingc5b42692019-10-18 01:01:48 -0400827 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 Niederdaae1922010-04-24 11:06:08 -0500829 if (err)
830 return err;
Junio C Hamano6d2d7802016-04-14 10:58:22 -0700831 if (memchr(buffer_begin, '\0', size)) {
Jeff Kingc5b42692019-10-18 01:01:48 -0400832 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT,
Junio C Hamano6d2d7802016-04-14 10:58:22 -0700833 "NUL byte in the commit object body");
834 if (err)
835 return err;
836 }
Martin Koeglerba002f32008-02-25 22:46:08 +0100837 return 0;
838}
839
Jeff King103fb6d2019-10-18 01:01:26 -0400840static int fsck_tag(const struct object_id *oid, const char *buffer,
Jeff King2175a0c2019-10-18 00:51:19 -0400841 unsigned long size, struct fsck_options *options)
Johannes Schindelincec097b2014-09-11 16:26:38 +0200842{
Jeff Kingf648ee72019-10-18 01:00:59 -0400843 struct object_id tagged_oid;
Johannes Schindelincec097b2014-09-11 16:26:38 +0200844 int ret = 0;
Jeff King23a173a2019-10-18 00:54:12 -0400845 char *eol;
Johannes Schindelincec097b2014-09-11 16:26:38 +0200846 struct strbuf sb = STRBUF_INIT;
brian m. carlsonc54f5ca2018-05-02 00:25:41 +0000847 const char *p;
Johannes Schindelincec097b2014-09-11 16:26:38 +0200848
Jeff King103fb6d2019-10-18 01:01:26 -0400849 ret = verify_headers(buffer, size, oid, OBJ_TAG, options);
René Scharfe8a272f22015-11-19 17:25:31 +0100850 if (ret)
Johannes Schindelincec097b2014-09-11 16:26:38 +0200851 goto done;
852
853 if (!skip_prefix(buffer, "object ", &buffer)) {
Jeff King103fb6d2019-10-18 01:01:26 -0400854 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
Johannes Schindelincec097b2014-09-11 16:26:38 +0200855 goto done;
856 }
Jeff Kingf648ee72019-10-18 01:00:59 -0400857 if (parse_oid_hex(buffer, &tagged_oid, &p) || *p != '\n') {
Jeff King103fb6d2019-10-18 01:01:26 -0400858 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
Johannes Schindelin7d7d5b02015-06-22 17:26:30 +0200859 if (ret)
860 goto done;
Johannes Schindelincec097b2014-09-11 16:26:38 +0200861 }
brian m. carlsonc54f5ca2018-05-02 00:25:41 +0000862 buffer = p + 1;
Johannes Schindelincec097b2014-09-11 16:26:38 +0200863
864 if (!skip_prefix(buffer, "type ", &buffer)) {
Jeff King103fb6d2019-10-18 01:01:26 -0400865 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
Johannes Schindelincec097b2014-09-11 16:26:38 +0200866 goto done;
867 }
868 eol = strchr(buffer, '\n');
869 if (!eol) {
Jeff King103fb6d2019-10-18 01:01:26 -0400870 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
Johannes Schindelincec097b2014-09-11 16:26:38 +0200871 goto done;
872 }
873 if (type_from_string_gently(buffer, eol - buffer, 1) < 0)
Jeff King103fb6d2019-10-18 01:01:26 -0400874 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
Johannes Schindelincec097b2014-09-11 16:26:38 +0200875 if (ret)
876 goto done;
877 buffer = eol + 1;
878
879 if (!skip_prefix(buffer, "tag ", &buffer)) {
Jeff King103fb6d2019-10-18 01:01:26 -0400880 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
Johannes Schindelincec097b2014-09-11 16:26:38 +0200881 goto done;
882 }
883 eol = strchr(buffer, '\n');
884 if (!eol) {
Jeff King103fb6d2019-10-18 01:01:26 -0400885 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
Johannes Schindelincec097b2014-09-11 16:26:38 +0200886 goto done;
887 }
888 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
Johannes Schindelinf27d05b2015-06-22 17:26:54 +0200889 if (check_refname_format(sb.buf, 0)) {
Jeff King103fb6d2019-10-18 01:01:26 -0400890 ret = report(options, oid, OBJ_TAG,
Jeff King38370252019-10-18 00:59:15 -0400891 FSCK_MSG_BAD_TAG_NAME,
892 "invalid 'tag' name: %.*s",
893 (int)(eol - buffer), buffer);
Johannes Schindelinf27d05b2015-06-22 17:26:54 +0200894 if (ret)
895 goto done;
896 }
Johannes Schindelincec097b2014-09-11 16:26:38 +0200897 buffer = eol + 1;
898
Johannes Schindelinf27d05b2015-06-22 17:26:54 +0200899 if (!skip_prefix(buffer, "tagger ", &buffer)) {
Johannes Schindelincec097b2014-09-11 16:26:38 +0200900 /* early tags do not contain 'tagger' lines; warn only */
Jeff King103fb6d2019-10-18 01:01:26 -0400901 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
Johannes Schindelinf27d05b2015-06-22 17:26:54 +0200902 if (ret)
903 goto done;
904 }
Johannes Schindelincec097b2014-09-11 16:26:38 +0200905 else
Jeff King103fb6d2019-10-18 01:01:26 -0400906 ret = fsck_ident(&buffer, oid, OBJ_TAG, options);
Johannes Schindelincec097b2014-09-11 16:26:38 +0200907
908done:
909 strbuf_release(&sb);
Johannes Schindelincec097b2014-09-11 16:26:38 +0200910 return ret;
911}
912
Jeff Kinged8b10f2018-05-02 17:25:27 -0400913struct fsck_gitmodules_data {
Jeff King6da40b22019-10-18 00:59:29 -0400914 const struct object_id *oid;
Jeff Kinged8b10f2018-05-02 17:25:27 -0400915 struct fsck_options *options;
916 int ret;
917};
918
919static 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 King38370252019-10-18 00:59:15 -0400932 data->ret |= report(data->options,
Jeff King6da40b22019-10-18 00:59:29 -0400933 data->oid, OBJ_BLOB,
Jeff Kinged8b10f2018-05-02 17:25:27 -0400934 FSCK_MSG_GITMODULES_NAME,
935 "disallowed submodule name: %s",
936 name);
Jeff Kinga1241332018-09-24 04:37:17 -0400937 if (!strcmp(key, "url") && value &&
938 looks_like_command_line_option(value))
Jeff King38370252019-10-18 00:59:15 -0400939 data->ret |= report(data->options,
Jeff King6da40b22019-10-18 00:59:29 -0400940 data->oid, OBJ_BLOB,
Jeff Kinga1241332018-09-24 04:37:17 -0400941 FSCK_MSG_GITMODULES_URL,
942 "disallowed submodule url: %s",
943 value);
Jeff King1a7fd1f2018-09-24 04:42:19 -0400944 if (!strcmp(key, "path") && value &&
945 looks_like_command_line_option(value))
Jeff King38370252019-10-18 00:59:15 -0400946 data->ret |= report(data->options,
Jeff King6da40b22019-10-18 00:59:29 -0400947 data->oid, OBJ_BLOB,
Jeff King1a7fd1f2018-09-24 04:42:19 -0400948 FSCK_MSG_GITMODULES_PATH,
949 "disallowed submodule path: %s",
950 value);
Jonathan Niederbb922552019-12-05 01:30:43 -0800951 if (!strcmp(key, "update") && value &&
952 parse_submodule_update_type(value) == SM_UPDATE_COMMAND)
Junio C Hamano7034cd02019-12-09 22:17:55 -0800953 data->ret |= report(data->options, data->oid, OBJ_BLOB,
Jonathan Niederbb922552019-12-05 01:30:43 -0800954 FSCK_MSG_GITMODULES_UPDATE,
955 "disallowed submodule update setting: %s",
956 value);
Jeff Kinged8b10f2018-05-02 17:25:27 -0400957 free(name);
958
959 return 0;
960}
961
Jeff King6da40b22019-10-18 00:59:29 -0400962static int fsck_blob(const struct object_id *oid, const char *buf,
Jeff King7ac4f3a2018-05-02 15:44:51 -0400963 unsigned long size, struct fsck_options *options)
964{
Jeff Kinged8b10f2018-05-02 17:25:27 -0400965 struct fsck_gitmodules_data data;
Jeff Kingde6bd9e2018-06-28 18:06:04 -0400966 struct config_options config_opts = { 0 };
Jeff Kinged8b10f2018-05-02 17:25:27 -0400967
Jeff King6da40b22019-10-18 00:59:29 -0400968 if (!oidset_contains(&gitmodules_found, oid))
Jeff Kinged8b10f2018-05-02 17:25:27 -0400969 return 0;
Jeff King6da40b22019-10-18 00:59:29 -0400970 oidset_insert(&gitmodules_done, oid);
Jeff Kinged8b10f2018-05-02 17:25:27 -0400971
Jeff King6da40b22019-10-18 00:59:29 -0400972 if (object_on_skiplist(options, oid))
Ramsay Jonesfb162872018-06-27 19:39:53 +0100973 return 0;
974
Jeff Kinged8b10f2018-05-02 17:25:27 -0400975 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 King6da40b22019-10-18 00:59:29 -0400981 return report(options, oid, OBJ_BLOB,
Jeff King0d687642018-07-13 15:39:53 -0400982 FSCK_MSG_GITMODULES_LARGE,
Jeff Kinged8b10f2018-05-02 17:25:27 -0400983 ".gitmodules too large to parse");
984 }
985
Jeff King6da40b22019-10-18 00:59:29 -0400986 data.oid = oid;
Jeff Kinged8b10f2018-05-02 17:25:27 -0400987 data.options = options;
988 data.ret = 0;
Jeff Kingde6bd9e2018-06-28 18:06:04 -0400989 config_opts.error_action = CONFIG_ERROR_SILENT;
Jeff Kinged8b10f2018-05-02 17:25:27 -0400990 if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
Jeff Kingde6bd9e2018-06-28 18:06:04 -0400991 ".gitmodules", buf, size, &data, &config_opts))
Jeff King6da40b22019-10-18 00:59:29 -0400992 data.ret |= report(options, oid, OBJ_BLOB,
Jeff Kinged8b10f2018-05-02 17:25:27 -0400993 FSCK_MSG_GITMODULES_PARSE,
994 "could not parse gitmodules blob");
995
996 return data.ret;
Jeff King7ac4f3a2018-05-02 15:44:51 -0400997}
998
Johannes Schindelin90a398b2014-09-10 15:52:51 +0200999int fsck_object(struct object *obj, void *data, unsigned long size,
Johannes Schindelin22410542015-06-22 17:25:00 +02001000 struct fsck_options *options)
Martin Koeglerba002f32008-02-25 22:46:08 +01001001{
1002 if (!obj)
Jeff King38370252019-10-18 00:59:15 -04001003 return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
Martin Koeglerba002f32008-02-25 22:46:08 +01001004
1005 if (obj->type == OBJ_BLOB)
Jeff King6da40b22019-10-18 00:59:29 -04001006 return fsck_blob(&obj->oid, data, size, options);
Martin Koeglerba002f32008-02-25 22:46:08 +01001007 if (obj->type == OBJ_TREE)
Jeff Kingb2f20392019-10-18 01:02:08 -04001008 return fsck_tree(&obj->oid, data, size, options);
Martin Koeglerba002f32008-02-25 22:46:08 +01001009 if (obj->type == OBJ_COMMIT)
Jeff Kingc5b42692019-10-18 01:01:48 -04001010 return fsck_commit(&obj->oid, data, size, options);
Martin Koeglerba002f32008-02-25 22:46:08 +01001011 if (obj->type == OBJ_TAG)
Jeff King103fb6d2019-10-18 01:01:26 -04001012 return fsck_tag(&obj->oid, data, size, options);
Martin Koeglerba002f32008-02-25 22:46:08 +01001013
Jeff King38370252019-10-18 00:59:15 -04001014 return report(options, &obj->oid, obj->type,
1015 FSCK_MSG_UNKNOWN_TYPE,
1016 "unknown type '%d' (internal fsck error)",
1017 obj->type);
Martin Koeglerba002f32008-02-25 22:46:08 +01001018}
Martin Koeglerd6ffc8d2008-02-25 22:46:09 +01001019
Johannes Schindelin1cd772c2016-07-17 12:59:57 +02001020int fsck_error_function(struct fsck_options *o,
Jeff King5afc4b12019-10-18 00:58:40 -04001021 const struct object_id *oid,
1022 enum object_type object_type,
1023 int msg_type, const char *message)
Martin Koeglerd6ffc8d2008-02-25 22:46:09 +01001024{
Johannes Schindelin0282f4d2015-06-22 17:25:25 +02001025 if (msg_type == FSCK_WARN) {
Jeff King5afc4b12019-10-18 00:58:40 -04001026 warning("object %s: %s", fsck_describe_object(o, oid), message);
Johannes Schindelin0282f4d2015-06-22 17:25:25 +02001027 return 0;
1028 }
Jeff King5afc4b12019-10-18 00:58:40 -04001029 error("object %s: %s", fsck_describe_object(o, oid), message);
Martin Koeglerd6ffc8d2008-02-25 22:46:09 +01001030 return 1;
1031}
Jeff King159e7b02018-05-02 17:20:08 -04001032
1033int 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 King159e7b02018-05-02 17:20:08 -04001041 enum object_type type;
1042 unsigned long size;
1043 char *buf;
1044
1045 if (oidset_contains(&gitmodules_done, oid))
1046 continue;
1047
Junio C Hamano7913f532018-05-29 17:09:58 +09001048 buf = read_object_file(oid, &type, &size);
Jeff King159e7b02018-05-02 17:20:08 -04001049 if (!buf) {
Jeff Kingb8b00f12019-10-18 00:59:54 -04001050 if (is_promisor_object(oid))
Jeff King27387442018-05-14 12:22:48 -04001051 continue;
Jeff King38370252019-10-18 00:59:15 -04001052 ret |= report(options,
Jeff Kingb8b00f12019-10-18 00:59:54 -04001053 oid, OBJ_BLOB,
Jeff King159e7b02018-05-02 17:20:08 -04001054 FSCK_MSG_GITMODULES_MISSING,
1055 "unable to read .gitmodules blob");
1056 continue;
1057 }
1058
1059 if (type == OBJ_BLOB)
Jeff Kingb8b00f12019-10-18 00:59:54 -04001060 ret |= fsck_blob(oid, buf, size, options);
Jeff King159e7b02018-05-02 17:20:08 -04001061 else
Jeff King38370252019-10-18 00:59:15 -04001062 ret |= report(options,
Jeff Kingb8b00f12019-10-18 00:59:54 -04001063 oid, type,
Jeff King159e7b02018-05-02 17:20:08 -04001064 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}