]> git.ipfire.org Git - thirdparty/git.git/blame - fsck.c
remote: drop auto-strlen behavior of make_branch() and make_rewrite()
[thirdparty/git.git] / fsck.c
CommitLineData
355885d5 1#include "cache.h"
cbd53a21 2#include "object-store.h"
109cd76d 3#include "repository.h"
355885d5
MK
4#include "object.h"
5#include "blob.h"
6#include "tree.h"
7#include "tree-walk.h"
8#include "commit.h"
9#include "tag.h"
10#include "fsck.h"
cec097be 11#include "refs.h"
a18fcc9f 12#include "utf8.h"
7b35efd7 13#include "decorate.h"
159e7b08 14#include "oidset.h"
27387444 15#include "packfile.h"
ed8b10f6
JK
16#include "submodule-config.h"
17#include "config.h"
3ac68a93 18#include "help.h"
159e7b08
JK
19
20static struct oidset gitmodules_found = OIDSET_INIT;
21static struct oidset gitmodules_done = OIDSET_INIT;
355885d5 22
f50c4407 23#define FSCK_FATAL -1
f27d05b1 24#define FSCK_INFO -2
f50c4407 25
c99ba492 26#define FOREACH_MSG_ID(FUNC) \
f50c4407
JS
27 /* fatal errors */ \
28 FUNC(NUL_IN_HEADER, FATAL) \
29 FUNC(UNTERMINATED_HEADER, FATAL) \
c99ba492
JS
30 /* errors */ \
31 FUNC(BAD_DATE, ERROR) \
32 FUNC(BAD_DATE_OVERFLOW, ERROR) \
33 FUNC(BAD_EMAIL, ERROR) \
34 FUNC(BAD_NAME, ERROR) \
35 FUNC(BAD_OBJECT_SHA1, ERROR) \
36 FUNC(BAD_PARENT_SHA1, ERROR) \
37 FUNC(BAD_TAG_OBJECT, ERROR) \
38 FUNC(BAD_TIMEZONE, ERROR) \
39 FUNC(BAD_TREE, ERROR) \
40 FUNC(BAD_TREE_SHA1, ERROR) \
41 FUNC(BAD_TYPE, ERROR) \
42 FUNC(DUPLICATE_ENTRIES, ERROR) \
43 FUNC(MISSING_AUTHOR, ERROR) \
44 FUNC(MISSING_COMMITTER, ERROR) \
45 FUNC(MISSING_EMAIL, ERROR) \
c99ba492
JS
46 FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
47 FUNC(MISSING_OBJECT, ERROR) \
c99ba492
JS
48 FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \
49 FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \
50 FUNC(MISSING_TAG, ERROR) \
51 FUNC(MISSING_TAG_ENTRY, ERROR) \
c99ba492 52 FUNC(MISSING_TREE, ERROR) \
159e7b08 53 FUNC(MISSING_TREE_OBJECT, ERROR) \
c99ba492
JS
54 FUNC(MISSING_TYPE, ERROR) \
55 FUNC(MISSING_TYPE_ENTRY, ERROR) \
c9ad147f 56 FUNC(MULTIPLE_AUTHORS, ERROR) \
c99ba492
JS
57 FUNC(TREE_NOT_SORTED, ERROR) \
58 FUNC(UNKNOWN_TYPE, ERROR) \
c99ba492 59 FUNC(ZERO_PADDED_DATE, ERROR) \
159e7b08
JK
60 FUNC(GITMODULES_MISSING, ERROR) \
61 FUNC(GITMODULES_BLOB, ERROR) \
0d68764d 62 FUNC(GITMODULES_LARGE, ERROR) \
ed8b10f6 63 FUNC(GITMODULES_NAME, ERROR) \
b7b1fca1 64 FUNC(GITMODULES_SYMLINK, ERROR) \
a124133e 65 FUNC(GITMODULES_URL, ERROR) \
1a7fd1fb 66 FUNC(GITMODULES_PATH, ERROR) \
bb92255e 67 FUNC(GITMODULES_UPDATE, ERROR) \
c99ba492
JS
68 /* warnings */ \
69 FUNC(BAD_FILEMODE, WARN) \
c99ba492
JS
70 FUNC(EMPTY_NAME, WARN) \
71 FUNC(FULL_PATHNAME, WARN) \
72 FUNC(HAS_DOT, WARN) \
73 FUNC(HAS_DOTDOT, WARN) \
74 FUNC(HAS_DOTGIT, WARN) \
c99ba492 75 FUNC(NULL_SHA1, WARN) \
f27d05b1 76 FUNC(ZERO_PADDED_FILEMODE, WARN) \
6d2d780f 77 FUNC(NUL_IN_COMMIT, WARN) \
f27d05b1 78 /* infos (reported as warnings, but ignored by default) */ \
64eb14d3 79 FUNC(GITMODULES_PARSE, INFO) \
f27d05b1
JS
80 FUNC(BAD_TAG_NAME, INFO) \
81 FUNC(MISSING_TAGGER_ENTRY, INFO)
c99ba492
JS
82
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
f417eed8 90#define STR(x) #x
a4a9cc19 91#define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
c99ba492 92static struct {
f417eed8
JS
93 const char *id_string;
94 const char *downcased;
a4a9cc19 95 const char *camelcased;
c99ba492
JS
96 int msg_type;
97} msg_id_info[FSCK_MSG_MAX + 1] = {
98 FOREACH_MSG_ID(MSG_ID)
a4a9cc19 99 { NULL, NULL, NULL, -1 }
c99ba492
JS
100};
101#undef MSG_ID
102
a46baac6 103static void prepare_msg_ids(void)
f417eed8
JS
104{
105 int i;
106
a46baac6
NTND
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';
a4a9cc19
NTND
123
124 p = msg_id_info[i].id_string;
125 q = xmalloc(len);
126 msg_id_info[i].camelcased = q;
127 while (*p) {
128 if (*p == '_') {
129 p++;
130 if (*p)
131 *q++ = *p++;
132 } else {
133 *q++ = tolower(*p++);
134 }
f417eed8 135 }
a4a9cc19 136 *q = '\0';
f417eed8 137 }
a46baac6
NTND
138}
139
140static int parse_msg_id(const char *text)
141{
142 int i;
143
144 prepare_msg_ids();
f417eed8
JS
145
146 for (i = 0; i < FSCK_MSG_MAX; i++)
147 if (!strcmp(text, msg_id_info[i].downcased))
148 return i;
149
150 return -1;
151}
152
3ac68a93
NTND
153void list_config_fsck_msg_ids(struct string_list *list, const char *prefix)
154{
155 int i;
156
157 prepare_msg_ids();
158
3ac68a93 159 for (i = 0; i < FSCK_MSG_MAX; i++)
a4a9cc19 160 list_config_item(list, prefix, msg_id_info[i].camelcased);
3ac68a93
NTND
161}
162
c99ba492
JS
163static int fsck_msg_type(enum fsck_msg_id msg_id,
164 struct fsck_options *options)
165{
166 int msg_type;
167
0282f4dc
JS
168 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
169
170 if (options->msg_type)
171 msg_type = options->msg_type[msg_id];
172 else {
173 msg_type = msg_id_info[msg_id].msg_type;
174 if (options->strict && msg_type == FSCK_WARN)
175 msg_type = FSCK_ERROR;
176 }
c99ba492
JS
177
178 return msg_type;
179}
180
0282f4dc
JS
181static 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;
efaba7cc
JS
187 else if (!strcmp(str, "ignore"))
188 return FSCK_IGNORE;
0282f4dc
JS
189 else
190 die("Unknown fsck message type: '%s'", str);
191}
192
5d477a33
JS
193int 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
0282f4dc
JS
201void 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
f50c4407
JS
210 if (type != FSCK_ERROR && msg_id_info[id].msg_type == FSCK_FATAL)
211 die("Cannot demote %s to %s", msg_id, msg_type);
212
0282f4dc
JS
213 if (!options->msg_type) {
214 int i;
b32fa95f
JK
215 int *msg_type;
216 ALLOC_ARRAY(msg_type, FSCK_MSG_MAX);
0282f4dc
JS
217 for (i = 0; i < FSCK_MSG_MAX; i++)
218 msg_type[i] = fsck_msg_type(i, options);
219 options->msg_type = msg_type;
220 }
221
222 options->msg_type[id] = type;
223}
224
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
cd94c6f9
JS
246 if (!strcmp(buf, "skiplist")) {
247 if (equal == len)
248 die("skiplist requires a path");
24eb33eb 249 oidset_parse_file(&options->skiplist, buf + equal + 1);
cd94c6f9
JS
250 buf += len + 1;
251 continue;
252 }
253
0282f4dc
JS
254 if (equal == len)
255 die("Missing '=': '%s'", buf);
355885d5 256
0282f4dc
JS
257 fsck_set_msg_type(options, buf, buf + equal + 1);
258 buf += len + 1;
259 }
260 free(to_free);
261}
262
71ab8fa8
JS
263static 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
f5979376
JK
281static int object_on_skiplist(struct fsck_options *opts,
282 const struct object_id *oid)
fb162877 283{
f5979376 284 return opts && oid && oidset_contains(&opts->skiplist, oid);
fb162877
RJ
285}
286
38370253
JK
287__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, ...)
c99ba492
JS
291{
292 va_list ap;
293 struct strbuf sb = STRBUF_INIT;
294 int msg_type = fsck_msg_type(id, options), result;
295
efaba7cc
JS
296 if (msg_type == FSCK_IGNORE)
297 return 0;
298
38370253 299 if (object_on_skiplist(options, oid))
cd94c6f9
JS
300 return 0;
301
f50c4407
JS
302 if (msg_type == FSCK_FATAL)
303 msg_type = FSCK_ERROR;
f27d05b1
JS
304 else if (msg_type == FSCK_INFO)
305 msg_type = FSCK_WARN;
f50c4407 306
71ab8fa8
JS
307 append_msg_id(&sb, msg_id_info[id].id_string);
308
c99ba492
JS
309 va_start(ap, fmt);
310 strbuf_vaddf(&sb, fmt, ap);
38370253 311 result = options->error_func(options, oid, object_type,
5afc4b1d 312 msg_type, sb.buf);
c99ba492
JS
313 strbuf_release(&sb);
314 va_end(ap);
315
316 return result;
317}
318
a59cfb32
JK
319void fsck_enable_object_names(struct fsck_options *options)
320{
321 if (!options->object_names)
73390290 322 options->object_names = kh_init_oid_map();
a59cfb32
JK
323}
324
73390290
JK
325const char *fsck_get_object_name(struct fsck_options *options,
326 const struct object_id *oid)
7b35efd7 327{
73390290 328 khiter_t pos;
7b35efd7
JS
329 if (!options->object_names)
330 return NULL;
73390290
JK
331 pos = kh_get_oid_map(options->object_names, *oid);
332 if (pos >= kh_end(options->object_names))
333 return NULL;
334 return kh_value(options->object_names, pos);
7b35efd7
JS
335}
336
73390290
JK
337void fsck_put_object_name(struct fsck_options *options,
338 const struct object_id *oid,
a59cfb32 339 const char *fmt, ...)
7b35efd7
JS
340{
341 va_list ap;
342 struct strbuf buf = STRBUF_INIT;
73390290
JK
343 khiter_t pos;
344 int hashret;
7b35efd7
JS
345
346 if (!options->object_names)
347 return;
73390290
JK
348
349 pos = kh_put_oid_map(options->object_names, *oid, &hashret);
350 if (!hashret)
7b35efd7
JS
351 return;
352 va_start(ap, fmt);
353 strbuf_vaddf(&buf, fmt, ap);
73390290 354 kh_value(options->object_names, pos) = strbuf_detach(&buf, NULL);
7b35efd7
JS
355 va_end(ap);
356}
357
a59cfb32 358const char *fsck_describe_object(struct fsck_options *options,
73390290 359 const struct object_id *oid)
90cf590f 360{
a59cfb32
JK
361 static struct strbuf bufs[] = {
362 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
363 };
364 static int b = 0;
365 struct strbuf *buf;
73390290 366 const char *name = fsck_get_object_name(options, oid);
a59cfb32
JK
367
368 buf = bufs + b;
369 b = (b + 1) % ARRAY_SIZE(bufs);
370 strbuf_reset(buf);
73390290 371 strbuf_addstr(buf, oid_to_hex(oid));
a59cfb32
JK
372 if (name)
373 strbuf_addf(buf, " (%s)", name);
90cf590f 374
a59cfb32 375 return buf->buf;
90cf590f
JS
376}
377
22410549 378static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
355885d5
MK
379{
380 struct tree_desc desc;
381 struct name_entry entry;
382 int res = 0;
7b35efd7 383 const char *name;
355885d5
MK
384
385 if (parse_tree(tree))
386 return -1;
387
73390290 388 name = fsck_get_object_name(options, &tree->object.oid);
8354fa3d
DT
389 if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
390 return -1;
391 while (tree_entry_gently(&desc, &entry)) {
7b35efd7 392 struct object *obj;
355885d5
MK
393 int result;
394
395 if (S_ISGITLINK(entry.mode))
396 continue;
7b35efd7
JS
397
398 if (S_ISDIR(entry.mode)) {
ea82b2a0 399 obj = (struct object *)lookup_tree(the_repository, &entry.oid);
2720f6db 400 if (name && obj)
73390290 401 fsck_put_object_name(options, &entry.oid, "%s%s/",
a59cfb32 402 name, entry.path);
7b35efd7
JS
403 result = options->walk(obj, OBJ_TREE, data, options);
404 }
405 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
ea82b2a0 406 obj = (struct object *)lookup_blob(the_repository, &entry.oid);
2720f6db 407 if (name && obj)
73390290 408 fsck_put_object_name(options, &entry.oid, "%s%s",
a59cfb32 409 name, entry.path);
7b35efd7
JS
410 result = options->walk(obj, OBJ_BLOB, data, options);
411 }
355885d5 412 else {
82247e9b 413 result = error("in tree %s: entry %s has bad mode %.6o",
73390290 414 fsck_describe_object(options, &tree->object.oid),
a59cfb32 415 entry.path, entry.mode);
355885d5
MK
416 }
417 if (result < 0)
418 return result;
419 if (!res)
420 res = result;
421 }
422 return res;
423}
424
22410549 425static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
355885d5 426{
7b35efd7 427 int counter = 0, generation = 0, name_prefix_len = 0;
355885d5
MK
428 struct commit_list *parents;
429 int res;
430 int result;
7b35efd7 431 const char *name;
355885d5
MK
432
433 if (parse_commit(commit))
434 return -1;
435
73390290 436 name = fsck_get_object_name(options, &commit->object.oid);
7b35efd7 437 if (name)
73390290 438 fsck_put_object_name(options, get_commit_tree_oid(commit),
a59cfb32 439 "%s:", name);
7b35efd7 440
2e27bd77
DS
441 result = options->walk((struct object *)get_commit_tree(commit),
442 OBJ_TREE, data, options);
355885d5
MK
443 if (result < 0)
444 return result;
445 res = result;
446
447 parents = commit->parents;
7b35efd7
JS
448 if (name && parents) {
449 int len = strlen(name), power;
450
451 if (len && name[len - 1] == '^') {
452 generation = 1;
453 name_prefix_len = len - 1;
454 }
455 else { /* parse ~<generation> suffix */
456 for (generation = 0, power = 1;
457 len && isdigit(name[len - 1]);
458 power *= 10)
459 generation += power * (name[--len] - '0');
460 if (power > 1 && len && name[len - 1] == '~')
461 name_prefix_len = len - 1;
462 }
463 }
464
355885d5 465 while (parents) {
7b35efd7 466 if (name) {
73390290 467 struct object_id *oid = &parents->item->object.oid;
7b35efd7 468
b84c7838 469 if (counter++)
73390290 470 fsck_put_object_name(options, oid, "%s^%d",
a59cfb32 471 name, counter);
7b35efd7 472 else if (generation > 0)
73390290 473 fsck_put_object_name(options, oid, "%.*s~%d",
a59cfb32
JK
474 name_prefix_len, name,
475 generation + 1);
7b35efd7 476 else
73390290 477 fsck_put_object_name(options, oid, "%s^", name);
7b35efd7 478 }
22410549 479 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
355885d5
MK
480 if (result < 0)
481 return result;
482 if (!res)
483 res = result;
484 parents = parents->next;
485 }
486 return res;
487}
488
22410549 489static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
355885d5 490{
73390290 491 const char *name = fsck_get_object_name(options, &tag->object.oid);
7b35efd7 492
355885d5
MK
493 if (parse_tag(tag))
494 return -1;
7b35efd7 495 if (name)
73390290 496 fsck_put_object_name(options, &tag->tagged->oid, "%s", name);
22410549 497 return options->walk(tag->tagged, OBJ_ANY, data, options);
355885d5
MK
498}
499
22410549 500int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
355885d5
MK
501{
502 if (!obj)
503 return -1;
a2b22854
JK
504
505 if (obj->type == OBJ_NONE)
109cd76d 506 parse_object(the_repository, &obj->oid);
a2b22854 507
355885d5
MK
508 switch (obj->type) {
509 case OBJ_BLOB:
510 return 0;
511 case OBJ_TREE:
22410549 512 return fsck_walk_tree((struct tree *)obj, data, options);
355885d5 513 case OBJ_COMMIT:
22410549 514 return fsck_walk_commit((struct commit *)obj, data, options);
355885d5 515 case OBJ_TAG:
22410549 516 return fsck_walk_tag((struct tag *)obj, data, options);
355885d5 517 default:
a59cfb32 518 error("Unknown object type for %s",
73390290 519 fsck_describe_object(options, &obj->oid));
355885d5
MK
520 return -1;
521 }
522}
ba002f3b
MK
523
524/*
525 * The entries in a tree are ordered in the _path_ order,
526 * which means that a directory entry is ordered by adding
527 * a slash to the end of it.
528 *
529 * So a directory called "a" is ordered _after_ a file
530 * called "a.c", because "a/" sorts after "a.c".
531 */
532#define TREE_UNORDERED (-1)
533#define TREE_HAS_DUPS (-2)
534
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
b2f2039c 570static int fsck_tree(const struct object_id *oid,
23a173a7
JK
571 const char *buffer, unsigned long size,
572 struct fsck_options *options)
ba002f3b 573{
8354fa3d 574 int retval = 0;
c479d14a 575 int has_null_sha1 = 0;
ba002f3b
MK
576 int has_full_path = 0;
577 int has_empty_name = 0;
5d34a435
JK
578 int has_dot = 0;
579 int has_dotdot = 0;
5c17f512 580 int has_dotgit = 0;
ba002f3b
MK
581 int has_zero_pad = 0;
582 int has_bad_modes = 0;
583 int has_dup_entries = 0;
584 int not_properly_sorted = 0;
585 struct tree_desc desc;
586 unsigned o_mode;
587 const char *o_name;
ba002f3b 588
23a173a7 589 if (init_tree_desc_gently(&desc, buffer, size)) {
b2f2039c 590 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
8354fa3d
DT
591 return retval;
592 }
ba002f3b
MK
593
594 o_mode = 0;
595 o_name = NULL;
ba002f3b
MK
596
597 while (desc.size) {
5ec1e728 598 unsigned short mode;
288a74bc 599 const char *name, *backslash;
ce6663a9 600 const struct object_id *oid;
ba002f3b 601
ce6663a9 602 oid = tree_entry_extract(&desc, &name, &mode);
ba002f3b 603
ce6663a9 604 has_null_sha1 |= is_null_oid(oid);
effd12ec
HS
605 has_full_path |= !!strchr(name, '/');
606 has_empty_name |= !*name;
607 has_dot |= !strcmp(name, ".");
608 has_dotdot |= !strcmp(name, "..");
ed9c3220 609 has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
ba002f3b 610 has_zero_pad |= *(char *)desc.buffer == '0';
159e7b08 611
b7b1fca1
JK
612 if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
613 if (!S_ISLNK(mode))
614 oidset_insert(&gitmodules_found, oid);
615 else
38370253 616 retval += report(options,
b2f2039c 617 oid, OBJ_TREE,
b7b1fca1
JK
618 FSCK_MSG_GITMODULES_SYMLINK,
619 ".gitmodules is a symbolic link");
620 }
159e7b08 621
288a74bc
JS
622 if ((backslash = strchr(name, '\\'))) {
623 while (backslash) {
624 backslash++;
625 has_dotgit |= is_ntfs_dotgit(backslash);
bdfef049
JS
626 if (is_ntfs_dotgitmodules(backslash)) {
627 if (!S_ISLNK(mode))
628 oidset_insert(&gitmodules_found, oid);
629 else
7034cd09 630 retval += report(options, oid, OBJ_TREE,
bdfef049
JS
631 FSCK_MSG_GITMODULES_SYMLINK,
632 ".gitmodules is a symbolic link");
633 }
288a74bc
JS
634 backslash = strchr(backslash, '\\');
635 }
636 }
637
8354fa3d 638 if (update_tree_entry_gently(&desc)) {
b2f2039c 639 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
8354fa3d
DT
640 break;
641 }
ba002f3b
MK
642
643 switch (mode) {
644 /*
645 * Standard modes..
646 */
647 case S_IFREG | 0755:
648 case S_IFREG | 0644:
649 case S_IFLNK:
650 case S_IFDIR:
651 case S_IFGITLINK:
652 break;
653 /*
654 * This is nonstandard, but we had a few of these
655 * early on when we honored the full set of mode
656 * bits..
657 */
658 case S_IFREG | 0664:
22410549 659 if (!options->strict)
ba002f3b 660 break;
1cf01a34 661 /* fallthrough */
ba002f3b
MK
662 default:
663 has_bad_modes = 1;
664 }
665
666 if (o_name) {
667 switch (verify_ordered(o_mode, o_name, mode, name)) {
668 case TREE_UNORDERED:
669 not_properly_sorted = 1;
670 break;
671 case TREE_HAS_DUPS:
672 has_dup_entries = 1;
673 break;
674 default:
675 break;
676 }
677 }
678
679 o_mode = mode;
680 o_name = name;
ba002f3b
MK
681 }
682
c479d14a 683 if (has_null_sha1)
b2f2039c 684 retval += report(options, oid, OBJ_TREE, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
ba002f3b 685 if (has_full_path)
b2f2039c 686 retval += report(options, oid, OBJ_TREE, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
ba002f3b 687 if (has_empty_name)
b2f2039c 688 retval += report(options, oid, OBJ_TREE, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
5d34a435 689 if (has_dot)
b2f2039c 690 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOT, "contains '.'");
5d34a435 691 if (has_dotdot)
b2f2039c 692 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTDOT, "contains '..'");
5c17f512 693 if (has_dotgit)
b2f2039c 694 retval += report(options, oid, OBJ_TREE, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
ba002f3b 695 if (has_zero_pad)
b2f2039c 696 retval += report(options, oid, OBJ_TREE, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
ba002f3b 697 if (has_bad_modes)
b2f2039c 698 retval += report(options, oid, OBJ_TREE, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
ba002f3b 699 if (has_dup_entries)
b2f2039c 700 retval += report(options, oid, OBJ_TREE, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries");
ba002f3b 701 if (not_properly_sorted)
b2f2039c 702 retval += report(options, oid, OBJ_TREE, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted");
ba002f3b
MK
703 return retval;
704}
705
84d18c0b 706static int verify_headers(const void *data, unsigned long size,
cc579000
JK
707 const struct object_id *oid, enum object_type type,
708 struct fsck_options *options)
4d0d8975
JS
709{
710 const char *buffer = (const char *)data;
711 unsigned long i;
712
713 for (i = 0; i < size; i++) {
714 switch (buffer[i]) {
715 case '\0':
cc579000 716 return report(options, oid, type,
c99ba492
JS
717 FSCK_MSG_NUL_IN_HEADER,
718 "unterminated header: NUL at offset %ld", i);
4d0d8975
JS
719 case '\n':
720 if (i + 1 < size && buffer[i + 1] == '\n')
721 return 0;
722 }
723 }
724
84d18c0b
JH
725 /*
726 * We did not find double-LF that separates the header
727 * and the body. Not having a body is not a crime but
728 * we do want to see the terminating LF for the last header
729 * line.
730 */
731 if (size && buffer[size - 1] == '\n')
732 return 0;
733
cc579000 734 return report(options, oid, type,
c99ba492 735 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
4d0d8975
JS
736}
737
78543993
JK
738static int fsck_ident(const char **ident,
739 const struct object_id *oid, enum object_type type,
740 struct fsck_options *options)
daae1922 741{
e6826e33 742 const char *p = *ident;
d4b8de04
JK
743 char *end;
744
e6826e33
JS
745 *ident = strchrnul(*ident, '\n');
746 if (**ident == '\n')
747 (*ident)++;
748
749 if (*p == '<')
78543993 750 return report(options, oid, type, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
751 p += strcspn(p, "<>\n");
752 if (*p == '>')
78543993 753 return report(options, oid, type, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
e6826e33 754 if (*p != '<')
78543993 755 return report(options, oid, type, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
e6826e33 756 if (p[-1] != ' ')
78543993 757 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
758 p++;
759 p += strcspn(p, "<>\n");
760 if (*p != '>')
78543993 761 return report(options, oid, type, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
e6826e33
JS
762 p++;
763 if (*p != ' ')
78543993 764 return report(options, oid, type, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
e6826e33
JS
765 p++;
766 if (*p == '0' && p[1] != ' ')
78543993 767 return report(options, oid, type, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
1aeb7e75 768 if (date_overflows(parse_timestamp(p, &end, 10)))
78543993 769 return report(options, oid, type, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
e6826e33 770 if ((end == p || *end != ' '))
78543993 771 return report(options, oid, type, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
e6826e33
JS
772 p = end + 1;
773 if ((*p != '+' && *p != '-') ||
774 !isdigit(p[1]) ||
775 !isdigit(p[2]) ||
776 !isdigit(p[3]) ||
777 !isdigit(p[4]) ||
778 (p[5] != '\n'))
78543993 779 return report(options, oid, type, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
e6826e33 780 p += 6;
daae1922
JN
781 return 0;
782}
783
c5b4269b
JK
784static int fsck_commit(const struct object_id *oid,
785 const char *buffer, unsigned long size,
786 struct fsck_options *options)
ba002f3b 787{
f648ee70 788 struct object_id tree_oid, parent_oid;
ec652315 789 unsigned author_count;
daae1922 790 int err;
6d2d780f 791 const char *buffer_begin = buffer;
c54f5ca9 792 const char *p;
ba002f3b 793
c5b4269b 794 if (verify_headers(buffer, size, oid, OBJ_COMMIT, options))
4d0d8975
JS
795 return -1;
796
cf4fff57 797 if (!skip_prefix(buffer, "tree ", &buffer))
c5b4269b 798 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
c54f5ca9 799 if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
c5b4269b 800 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
b3584761
JS
801 if (err)
802 return err;
803 }
c54f5ca9 804 buffer = p + 1;
cf4fff57 805 while (skip_prefix(buffer, "parent ", &buffer)) {
f648ee70 806 if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') {
c5b4269b 807 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
b3584761
JS
808 if (err)
809 return err;
810 }
c54f5ca9 811 buffer = p + 1;
ba002f3b 812 }
c9ad147f
JS
813 author_count = 0;
814 while (skip_prefix(buffer, "author ", &buffer)) {
815 author_count++;
c5b4269b 816 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
c9ad147f
JS
817 if (err)
818 return err;
ba002f3b 819 }
c9ad147f 820 if (author_count < 1)
c5b4269b 821 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
c9ad147f 822 else if (author_count > 1)
c5b4269b 823 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
daae1922
JN
824 if (err)
825 return err;
cf4fff57 826 if (!skip_prefix(buffer, "committer ", &buffer))
c5b4269b
JK
827 return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
828 err = fsck_ident(&buffer, oid, OBJ_COMMIT, options);
daae1922
JN
829 if (err)
830 return err;
6d2d780f 831 if (memchr(buffer_begin, '\0', size)) {
c5b4269b 832 err = report(options, oid, OBJ_COMMIT, FSCK_MSG_NUL_IN_COMMIT,
6d2d780f
JH
833 "NUL byte in the commit object body");
834 if (err)
835 return err;
836 }
ba002f3b
MK
837 return 0;
838}
839
103fb6d4 840static int fsck_tag(const struct object_id *oid, const char *buffer,
2175a0c6 841 unsigned long size, struct fsck_options *options)
cec097be 842{
f648ee70 843 struct object_id tagged_oid;
cec097be 844 int ret = 0;
23a173a7 845 char *eol;
cec097be 846 struct strbuf sb = STRBUF_INIT;
c54f5ca9 847 const char *p;
cec097be 848
103fb6d4 849 ret = verify_headers(buffer, size, oid, OBJ_TAG, options);
8a272f29 850 if (ret)
cec097be
JS
851 goto done;
852
853 if (!skip_prefix(buffer, "object ", &buffer)) {
103fb6d4 854 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
cec097be
JS
855 goto done;
856 }
f648ee70 857 if (parse_oid_hex(buffer, &tagged_oid, &p) || *p != '\n') {
103fb6d4 858 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
7d7d5b05
JS
859 if (ret)
860 goto done;
cec097be 861 }
c54f5ca9 862 buffer = p + 1;
cec097be
JS
863
864 if (!skip_prefix(buffer, "type ", &buffer)) {
103fb6d4 865 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
cec097be
JS
866 goto done;
867 }
868 eol = strchr(buffer, '\n');
869 if (!eol) {
103fb6d4 870 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
cec097be
JS
871 goto done;
872 }
873 if (type_from_string_gently(buffer, eol - buffer, 1) < 0)
103fb6d4 874 ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
cec097be
JS
875 if (ret)
876 goto done;
877 buffer = eol + 1;
878
879 if (!skip_prefix(buffer, "tag ", &buffer)) {
103fb6d4 880 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
cec097be
JS
881 goto done;
882 }
883 eol = strchr(buffer, '\n');
884 if (!eol) {
103fb6d4 885 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
cec097be
JS
886 goto done;
887 }
888 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
f27d05b1 889 if (check_refname_format(sb.buf, 0)) {
103fb6d4 890 ret = report(options, oid, OBJ_TAG,
38370253
JK
891 FSCK_MSG_BAD_TAG_NAME,
892 "invalid 'tag' name: %.*s",
893 (int)(eol - buffer), buffer);
f27d05b1
JS
894 if (ret)
895 goto done;
896 }
cec097be
JS
897 buffer = eol + 1;
898
f27d05b1 899 if (!skip_prefix(buffer, "tagger ", &buffer)) {
cec097be 900 /* early tags do not contain 'tagger' lines; warn only */
103fb6d4 901 ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
f27d05b1
JS
902 if (ret)
903 goto done;
904 }
cec097be 905 else
103fb6d4 906 ret = fsck_ident(&buffer, oid, OBJ_TAG, options);
cec097be
JS
907
908done:
909 strbuf_release(&sb);
cec097be
JS
910 return ret;
911}
912
ed8b10f6 913struct fsck_gitmodules_data {
6da40b22 914 const struct object_id *oid;
ed8b10f6
JK
915 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)
38370253 932 data->ret |= report(data->options,
6da40b22 933 data->oid, OBJ_BLOB,
ed8b10f6
JK
934 FSCK_MSG_GITMODULES_NAME,
935 "disallowed submodule name: %s",
936 name);
a124133e
JK
937 if (!strcmp(key, "url") && value &&
938 looks_like_command_line_option(value))
38370253 939 data->ret |= report(data->options,
6da40b22 940 data->oid, OBJ_BLOB,
a124133e
JK
941 FSCK_MSG_GITMODULES_URL,
942 "disallowed submodule url: %s",
943 value);
1a7fd1fb
JK
944 if (!strcmp(key, "path") && value &&
945 looks_like_command_line_option(value))
38370253 946 data->ret |= report(data->options,
6da40b22 947 data->oid, OBJ_BLOB,
1a7fd1fb
JK
948 FSCK_MSG_GITMODULES_PATH,
949 "disallowed submodule path: %s",
950 value);
bb92255e
JN
951 if (!strcmp(key, "update") && value &&
952 parse_submodule_update_type(value) == SM_UPDATE_COMMAND)
7034cd09 953 data->ret |= report(data->options, data->oid, OBJ_BLOB,
bb92255e
JN
954 FSCK_MSG_GITMODULES_UPDATE,
955 "disallowed submodule update setting: %s",
956 value);
ed8b10f6
JK
957 free(name);
958
959 return 0;
960}
961
6da40b22 962static int fsck_blob(const struct object_id *oid, const char *buf,
7ac4f3a0
JK
963 unsigned long size, struct fsck_options *options)
964{
ed8b10f6 965 struct fsck_gitmodules_data data;
de6bd9e3 966 struct config_options config_opts = { 0 };
ed8b10f6 967
6da40b22 968 if (!oidset_contains(&gitmodules_found, oid))
ed8b10f6 969 return 0;
6da40b22 970 oidset_insert(&gitmodules_done, oid);
ed8b10f6 971
6da40b22 972 if (object_on_skiplist(options, oid))
fb162877
RJ
973 return 0;
974
ed8b10f6
JK
975 if (!buf) {
976 /*
977 * A missing buffer here is a sign that the caller found the
978 * blob too gigantic to load into memory. Let's just consider
979 * that an error.
980 */
6da40b22 981 return report(options, oid, OBJ_BLOB,
0d68764d 982 FSCK_MSG_GITMODULES_LARGE,
ed8b10f6
JK
983 ".gitmodules too large to parse");
984 }
985
6da40b22 986 data.oid = oid;
ed8b10f6
JK
987 data.options = options;
988 data.ret = 0;
de6bd9e3 989 config_opts.error_action = CONFIG_ERROR_SILENT;
ed8b10f6 990 if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
de6bd9e3 991 ".gitmodules", buf, size, &data, &config_opts))
6da40b22 992 data.ret |= report(options, oid, OBJ_BLOB,
ed8b10f6
JK
993 FSCK_MSG_GITMODULES_PARSE,
994 "could not parse gitmodules blob");
995
996 return data.ret;
7ac4f3a0
JK
997}
998
90a398bb 999int fsck_object(struct object *obj, void *data, unsigned long size,
22410549 1000 struct fsck_options *options)
ba002f3b
MK
1001{
1002 if (!obj)
38370253 1003 return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
ba002f3b
MK
1004
1005 if (obj->type == OBJ_BLOB)
6da40b22 1006 return fsck_blob(&obj->oid, data, size, options);
ba002f3b 1007 if (obj->type == OBJ_TREE)
b2f2039c 1008 return fsck_tree(&obj->oid, data, size, options);
ba002f3b 1009 if (obj->type == OBJ_COMMIT)
c5b4269b 1010 return fsck_commit(&obj->oid, data, size, options);
ba002f3b 1011 if (obj->type == OBJ_TAG)
103fb6d4 1012 return fsck_tag(&obj->oid, data, size, options);
ba002f3b 1013
38370253
JK
1014 return report(options, &obj->oid, obj->type,
1015 FSCK_MSG_UNKNOWN_TYPE,
1016 "unknown type '%d' (internal fsck error)",
1017 obj->type);
ba002f3b 1018}
d6ffc8d7 1019
1cd772cc 1020int fsck_error_function(struct fsck_options *o,
5afc4b1d
JK
1021 const struct object_id *oid,
1022 enum object_type object_type,
1023 int msg_type, const char *message)
d6ffc8d7 1024{
0282f4dc 1025 if (msg_type == FSCK_WARN) {
5afc4b1d 1026 warning("object %s: %s", fsck_describe_object(o, oid), message);
0282f4dc
JS
1027 return 0;
1028 }
5afc4b1d 1029 error("object %s: %s", fsck_describe_object(o, oid), message);
d6ffc8d7
MK
1030 return 1;
1031}
159e7b08
JK
1032
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))) {
159e7b08
JK
1041 enum object_type type;
1042 unsigned long size;
1043 char *buf;
1044
1045 if (oidset_contains(&gitmodules_done, oid))
1046 continue;
1047
7913f53b 1048 buf = read_object_file(oid, &type, &size);
159e7b08 1049 if (!buf) {
b8b00f16 1050 if (is_promisor_object(oid))
27387444 1051 continue;
38370253 1052 ret |= report(options,
b8b00f16 1053 oid, OBJ_BLOB,
159e7b08
JK
1054 FSCK_MSG_GITMODULES_MISSING,
1055 "unable to read .gitmodules blob");
1056 continue;
1057 }
1058
1059 if (type == OBJ_BLOB)
b8b00f16 1060 ret |= fsck_blob(oid, buf, size, options);
159e7b08 1061 else
38370253 1062 ret |= report(options,
b8b00f16 1063 oid, type,
159e7b08
JK
1064 FSCK_MSG_GITMODULES_BLOB,
1065 "non-blob found at .gitmodules");
1066 free(buf);
1067 }
1068
1069
1070 oidset_clear(&gitmodules_found);
1071 oidset_clear(&gitmodules_done);
1072 return ret;
1073}