]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fsck.c
"log --cherry-pick" documentation regression fix
[thirdparty/git.git] / builtin / fsck.c
CommitLineData
baffc0e7 1#include "builtin.h"
4b182421 2#include "cache.h"
ff5ebe39
DB
3#include "commit.h"
4#include "tree.h"
5#include "blob.h"
c418eda4 6#include "tag.h"
944d8589 7#include "refs.h"
f9253394 8#include "pack.h"
53dc3f3e 9#include "cache-tree.h"
e9a95bef 10#include "tree-walk.h"
271b8d25 11#include "fsck.h"
5ac0a206 12#include "parse-options.h"
8ca12c0d 13#include "dir.h"
ff5ebe39
DB
14
15#define REACHABLE 0x0001
2d9c58c6 16#define SEEN 0x0002
d9839e03 17
96f1e58f
DR
18static int show_root;
19static int show_tags;
20static int show_unreachable;
566842f6 21static int include_reflogs = 1;
f29cd393 22static int check_full = 1;
96f1e58f
DR
23static int check_strict;
24static int keep_cache_objects;
d9839e03 25static unsigned char head_sha1[20];
469e2ebf 26static const char *head_points_at;
e2b4f635 27static int errors_found;
68f6c019 28static int write_lost_and_found;
20f1eb6b 29static int verbose;
e2b4f635
JH
30#define ERROR_OBJECT 01
31#define ERROR_REACHABLE 02
d9839e03 32
962554c6 33#ifdef NO_D_INO_IN_DIRENT
35a730f0
JH
34#define SORT_DIRENT 0
35#define DIRENT_SORT_HINT(de) 0
36#else
37#define SORT_DIRENT 1
38#define DIRENT_SORT_HINT(de) ((de)->d_ino)
39#endif
f1f0d088
PB
40
41static void objreport(struct object *obj, const char *severity,
42 const char *err, va_list params)
43{
44 fprintf(stderr, "%s in %s %s: ",
885a86ab 45 severity, typename(obj->type), sha1_to_hex(obj->sha1));
f1f0d088
PB
46 vfprintf(stderr, err, params);
47 fputs("\n", stderr);
48}
49
28bea9e5 50__attribute__((format (printf, 2, 3)))
a7928f8e 51static int objerror(struct object *obj, const char *err, ...)
f1f0d088
PB
52{
53 va_list params;
54 va_start(params, err);
e2b4f635 55 errors_found |= ERROR_OBJECT;
f1f0d088
PB
56 objreport(obj, "error", err, params);
57 va_end(params);
58 return -1;
59}
60
28bea9e5 61__attribute__((format (printf, 3, 4)))
ba002f3b 62static int fsck_error_func(struct object *obj, int type, const char *err, ...)
f1f0d088
PB
63{
64 va_list params;
65 va_start(params, err);
ba002f3b 66 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params);
f1f0d088 67 va_end(params);
ba002f3b 68 return (type == FSCK_WARN) ? 0 : 1;
f1f0d088
PB
69}
70
04d39759
LT
71static struct object_array pending;
72
271b8d25
MK
73static int mark_object(struct object *obj, int type, void *data)
74{
271b8d25 75 struct object *parent = data;
271b8d25 76
a1cdc251
JH
77 /*
78 * The only case data is NULL or type is OBJ_ANY is when
79 * mark_object_reachable() calls us. All the callers of
80 * that function has non-NULL obj hence ...
81 */
271b8d25 82 if (!obj) {
a1cdc251 83 /* ... these references to parent->fld are safe here */
271b8d25
MK
84 printf("broken link from %7s %s\n",
85 typename(parent->type), sha1_to_hex(parent->sha1));
86 printf("broken link from %7s %s\n",
87 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
88 errors_found |= ERROR_REACHABLE;
89 return 1;
90 }
91
92 if (type != OBJ_ANY && obj->type != type)
a1cdc251 93 /* ... and the reference to parent is safe here */
271b8d25
MK
94 objerror(parent, "wrong object type in link");
95
96 if (obj->flags & REACHABLE)
97 return 0;
98 obj->flags |= REACHABLE;
99 if (!obj->parsed) {
100 if (parent && !has_sha1_file(obj->sha1)) {
101 printf("broken link from %7s %s\n",
102 typename(parent->type), sha1_to_hex(parent->sha1));
103 printf(" to %7s %s\n",
104 typename(obj->type), sha1_to_hex(obj->sha1));
105 errors_found |= ERROR_REACHABLE;
106 }
107 return 1;
108 }
109
04d39759
LT
110 add_object_array(obj, (void *) parent, &pending);
111 return 0;
112}
113
114static void mark_object_reachable(struct object *obj)
115{
2af202be 116 mark_object(obj, OBJ_ANY, NULL);
04d39759
LT
117}
118
a1cdc251 119static int traverse_one_object(struct object *obj)
04d39759
LT
120{
121 int result;
122 struct tree *tree = NULL;
123
271b8d25
MK
124 if (obj->type == OBJ_TREE) {
125 obj->parsed = 0;
126 tree = (struct tree *)obj;
127 if (parse_tree(tree) < 0)
128 return 1; /* error already displayed */
129 }
130 result = fsck_walk(obj, mark_object, obj);
131 if (tree) {
132 free(tree->buffer);
133 tree->buffer = NULL;
134 }
271b8d25
MK
135 return result;
136}
137
04d39759 138static int traverse_reachable(void)
271b8d25 139{
04d39759
LT
140 int result = 0;
141 while (pending.nr) {
142 struct object_array_entry *entry;
143 struct object *obj, *parent;
144
145 entry = pending.objects + --pending.nr;
146 obj = entry->item;
147 parent = (struct object *) entry->name;
a1cdc251 148 result |= traverse_one_object(obj);
04d39759
LT
149 }
150 return !!result;
271b8d25
MK
151}
152
153static int mark_used(struct object *obj, int type, void *data)
154{
155 if (!obj)
156 return 1;
157 obj->used = 1;
158 return 0;
f1f0d088
PB
159}
160
18af29f2
LT
161/*
162 * Check a single reachable object
163 */
164static void check_reachable_object(struct object *obj)
165{
18af29f2
LT
166 /*
167 * We obviously want the object to be parsed,
168 * except if it was in a pack-file and we didn't
169 * do a full fsck
170 */
171 if (!obj->parsed) {
cd673c1f 172 if (has_sha1_pack(obj->sha1))
18af29f2
LT
173 return; /* it is in pack - forget about it */
174 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
e2b4f635 175 errors_found |= ERROR_REACHABLE;
18af29f2
LT
176 return;
177 }
18af29f2
LT
178}
179
180/*
181 * Check a single unreachable object
182 */
183static void check_unreachable_object(struct object *obj)
184{
185 /*
186 * Missing unreachable object? Ignore it. It's not like
187 * we miss it (since it can't be reached), nor do we want
188 * to complain about it being unreachable (since it does
189 * not exist).
190 */
191 if (!obj->parsed)
192 return;
193
194 /*
195 * Unreachable object that exists? Show it if asked to,
196 * since this is something that is prunable.
197 */
198 if (show_unreachable) {
199 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
200 return;
201 }
202
203 /*
204 * "!used" means that nothing at all points to it, including
3dff5379 205 * other unreachable objects. In other words, it's the "tip"
18af29f2
LT
206 * of some set of unreachable objects, usually a commit that
207 * got dropped.
208 *
209 * Such starting points are more interesting than some random
210 * set of unreachable objects, so we show them even if the user
211 * hasn't asked for _all_ unreachable objects. If you have
212 * deleted a branch by mistake, this is a prime candidate to
213 * start looking at, for example.
214 */
215 if (!obj->used) {
216 printf("dangling %s %s\n", typename(obj->type),
217 sha1_to_hex(obj->sha1));
68f6c019
JS
218 if (write_lost_and_found) {
219 char *filename = git_path("lost-found/%s/%s",
220 obj->type == OBJ_COMMIT ? "commit" : "other",
221 sha1_to_hex(obj->sha1));
222 FILE *f;
223
224 if (safe_create_leading_directories(filename)) {
225 error("Could not create lost-found");
226 return;
227 }
228 if (!(f = fopen(filename, "w")))
0721c314 229 die_errno("Could not open '%s'", filename);
16a7fcfe
JS
230 if (obj->type == OBJ_BLOB) {
231 enum object_type type;
232 unsigned long size;
233 char *buf = read_sha1_file(obj->sha1,
234 &type, &size);
235 if (buf) {
47d32af2 236 if (fwrite(buf, size, 1, f) != 1)
d824cbba
TR
237 die_errno("Could not write '%s'",
238 filename);
16a7fcfe
JS
239 free(buf);
240 }
241 } else
242 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
47d32af2 243 if (fclose(f))
d824cbba
TR
244 die_errno("Could not finish '%s'",
245 filename);
68f6c019 246 }
18af29f2
LT
247 return;
248 }
249
250 /*
251 * Otherwise? It's there, it's unreachable, and some other unreachable
252 * object points to it. Ignore it - it's not interesting, and we showed
253 * all the interesting cases above.
254 */
255}
256
257static void check_object(struct object *obj)
258{
20f1eb6b
JS
259 if (verbose)
260 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
261
18af29f2
LT
262 if (obj->flags & REACHABLE)
263 check_reachable_object(obj);
264 else
265 check_unreachable_object(obj);
266}
f1f0d088 267
8ba0bbb2
LT
268static void check_connectivity(void)
269{
fc046a75 270 int i, max;
8ba0bbb2 271
04d39759
LT
272 /* Traverse the pending reachable objects */
273 traverse_reachable();
274
8ba0bbb2 275 /* Look up all the requirements, warn about missing objects.. */
fc046a75 276 max = get_max_object_index();
20f1eb6b
JS
277 if (verbose)
278 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
279
fc046a75 280 for (i = 0; i < max; i++) {
fc046a75 281 struct object *obj = get_indexed_object(i);
8ba0bbb2 282
18af29f2
LT
283 if (obj)
284 check_object(obj);
8ba0bbb2
LT
285 }
286}
287
ba002f3b 288static int fsck_sha1(const unsigned char *sha1)
85003492 289{
ba002f3b
MK
290 struct object *obj = parse_object(sha1);
291 if (!obj) {
292 errors_found |= ERROR_OBJECT;
293 return error("%s: object corrupt or missing",
294 sha1_to_hex(sha1));
295 }
296 if (obj->flags & SEEN)
85003492 297 return 0;
ba002f3b 298 obj->flags |= SEEN;
85003492 299
20f1eb6b 300 if (verbose)
ba002f3b
MK
301 fprintf(stderr, "Checking %s %s\n",
302 typename(obj->type), sha1_to_hex(obj->sha1));
42ea9cb2 303
2af202be 304 if (fsck_walk(obj, mark_used, NULL))
ba002f3b
MK
305 objerror(obj, "broken links");
306 if (fsck_object(obj, check_strict, fsck_error_func))
307 return -1;
85003492 308
ba002f3b
MK
309 if (obj->type == OBJ_TREE) {
310 struct tree *item = (struct tree *) obj;
85003492 311
ba002f3b
MK
312 free(item->buffer);
313 item->buffer = NULL;
1ea34e36 314 }
1ea34e36 315
ba002f3b
MK
316 if (obj->type == OBJ_COMMIT) {
317 struct commit *commit = (struct commit *) obj;
de2eb7f6 318
ba002f3b
MK
319 free(commit->buffer);
320 commit->buffer = NULL;
4728b861 321
ba002f3b
MK
322 if (!commit->parents && show_root)
323 printf("root %s\n", sha1_to_hex(commit->object.sha1));
324 }
92d4c85d 325
ba002f3b
MK
326 if (obj->type == OBJ_TAG) {
327 struct tag *tag = (struct tag *) obj;
20f1eb6b 328
ba002f3b
MK
329 if (show_tags && tag->tagged) {
330 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
331 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
332 }
92d4c85d 333 }
889262ea 334
ff5ebe39 335 return 0;
20222118
LT
336}
337
7e8c174a
LT
338/*
339 * This is the sorting chunk size: make it reasonably
340 * big so that we can sort well..
341 */
342#define MAX_SHA1_ENTRIES (1024)
343
344struct sha1_entry {
345 unsigned long ino;
20222118 346 unsigned char sha1[20];
7e8c174a
LT
347};
348
349static struct {
350 unsigned long nr;
351 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
352} sha1_list;
353
354static int ino_compare(const void *_a, const void *_b)
355{
356 const struct sha1_entry *a = _a, *b = _b;
357 unsigned long ino1 = a->ino, ino2 = b->ino;
358 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
359}
360
361static void fsck_sha1_list(void)
362{
363 int i, nr = sha1_list.nr;
364
35a730f0
JH
365 if (SORT_DIRENT)
366 qsort(sha1_list.entry, nr,
367 sizeof(struct sha1_entry *), ino_compare);
7e8c174a
LT
368 for (i = 0; i < nr; i++) {
369 struct sha1_entry *entry = sha1_list.entry[i];
370 unsigned char *sha1 = entry->sha1;
371
372 sha1_list.entry[i] = NULL;
f1f0d088 373 fsck_sha1(sha1);
7e8c174a 374 free(entry);
20222118 375 }
7e8c174a
LT
376 sha1_list.nr = 0;
377}
378
379static void add_sha1_list(unsigned char *sha1, unsigned long ino)
380{
381 struct sha1_entry *entry = xmalloc(sizeof(*entry));
382 int nr;
383
384 entry->ino = ino;
e702496e 385 hashcpy(entry->sha1, sha1);
7e8c174a
LT
386 nr = sha1_list.nr;
387 if (nr == MAX_SHA1_ENTRIES) {
388 fsck_sha1_list();
389 nr = 0;
390 }
391 sha1_list.entry[nr] = entry;
392 sha1_list.nr = ++nr;
20222118
LT
393}
394
ea6f0a23
JH
395static inline int is_loose_object_file(struct dirent *de,
396 char *name, unsigned char *sha1)
397{
398 if (strlen(de->d_name) != 38)
399 return 0;
400 memcpy(name + 2, de->d_name, 39);
401 return !get_sha1_hex(name, sha1);
402}
403
b5524c82 404static void fsck_dir(int i, char *path)
20222118
LT
405{
406 DIR *dir = opendir(path);
407 struct dirent *de;
ea6f0a23 408 char name[100];
20222118 409
230f1322 410 if (!dir)
b5524c82 411 return;
20222118 412
20f1eb6b
JS
413 if (verbose)
414 fprintf(stderr, "Checking directory %s\n", path);
415
ea6f0a23 416 sprintf(name, "%02x", i);
20222118 417 while ((de = readdir(dir)) != NULL) {
7e8c174a 418 unsigned char sha1[20];
20222118 419
8ca12c0d 420 if (is_dot_or_dotdot(de->d_name))
20222118 421 continue;
ea6f0a23 422 if (is_loose_object_file(de, name, sha1)) {
35a730f0 423 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
7e8c174a 424 continue;
20222118 425 }
3d32a46b 426 if (!prefixcmp(de->d_name, "tmp_obj_"))
a08c53a1 427 continue;
20222118
LT
428 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
429 }
430 closedir(dir);
20222118
LT
431}
432
96f1e58f 433static int default_refs;
944d8589 434
883d60fa
JS
435static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
436 const char *email, unsigned long timestamp, int tz,
437 const char *message, void *cb_data)
55dd5526
JH
438{
439 struct object *obj;
440
20f1eb6b
JS
441 if (verbose)
442 fprintf(stderr, "Checking reflog %s->%s\n",
443 sha1_to_hex(osha1), sha1_to_hex(nsha1));
444
55dd5526
JH
445 if (!is_null_sha1(osha1)) {
446 obj = lookup_object(osha1);
447 if (obj) {
448 obj->used = 1;
271b8d25 449 mark_object_reachable(obj);
55dd5526
JH
450 }
451 }
452 obj = lookup_object(nsha1);
453 if (obj) {
454 obj->used = 1;
271b8d25 455 mark_object_reachable(obj);
55dd5526
JH
456 }
457 return 0;
458}
459
eb8381c8
NP
460static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
461{
462 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
463 return 0;
464}
465
6232f62b
LT
466static int is_branch(const char *refname)
467{
468 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
469}
470
8da19775 471static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
1024932f 472{
1024932f
LT
473 struct object *obj;
474
6232f62b 475 obj = parse_object(sha1);
8a498a05 476 if (!obj) {
944d8589
LT
477 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
478 /* We'll continue with the rest despite the error.. */
479 return 0;
8a498a05 480 }
6232f62b
LT
481 if (obj->type != OBJ_COMMIT && is_branch(refname))
482 error("%s: not a commit", refname);
944d8589 483 default_refs++;
1024932f 484 obj->used = 1;
271b8d25 485 mark_object_reachable(obj);
55dd5526 486
7c4d07c7 487 return 0;
1024932f
LT
488}
489
1024932f
LT
490static void get_default_heads(void)
491{
469e2ebf
JH
492 if (head_points_at && !is_null_sha1(head_sha1))
493 fsck_handle_ref("HEAD", head_sha1, 0, NULL);
cb5d709f 494 for_each_ref(fsck_handle_ref, NULL);
566842f6
SP
495 if (include_reflogs)
496 for_each_reflog(fsck_handle_reflog, NULL);
071fa89e
LT
497
498 /*
499 * Not having any default heads isn't really fatal, but
500 * it does mean that "--unreachable" no longer makes any
501 * sense (since in this case everything will obviously
502 * be unreachable by definition.
503 *
504 * Showing dangling objects is valid, though (as those
505 * dangling objects are likely lost heads).
506 *
507 * So we just print a warning about it, and clear the
508 * "show_unreachable" flag.
509 */
510 if (!default_refs) {
8eb2d0be 511 fprintf(stderr, "notice: No default references\n");
071fa89e
LT
512 show_unreachable = 0;
513 }
1024932f
LT
514}
515
8a498a05
JH
516static void fsck_object_dir(const char *path)
517{
518 int i;
20f1eb6b
JS
519
520 if (verbose)
521 fprintf(stderr, "Checking object directory\n");
522
8a498a05
JH
523 for (i = 0; i < 256; i++) {
524 static char dir[4096];
525 sprintf(dir, "%s/%02x", path, i);
526 fsck_dir(i, dir);
527 }
528 fsck_sha1_list();
529}
530
c3330383
LT
531static int fsck_head_link(void)
532{
8da19775 533 int flag;
8eb2d0be 534 int null_is_error = 0;
8eb2d0be 535
20f1eb6b
JS
536 if (verbose)
537 fprintf(stderr, "Checking HEAD link\n");
538
469e2ebf 539 head_points_at = resolve_ref("HEAD", head_sha1, 0, &flag);
8eb2d0be
JH
540 if (!head_points_at)
541 return error("Invalid HEAD");
542 if (!strcmp(head_points_at, "HEAD"))
543 /* detached HEAD */
544 null_is_error = 1;
545 else if (prefixcmp(head_points_at, "refs/heads/"))
8098a178 546 return error("HEAD points to something strange (%s)",
5b10b091 547 head_points_at);
469e2ebf 548 if (is_null_sha1(head_sha1)) {
8eb2d0be
JH
549 if (null_is_error)
550 return error("HEAD: detached HEAD points at nothing");
551 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
552 head_points_at + 11);
553 }
c3330383
LT
554 return 0;
555}
556
53dc3f3e
JH
557static int fsck_cache_tree(struct cache_tree *it)
558{
559 int i;
560 int err = 0;
561
20f1eb6b
JS
562 if (verbose)
563 fprintf(stderr, "Checking cache tree\n");
564
53dc3f3e
JH
565 if (0 <= it->entry_count) {
566 struct object *obj = parse_object(it->sha1);
6d60bbef
JH
567 if (!obj) {
568 error("%s: invalid sha1 pointer in cache-tree",
569 sha1_to_hex(it->sha1));
570 return 1;
571 }
cdc08b33 572 obj->used = 1;
a1cdc251 573 mark_object_reachable(obj);
1974632c 574 if (obj->type != OBJ_TREE)
53dc3f3e
JH
575 err |= objerror(obj, "non-tree in cache-tree");
576 }
577 for (i = 0; i < it->subtree_nr; i++)
578 err |= fsck_cache_tree(it->down[i]->cache_tree);
579 return err;
580}
581
5ac0a206 582static char const * const fsck_usage[] = {
1b1dd23f 583 "git fsck [options] [<object>...]",
5ac0a206
PH
584 NULL
585};
586
587static struct option fsck_opts[] = {
fd03881a 588 OPT__VERBOSE(&verbose, "be verbose"),
5ac0a206
PH
589 OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
590 OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
591 OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
592 OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
593 OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
2b26e0e1 594 OPT_BOOLEAN(0, "full", &check_full, "also consider packs and alternate objects"),
dd46b9b9 595 OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
5ac0a206
PH
596 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
597 "write dangling objects in .git/lost-found"),
598 OPT_END(),
599};
e2b4f635 600
baffc0e7 601int cmd_fsck(int argc, const char **argv, const char *prefix)
20222118 602{
bcee6fd8 603 int i, heads;
e15ef669 604 struct alternate_object_database *alt;
20222118 605
e2b4f635 606 errors_found = 0;
dae556bd 607 read_replace_refs = 0;
61e2b015 608
37782920 609 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
5ac0a206
PH
610 if (write_lost_and_found) {
611 check_full = 1;
612 include_reflogs = 0;
889262ea
LT
613 }
614
c3330383 615 fsck_head_link();
8a498a05 616 fsck_object_dir(get_object_directory());
e15ef669
JH
617
618 prepare_alt_odb();
619 for (alt = alt_odb_list; alt; alt = alt->next) {
620 char namebuf[PATH_MAX];
621 int namelen = alt->name - alt->base;
622 memcpy(namebuf, alt->base, namelen);
623 namebuf[namelen - 1] = 0;
624 fsck_object_dir(namebuf);
625 }
626
8a498a05 627 if (check_full) {
8a498a05 628 struct packed_git *p;
e15ef669 629
8a498a05 630 prepare_packed_git();
f9253394
JH
631 for (p = packed_git; p; p = p->next)
632 /* verify gives error messages itself */
77d3ecee 633 verify_pack(p);
f9253394 634
8a498a05 635 for (p = packed_git; p; p = p->next) {
5ac0a206 636 uint32_t j, num;
b77ffe8a
SP
637 if (open_pack_index(p))
638 continue;
639 num = p->num_objects;
5ac0a206
PH
640 for (j = 0; j < num; j++)
641 fsck_sha1(nth_packed_object_sha1(p, j));
8a498a05 642 }
bcee6fd8
LT
643 }
644
645 heads = 0;
3aed2fda 646 for (i = 0; i < argc; i++) {
a6080a0a 647 const char *arg = argv[i];
469e2ebf
JH
648 unsigned char sha1[20];
649 if (!get_sha1(arg, sha1)) {
650 struct object *obj = lookup_object(sha1);
e1a1388d 651
770896e5
LT
652 /* Error is printed by lookup_object(). */
653 if (!obj)
e1a1388d
JF
654 continue;
655
ff5ebe39 656 obj->used = 1;
271b8d25 657 mark_object_reachable(obj);
bcee6fd8 658 heads++;
d9839e03
LT
659 continue;
660 }
f1f0d088 661 error("invalid parameter: expected sha1, got '%s'", arg);
d9839e03 662 }
d9839e03 663
1024932f 664 /*
d1af002d 665 * If we've not been given any explicit head information, do the
e7bd907d
LT
666 * default ones from .git/refs. We also consider the index file
667 * in this case (ie this implies --cache).
1024932f 668 */
e7bd907d 669 if (!heads) {
1024932f
LT
670 get_default_heads();
671 keep_cache_objects = 1;
672 }
673
ae7c0c92 674 if (keep_cache_objects) {
ae7c0c92
JH
675 read_cache();
676 for (i = 0; i < active_nr; i++) {
8d9721c8
LT
677 unsigned int mode;
678 struct blob *blob;
ae7c0c92 679 struct object *obj;
8d9721c8 680
7a51ed66 681 mode = active_cache[i]->ce_mode;
302b9282 682 if (S_ISGITLINK(mode))
8d9721c8
LT
683 continue;
684 blob = lookup_blob(active_cache[i]->sha1);
ae7c0c92
JH
685 if (!blob)
686 continue;
687 obj = &blob->object;
688 obj->used = 1;
271b8d25 689 mark_object_reachable(obj);
ae7c0c92 690 }
53dc3f3e
JH
691 if (active_cache_tree)
692 fsck_cache_tree(active_cache_tree);
ae7c0c92
JH
693 }
694
8ba0bbb2 695 check_connectivity();
e2b4f635 696 return errors_found;
20222118 697}