blob: c5a81e4ff05a0558e3e135f479bf298c3b46ede6 [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"
Jonathan Niedera2b26ff2020-04-18 20:52:34 -070012#include "url.h"
Jeff Kinga18fcc92014-12-15 18:21:57 -050013#include "utf8.h"
Johannes Schindelin7b35efd2016-07-17 12:59:49 +020014#include "decorate.h"
Jeff King159e7b02018-05-02 17:20:08 -040015#include "oidset.h"
Jeff King27387442018-05-14 12:22:48 -040016#include "packfile.h"
Jeff Kinged8b10f2018-05-02 17:25:27 -040017#include "submodule-config.h"
18#include "config.h"
Jeff King07259e72020-03-11 18:48:24 -040019#include "credential.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020020#include "help.h"
Jeff King159e7b02018-05-02 17:20:08 -040021
22static struct oidset gitmodules_found = OIDSET_INIT;
23static struct oidset gitmodules_done = OIDSET_INIT;
Martin Koegler355885d2008-02-25 22:46:04 +010024
Johannes Schindelinc99ba492015-06-22 17:25:09 +020025#define FOREACH_MSG_ID(FUNC) \
Johannes Schindelinf50c4402015-06-22 17:26:42 +020026 /* fatal errors */ \
27 FUNC(NUL_IN_HEADER, FATAL) \
28 FUNC(UNTERMINATED_HEADER, FATAL) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020029 /* errors */ \
30 FUNC(BAD_DATE, ERROR) \
31 FUNC(BAD_DATE_OVERFLOW, ERROR) \
32 FUNC(BAD_EMAIL, ERROR) \
33 FUNC(BAD_NAME, ERROR) \
34 FUNC(BAD_OBJECT_SHA1, ERROR) \
35 FUNC(BAD_PARENT_SHA1, ERROR) \
36 FUNC(BAD_TAG_OBJECT, ERROR) \
37 FUNC(BAD_TIMEZONE, ERROR) \
38 FUNC(BAD_TREE, ERROR) \
39 FUNC(BAD_TREE_SHA1, ERROR) \
40 FUNC(BAD_TYPE, ERROR) \
41 FUNC(DUPLICATE_ENTRIES, ERROR) \
42 FUNC(MISSING_AUTHOR, ERROR) \
43 FUNC(MISSING_COMMITTER, ERROR) \
44 FUNC(MISSING_EMAIL, ERROR) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020045 FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
46 FUNC(MISSING_OBJECT, ERROR) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020047 FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \
48 FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \
49 FUNC(MISSING_TAG, ERROR) \
50 FUNC(MISSING_TAG_ENTRY, ERROR) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020051 FUNC(MISSING_TREE, ERROR) \
Jeff King159e7b02018-05-02 17:20:08 -040052 FUNC(MISSING_TREE_OBJECT, ERROR) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020053 FUNC(MISSING_TYPE, ERROR) \
54 FUNC(MISSING_TYPE_ENTRY, ERROR) \
Johannes Schindelinc9ad1472015-06-22 17:26:23 +020055 FUNC(MULTIPLE_AUTHORS, ERROR) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020056 FUNC(TREE_NOT_SORTED, ERROR) \
57 FUNC(UNKNOWN_TYPE, ERROR) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020058 FUNC(ZERO_PADDED_DATE, ERROR) \
Jeff King159e7b02018-05-02 17:20:08 -040059 FUNC(GITMODULES_MISSING, ERROR) \
60 FUNC(GITMODULES_BLOB, ERROR) \
Jeff King0d687642018-07-13 15:39:53 -040061 FUNC(GITMODULES_LARGE, ERROR) \
Jeff Kinged8b10f2018-05-02 17:25:27 -040062 FUNC(GITMODULES_NAME, ERROR) \
Jeff Kingb7b1fca2018-05-04 20:03:35 -040063 FUNC(GITMODULES_SYMLINK, ERROR) \
Jeff Kinga1241332018-09-24 04:37:17 -040064 FUNC(GITMODULES_URL, ERROR) \
Jeff King1a7fd1f2018-09-24 04:42:19 -040065 FUNC(GITMODULES_PATH, ERROR) \
Jonathan Niederbb922552019-12-05 01:30:43 -080066 FUNC(GITMODULES_UPDATE, ERROR) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020067 /* warnings */ \
68 FUNC(BAD_FILEMODE, WARN) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020069 FUNC(EMPTY_NAME, WARN) \
70 FUNC(FULL_PATHNAME, WARN) \
71 FUNC(HAS_DOT, WARN) \
72 FUNC(HAS_DOTDOT, WARN) \
73 FUNC(HAS_DOTGIT, WARN) \
Johannes Schindelinc99ba492015-06-22 17:25:09 +020074 FUNC(NULL_SHA1, WARN) \
Johannes Schindelinf27d05b2015-06-22 17:26:54 +020075 FUNC(ZERO_PADDED_FILEMODE, WARN) \
Junio C Hamano6d2d7802016-04-14 10:58:22 -070076 FUNC(NUL_IN_COMMIT, WARN) \
Johannes Schindelinf27d05b2015-06-22 17:26:54 +020077 /* infos (reported as warnings, but ignored by default) */ \
Jeff King64eb14d2018-07-13 15:39:58 -040078 FUNC(GITMODULES_PARSE, INFO) \
Johannes Schindelinf27d05b2015-06-22 17:26:54 +020079 FUNC(BAD_TAG_NAME, INFO) \
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +010080 FUNC(MISSING_TAGGER_ENTRY, INFO) \
81 /* ignored (elevated when requested) */ \
82 FUNC(EXTRA_HEADER_ENTRY, IGNORE)
Johannes Schindelinc99ba492015-06-22 17:25:09 +020083
84#define MSG_ID(id, msg_type) FSCK_MSG_##id,
85enum fsck_msg_id {
86 FOREACH_MSG_ID(MSG_ID)
87 FSCK_MSG_MAX
88};
89#undef MSG_ID
90
Johannes Schindelinf417eed2015-06-22 17:25:14 +020091#define STR(x) #x
Nguyễn Thái Ngọc Duya4a9cc12018-05-26 15:55:25 +020092#define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
Johannes Schindelinc99ba492015-06-22 17:25:09 +020093static struct {
Johannes Schindelinf417eed2015-06-22 17:25:14 +020094 const char *id_string;
95 const char *downcased;
Nguyễn Thái Ngọc Duya4a9cc12018-05-26 15:55:25 +020096 const char *camelcased;
Ævar Arnfjörð Bjarmason1b32b592021-03-28 15:15:40 +020097 enum fsck_msg_type msg_type;
Johannes Schindelinc99ba492015-06-22 17:25:09 +020098} msg_id_info[FSCK_MSG_MAX + 1] = {
99 FOREACH_MSG_ID(MSG_ID)
Nguyễn Thái Ngọc Duya4a9cc12018-05-26 15:55:25 +0200100 { NULL, NULL, NULL, -1 }
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200101};
102#undef MSG_ID
103
Nguyễn Thái Ngọc Duya46baac2018-05-26 15:55:23 +0200104static void prepare_msg_ids(void)
105{
106 int i;
107
108 if (msg_id_info[0].downcased)
109 return;
110
111 /* convert id_string to lower case, without underscores. */
112 for (i = 0; i < FSCK_MSG_MAX; i++) {
113 const char *p = msg_id_info[i].id_string;
114 int len = strlen(p);
115 char *q = xmalloc(len);
116
117 msg_id_info[i].downcased = q;
118 while (*p)
119 if (*p == '_')
120 p++;
121 else
122 *(q)++ = tolower(*(p)++);
123 *q = '\0';
Nguyễn Thái Ngọc Duya4a9cc12018-05-26 15:55:25 +0200124
125 p = msg_id_info[i].id_string;
126 q = xmalloc(len);
127 msg_id_info[i].camelcased = q;
128 while (*p) {
129 if (*p == '_') {
130 p++;
131 if (*p)
132 *q++ = *p++;
133 } else {
134 *q++ = tolower(*p++);
135 }
136 }
137 *q = '\0';
Nguyễn Thái Ngọc Duya46baac2018-05-26 15:55:23 +0200138 }
139}
140
Johannes Schindelinf417eed2015-06-22 17:25:14 +0200141static int parse_msg_id(const char *text)
142{
143 int i;
144
Nguyễn Thái Ngọc Duya46baac2018-05-26 15:55:23 +0200145 prepare_msg_ids();
Johannes Schindelinf417eed2015-06-22 17:25:14 +0200146
147 for (i = 0; i < FSCK_MSG_MAX; i++)
148 if (!strcmp(text, msg_id_info[i].downcased))
149 return i;
150
151 return -1;
152}
153
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +0200154void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
155{
156 int i;
157
158 prepare_msg_ids();
159
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +0200160 for (i = 0; i < FSCK_MSG_MAX; i++)
Nguyễn Thái Ngọc Duya4a9cc12018-05-26 15:55:25 +0200161 list_config_item(list, prefix, msg_id_info[i].camelcased);
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +0200162}
163
Ævar Arnfjörð Bjarmason1b32b592021-03-28 15:15:40 +0200164static enum fsck_msg_type fsck_msg_type(enum fsck_msg_id msg_id,
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200165 struct fsck_options *options)
166{
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200167 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
168
Ævar Arnfjörð Bjarmasone35d65a2021-03-28 15:15:39 +0200169 if (!options->msg_type) {
Ævar Arnfjörð Bjarmason1b32b592021-03-28 15:15:40 +0200170 enum fsck_msg_type msg_type = msg_id_info[msg_id].msg_type;
Ævar Arnfjörð Bjarmasone35d65a2021-03-28 15:15:39 +0200171
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200172 if (options->strict && msg_type == FSCK_WARN)
173 msg_type = FSCK_ERROR;
Ævar Arnfjörð Bjarmasone35d65a2021-03-28 15:15:39 +0200174 return msg_type;
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200175 }
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200176
Ævar Arnfjörð Bjarmasone35d65a2021-03-28 15:15:39 +0200177 return options->msg_type[msg_id];
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200178}
179
Ævar Arnfjörð Bjarmason1b32b592021-03-28 15:15:40 +0200180static enum fsck_msg_type parse_msg_type(const char *str)
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200181{
182 if (!strcmp(str, "error"))
183 return FSCK_ERROR;
184 else if (!strcmp(str, "warn"))
185 return FSCK_WARN;
Johannes Schindelinefaba7c2015-06-22 17:26:48 +0200186 else if (!strcmp(str, "ignore"))
187 return FSCK_IGNORE;
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200188 else
189 die("Unknown fsck message type: '%s'", str);
190}
191
Johannes Schindelin5d477a32015-06-22 17:25:31 +0200192int is_valid_msg_type(const char *msg_id, const char *msg_type)
193{
194 if (parse_msg_id(msg_id) < 0)
195 return 0;
196 parse_msg_type(msg_type);
197 return 1;
198}
199
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200200void fsck_set_msg_type(struct fsck_options *options,
Ævar Arnfjörð Bjarmasonf1abc2d2021-03-28 15:15:36 +0200201 const char *msg_id_str, const char *msg_type_str)
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200202{
Ævar Arnfjörð Bjarmason1b32b592021-03-28 15:15:40 +0200203 int msg_id = parse_msg_id(msg_id_str);
204 enum fsck_msg_type msg_type;
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200205
Ævar Arnfjörð Bjarmasonf1abc2d2021-03-28 15:15:36 +0200206 if (msg_id < 0)
207 die("Unhandled message id: %s", msg_id_str);
208 msg_type = parse_msg_type(msg_type_str);
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200209
Ævar Arnfjörð Bjarmasonf1abc2d2021-03-28 15:15:36 +0200210 if (msg_type != FSCK_ERROR && msg_id_info[msg_id].msg_type == FSCK_FATAL)
211 die("Cannot demote %s to %s", msg_id_str, msg_type_str);
Johannes Schindelinf50c4402015-06-22 17:26:42 +0200212
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200213 if (!options->msg_type) {
214 int i;
Ævar Arnfjörð Bjarmason1b32b592021-03-28 15:15:40 +0200215 enum fsck_msg_type *severity;
Ævar Arnfjörð Bjarmasonf1abc2d2021-03-28 15:15:36 +0200216 ALLOC_ARRAY(severity, FSCK_MSG_MAX);
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200217 for (i = 0; i < FSCK_MSG_MAX; i++)
Ævar Arnfjörð Bjarmasonf1abc2d2021-03-28 15:15:36 +0200218 severity[i] = fsck_msg_type(i, options);
219 options->msg_type = severity;
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200220 }
221
Ævar Arnfjörð Bjarmasonf1abc2d2021-03-28 15:15:36 +0200222 options->msg_type[msg_id] = msg_type;
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200223}
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
Jeff Kingf5979372019-10-18 00:58:51 -0400263static int object_on_skiplist(struct fsck_options *opts,
264 const struct object_id *oid)
Ramsay Jonesfb162872018-06-27 19:39:53 +0100265{
Jeff Kingf5979372019-10-18 00:58:51 -0400266 return opts && oid && oidset_contains(&opts->skiplist, oid);
Ramsay Jonesfb162872018-06-27 19:39:53 +0100267}
268
Jeff King38370252019-10-18 00:59:15 -0400269__attribute__((format (printf, 5, 6)))
270static int report(struct fsck_options *options,
271 const struct object_id *oid, enum object_type object_type,
Ævar Arnfjörð Bjarmason35af7542021-03-28 15:15:38 +0200272 enum fsck_msg_id msg_id, const char *fmt, ...)
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200273{
274 va_list ap;
275 struct strbuf sb = STRBUF_INIT;
Ævar Arnfjörð Bjarmason1b32b592021-03-28 15:15:40 +0200276 enum fsck_msg_type msg_type = fsck_msg_type(msg_id, options);
277 int result;
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200278
Johannes Schindelinefaba7c2015-06-22 17:26:48 +0200279 if (msg_type == FSCK_IGNORE)
280 return 0;
281
Jeff King38370252019-10-18 00:59:15 -0400282 if (object_on_skiplist(options, oid))
Johannes Schindelincd94c6f2015-06-22 17:27:18 +0200283 return 0;
284
Johannes Schindelinf50c4402015-06-22 17:26:42 +0200285 if (msg_type == FSCK_FATAL)
286 msg_type = FSCK_ERROR;
Johannes Schindelinf27d05b2015-06-22 17:26:54 +0200287 else if (msg_type == FSCK_INFO)
288 msg_type = FSCK_WARN;
Johannes Schindelinf50c4402015-06-22 17:26:42 +0200289
Ævar Arnfjörð Bjarmason034a7b72021-03-28 15:15:37 +0200290 prepare_msg_ids();
Ævar Arnfjörð Bjarmason35af7542021-03-28 15:15:38 +0200291 strbuf_addf(&sb, "%s: ", msg_id_info[msg_id].camelcased);
Johannes Schindelin71ab8fa2015-06-22 17:25:52 +0200292
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200293 va_start(ap, fmt);
294 strbuf_vaddf(&sb, fmt, ap);
Jeff King38370252019-10-18 00:59:15 -0400295 result = options->error_func(options, oid, object_type,
Jeff King5afc4b12019-10-18 00:58:40 -0400296 msg_type, sb.buf);
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200297 strbuf_release(&sb);
298 va_end(ap);
299
300 return result;
301}
302
Jeff Kinga59cfb32019-10-18 00:56:13 -0400303void fsck_enable_object_names(struct fsck_options *options)
304{
305 if (!options->object_names)
Jeff King73390292019-10-18 00:57:37 -0400306 options->object_names = kh_init_oid_map();
Jeff Kinga59cfb32019-10-18 00:56:13 -0400307}
308
Jeff King73390292019-10-18 00:57:37 -0400309const char *fsck_get_object_name(struct fsck_options *options,
310 const struct object_id *oid)
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200311{
Jeff King73390292019-10-18 00:57:37 -0400312 khiter_t pos;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200313 if (!options->object_names)
314 return NULL;
Jeff King73390292019-10-18 00:57:37 -0400315 pos = kh_get_oid_map(options->object_names, *oid);
316 if (pos >= kh_end(options->object_names))
317 return NULL;
318 return kh_value(options->object_names, pos);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200319}
320
Jeff King73390292019-10-18 00:57:37 -0400321void fsck_put_object_name(struct fsck_options *options,
322 const struct object_id *oid,
Jeff Kinga59cfb32019-10-18 00:56:13 -0400323 const char *fmt, ...)
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200324{
325 va_list ap;
326 struct strbuf buf = STRBUF_INIT;
Jeff King73390292019-10-18 00:57:37 -0400327 khiter_t pos;
328 int hashret;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200329
330 if (!options->object_names)
331 return;
Jeff King73390292019-10-18 00:57:37 -0400332
333 pos = kh_put_oid_map(options->object_names, *oid, &hashret);
334 if (!hashret)
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200335 return;
336 va_start(ap, fmt);
337 strbuf_vaddf(&buf, fmt, ap);
Jeff King73390292019-10-18 00:57:37 -0400338 kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200339 va_end(ap);
340}
341
Jeff Kinga59cfb32019-10-18 00:56:13 -0400342const char *fsck_describe_object(struct fsck_options *options,
Jeff King73390292019-10-18 00:57:37 -0400343 const struct object_id *oid)
Johannes Schindelin90cf5902016-07-17 13:00:02 +0200344{
Jeff Kinga59cfb32019-10-18 00:56:13 -0400345 static struct strbuf bufs[] = {
346 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
347 };
348 static int b = 0;
349 struct strbuf *buf;
Jeff King73390292019-10-18 00:57:37 -0400350 const char *name = fsck_get_object_name(options, oid);
Johannes Schindelin90cf5902016-07-17 13:00:02 +0200351
Jeff Kinga59cfb32019-10-18 00:56:13 -0400352 buf = bufs + b;
353 b = (b + 1) % ARRAY_SIZE(bufs);
354 strbuf_reset(buf);
Jeff King73390292019-10-18 00:57:37 -0400355 strbuf_addstr(buf, oid_to_hex(oid));
Jeff Kinga59cfb32019-10-18 00:56:13 -0400356 if (name)
357 strbuf_addf(buf, " (%s)", name);
358
359 return buf->buf;
Johannes Schindelin90cf5902016-07-17 13:00:02 +0200360}
361
Johannes Schindelin22410542015-06-22 17:25:00 +0200362static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
Martin Koegler355885d2008-02-25 22:46:04 +0100363{
364 struct tree_desc desc;
365 struct name_entry entry;
366 int res = 0;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200367 const char *name;
Martin Koegler355885d2008-02-25 22:46:04 +0100368
369 if (parse_tree(tree))
370 return -1;
371
Jeff King73390292019-10-18 00:57:37 -0400372 name = fsck_get_object_name(options, &tree->object.oid);
David Turner8354fa32016-09-27 16:59:51 -0400373 if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
374 return -1;
375 while (tree_entry_gently(&desc, &entry)) {
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200376 struct object *obj;
Martin Koegler355885d2008-02-25 22:46:04 +0100377 int result;
378
379 if (S_ISGITLINK(entry.mode))
380 continue;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200381
382 if (S_ISDIR(entry.mode)) {
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000383 obj = (struct object *)lookup_tree(the_repository, &entry.oid);
René Scharfe2720f6d2017-10-05 21:41:26 +0200384 if (name && obj)
Jeff King73390292019-10-18 00:57:37 -0400385 fsck_put_object_name(options, &entry.oid, "%s%s/",
Jeff Kinga59cfb32019-10-18 00:56:13 -0400386 name, entry.path);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200387 result = options->walk(obj, OBJ_TREE, data, options);
388 }
389 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000390 obj = (struct object *)lookup_blob(the_repository, &entry.oid);
René Scharfe2720f6d2017-10-05 21:41:26 +0200391 if (name && obj)
Jeff King73390292019-10-18 00:57:37 -0400392 fsck_put_object_name(options, &entry.oid, "%s%s",
Jeff Kinga59cfb32019-10-18 00:56:13 -0400393 name, entry.path);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200394 result = options->walk(obj, OBJ_BLOB, data, options);
395 }
Martin Koegler355885d2008-02-25 22:46:04 +0100396 else {
Pete Wyckoff82247e92012-04-29 20:28:45 -0400397 result = error("in tree %s: entry %s has bad mode %.6o",
Jeff King73390292019-10-18 00:57:37 -0400398 fsck_describe_object(options, &tree->object.oid),
Jeff Kinga59cfb32019-10-18 00:56:13 -0400399 entry.path, entry.mode);
Martin Koegler355885d2008-02-25 22:46:04 +0100400 }
401 if (result < 0)
402 return result;
403 if (!res)
404 res = result;
405 }
406 return res;
407}
408
Johannes Schindelin22410542015-06-22 17:25:00 +0200409static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
Martin Koegler355885d2008-02-25 22:46:04 +0100410{
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200411 int counter = 0, generation = 0, name_prefix_len = 0;
Martin Koegler355885d2008-02-25 22:46:04 +0100412 struct commit_list *parents;
413 int res;
414 int result;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200415 const char *name;
Martin Koegler355885d2008-02-25 22:46:04 +0100416
417 if (parse_commit(commit))
418 return -1;
419
Jeff King73390292019-10-18 00:57:37 -0400420 name = fsck_get_object_name(options, &commit->object.oid);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200421 if (name)
Jeff King73390292019-10-18 00:57:37 -0400422 fsck_put_object_name(options, get_commit_tree_oid(commit),
Jeff Kinga59cfb32019-10-18 00:56:13 -0400423 "%s:", name);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200424
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000425 result = options->walk((struct object *)get_commit_tree(commit),
426 OBJ_TREE, data, options);
Martin Koegler355885d2008-02-25 22:46:04 +0100427 if (result < 0)
428 return result;
429 res = result;
430
431 parents = commit->parents;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200432 if (name && parents) {
433 int len = strlen(name), power;
434
435 if (len && name[len - 1] == '^') {
436 generation = 1;
437 name_prefix_len = len - 1;
438 }
439 else { /* parse ~<generation> suffix */
440 for (generation = 0, power = 1;
441 len && isdigit(name[len - 1]);
442 power *= 10)
443 generation += power * (name[--len] - '0');
444 if (power > 1 && len && name[len - 1] == '~')
445 name_prefix_len = len - 1;
Johannes Schindeline89f8932021-02-10 18:01:30 +0000446 else {
447 /* Maybe a non-first parent, e.g. HEAD^2 */
448 generation = 0;
449 name_prefix_len = len;
450 }
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200451 }
452 }
453
Martin Koegler355885d2008-02-25 22:46:04 +0100454 while (parents) {
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200455 if (name) {
Jeff King73390292019-10-18 00:57:37 -0400456 struct object_id *oid = &parents->item->object.oid;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200457
Junio C Hamanob84c78382018-10-24 10:25:12 +0900458 if (counter++)
Jeff King73390292019-10-18 00:57:37 -0400459 fsck_put_object_name(options, oid, "%s^%d",
Jeff Kinga59cfb32019-10-18 00:56:13 -0400460 name, counter);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200461 else if (generation > 0)
Jeff King73390292019-10-18 00:57:37 -0400462 fsck_put_object_name(options, oid, "%.*s~%d",
Jeff Kinga59cfb32019-10-18 00:56:13 -0400463 name_prefix_len, name,
464 generation + 1);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200465 else
Jeff King73390292019-10-18 00:57:37 -0400466 fsck_put_object_name(options, oid, "%s^", name);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200467 }
Johannes Schindelin22410542015-06-22 17:25:00 +0200468 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
Martin Koegler355885d2008-02-25 22:46:04 +0100469 if (result < 0)
470 return result;
471 if (!res)
472 res = result;
473 parents = parents->next;
474 }
475 return res;
476}
477
Johannes Schindelin22410542015-06-22 17:25:00 +0200478static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
Martin Koegler355885d2008-02-25 22:46:04 +0100479{
Jeff King73390292019-10-18 00:57:37 -0400480 const char *name = fsck_get_object_name(options, &tag->object.oid);
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200481
Martin Koegler355885d2008-02-25 22:46:04 +0100482 if (parse_tag(tag))
483 return -1;
Johannes Schindelin7b35efd2016-07-17 12:59:49 +0200484 if (name)
Jeff King73390292019-10-18 00:57:37 -0400485 fsck_put_object_name(options, &tag->tagged->oid, "%s", name);
Johannes Schindelin22410542015-06-22 17:25:00 +0200486 return options->walk(tag->tagged, OBJ_ANY, data, options);
Martin Koegler355885d2008-02-25 22:46:04 +0100487}
488
Johannes Schindelin22410542015-06-22 17:25:00 +0200489int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
Martin Koegler355885d2008-02-25 22:46:04 +0100490{
491 if (!obj)
492 return -1;
Jeff Kinga2b22852017-01-25 23:12:07 -0500493
494 if (obj->type == OBJ_NONE)
Stefan Beller109cd762018-06-28 18:21:51 -0700495 parse_object(the_repository, &obj->oid);
Jeff Kinga2b22852017-01-25 23:12:07 -0500496
Martin Koegler355885d2008-02-25 22:46:04 +0100497 switch (obj->type) {
498 case OBJ_BLOB:
499 return 0;
500 case OBJ_TREE:
Johannes Schindelin22410542015-06-22 17:25:00 +0200501 return fsck_walk_tree((struct tree *)obj, data, options);
Martin Koegler355885d2008-02-25 22:46:04 +0100502 case OBJ_COMMIT:
Johannes Schindelin22410542015-06-22 17:25:00 +0200503 return fsck_walk_commit((struct commit *)obj, data, options);
Martin Koegler355885d2008-02-25 22:46:04 +0100504 case OBJ_TAG:
Johannes Schindelin22410542015-06-22 17:25:00 +0200505 return fsck_walk_tag((struct tag *)obj, data, options);
Martin Koegler355885d2008-02-25 22:46:04 +0100506 default:
Jeff Kinga59cfb32019-10-18 00:56:13 -0400507 error("Unknown object type for %s",
Jeff King73390292019-10-18 00:57:37 -0400508 fsck_describe_object(options, &obj->oid));
Martin Koegler355885d2008-02-25 22:46:04 +0100509 return -1;
510 }
511}
Martin Koeglerba002f32008-02-25 22:46:08 +0100512
René Scharfe9068cfb2020-05-10 18:12:16 +0200513struct name_stack {
514 const char **names;
515 size_t nr, alloc;
516};
517
518static void name_stack_push(struct name_stack *stack, const char *name)
519{
520 ALLOC_GROW(stack->names, stack->nr + 1, stack->alloc);
521 stack->names[stack->nr++] = name;
522}
523
524static const char *name_stack_pop(struct name_stack *stack)
525{
526 return stack->nr ? stack->names[--stack->nr] : NULL;
527}
528
529static void name_stack_clear(struct name_stack *stack)
530{
531 FREE_AND_NULL(stack->names);
532 stack->nr = stack->alloc = 0;
533}
534
Martin Koeglerba002f32008-02-25 22:46:08 +0100535/*
536 * The entries in a tree are ordered in the _path_ order,
537 * which means that a directory entry is ordered by adding
538 * a slash to the end of it.
539 *
540 * So a directory called "a" is ordered _after_ a file
541 * called "a.c", because "a/" sorts after "a.c".
542 */
543#define TREE_UNORDERED (-1)
544#define TREE_HAS_DUPS (-2)
545
René Scharfe9068cfb2020-05-10 18:12:16 +0200546static int is_less_than_slash(unsigned char c)
547{
548 return '\0' < c && c < '/';
549}
550
551static int verify_ordered(unsigned mode1, const char *name1,
552 unsigned mode2, const char *name2,
553 struct name_stack *candidates)
Martin Koeglerba002f32008-02-25 22:46:08 +0100554{
555 int len1 = strlen(name1);
556 int len2 = strlen(name2);
557 int len = len1 < len2 ? len1 : len2;
558 unsigned char c1, c2;
559 int cmp;
560
561 cmp = memcmp(name1, name2, len);
562 if (cmp < 0)
563 return 0;
564 if (cmp > 0)
565 return TREE_UNORDERED;
566
567 /*
568 * Ok, the first <len> characters are the same.
569 * Now we need to order the next one, but turn
570 * a '\0' into a '/' for a directory entry.
571 */
572 c1 = name1[len];
573 c2 = name2[len];
574 if (!c1 && !c2)
575 /*
576 * git-write-tree used to write out a nonsense tree that has
577 * entries with the same name, one blob and one tree. Make
578 * sure we do not have duplicate entries.
579 */
580 return TREE_HAS_DUPS;
581 if (!c1 && S_ISDIR(mode1))
582 c1 = '/';
583 if (!c2 && S_ISDIR(mode2))
584 c2 = '/';
René Scharfe9068cfb2020-05-10 18:12:16 +0200585
586 /*
587 * There can be non-consecutive duplicates due to the implicitly
René Scharfe86715592020-05-21 11:52:04 +0200588 * added slash, e.g.:
René Scharfe9068cfb2020-05-10 18:12:16 +0200589 *
590 * foo
591 * foo.bar
592 * foo.bar.baz
593 * foo.bar/
594 * foo/
595 *
596 * Record non-directory candidates (like "foo" and "foo.bar" in
597 * the example) on a stack and check directory candidates (like
598 * foo/" and "foo.bar/") against that stack.
599 */
600 if (!c1 && is_less_than_slash(c2)) {
601 name_stack_push(candidates, name1);
602 } else if (c2 == '/' && is_less_than_slash(c1)) {
603 for (;;) {
604 const char *p;
605 const char *f_name = name_stack_pop(candidates);
606
607 if (!f_name)
608 break;
609 if (!skip_prefix(name2, f_name, &p))
René Scharfefe747042020-05-21 11:52:54 +0200610 continue;
René Scharfe9068cfb2020-05-10 18:12:16 +0200611 if (!*p)
612 return TREE_HAS_DUPS;
613 if (is_less_than_slash(*p)) {
614 name_stack_push(candidates, f_name);
615 break;
616 }
617 }
618 }
619
Martin Koeglerba002f32008-02-25 22:46:08 +0100620 return c1 < c2 ? 0 : TREE_UNORDERED;
621}
622
Jeff Kingb2f20392019-10-18 01:02:08 -0400623static int fsck_tree(const struct object_id *oid,
Jeff King23a173a2019-10-18 00:54:12 -0400624 const char *buffer, unsigned long size,
625 struct fsck_options *options)
Martin Koeglerba002f32008-02-25 22:46:08 +0100626{
David Turner8354fa32016-09-27 16:59:51 -0400627 int retval = 0;
Jeff Kingc479d142012-07-28 11:06:29 -0400628 int has_null_sha1 = 0;
Martin Koeglerba002f32008-02-25 22:46:08 +0100629 int has_full_path = 0;
630 int has_empty_name = 0;
Jeff King5d34a432012-11-27 21:27:37 -0500631 int has_dot = 0;
632 int has_dotdot = 0;
Jeff King5c17f512012-11-28 16:35:29 -0500633 int has_dotgit = 0;
Martin Koeglerba002f32008-02-25 22:46:08 +0100634 int has_zero_pad = 0;
635 int has_bad_modes = 0;
636 int has_dup_entries = 0;
637 int not_properly_sorted = 0;
638 struct tree_desc desc;
639 unsigned o_mode;
640 const char *o_name;
René Scharfe9068cfb2020-05-10 18:12:16 +0200641 struct name_stack df_dup_candidates = { NULL };
Martin Koeglerba002f32008-02-25 22:46:08 +0100642
Jeff King23a173a2019-10-18 00:54:12 -0400643 if (init_tree_desc_gently(&desc, buffer, size)) {
Jeff Kingb2f20392019-10-18 01:02:08 -0400644 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
David Turner8354fa32016-09-27 16:59:51 -0400645 return retval;
646 }
Martin Koeglerba002f32008-02-25 22:46:08 +0100647
648 o_mode = 0;
649 o_name = NULL;
Martin Koeglerba002f32008-02-25 22:46:08 +0100650
651 while (desc.size) {
Elijah Newren5ec1e722019-04-05 08:00:12 -0700652 unsigned short mode;
Johannes Schindelin288a74b2019-09-23 08:58:11 +0200653 const char *name, *backslash;
brian m. carlsonce6663a2016-04-17 23:10:40 +0000654 const struct object_id *oid;
Martin Koeglerba002f32008-02-25 22:46:08 +0100655
brian m. carlsonce6663a2016-04-17 23:10:40 +0000656 oid = tree_entry_extract(&desc, &name, &mode);
Martin Koeglerba002f32008-02-25 22:46:08 +0100657
brian m. carlsonce6663a2016-04-17 23:10:40 +0000658 has_null_sha1 |= is_null_oid(oid);
Hiroyuki Sanoeffd12e2014-03-20 08:02:04 +0900659 has_full_path |= !!strchr(name, '/');
660 has_empty_name |= !*name;
661 has_dot |= !strcmp(name, ".");
662 has_dotdot |= !strcmp(name, "..");
Jeff Kinged9c3222018-05-13 12:35:37 -0400663 has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
Martin Koeglerba002f32008-02-25 22:46:08 +0100664 has_zero_pad |= *(char *)desc.buffer == '0';
Jeff King159e7b02018-05-02 17:20:08 -0400665
Jeff Kingb7b1fca2018-05-04 20:03:35 -0400666 if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
667 if (!S_ISLNK(mode))
668 oidset_insert(&gitmodules_found, oid);
669 else
Jeff King38370252019-10-18 00:59:15 -0400670 retval += report(options,
Jeff Kingb2f20392019-10-18 01:02:08 -0400671 oid, OBJ_TREE,
Jeff Kingb7b1fca2018-05-04 20:03:35 -0400672 FSCK_MSG_GITMODULES_SYMLINK,
673 ".gitmodules is a symbolic link");
674 }
Jeff King159e7b02018-05-02 17:20:08 -0400675
Johannes Schindelin288a74b2019-09-23 08:58:11 +0200676 if ((backslash = strchr(name, '\\'))) {
677 while (backslash) {
678 backslash++;
679 has_dotgit |= is_ntfs_dotgit(backslash);
Johannes Schindelinbdfef042019-12-04 21:52:10 +0100680 if (is_ntfs_dotgitmodules(backslash)) {
681 if (!S_ISLNK(mode))
682 oidset_insert(&gitmodules_found, oid);
683 else
Junio C Hamano7034cd02019-12-09 22:17:55 -0800684 retval += report(options, oid, OBJ_TREE,
Johannes Schindelinbdfef042019-12-04 21:52:10 +0100685 FSCK_MSG_GITMODULES_SYMLINK,
686 ".gitmodules is a symbolic link");
687 }
Johannes Schindelin288a74b2019-09-23 08:58:11 +0200688 backslash = strchr(backslash, '\\');
689 }
690 }
691
David Turner8354fa32016-09-27 16:59:51 -0400692 if (update_tree_entry_gently(&desc)) {
Jeff Kingb2f20392019-10-18 01:02:08 -0400693 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
David Turner8354fa32016-09-27 16:59:51 -0400694 break;
695 }
Martin Koeglerba002f32008-02-25 22:46:08 +0100696
697 switch (mode) {
698 /*
699 * Standard modes..
700 */
701 case S_IFREG | 0755:
702 case S_IFREG | 0644:
703 case S_IFLNK:
704 case S_IFDIR:
705 case S_IFGITLINK:
706 break;
707 /*
708 * This is nonstandard, but we had a few of these
709 * early on when we honored the full set of mode
710 * bits..
711 */
712 case S_IFREG | 0664:
Johannes Schindelin22410542015-06-22 17:25:00 +0200713 if (!options->strict)
Martin Koeglerba002f32008-02-25 22:46:08 +0100714 break;
Jeff King1cf01a32017-09-21 02:25:41 -0400715 /* fallthrough */
Martin Koeglerba002f32008-02-25 22:46:08 +0100716 default:
717 has_bad_modes = 1;
718 }
719
720 if (o_name) {
René Scharfe9068cfb2020-05-10 18:12:16 +0200721 switch (verify_ordered(o_mode, o_name, mode, name,
722 &df_dup_candidates)) {
Martin Koeglerba002f32008-02-25 22:46:08 +0100723 case TREE_UNORDERED:
724 not_properly_sorted = 1;
725 break;
726 case TREE_HAS_DUPS:
727 has_dup_entries = 1;
728 break;
729 default:
730 break;
731 }
732 }
733
734 o_mode = mode;
735 o_name = name;
Martin Koeglerba002f32008-02-25 22:46:08 +0100736 }
737
René Scharfe9068cfb2020-05-10 18:12:16 +0200738 name_stack_clear(&df_dup_candidates);
739
Jeff Kingc479d142012-07-28 11:06:29 -0400740 if (has_null_sha1)
Jeff Kingb2f20392019-10-18 01:02:08 -0400741 retval += report(options, oid, OBJ_TREE, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
Martin Koeglerba002f32008-02-25 22:46:08 +0100742 if (has_full_path)
Jeff Kingb2f20392019-10-18 01:02:08 -0400743 retval += report(options, oid, OBJ_TREE, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
Martin Koeglerba002f32008-02-25 22:46:08 +0100744 if (has_empty_name)
Jeff Kingb2f20392019-10-18 01:02:08 -0400745 retval += report(options, oid, OBJ_TREE, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
Jeff King5d34a432012-11-27 21:27:37 -0500746 if (has_dot)
Jeff Kingb2f20392019-10-18 01:02:08 -0400747 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOT, "contains '.'");
Jeff King5d34a432012-11-27 21:27:37 -0500748 if (has_dotdot)
Jeff Kingb2f20392019-10-18 01:02:08 -0400749 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTDOT, "contains '..'");
Jeff King5c17f512012-11-28 16:35:29 -0500750 if (has_dotgit)
Jeff Kingb2f20392019-10-18 01:02:08 -0400751 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
Martin Koeglerba002f32008-02-25 22:46:08 +0100752 if (has_zero_pad)
Jeff Kingb2f20392019-10-18 01:02:08 -0400753 retval += report(options, oid, OBJ_TREE, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
Martin Koeglerba002f32008-02-25 22:46:08 +0100754 if (has_bad_modes)
Jeff Kingb2f20392019-10-18 01:02:08 -0400755 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
Martin Koeglerba002f32008-02-25 22:46:08 +0100756 if (has_dup_entries)
Jeff Kingb2f20392019-10-18 01:02:08 -0400757 retval += report(options, oid, OBJ_TREE, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries");
Martin Koeglerba002f32008-02-25 22:46:08 +0100758 if (not_properly_sorted)
Jeff Kingb2f20392019-10-18 01:02:08 -0400759 retval += report(options, oid, OBJ_TREE, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted");
Martin Koeglerba002f32008-02-25 22:46:08 +0100760 return retval;
761}
762
Junio C Hamano84d18c02015-06-28 11:18:31 -0700763static int verify_headers(const void *data, unsigned long size,
Jeff Kingcc579002019-10-18 01:00:50 -0400764 const struct object_id *oid, enum object_type type,
765 struct fsck_options *options)
Johannes Schindelin4d0d8972014-09-11 16:26:33 +0200766{
767 const char *buffer = (const char *)data;
768 unsigned long i;
769
770 for (i = 0; i < size; i++) {
771 switch (buffer[i]) {
772 case '\0':
Jeff Kingcc579002019-10-18 01:00:50 -0400773 return report(options, oid, type,
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200774 FSCK_MSG_NUL_IN_HEADER,
775 "unterminated header: NUL at offset %ld", i);
Johannes Schindelin4d0d8972014-09-11 16:26:33 +0200776 case '\n':
777 if (i + 1 < size && buffer[i + 1] == '\n')
778 return 0;
779 }
780 }
781
Junio C Hamano84d18c02015-06-28 11:18:31 -0700782 /*
783 * We did not find double-LF that separates the header
784 * and the body. Not having a body is not a crime but
785 * we do want to see the terminating LF for the last header
786 * line.
787 */
788 if (size && buffer[size - 1] == '\n')
789 return 0;
790
Jeff Kingcc579002019-10-18 01:00:50 -0400791 return report(options, oid, type,
Johannes Schindelinc99ba492015-06-22 17:25:09 +0200792 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
Johannes Schindelin4d0d8972014-09-11 16:26:33 +0200793}
794
Jeff King78543992019-10-18 01:00:04 -0400795static int fsck_ident(const char **ident,
796 const struct object_id *oid, enum object_type type,
797 struct fsck_options *options)
Jonathan Niederdaae1922010-04-24 11:06:08 -0500798{
Johannes Schindeline6826e32015-06-22 17:26:03 +0200799 const char *p = *ident;
Jeff Kingd4b8de02014-02-24 02:39:04 -0500800 char *end;
801
Johannes Schindeline6826e32015-06-22 17:26:03 +0200802 *ident = strchrnul(*ident, '\n');
803 if (**ident == '\n')
804 (*ident)++;
805
806 if (*p == '<')
Jeff King78543992019-10-18 01:00:04 -0400807 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 +0200808 p += strcspn(p, "<>\n");
809 if (*p == '>')
Jeff King78543992019-10-18 01:00:04 -0400810 return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
Johannes Schindeline6826e32015-06-22 17:26:03 +0200811 if (*p != '<')
Jeff King78543992019-10-18 01:00:04 -0400812 return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
Johannes Schindeline6826e32015-06-22 17:26:03 +0200813 if (p[-1] != ' ')
Jeff King78543992019-10-18 01:00:04 -0400814 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 +0200815 p++;
816 p += strcspn(p, "<>\n");
817 if (*p != '>')
Jeff King78543992019-10-18 01:00:04 -0400818 return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
Johannes Schindeline6826e32015-06-22 17:26:03 +0200819 p++;
820 if (*p != ' ')
Jeff King78543992019-10-18 01:00:04 -0400821 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 +0200822 p++;
823 if (*p == '0' && p[1] != ' ')
Jeff King78543992019-10-18 01:00:04 -0400824 return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200825 if (date_overflows(parse_timestamp(p, &end, 10)))
Jeff King78543992019-10-18 01:00:04 -0400826 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 +0200827 if ((end == p || *end != ' '))
Jeff King78543992019-10-18 01:00:04 -0400828 return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
Johannes Schindeline6826e32015-06-22 17:26:03 +0200829 p = end + 1;
830 if ((*p != '+' && *p != '-') ||
831 !isdigit(p[1]) ||
832 !isdigit(p[2]) ||
833 !isdigit(p[3]) ||
834 !isdigit(p[4]) ||
835 (p[5] != '\n'))
Jeff King78543992019-10-18 01:00:04 -0400836 return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
Johannes Schindeline6826e32015-06-22 17:26:03 +0200837 p += 6;
Jonathan Niederdaae1922010-04-24 11:06:08 -0500838 return 0;
839}
840
Jeff Kingc5b42692019-10-18 01:01:48 -0400841static int fsck_commit(const struct object_id *oid,
842 const char *buffer, unsigned long size,
843 struct fsck_options *options)
Martin Koeglerba002f32008-02-25 22:46:08 +0100844{
Jeff Kingf648ee72019-10-18 01:00:59 -0400845 struct object_id tree_oid, parent_oid;
Jeff Kingec652312019-10-18 00:49:10 -0400846 unsigned author_count;
Jonathan Niederdaae1922010-04-24 11:06:08 -0500847 int err;
Junio C Hamano6d2d7802016-04-14 10:58:22 -0700848 const char *buffer_begin = buffer;
brian m. carlsonc54f5ca2018-05-02 00:25:41 +0000849 const char *p;
Martin Koeglerba002f32008-02-25 22:46:08 +0100850
Jeff Kingc5b42692019-10-18 01:01:48 -0400851 if (verify_headers(buffer, size, oid, OBJ_COMMIT, options))
Johannes Schindelin4d0d8972014-09-11 16:26:33 +0200852 return -1;
853
Jeff Kingcf4fff52014-06-18 15:44:19 -0400854 if (!skip_prefix(buffer, "tree ", &buffer))
Jeff Kingc5b42692019-10-18 01:01:48 -0400855 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
brian m. carlsonc54f5ca2018-05-02 00:25:41 +0000856 if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
Jeff Kingc5b42692019-10-18 01:01:48 -0400857 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
Johannes Schindelinb3584762015-06-22 17:26:11 +0200858 if (err)
859 return err;
860 }
brian m. carlsonc54f5ca2018-05-02 00:25:41 +0000861 buffer = p + 1;
Jeff Kingcf4fff52014-06-18 15:44:19 -0400862 while (skip_prefix(buffer, "parent ", &buffer)) {
Jeff Kingf648ee72019-10-18 01:00:59 -0400863 if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') {
Jeff Kingc5b42692019-10-18 01:01:48 -0400864 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
Johannes Schindelinb3584762015-06-22 17:26:11 +0200865 if (err)
866 return err;
867 }
brian m. carlsonc54f5ca2018-05-02 00:25:41 +0000868 buffer = p + 1;
Martin Koeglerba002f32008-02-25 22:46:08 +0100869 }
Johannes Schindelinc9ad1472015-06-22 17:26:23 +0200870 author_count = 0;
871 while (skip_prefix(buffer, "author ", &buffer)) {
872 author_count++;
Jeff Kingc5b42692019-10-18 01:01:48 -0400873 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
Johannes Schindelinc9ad1472015-06-22 17:26:23 +0200874 if (err)
875 return err;
876 }
877 if (author_count < 1)
Jeff Kingc5b42692019-10-18 01:01:48 -0400878 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
Johannes Schindelinc9ad1472015-06-22 17:26:23 +0200879 else if (author_count > 1)
Jeff Kingc5b42692019-10-18 01:01:48 -0400880 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
Jonathan Niederdaae1922010-04-24 11:06:08 -0500881 if (err)
882 return err;
Jeff Kingcf4fff52014-06-18 15:44:19 -0400883 if (!skip_prefix(buffer, "committer ", &buffer))
Jeff Kingc5b42692019-10-18 01:01:48 -0400884 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
885 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
Jonathan Niederdaae1922010-04-24 11:06:08 -0500886 if (err)
887 return err;
Junio C Hamano6d2d7802016-04-14 10:58:22 -0700888 if (memchr(buffer_begin, '\0', size)) {
Jeff Kingc5b42692019-10-18 01:01:48 -0400889 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT,
Junio C Hamano6d2d7802016-04-14 10:58:22 -0700890 "NUL byte in the commit object body");
891 if (err)
892 return err;
893 }
Martin Koeglerba002f32008-02-25 22:46:08 +0100894 return 0;
895}
896
Jeff King103fb6d2019-10-18 01:01:26 -0400897static int fsck_tag(const struct object_id *oid, const char *buffer,
Jeff King2175a0c2019-10-18 00:51:19 -0400898 unsigned long size, struct fsck_options *options)
Johannes Schindelincec097b2014-09-11 16:26:38 +0200899{
Jeff Kingf648ee72019-10-18 01:00:59 -0400900 struct object_id tagged_oid;
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +0100901 int tagged_type;
902 return fsck_tag_standalone(oid, buffer, size, options, &tagged_oid,
903 &tagged_type);
904}
905
906int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
907 unsigned long size, struct fsck_options *options,
908 struct object_id *tagged_oid,
909 int *tagged_type)
910{
Johannes Schindelincec097b2014-09-11 16:26:38 +0200911 int ret = 0;
Jeff King23a173a2019-10-18 00:54:12 -0400912 char *eol;
Johannes Schindelincec097b2014-09-11 16:26:38 +0200913 struct strbuf sb = STRBUF_INIT;
brian m. carlsonc54f5ca2018-05-02 00:25:41 +0000914 const char *p;
Johannes Schindelincec097b2014-09-11 16:26:38 +0200915
Jeff King103fb6d2019-10-18 01:01:26 -0400916 ret = verify_headers(buffer, size, oid, OBJ_TAG, options);
René Scharfe8a272f22015-11-19 17:25:31 +0100917 if (ret)
Johannes Schindelincec097b2014-09-11 16:26:38 +0200918 goto done;
919
920 if (!skip_prefix(buffer, "object ", &buffer)) {
Jeff King103fb6d2019-10-18 01:01:26 -0400921 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
Johannes Schindelincec097b2014-09-11 16:26:38 +0200922 goto done;
923 }
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +0100924 if (parse_oid_hex(buffer, tagged_oid, &p) || *p != '\n') {
Jeff King103fb6d2019-10-18 01:01:26 -0400925 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
Johannes Schindelin7d7d5b02015-06-22 17:26:30 +0200926 if (ret)
927 goto done;
Johannes Schindelincec097b2014-09-11 16:26:38 +0200928 }
brian m. carlsonc54f5ca2018-05-02 00:25:41 +0000929 buffer = p + 1;
Johannes Schindelincec097b2014-09-11 16:26:38 +0200930
931 if (!skip_prefix(buffer, "type ", &buffer)) {
Jeff King103fb6d2019-10-18 01:01:26 -0400932 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
Johannes Schindelincec097b2014-09-11 16:26:38 +0200933 goto done;
934 }
935 eol = strchr(buffer, '\n');
936 if (!eol) {
Jeff King103fb6d2019-10-18 01:01:26 -0400937 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
Johannes Schindelincec097b2014-09-11 16:26:38 +0200938 goto done;
939 }
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +0100940 *tagged_type = type_from_string_gently(buffer, eol - buffer, 1);
941 if (*tagged_type < 0)
Jeff King103fb6d2019-10-18 01:01:26 -0400942 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
Johannes Schindelincec097b2014-09-11 16:26:38 +0200943 if (ret)
944 goto done;
945 buffer = eol + 1;
946
947 if (!skip_prefix(buffer, "tag ", &buffer)) {
Jeff King103fb6d2019-10-18 01:01:26 -0400948 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
Johannes Schindelincec097b2014-09-11 16:26:38 +0200949 goto done;
950 }
951 eol = strchr(buffer, '\n');
952 if (!eol) {
Jeff King103fb6d2019-10-18 01:01:26 -0400953 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
Johannes Schindelincec097b2014-09-11 16:26:38 +0200954 goto done;
955 }
956 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
Johannes Schindelinf27d05b2015-06-22 17:26:54 +0200957 if (check_refname_format(sb.buf, 0)) {
Jeff King103fb6d2019-10-18 01:01:26 -0400958 ret = report(options, oid, OBJ_TAG,
Jeff King38370252019-10-18 00:59:15 -0400959 FSCK_MSG_BAD_TAG_NAME,
960 "invalid 'tag' name: %.*s",
961 (int)(eol - buffer), buffer);
Johannes Schindelinf27d05b2015-06-22 17:26:54 +0200962 if (ret)
963 goto done;
964 }
Johannes Schindelincec097b2014-09-11 16:26:38 +0200965 buffer = eol + 1;
966
Johannes Schindelinf27d05b2015-06-22 17:26:54 +0200967 if (!skip_prefix(buffer, "tagger ", &buffer)) {
Johannes Schindelincec097b2014-09-11 16:26:38 +0200968 /* early tags do not contain 'tagger' lines; warn only */
Jeff King103fb6d2019-10-18 01:01:26 -0400969 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
Johannes Schindelinf27d05b2015-06-22 17:26:54 +0200970 if (ret)
971 goto done;
972 }
Johannes Schindelincec097b2014-09-11 16:26:38 +0200973 else
Jeff King103fb6d2019-10-18 01:01:26 -0400974 ret = fsck_ident(&buffer, oid, OBJ_TAG, options);
Ævar Arnfjörð Bjarmason9a1a3a42021-01-05 20:42:49 +0100975 if (!*buffer)
976 goto done;
Johannes Schindelincec097b2014-09-11 16:26:38 +0200977
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +0100978 if (!starts_with(buffer, "\n")) {
979 /*
980 * The verify_headers() check will allow
981 * e.g. "[...]tagger <tagger>\nsome
982 * garbage\n\nmessage" to pass, thinking "some
983 * garbage" could be a custom header. E.g. "mktag"
984 * doesn't want any unknown headers.
985 */
986 ret = report(options, oid, OBJ_TAG, FSCK_MSG_EXTRA_HEADER_ENTRY, "invalid format - extra header(s) after 'tagger'");
987 if (ret)
988 goto done;
989 }
990
Johannes Schindelincec097b2014-09-11 16:26:38 +0200991done:
992 strbuf_release(&sb);
Johannes Schindelincec097b2014-09-11 16:26:38 +0200993 return ret;
994}
995
Jonathan Niedera2b26ff2020-04-18 20:52:34 -0700996/*
997 * Like builtin/submodule--helper.c's starts_with_dot_slash, but without
998 * relying on the platform-dependent is_dir_sep helper.
999 *
1000 * This is for use in checking whether a submodule URL is interpreted as
1001 * relative to the current directory on any platform, since \ is a
1002 * directory separator on Windows but not on other platforms.
1003 */
1004static int starts_with_dot_slash(const char *str)
1005{
1006 return str[0] == '.' && (str[1] == '/' || str[1] == '\\');
1007}
1008
1009/*
1010 * Like starts_with_dot_slash, this is a variant of submodule--helper's
1011 * helper of the same name with the twist that it accepts backslash as a
1012 * directory separator even on non-Windows platforms.
1013 */
1014static int starts_with_dot_dot_slash(const char *str)
1015{
1016 return str[0] == '.' && starts_with_dot_slash(str + 1);
1017}
1018
1019static int submodule_url_is_relative(const char *url)
1020{
1021 return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url);
1022}
1023
1024/*
Jonathan Niederc44088e2020-04-18 20:54:13 -07001025 * Count directory components that a relative submodule URL should chop
1026 * from the remote_url it is to be resolved against.
1027 *
1028 * In other words, this counts "../" components at the start of a
1029 * submodule URL.
1030 *
1031 * Returns the number of directory components to chop and writes a
1032 * pointer to the next character of url after all leading "./" and
1033 * "../" components to out.
1034 */
1035static int count_leading_dotdots(const char *url, const char **out)
1036{
1037 int result = 0;
1038 while (1) {
1039 if (starts_with_dot_dot_slash(url)) {
1040 result++;
1041 url += strlen("../");
1042 continue;
1043 }
1044 if (starts_with_dot_slash(url)) {
1045 url += strlen("./");
1046 continue;
1047 }
1048 *out = url;
1049 return result;
1050 }
1051}
1052/*
Jonathan Niedera2b26ff2020-04-18 20:52:34 -07001053 * Check whether a transport is implemented by git-remote-curl.
1054 *
1055 * If it is, returns 1 and writes the URL that would be passed to
1056 * git-remote-curl to the "out" parameter.
1057 *
1058 * Otherwise, returns 0 and leaves "out" untouched.
1059 *
1060 * Examples:
1061 * http::https://example.com/repo.git -> 1, https://example.com/repo.git
1062 * https://example.com/repo.git -> 1, https://example.com/repo.git
1063 * git://example.com/repo.git -> 0
1064 *
1065 * This is for use in checking for previously exploitable bugs that
1066 * required a submodule URL to be passed to git-remote-curl.
1067 */
1068static int url_to_curl_url(const char *url, const char **out)
1069{
1070 /*
1071 * We don't need to check for case-aliases, "http.exe", and so
1072 * on because in the default configuration, is_transport_allowed
1073 * prevents URLs with those schemes from being cloned
1074 * automatically.
1075 */
1076 if (skip_prefix(url, "http::", out) ||
1077 skip_prefix(url, "https::", out) ||
1078 skip_prefix(url, "ftp::", out) ||
1079 skip_prefix(url, "ftps::", out))
1080 return 1;
1081 if (starts_with(url, "http://") ||
1082 starts_with(url, "https://") ||
1083 starts_with(url, "ftp://") ||
1084 starts_with(url, "ftps://")) {
1085 *out = url;
1086 return 1;
1087 }
1088 return 0;
1089}
1090
Jeff King07259e72020-03-11 18:48:24 -04001091static int check_submodule_url(const char *url)
1092{
Jonathan Niedera2b26ff2020-04-18 20:52:34 -07001093 const char *curl_url;
Jeff King07259e72020-03-11 18:48:24 -04001094
1095 if (looks_like_command_line_option(url))
1096 return -1;
1097
Jeff King6aed5672021-01-07 04:44:17 -05001098 if (submodule_url_is_relative(url) || starts_with(url, "git://")) {
Jonathan Niederc44088e2020-04-18 20:54:13 -07001099 char *decoded;
1100 const char *next;
1101 int has_nl;
1102
Jonathan Niedera2b26ff2020-04-18 20:52:34 -07001103 /*
1104 * This could be appended to an http URL and url-decoded;
1105 * check for malicious characters.
1106 */
Jonathan Niederc44088e2020-04-18 20:54:13 -07001107 decoded = url_decode(url);
1108 has_nl = !!strchr(decoded, '\n');
1109
Jonathan Niedera2b26ff2020-04-18 20:52:34 -07001110 free(decoded);
1111 if (has_nl)
1112 return -1;
Jonathan Niederc44088e2020-04-18 20:54:13 -07001113
1114 /*
1115 * URLs which escape their root via "../" can overwrite
1116 * the host field and previous components, resolving to
Jonathan Nieder1a3609e2020-04-18 20:57:22 -07001117 * URLs like https::example.com/submodule.git and
1118 * https:///example.com/submodule.git that were
Jonathan Niederc44088e2020-04-18 20:54:13 -07001119 * susceptible to CVE-2020-11008.
1120 */
1121 if (count_leading_dotdots(url, &next) > 0 &&
Jonathan Nieder1a3609e2020-04-18 20:57:22 -07001122 (*next == ':' || *next == '/'))
Jonathan Niederc44088e2020-04-18 20:54:13 -07001123 return -1;
Jonathan Niedera2b26ff2020-04-18 20:52:34 -07001124 }
1125
1126 else if (url_to_curl_url(url, &curl_url)) {
1127 struct credential c = CREDENTIAL_INIT;
Jonathan Nieder1a3609e2020-04-18 20:57:22 -07001128 int ret = 0;
1129 if (credential_from_url_gently(&c, curl_url, 1) ||
1130 !*c.host)
1131 ret = -1;
Jonathan Niedera2b26ff2020-04-18 20:52:34 -07001132 credential_clear(&c);
1133 return ret;
1134 }
1135
1136 return 0;
Jeff King07259e72020-03-11 18:48:24 -04001137}
1138
Jeff Kinged8b10f2018-05-02 17:25:27 -04001139struct fsck_gitmodules_data {
Jeff King6da40b22019-10-18 00:59:29 -04001140 const struct object_id *oid;
Jeff Kinged8b10f2018-05-02 17:25:27 -04001141 struct fsck_options *options;
1142 int ret;
1143};
1144
1145static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
1146{
1147 struct fsck_gitmodules_data *data = vdata;
1148 const char *subsection, *key;
Jeff Kingf5914f42020-04-10 15:44:28 -04001149 size_t subsection_len;
Jeff Kinged8b10f2018-05-02 17:25:27 -04001150 char *name;
1151
1152 if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
1153 !subsection)
1154 return 0;
1155
1156 name = xmemdupz(subsection, subsection_len);
1157 if (check_submodule_name(name) < 0)
Jeff King38370252019-10-18 00:59:15 -04001158 data->ret |= report(data->options,
Jeff King6da40b22019-10-18 00:59:29 -04001159 data->oid, OBJ_BLOB,
Jeff Kinged8b10f2018-05-02 17:25:27 -04001160 FSCK_MSG_GITMODULES_NAME,
1161 "disallowed submodule name: %s",
1162 name);
Jeff Kinga1241332018-09-24 04:37:17 -04001163 if (!strcmp(key, "url") && value &&
Jeff King07259e72020-03-11 18:48:24 -04001164 check_submodule_url(value) < 0)
Jeff King38370252019-10-18 00:59:15 -04001165 data->ret |= report(data->options,
Jeff King6da40b22019-10-18 00:59:29 -04001166 data->oid, OBJ_BLOB,
Jeff Kinga1241332018-09-24 04:37:17 -04001167 FSCK_MSG_GITMODULES_URL,
1168 "disallowed submodule url: %s",
1169 value);
Jeff King1a7fd1f2018-09-24 04:42:19 -04001170 if (!strcmp(key, "path") && value &&
1171 looks_like_command_line_option(value))
Jeff King38370252019-10-18 00:59:15 -04001172 data->ret |= report(data->options,
Jeff King6da40b22019-10-18 00:59:29 -04001173 data->oid, OBJ_BLOB,
Jeff King1a7fd1f2018-09-24 04:42:19 -04001174 FSCK_MSG_GITMODULES_PATH,
1175 "disallowed submodule path: %s",
1176 value);
Jonathan Niederbb922552019-12-05 01:30:43 -08001177 if (!strcmp(key, "update") && value &&
1178 parse_submodule_update_type(value) == SM_UPDATE_COMMAND)
Junio C Hamano7034cd02019-12-09 22:17:55 -08001179 data->ret |= report(data->options, data->oid, OBJ_BLOB,
Jonathan Niederbb922552019-12-05 01:30:43 -08001180 FSCK_MSG_GITMODULES_UPDATE,
1181 "disallowed submodule update setting: %s",
1182 value);
Jeff Kinged8b10f2018-05-02 17:25:27 -04001183 free(name);
1184
1185 return 0;
1186}
1187
Jeff King6da40b22019-10-18 00:59:29 -04001188static int fsck_blob(const struct object_id *oid, const char *buf,
Jeff King7ac4f3a2018-05-02 15:44:51 -04001189 unsigned long size, struct fsck_options *options)
1190{
Jeff Kinged8b10f2018-05-02 17:25:27 -04001191 struct fsck_gitmodules_data data;
Jeff Kingde6bd9e2018-06-28 18:06:04 -04001192 struct config_options config_opts = { 0 };
Jeff Kinged8b10f2018-05-02 17:25:27 -04001193
Jeff King6da40b22019-10-18 00:59:29 -04001194 if (!oidset_contains(&gitmodules_found, oid))
Jeff Kinged8b10f2018-05-02 17:25:27 -04001195 return 0;
Jeff King6da40b22019-10-18 00:59:29 -04001196 oidset_insert(&gitmodules_done, oid);
Jeff Kinged8b10f2018-05-02 17:25:27 -04001197
Jeff King6da40b22019-10-18 00:59:29 -04001198 if (object_on_skiplist(options, oid))
Ramsay Jonesfb162872018-06-27 19:39:53 +01001199 return 0;
1200
Jeff Kinged8b10f2018-05-02 17:25:27 -04001201 if (!buf) {
1202 /*
1203 * A missing buffer here is a sign that the caller found the
1204 * blob too gigantic to load into memory. Let's just consider
1205 * that an error.
1206 */
Jeff King6da40b22019-10-18 00:59:29 -04001207 return report(options, oid, OBJ_BLOB,
Jeff King0d687642018-07-13 15:39:53 -04001208 FSCK_MSG_GITMODULES_LARGE,
Jeff Kinged8b10f2018-05-02 17:25:27 -04001209 ".gitmodules too large to parse");
1210 }
1211
Jeff King6da40b22019-10-18 00:59:29 -04001212 data.oid = oid;
Jeff Kinged8b10f2018-05-02 17:25:27 -04001213 data.options = options;
1214 data.ret = 0;
Jeff Kingde6bd9e2018-06-28 18:06:04 -04001215 config_opts.error_action = CONFIG_ERROR_SILENT;
Jeff Kinged8b10f2018-05-02 17:25:27 -04001216 if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
Jeff Kingde6bd9e2018-06-28 18:06:04 -04001217 ".gitmodules", buf, size, &data, &config_opts))
Jeff King6da40b22019-10-18 00:59:29 -04001218 data.ret |= report(options, oid, OBJ_BLOB,
Jeff Kinged8b10f2018-05-02 17:25:27 -04001219 FSCK_MSG_GITMODULES_PARSE,
1220 "could not parse gitmodules blob");
1221
1222 return data.ret;
Jeff King7ac4f3a2018-05-02 15:44:51 -04001223}
1224
Johannes Schindelin90a398b2014-09-10 15:52:51 +02001225int fsck_object(struct object *obj, void *data, unsigned long size,
Johannes Schindelin22410542015-06-22 17:25:00 +02001226 struct fsck_options *options)
Martin Koeglerba002f32008-02-25 22:46:08 +01001227{
1228 if (!obj)
Jeff King38370252019-10-18 00:59:15 -04001229 return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
Martin Koeglerba002f32008-02-25 22:46:08 +01001230
1231 if (obj->type == OBJ_BLOB)
Jeff King6da40b22019-10-18 00:59:29 -04001232 return fsck_blob(&obj->oid, data, size, options);
Martin Koeglerba002f32008-02-25 22:46:08 +01001233 if (obj->type == OBJ_TREE)
Jeff Kingb2f20392019-10-18 01:02:08 -04001234 return fsck_tree(&obj->oid, data, size, options);
Martin Koeglerba002f32008-02-25 22:46:08 +01001235 if (obj->type == OBJ_COMMIT)
Jeff Kingc5b42692019-10-18 01:01:48 -04001236 return fsck_commit(&obj->oid, data, size, options);
Martin Koeglerba002f32008-02-25 22:46:08 +01001237 if (obj->type == OBJ_TAG)
Jeff King103fb6d2019-10-18 01:01:26 -04001238 return fsck_tag(&obj->oid, data, size, options);
Martin Koeglerba002f32008-02-25 22:46:08 +01001239
Jeff King38370252019-10-18 00:59:15 -04001240 return report(options, &obj->oid, obj->type,
1241 FSCK_MSG_UNKNOWN_TYPE,
1242 "unknown type '%d' (internal fsck error)",
1243 obj->type);
Martin Koeglerba002f32008-02-25 22:46:08 +01001244}
Martin Koeglerd6ffc8d2008-02-25 22:46:09 +01001245
Johannes Schindelin1cd772c2016-07-17 12:59:57 +02001246int fsck_error_function(struct fsck_options *o,
Jeff King5afc4b12019-10-18 00:58:40 -04001247 const struct object_id *oid,
1248 enum object_type object_type,
Ævar Arnfjörð Bjarmason1b32b592021-03-28 15:15:40 +02001249 enum fsck_msg_type msg_type, const char *message)
Martin Koeglerd6ffc8d2008-02-25 22:46:09 +01001250{
Johannes Schindelin0282f4d2015-06-22 17:25:25 +02001251 if (msg_type == FSCK_WARN) {
Jeff King5afc4b12019-10-18 00:58:40 -04001252 warning("object %s: %s", fsck_describe_object(o, oid), message);
Johannes Schindelin0282f4d2015-06-22 17:25:25 +02001253 return 0;
1254 }
Jeff King5afc4b12019-10-18 00:58:40 -04001255 error("object %s: %s", fsck_describe_object(o, oid), message);
Martin Koeglerd6ffc8d2008-02-25 22:46:09 +01001256 return 1;
1257}
Jeff King159e7b02018-05-02 17:20:08 -04001258
Jonathan Tan5476e1e2021-02-22 11:20:09 -08001259void register_found_gitmodules(const struct object_id *oid)
1260{
1261 oidset_insert(&gitmodules_found, oid);
1262}
1263
Jeff King159e7b02018-05-02 17:20:08 -04001264int fsck_finish(struct fsck_options *options)
1265{
1266 int ret = 0;
1267 struct oidset_iter iter;
1268 const struct object_id *oid;
1269
1270 oidset_iter_init(&gitmodules_found, &iter);
1271 while ((oid = oidset_iter_next(&iter))) {
Jeff King159e7b02018-05-02 17:20:08 -04001272 enum object_type type;
1273 unsigned long size;
1274 char *buf;
1275
1276 if (oidset_contains(&gitmodules_done, oid))
1277 continue;
1278
Junio C Hamano7913f532018-05-29 17:09:58 +09001279 buf = read_object_file(oid, &type, &size);
Jeff King159e7b02018-05-02 17:20:08 -04001280 if (!buf) {
Jeff Kingb8b00f12019-10-18 00:59:54 -04001281 if (is_promisor_object(oid))
Jeff King27387442018-05-14 12:22:48 -04001282 continue;
Jeff King38370252019-10-18 00:59:15 -04001283 ret |= report(options,
Jeff Kingb8b00f12019-10-18 00:59:54 -04001284 oid, OBJ_BLOB,
Jeff King159e7b02018-05-02 17:20:08 -04001285 FSCK_MSG_GITMODULES_MISSING,
1286 "unable to read .gitmodules blob");
1287 continue;
1288 }
1289
1290 if (type == OBJ_BLOB)
Jeff Kingb8b00f12019-10-18 00:59:54 -04001291 ret |= fsck_blob(oid, buf, size, options);
Jeff King159e7b02018-05-02 17:20:08 -04001292 else
Jeff King38370252019-10-18 00:59:15 -04001293 ret |= report(options,
Jeff Kingb8b00f12019-10-18 00:59:54 -04001294 oid, type,
Jeff King159e7b02018-05-02 17:20:08 -04001295 FSCK_MSG_GITMODULES_BLOB,
1296 "non-blob found at .gitmodules");
1297 free(buf);
1298 }
1299
1300
1301 oidset_clear(&gitmodules_found);
1302 oidset_clear(&gitmodules_done);
1303 return ret;
1304}
Ævar Arnfjörð Bjarmason1f3299f2021-01-05 20:42:47 +01001305
Ævar Arnfjörð Bjarmasonfb79f5b2021-03-17 19:20:36 +01001306int git_fsck_config(const char *var, const char *value, void *cb)
Ævar Arnfjörð Bjarmason1f3299f2021-01-05 20:42:47 +01001307{
Ævar Arnfjörð Bjarmasonfb79f5b2021-03-17 19:20:36 +01001308 struct fsck_options *options = cb;
Ævar Arnfjörð Bjarmason1f3299f2021-01-05 20:42:47 +01001309 if (strcmp(var, "fsck.skiplist") == 0) {
1310 const char *path;
1311 struct strbuf sb = STRBUF_INIT;
1312
1313 if (git_config_pathname(&path, var, value))
1314 return 1;
1315 strbuf_addf(&sb, "skiplist=%s", path);
1316 free((char *)path);
1317 fsck_set_msg_types(options, sb.buf);
1318 strbuf_release(&sb);
1319 return 0;
1320 }
1321
1322 if (skip_prefix(var, "fsck.", &var)) {
1323 fsck_set_msg_type(options, var, value);
1324 return 0;
1325 }
1326
1327 return git_default_config(var, value, cb);
1328}