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