]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fsck.c
Merge branch 'js/mingw-res-rebuild'
[thirdparty/git.git] / builtin / fsck.c
CommitLineData
baffc0e7 1#include "builtin.h"
4b182421 2#include "cache.h"
031dc927 3#include "repository.h"
b2141fc1 4#include "config.h"
ff5ebe39
DB
5#include "commit.h"
6#include "tree.h"
7#include "blob.h"
c418eda4 8#include "tag.h"
944d8589 9#include "refs.h"
f9253394 10#include "pack.h"
53dc3f3e 11#include "cache-tree.h"
e9a95bef 12#include "tree-walk.h"
271b8d25 13#include "fsck.h"
5ac0a206 14#include "parse-options.h"
8ca12c0d 15#include "dir.h"
1e49f22f 16#include "progress.h"
6f7f3beb 17#include "streaming.h"
90cf590f 18#include "decorate.h"
0317f455 19#include "packfile.h"
0d4a1321 20#include "object-store.h"
e0fd51e1 21#include "run-command.h"
b29759d8 22#include "worktree.h"
ff5ebe39
DB
23
24#define REACHABLE 0x0001
2d9c58c6 25#define SEEN 0x0002
6e454b9a 26#define HAS_OBJ 0x0004
092c55d0
JT
27/* This flag is set if something points to this object. */
28#define USED 0x0008
d9839e03 29
96f1e58f
DR
30static int show_root;
31static int show_tags;
32static int show_unreachable;
566842f6 33static int include_reflogs = 1;
f29cd393 34static int check_full = 1;
02976bf8 35static int connectivity_only;
96f1e58f
DR
36static int check_strict;
37static int keep_cache_objects;
22410549
JS
38static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
39static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
e2b4f635 40static int errors_found;
68f6c019 41static int write_lost_and_found;
20f1eb6b 42static int verbose;
1e49f22f 43static int show_progress = -1;
c6a13b2c 44static int show_dangling = 1;
90cf590f 45static int name_objects;
e2b4f635
JH
46#define ERROR_OBJECT 01
47#define ERROR_REACHABLE 02
a3ed7552 48#define ERROR_PACK 04
122f76f5 49#define ERROR_REFS 010
e0fd51e1 50#define ERROR_COMMIT_GRAPH 020
d9839e03 51
993a21b0
JS
52static const char *describe_object(struct object *obj)
53{
90cf590f
JS
54 static struct strbuf buf = STRBUF_INIT;
55 char *name = name_objects ?
56 lookup_decoration(fsck_walk_options.object_names, obj) : NULL;
57
58 strbuf_reset(&buf);
59 strbuf_addstr(&buf, oid_to_hex(&obj->oid));
60 if (name)
61 strbuf_addf(&buf, " (%s)", name);
62
63 return buf.buf;
993a21b0
JS
64}
65
97ca7ca8
JK
66static const char *printable_type(struct object *obj)
67{
68 const char *ret;
69
a2b22854 70 if (obj->type == OBJ_NONE) {
0df8e965
SB
71 enum object_type type = oid_object_info(the_repository,
72 &obj->oid, NULL);
a2b22854 73 if (type > 0)
1268dfac 74 object_as_type(the_repository, obj, type, 0);
a2b22854
JK
75 }
76
debca9d2 77 ret = type_name(obj->type);
97ca7ca8
JK
78 if (!ret)
79 ret = "unknown";
80
81 return ret;
82}
83
2becf00f 84static int fsck_config(const char *var, const char *value, void *cb)
f1f0d088 85{
1335f732
JS
86 if (strcmp(var, "fsck.skiplist") == 0) {
87 const char *path;
88 struct strbuf sb = STRBUF_INIT;
89
90 if (git_config_pathname(&path, var, value))
91 return 1;
92 strbuf_addf(&sb, "skiplist=%s", path);
93 free((char *)path);
94 fsck_set_msg_types(&fsck_obj_options, sb.buf);
95 strbuf_release(&sb);
96 return 0;
97 }
98
2becf00f
JS
99 if (skip_prefix(var, "fsck.", &var)) {
100 fsck_set_msg_type(&fsck_obj_options, var, value);
101 return 0;
102 }
103
104 return git_default_config(var, value, cb);
105}
106
c99ba492
JS
107static void objreport(struct object *obj, const char *msg_type,
108 const char *err)
f1f0d088 109{
c99ba492 110 fprintf(stderr, "%s in %s %s: %s\n",
97ca7ca8 111 msg_type, printable_type(obj), describe_object(obj), err);
f1f0d088
PB
112}
113
c99ba492 114static int objerror(struct object *obj, const char *err)
f1f0d088 115{
e2b4f635 116 errors_found |= ERROR_OBJECT;
c99ba492 117 objreport(obj, "error", err);
f1f0d088
PB
118 return -1;
119}
120
1cd772cc
JS
121static int fsck_error_func(struct fsck_options *o,
122 struct object *obj, int type, const char *message)
f1f0d088 123{
c99ba492 124 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", message);
ba002f3b 125 return (type == FSCK_WARN) ? 0 : 1;
f1f0d088
PB
126}
127
04d39759
LT
128static struct object_array pending;
129
22410549 130static int mark_object(struct object *obj, int type, void *data, struct fsck_options *options)
271b8d25 131{
271b8d25 132 struct object *parent = data;
271b8d25 133
a1cdc251
JH
134 /*
135 * The only case data is NULL or type is OBJ_ANY is when
136 * mark_object_reachable() calls us. All the callers of
137 * that function has non-NULL obj hence ...
138 */
271b8d25 139 if (!obj) {
a1cdc251 140 /* ... these references to parent->fld are safe here */
271b8d25 141 printf("broken link from %7s %s\n",
97ca7ca8 142 printable_type(parent), describe_object(parent));
271b8d25 143 printf("broken link from %7s %s\n",
debca9d2 144 (type == OBJ_ANY ? "unknown" : type_name(type)), "unknown");
271b8d25
MK
145 errors_found |= ERROR_REACHABLE;
146 return 1;
147 }
148
149 if (type != OBJ_ANY && obj->type != type)
a1cdc251 150 /* ... and the reference to parent is safe here */
271b8d25
MK
151 objerror(parent, "wrong object type in link");
152
153 if (obj->flags & REACHABLE)
154 return 0;
155 obj->flags |= REACHABLE;
caba7fc3
JT
156
157 if (is_promisor_object(&obj->oid))
158 /*
159 * Further recursion does not need to be performed on this
160 * object since it is a promisor object (so it does not need to
161 * be added to "pending").
162 */
163 return 0;
164
6e454b9a 165 if (!(obj->flags & HAS_OBJ)) {
f2fd0760 166 if (parent && !has_object_file(&obj->oid)) {
271b8d25 167 printf("broken link from %7s %s\n",
97ca7ca8 168 printable_type(parent), describe_object(parent));
271b8d25 169 printf(" to %7s %s\n",
97ca7ca8 170 printable_type(obj), describe_object(obj));
271b8d25
MK
171 errors_found |= ERROR_REACHABLE;
172 }
173 return 1;
174 }
175
16aa3bfc 176 add_object_array(obj, NULL, &pending);
04d39759
LT
177 return 0;
178}
179
180static void mark_object_reachable(struct object *obj)
181{
22410549 182 mark_object(obj, OBJ_ANY, NULL, NULL);
04d39759
LT
183}
184
a1cdc251 185static int traverse_one_object(struct object *obj)
04d39759 186{
ba3a08ca
EW
187 int result = fsck_walk(obj, obj, &fsck_walk_options);
188
189 if (obj->type == OBJ_TREE) {
190 struct tree *tree = (struct tree *)obj;
191 free_tree_buffer(tree);
192 }
193 return result;
271b8d25
MK
194}
195
04d39759 196static int traverse_reachable(void)
271b8d25 197{
1e49f22f
NTND
198 struct progress *progress = NULL;
199 unsigned int nr = 0;
04d39759 200 int result = 0;
1e49f22f 201 if (show_progress)
8aade107 202 progress = start_delayed_progress(_("Checking connectivity"), 0);
04d39759 203 while (pending.nr) {
71992039 204 result |= traverse_one_object(object_array_pop(&pending));
1e49f22f 205 display_progress(progress, ++nr);
04d39759 206 }
1e49f22f 207 stop_progress(&progress);
04d39759 208 return !!result;
271b8d25
MK
209}
210
22410549 211static int mark_used(struct object *obj, int type, void *data, struct fsck_options *options)
271b8d25
MK
212{
213 if (!obj)
214 return 1;
092c55d0 215 obj->flags |= USED;
271b8d25 216 return 0;
f1f0d088
PB
217}
218
18af29f2
LT
219/*
220 * Check a single reachable object
221 */
222static void check_reachable_object(struct object *obj)
223{
18af29f2
LT
224 /*
225 * We obviously want the object to be parsed,
226 * except if it was in a pack-file and we didn't
227 * do a full fsck
228 */
6e454b9a 229 if (!(obj->flags & HAS_OBJ)) {
caba7fc3
JT
230 if (is_promisor_object(&obj->oid))
231 return;
14c3c80c 232 if (has_object_pack(&obj->oid))
18af29f2 233 return; /* it is in pack - forget about it */
97ca7ca8 234 printf("missing %s %s\n", printable_type(obj),
993a21b0 235 describe_object(obj));
e2b4f635 236 errors_found |= ERROR_REACHABLE;
18af29f2
LT
237 return;
238 }
18af29f2
LT
239}
240
241/*
242 * Check a single unreachable object
243 */
244static void check_unreachable_object(struct object *obj)
245{
246 /*
247 * Missing unreachable object? Ignore it. It's not like
248 * we miss it (since it can't be reached), nor do we want
249 * to complain about it being unreachable (since it does
250 * not exist).
251 */
b4584e4f 252 if (!(obj->flags & HAS_OBJ))
18af29f2
LT
253 return;
254
255 /*
256 * Unreachable object that exists? Show it if asked to,
257 * since this is something that is prunable.
258 */
259 if (show_unreachable) {
97ca7ca8 260 printf("unreachable %s %s\n", printable_type(obj),
993a21b0 261 describe_object(obj));
18af29f2
LT
262 return;
263 }
264
265 /*
092c55d0 266 * "!USED" means that nothing at all points to it, including
3dff5379 267 * other unreachable objects. In other words, it's the "tip"
18af29f2
LT
268 * of some set of unreachable objects, usually a commit that
269 * got dropped.
270 *
271 * Such starting points are more interesting than some random
272 * set of unreachable objects, so we show them even if the user
273 * hasn't asked for _all_ unreachable objects. If you have
274 * deleted a branch by mistake, this is a prime candidate to
275 * start looking at, for example.
276 */
092c55d0 277 if (!(obj->flags & USED)) {
c6a13b2c 278 if (show_dangling)
97ca7ca8 279 printf("dangling %s %s\n", printable_type(obj),
993a21b0 280 describe_object(obj));
68f6c019 281 if (write_lost_and_found) {
fcd12db6 282 char *filename = git_pathdup("lost-found/%s/%s",
68f6c019 283 obj->type == OBJ_COMMIT ? "commit" : "other",
993a21b0 284 describe_object(obj));
68f6c019
JS
285 FILE *f;
286
dcf69262 287 if (safe_create_leading_directories_const(filename)) {
68f6c019 288 error("Could not create lost-found");
fcd12db6 289 free(filename);
68f6c019
JS
290 return;
291 }
23a9e071 292 f = xfopen(filename, "w");
16a7fcfe 293 if (obj->type == OBJ_BLOB) {
7eda0e4f 294 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
eb726f2d 295 die_errno("Could not write '%s'", filename);
16a7fcfe 296 } else
993a21b0 297 fprintf(f, "%s\n", describe_object(obj));
47d32af2 298 if (fclose(f))
d824cbba
TR
299 die_errno("Could not finish '%s'",
300 filename);
fcd12db6 301 free(filename);
68f6c019 302 }
18af29f2
LT
303 return;
304 }
305
306 /*
307 * Otherwise? It's there, it's unreachable, and some other unreachable
308 * object points to it. Ignore it - it's not interesting, and we showed
309 * all the interesting cases above.
310 */
311}
312
313static void check_object(struct object *obj)
314{
20f1eb6b 315 if (verbose)
993a21b0 316 fprintf(stderr, "Checking %s\n", describe_object(obj));
20f1eb6b 317
18af29f2
LT
318 if (obj->flags & REACHABLE)
319 check_reachable_object(obj);
320 else
321 check_unreachable_object(obj);
322}
f1f0d088 323
8ba0bbb2
LT
324static void check_connectivity(void)
325{
fc046a75 326 int i, max;
8ba0bbb2 327
04d39759
LT
328 /* Traverse the pending reachable objects */
329 traverse_reachable();
330
8ba0bbb2 331 /* Look up all the requirements, warn about missing objects.. */
fc046a75 332 max = get_max_object_index();
20f1eb6b
JS
333 if (verbose)
334 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
335
fc046a75 336 for (i = 0; i < max; i++) {
fc046a75 337 struct object *obj = get_indexed_object(i);
8ba0bbb2 338
18af29f2
LT
339 if (obj)
340 check_object(obj);
8ba0bbb2
LT
341 }
342}
343
7ac4f3a0 344static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
85003492 345{
83cd6f90
RS
346 int err;
347
ba002f3b 348 if (obj->flags & SEEN)
85003492 349 return 0;
ba002f3b 350 obj->flags |= SEEN;
85003492 351
20f1eb6b 352 if (verbose)
ba002f3b 353 fprintf(stderr, "Checking %s %s\n",
97ca7ca8 354 printable_type(obj), describe_object(obj));
42ea9cb2 355
22410549 356 if (fsck_walk(obj, NULL, &fsck_obj_options))
ba002f3b 357 objerror(obj, "broken links");
7ac4f3a0 358 err = fsck_object(obj, buffer, size, &fsck_obj_options);
83cd6f90
RS
359 if (err)
360 goto out;
1ea34e36 361
ba002f3b
MK
362 if (obj->type == OBJ_COMMIT) {
363 struct commit *commit = (struct commit *) obj;
de2eb7f6 364
ba002f3b 365 if (!commit->parents && show_root)
993a21b0 366 printf("root %s\n", describe_object(&commit->object));
ba002f3b 367 }
92d4c85d 368
ba002f3b
MK
369 if (obj->type == OBJ_TAG) {
370 struct tag *tag = (struct tag *) obj;
20f1eb6b 371
ba002f3b 372 if (show_tags && tag->tagged) {
97ca7ca8 373 printf("tagged %s %s", printable_type(tag->tagged),
993a21b0
JS
374 describe_object(tag->tagged));
375 printf(" (%s) in %s\n", tag->tag,
376 describe_object(&tag->object));
ba002f3b 377 }
92d4c85d 378 }
889262ea 379
83cd6f90
RS
380out:
381 if (obj->type == OBJ_TREE)
382 free_tree_buffer((struct tree *)obj);
383 if (obj->type == OBJ_COMMIT)
384 free_commit_buffer((struct commit *)obj);
385 return err;
20222118
LT
386}
387
9fd75046 388static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
c9486eb0
NTND
389 unsigned long size, void *buffer, int *eaten)
390{
ec9d2249
NTND
391 /*
392 * Note, buffer may be NULL if type is OBJ_BLOB. See
393 * verify_packfile(), data_valid variable for details.
394 */
c9486eb0 395 struct object *obj;
1ec5bfd2
SB
396 obj = parse_object_buffer(the_repository, oid, type, size, buffer,
397 eaten);
c9486eb0
NTND
398 if (!obj) {
399 errors_found |= ERROR_OBJECT;
9fd75046 400 return error("%s: object corrupt or missing", oid_to_hex(oid));
c9486eb0 401 }
092c55d0
JT
402 obj->flags &= ~(REACHABLE | SEEN);
403 obj->flags |= HAS_OBJ;
7ac4f3a0 404 return fsck_obj(obj, buffer, size);
c9486eb0
NTND
405}
406
96f1e58f 407static int default_refs;
944d8589 408
9461d272 409static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
dddbad72 410 timestamp_t timestamp)
55dd5526
JH
411{
412 struct object *obj;
413
9461d272 414 if (!is_null_oid(oid)) {
5abddd1e 415 obj = lookup_object(the_repository, oid->hash);
c2d17b3b 416 if (obj && (obj->flags & HAS_OBJ)) {
90cf590f
JS
417 if (timestamp && name_objects)
418 add_decoration(fsck_walk_options.object_names,
419 obj,
cb71f8bd 420 xstrfmt("%s@{%"PRItime"}", refname, timestamp));
092c55d0 421 obj->flags |= USED;
271b8d25 422 mark_object_reachable(obj);
498f1f61 423 } else if (!is_promisor_object(oid)) {
9461d272 424 error("%s: invalid reflog entry %s", refname, oid_to_hex(oid));
19bf6c9b 425 errors_found |= ERROR_REACHABLE;
55dd5526
JH
426 }
427 }
d66ae59b
MH
428}
429
9461d272 430static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
dddbad72 431 const char *email, timestamp_t timestamp, int tz,
883d60fa 432 const char *message, void *cb_data)
55dd5526 433{
19bf6c9b 434 const char *refname = cb_data;
55dd5526 435
20f1eb6b
JS
436 if (verbose)
437 fprintf(stderr, "Checking reflog %s->%s\n",
9461d272 438 oid_to_hex(ooid), oid_to_hex(noid));
20f1eb6b 439
9461d272 440 fsck_handle_reflog_oid(refname, ooid, 0);
441 fsck_handle_reflog_oid(refname, noid, timestamp);
55dd5526
JH
442 return 0;
443}
444
635b99a0
MH
445static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
446 int flag, void *cb_data)
eb8381c8 447{
b29759d8
NTND
448 struct strbuf refname = STRBUF_INIT;
449
450 strbuf_worktree_ref(cb_data, &refname, logname);
451 for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
452 strbuf_release(&refname);
eb8381c8
NP
453 return 0;
454}
455
635b99a0
MH
456static int fsck_handle_ref(const char *refname, const struct object_id *oid,
457 int flag, void *cb_data)
1024932f 458{
1024932f
LT
459 struct object *obj;
460
109cd76d 461 obj = parse_object(the_repository, oid);
8a498a05 462 if (!obj) {
43f25158
JT
463 if (is_promisor_object(oid)) {
464 /*
465 * Increment default_refs anyway, because this is a
466 * valid ref.
467 */
468 default_refs++;
469 return 0;
470 }
635b99a0 471 error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
30d1038d 472 errors_found |= ERROR_REACHABLE;
944d8589
LT
473 /* We'll continue with the rest despite the error.. */
474 return 0;
8a498a05 475 }
122f76f5 476 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
6232f62b 477 error("%s: not a commit", refname);
122f76f5
JH
478 errors_found |= ERROR_REFS;
479 }
944d8589 480 default_refs++;
092c55d0 481 obj->flags |= USED;
90cf590f
JS
482 if (name_objects)
483 add_decoration(fsck_walk_options.object_names,
484 obj, xstrdup(refname));
271b8d25 485 mark_object_reachable(obj);
55dd5526 486
7c4d07c7 487 return 0;
1024932f
LT
488}
489
b29759d8
NTND
490static int fsck_head_link(const char *head_ref_name,
491 const char **head_points_at,
a8c754d4
EN
492 struct object_id *head_oid);
493
1024932f
LT
494static void get_default_heads(void)
495{
b29759d8 496 struct worktree **worktrees, **p;
a8c754d4
EN
497 const char *head_points_at;
498 struct object_id head_oid;
499
635b99a0 500 for_each_rawref(fsck_handle_ref, NULL);
b29759d8
NTND
501
502 worktrees = get_worktrees(0);
503 for (p = worktrees; *p; p++) {
504 struct worktree *wt = *p;
505 struct strbuf ref = STRBUF_INIT;
506
507 strbuf_worktree_ref(wt, &ref, "HEAD");
508 fsck_head_link(ref.buf, &head_points_at, &head_oid);
509 if (head_points_at && !is_null_oid(&head_oid))
510 fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
511 strbuf_release(&ref);
512
513 if (include_reflogs)
514 refs_for_each_reflog(get_worktree_ref_store(wt),
515 fsck_handle_reflog, wt);
516 }
517 free_worktrees(worktrees);
071fa89e
LT
518
519 /*
520 * Not having any default heads isn't really fatal, but
521 * it does mean that "--unreachable" no longer makes any
522 * sense (since in this case everything will obviously
523 * be unreachable by definition.
524 *
525 * Showing dangling objects is valid, though (as those
526 * dangling objects are likely lost heads).
527 *
528 * So we just print a warning about it, and clear the
529 * "show_unreachable" flag.
530 */
531 if (!default_refs) {
8eb2d0be 532 fprintf(stderr, "notice: No default references\n");
071fa89e
LT
533 show_unreachable = 0;
534 }
1024932f
LT
535}
536
7ac4f3a0 537static int fsck_loose(const struct object_id *oid, const char *path, void *data)
c68b489e
JK
538{
539 struct object *obj;
c68b489e
JK
540 enum object_type type;
541 unsigned long size;
7ac4f3a0 542 void *contents;
c68b489e
JK
543 int eaten;
544
7913f53b 545 if (read_loose_object(path, oid, &type, &size, &contents) < 0) {
7ac4f3a0
JK
546 errors_found |= ERROR_OBJECT;
547 error("%s: object corrupt or missing: %s",
548 oid_to_hex(oid), path);
549 return 0; /* keep checking other objects */
550 }
c68b489e
JK
551
552 if (!contents && type != OBJ_BLOB)
7ac4f3a0 553 BUG("read_loose_object streamed a non-blob");
c68b489e 554
1ec5bfd2
SB
555 obj = parse_object_buffer(the_repository, oid, type, size,
556 contents, &eaten);
557
c68b489e
JK
558 if (!obj) {
559 errors_found |= ERROR_OBJECT;
7ac4f3a0 560 error("%s: object could not be parsed: %s",
76c1d9a0 561 oid_to_hex(oid), path);
7ac4f3a0
JK
562 if (!eaten)
563 free(contents);
c68b489e
JK
564 return 0; /* keep checking other objects */
565 }
566
092c55d0
JT
567 obj->flags &= ~(REACHABLE | SEEN);
568 obj->flags |= HAS_OBJ;
7ac4f3a0 569 if (fsck_obj(obj, contents, size))
f0766bf9 570 errors_found |= ERROR_OBJECT;
7ac4f3a0
JK
571
572 if (!eaten)
573 free(contents);
574 return 0; /* keep checking other objects, even if we saw an error */
f0766bf9
JK
575}
576
577static int fsck_cruft(const char *basename, const char *path, void *data)
578{
579 if (!starts_with(basename, "tmp_obj_"))
580 fprintf(stderr, "bad sha1 file: %s\n", path);
581 return 0;
582}
583
70c49050 584static int fsck_subdir(unsigned int nr, const char *path, void *progress)
f0766bf9
JK
585{
586 display_progress(progress, nr + 1);
587 return 0;
588}
589
8a498a05
JH
590static void fsck_object_dir(const char *path)
591{
1e49f22f 592 struct progress *progress = NULL;
20f1eb6b
JS
593
594 if (verbose)
595 fprintf(stderr, "Checking object directory\n");
596
1e49f22f 597 if (show_progress)
754dbc43 598 progress = start_progress(_("Checking object directories"), 256);
f0766bf9
JK
599
600 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
601 progress);
602 display_progress(progress, 256);
1e49f22f 603 stop_progress(&progress);
8a498a05
JH
604}
605
b29759d8
NTND
606static int fsck_head_link(const char *head_ref_name,
607 const char **head_points_at,
a8c754d4 608 struct object_id *head_oid)
c3330383 609{
8eb2d0be 610 int null_is_error = 0;
8eb2d0be 611
20f1eb6b 612 if (verbose)
b29759d8 613 fprintf(stderr, "Checking %s link\n", head_ref_name);
20f1eb6b 614
b29759d8 615 *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
a8c754d4 616 if (!*head_points_at) {
122f76f5 617 errors_found |= ERROR_REFS;
b29759d8 618 return error("Invalid %s", head_ref_name);
122f76f5 619 }
b29759d8 620 if (!strcmp(*head_points_at, head_ref_name))
8eb2d0be
JH
621 /* detached HEAD */
622 null_is_error = 1;
a8c754d4 623 else if (!starts_with(*head_points_at, "refs/heads/")) {
122f76f5 624 errors_found |= ERROR_REFS;
b29759d8
NTND
625 return error("%s points to something strange (%s)",
626 head_ref_name, *head_points_at);
122f76f5 627 }
a8c754d4 628 if (is_null_oid(head_oid)) {
122f76f5
JH
629 if (null_is_error) {
630 errors_found |= ERROR_REFS;
b29759d8
NTND
631 return error("%s: detached HEAD points at nothing",
632 head_ref_name);
122f76f5 633 }
b29759d8
NTND
634 fprintf(stderr, "notice: %s points to an unborn branch (%s)\n",
635 head_ref_name, *head_points_at + 11);
8eb2d0be 636 }
c3330383
LT
637 return 0;
638}
639
53dc3f3e
JH
640static int fsck_cache_tree(struct cache_tree *it)
641{
642 int i;
643 int err = 0;
644
20f1eb6b
JS
645 if (verbose)
646 fprintf(stderr, "Checking cache tree\n");
647
53dc3f3e 648 if (0 <= it->entry_count) {
109cd76d 649 struct object *obj = parse_object(the_repository, &it->oid);
6d60bbef
JH
650 if (!obj) {
651 error("%s: invalid sha1 pointer in cache-tree",
e0a92804 652 oid_to_hex(&it->oid));
122f76f5 653 errors_found |= ERROR_REFS;
6d60bbef
JH
654 return 1;
655 }
092c55d0 656 obj->flags |= USED;
90cf590f
JS
657 if (name_objects)
658 add_decoration(fsck_walk_options.object_names,
659 obj, xstrdup(":"));
a1cdc251 660 mark_object_reachable(obj);
1974632c 661 if (obj->type != OBJ_TREE)
53dc3f3e
JH
662 err |= objerror(obj, "non-tree in cache-tree");
663 }
664 for (i = 0; i < it->subtree_nr; i++)
665 err |= fsck_cache_tree(it->down[i]->cache_tree);
666 return err;
667}
668
76c1d9a0 669static void mark_object_for_connectivity(const struct object_id *oid)
3e3f8bd6 670{
76c1d9a0 671 struct object *obj = lookup_unknown_object(oid->hash);
3e3f8bd6
JK
672 obj->flags |= HAS_OBJ;
673}
674
76c1d9a0 675static int mark_loose_for_connectivity(const struct object_id *oid,
3e3f8bd6
JK
676 const char *path,
677 void *data)
678{
76c1d9a0 679 mark_object_for_connectivity(oid);
3e3f8bd6
JK
680 return 0;
681}
682
76c1d9a0 683static int mark_packed_for_connectivity(const struct object_id *oid,
3e3f8bd6
JK
684 struct packed_git *pack,
685 uint32_t pos,
686 void *data)
687{
76c1d9a0 688 mark_object_for_connectivity(oid);
3e3f8bd6
JK
689 return 0;
690}
691
5ac0a206 692static char const * const fsck_usage[] = {
9c9b4f2f 693 N_("git fsck [<options>] [<object>...]"),
5ac0a206
PH
694 NULL
695};
696
697static struct option fsck_opts[] = {
cf8fe315 698 OPT__VERBOSE(&verbose, N_("be verbose")),
d5d09d47 699 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
cf8fe315 700 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
d5d09d47
SB
701 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
702 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
703 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
704 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
705 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
02976bf8 706 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
d5d09d47
SB
707 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
708 OPT_BOOL(0, "lost-found", &write_lost_and_found,
cf8fe315
NTND
709 N_("write dangling objects in .git/lost-found")),
710 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
90cf590f 711 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
5ac0a206
PH
712 OPT_END(),
713};
e2b4f635 714
baffc0e7 715int cmd_fsck(int argc, const char **argv, const char *prefix)
20222118 716{
78e7b98f 717 int i;
e15ef669 718 struct alternate_object_database *alt;
20222118 719
8b4c0103
JT
720 /* fsck knows how to handle missing promisor objects */
721 fetch_if_missing = 0;
722
e2b4f635 723 errors_found = 0;
6ebd1caf 724 read_replace_refs = 0;
61e2b015 725
37782920 726 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
1e49f22f 727
22410549
JS
728 fsck_walk_options.walk = mark_object;
729 fsck_obj_options.walk = mark_used;
730 fsck_obj_options.error_func = fsck_error_func;
731 if (check_strict)
732 fsck_obj_options.strict = 1;
733
1e49f22f
NTND
734 if (show_progress == -1)
735 show_progress = isatty(2);
736 if (verbose)
737 show_progress = 0;
738
5ac0a206
PH
739 if (write_lost_and_found) {
740 check_full = 1;
741 include_reflogs = 0;
889262ea
LT
742 }
743
90cf590f
JS
744 if (name_objects)
745 fsck_walk_options.object_names =
746 xcalloc(1, sizeof(struct decoration));
747
2becf00f
JS
748 git_config(fsck_config, NULL);
749
3e3f8bd6
JK
750 if (connectivity_only) {
751 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
752 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
753 } else {
031dc927
SB
754 struct alternate_object_database *alt_odb_list;
755
02976bf8 756 fsck_object_dir(get_object_directory());
e15ef669 757
0b209034 758 prepare_alt_odb(the_repository);
031dc927 759 alt_odb_list = the_repository->objects->alt_odb_list;
597f9134
JK
760 for (alt = alt_odb_list; alt; alt = alt->next)
761 fsck_object_dir(alt->path);
e15ef669 762
3e3f8bd6
JK
763 if (check_full) {
764 struct packed_git *p;
765 uint32_t total = 0, count = 0;
766 struct progress *progress = NULL;
e15ef669 767
3e3f8bd6 768 if (show_progress) {
454ea2e4 769 for (p = get_all_packs(the_repository); p;
a80d72db 770 p = p->next) {
3e3f8bd6
JK
771 if (open_pack_index(p))
772 continue;
773 total += p->num_objects;
774 }
1e49f22f 775
3e3f8bd6
JK
776 progress = start_progress(_("Checking objects"), total);
777 }
454ea2e4 778 for (p = get_all_packs(the_repository); p;
a80d72db 779 p = p->next) {
3e3f8bd6
JK
780 /* verify gives error messages itself */
781 if (verify_pack(p, fsck_obj_buffer,
782 progress, count))
783 errors_found |= ERROR_PACK;
784 count += p->num_objects;
1e49f22f 785 }
3e3f8bd6 786 stop_progress(&progress);
1e49f22f 787 }
1995b5e0
JK
788
789 if (fsck_finish(&fsck_obj_options))
790 errors_found |= ERROR_OBJECT;
bcee6fd8
LT
791 }
792
3aed2fda 793 for (i = 0; i < argc; i++) {
a6080a0a 794 const char *arg = argv[i];
aca6065c 795 struct object_id oid;
796 if (!get_oid(arg, &oid)) {
5abddd1e
SB
797 struct object *obj = lookup_object(the_repository,
798 oid.hash);
e1a1388d 799
c2d17b3b 800 if (!obj || !(obj->flags & HAS_OBJ)) {
096c9b8b
JT
801 if (is_promisor_object(&oid))
802 continue;
aca6065c 803 error("%s: object missing", oid_to_hex(&oid));
c6c7b16d 804 errors_found |= ERROR_OBJECT;
e1a1388d 805 continue;
c6c7b16d 806 }
e1a1388d 807
092c55d0 808 obj->flags |= USED;
90cf590f
JS
809 if (name_objects)
810 add_decoration(fsck_walk_options.object_names,
811 obj, xstrdup(arg));
271b8d25 812 mark_object_reachable(obj);
d9839e03
LT
813 continue;
814 }
f1f0d088 815 error("invalid parameter: expected sha1, got '%s'", arg);
c6c7b16d 816 errors_found |= ERROR_OBJECT;
d9839e03 817 }
d9839e03 818
1024932f 819 /*
d1af002d 820 * If we've not been given any explicit head information, do the
e7bd907d
LT
821 * default ones from .git/refs. We also consider the index file
822 * in this case (ie this implies --cache).
1024932f 823 */
c3271a0e 824 if (!argc) {
1024932f
LT
825 get_default_heads();
826 keep_cache_objects = 1;
827 }
828
ae7c0c92 829 if (keep_cache_objects) {
a33fc72f 830 verify_index_checksum = 1;
00ec50e5 831 verify_ce_order = 1;
ae7c0c92
JH
832 read_cache();
833 for (i = 0; i < active_nr; i++) {
8d9721c8
LT
834 unsigned int mode;
835 struct blob *blob;
ae7c0c92 836 struct object *obj;
8d9721c8 837
7a51ed66 838 mode = active_cache[i]->ce_mode;
302b9282 839 if (S_ISGITLINK(mode))
8d9721c8 840 continue;
da14a7ff
SB
841 blob = lookup_blob(the_repository,
842 &active_cache[i]->oid);
ae7c0c92
JH
843 if (!blob)
844 continue;
845 obj = &blob->object;
092c55d0 846 obj->flags |= USED;
90cf590f
JS
847 if (name_objects)
848 add_decoration(fsck_walk_options.object_names,
849 obj,
850 xstrfmt(":%s", active_cache[i]->name));
271b8d25 851 mark_object_reachable(obj);
ae7c0c92 852 }
53dc3f3e
JH
853 if (active_cache_tree)
854 fsck_cache_tree(active_cache_tree);
ae7c0c92
JH
855 }
856
8ba0bbb2 857 check_connectivity();
e0fd51e1 858
dade47c0 859 if (!git_config_get_bool("core.commitgraph", &i) && i) {
e0fd51e1
DS
860 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
861 const char *verify_argv[] = { "commit-graph", "verify", NULL, NULL, NULL };
862
863 commit_graph_verify.argv = verify_argv;
864 commit_graph_verify.git_cmd = 1;
865 if (run_command(&commit_graph_verify))
866 errors_found |= ERROR_COMMIT_GRAPH;
867
868 prepare_alt_odb(the_repository);
869 for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
870 verify_argv[2] = "--object-dir";
871 verify_argv[3] = alt->path;
872 if (run_command(&commit_graph_verify))
873 errors_found |= ERROR_COMMIT_GRAPH;
874 }
875 }
876
66ec0390
DS
877 if (!git_config_get_bool("core.multipackindex", &i) && i) {
878 struct child_process midx_verify = CHILD_PROCESS_INIT;
879 const char *midx_argv[] = { "multi-pack-index", "verify", NULL, NULL, NULL };
880
881 midx_verify.argv = midx_argv;
882 midx_verify.git_cmd = 1;
883 if (run_command(&midx_verify))
884 errors_found |= ERROR_COMMIT_GRAPH;
885
886 prepare_alt_odb(the_repository);
887 for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
888 midx_argv[2] = "--object-dir";
889 midx_argv[3] = alt->path;
890 if (run_command(&midx_verify))
891 errors_found |= ERROR_COMMIT_GRAPH;
892 }
893 }
894
e2b4f635 895 return errors_found;
20222118 896}