fsck: always compute USED flags for unreachable objects
[git/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
8d8c2a5a
JK
219static void mark_unreachable_referents(const struct object_id *oid)
220{
221 struct fsck_options options = FSCK_OPTIONS_DEFAULT;
222 struct object *obj = lookup_object(the_repository, oid->hash);
223
224 if (!obj || !(obj->flags & HAS_OBJ))
225 return; /* not part of our original set */
226 if (obj->flags & REACHABLE)
227 return; /* reachable objects already traversed */
228
229 /*
230 * Avoid passing OBJ_NONE to fsck_walk, which will parse the object
231 * (and we want to avoid parsing blobs).
232 */
233 if (obj->type == OBJ_NONE) {
234 enum object_type type = oid_object_info(the_repository,
235 &obj->oid, NULL);
236 if (type > 0)
237 object_as_type(the_repository, obj, type, 0);
238 }
239
240 options.walk = mark_used;
241 fsck_walk(obj, NULL, &options);
242}
243
244static int mark_loose_unreachable_referents(const struct object_id *oid,
245 const char *path,
246 void *data)
247{
248 mark_unreachable_referents(oid);
249 return 0;
250}
251
252static int mark_packed_unreachable_referents(const struct object_id *oid,
253 struct packed_git *pack,
254 uint32_t pos,
255 void *data)
256{
257 mark_unreachable_referents(oid);
258 return 0;
259}
260
18af29f2
LT
261/*
262 * Check a single reachable object
263 */
264static void check_reachable_object(struct object *obj)
265{
18af29f2
LT
266 /*
267 * We obviously want the object to be parsed,
268 * except if it was in a pack-file and we didn't
269 * do a full fsck
270 */
6e454b9a 271 if (!(obj->flags & HAS_OBJ)) {
caba7fc3
JT
272 if (is_promisor_object(&obj->oid))
273 return;
14c3c80c 274 if (has_object_pack(&obj->oid))
18af29f2 275 return; /* it is in pack - forget about it */
97ca7ca8 276 printf("missing %s %s\n", printable_type(obj),
993a21b0 277 describe_object(obj));
e2b4f635 278 errors_found |= ERROR_REACHABLE;
18af29f2
LT
279 return;
280 }
18af29f2
LT
281}
282
283/*
284 * Check a single unreachable object
285 */
286static void check_unreachable_object(struct object *obj)
287{
288 /*
289 * Missing unreachable object? Ignore it. It's not like
290 * we miss it (since it can't be reached), nor do we want
291 * to complain about it being unreachable (since it does
292 * not exist).
293 */
b4584e4f 294 if (!(obj->flags & HAS_OBJ))
18af29f2
LT
295 return;
296
297 /*
298 * Unreachable object that exists? Show it if asked to,
299 * since this is something that is prunable.
300 */
301 if (show_unreachable) {
97ca7ca8 302 printf("unreachable %s %s\n", printable_type(obj),
993a21b0 303 describe_object(obj));
18af29f2
LT
304 return;
305 }
306
307 /*
092c55d0 308 * "!USED" means that nothing at all points to it, including
3dff5379 309 * other unreachable objects. In other words, it's the "tip"
18af29f2
LT
310 * of some set of unreachable objects, usually a commit that
311 * got dropped.
312 *
313 * Such starting points are more interesting than some random
314 * set of unreachable objects, so we show them even if the user
315 * hasn't asked for _all_ unreachable objects. If you have
316 * deleted a branch by mistake, this is a prime candidate to
317 * start looking at, for example.
318 */
092c55d0 319 if (!(obj->flags & USED)) {
c6a13b2c 320 if (show_dangling)
97ca7ca8 321 printf("dangling %s %s\n", printable_type(obj),
993a21b0 322 describe_object(obj));
68f6c019 323 if (write_lost_and_found) {
fcd12db6 324 char *filename = git_pathdup("lost-found/%s/%s",
68f6c019 325 obj->type == OBJ_COMMIT ? "commit" : "other",
993a21b0 326 describe_object(obj));
68f6c019
JS
327 FILE *f;
328
dcf69262 329 if (safe_create_leading_directories_const(filename)) {
68f6c019 330 error("Could not create lost-found");
fcd12db6 331 free(filename);
68f6c019
JS
332 return;
333 }
23a9e071 334 f = xfopen(filename, "w");
16a7fcfe 335 if (obj->type == OBJ_BLOB) {
7eda0e4f 336 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
eb726f2d 337 die_errno("Could not write '%s'", filename);
16a7fcfe 338 } else
993a21b0 339 fprintf(f, "%s\n", describe_object(obj));
47d32af2 340 if (fclose(f))
d824cbba
TR
341 die_errno("Could not finish '%s'",
342 filename);
fcd12db6 343 free(filename);
68f6c019 344 }
18af29f2
LT
345 return;
346 }
347
348 /*
349 * Otherwise? It's there, it's unreachable, and some other unreachable
350 * object points to it. Ignore it - it's not interesting, and we showed
351 * all the interesting cases above.
352 */
353}
354
355static void check_object(struct object *obj)
356{
20f1eb6b 357 if (verbose)
993a21b0 358 fprintf(stderr, "Checking %s\n", describe_object(obj));
20f1eb6b 359
18af29f2
LT
360 if (obj->flags & REACHABLE)
361 check_reachable_object(obj);
362 else
363 check_unreachable_object(obj);
364}
f1f0d088 365
8ba0bbb2
LT
366static void check_connectivity(void)
367{
fc046a75 368 int i, max;
8ba0bbb2 369
04d39759
LT
370 /* Traverse the pending reachable objects */
371 traverse_reachable();
372
8d8c2a5a
JK
373 /*
374 * With --connectivity-only, we won't have actually opened and marked
375 * unreachable objects with USED. Do that now to make --dangling, etc
376 * accurate.
377 */
378 if (connectivity_only && (show_dangling || write_lost_and_found)) {
379 /*
380 * Even though we already have a "struct object" for each of
381 * these in memory, we must not iterate over the internal
382 * object hash as we do below. Our loop would potentially
383 * resize the hash, making our iteration invalid.
384 *
385 * Instead, we'll just go back to the source list of objects,
386 * and ignore any that weren't present in our earlier
387 * traversal.
388 */
389 for_each_loose_object(mark_loose_unreachable_referents, NULL, 0);
390 for_each_packed_object(mark_packed_unreachable_referents, NULL, 0);
391 }
392
8ba0bbb2 393 /* Look up all the requirements, warn about missing objects.. */
fc046a75 394 max = get_max_object_index();
20f1eb6b
JS
395 if (verbose)
396 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
397
fc046a75 398 for (i = 0; i < max; i++) {
fc046a75 399 struct object *obj = get_indexed_object(i);
8ba0bbb2 400
18af29f2
LT
401 if (obj)
402 check_object(obj);
8ba0bbb2
LT
403 }
404}
405
7ac4f3a0 406static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
85003492 407{
83cd6f90
RS
408 int err;
409
ba002f3b 410 if (obj->flags & SEEN)
85003492 411 return 0;
ba002f3b 412 obj->flags |= SEEN;
85003492 413
20f1eb6b 414 if (verbose)
ba002f3b 415 fprintf(stderr, "Checking %s %s\n",
97ca7ca8 416 printable_type(obj), describe_object(obj));
42ea9cb2 417
22410549 418 if (fsck_walk(obj, NULL, &fsck_obj_options))
ba002f3b 419 objerror(obj, "broken links");
7ac4f3a0 420 err = fsck_object(obj, buffer, size, &fsck_obj_options);
83cd6f90
RS
421 if (err)
422 goto out;
1ea34e36 423
ba002f3b
MK
424 if (obj->type == OBJ_COMMIT) {
425 struct commit *commit = (struct commit *) obj;
de2eb7f6 426
ba002f3b 427 if (!commit->parents && show_root)
993a21b0 428 printf("root %s\n", describe_object(&commit->object));
ba002f3b 429 }
92d4c85d 430
ba002f3b
MK
431 if (obj->type == OBJ_TAG) {
432 struct tag *tag = (struct tag *) obj;
20f1eb6b 433
ba002f3b 434 if (show_tags && tag->tagged) {
97ca7ca8 435 printf("tagged %s %s", printable_type(tag->tagged),
993a21b0
JS
436 describe_object(tag->tagged));
437 printf(" (%s) in %s\n", tag->tag,
438 describe_object(&tag->object));
ba002f3b 439 }
92d4c85d 440 }
889262ea 441
83cd6f90
RS
442out:
443 if (obj->type == OBJ_TREE)
444 free_tree_buffer((struct tree *)obj);
445 if (obj->type == OBJ_COMMIT)
446 free_commit_buffer((struct commit *)obj);
447 return err;
20222118
LT
448}
449
9fd75046 450static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
c9486eb0
NTND
451 unsigned long size, void *buffer, int *eaten)
452{
ec9d2249
NTND
453 /*
454 * Note, buffer may be NULL if type is OBJ_BLOB. See
455 * verify_packfile(), data_valid variable for details.
456 */
c9486eb0 457 struct object *obj;
1ec5bfd2
SB
458 obj = parse_object_buffer(the_repository, oid, type, size, buffer,
459 eaten);
c9486eb0
NTND
460 if (!obj) {
461 errors_found |= ERROR_OBJECT;
9fd75046 462 return error("%s: object corrupt or missing", oid_to_hex(oid));
c9486eb0 463 }
092c55d0
JT
464 obj->flags &= ~(REACHABLE | SEEN);
465 obj->flags |= HAS_OBJ;
7ac4f3a0 466 return fsck_obj(obj, buffer, size);
c9486eb0
NTND
467}
468
96f1e58f 469static int default_refs;
944d8589 470
9461d272 471static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
dddbad72 472 timestamp_t timestamp)
55dd5526
JH
473{
474 struct object *obj;
475
9461d272 476 if (!is_null_oid(oid)) {
5abddd1e 477 obj = lookup_object(the_repository, oid->hash);
c2d17b3b 478 if (obj && (obj->flags & HAS_OBJ)) {
90cf590f
JS
479 if (timestamp && name_objects)
480 add_decoration(fsck_walk_options.object_names,
481 obj,
cb71f8bd 482 xstrfmt("%s@{%"PRItime"}", refname, timestamp));
092c55d0 483 obj->flags |= USED;
271b8d25 484 mark_object_reachable(obj);
498f1f61 485 } else if (!is_promisor_object(oid)) {
9461d272 486 error("%s: invalid reflog entry %s", refname, oid_to_hex(oid));
19bf6c9b 487 errors_found |= ERROR_REACHABLE;
55dd5526
JH
488 }
489 }
d66ae59b
MH
490}
491
9461d272 492static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
dddbad72 493 const char *email, timestamp_t timestamp, int tz,
883d60fa 494 const char *message, void *cb_data)
55dd5526 495{
19bf6c9b 496 const char *refname = cb_data;
55dd5526 497
20f1eb6b
JS
498 if (verbose)
499 fprintf(stderr, "Checking reflog %s->%s\n",
9461d272 500 oid_to_hex(ooid), oid_to_hex(noid));
20f1eb6b 501
9461d272 502 fsck_handle_reflog_oid(refname, ooid, 0);
503 fsck_handle_reflog_oid(refname, noid, timestamp);
55dd5526
JH
504 return 0;
505}
506
635b99a0
MH
507static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
508 int flag, void *cb_data)
eb8381c8 509{
b29759d8
NTND
510 struct strbuf refname = STRBUF_INIT;
511
512 strbuf_worktree_ref(cb_data, &refname, logname);
513 for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
514 strbuf_release(&refname);
eb8381c8
NP
515 return 0;
516}
517
635b99a0
MH
518static int fsck_handle_ref(const char *refname, const struct object_id *oid,
519 int flag, void *cb_data)
1024932f 520{
1024932f
LT
521 struct object *obj;
522
109cd76d 523 obj = parse_object(the_repository, oid);
8a498a05 524 if (!obj) {
43f25158
JT
525 if (is_promisor_object(oid)) {
526 /*
527 * Increment default_refs anyway, because this is a
528 * valid ref.
529 */
530 default_refs++;
531 return 0;
532 }
635b99a0 533 error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
30d1038d 534 errors_found |= ERROR_REACHABLE;
944d8589
LT
535 /* We'll continue with the rest despite the error.. */
536 return 0;
8a498a05 537 }
122f76f5 538 if (obj->type != OBJ_COMMIT && is_branch(refname)) {
6232f62b 539 error("%s: not a commit", refname);
122f76f5
JH
540 errors_found |= ERROR_REFS;
541 }
944d8589 542 default_refs++;
092c55d0 543 obj->flags |= USED;
90cf590f
JS
544 if (name_objects)
545 add_decoration(fsck_walk_options.object_names,
546 obj, xstrdup(refname));
271b8d25 547 mark_object_reachable(obj);
55dd5526 548
7c4d07c7 549 return 0;
1024932f
LT
550}
551
b29759d8
NTND
552static int fsck_head_link(const char *head_ref_name,
553 const char **head_points_at,
a8c754d4
EN
554 struct object_id *head_oid);
555
1024932f
LT
556static void get_default_heads(void)
557{
b29759d8 558 struct worktree **worktrees, **p;
a8c754d4
EN
559 const char *head_points_at;
560 struct object_id head_oid;
561
635b99a0 562 for_each_rawref(fsck_handle_ref, NULL);
b29759d8
NTND
563
564 worktrees = get_worktrees(0);
565 for (p = worktrees; *p; p++) {
566 struct worktree *wt = *p;
567 struct strbuf ref = STRBUF_INIT;
568
569 strbuf_worktree_ref(wt, &ref, "HEAD");
570 fsck_head_link(ref.buf, &head_points_at, &head_oid);
571 if (head_points_at && !is_null_oid(&head_oid))
572 fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
573 strbuf_release(&ref);
574
575 if (include_reflogs)
576 refs_for_each_reflog(get_worktree_ref_store(wt),
577 fsck_handle_reflog, wt);
578 }
579 free_worktrees(worktrees);
071fa89e
LT
580
581 /*
582 * Not having any default heads isn't really fatal, but
583 * it does mean that "--unreachable" no longer makes any
584 * sense (since in this case everything will obviously
585 * be unreachable by definition.
586 *
587 * Showing dangling objects is valid, though (as those
588 * dangling objects are likely lost heads).
589 *
590 * So we just print a warning about it, and clear the
591 * "show_unreachable" flag.
592 */
593 if (!default_refs) {
8eb2d0be 594 fprintf(stderr, "notice: No default references\n");
071fa89e
LT
595 show_unreachable = 0;
596 }
1024932f
LT
597}
598
7ac4f3a0 599static int fsck_loose(const struct object_id *oid, const char *path, void *data)
c68b489e
JK
600{
601 struct object *obj;
c68b489e
JK
602 enum object_type type;
603 unsigned long size;
7ac4f3a0 604 void *contents;
c68b489e
JK
605 int eaten;
606
7913f53b 607 if (read_loose_object(path, oid, &type, &size, &contents) < 0) {
7ac4f3a0
JK
608 errors_found |= ERROR_OBJECT;
609 error("%s: object corrupt or missing: %s",
610 oid_to_hex(oid), path);
611 return 0; /* keep checking other objects */
612 }
c68b489e
JK
613
614 if (!contents && type != OBJ_BLOB)
7ac4f3a0 615 BUG("read_loose_object streamed a non-blob");
c68b489e 616
1ec5bfd2
SB
617 obj = parse_object_buffer(the_repository, oid, type, size,
618 contents, &eaten);
619
c68b489e
JK
620 if (!obj) {
621 errors_found |= ERROR_OBJECT;
7ac4f3a0 622 error("%s: object could not be parsed: %s",
76c1d9a0 623 oid_to_hex(oid), path);
7ac4f3a0
JK
624 if (!eaten)
625 free(contents);
c68b489e
JK
626 return 0; /* keep checking other objects */
627 }
628
092c55d0
JT
629 obj->flags &= ~(REACHABLE | SEEN);
630 obj->flags |= HAS_OBJ;
7ac4f3a0 631 if (fsck_obj(obj, contents, size))
f0766bf9 632 errors_found |= ERROR_OBJECT;
7ac4f3a0
JK
633
634 if (!eaten)
635 free(contents);
636 return 0; /* keep checking other objects, even if we saw an error */
f0766bf9
JK
637}
638
639static int fsck_cruft(const char *basename, const char *path, void *data)
640{
641 if (!starts_with(basename, "tmp_obj_"))
642 fprintf(stderr, "bad sha1 file: %s\n", path);
643 return 0;
644}
645
70c49050 646static int fsck_subdir(unsigned int nr, const char *path, void *progress)
f0766bf9
JK
647{
648 display_progress(progress, nr + 1);
649 return 0;
650}
651
8a498a05
JH
652static void fsck_object_dir(const char *path)
653{
1e49f22f 654 struct progress *progress = NULL;
20f1eb6b
JS
655
656 if (verbose)
657 fprintf(stderr, "Checking object directory\n");
658
1e49f22f 659 if (show_progress)
754dbc43 660 progress = start_progress(_("Checking object directories"), 256);
f0766bf9
JK
661
662 for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
663 progress);
664 display_progress(progress, 256);
1e49f22f 665 stop_progress(&progress);
8a498a05
JH
666}
667
b29759d8
NTND
668static int fsck_head_link(const char *head_ref_name,
669 const char **head_points_at,
a8c754d4 670 struct object_id *head_oid)
c3330383 671{
8eb2d0be 672 int null_is_error = 0;
8eb2d0be 673
20f1eb6b 674 if (verbose)
b29759d8 675 fprintf(stderr, "Checking %s link\n", head_ref_name);
20f1eb6b 676
b29759d8 677 *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
a8c754d4 678 if (!*head_points_at) {
122f76f5 679 errors_found |= ERROR_REFS;
b29759d8 680 return error("Invalid %s", head_ref_name);
122f76f5 681 }
b29759d8 682 if (!strcmp(*head_points_at, head_ref_name))
8eb2d0be
JH
683 /* detached HEAD */
684 null_is_error = 1;
a8c754d4 685 else if (!starts_with(*head_points_at, "refs/heads/")) {
122f76f5 686 errors_found |= ERROR_REFS;
b29759d8
NTND
687 return error("%s points to something strange (%s)",
688 head_ref_name, *head_points_at);
122f76f5 689 }
a8c754d4 690 if (is_null_oid(head_oid)) {
122f76f5
JH
691 if (null_is_error) {
692 errors_found |= ERROR_REFS;
b29759d8
NTND
693 return error("%s: detached HEAD points at nothing",
694 head_ref_name);
122f76f5 695 }
b29759d8
NTND
696 fprintf(stderr, "notice: %s points to an unborn branch (%s)\n",
697 head_ref_name, *head_points_at + 11);
8eb2d0be 698 }
c3330383
LT
699 return 0;
700}
701
53dc3f3e
JH
702static int fsck_cache_tree(struct cache_tree *it)
703{
704 int i;
705 int err = 0;
706
20f1eb6b
JS
707 if (verbose)
708 fprintf(stderr, "Checking cache tree\n");
709
53dc3f3e 710 if (0 <= it->entry_count) {
109cd76d 711 struct object *obj = parse_object(the_repository, &it->oid);
6d60bbef
JH
712 if (!obj) {
713 error("%s: invalid sha1 pointer in cache-tree",
e0a92804 714 oid_to_hex(&it->oid));
122f76f5 715 errors_found |= ERROR_REFS;
6d60bbef
JH
716 return 1;
717 }
092c55d0 718 obj->flags |= USED;
90cf590f
JS
719 if (name_objects)
720 add_decoration(fsck_walk_options.object_names,
721 obj, xstrdup(":"));
a1cdc251 722 mark_object_reachable(obj);
1974632c 723 if (obj->type != OBJ_TREE)
53dc3f3e
JH
724 err |= objerror(obj, "non-tree in cache-tree");
725 }
726 for (i = 0; i < it->subtree_nr; i++)
727 err |= fsck_cache_tree(it->down[i]->cache_tree);
728 return err;
729}
730
76c1d9a0 731static void mark_object_for_connectivity(const struct object_id *oid)
3e3f8bd6 732{
76c1d9a0 733 struct object *obj = lookup_unknown_object(oid->hash);
3e3f8bd6
JK
734 obj->flags |= HAS_OBJ;
735}
736
76c1d9a0 737static int mark_loose_for_connectivity(const struct object_id *oid,
3e3f8bd6
JK
738 const char *path,
739 void *data)
740{
76c1d9a0 741 mark_object_for_connectivity(oid);
3e3f8bd6
JK
742 return 0;
743}
744
76c1d9a0 745static int mark_packed_for_connectivity(const struct object_id *oid,
3e3f8bd6
JK
746 struct packed_git *pack,
747 uint32_t pos,
748 void *data)
749{
76c1d9a0 750 mark_object_for_connectivity(oid);
3e3f8bd6
JK
751 return 0;
752}
753
5ac0a206 754static char const * const fsck_usage[] = {
9c9b4f2f 755 N_("git fsck [<options>] [<object>...]"),
5ac0a206
PH
756 NULL
757};
758
759static struct option fsck_opts[] = {
cf8fe315 760 OPT__VERBOSE(&verbose, N_("be verbose")),
d5d09d47 761 OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
cf8fe315 762 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
d5d09d47
SB
763 OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
764 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
765 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
766 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
767 OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
02976bf8 768 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
d5d09d47
SB
769 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
770 OPT_BOOL(0, "lost-found", &write_lost_and_found,
cf8fe315
NTND
771 N_("write dangling objects in .git/lost-found")),
772 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
90cf590f 773 OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
5ac0a206
PH
774 OPT_END(),
775};
e2b4f635 776
baffc0e7 777int cmd_fsck(int argc, const char **argv, const char *prefix)
20222118 778{
78e7b98f 779 int i;
e15ef669 780 struct alternate_object_database *alt;
20222118 781
8b4c0103
JT
782 /* fsck knows how to handle missing promisor objects */
783 fetch_if_missing = 0;
784
e2b4f635 785 errors_found = 0;
6ebd1caf 786 read_replace_refs = 0;
61e2b015 787
37782920 788 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
1e49f22f 789
22410549
JS
790 fsck_walk_options.walk = mark_object;
791 fsck_obj_options.walk = mark_used;
792 fsck_obj_options.error_func = fsck_error_func;
793 if (check_strict)
794 fsck_obj_options.strict = 1;
795
1e49f22f
NTND
796 if (show_progress == -1)
797 show_progress = isatty(2);
798 if (verbose)
799 show_progress = 0;
800
5ac0a206
PH
801 if (write_lost_and_found) {
802 check_full = 1;
803 include_reflogs = 0;
889262ea
LT
804 }
805
90cf590f
JS
806 if (name_objects)
807 fsck_walk_options.object_names =
808 xcalloc(1, sizeof(struct decoration));
809
2becf00f
JS
810 git_config(fsck_config, NULL);
811
3e3f8bd6
JK
812 if (connectivity_only) {
813 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
814 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);
815 } else {
031dc927
SB
816 struct alternate_object_database *alt_odb_list;
817
02976bf8 818 fsck_object_dir(get_object_directory());
e15ef669 819
0b209034 820 prepare_alt_odb(the_repository);
031dc927 821 alt_odb_list = the_repository->objects->alt_odb_list;
597f9134
JK
822 for (alt = alt_odb_list; alt; alt = alt->next)
823 fsck_object_dir(alt->path);
e15ef669 824
3e3f8bd6
JK
825 if (check_full) {
826 struct packed_git *p;
827 uint32_t total = 0, count = 0;
828 struct progress *progress = NULL;
e15ef669 829
3e3f8bd6 830 if (show_progress) {
454ea2e4 831 for (p = get_all_packs(the_repository); p;
a80d72db 832 p = p->next) {
3e3f8bd6
JK
833 if (open_pack_index(p))
834 continue;
835 total += p->num_objects;
836 }
1e49f22f 837
3e3f8bd6
JK
838 progress = start_progress(_("Checking objects"), total);
839 }
454ea2e4 840 for (p = get_all_packs(the_repository); p;
a80d72db 841 p = p->next) {
3e3f8bd6
JK
842 /* verify gives error messages itself */
843 if (verify_pack(p, fsck_obj_buffer,
844 progress, count))
845 errors_found |= ERROR_PACK;
846 count += p->num_objects;
1e49f22f 847 }
3e3f8bd6 848 stop_progress(&progress);
1e49f22f 849 }
1995b5e0
JK
850
851 if (fsck_finish(&fsck_obj_options))
852 errors_found |= ERROR_OBJECT;
bcee6fd8
LT
853 }
854
3aed2fda 855 for (i = 0; i < argc; i++) {
a6080a0a 856 const char *arg = argv[i];
aca6065c 857 struct object_id oid;
858 if (!get_oid(arg, &oid)) {
5abddd1e
SB
859 struct object *obj = lookup_object(the_repository,
860 oid.hash);
e1a1388d 861
c2d17b3b 862 if (!obj || !(obj->flags & HAS_OBJ)) {
096c9b8b
JT
863 if (is_promisor_object(&oid))
864 continue;
aca6065c 865 error("%s: object missing", oid_to_hex(&oid));
c6c7b16d 866 errors_found |= ERROR_OBJECT;
e1a1388d 867 continue;
c6c7b16d 868 }
e1a1388d 869
092c55d0 870 obj->flags |= USED;
90cf590f
JS
871 if (name_objects)
872 add_decoration(fsck_walk_options.object_names,
873 obj, xstrdup(arg));
271b8d25 874 mark_object_reachable(obj);
d9839e03
LT
875 continue;
876 }
f1f0d088 877 error("invalid parameter: expected sha1, got '%s'", arg);
c6c7b16d 878 errors_found |= ERROR_OBJECT;
d9839e03 879 }
d9839e03 880
1024932f 881 /*
d1af002d 882 * If we've not been given any explicit head information, do the
e7bd907d
LT
883 * default ones from .git/refs. We also consider the index file
884 * in this case (ie this implies --cache).
1024932f 885 */
c3271a0e 886 if (!argc) {
1024932f
LT
887 get_default_heads();
888 keep_cache_objects = 1;
889 }
890
ae7c0c92 891 if (keep_cache_objects) {
a33fc72f 892 verify_index_checksum = 1;
00ec50e5 893 verify_ce_order = 1;
ae7c0c92
JH
894 read_cache();
895 for (i = 0; i < active_nr; i++) {
8d9721c8
LT
896 unsigned int mode;
897 struct blob *blob;
ae7c0c92 898 struct object *obj;
8d9721c8 899
7a51ed66 900 mode = active_cache[i]->ce_mode;
302b9282 901 if (S_ISGITLINK(mode))
8d9721c8 902 continue;
da14a7ff
SB
903 blob = lookup_blob(the_repository,
904 &active_cache[i]->oid);
ae7c0c92
JH
905 if (!blob)
906 continue;
907 obj = &blob->object;
092c55d0 908 obj->flags |= USED;
90cf590f
JS
909 if (name_objects)
910 add_decoration(fsck_walk_options.object_names,
911 obj,
912 xstrfmt(":%s", active_cache[i]->name));
271b8d25 913 mark_object_reachable(obj);
ae7c0c92 914 }
53dc3f3e
JH
915 if (active_cache_tree)
916 fsck_cache_tree(active_cache_tree);
ae7c0c92
JH
917 }
918
8ba0bbb2 919 check_connectivity();
e0fd51e1 920
dade47c0 921 if (!git_config_get_bool("core.commitgraph", &i) && i) {
e0fd51e1
DS
922 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
923 const char *verify_argv[] = { "commit-graph", "verify", NULL, NULL, NULL };
924
925 commit_graph_verify.argv = verify_argv;
926 commit_graph_verify.git_cmd = 1;
927 if (run_command(&commit_graph_verify))
928 errors_found |= ERROR_COMMIT_GRAPH;
929
930 prepare_alt_odb(the_repository);
931 for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
932 verify_argv[2] = "--object-dir";
933 verify_argv[3] = alt->path;
934 if (run_command(&commit_graph_verify))
935 errors_found |= ERROR_COMMIT_GRAPH;
936 }
937 }
938
66ec0390
DS
939 if (!git_config_get_bool("core.multipackindex", &i) && i) {
940 struct child_process midx_verify = CHILD_PROCESS_INIT;
941 const char *midx_argv[] = { "multi-pack-index", "verify", NULL, NULL, NULL };
942
943 midx_verify.argv = midx_argv;
944 midx_verify.git_cmd = 1;
945 if (run_command(&midx_verify))
946 errors_found |= ERROR_COMMIT_GRAPH;
947
948 prepare_alt_odb(the_repository);
949 for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
950 midx_argv[2] = "--object-dir";
951 midx_argv[3] = alt->path;
952 if (run_command(&midx_verify))
953 errors_found |= ERROR_COMMIT_GRAPH;
954 }
955 }
956
e2b4f635 957 return errors_found;
20222118 958}