]>
Commit | Line | Data |
---|---|---|
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, | |
60 | enum 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 | 68 | static 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 |
78 | static 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 |
106 | static int fsck_msg_type(enum fsck_msg_id msg_id, |
107 | struct fsck_options *options) | |
108 | { | |
109 | int msg_type; | |
110 | ||
111 | msg_type = msg_id_info[msg_id].msg_type; | |
112 | if (options->strict && msg_type == FSCK_WARN) | |
113 | msg_type = FSCK_ERROR; | |
114 | ||
115 | return msg_type; | |
116 | } | |
117 | ||
118 | __attribute__((format (printf, 4, 5))) | |
119 | static int report(struct fsck_options *options, struct object *object, | |
120 | enum fsck_msg_id id, const char *fmt, ...) | |
121 | { | |
122 | va_list ap; | |
123 | struct strbuf sb = STRBUF_INIT; | |
124 | int msg_type = fsck_msg_type(id, options), result; | |
125 | ||
126 | va_start(ap, fmt); | |
127 | strbuf_vaddf(&sb, fmt, ap); | |
128 | result = options->error_func(object, msg_type, sb.buf); | |
129 | strbuf_release(&sb); | |
130 | va_end(ap); | |
131 | ||
132 | return result; | |
133 | } | |
134 | ||
22410549 | 135 | static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *options) |
355885d5 MK |
136 | { |
137 | struct tree_desc desc; | |
138 | struct name_entry entry; | |
139 | int res = 0; | |
140 | ||
141 | if (parse_tree(tree)) | |
142 | return -1; | |
143 | ||
144 | init_tree_desc(&desc, tree->buffer, tree->size); | |
145 | while (tree_entry(&desc, &entry)) { | |
146 | int result; | |
147 | ||
148 | if (S_ISGITLINK(entry.mode)) | |
149 | continue; | |
150 | if (S_ISDIR(entry.mode)) | |
22410549 | 151 | result = options->walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data, options); |
355885d5 | 152 | else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) |
22410549 | 153 | result = options->walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data, options); |
355885d5 | 154 | else { |
82247e9b | 155 | result = error("in tree %s: entry %s has bad mode %.6o", |
355885d5 MK |
156 | sha1_to_hex(tree->object.sha1), entry.path, entry.mode); |
157 | } | |
158 | if (result < 0) | |
159 | return result; | |
160 | if (!res) | |
161 | res = result; | |
162 | } | |
163 | return res; | |
164 | } | |
165 | ||
22410549 | 166 | static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_options *options) |
355885d5 MK |
167 | { |
168 | struct commit_list *parents; | |
169 | int res; | |
170 | int result; | |
171 | ||
172 | if (parse_commit(commit)) | |
173 | return -1; | |
174 | ||
22410549 | 175 | result = options->walk((struct object *)commit->tree, OBJ_TREE, data, options); |
355885d5 MK |
176 | if (result < 0) |
177 | return result; | |
178 | res = result; | |
179 | ||
180 | parents = commit->parents; | |
181 | while (parents) { | |
22410549 | 182 | result = options->walk((struct object *)parents->item, OBJ_COMMIT, data, options); |
355885d5 MK |
183 | if (result < 0) |
184 | return result; | |
185 | if (!res) | |
186 | res = result; | |
187 | parents = parents->next; | |
188 | } | |
189 | return res; | |
190 | } | |
191 | ||
22410549 | 192 | static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *options) |
355885d5 MK |
193 | { |
194 | if (parse_tag(tag)) | |
195 | return -1; | |
22410549 | 196 | return options->walk(tag->tagged, OBJ_ANY, data, options); |
355885d5 MK |
197 | } |
198 | ||
22410549 | 199 | int fsck_walk(struct object *obj, void *data, struct fsck_options *options) |
355885d5 MK |
200 | { |
201 | if (!obj) | |
202 | return -1; | |
203 | switch (obj->type) { | |
204 | case OBJ_BLOB: | |
205 | return 0; | |
206 | case OBJ_TREE: | |
22410549 | 207 | return fsck_walk_tree((struct tree *)obj, data, options); |
355885d5 | 208 | case OBJ_COMMIT: |
22410549 | 209 | return fsck_walk_commit((struct commit *)obj, data, options); |
355885d5 | 210 | case OBJ_TAG: |
22410549 | 211 | return fsck_walk_tag((struct tag *)obj, data, options); |
355885d5 MK |
212 | default: |
213 | error("Unknown object type for %s", sha1_to_hex(obj->sha1)); | |
214 | return -1; | |
215 | } | |
216 | } | |
ba002f3b MK |
217 | |
218 | /* | |
219 | * The entries in a tree are ordered in the _path_ order, | |
220 | * which means that a directory entry is ordered by adding | |
221 | * a slash to the end of it. | |
222 | * | |
223 | * So a directory called "a" is ordered _after_ a file | |
224 | * called "a.c", because "a/" sorts after "a.c". | |
225 | */ | |
226 | #define TREE_UNORDERED (-1) | |
227 | #define TREE_HAS_DUPS (-2) | |
228 | ||
229 | static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2) | |
230 | { | |
231 | int len1 = strlen(name1); | |
232 | int len2 = strlen(name2); | |
233 | int len = len1 < len2 ? len1 : len2; | |
234 | unsigned char c1, c2; | |
235 | int cmp; | |
236 | ||
237 | cmp = memcmp(name1, name2, len); | |
238 | if (cmp < 0) | |
239 | return 0; | |
240 | if (cmp > 0) | |
241 | return TREE_UNORDERED; | |
242 | ||
243 | /* | |
244 | * Ok, the first <len> characters are the same. | |
245 | * Now we need to order the next one, but turn | |
246 | * a '\0' into a '/' for a directory entry. | |
247 | */ | |
248 | c1 = name1[len]; | |
249 | c2 = name2[len]; | |
250 | if (!c1 && !c2) | |
251 | /* | |
252 | * git-write-tree used to write out a nonsense tree that has | |
253 | * entries with the same name, one blob and one tree. Make | |
254 | * sure we do not have duplicate entries. | |
255 | */ | |
256 | return TREE_HAS_DUPS; | |
257 | if (!c1 && S_ISDIR(mode1)) | |
258 | c1 = '/'; | |
259 | if (!c2 && S_ISDIR(mode2)) | |
260 | c2 = '/'; | |
261 | return c1 < c2 ? 0 : TREE_UNORDERED; | |
262 | } | |
263 | ||
22410549 | 264 | static int fsck_tree(struct tree *item, struct fsck_options *options) |
ba002f3b MK |
265 | { |
266 | int retval; | |
c479d14a | 267 | int has_null_sha1 = 0; |
ba002f3b MK |
268 | int has_full_path = 0; |
269 | int has_empty_name = 0; | |
5d34a435 JK |
270 | int has_dot = 0; |
271 | int has_dotdot = 0; | |
5c17f512 | 272 | int has_dotgit = 0; |
ba002f3b MK |
273 | int has_zero_pad = 0; |
274 | int has_bad_modes = 0; | |
275 | int has_dup_entries = 0; | |
276 | int not_properly_sorted = 0; | |
277 | struct tree_desc desc; | |
278 | unsigned o_mode; | |
279 | const char *o_name; | |
ba002f3b MK |
280 | |
281 | init_tree_desc(&desc, item->buffer, item->size); | |
282 | ||
283 | o_mode = 0; | |
284 | o_name = NULL; | |
ba002f3b MK |
285 | |
286 | while (desc.size) { | |
287 | unsigned mode; | |
288 | const char *name; | |
c479d14a | 289 | const unsigned char *sha1; |
ba002f3b | 290 | |
c479d14a | 291 | sha1 = tree_entry_extract(&desc, &name, &mode); |
ba002f3b | 292 | |
effd12ec HS |
293 | has_null_sha1 |= is_null_sha1(sha1); |
294 | has_full_path |= !!strchr(name, '/'); | |
295 | has_empty_name |= !*name; | |
296 | has_dot |= !strcmp(name, "."); | |
297 | has_dotdot |= !strcmp(name, ".."); | |
5e519fb8 JH |
298 | has_dotgit |= (!strcmp(name, ".git") || |
299 | is_hfs_dotgit(name) || | |
300 | is_ntfs_dotgit(name)); | |
ba002f3b MK |
301 | has_zero_pad |= *(char *)desc.buffer == '0'; |
302 | update_tree_entry(&desc); | |
303 | ||
304 | switch (mode) { | |
305 | /* | |
306 | * Standard modes.. | |
307 | */ | |
308 | case S_IFREG | 0755: | |
309 | case S_IFREG | 0644: | |
310 | case S_IFLNK: | |
311 | case S_IFDIR: | |
312 | case S_IFGITLINK: | |
313 | break; | |
314 | /* | |
315 | * This is nonstandard, but we had a few of these | |
316 | * early on when we honored the full set of mode | |
317 | * bits.. | |
318 | */ | |
319 | case S_IFREG | 0664: | |
22410549 | 320 | if (!options->strict) |
ba002f3b MK |
321 | break; |
322 | default: | |
323 | has_bad_modes = 1; | |
324 | } | |
325 | ||
326 | if (o_name) { | |
327 | switch (verify_ordered(o_mode, o_name, mode, name)) { | |
328 | case TREE_UNORDERED: | |
329 | not_properly_sorted = 1; | |
330 | break; | |
331 | case TREE_HAS_DUPS: | |
332 | has_dup_entries = 1; | |
333 | break; | |
334 | default: | |
335 | break; | |
336 | } | |
337 | } | |
338 | ||
339 | o_mode = mode; | |
340 | o_name = name; | |
ba002f3b MK |
341 | } |
342 | ||
343 | retval = 0; | |
c479d14a | 344 | if (has_null_sha1) |
c99ba492 | 345 | retval += report(options, &item->object, FSCK_MSG_NULL_SHA1, "contains entries pointing to null sha1"); |
ba002f3b | 346 | if (has_full_path) |
c99ba492 | 347 | retval += report(options, &item->object, FSCK_MSG_FULL_PATHNAME, "contains full pathnames"); |
ba002f3b | 348 | if (has_empty_name) |
c99ba492 | 349 | retval += report(options, &item->object, FSCK_MSG_EMPTY_NAME, "contains empty pathname"); |
5d34a435 | 350 | if (has_dot) |
c99ba492 | 351 | retval += report(options, &item->object, FSCK_MSG_HAS_DOT, "contains '.'"); |
5d34a435 | 352 | if (has_dotdot) |
c99ba492 | 353 | retval += report(options, &item->object, FSCK_MSG_HAS_DOTDOT, "contains '..'"); |
5c17f512 | 354 | if (has_dotgit) |
c99ba492 | 355 | retval += report(options, &item->object, FSCK_MSG_HAS_DOTGIT, "contains '.git'"); |
ba002f3b | 356 | if (has_zero_pad) |
c99ba492 | 357 | retval += report(options, &item->object, FSCK_MSG_ZERO_PADDED_FILEMODE, "contains zero-padded file modes"); |
ba002f3b | 358 | if (has_bad_modes) |
c99ba492 | 359 | retval += report(options, &item->object, FSCK_MSG_BAD_FILEMODE, "contains bad file modes"); |
ba002f3b | 360 | if (has_dup_entries) |
c99ba492 | 361 | retval += report(options, &item->object, FSCK_MSG_DUPLICATE_ENTRIES, "contains duplicate file entries"); |
ba002f3b | 362 | if (not_properly_sorted) |
c99ba492 | 363 | retval += report(options, &item->object, FSCK_MSG_TREE_NOT_SORTED, "not properly sorted"); |
ba002f3b MK |
364 | return retval; |
365 | } | |
366 | ||
4d0d8975 | 367 | static int require_end_of_header(const void *data, unsigned long size, |
22410549 | 368 | struct object *obj, struct fsck_options *options) |
4d0d8975 JS |
369 | { |
370 | const char *buffer = (const char *)data; | |
371 | unsigned long i; | |
372 | ||
373 | for (i = 0; i < size; i++) { | |
374 | switch (buffer[i]) { | |
375 | case '\0': | |
c99ba492 JS |
376 | return report(options, obj, |
377 | FSCK_MSG_NUL_IN_HEADER, | |
378 | "unterminated header: NUL at offset %ld", i); | |
4d0d8975 JS |
379 | case '\n': |
380 | if (i + 1 < size && buffer[i + 1] == '\n') | |
381 | return 0; | |
382 | } | |
383 | } | |
384 | ||
c99ba492 JS |
385 | return report(options, obj, |
386 | FSCK_MSG_UNTERMINATED_HEADER, "unterminated header"); | |
4d0d8975 JS |
387 | } |
388 | ||
22410549 | 389 | static int fsck_ident(const char **ident, struct object *obj, struct fsck_options *options) |
daae1922 | 390 | { |
d4b8de04 JK |
391 | char *end; |
392 | ||
53f53cff | 393 | if (**ident == '<') |
c99ba492 | 394 | return report(options, obj, FSCK_MSG_MISSING_NAME_BEFORE_EMAIL, "invalid author/committer line - missing space before email"); |
53f53cff DI |
395 | *ident += strcspn(*ident, "<>\n"); |
396 | if (**ident == '>') | |
c99ba492 | 397 | return report(options, obj, FSCK_MSG_BAD_NAME, "invalid author/committer line - bad name"); |
daae1922 | 398 | if (**ident != '<') |
c99ba492 | 399 | return report(options, obj, FSCK_MSG_MISSING_EMAIL, "invalid author/committer line - missing email"); |
53f53cff | 400 | if ((*ident)[-1] != ' ') |
c99ba492 | 401 | return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_EMAIL, "invalid author/committer line - missing space before email"); |
daae1922 JN |
402 | (*ident)++; |
403 | *ident += strcspn(*ident, "<>\n"); | |
404 | if (**ident != '>') | |
c99ba492 | 405 | return report(options, obj, FSCK_MSG_BAD_EMAIL, "invalid author/committer line - bad email"); |
daae1922 JN |
406 | (*ident)++; |
407 | if (**ident != ' ') | |
c99ba492 | 408 | return report(options, obj, FSCK_MSG_MISSING_SPACE_BEFORE_DATE, "invalid author/committer line - missing space before date"); |
daae1922 JN |
409 | (*ident)++; |
410 | if (**ident == '0' && (*ident)[1] != ' ') | |
c99ba492 | 411 | return report(options, obj, FSCK_MSG_ZERO_PADDED_DATE, "invalid author/committer line - zero-padded date"); |
7ca36d93 | 412 | if (date_overflows(strtoul(*ident, &end, 10))) |
c99ba492 | 413 | return report(options, obj, FSCK_MSG_BAD_DATE_OVERFLOW, "invalid author/committer line - date causes integer overflow"); |
d4b8de04 | 414 | if (end == *ident || *end != ' ') |
c99ba492 | 415 | return report(options, obj, FSCK_MSG_BAD_DATE, "invalid author/committer line - bad date"); |
d4b8de04 | 416 | *ident = end + 1; |
daae1922 JN |
417 | if ((**ident != '+' && **ident != '-') || |
418 | !isdigit((*ident)[1]) || | |
419 | !isdigit((*ident)[2]) || | |
420 | !isdigit((*ident)[3]) || | |
421 | !isdigit((*ident)[4]) || | |
422 | ((*ident)[5] != '\n')) | |
c99ba492 | 423 | return report(options, obj, FSCK_MSG_BAD_TIMEZONE, "invalid author/committer line - bad time zone"); |
daae1922 JN |
424 | (*ident) += 6; |
425 | return 0; | |
426 | } | |
427 | ||
bc6b8fc1 | 428 | static int fsck_commit_buffer(struct commit *commit, const char *buffer, |
22410549 | 429 | unsigned long size, struct fsck_options *options) |
ba002f3b | 430 | { |
ba002f3b MK |
431 | unsigned char tree_sha1[20], sha1[20]; |
432 | struct commit_graft *graft; | |
9d02150c | 433 | unsigned parent_count, parent_line_count = 0; |
daae1922 | 434 | int err; |
ba002f3b | 435 | |
22410549 | 436 | if (require_end_of_header(buffer, size, &commit->object, options)) |
4d0d8975 JS |
437 | return -1; |
438 | ||
cf4fff57 | 439 | if (!skip_prefix(buffer, "tree ", &buffer)) |
c99ba492 | 440 | return report(options, &commit->object, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line"); |
2d820a61 | 441 | if (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\n') |
c99ba492 | 442 | return report(options, &commit->object, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1"); |
2d820a61 | 443 | buffer += 41; |
cf4fff57 | 444 | while (skip_prefix(buffer, "parent ", &buffer)) { |
2d820a61 | 445 | if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') |
c99ba492 | 446 | return report(options, &commit->object, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1"); |
2d820a61 | 447 | buffer += 41; |
9d02150c | 448 | parent_line_count++; |
ba002f3b MK |
449 | } |
450 | graft = lookup_commit_graft(commit->object.sha1); | |
9d02150c | 451 | parent_count = commit_list_count(commit->parents); |
ba002f3b | 452 | if (graft) { |
9d02150c | 453 | if (graft->nr_parent == -1 && !parent_count) |
ba002f3b | 454 | ; /* shallow commit */ |
9d02150c | 455 | else if (graft->nr_parent != parent_count) |
c99ba492 | 456 | return report(options, &commit->object, FSCK_MSG_MISSING_GRAFT, "graft objects missing"); |
ba002f3b | 457 | } else { |
9d02150c | 458 | if (parent_count != parent_line_count) |
c99ba492 | 459 | return report(options, &commit->object, FSCK_MSG_MISSING_PARENT, "parent objects missing"); |
ba002f3b | 460 | } |
cf4fff57 | 461 | if (!skip_prefix(buffer, "author ", &buffer)) |
c99ba492 | 462 | return report(options, &commit->object, FSCK_MSG_MISSING_AUTHOR, "invalid format - expected 'author' line"); |
22410549 | 463 | err = fsck_ident(&buffer, &commit->object, options); |
daae1922 JN |
464 | if (err) |
465 | return err; | |
cf4fff57 | 466 | if (!skip_prefix(buffer, "committer ", &buffer)) |
c99ba492 | 467 | return report(options, &commit->object, FSCK_MSG_MISSING_COMMITTER, "invalid format - expected 'committer' line"); |
22410549 | 468 | err = fsck_ident(&buffer, &commit->object, options); |
daae1922 JN |
469 | if (err) |
470 | return err; | |
ba002f3b | 471 | if (!commit->tree) |
c99ba492 | 472 | return report(options, &commit->object, FSCK_MSG_BAD_TREE, "could not load commit's tree %s", sha1_to_hex(tree_sha1)); |
ba002f3b MK |
473 | |
474 | return 0; | |
475 | } | |
476 | ||
90a398bb | 477 | static int fsck_commit(struct commit *commit, const char *data, |
22410549 | 478 | unsigned long size, struct fsck_options *options) |
bc6b8fc1 | 479 | { |
90a398bb | 480 | const char *buffer = data ? data : get_commit_buffer(commit, &size); |
22410549 | 481 | int ret = fsck_commit_buffer(commit, buffer, size, options); |
90a398bb JS |
482 | if (!data) |
483 | unuse_commit_buffer(commit, buffer); | |
bc6b8fc1 JK |
484 | return ret; |
485 | } | |
486 | ||
cec097be | 487 | static int fsck_tag_buffer(struct tag *tag, const char *data, |
22410549 | 488 | unsigned long size, struct fsck_options *options) |
cec097be JS |
489 | { |
490 | unsigned char sha1[20]; | |
491 | int ret = 0; | |
492 | const char *buffer; | |
493 | char *to_free = NULL, *eol; | |
494 | struct strbuf sb = STRBUF_INIT; | |
495 | ||
496 | if (data) | |
497 | buffer = data; | |
498 | else { | |
499 | enum object_type type; | |
500 | ||
501 | buffer = to_free = | |
502 | read_sha1_file(tag->object.sha1, &type, &size); | |
503 | if (!buffer) | |
c99ba492 JS |
504 | return report(options, &tag->object, |
505 | FSCK_MSG_MISSING_TAG_OBJECT, | |
cec097be JS |
506 | "cannot read tag object"); |
507 | ||
508 | if (type != OBJ_TAG) { | |
c99ba492 JS |
509 | ret = report(options, &tag->object, |
510 | FSCK_MSG_TAG_OBJECT_NOT_TAG, | |
cec097be JS |
511 | "expected tag got %s", |
512 | typename(type)); | |
513 | goto done; | |
514 | } | |
515 | } | |
516 | ||
22410549 | 517 | if (require_end_of_header(buffer, size, &tag->object, options)) |
cec097be JS |
518 | goto done; |
519 | ||
520 | if (!skip_prefix(buffer, "object ", &buffer)) { | |
c99ba492 | 521 | ret = report(options, &tag->object, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line"); |
cec097be JS |
522 | goto done; |
523 | } | |
524 | if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n') { | |
c99ba492 | 525 | ret = report(options, &tag->object, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1"); |
cec097be JS |
526 | goto done; |
527 | } | |
528 | buffer += 41; | |
529 | ||
530 | if (!skip_prefix(buffer, "type ", &buffer)) { | |
c99ba492 | 531 | ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE_ENTRY, "invalid format - expected 'type' line"); |
cec097be JS |
532 | goto done; |
533 | } | |
534 | eol = strchr(buffer, '\n'); | |
535 | if (!eol) { | |
c99ba492 | 536 | ret = report(options, &tag->object, FSCK_MSG_MISSING_TYPE, "invalid format - unexpected end after 'type' line"); |
cec097be JS |
537 | goto done; |
538 | } | |
539 | if (type_from_string_gently(buffer, eol - buffer, 1) < 0) | |
c99ba492 | 540 | ret = report(options, &tag->object, FSCK_MSG_BAD_TYPE, "invalid 'type' value"); |
cec097be JS |
541 | if (ret) |
542 | goto done; | |
543 | buffer = eol + 1; | |
544 | ||
545 | if (!skip_prefix(buffer, "tag ", &buffer)) { | |
c99ba492 | 546 | ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG_ENTRY, "invalid format - expected 'tag' line"); |
cec097be JS |
547 | goto done; |
548 | } | |
549 | eol = strchr(buffer, '\n'); | |
550 | if (!eol) { | |
c99ba492 | 551 | ret = report(options, &tag->object, FSCK_MSG_MISSING_TAG, "invalid format - unexpected end after 'type' line"); |
cec097be JS |
552 | goto done; |
553 | } | |
554 | strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer); | |
555 | if (check_refname_format(sb.buf, 0)) | |
c99ba492 JS |
556 | report(options, &tag->object, FSCK_MSG_BAD_TAG_NAME, |
557 | "invalid 'tag' name: %.*s", | |
7add4419 | 558 | (int)(eol - buffer), buffer); |
cec097be JS |
559 | buffer = eol + 1; |
560 | ||
561 | if (!skip_prefix(buffer, "tagger ", &buffer)) | |
562 | /* early tags do not contain 'tagger' lines; warn only */ | |
c99ba492 | 563 | report(options, &tag->object, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line"); |
cec097be | 564 | else |
22410549 | 565 | ret = fsck_ident(&buffer, &tag->object, options); |
cec097be JS |
566 | |
567 | done: | |
568 | strbuf_release(&sb); | |
569 | free(to_free); | |
570 | return ret; | |
571 | } | |
572 | ||
90a398bb | 573 | static int fsck_tag(struct tag *tag, const char *data, |
22410549 | 574 | unsigned long size, struct fsck_options *options) |
ba002f3b MK |
575 | { |
576 | struct object *tagged = tag->tagged; | |
577 | ||
578 | if (!tagged) | |
c99ba492 | 579 | return report(options, &tag->object, FSCK_MSG_BAD_TAG_OBJECT, "could not load tagged object"); |
cec097be | 580 | |
22410549 | 581 | return fsck_tag_buffer(tag, data, size, options); |
ba002f3b MK |
582 | } |
583 | ||
90a398bb | 584 | int fsck_object(struct object *obj, void *data, unsigned long size, |
22410549 | 585 | struct fsck_options *options) |
ba002f3b MK |
586 | { |
587 | if (!obj) | |
c99ba492 | 588 | return report(options, obj, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck"); |
ba002f3b MK |
589 | |
590 | if (obj->type == OBJ_BLOB) | |
591 | return 0; | |
592 | if (obj->type == OBJ_TREE) | |
22410549 | 593 | return fsck_tree((struct tree *) obj, options); |
ba002f3b | 594 | if (obj->type == OBJ_COMMIT) |
90a398bb | 595 | return fsck_commit((struct commit *) obj, (const char *) data, |
22410549 | 596 | size, options); |
ba002f3b | 597 | if (obj->type == OBJ_TAG) |
90a398bb | 598 | return fsck_tag((struct tag *) obj, (const char *) data, |
22410549 | 599 | size, options); |
ba002f3b | 600 | |
c99ba492 | 601 | return report(options, obj, FSCK_MSG_UNKNOWN_TYPE, "unknown type '%d' (internal fsck error)", |
ba002f3b MK |
602 | obj->type); |
603 | } | |
d6ffc8d7 | 604 | |
c99ba492 | 605 | int fsck_error_function(struct object *obj, int msg_type, const char *message) |
d6ffc8d7 | 606 | { |
c99ba492 | 607 | error("object %s: %s", sha1_to_hex(obj->sha1), message); |
d6ffc8d7 MK |
608 | return 1; |
609 | } |