]> git.ipfire.org Git - thirdparty/git.git/blame - fsck.c
fsck: optionally ignore specific fsck issues completely
[thirdparty/git.git] / fsck.c
CommitLineData
355885d5
MK
1#include "cache.h"
2#include "object.h"
3#include "blob.h"
4#include "tree.h"
5#include "tree-walk.h"
6#include "commit.h"
7#include "tag.h"
8#include "fsck.h"
cec097be 9#include "refs.h"
a18fcc9f 10#include "utf8.h"
355885d5 11
f50c4407
JS
12#define FSCK_FATAL -1
13
c99ba492 14#define FOREACH_MSG_ID(FUNC) \
f50c4407
JS
15 /* fatal errors */ \
16 FUNC(NUL_IN_HEADER, FATAL) \
17 FUNC(UNTERMINATED_HEADER, FATAL) \
c99ba492
JS
18 /* errors */ \
19 FUNC(BAD_DATE, ERROR) \
20 FUNC(BAD_DATE_OVERFLOW, ERROR) \
21 FUNC(BAD_EMAIL, ERROR) \
22 FUNC(BAD_NAME, ERROR) \
23 FUNC(BAD_OBJECT_SHA1, ERROR) \
24 FUNC(BAD_PARENT_SHA1, ERROR) \
25 FUNC(BAD_TAG_OBJECT, ERROR) \
26 FUNC(BAD_TIMEZONE, ERROR) \
27 FUNC(BAD_TREE, ERROR) \
28 FUNC(BAD_TREE_SHA1, ERROR) \
29 FUNC(BAD_TYPE, ERROR) \
30 FUNC(DUPLICATE_ENTRIES, ERROR) \
31 FUNC(MISSING_AUTHOR, ERROR) \
32 FUNC(MISSING_COMMITTER, ERROR) \
33 FUNC(MISSING_EMAIL, ERROR) \
34 FUNC(MISSING_GRAFT, ERROR) \
35 FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
36 FUNC(MISSING_OBJECT, ERROR) \
37 FUNC(MISSING_PARENT, ERROR) \
38 FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \
39 FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \
40 FUNC(MISSING_TAG, ERROR) \
41 FUNC(MISSING_TAG_ENTRY, ERROR) \
42 FUNC(MISSING_TAG_OBJECT, ERROR) \
43 FUNC(MISSING_TREE, ERROR) \
44 FUNC(MISSING_TYPE, ERROR) \
45 FUNC(MISSING_TYPE_ENTRY, ERROR) \
c9ad147f 46 FUNC(MULTIPLE_AUTHORS, ERROR) \
c99ba492
JS
47 FUNC(TAG_OBJECT_NOT_TAG, ERROR) \
48 FUNC(TREE_NOT_SORTED, ERROR) \
49 FUNC(UNKNOWN_TYPE, ERROR) \
c99ba492
JS
50 FUNC(ZERO_PADDED_DATE, ERROR) \
51 /* warnings */ \
52 FUNC(BAD_FILEMODE, WARN) \
53 FUNC(BAD_TAG_NAME, WARN) \
54 FUNC(EMPTY_NAME, WARN) \
55 FUNC(FULL_PATHNAME, WARN) \
56 FUNC(HAS_DOT, WARN) \
57 FUNC(HAS_DOTDOT, WARN) \
58 FUNC(HAS_DOTGIT, WARN) \
59 FUNC(MISSING_TAGGER_ENTRY, WARN) \
60 FUNC(NULL_SHA1, WARN) \
61 FUNC(ZERO_PADDED_FILEMODE, WARN)
62
63#define MSG_ID(id, msg_type) FSCK_MSG_##id,
64enum fsck_msg_id {
65 FOREACH_MSG_ID(MSG_ID)
66 FSCK_MSG_MAX
67};
68#undef MSG_ID
69
f417eed8
JS
70#define STR(x) #x
71#define MSG_ID(id, msg_type) { STR(id), NULL, FSCK_##msg_type },
c99ba492 72static struct {
f417eed8
JS
73 const char *id_string;
74 const char *downcased;
c99ba492
JS
75 int msg_type;
76} msg_id_info[FSCK_MSG_MAX + 1] = {
77 FOREACH_MSG_ID(MSG_ID)
f417eed8 78 { NULL, NULL, -1 }
c99ba492
JS
79};
80#undef MSG_ID
81
f417eed8
JS
82static int parse_msg_id(const char *text)
83{
84 int i;
85
86 if (!msg_id_info[0].downcased) {
87 /* convert id_string to lower case, without underscores. */
88 for (i = 0; i < FSCK_MSG_MAX; i++) {
89 const char *p = msg_id_info[i].id_string;
90 int len = strlen(p);
91 char *q = xmalloc(len);
92
93 msg_id_info[i].downcased = q;
94 while (*p)
95 if (*p == '_')
96 p++;
97 else
98 *(q)++ = tolower(*(p)++);
99 *q = '\0';
100 }
101 }
102
103 for (i = 0; i < FSCK_MSG_MAX; i++)
104 if (!strcmp(text, msg_id_info[i].downcased))
105 return i;
106
107 return -1;
108}
109
c99ba492
JS
110static int fsck_msg_type(enum fsck_msg_id msg_id,
111 struct fsck_options *options)
112{
113 int msg_type;
114
0282f4dc
JS
115 assert(msg_id >= 0 && msg_id < FSCK_MSG_MAX);
116
117 if (options->msg_type)
118 msg_type = options->msg_type[msg_id];
119 else {
120 msg_type = msg_id_info[msg_id].msg_type;
121 if (options->strict && msg_type == FSCK_WARN)
122 msg_type = FSCK_ERROR;
123 }
c99ba492
JS
124
125 return msg_type;
126}
127
0282f4dc
JS
128static int parse_msg_type(const char *str)
129{
130 if (!strcmp(str, "error"))
131 return FSCK_ERROR;
132 else if (!strcmp(str, "warn"))
133 return FSCK_WARN;
efaba7cc
JS
134 else if (!strcmp(str, "ignore"))
135 return FSCK_IGNORE;
0282f4dc
JS
136 else
137 die("Unknown fsck message type: '%s'", str);
138}
139
5d477a33
JS
140int is_valid_msg_type(const char *msg_id, const char *msg_type)
141{
142 if (parse_msg_id(msg_id) < 0)
143 return 0;
144 parse_msg_type(msg_type);
145 return 1;
146}
147
0282f4dc
JS
148void fsck_set_msg_type(struct fsck_options *options,
149 const char *msg_id, const char *msg_type)
150{
151 int id = parse_msg_id(msg_id), type;
152
153 if (id < 0)
154 die("Unhandled message id: %s", msg_id);
155 type = parse_msg_type(msg_type);
156
f50c4407
JS
157 if (type != FSCK_ERROR && msg_id_info[id].msg_type == FSCK_FATAL)
158 die("Cannot demote %s to %s", msg_id, msg_type);
159
0282f4dc
JS
160 if (!options->msg_type) {
161 int i;
162 int *msg_type = xmalloc(sizeof(int) * FSCK_MSG_MAX);
163 for (i = 0; i < FSCK_MSG_MAX; i++)
164 msg_type[i] = fsck_msg_type(i, options);
165 options->msg_type = msg_type;
166 }
167
168 options->msg_type[id] = type;
169}
170
171void fsck_set_msg_types(struct fsck_options *options, const char *values)
172{
173 char *buf = xstrdup(values), *to_free = buf;
174 int done = 0;
175
176 while (!done) {
177 int len = strcspn(buf, " ,|"), equal;
178
179 done = !buf[len];
180 if (!len) {
181 buf++;
182 continue;
183 }
184 buf[len] = '\0';
185
186 for (equal = 0;
187 equal < len && buf[equal] != '=' && buf[equal] != ':';
188 equal++)
189 buf[equal] = tolower(buf[equal]);
190 buf[equal] = '\0';
191
192 if (equal == len)
193 die("Missing '=': '%s'", buf);
194
195 fsck_set_msg_type(options, buf, buf + equal + 1);
196 buf += len + 1;
197 }
198 free(to_free);
199}
200
71ab8fa8
JS
201static void append_msg_id(struct strbuf *sb, const char *msg_id)
202{
203 for (;;) {
204 char c = *(msg_id)++;
205
206 if (!c)
207 break;
208 if (c != '_')
209 strbuf_addch(sb, tolower(c));
210 else {
211 assert(*msg_id);
212 strbuf_addch(sb, *(msg_id)++);
213 }
214 }
215
216 strbuf_addstr(sb, ": ");
217}
218
c99ba492
JS
219__attribute__((format (printf, 4, 5)))
220static int report(struct fsck_options *options, struct object *object,
221 enum fsck_msg_id id, const char *fmt, ...)
222{
223 va_list ap;
224 struct strbuf sb = STRBUF_INIT;
225 int msg_type = fsck_msg_type(id, options), result;
226
efaba7cc
JS
227 if (msg_type == FSCK_IGNORE)
228 return 0;
229
f50c4407
JS
230 if (msg_type == FSCK_FATAL)
231 msg_type = FSCK_ERROR;
232
71ab8fa8
JS
233 append_msg_id(&sb, msg_id_info[id].id_string);
234
c99ba492
JS
235 va_start(ap, fmt);
236 strbuf_vaddf(&sb, fmt, ap);
237 result = options->error_func(object, msg_type, sb.buf);
238 strbuf_release(&sb);
239 va_end(ap);
240
241 return result;
242}
243
22410549 244static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options)
355885d5
MK
245{
246 struct tree_desc desc;
247 struct name_entry entry;
248 int res = 0;
249
250 if (parse_tree(tree))
251 return -1;
252
253 init_tree_desc(&desc, tree->buffer, tree->size);
254 while (tree_entry(&desc, &entry)) {
255 int result;
256
257 if (S_ISGITLINK(entry.mode))
258 continue;
259 if (S_ISDIR(entry.mode))
22410549 260 result = options->walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data, options);
355885d5 261 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode))
22410549 262 result = options->walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data, options);
355885d5 263 else {
82247e9b 264 result = error("in tree %s: entry %s has bad mode %.6o",
355885d5
MK
265 sha1_to_hex(tree->object.sha1), entry.path, entry.mode);
266 }
267 if (result < 0)
268 return result;
269 if (!res)
270 res = result;
271 }
272 return res;
273}
274
22410549 275static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options)
355885d5
MK
276{
277 struct commit_list *parents;
278 int res;
279 int result;
280
281 if (parse_commit(commit))
282 return -1;
283
22410549 284 result = options->walk((struct object *)commit->tree, OBJ_TREE, data, options);
355885d5
MK
285 if (result < 0)
286 return result;
287 res = result;
288
289 parents = commit->parents;
290 while (parents) {
22410549 291 result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options);
355885d5
MK
292 if (result < 0)
293 return result;
294 if (!res)
295 res = result;
296 parents = parents->next;
297 }
298 return res;
299}
300
22410549 301static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options)
355885d5
MK
302{
303 if (parse_tag(tag))
304 return -1;
22410549 305 return options->walk(tag->tagged, OBJ_ANY, data, options);
355885d5
MK
306}
307
22410549 308int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
355885d5
MK
309{
310 if (!obj)
311 return -1;
312 switch (obj->type) {
313 case OBJ_BLOB:
314 return 0;
315 case OBJ_TREE:
22410549 316 return fsck_walk_tree((struct tree *)obj, data, options);
355885d5 317 case OBJ_COMMIT:
22410549 318 return fsck_walk_commit((struct commit *)obj, data, options);
355885d5 319 case OBJ_TAG:
22410549 320 return fsck_walk_tag((struct tag *)obj, data, options);
355885d5
MK
321 default:
322 error("Unknown object type for %s", sha1_to_hex(obj->sha1));
323 return -1;
324 }
325}
ba002f3b
MK
326
327/*
328 * The entries in a tree are ordered in the _path_ order,
329 * which means that a directory entry is ordered by adding
330 * a slash to the end of it.
331 *
332 * So a directory called "a" is ordered _after_ a file
333 * called "a.c", because "a/" sorts after "a.c".
334 */
335#define TREE_UNORDERED (-1)
336#define TREE_HAS_DUPS (-2)
337
338static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
339{
340 int len1 = strlen(name1);
341 int len2 = strlen(name2);
342 int len = len1 < len2 ? len1 : len2;
343 unsigned char c1, c2;
344 int cmp;
345
346 cmp = memcmp(name1, name2, len);
347 if (cmp < 0)
348 return 0;
349 if (cmp > 0)
350 return TREE_UNORDERED;
351
352 /*
353 * Ok, the first <len> characters are the same.
354 * Now we need to order the next one, but turn
355 * a '\0' into a '/' for a directory entry.
356 */
357 c1 = name1[len];
358 c2 = name2[len];
359 if (!c1 && !c2)
360 /*
361 * git-write-tree used to write out a nonsense tree that has
362 * entries with the same name, one blob and one tree. Make
363 * sure we do not have duplicate entries.
364 */
365 return TREE_HAS_DUPS;
366 if (!c1 && S_ISDIR(mode1))
367 c1 = '/';
368 if (!c2 && S_ISDIR(mode2))
369 c2 = '/';
370 return c1 < c2 ? 0 : TREE_UNORDERED;
371}
372
22410549 373static int fsck_tree(struct tree *item, struct fsck_options *options)
ba002f3b
MK
374{
375 int retval;
c479d14a 376 int has_null_sha1 = 0;
ba002f3b
MK
377 int has_full_path = 0;
378 int has_empty_name = 0;
5d34a435
JK
379 int has_dot = 0;
380 int has_dotdot = 0;
5c17f512 381 int has_dotgit = 0;
ba002f3b
MK
382 int has_zero_pad = 0;
383 int has_bad_modes = 0;
384 int has_dup_entries = 0;
385 int not_properly_sorted = 0;
386 struct tree_desc desc;
387 unsigned o_mode;
388 const char *o_name;
ba002f3b
MK
389
390 init_tree_desc(&desc, item->buffer, item->size);
391
392 o_mode = 0;
393 o_name = NULL;
ba002f3b
MK
394
395 while (desc.size) {
396 unsigned mode;
397 const char *name;
c479d14a 398 const unsigned char *sha1;
ba002f3b 399
c479d14a 400 sha1 = tree_entry_extract(&desc, &name, &mode);
ba002f3b 401
effd12ec
HS
402 has_null_sha1 |= is_null_sha1(sha1);
403 has_full_path |= !!strchr(name, '/');
404 has_empty_name |= !*name;
405 has_dot |= !strcmp(name, ".");
406 has_dotdot |= !strcmp(name, "..");
5e519fb8
JH
407 has_dotgit |= (!strcmp(name, ".git") ||
408 is_hfs_dotgit(name) ||
409 is_ntfs_dotgit(name));
ba002f3b
MK
410 has_zero_pad |= *(char *)desc.buffer == '0';
411 update_tree_entry(&desc);
412
413 switch (mode) {
414 /*
415 * Standard modes..
416 */
417 case S_IFREG | 0755:
418 case S_IFREG | 0644:
419 case S_IFLNK:
420 case S_IFDIR:
421 case S_IFGITLINK:
422 break;
423 /*
424 * This is nonstandard, but we had a few of these
425 * early on when we honored the full set of mode
426 * bits..
427 */
428 case S_IFREG | 0664:
22410549 429 if (!options->strict)
ba002f3b
MK
430 break;
431 default:
432 has_bad_modes = 1;
433 }
434
435 if (o_name) {
436 switch (verify_ordered(o_mode, o_name, mode, name)) {
437 case TREE_UNORDERED:
438 not_properly_sorted = 1;
439 break;
440 case TREE_HAS_DUPS:
441 has_dup_entries = 1;
442 break;
443 default:
444 break;
445 }
446 }
447
448 o_mode = mode;
449 o_name = name;
ba002f3b
MK
450 }
451
452 retval = 0;
c479d14a 453 if (has_null_sha1)
c99ba492 454 retval += report(options, &item->object, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1");
ba002f3b 455 if (has_full_path)
c99ba492 456 retval += report(options, &item->object, FSCK_MSG_FULL_PATHNAME, "contains full pathnames");
ba002f3b 457 if (has_empty_name)
c99ba492 458 retval += report(options, &item->object, FSCK_MSG_EMPTY_NAME, "contains empty pathname");
5d34a435 459 if (has_dot)
c99ba492 460 retval += report(options, &item->object, FSCK_MSG_HAS_DOT, "contains '.'");
5d34a435 461 if (has_dotdot)
c99ba492 462 retval += report(options, &item->object, FSCK_MSG_HAS_DOTDOT, "contains '..'");
5c17f512 463 if (has_dotgit)
c99ba492 464 retval += report(options, &item->object, FSCK_MSG_HAS_DOTGIT, "contains '.git'");
ba002f3b 465 if (has_zero_pad)
c99ba492 466 retval += report(options, &item->object, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes");
ba002f3b 467 if (has_bad_modes)
c99ba492 468 retval += report(options, &item->object, FSCK_MSG_BAD_FILEMODE, "contains bad file modes");
ba002f3b 469 if (has_dup_entries)
c99ba492 470 retval += report(options, &item->object, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries");
ba002f3b 471 if (not_properly_sorted)
c99ba492 472 retval += report(options, &item->object, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted");
ba002f3b
MK
473 return retval;
474}
475
4d0d8975 476static int require_end_of_header(const void *data, unsigned long size,
22410549 477 struct object *obj, struct fsck_options *options)
4d0d8975
JS
478{
479 const char *buffer = (const char *)data;
480 unsigned long i;
481
482 for (i = 0; i < size; i++) {
483 switch (buffer[i]) {
484 case '\0':
c99ba492
JS
485 return report(options, obj,
486 FSCK_MSG_NUL_IN_HEADER,
487 "unterminated header: NUL at offset %ld", i);
4d0d8975
JS
488 case '\n':
489 if (i + 1 < size && buffer[i + 1] == '\n')
490 return 0;
491 }
492 }
493
c99ba492
JS
494 return report(options, obj,
495 FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
4d0d8975
JS
496}
497
22410549 498static int fsck_ident(const char **ident, struct object *obj, struct fsck_options *options)
daae1922 499{
e6826e33 500 const char *p = *ident;
d4b8de04
JK
501 char *end;
502
e6826e33
JS
503 *ident = strchrnul(*ident, '\n');
504 if (**ident == '\n')
505 (*ident)++;
506
507 if (*p == '<')
c99ba492 508 return report(options, obj, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
509 p += strcspn(p, "<>\n");
510 if (*p == '>')
c99ba492 511 return report(options, obj, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name");
e6826e33 512 if (*p != '<')
c99ba492 513 return report(options, obj, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email");
e6826e33 514 if (p[-1] != ' ')
c99ba492 515 return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email");
e6826e33
JS
516 p++;
517 p += strcspn(p, "<>\n");
518 if (*p != '>')
c99ba492 519 return report(options, obj, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email");
e6826e33
JS
520 p++;
521 if (*p != ' ')
c99ba492 522 return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date");
e6826e33
JS
523 p++;
524 if (*p == '0' && p[1] != ' ')
c99ba492 525 return report(options, obj, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date");
e6826e33 526 if (date_overflows(strtoul(p, &end, 10)))
c99ba492 527 return report(options, obj, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow");
e6826e33 528 if ((end == p || *end != ' '))
c99ba492 529 return report(options, obj, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date");
e6826e33
JS
530 p = end + 1;
531 if ((*p != '+' && *p != '-') ||
532 !isdigit(p[1]) ||
533 !isdigit(p[2]) ||
534 !isdigit(p[3]) ||
535 !isdigit(p[4]) ||
536 (p[5] != '\n'))
c99ba492 537 return report(options, obj, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone");
e6826e33 538 p += 6;
daae1922
JN
539 return 0;
540}
541
bc6b8fc1 542static int fsck_commit_buffer(struct commit *commit, const char *buffer,
22410549 543 unsigned long size, struct fsck_options *options)
ba002f3b 544{
ba002f3b
MK
545 unsigned char tree_sha1[20], sha1[20];
546 struct commit_graft *graft;
c9ad147f 547 unsigned parent_count, parent_line_count = 0, author_count;
daae1922 548 int err;
ba002f3b 549
22410549 550 if (require_end_of_header(buffer, size, &commit->object, options))
4d0d8975
JS
551 return -1;
552
cf4fff57 553 if (!skip_prefix(buffer, "tree ", &buffer))
c99ba492 554 return report(options, &commit->object, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
b3584761
JS
555 if (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\n') {
556 err = report(options, &commit->object, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
557 if (err)
558 return err;
559 }
2d820a61 560 buffer += 41;
cf4fff57 561 while (skip_prefix(buffer, "parent ", &buffer)) {
b3584761
JS
562 if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') {
563 err = report(options, &commit->object, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
564 if (err)
565 return err;
566 }
2d820a61 567 buffer += 41;
9d02150c 568 parent_line_count++;
ba002f3b
MK
569 }
570 graft = lookup_commit_graft(commit->object.sha1);
9d02150c 571 parent_count = commit_list_count(commit->parents);
ba002f3b 572 if (graft) {
9d02150c 573 if (graft->nr_parent == -1 && !parent_count)
ba002f3b 574 ; /* shallow commit */
b3584761
JS
575 else if (graft->nr_parent != parent_count) {
576 err = report(options, &commit->object, FSCK_MSG_MISSING_GRAFT, "graft objects missing");
577 if (err)
578 return err;
579 }
ba002f3b 580 } else {
b3584761
JS
581 if (parent_count != parent_line_count) {
582 err = report(options, &commit->object, FSCK_MSG_MISSING_PARENT, "parent objects missing");
583 if (err)
584 return err;
585 }
ba002f3b 586 }
c9ad147f
JS
587 author_count = 0;
588 while (skip_prefix(buffer, "author ", &buffer)) {
589 author_count++;
590 err = fsck_ident(&buffer, &commit->object, options);
591 if (err)
592 return err;
593 }
594 if (author_count < 1)
595 err = report(options, &commit->object, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line");
596 else if (author_count > 1)
597 err = report(options, &commit->object, FSCK_MSG_MULTIPLE_AUTHORS, "invalid format - multiple 'author' lines");
daae1922
JN
598 if (err)
599 return err;
cf4fff57 600 if (!skip_prefix(buffer, "committer ", &buffer))
c99ba492 601 return report(options, &commit->object, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line");
22410549 602 err = fsck_ident(&buffer, &commit->object, options);
daae1922
JN
603 if (err)
604 return err;
ba002f3b 605 if (!commit->tree)
c99ba492 606 return report(options, &commit->object, FSCK_MSG_BAD_TREE, "could not load commit's tree %s", sha1_to_hex(tree_sha1));
ba002f3b
MK
607
608 return 0;
609}
610
90a398bb 611static int fsck_commit(struct commit *commit, const char *data,
22410549 612 unsigned long size, struct fsck_options *options)
bc6b8fc1 613{
90a398bb 614 const char *buffer = data ? data : get_commit_buffer(commit, &size);
22410549 615 int ret = fsck_commit_buffer(commit, buffer, size, options);
90a398bb
JS
616 if (!data)
617 unuse_commit_buffer(commit, buffer);
bc6b8fc1
JK
618 return ret;
619}
620
cec097be 621static int fsck_tag_buffer(struct tag *tag, const char *data,
22410549 622 unsigned long size, struct fsck_options *options)
cec097be
JS
623{
624 unsigned char sha1[20];
625 int ret = 0;
626 const char *buffer;
627 char *to_free = NULL, *eol;
628 struct strbuf sb = STRBUF_INIT;
629
630 if (data)
631 buffer = data;
632 else {
633 enum object_type type;
634
635 buffer = to_free =
636 read_sha1_file(tag->object.sha1, &type, &size);
637 if (!buffer)
c99ba492
JS
638 return report(options, &tag->object,
639 FSCK_MSG_MISSING_TAG_OBJECT,
cec097be
JS
640 "cannot read tag object");
641
642 if (type != OBJ_TAG) {
c99ba492
JS
643 ret = report(options, &tag->object,
644 FSCK_MSG_TAG_OBJECT_NOT_TAG,
cec097be
JS
645 "expected tag got %s",
646 typename(type));
647 goto done;
648 }
649 }
650
22410549 651 if (require_end_of_header(buffer, size, &tag->object, options))
cec097be
JS
652 goto done;
653
654 if (!skip_prefix(buffer, "object ", &buffer)) {
c99ba492 655 ret = report(options, &tag->object, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
cec097be
JS
656 goto done;
657 }
658 if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') {
c99ba492 659 ret = report(options, &tag->object, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
7d7d5b05
JS
660 if (ret)
661 goto done;
cec097be
JS
662 }
663 buffer += 41;
664
665 if (!skip_prefix(buffer, "type ", &buffer)) {
c99ba492 666 ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line");
cec097be
JS
667 goto done;
668 }
669 eol = strchr(buffer, '\n');
670 if (!eol) {
c99ba492 671 ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line");
cec097be
JS
672 goto done;
673 }
674 if (type_from_string_gently(buffer, eol - buffer, 1) < 0)
c99ba492 675 ret = report(options, &tag->object, FSCK_MSG_BAD_TYPE, "invalid 'type' value");
cec097be
JS
676 if (ret)
677 goto done;
678 buffer = eol + 1;
679
680 if (!skip_prefix(buffer, "tag ", &buffer)) {
c99ba492 681 ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line");
cec097be
JS
682 goto done;
683 }
684 eol = strchr(buffer, '\n');
685 if (!eol) {
c99ba492 686 ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line");
cec097be
JS
687 goto done;
688 }
689 strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
690 if (check_refname_format(sb.buf, 0))
c99ba492
JS
691 report(options, &tag->object, FSCK_MSG_BAD_TAG_NAME,
692 "invalid 'tag' name: %.*s",
7add4419 693 (int)(eol - buffer), buffer);
cec097be
JS
694 buffer = eol + 1;
695
696 if (!skip_prefix(buffer, "tagger ", &buffer))
697 /* early tags do not contain 'tagger' lines; warn only */
c99ba492 698 report(options, &tag->object, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
cec097be 699 else
22410549 700 ret = fsck_ident(&buffer, &tag->object, options);
cec097be
JS
701
702done:
703 strbuf_release(&sb);
704 free(to_free);
705 return ret;
706}
707
90a398bb 708static int fsck_tag(struct tag *tag, const char *data,
22410549 709 unsigned long size, struct fsck_options *options)
ba002f3b
MK
710{
711 struct object *tagged = tag->tagged;
712
713 if (!tagged)
c99ba492 714 return report(options, &tag->object, FSCK_MSG_BAD_TAG_OBJECT, "could not load tagged object");
cec097be 715
22410549 716 return fsck_tag_buffer(tag, data, size, options);
ba002f3b
MK
717}
718
90a398bb 719int fsck_object(struct object *obj, void *data, unsigned long size,
22410549 720 struct fsck_options *options)
ba002f3b
MK
721{
722 if (!obj)
c99ba492 723 return report(options, obj, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
ba002f3b
MK
724
725 if (obj->type == OBJ_BLOB)
726 return 0;
727 if (obj->type == OBJ_TREE)
22410549 728 return fsck_tree((struct tree *) obj, options);
ba002f3b 729 if (obj->type == OBJ_COMMIT)
90a398bb 730 return fsck_commit((struct commit *) obj, (const char *) data,
22410549 731 size, options);
ba002f3b 732 if (obj->type == OBJ_TAG)
90a398bb 733 return fsck_tag((struct tag *) obj, (const char *) data,
22410549 734 size, options);
ba002f3b 735
c99ba492 736 return report(options, obj, FSCK_MSG_UNKNOWN_TYPE, "unknown type '%d' (internal fsck error)",
ba002f3b
MK
737 obj->type);
738}
d6ffc8d7 739
c99ba492 740int fsck_error_function(struct object *obj, int msg_type, const char *message)
d6ffc8d7 741{
0282f4dc
JS
742 if (msg_type == FSCK_WARN) {
743 warning("object %s: %s", sha1_to_hex(obj->sha1), message);
744 return 0;
745 }
c99ba492 746 error("object %s: %s", sha1_to_hex(obj->sha1), message);
d6ffc8d7
MK
747 return 1;
748}