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