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