]>
Commit | Line | Data |
---|---|---|
4b182421 | 1 | #include "cache.h" |
ff5ebe39 DB |
2 | #include "commit.h" |
3 | #include "tree.h" | |
4 | #include "blob.h" | |
c418eda4 | 5 | #include "tag.h" |
944d8589 | 6 | #include "refs.h" |
f9253394 | 7 | #include "pack.h" |
53dc3f3e | 8 | #include "cache-tree.h" |
e9a95bef | 9 | #include "tree-walk.h" |
ff5ebe39 DB |
10 | |
11 | #define REACHABLE 0x0001 | |
2d9c58c6 | 12 | #define SEEN 0x0002 |
d9839e03 | 13 | |
96f1e58f DR |
14 | static int show_root; |
15 | static int show_tags; | |
16 | static int show_unreachable; | |
17 | static int check_full; | |
18 | static int check_strict; | |
19 | static int keep_cache_objects; | |
d9839e03 LT |
20 | static unsigned char head_sha1[20]; |
21 | ||
962554c6 | 22 | #ifdef NO_D_INO_IN_DIRENT |
35a730f0 JH |
23 | #define SORT_DIRENT 0 |
24 | #define DIRENT_SORT_HINT(de) 0 | |
25 | #else | |
26 | #define SORT_DIRENT 1 | |
27 | #define DIRENT_SORT_HINT(de) ((de)->d_ino) | |
28 | #endif | |
f1f0d088 PB |
29 | |
30 | static void objreport(struct object *obj, const char *severity, | |
31 | const char *err, va_list params) | |
32 | { | |
33 | fprintf(stderr, "%s in %s %s: ", | |
885a86ab | 34 | severity, typename(obj->type), sha1_to_hex(obj->sha1)); |
f1f0d088 PB |
35 | vfprintf(stderr, err, params); |
36 | fputs("\n", stderr); | |
37 | } | |
38 | ||
a7928f8e | 39 | static int objerror(struct object *obj, const char *err, ...) |
f1f0d088 PB |
40 | { |
41 | va_list params; | |
42 | va_start(params, err); | |
43 | objreport(obj, "error", err, params); | |
44 | va_end(params); | |
45 | return -1; | |
46 | } | |
47 | ||
a7928f8e | 48 | static int objwarning(struct object *obj, const char *err, ...) |
f1f0d088 PB |
49 | { |
50 | va_list params; | |
51 | va_start(params, err); | |
52 | objreport(obj, "warning", err, params); | |
53 | va_end(params); | |
54 | return -1; | |
55 | } | |
56 | ||
57 | ||
8ba0bbb2 LT |
58 | static void check_connectivity(void) |
59 | { | |
fc046a75 | 60 | int i, max; |
8ba0bbb2 | 61 | |
8ba0bbb2 | 62 | /* Look up all the requirements, warn about missing objects.. */ |
fc046a75 LT |
63 | max = get_max_object_index(); |
64 | for (i = 0; i < max; i++) { | |
3e4339e6 | 65 | const struct object_refs *refs; |
fc046a75 | 66 | struct object *obj = get_indexed_object(i); |
8ba0bbb2 | 67 | |
070879ca JS |
68 | if (!obj) |
69 | continue; | |
70 | ||
3a6a23e6 | 71 | if (!obj->parsed) { |
7aaa715d | 72 | if (has_sha1_file(obj->sha1)) |
8a498a05 JH |
73 | ; /* it is in pack */ |
74 | else | |
75 | printf("missing %s %s\n", | |
885a86ab | 76 | typename(obj->type), sha1_to_hex(obj->sha1)); |
3a6a23e6 LT |
77 | continue; |
78 | } | |
79 | ||
3e4339e6 LT |
80 | refs = lookup_object_refs(obj); |
81 | if (refs) { | |
4a4e6fd7 SV |
82 | unsigned j; |
83 | for (j = 0; j < refs->count; j++) { | |
84 | struct object *ref = refs->ref[j]; | |
85 | if (ref->parsed || | |
7aaa715d | 86 | (has_sha1_file(ref->sha1))) |
4a4e6fd7 SV |
87 | continue; |
88 | printf("broken link from %7s %s\n", | |
885a86ab | 89 | typename(obj->type), sha1_to_hex(obj->sha1)); |
4a4e6fd7 | 90 | printf(" to %7s %s\n", |
885a86ab | 91 | typename(ref->type), sha1_to_hex(ref->sha1)); |
4a4e6fd7 | 92 | } |
3a6a23e6 LT |
93 | } |
94 | ||
ff5ebe39 | 95 | if (show_unreachable && !(obj->flags & REACHABLE)) { |
c4584ae3 | 96 | printf("unreachable %s %s\n", |
885a86ab | 97 | typename(obj->type), sha1_to_hex(obj->sha1)); |
8ba0bbb2 | 98 | continue; |
d9839e03 | 99 | } |
8ba0bbb2 | 100 | |
ff5ebe39 | 101 | if (!obj->used) { |
885a86ab | 102 | printf("dangling %s %s\n", typename(obj->type), |
ff5ebe39 | 103 | sha1_to_hex(obj->sha1)); |
d9839e03 | 104 | } |
8ba0bbb2 LT |
105 | } |
106 | } | |
107 | ||
85003492 LT |
108 | /* |
109 | * The entries in a tree are ordered in the _path_ order, | |
110 | * which means that a directory entry is ordered by adding | |
111 | * a slash to the end of it. | |
112 | * | |
113 | * So a directory called "a" is ordered _after_ a file | |
114 | * called "a.c", because "a/" sorts after "a.c". | |
115 | */ | |
a4f35a2d JH |
116 | #define TREE_UNORDERED (-1) |
117 | #define TREE_HAS_DUPS (-2) | |
118 | ||
e9a95bef | 119 | static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2) |
85003492 | 120 | { |
e9a95bef LT |
121 | int len1 = strlen(name1); |
122 | int len2 = strlen(name2); | |
85003492 LT |
123 | int len = len1 < len2 ? len1 : len2; |
124 | unsigned char c1, c2; | |
125 | int cmp; | |
126 | ||
e9a95bef | 127 | cmp = memcmp(name1, name2, len); |
85003492 LT |
128 | if (cmp < 0) |
129 | return 0; | |
130 | if (cmp > 0) | |
a4f35a2d | 131 | return TREE_UNORDERED; |
85003492 LT |
132 | |
133 | /* | |
134 | * Ok, the first <len> characters are the same. | |
135 | * Now we need to order the next one, but turn | |
136 | * a '\0' into a '/' for a directory entry. | |
137 | */ | |
e9a95bef LT |
138 | c1 = name1[len]; |
139 | c2 = name2[len]; | |
a4f35a2d JH |
140 | if (!c1 && !c2) |
141 | /* | |
142 | * git-write-tree used to write out a nonsense tree that has | |
143 | * entries with the same name, one blob and one tree. Make | |
144 | * sure we do not have duplicate entries. | |
145 | */ | |
146 | return TREE_HAS_DUPS; | |
e9a95bef | 147 | if (!c1 && S_ISDIR(mode1)) |
85003492 | 148 | c1 = '/'; |
e9a95bef | 149 | if (!c2 && S_ISDIR(mode2)) |
85003492 | 150 | c2 = '/'; |
a4f35a2d | 151 | return c1 < c2 ? 0 : TREE_UNORDERED; |
85003492 LT |
152 | } |
153 | ||
c418eda4 | 154 | static int fsck_tree(struct tree *item) |
20222118 | 155 | { |
64071805 | 156 | int retval; |
85003492 | 157 | int has_full_path = 0; |
64071805 LT |
158 | int has_zero_pad = 0; |
159 | int has_bad_modes = 0; | |
160 | int has_dup_entries = 0; | |
161 | int not_properly_sorted = 0; | |
e9a95bef LT |
162 | struct tree_desc desc; |
163 | unsigned o_mode; | |
164 | const char *o_name; | |
165 | const unsigned char *o_sha1; | |
85003492 | 166 | |
e9a95bef LT |
167 | desc.buf = item->buffer; |
168 | desc.size = item->size; | |
169 | ||
170 | o_mode = 0; | |
171 | o_name = NULL; | |
172 | o_sha1 = NULL; | |
173 | while (desc.size) { | |
174 | unsigned mode; | |
175 | const char *name; | |
176 | const unsigned char *sha1; | |
177 | ||
178 | sha1 = tree_entry_extract(&desc, &name, &mode); | |
179 | ||
180 | if (strchr(name, '/')) | |
85003492 | 181 | has_full_path = 1; |
e9a95bef LT |
182 | has_zero_pad |= *(char *)desc.buf == '0'; |
183 | update_tree_entry(&desc); | |
85003492 | 184 | |
e9a95bef | 185 | switch (mode) { |
42ea9cb2 | 186 | /* |
e9a95bef | 187 | * Standard modes.. |
42ea9cb2 LT |
188 | */ |
189 | case S_IFREG | 0755: | |
190 | case S_IFREG | 0644: | |
191 | case S_IFLNK: | |
192 | case S_IFDIR: | |
193 | break; | |
194 | /* | |
195 | * This is nonstandard, but we had a few of these | |
196 | * early on when we honored the full set of mode | |
197 | * bits.. | |
198 | */ | |
199 | case S_IFREG | 0664: | |
de2eb7f6 LT |
200 | if (!check_strict) |
201 | break; | |
42ea9cb2 | 202 | default: |
64071805 | 203 | has_bad_modes = 1; |
42ea9cb2 LT |
204 | } |
205 | ||
e9a95bef LT |
206 | if (o_name) { |
207 | switch (verify_ordered(o_mode, o_name, mode, name)) { | |
a4f35a2d | 208 | case TREE_UNORDERED: |
64071805 LT |
209 | not_properly_sorted = 1; |
210 | break; | |
a4f35a2d | 211 | case TREE_HAS_DUPS: |
64071805 LT |
212 | has_dup_entries = 1; |
213 | break; | |
a4f35a2d JH |
214 | default: |
215 | break; | |
85003492 LT |
216 | } |
217 | } | |
218 | ||
e9a95bef LT |
219 | o_mode = mode; |
220 | o_name = name; | |
221 | o_sha1 = sha1; | |
85003492 | 222 | } |
136f2e54 LT |
223 | free(item->buffer); |
224 | item->buffer = NULL; | |
85003492 | 225 | |
64071805 | 226 | retval = 0; |
85003492 | 227 | if (has_full_path) { |
f1f0d088 | 228 | objwarning(&item->object, "contains full pathnames"); |
1ea34e36 | 229 | } |
64071805 | 230 | if (has_zero_pad) { |
f1f0d088 | 231 | objwarning(&item->object, "contains zero-padded file modes"); |
64071805 LT |
232 | } |
233 | if (has_bad_modes) { | |
f1f0d088 | 234 | objwarning(&item->object, "contains bad file modes"); |
64071805 LT |
235 | } |
236 | if (has_dup_entries) { | |
f1f0d088 | 237 | retval = objerror(&item->object, "contains duplicate file entries"); |
64071805 LT |
238 | } |
239 | if (not_properly_sorted) { | |
f1f0d088 | 240 | retval = objerror(&item->object, "not properly sorted"); |
64071805 LT |
241 | } |
242 | return retval; | |
1ea34e36 LT |
243 | } |
244 | ||
c418eda4 | 245 | static int fsck_commit(struct commit *commit) |
1ea34e36 | 246 | { |
de2eb7f6 | 247 | char *buffer = commit->buffer; |
f1f0d088 | 248 | unsigned char tree_sha1[20], sha1[20]; |
de2eb7f6 LT |
249 | |
250 | if (memcmp(buffer, "tree ", 5)) | |
f1f0d088 PB |
251 | return objerror(&commit->object, "invalid format - expected 'tree' line"); |
252 | if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n') | |
253 | return objerror(&commit->object, "invalid 'tree' line format - bad sha1"); | |
de2eb7f6 LT |
254 | buffer += 46; |
255 | while (!memcmp(buffer, "parent ", 7)) { | |
256 | if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n') | |
f1f0d088 | 257 | return objerror(&commit->object, "invalid 'parent' line format - bad sha1"); |
de2eb7f6 LT |
258 | buffer += 48; |
259 | } | |
260 | if (memcmp(buffer, "author ", 7)) | |
f1f0d088 | 261 | return objerror(&commit->object, "invalid format - expected 'author' line"); |
bd1e17e2 LT |
262 | free(commit->buffer); |
263 | commit->buffer = NULL; | |
ff5ebe39 | 264 | if (!commit->tree) |
f1f0d088 | 265 | return objerror(&commit->object, "could not load commit's tree %s", tree_sha1); |
ab7df187 | 266 | if (!commit->parents && show_root) |
c418eda4 | 267 | printf("root %s\n", sha1_to_hex(commit->object.sha1)); |
e6948b6d | 268 | if (!commit->date) |
c418eda4 DB |
269 | printf("bad commit date in %s\n", |
270 | sha1_to_hex(commit->object.sha1)); | |
4728b861 LT |
271 | return 0; |
272 | } | |
273 | ||
c418eda4 | 274 | static int fsck_tag(struct tag *tag) |
ec4465ad | 275 | { |
92d4c85d LT |
276 | struct object *tagged = tag->tagged; |
277 | ||
278 | if (!tagged) { | |
f1f0d088 | 279 | return objerror(&tag->object, "could not load tagged object"); |
92d4c85d | 280 | } |
889262ea LT |
281 | if (!show_tags) |
282 | return 0; | |
283 | ||
885a86ab | 284 | printf("tagged %s %s", typename(tagged->type), sha1_to_hex(tagged->sha1)); |
92d4c85d | 285 | printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1)); |
ff5ebe39 | 286 | return 0; |
20222118 LT |
287 | } |
288 | ||
7e8c174a | 289 | static int fsck_sha1(unsigned char *sha1) |
20222118 | 290 | { |
7e8c174a LT |
291 | struct object *obj = parse_object(sha1); |
292 | if (!obj) | |
f1f0d088 | 293 | return error("%s: object not found", sha1_to_hex(sha1)); |
2d9c58c6 LT |
294 | if (obj->flags & SEEN) |
295 | return 0; | |
296 | obj->flags |= SEEN; | |
1974632c | 297 | if (obj->type == OBJ_BLOB) |
7e8c174a | 298 | return 0; |
1974632c | 299 | if (obj->type == OBJ_TREE) |
7e8c174a | 300 | return fsck_tree((struct tree *) obj); |
1974632c | 301 | if (obj->type == OBJ_COMMIT) |
7e8c174a | 302 | return fsck_commit((struct commit *) obj); |
1974632c | 303 | if (obj->type == OBJ_TAG) |
7e8c174a | 304 | return fsck_tag((struct tag *) obj); |
f1f0d088 | 305 | /* By now, parse_object() would've returned NULL instead. */ |
885a86ab | 306 | return objerror(obj, "unknown type '%d' (internal fsck error)", obj->type); |
7e8c174a LT |
307 | } |
308 | ||
309 | /* | |
310 | * This is the sorting chunk size: make it reasonably | |
311 | * big so that we can sort well.. | |
312 | */ | |
313 | #define MAX_SHA1_ENTRIES (1024) | |
314 | ||
315 | struct sha1_entry { | |
316 | unsigned long ino; | |
20222118 | 317 | unsigned char sha1[20]; |
7e8c174a LT |
318 | }; |
319 | ||
320 | static struct { | |
321 | unsigned long nr; | |
322 | struct sha1_entry *entry[MAX_SHA1_ENTRIES]; | |
323 | } sha1_list; | |
324 | ||
325 | static int ino_compare(const void *_a, const void *_b) | |
326 | { | |
327 | const struct sha1_entry *a = _a, *b = _b; | |
328 | unsigned long ino1 = a->ino, ino2 = b->ino; | |
329 | return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0; | |
330 | } | |
331 | ||
332 | static void fsck_sha1_list(void) | |
333 | { | |
334 | int i, nr = sha1_list.nr; | |
335 | ||
35a730f0 JH |
336 | if (SORT_DIRENT) |
337 | qsort(sha1_list.entry, nr, | |
338 | sizeof(struct sha1_entry *), ino_compare); | |
7e8c174a LT |
339 | for (i = 0; i < nr; i++) { |
340 | struct sha1_entry *entry = sha1_list.entry[i]; | |
341 | unsigned char *sha1 = entry->sha1; | |
342 | ||
343 | sha1_list.entry[i] = NULL; | |
f1f0d088 | 344 | fsck_sha1(sha1); |
7e8c174a | 345 | free(entry); |
20222118 | 346 | } |
7e8c174a LT |
347 | sha1_list.nr = 0; |
348 | } | |
349 | ||
350 | static void add_sha1_list(unsigned char *sha1, unsigned long ino) | |
351 | { | |
352 | struct sha1_entry *entry = xmalloc(sizeof(*entry)); | |
353 | int nr; | |
354 | ||
355 | entry->ino = ino; | |
e702496e | 356 | hashcpy(entry->sha1, sha1); |
7e8c174a LT |
357 | nr = sha1_list.nr; |
358 | if (nr == MAX_SHA1_ENTRIES) { | |
359 | fsck_sha1_list(); | |
360 | nr = 0; | |
361 | } | |
362 | sha1_list.entry[nr] = entry; | |
363 | sha1_list.nr = ++nr; | |
20222118 LT |
364 | } |
365 | ||
b5524c82 | 366 | static void fsck_dir(int i, char *path) |
20222118 LT |
367 | { |
368 | DIR *dir = opendir(path); | |
369 | struct dirent *de; | |
370 | ||
230f1322 | 371 | if (!dir) |
b5524c82 | 372 | return; |
20222118 LT |
373 | |
374 | while ((de = readdir(dir)) != NULL) { | |
375 | char name[100]; | |
7e8c174a | 376 | unsigned char sha1[20]; |
20222118 LT |
377 | int len = strlen(de->d_name); |
378 | ||
379 | switch (len) { | |
380 | case 2: | |
381 | if (de->d_name[1] != '.') | |
382 | break; | |
383 | case 1: | |
384 | if (de->d_name[0] != '.') | |
385 | break; | |
386 | continue; | |
387 | case 38: | |
388 | sprintf(name, "%02x", i); | |
389 | memcpy(name+2, de->d_name, len+1); | |
7e8c174a LT |
390 | if (get_sha1_hex(name, sha1) < 0) |
391 | break; | |
35a730f0 | 392 | add_sha1_list(sha1, DIRENT_SORT_HINT(de)); |
7e8c174a | 393 | continue; |
20222118 LT |
394 | } |
395 | fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name); | |
396 | } | |
397 | closedir(dir); | |
20222118 LT |
398 | } |
399 | ||
96f1e58f | 400 | static int default_refs; |
944d8589 | 401 | |
55dd5526 JH |
402 | static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1, char *datail, void *cb_data) |
403 | { | |
404 | struct object *obj; | |
405 | ||
406 | if (!is_null_sha1(osha1)) { | |
407 | obj = lookup_object(osha1); | |
408 | if (obj) { | |
409 | obj->used = 1; | |
410 | mark_reachable(obj, REACHABLE); | |
411 | } | |
412 | } | |
413 | obj = lookup_object(nsha1); | |
414 | if (obj) { | |
415 | obj->used = 1; | |
416 | mark_reachable(obj, REACHABLE); | |
417 | } | |
418 | return 0; | |
419 | } | |
420 | ||
8da19775 | 421 | static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
1024932f | 422 | { |
1024932f LT |
423 | struct object *obj; |
424 | ||
1024932f | 425 | obj = lookup_object(sha1); |
8a498a05 | 426 | if (!obj) { |
7aaa715d | 427 | if (has_sha1_file(sha1)) { |
659cacf5 | 428 | default_refs++; |
944d8589 | 429 | return 0; /* it is in a pack */ |
659cacf5 | 430 | } |
944d8589 LT |
431 | error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1)); |
432 | /* We'll continue with the rest despite the error.. */ | |
433 | return 0; | |
8a498a05 | 434 | } |
944d8589 | 435 | default_refs++; |
1024932f LT |
436 | obj->used = 1; |
437 | mark_reachable(obj, REACHABLE); | |
55dd5526 JH |
438 | |
439 | for_each_reflog_ent(refname, fsck_handle_reflog_ent, NULL); | |
440 | ||
7c4d07c7 | 441 | return 0; |
1024932f LT |
442 | } |
443 | ||
1024932f LT |
444 | static void get_default_heads(void) |
445 | { | |
cb5d709f | 446 | for_each_ref(fsck_handle_ref, NULL); |
071fa89e LT |
447 | |
448 | /* | |
449 | * Not having any default heads isn't really fatal, but | |
450 | * it does mean that "--unreachable" no longer makes any | |
451 | * sense (since in this case everything will obviously | |
452 | * be unreachable by definition. | |
453 | * | |
454 | * Showing dangling objects is valid, though (as those | |
455 | * dangling objects are likely lost heads). | |
456 | * | |
457 | * So we just print a warning about it, and clear the | |
458 | * "show_unreachable" flag. | |
459 | */ | |
460 | if (!default_refs) { | |
461 | error("No default references"); | |
462 | show_unreachable = 0; | |
463 | } | |
1024932f LT |
464 | } |
465 | ||
8a498a05 JH |
466 | static void fsck_object_dir(const char *path) |
467 | { | |
468 | int i; | |
469 | for (i = 0; i < 256; i++) { | |
470 | static char dir[4096]; | |
471 | sprintf(dir, "%s/%02x", path, i); | |
472 | fsck_dir(i, dir); | |
473 | } | |
474 | fsck_sha1_list(); | |
475 | } | |
476 | ||
c3330383 LT |
477 | static int fsck_head_link(void) |
478 | { | |
c3330383 | 479 | unsigned char sha1[20]; |
8da19775 JH |
480 | int flag; |
481 | const char *head_points_at = resolve_ref("HEAD", sha1, 1, &flag); | |
8098a178 | 482 | |
8da19775 | 483 | if (!head_points_at || !(flag & REF_ISSYMREF)) |
8098a178 | 484 | return error("HEAD is not a symbolic ref"); |
5b10b091 | 485 | if (strncmp(head_points_at, "refs/heads/", 11)) |
8098a178 | 486 | return error("HEAD points to something strange (%s)", |
5b10b091 | 487 | head_points_at); |
0bef57ee | 488 | if (is_null_sha1(sha1)) |
c3330383 LT |
489 | return error("HEAD: not a valid git pointer"); |
490 | return 0; | |
491 | } | |
492 | ||
53dc3f3e JH |
493 | static int fsck_cache_tree(struct cache_tree *it) |
494 | { | |
495 | int i; | |
496 | int err = 0; | |
497 | ||
498 | if (0 <= it->entry_count) { | |
499 | struct object *obj = parse_object(it->sha1); | |
6d60bbef JH |
500 | if (!obj) { |
501 | error("%s: invalid sha1 pointer in cache-tree", | |
502 | sha1_to_hex(it->sha1)); | |
503 | return 1; | |
504 | } | |
cdc08b33 JH |
505 | mark_reachable(obj, REACHABLE); |
506 | obj->used = 1; | |
1974632c | 507 | if (obj->type != OBJ_TREE) |
53dc3f3e JH |
508 | err |= objerror(obj, "non-tree in cache-tree"); |
509 | } | |
510 | for (i = 0; i < it->subtree_nr; i++) | |
511 | err |= fsck_cache_tree(it->down[i]->cache_tree); | |
512 | return err; | |
513 | } | |
514 | ||
20222118 LT |
515 | int main(int argc, char **argv) |
516 | { | |
bcee6fd8 | 517 | int i, heads; |
20222118 | 518 | |
3a7c352b | 519 | track_object_refs = 1; |
61e2b015 JH |
520 | setup_git_directory(); |
521 | ||
889262ea LT |
522 | for (i = 1; i < argc; i++) { |
523 | const char *arg = argv[i]; | |
524 | ||
525 | if (!strcmp(arg, "--unreachable")) { | |
526 | show_unreachable = 1; | |
527 | continue; | |
528 | } | |
529 | if (!strcmp(arg, "--tags")) { | |
530 | show_tags = 1; | |
531 | continue; | |
532 | } | |
ab7df187 LT |
533 | if (!strcmp(arg, "--root")) { |
534 | show_root = 1; | |
535 | continue; | |
536 | } | |
ae7c0c92 JH |
537 | if (!strcmp(arg, "--cache")) { |
538 | keep_cache_objects = 1; | |
539 | continue; | |
540 | } | |
8a498a05 JH |
541 | if (!strcmp(arg, "--full")) { |
542 | check_full = 1; | |
543 | continue; | |
544 | } | |
de2eb7f6 LT |
545 | if (!strcmp(arg, "--strict")) { |
546 | check_strict = 1; | |
547 | continue; | |
548 | } | |
889262ea | 549 | if (*arg == '-') |
7aaa715d | 550 | usage("git-fsck-objects [--tags] [--root] [[--unreachable] [--cache] [--full] [--strict] <head-sha1>*]"); |
889262ea LT |
551 | } |
552 | ||
c3330383 | 553 | fsck_head_link(); |
8a498a05 JH |
554 | fsck_object_dir(get_object_directory()); |
555 | if (check_full) { | |
d5a63b99 | 556 | struct alternate_object_database *alt; |
8a498a05 JH |
557 | struct packed_git *p; |
558 | prepare_alt_odb(); | |
d5a63b99 | 559 | for (alt = alt_odb_list; alt; alt = alt->next) { |
a3eb250f | 560 | char namebuf[PATH_MAX]; |
d5a63b99 JH |
561 | int namelen = alt->name - alt->base; |
562 | memcpy(namebuf, alt->base, namelen); | |
a3eb250f JH |
563 | namebuf[namelen - 1] = 0; |
564 | fsck_object_dir(namebuf); | |
8a498a05 JH |
565 | } |
566 | prepare_packed_git(); | |
f9253394 JH |
567 | for (p = packed_git; p; p = p->next) |
568 | /* verify gives error messages itself */ | |
f3bf9224 | 569 | verify_pack(p, 0); |
f9253394 | 570 | |
8a498a05 JH |
571 | for (p = packed_git; p; p = p->next) { |
572 | int num = num_packed_objects(p); | |
573 | for (i = 0; i < num; i++) { | |
574 | unsigned char sha1[20]; | |
575 | nth_packed_object_sha1(p, i, sha1); | |
f1f0d088 | 576 | fsck_sha1(sha1); |
8a498a05 JH |
577 | } |
578 | } | |
bcee6fd8 LT |
579 | } |
580 | ||
581 | heads = 0; | |
d9839e03 | 582 | for (i = 1; i < argc; i++) { |
889262ea LT |
583 | const char *arg = argv[i]; |
584 | ||
585 | if (*arg == '-') | |
d9839e03 | 586 | continue; |
889262ea | 587 | |
3c249c95 | 588 | if (!get_sha1(arg, head_sha1)) { |
770896e5 | 589 | struct object *obj = lookup_object(head_sha1); |
e1a1388d | 590 | |
770896e5 LT |
591 | /* Error is printed by lookup_object(). */ |
592 | if (!obj) | |
e1a1388d JF |
593 | continue; |
594 | ||
ff5ebe39 DB |
595 | obj->used = 1; |
596 | mark_reachable(obj, REACHABLE); | |
bcee6fd8 | 597 | heads++; |
d9839e03 LT |
598 | continue; |
599 | } | |
f1f0d088 | 600 | error("invalid parameter: expected sha1, got '%s'", arg); |
d9839e03 | 601 | } |
d9839e03 | 602 | |
1024932f | 603 | /* |
d1af002d | 604 | * If we've not been given any explicit head information, do the |
e7bd907d LT |
605 | * default ones from .git/refs. We also consider the index file |
606 | * in this case (ie this implies --cache). | |
1024932f | 607 | */ |
e7bd907d | 608 | if (!heads) { |
1024932f LT |
609 | get_default_heads(); |
610 | keep_cache_objects = 1; | |
611 | } | |
612 | ||
ae7c0c92 JH |
613 | if (keep_cache_objects) { |
614 | int i; | |
615 | read_cache(); | |
616 | for (i = 0; i < active_nr; i++) { | |
617 | struct blob *blob = lookup_blob(active_cache[i]->sha1); | |
618 | struct object *obj; | |
619 | if (!blob) | |
620 | continue; | |
621 | obj = &blob->object; | |
622 | obj->used = 1; | |
623 | mark_reachable(obj, REACHABLE); | |
624 | } | |
53dc3f3e JH |
625 | if (active_cache_tree) |
626 | fsck_cache_tree(active_cache_tree); | |
ae7c0c92 JH |
627 | } |
628 | ||
8ba0bbb2 | 629 | check_connectivity(); |
20222118 LT |
630 | return 0; |
631 | } |