Catch empty pathnames in trees during fsck
[git/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;
cb2cada6 221 int has_empty_name = 0;
64071805
LT
222 int has_zero_pad = 0;
223 int has_bad_modes = 0;
224 int has_dup_entries = 0;
225 int not_properly_sorted = 0;
e9a95bef
LT
226 struct tree_desc desc;
227 unsigned o_mode;
228 const char *o_name;
229 const unsigned char *o_sha1;
85003492 230
6fda5e51 231 init_tree_desc(&desc, item->buffer, item->size);
e9a95bef
LT
232
233 o_mode = 0;
234 o_name = NULL;
235 o_sha1 = NULL;
236 while (desc.size) {
237 unsigned mode;
238 const char *name;
239 const unsigned char *sha1;
240
241 sha1 = tree_entry_extract(&desc, &name, &mode);
242
243 if (strchr(name, '/'))
85003492 244 has_full_path = 1;
cb2cada6
SP
245 if (!*name)
246 has_empty_name = 1;
6fda5e51 247 has_zero_pad |= *(char *)desc.buffer == '0';
e9a95bef 248 update_tree_entry(&desc);
85003492 249
e9a95bef 250 switch (mode) {
42ea9cb2 251 /*
e9a95bef 252 * Standard modes..
42ea9cb2
LT
253 */
254 case S_IFREG | 0755:
255 case S_IFREG | 0644:
256 case S_IFLNK:
257 case S_IFDIR:
258 break;
259 /*
260 * This is nonstandard, but we had a few of these
261 * early on when we honored the full set of mode
262 * bits..
263 */
264 case S_IFREG | 0664:
de2eb7f6
LT
265 if (!check_strict)
266 break;
42ea9cb2 267 default:
64071805 268 has_bad_modes = 1;
42ea9cb2
LT
269 }
270
e9a95bef
LT
271 if (o_name) {
272 switch (verify_ordered(o_mode, o_name, mode, name)) {
a4f35a2d 273 case TREE_UNORDERED:
64071805
LT
274 not_properly_sorted = 1;
275 break;
a4f35a2d 276 case TREE_HAS_DUPS:
64071805
LT
277 has_dup_entries = 1;
278 break;
a4f35a2d
JH
279 default:
280 break;
85003492
LT
281 }
282 }
283
e9a95bef
LT
284 o_mode = mode;
285 o_name = name;
286 o_sha1 = sha1;
85003492 287 }
136f2e54
LT
288 free(item->buffer);
289 item->buffer = NULL;
85003492 290
64071805 291 retval = 0;
85003492 292 if (has_full_path) {
f1f0d088 293 objwarning(&item->object, "contains full pathnames");
1ea34e36 294 }
cb2cada6
SP
295 if (has_empty_name) {
296 objwarning(&item->object, "contains empty pathname");
297 }
64071805 298 if (has_zero_pad) {
f1f0d088 299 objwarning(&item->object, "contains zero-padded file modes");
64071805
LT
300 }
301 if (has_bad_modes) {
f1f0d088 302 objwarning(&item->object, "contains bad file modes");
64071805
LT
303 }
304 if (has_dup_entries) {
f1f0d088 305 retval = objerror(&item->object, "contains duplicate file entries");
64071805
LT
306 }
307 if (not_properly_sorted) {
f1f0d088 308 retval = objerror(&item->object, "not properly sorted");
64071805
LT
309 }
310 return retval;
1ea34e36
LT
311}
312
c418eda4 313static int fsck_commit(struct commit *commit)
1ea34e36 314{
de2eb7f6 315 char *buffer = commit->buffer;
f1f0d088 316 unsigned char tree_sha1[20], sha1[20];
de2eb7f6
LT
317
318 if (memcmp(buffer, "tree ", 5))
f1f0d088
PB
319 return objerror(&commit->object, "invalid format - expected 'tree' line");
320 if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
321 return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
de2eb7f6
LT
322 buffer += 46;
323 while (!memcmp(buffer, "parent ", 7)) {
324 if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
f1f0d088 325 return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
de2eb7f6
LT
326 buffer += 48;
327 }
328 if (memcmp(buffer, "author ", 7))
f1f0d088 329 return objerror(&commit->object, "invalid format - expected 'author' line");
bd1e17e2
LT
330 free(commit->buffer);
331 commit->buffer = NULL;
ff5ebe39 332 if (!commit->tree)
f1f0d088 333 return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
ab7df187 334 if (!commit->parents && show_root)
c418eda4 335 printf("root %s\n", sha1_to_hex(commit->object.sha1));
e6948b6d 336 if (!commit->date)
c418eda4
DB
337 printf("bad commit date in %s\n",
338 sha1_to_hex(commit->object.sha1));
4728b861
LT
339 return 0;
340}
341
c418eda4 342static int fsck_tag(struct tag *tag)
ec4465ad 343{
92d4c85d
LT
344 struct object *tagged = tag->tagged;
345
346 if (!tagged) {
f1f0d088 347 return objerror(&tag->object, "could not load tagged object");
92d4c85d 348 }
889262ea
LT
349 if (!show_tags)
350 return 0;
351
885a86ab 352 printf("tagged %s %s", typename(tagged->type), sha1_to_hex(tagged->sha1));
92d4c85d 353 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
ff5ebe39 354 return 0;
20222118
LT
355}
356
7e8c174a 357static int fsck_sha1(unsigned char *sha1)
20222118 358{
7e8c174a 359 struct object *obj = parse_object(sha1);
e2b4f635
JH
360 if (!obj) {
361 errors_found |= ERROR_OBJECT;
362 return error("%s: object corrupt or missing",
363 sha1_to_hex(sha1));
364 }
2d9c58c6
LT
365 if (obj->flags & SEEN)
366 return 0;
367 obj->flags |= SEEN;
1974632c 368 if (obj->type == OBJ_BLOB)
7e8c174a 369 return 0;
1974632c 370 if (obj->type == OBJ_TREE)
7e8c174a 371 return fsck_tree((struct tree *) obj);
1974632c 372 if (obj->type == OBJ_COMMIT)
7e8c174a 373 return fsck_commit((struct commit *) obj);
1974632c 374 if (obj->type == OBJ_TAG)
7e8c174a 375 return fsck_tag((struct tag *) obj);
e2b4f635 376
f1f0d088 377 /* By now, parse_object() would've returned NULL instead. */
e2b4f635
JH
378 return objerror(obj, "unknown type '%d' (internal fsck error)",
379 obj->type);
7e8c174a
LT
380}
381
382/*
383 * This is the sorting chunk size: make it reasonably
384 * big so that we can sort well..
385 */
386#define MAX_SHA1_ENTRIES (1024)
387
388struct sha1_entry {
389 unsigned long ino;
20222118 390 unsigned char sha1[20];
7e8c174a
LT
391};
392
393static struct {
394 unsigned long nr;
395 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
396} sha1_list;
397
398static int ino_compare(const void *_a, const void *_b)
399{
400 const struct sha1_entry *a = _a, *b = _b;
401 unsigned long ino1 = a->ino, ino2 = b->ino;
402 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
403}
404
405static void fsck_sha1_list(void)
406{
407 int i, nr = sha1_list.nr;
408
35a730f0
JH
409 if (SORT_DIRENT)
410 qsort(sha1_list.entry, nr,
411 sizeof(struct sha1_entry *), ino_compare);
7e8c174a
LT
412 for (i = 0; i < nr; i++) {
413 struct sha1_entry *entry = sha1_list.entry[i];
414 unsigned char *sha1 = entry->sha1;
415
416 sha1_list.entry[i] = NULL;
f1f0d088 417 fsck_sha1(sha1);
7e8c174a 418 free(entry);
20222118 419 }
7e8c174a
LT
420 sha1_list.nr = 0;
421}
422
423static void add_sha1_list(unsigned char *sha1, unsigned long ino)
424{
425 struct sha1_entry *entry = xmalloc(sizeof(*entry));
426 int nr;
427
428 entry->ino = ino;
e702496e 429 hashcpy(entry->sha1, sha1);
7e8c174a
LT
430 nr = sha1_list.nr;
431 if (nr == MAX_SHA1_ENTRIES) {
432 fsck_sha1_list();
433 nr = 0;
434 }
435 sha1_list.entry[nr] = entry;
436 sha1_list.nr = ++nr;
20222118
LT
437}
438
b5524c82 439static void fsck_dir(int i, char *path)
20222118
LT
440{
441 DIR *dir = opendir(path);
442 struct dirent *de;
443
230f1322 444 if (!dir)
b5524c82 445 return;
20222118
LT
446
447 while ((de = readdir(dir)) != NULL) {
448 char name[100];
7e8c174a 449 unsigned char sha1[20];
20222118
LT
450 int len = strlen(de->d_name);
451
452 switch (len) {
453 case 2:
454 if (de->d_name[1] != '.')
455 break;
456 case 1:
457 if (de->d_name[0] != '.')
458 break;
459 continue;
460 case 38:
461 sprintf(name, "%02x", i);
462 memcpy(name+2, de->d_name, len+1);
7e8c174a
LT
463 if (get_sha1_hex(name, sha1) < 0)
464 break;
35a730f0 465 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
7e8c174a 466 continue;
20222118
LT
467 }
468 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
469 }
470 closedir(dir);
20222118
LT
471}
472
96f1e58f 473static int default_refs;
944d8589 474
883d60fa
JS
475static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
476 const char *email, unsigned long timestamp, int tz,
477 const char *message, void *cb_data)
55dd5526
JH
478{
479 struct object *obj;
480
481 if (!is_null_sha1(osha1)) {
482 obj = lookup_object(osha1);
483 if (obj) {
484 obj->used = 1;
485 mark_reachable(obj, REACHABLE);
486 }
487 }
488 obj = lookup_object(nsha1);
489 if (obj) {
490 obj->used = 1;
491 mark_reachable(obj, REACHABLE);
492 }
493 return 0;
494}
495
eb8381c8
NP
496static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
497{
498 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
499 return 0;
500}
501
8da19775 502static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1024932f 503{
1024932f
LT
504 struct object *obj;
505
1024932f 506 obj = lookup_object(sha1);
8a498a05 507 if (!obj) {
7aaa715d 508 if (has_sha1_file(sha1)) {
659cacf5 509 default_refs++;
944d8589 510 return 0; /* it is in a pack */
659cacf5 511 }
944d8589
LT
512 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
513 /* We'll continue with the rest despite the error.. */
514 return 0;
8a498a05 515 }
944d8589 516 default_refs++;
1024932f
LT
517 obj->used = 1;
518 mark_reachable(obj, REACHABLE);
55dd5526 519
7c4d07c7 520 return 0;
1024932f
LT
521}
522
1024932f
LT
523static void get_default_heads(void)
524{
cb5d709f 525 for_each_ref(fsck_handle_ref, NULL);
eb8381c8 526 for_each_reflog(fsck_handle_reflog, NULL);
071fa89e
LT
527
528 /*
529 * Not having any default heads isn't really fatal, but
530 * it does mean that "--unreachable" no longer makes any
531 * sense (since in this case everything will obviously
532 * be unreachable by definition.
533 *
534 * Showing dangling objects is valid, though (as those
535 * dangling objects are likely lost heads).
536 *
537 * So we just print a warning about it, and clear the
538 * "show_unreachable" flag.
539 */
540 if (!default_refs) {
8eb2d0be 541 fprintf(stderr, "notice: No default references\n");
071fa89e
LT
542 show_unreachable = 0;
543 }
1024932f
LT
544}
545
8a498a05
JH
546static void fsck_object_dir(const char *path)
547{
548 int i;
549 for (i = 0; i < 256; i++) {
550 static char dir[4096];
551 sprintf(dir, "%s/%02x", path, i);
552 fsck_dir(i, dir);
553 }
554 fsck_sha1_list();
555}
556
c3330383
LT
557static int fsck_head_link(void)
558{
c3330383 559 unsigned char sha1[20];
8da19775 560 int flag;
8eb2d0be
JH
561 int null_is_error = 0;
562 const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
563
564 if (!head_points_at)
565 return error("Invalid HEAD");
566 if (!strcmp(head_points_at, "HEAD"))
567 /* detached HEAD */
568 null_is_error = 1;
569 else if (prefixcmp(head_points_at, "refs/heads/"))
8098a178 570 return error("HEAD points to something strange (%s)",
5b10b091 571 head_points_at);
8eb2d0be
JH
572 if (is_null_sha1(sha1)) {
573 if (null_is_error)
574 return error("HEAD: detached HEAD points at nothing");
575 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
576 head_points_at + 11);
577 }
c3330383
LT
578 return 0;
579}
580
53dc3f3e
JH
581static int fsck_cache_tree(struct cache_tree *it)
582{
583 int i;
584 int err = 0;
585
586 if (0 <= it->entry_count) {
587 struct object *obj = parse_object(it->sha1);
6d60bbef
JH
588 if (!obj) {
589 error("%s: invalid sha1 pointer in cache-tree",
590 sha1_to_hex(it->sha1));
591 return 1;
592 }
cdc08b33
JH
593 mark_reachable(obj, REACHABLE);
594 obj->used = 1;
1974632c 595 if (obj->type != OBJ_TREE)
53dc3f3e
JH
596 err |= objerror(obj, "non-tree in cache-tree");
597 }
598 for (i = 0; i < it->subtree_nr; i++)
599 err |= fsck_cache_tree(it->down[i]->cache_tree);
600 return err;
601}
602
e2b4f635
JH
603static const char fsck_usage[] =
604"git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] "
605"[--strict] <head-sha1>*]";
606
b4dfefe0 607int cmd_fsck(int argc, char **argv, const char *prefix)
20222118 608{
bcee6fd8 609 int i, heads;
20222118 610
3a7c352b 611 track_object_refs = 1;
e2b4f635 612 errors_found = 0;
61e2b015 613
889262ea
LT
614 for (i = 1; i < argc; i++) {
615 const char *arg = argv[i];
616
617 if (!strcmp(arg, "--unreachable")) {
618 show_unreachable = 1;
619 continue;
620 }
621 if (!strcmp(arg, "--tags")) {
622 show_tags = 1;
623 continue;
624 }
ab7df187
LT
625 if (!strcmp(arg, "--root")) {
626 show_root = 1;
627 continue;
628 }
ae7c0c92
JH
629 if (!strcmp(arg, "--cache")) {
630 keep_cache_objects = 1;
631 continue;
632 }
8a498a05
JH
633 if (!strcmp(arg, "--full")) {
634 check_full = 1;
635 continue;
636 }
de2eb7f6
LT
637 if (!strcmp(arg, "--strict")) {
638 check_strict = 1;
639 continue;
640 }
889262ea 641 if (*arg == '-')
e2b4f635 642 usage(fsck_usage);
889262ea
LT
643 }
644
c3330383 645 fsck_head_link();
8a498a05
JH
646 fsck_object_dir(get_object_directory());
647 if (check_full) {
d5a63b99 648 struct alternate_object_database *alt;
8a498a05
JH
649 struct packed_git *p;
650 prepare_alt_odb();
d5a63b99 651 for (alt = alt_odb_list; alt; alt = alt->next) {
a3eb250f 652 char namebuf[PATH_MAX];
d5a63b99
JH
653 int namelen = alt->name - alt->base;
654 memcpy(namebuf, alt->base, namelen);
a3eb250f
JH
655 namebuf[namelen - 1] = 0;
656 fsck_object_dir(namebuf);
8a498a05
JH
657 }
658 prepare_packed_git();
f9253394
JH
659 for (p = packed_git; p; p = p->next)
660 /* verify gives error messages itself */
f3bf9224 661 verify_pack(p, 0);
f9253394 662
8a498a05 663 for (p = packed_git; p; p = p->next) {
326bf396 664 uint32_t i, num = num_packed_objects(p);
8a498a05
JH
665 for (i = 0; i < num; i++) {
666 unsigned char sha1[20];
667 nth_packed_object_sha1(p, i, sha1);
f1f0d088 668 fsck_sha1(sha1);
8a498a05
JH
669 }
670 }
bcee6fd8
LT
671 }
672
673 heads = 0;
d9839e03 674 for (i = 1; i < argc; i++) {
889262ea
LT
675 const char *arg = argv[i];
676
677 if (*arg == '-')
d9839e03 678 continue;
889262ea 679
3c249c95 680 if (!get_sha1(arg, head_sha1)) {
770896e5 681 struct object *obj = lookup_object(head_sha1);
e1a1388d 682
770896e5
LT
683 /* Error is printed by lookup_object(). */
684 if (!obj)
e1a1388d
JF
685 continue;
686
ff5ebe39
DB
687 obj->used = 1;
688 mark_reachable(obj, REACHABLE);
bcee6fd8 689 heads++;
d9839e03
LT
690 continue;
691 }
f1f0d088 692 error("invalid parameter: expected sha1, got '%s'", arg);
d9839e03 693 }
d9839e03 694
1024932f 695 /*
d1af002d 696 * If we've not been given any explicit head information, do the
e7bd907d
LT
697 * default ones from .git/refs. We also consider the index file
698 * in this case (ie this implies --cache).
1024932f 699 */
e7bd907d 700 if (!heads) {
1024932f
LT
701 get_default_heads();
702 keep_cache_objects = 1;
703 }
704
ae7c0c92
JH
705 if (keep_cache_objects) {
706 int i;
707 read_cache();
708 for (i = 0; i < active_nr; i++) {
709 struct blob *blob = lookup_blob(active_cache[i]->sha1);
710 struct object *obj;
711 if (!blob)
712 continue;
713 obj = &blob->object;
714 obj->used = 1;
715 mark_reachable(obj, REACHABLE);
716 }
53dc3f3e
JH
717 if (active_cache_tree)
718 fsck_cache_tree(active_cache_tree);
ae7c0c92
JH
719 }
720
8ba0bbb2 721 check_connectivity();
e2b4f635 722 return errors_found;
20222118 723}