]>
Commit | Line | Data |
---|---|---|
1 | #define USE_THE_REPOSITORY_VARIABLE | |
2 | #include "builtin.h" | |
3 | #include "gettext.h" | |
4 | #include "hex.h" | |
5 | #include "config.h" | |
6 | #include "commit.h" | |
7 | #include "tree.h" | |
8 | #include "blob.h" | |
9 | #include "tag.h" | |
10 | #include "refs.h" | |
11 | #include "pack.h" | |
12 | #include "cache-tree.h" | |
13 | #include "fsck.h" | |
14 | #include "parse-options.h" | |
15 | #include "progress.h" | |
16 | #include "streaming.h" | |
17 | #include "packfile.h" | |
18 | #include "object-file.h" | |
19 | #include "object-name.h" | |
20 | #include "object-store.h" | |
21 | #include "path.h" | |
22 | #include "read-cache-ll.h" | |
23 | #include "replace-object.h" | |
24 | #include "resolve-undo.h" | |
25 | #include "run-command.h" | |
26 | #include "sparse-index.h" | |
27 | #include "worktree.h" | |
28 | #include "pack-revindex.h" | |
29 | #include "pack-bitmap.h" | |
30 | ||
31 | #define REACHABLE 0x0001 | |
32 | #define SEEN 0x0002 | |
33 | #define HAS_OBJ 0x0004 | |
34 | /* This flag is set if something points to this object. */ | |
35 | #define USED 0x0008 | |
36 | ||
37 | static int show_root; | |
38 | static int show_tags; | |
39 | static int show_unreachable; | |
40 | static int include_reflogs = 1; | |
41 | static int check_full = 1; | |
42 | static int connectivity_only; | |
43 | static int check_strict; | |
44 | static int keep_cache_objects; | |
45 | static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT; | |
46 | static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT; | |
47 | static int errors_found; | |
48 | static int write_lost_and_found; | |
49 | static int verbose; | |
50 | static int show_progress = -1; | |
51 | static int show_dangling = 1; | |
52 | static int name_objects; | |
53 | static int check_references = 1; | |
54 | #define ERROR_OBJECT 01 | |
55 | #define ERROR_REACHABLE 02 | |
56 | #define ERROR_PACK 04 | |
57 | #define ERROR_REFS 010 | |
58 | #define ERROR_COMMIT_GRAPH 020 | |
59 | #define ERROR_MULTI_PACK_INDEX 040 | |
60 | #define ERROR_PACK_REV_INDEX 0100 | |
61 | #define ERROR_BITMAP 0200 | |
62 | ||
63 | static const char *describe_object(const struct object_id *oid) | |
64 | { | |
65 | return fsck_describe_object(&fsck_walk_options, oid); | |
66 | } | |
67 | ||
68 | static const char *printable_type(const struct object_id *oid, | |
69 | enum object_type type) | |
70 | { | |
71 | const char *ret; | |
72 | ||
73 | if (type == OBJ_NONE) | |
74 | type = oid_object_info(the_repository, oid, NULL); | |
75 | ||
76 | ret = type_name(type); | |
77 | if (!ret) | |
78 | ret = _("unknown"); | |
79 | ||
80 | return ret; | |
81 | } | |
82 | ||
83 | static int objerror(struct object *obj, const char *err) | |
84 | { | |
85 | errors_found |= ERROR_OBJECT; | |
86 | /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */ | |
87 | fprintf_ln(stderr, _("error in %s %s: %s"), | |
88 | printable_type(&obj->oid, obj->type), | |
89 | describe_object(&obj->oid), err); | |
90 | return -1; | |
91 | } | |
92 | ||
93 | static int fsck_objects_error_func(struct fsck_options *o UNUSED, | |
94 | void *fsck_report, | |
95 | enum fsck_msg_type msg_type, | |
96 | enum fsck_msg_id msg_id UNUSED, | |
97 | const char *message) | |
98 | { | |
99 | struct fsck_object_report *report = fsck_report; | |
100 | const struct object_id *oid = report->oid; | |
101 | enum object_type object_type = report->object_type; | |
102 | ||
103 | switch (msg_type) { | |
104 | case FSCK_WARN: | |
105 | /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */ | |
106 | fprintf_ln(stderr, _("warning in %s %s: %s"), | |
107 | printable_type(oid, object_type), | |
108 | describe_object(oid), message); | |
109 | return 0; | |
110 | case FSCK_ERROR: | |
111 | /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */ | |
112 | fprintf_ln(stderr, _("error in %s %s: %s"), | |
113 | printable_type(oid, object_type), | |
114 | describe_object(oid), message); | |
115 | return 1; | |
116 | default: | |
117 | BUG("%d (FSCK_IGNORE?) should never trigger this callback", | |
118 | msg_type); | |
119 | } | |
120 | } | |
121 | ||
122 | static struct object_array pending; | |
123 | ||
124 | static int mark_object(struct object *obj, enum object_type type, | |
125 | void *data, struct fsck_options *options UNUSED) | |
126 | { | |
127 | struct object *parent = data; | |
128 | ||
129 | /* | |
130 | * The only case data is NULL or type is OBJ_ANY is when | |
131 | * mark_object_reachable() calls us. All the callers of | |
132 | * that function has non-NULL obj hence ... | |
133 | */ | |
134 | if (!obj) { | |
135 | /* ... these references to parent->fld are safe here */ | |
136 | printf_ln(_("broken link from %7s %s"), | |
137 | printable_type(&parent->oid, parent->type), | |
138 | describe_object(&parent->oid)); | |
139 | printf_ln(_("broken link from %7s %s"), | |
140 | (type == OBJ_ANY ? _("unknown") : type_name(type)), | |
141 | _("unknown")); | |
142 | errors_found |= ERROR_REACHABLE; | |
143 | return 1; | |
144 | } | |
145 | ||
146 | if (type != OBJ_ANY && obj->type != type) | |
147 | /* ... and the reference to parent is safe here */ | |
148 | objerror(parent, _("wrong object type in link")); | |
149 | ||
150 | if (obj->flags & REACHABLE) | |
151 | return 0; | |
152 | obj->flags |= REACHABLE; | |
153 | ||
154 | if (is_promisor_object(the_repository, &obj->oid)) | |
155 | /* | |
156 | * Further recursion does not need to be performed on this | |
157 | * object since it is a promisor object (so it does not need to | |
158 | * be added to "pending"). | |
159 | */ | |
160 | return 0; | |
161 | ||
162 | if (!(obj->flags & HAS_OBJ)) { | |
163 | if (parent && !has_object(the_repository, &obj->oid, 1)) { | |
164 | printf_ln(_("broken link from %7s %s\n" | |
165 | " to %7s %s"), | |
166 | printable_type(&parent->oid, parent->type), | |
167 | describe_object(&parent->oid), | |
168 | printable_type(&obj->oid, obj->type), | |
169 | describe_object(&obj->oid)); | |
170 | errors_found |= ERROR_REACHABLE; | |
171 | } | |
172 | return 1; | |
173 | } | |
174 | ||
175 | add_object_array(obj, NULL, &pending); | |
176 | return 0; | |
177 | } | |
178 | ||
179 | static void mark_object_reachable(struct object *obj) | |
180 | { | |
181 | mark_object(obj, OBJ_ANY, NULL, NULL); | |
182 | } | |
183 | ||
184 | static int traverse_one_object(struct object *obj) | |
185 | { | |
186 | int result = fsck_walk(obj, obj, &fsck_walk_options); | |
187 | ||
188 | if (obj->type == OBJ_TREE) { | |
189 | struct tree *tree = (struct tree *)obj; | |
190 | free_tree_buffer(tree); | |
191 | } | |
192 | return result; | |
193 | } | |
194 | ||
195 | static int traverse_reachable(void) | |
196 | { | |
197 | struct progress *progress = NULL; | |
198 | unsigned int nr = 0; | |
199 | int result = 0; | |
200 | if (show_progress) | |
201 | progress = start_delayed_progress(the_repository, | |
202 | _("Checking connectivity"), 0); | |
203 | while (pending.nr) { | |
204 | result |= traverse_one_object(object_array_pop(&pending)); | |
205 | display_progress(progress, ++nr); | |
206 | } | |
207 | stop_progress(&progress); | |
208 | return !!result; | |
209 | } | |
210 | ||
211 | static int mark_used(struct object *obj, enum object_type type UNUSED, | |
212 | void *data UNUSED, struct fsck_options *options UNUSED) | |
213 | { | |
214 | if (!obj) | |
215 | return 1; | |
216 | obj->flags |= USED; | |
217 | return 0; | |
218 | } | |
219 | ||
220 | static void mark_unreachable_referents(const struct object_id *oid) | |
221 | { | |
222 | struct fsck_options options = FSCK_OPTIONS_DEFAULT; | |
223 | struct object *obj = lookup_object(the_repository, oid); | |
224 | ||
225 | if (!obj || !(obj->flags & HAS_OBJ)) | |
226 | return; /* not part of our original set */ | |
227 | if (obj->flags & REACHABLE) | |
228 | return; /* reachable objects already traversed */ | |
229 | ||
230 | /* | |
231 | * Avoid passing OBJ_NONE to fsck_walk, which will parse the object | |
232 | * (and we want to avoid parsing blobs). | |
233 | */ | |
234 | if (obj->type == OBJ_NONE) { | |
235 | enum object_type type = oid_object_info(the_repository, | |
236 | &obj->oid, NULL); | |
237 | if (type > 0) | |
238 | object_as_type(obj, type, 0); | |
239 | } | |
240 | ||
241 | options.walk = mark_used; | |
242 | fsck_walk(obj, NULL, &options); | |
243 | if (obj->type == OBJ_TREE) | |
244 | free_tree_buffer((struct tree *)obj); | |
245 | } | |
246 | ||
247 | static int mark_loose_unreachable_referents(const struct object_id *oid, | |
248 | const char *path UNUSED, | |
249 | void *data UNUSED) | |
250 | { | |
251 | mark_unreachable_referents(oid); | |
252 | return 0; | |
253 | } | |
254 | ||
255 | static int mark_packed_unreachable_referents(const struct object_id *oid, | |
256 | struct packed_git *pack UNUSED, | |
257 | uint32_t pos UNUSED, | |
258 | void *data UNUSED) | |
259 | { | |
260 | mark_unreachable_referents(oid); | |
261 | return 0; | |
262 | } | |
263 | ||
264 | /* | |
265 | * Check a single reachable object | |
266 | */ | |
267 | static void check_reachable_object(struct object *obj) | |
268 | { | |
269 | /* | |
270 | * We obviously want the object to be parsed, | |
271 | * except if it was in a pack-file and we didn't | |
272 | * do a full fsck | |
273 | */ | |
274 | if (!(obj->flags & HAS_OBJ)) { | |
275 | if (is_promisor_object(the_repository, &obj->oid)) | |
276 | return; | |
277 | if (has_object_pack(the_repository, &obj->oid)) | |
278 | return; /* it is in pack - forget about it */ | |
279 | printf_ln(_("missing %s %s"), | |
280 | printable_type(&obj->oid, obj->type), | |
281 | describe_object(&obj->oid)); | |
282 | errors_found |= ERROR_REACHABLE; | |
283 | return; | |
284 | } | |
285 | } | |
286 | ||
287 | /* | |
288 | * Check a single unreachable object | |
289 | */ | |
290 | static void check_unreachable_object(struct object *obj) | |
291 | { | |
292 | /* | |
293 | * Missing unreachable object? Ignore it. It's not like | |
294 | * we miss it (since it can't be reached), nor do we want | |
295 | * to complain about it being unreachable (since it does | |
296 | * not exist). | |
297 | */ | |
298 | if (!(obj->flags & HAS_OBJ)) | |
299 | return; | |
300 | ||
301 | /* | |
302 | * Unreachable object that exists? Show it if asked to, | |
303 | * since this is something that is prunable. | |
304 | */ | |
305 | if (show_unreachable) { | |
306 | printf_ln(_("unreachable %s %s"), | |
307 | printable_type(&obj->oid, obj->type), | |
308 | describe_object(&obj->oid)); | |
309 | return; | |
310 | } | |
311 | ||
312 | /* | |
313 | * "!USED" means that nothing at all points to it, including | |
314 | * other unreachable objects. In other words, it's the "tip" | |
315 | * of some set of unreachable objects, usually a commit that | |
316 | * got dropped. | |
317 | * | |
318 | * Such starting points are more interesting than some random | |
319 | * set of unreachable objects, so we show them even if the user | |
320 | * hasn't asked for _all_ unreachable objects. If you have | |
321 | * deleted a branch by mistake, this is a prime candidate to | |
322 | * start looking at, for example. | |
323 | */ | |
324 | if (!(obj->flags & USED)) { | |
325 | if (show_dangling) | |
326 | printf_ln(_("dangling %s %s"), | |
327 | printable_type(&obj->oid, obj->type), | |
328 | describe_object(&obj->oid)); | |
329 | if (write_lost_and_found) { | |
330 | char *filename = repo_git_path(the_repository, "lost-found/%s/%s", | |
331 | obj->type == OBJ_COMMIT ? "commit" : "other", | |
332 | describe_object(&obj->oid)); | |
333 | FILE *f; | |
334 | ||
335 | if (safe_create_leading_directories_const(the_repository, filename)) { | |
336 | error(_("could not create lost-found")); | |
337 | free(filename); | |
338 | return; | |
339 | } | |
340 | f = xfopen(filename, "w"); | |
341 | if (obj->type == OBJ_BLOB) { | |
342 | if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1)) | |
343 | die_errno(_("could not write '%s'"), filename); | |
344 | } else | |
345 | fprintf(f, "%s\n", describe_object(&obj->oid)); | |
346 | if (fclose(f)) | |
347 | die_errno(_("could not finish '%s'"), | |
348 | filename); | |
349 | free(filename); | |
350 | } | |
351 | return; | |
352 | } | |
353 | ||
354 | /* | |
355 | * Otherwise? It's there, it's unreachable, and some other unreachable | |
356 | * object points to it. Ignore it - it's not interesting, and we showed | |
357 | * all the interesting cases above. | |
358 | */ | |
359 | } | |
360 | ||
361 | static void check_object(struct object *obj) | |
362 | { | |
363 | if (verbose) | |
364 | fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid)); | |
365 | ||
366 | if (obj->flags & REACHABLE) | |
367 | check_reachable_object(obj); | |
368 | else | |
369 | check_unreachable_object(obj); | |
370 | } | |
371 | ||
372 | static void check_connectivity(void) | |
373 | { | |
374 | int i, max; | |
375 | ||
376 | /* Traverse the pending reachable objects */ | |
377 | traverse_reachable(); | |
378 | ||
379 | /* | |
380 | * With --connectivity-only, we won't have actually opened and marked | |
381 | * unreachable objects with USED. Do that now to make --dangling, etc | |
382 | * accurate. | |
383 | */ | |
384 | if (connectivity_only && (show_dangling || write_lost_and_found)) { | |
385 | /* | |
386 | * Even though we already have a "struct object" for each of | |
387 | * these in memory, we must not iterate over the internal | |
388 | * object hash as we do below. Our loop would potentially | |
389 | * resize the hash, making our iteration invalid. | |
390 | * | |
391 | * Instead, we'll just go back to the source list of objects, | |
392 | * and ignore any that weren't present in our earlier | |
393 | * traversal. | |
394 | */ | |
395 | for_each_loose_object(mark_loose_unreachable_referents, NULL, 0); | |
396 | for_each_packed_object(the_repository, | |
397 | mark_packed_unreachable_referents, | |
398 | NULL, | |
399 | 0); | |
400 | } | |
401 | ||
402 | /* Look up all the requirements, warn about missing objects.. */ | |
403 | max = get_max_object_index(the_repository); | |
404 | if (verbose) | |
405 | fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max); | |
406 | ||
407 | for (i = 0; i < max; i++) { | |
408 | struct object *obj = get_indexed_object(the_repository, i); | |
409 | ||
410 | if (obj) | |
411 | check_object(obj); | |
412 | } | |
413 | } | |
414 | ||
415 | static int fsck_obj(struct object *obj, void *buffer, unsigned long size) | |
416 | { | |
417 | int err; | |
418 | ||
419 | if (obj->flags & SEEN) | |
420 | return 0; | |
421 | obj->flags |= SEEN; | |
422 | ||
423 | if (verbose) | |
424 | fprintf_ln(stderr, _("Checking %s %s"), | |
425 | printable_type(&obj->oid, obj->type), | |
426 | describe_object(&obj->oid)); | |
427 | ||
428 | if (fsck_walk(obj, NULL, &fsck_obj_options)) | |
429 | objerror(obj, _("broken links")); | |
430 | err = fsck_object(obj, buffer, size, &fsck_obj_options); | |
431 | if (err) | |
432 | goto out; | |
433 | ||
434 | if (obj->type == OBJ_COMMIT) { | |
435 | struct commit *commit = (struct commit *) obj; | |
436 | ||
437 | if (!commit->parents && show_root) | |
438 | printf_ln(_("root %s"), | |
439 | describe_object(&commit->object.oid)); | |
440 | } | |
441 | ||
442 | if (obj->type == OBJ_TAG) { | |
443 | struct tag *tag = (struct tag *) obj; | |
444 | ||
445 | if (show_tags && tag->tagged) { | |
446 | printf_ln(_("tagged %s %s (%s) in %s"), | |
447 | printable_type(&tag->tagged->oid, tag->tagged->type), | |
448 | describe_object(&tag->tagged->oid), | |
449 | tag->tag, | |
450 | describe_object(&tag->object.oid)); | |
451 | } | |
452 | } | |
453 | ||
454 | out: | |
455 | if (obj->type == OBJ_TREE) | |
456 | free_tree_buffer((struct tree *)obj); | |
457 | return err; | |
458 | } | |
459 | ||
460 | static int fsck_obj_buffer(const struct object_id *oid, enum object_type type, | |
461 | unsigned long size, void *buffer, int *eaten) | |
462 | { | |
463 | /* | |
464 | * Note, buffer may be NULL if type is OBJ_BLOB. See | |
465 | * verify_packfile(), data_valid variable for details. | |
466 | */ | |
467 | struct object *obj; | |
468 | obj = parse_object_buffer(the_repository, oid, type, size, buffer, | |
469 | eaten); | |
470 | if (!obj) { | |
471 | errors_found |= ERROR_OBJECT; | |
472 | return error(_("%s: object corrupt or missing"), | |
473 | oid_to_hex(oid)); | |
474 | } | |
475 | obj->flags &= ~(REACHABLE | SEEN); | |
476 | obj->flags |= HAS_OBJ; | |
477 | return fsck_obj(obj, buffer, size); | |
478 | } | |
479 | ||
480 | static int default_refs; | |
481 | ||
482 | static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid, | |
483 | timestamp_t timestamp) | |
484 | { | |
485 | struct object *obj; | |
486 | ||
487 | if (!is_null_oid(oid)) { | |
488 | obj = lookup_object(the_repository, oid); | |
489 | if (obj && (obj->flags & HAS_OBJ)) { | |
490 | if (timestamp) | |
491 | fsck_put_object_name(&fsck_walk_options, oid, | |
492 | "%s@{%"PRItime"}", | |
493 | refname, timestamp); | |
494 | obj->flags |= USED; | |
495 | mark_object_reachable(obj); | |
496 | } else if (!is_promisor_object(the_repository, oid)) { | |
497 | error(_("%s: invalid reflog entry %s"), | |
498 | refname, oid_to_hex(oid)); | |
499 | errors_found |= ERROR_REACHABLE; | |
500 | } | |
501 | } | |
502 | } | |
503 | ||
504 | static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid, | |
505 | const char *email UNUSED, | |
506 | timestamp_t timestamp, int tz UNUSED, | |
507 | const char *message UNUSED, void *cb_data) | |
508 | { | |
509 | const char *refname = cb_data; | |
510 | ||
511 | if (verbose) | |
512 | fprintf_ln(stderr, _("Checking reflog %s->%s"), | |
513 | oid_to_hex(ooid), oid_to_hex(noid)); | |
514 | ||
515 | fsck_handle_reflog_oid(refname, ooid, 0); | |
516 | fsck_handle_reflog_oid(refname, noid, timestamp); | |
517 | return 0; | |
518 | } | |
519 | ||
520 | static int fsck_handle_reflog(const char *logname, void *cb_data) | |
521 | { | |
522 | struct strbuf refname = STRBUF_INIT; | |
523 | ||
524 | strbuf_worktree_ref(cb_data, &refname, logname); | |
525 | refs_for_each_reflog_ent(get_main_ref_store(the_repository), | |
526 | refname.buf, fsck_handle_reflog_ent, | |
527 | refname.buf); | |
528 | strbuf_release(&refname); | |
529 | return 0; | |
530 | } | |
531 | ||
532 | static int fsck_handle_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid, | |
533 | int flag UNUSED, void *cb_data UNUSED) | |
534 | { | |
535 | struct object *obj; | |
536 | ||
537 | obj = parse_object(the_repository, oid); | |
538 | if (!obj) { | |
539 | if (is_promisor_object(the_repository, oid)) { | |
540 | /* | |
541 | * Increment default_refs anyway, because this is a | |
542 | * valid ref. | |
543 | */ | |
544 | default_refs++; | |
545 | return 0; | |
546 | } | |
547 | error(_("%s: invalid sha1 pointer %s"), | |
548 | refname, oid_to_hex(oid)); | |
549 | errors_found |= ERROR_REACHABLE; | |
550 | /* We'll continue with the rest despite the error.. */ | |
551 | return 0; | |
552 | } | |
553 | if (obj->type != OBJ_COMMIT && is_branch(refname)) { | |
554 | error(_("%s: not a commit"), refname); | |
555 | errors_found |= ERROR_REFS; | |
556 | } | |
557 | default_refs++; | |
558 | obj->flags |= USED; | |
559 | fsck_put_object_name(&fsck_walk_options, | |
560 | oid, "%s", refname); | |
561 | mark_object_reachable(obj); | |
562 | ||
563 | return 0; | |
564 | } | |
565 | ||
566 | static int fsck_head_link(const char *head_ref_name, | |
567 | const char **head_points_at, | |
568 | struct object_id *head_oid); | |
569 | ||
570 | static void get_default_heads(void) | |
571 | { | |
572 | struct worktree **worktrees, **p; | |
573 | const char *head_points_at; | |
574 | struct object_id head_oid; | |
575 | ||
576 | refs_for_each_rawref(get_main_ref_store(the_repository), | |
577 | fsck_handle_ref, NULL); | |
578 | ||
579 | worktrees = get_worktrees(); | |
580 | for (p = worktrees; *p; p++) { | |
581 | struct worktree *wt = *p; | |
582 | struct strbuf ref = STRBUF_INIT; | |
583 | ||
584 | strbuf_worktree_ref(wt, &ref, "HEAD"); | |
585 | fsck_head_link(ref.buf, &head_points_at, &head_oid); | |
586 | if (head_points_at && !is_null_oid(&head_oid)) | |
587 | fsck_handle_ref(ref.buf, NULL, &head_oid, 0, NULL); | |
588 | strbuf_release(&ref); | |
589 | ||
590 | if (include_reflogs) | |
591 | refs_for_each_reflog(get_worktree_ref_store(wt), | |
592 | fsck_handle_reflog, wt); | |
593 | } | |
594 | free_worktrees(worktrees); | |
595 | ||
596 | /* | |
597 | * Not having any default heads isn't really fatal, but | |
598 | * it does mean that "--unreachable" no longer makes any | |
599 | * sense (since in this case everything will obviously | |
600 | * be unreachable by definition. | |
601 | * | |
602 | * Showing dangling objects is valid, though (as those | |
603 | * dangling objects are likely lost heads). | |
604 | * | |
605 | * So we just print a warning about it, and clear the | |
606 | * "show_unreachable" flag. | |
607 | */ | |
608 | if (!default_refs) { | |
609 | fprintf_ln(stderr, _("notice: No default references")); | |
610 | show_unreachable = 0; | |
611 | } | |
612 | } | |
613 | ||
614 | struct for_each_loose_cb | |
615 | { | |
616 | struct progress *progress; | |
617 | }; | |
618 | ||
619 | static int fsck_loose(const struct object_id *oid, const char *path, | |
620 | void *data UNUSED) | |
621 | { | |
622 | struct object *obj; | |
623 | enum object_type type = OBJ_NONE; | |
624 | unsigned long size; | |
625 | void *contents = NULL; | |
626 | int eaten; | |
627 | struct object_info oi = OBJECT_INFO_INIT; | |
628 | struct object_id real_oid = *null_oid(the_hash_algo); | |
629 | int err = 0; | |
630 | ||
631 | oi.sizep = &size; | |
632 | oi.typep = &type; | |
633 | ||
634 | if (read_loose_object(path, oid, &real_oid, &contents, &oi) < 0) { | |
635 | if (contents && !oideq(&real_oid, oid)) | |
636 | err = error(_("%s: hash-path mismatch, found at: %s"), | |
637 | oid_to_hex(&real_oid), path); | |
638 | else | |
639 | err = error(_("%s: object corrupt or missing: %s"), | |
640 | oid_to_hex(oid), path); | |
641 | } | |
642 | if (err < 0) { | |
643 | errors_found |= ERROR_OBJECT; | |
644 | free(contents); | |
645 | return 0; /* keep checking other objects */ | |
646 | } | |
647 | ||
648 | if (!contents && type != OBJ_BLOB) | |
649 | BUG("read_loose_object streamed a non-blob"); | |
650 | ||
651 | obj = parse_object_buffer(the_repository, oid, type, size, | |
652 | contents, &eaten); | |
653 | ||
654 | if (!obj) { | |
655 | errors_found |= ERROR_OBJECT; | |
656 | error(_("%s: object could not be parsed: %s"), | |
657 | oid_to_hex(oid), path); | |
658 | if (!eaten) | |
659 | free(contents); | |
660 | return 0; /* keep checking other objects */ | |
661 | } | |
662 | ||
663 | obj->flags &= ~(REACHABLE | SEEN); | |
664 | obj->flags |= HAS_OBJ; | |
665 | if (fsck_obj(obj, contents, size)) | |
666 | errors_found |= ERROR_OBJECT; | |
667 | ||
668 | if (!eaten) | |
669 | free(contents); | |
670 | return 0; /* keep checking other objects, even if we saw an error */ | |
671 | } | |
672 | ||
673 | static int fsck_cruft(const char *basename, const char *path, | |
674 | void *data UNUSED) | |
675 | { | |
676 | if (!starts_with(basename, "tmp_obj_")) | |
677 | fprintf_ln(stderr, _("bad sha1 file: %s"), path); | |
678 | return 0; | |
679 | } | |
680 | ||
681 | static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data) | |
682 | { | |
683 | struct for_each_loose_cb *cb_data = data; | |
684 | struct progress *progress = cb_data->progress; | |
685 | display_progress(progress, nr + 1); | |
686 | return 0; | |
687 | } | |
688 | ||
689 | static void fsck_object_dir(const char *path) | |
690 | { | |
691 | struct progress *progress = NULL; | |
692 | struct for_each_loose_cb cb_data = { | |
693 | .progress = progress, | |
694 | }; | |
695 | ||
696 | if (verbose) | |
697 | fprintf_ln(stderr, _("Checking object directory")); | |
698 | ||
699 | if (show_progress) | |
700 | progress = start_progress(the_repository, | |
701 | _("Checking object directories"), 256); | |
702 | ||
703 | for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir, | |
704 | &cb_data); | |
705 | display_progress(progress, 256); | |
706 | stop_progress(&progress); | |
707 | } | |
708 | ||
709 | static int fsck_head_link(const char *head_ref_name, | |
710 | const char **head_points_at, | |
711 | struct object_id *head_oid) | |
712 | { | |
713 | int null_is_error = 0; | |
714 | ||
715 | if (verbose) | |
716 | fprintf_ln(stderr, _("Checking %s link"), head_ref_name); | |
717 | ||
718 | *head_points_at = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), | |
719 | head_ref_name, 0, head_oid, | |
720 | NULL); | |
721 | if (!*head_points_at) { | |
722 | errors_found |= ERROR_REFS; | |
723 | return error(_("invalid %s"), head_ref_name); | |
724 | } | |
725 | if (!strcmp(*head_points_at, head_ref_name)) | |
726 | /* detached HEAD */ | |
727 | null_is_error = 1; | |
728 | else if (!starts_with(*head_points_at, "refs/heads/")) { | |
729 | errors_found |= ERROR_REFS; | |
730 | return error(_("%s points to something strange (%s)"), | |
731 | head_ref_name, *head_points_at); | |
732 | } | |
733 | if (is_null_oid(head_oid)) { | |
734 | if (null_is_error) { | |
735 | errors_found |= ERROR_REFS; | |
736 | return error(_("%s: detached HEAD points at nothing"), | |
737 | head_ref_name); | |
738 | } | |
739 | fprintf_ln(stderr, | |
740 | _("notice: %s points to an unborn branch (%s)"), | |
741 | head_ref_name, *head_points_at + 11); | |
742 | } | |
743 | return 0; | |
744 | } | |
745 | ||
746 | static int fsck_cache_tree(struct cache_tree *it, const char *index_path) | |
747 | { | |
748 | int i; | |
749 | int err = 0; | |
750 | ||
751 | if (verbose) | |
752 | fprintf_ln(stderr, _("Checking cache tree of %s"), index_path); | |
753 | ||
754 | if (0 <= it->entry_count) { | |
755 | struct object *obj = parse_object(the_repository, &it->oid); | |
756 | if (!obj) { | |
757 | error(_("%s: invalid sha1 pointer in cache-tree of %s"), | |
758 | oid_to_hex(&it->oid), index_path); | |
759 | errors_found |= ERROR_REFS; | |
760 | return 1; | |
761 | } | |
762 | obj->flags |= USED; | |
763 | fsck_put_object_name(&fsck_walk_options, &it->oid, ":"); | |
764 | mark_object_reachable(obj); | |
765 | if (obj->type != OBJ_TREE) | |
766 | err |= objerror(obj, _("non-tree in cache-tree")); | |
767 | } | |
768 | for (i = 0; i < it->subtree_nr; i++) | |
769 | err |= fsck_cache_tree(it->down[i]->cache_tree, index_path); | |
770 | return err; | |
771 | } | |
772 | ||
773 | static int fsck_resolve_undo(struct index_state *istate, | |
774 | const char *index_path) | |
775 | { | |
776 | struct string_list_item *item; | |
777 | struct string_list *resolve_undo = istate->resolve_undo; | |
778 | ||
779 | if (!resolve_undo) | |
780 | return 0; | |
781 | ||
782 | for_each_string_list_item(item, resolve_undo) { | |
783 | const char *path = item->string; | |
784 | struct resolve_undo_info *ru = item->util; | |
785 | int i; | |
786 | ||
787 | if (!ru) | |
788 | continue; | |
789 | for (i = 0; i < 3; i++) { | |
790 | struct object *obj; | |
791 | ||
792 | if (!ru->mode[i] || !S_ISREG(ru->mode[i])) | |
793 | continue; | |
794 | ||
795 | obj = parse_object(the_repository, &ru->oid[i]); | |
796 | if (!obj) { | |
797 | error(_("%s: invalid sha1 pointer in resolve-undo of %s"), | |
798 | oid_to_hex(&ru->oid[i]), | |
799 | index_path); | |
800 | errors_found |= ERROR_REFS; | |
801 | continue; | |
802 | } | |
803 | obj->flags |= USED; | |
804 | fsck_put_object_name(&fsck_walk_options, &ru->oid[i], | |
805 | ":(%d):%s", i, path); | |
806 | mark_object_reachable(obj); | |
807 | } | |
808 | } | |
809 | return 0; | |
810 | } | |
811 | ||
812 | static void fsck_index(struct index_state *istate, const char *index_path, | |
813 | int is_current_worktree) | |
814 | { | |
815 | unsigned int i; | |
816 | ||
817 | /* TODO: audit for interaction with sparse-index. */ | |
818 | ensure_full_index(istate); | |
819 | for (i = 0; i < istate->cache_nr; i++) { | |
820 | unsigned int mode; | |
821 | struct blob *blob; | |
822 | struct object *obj; | |
823 | ||
824 | mode = istate->cache[i]->ce_mode; | |
825 | if (S_ISGITLINK(mode)) | |
826 | continue; | |
827 | blob = lookup_blob(the_repository, | |
828 | &istate->cache[i]->oid); | |
829 | if (!blob) | |
830 | continue; | |
831 | obj = &blob->object; | |
832 | obj->flags |= USED; | |
833 | fsck_put_object_name(&fsck_walk_options, &obj->oid, | |
834 | "%s:%s", | |
835 | is_current_worktree ? "" : index_path, | |
836 | istate->cache[i]->name); | |
837 | mark_object_reachable(obj); | |
838 | } | |
839 | if (istate->cache_tree) | |
840 | fsck_cache_tree(istate->cache_tree, index_path); | |
841 | fsck_resolve_undo(istate, index_path); | |
842 | } | |
843 | ||
844 | static void mark_object_for_connectivity(const struct object_id *oid) | |
845 | { | |
846 | struct object *obj = lookup_unknown_object(the_repository, oid); | |
847 | obj->flags |= HAS_OBJ; | |
848 | } | |
849 | ||
850 | static int mark_loose_for_connectivity(const struct object_id *oid, | |
851 | const char *path UNUSED, | |
852 | void *data UNUSED) | |
853 | { | |
854 | mark_object_for_connectivity(oid); | |
855 | return 0; | |
856 | } | |
857 | ||
858 | static int mark_packed_for_connectivity(const struct object_id *oid, | |
859 | struct packed_git *pack UNUSED, | |
860 | uint32_t pos UNUSED, | |
861 | void *data UNUSED) | |
862 | { | |
863 | mark_object_for_connectivity(oid); | |
864 | return 0; | |
865 | } | |
866 | ||
867 | static int check_pack_rev_indexes(struct repository *r, int show_progress) | |
868 | { | |
869 | struct progress *progress = NULL; | |
870 | uint32_t pack_count = 0; | |
871 | int res = 0; | |
872 | ||
873 | if (show_progress) { | |
874 | for (struct packed_git *p = get_all_packs(r); p; p = p->next) | |
875 | pack_count++; | |
876 | progress = start_delayed_progress(the_repository, | |
877 | "Verifying reverse pack-indexes", pack_count); | |
878 | pack_count = 0; | |
879 | } | |
880 | ||
881 | for (struct packed_git *p = get_all_packs(r); p; p = p->next) { | |
882 | int load_error = load_pack_revindex_from_disk(p); | |
883 | ||
884 | if (load_error < 0) { | |
885 | error(_("unable to load rev-index for pack '%s'"), p->pack_name); | |
886 | res = ERROR_PACK_REV_INDEX; | |
887 | } else if (!load_error && | |
888 | !load_pack_revindex(r, p) && | |
889 | verify_pack_revindex(p)) { | |
890 | error(_("invalid rev-index for pack '%s'"), p->pack_name); | |
891 | res = ERROR_PACK_REV_INDEX; | |
892 | } | |
893 | display_progress(progress, ++pack_count); | |
894 | } | |
895 | stop_progress(&progress); | |
896 | ||
897 | return res; | |
898 | } | |
899 | ||
900 | static void fsck_refs(struct repository *r) | |
901 | { | |
902 | struct child_process refs_verify = CHILD_PROCESS_INIT; | |
903 | struct progress *progress = NULL; | |
904 | ||
905 | if (show_progress) | |
906 | progress = start_progress(r, _("Checking ref database"), 1); | |
907 | ||
908 | if (verbose) | |
909 | fprintf_ln(stderr, _("Checking ref database")); | |
910 | ||
911 | child_process_init(&refs_verify); | |
912 | refs_verify.git_cmd = 1; | |
913 | strvec_pushl(&refs_verify.args, "refs", "verify", NULL); | |
914 | if (verbose) | |
915 | strvec_push(&refs_verify.args, "--verbose"); | |
916 | if (check_strict) | |
917 | strvec_push(&refs_verify.args, "--strict"); | |
918 | ||
919 | if (run_command(&refs_verify)) | |
920 | errors_found |= ERROR_REFS; | |
921 | ||
922 | display_progress(progress, 1); | |
923 | stop_progress(&progress); | |
924 | } | |
925 | ||
926 | static char const * const fsck_usage[] = { | |
927 | N_("git fsck [--tags] [--root] [--unreachable] [--cache] [--no-reflogs]\n" | |
928 | " [--[no-]full] [--strict] [--verbose] [--lost-found]\n" | |
929 | " [--[no-]dangling] [--[no-]progress] [--connectivity-only]\n" | |
930 | " [--[no-]name-objects] [--[no-]references] [<object>...]"), | |
931 | NULL | |
932 | }; | |
933 | ||
934 | static struct option fsck_opts[] = { | |
935 | OPT__VERBOSE(&verbose, N_("be verbose")), | |
936 | OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")), | |
937 | OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")), | |
938 | OPT_BOOL(0, "tags", &show_tags, N_("report tags")), | |
939 | OPT_BOOL(0, "root", &show_root, N_("report root nodes")), | |
940 | OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")), | |
941 | OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")), | |
942 | OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")), | |
943 | OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")), | |
944 | OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")), | |
945 | OPT_BOOL(0, "lost-found", &write_lost_and_found, | |
946 | N_("write dangling objects in .git/lost-found")), | |
947 | OPT_BOOL(0, "progress", &show_progress, N_("show progress")), | |
948 | OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")), | |
949 | OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")), | |
950 | OPT_END(), | |
951 | }; | |
952 | ||
953 | int cmd_fsck(int argc, | |
954 | const char **argv, | |
955 | const char *prefix, | |
956 | struct repository *repo UNUSED) | |
957 | { | |
958 | int i; | |
959 | struct object_directory *odb; | |
960 | ||
961 | /* fsck knows how to handle missing promisor objects */ | |
962 | fetch_if_missing = 0; | |
963 | ||
964 | errors_found = 0; | |
965 | disable_replace_refs(); | |
966 | save_commit_buffer = 0; | |
967 | ||
968 | argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0); | |
969 | ||
970 | fsck_walk_options.walk = mark_object; | |
971 | fsck_obj_options.walk = mark_used; | |
972 | fsck_obj_options.error_func = fsck_objects_error_func; | |
973 | if (check_strict) | |
974 | fsck_obj_options.strict = 1; | |
975 | ||
976 | if (show_progress == -1) | |
977 | show_progress = isatty(2); | |
978 | if (verbose) | |
979 | show_progress = 0; | |
980 | ||
981 | if (write_lost_and_found) { | |
982 | check_full = 1; | |
983 | include_reflogs = 0; | |
984 | } | |
985 | ||
986 | if (name_objects) | |
987 | fsck_enable_object_names(&fsck_walk_options); | |
988 | ||
989 | git_config(git_fsck_config, &fsck_obj_options); | |
990 | prepare_repo_settings(the_repository); | |
991 | ||
992 | if (check_references) | |
993 | fsck_refs(the_repository); | |
994 | ||
995 | if (connectivity_only) { | |
996 | for_each_loose_object(mark_loose_for_connectivity, NULL, 0); | |
997 | for_each_packed_object(the_repository, | |
998 | mark_packed_for_connectivity, NULL, 0); | |
999 | } else { | |
1000 | prepare_alt_odb(the_repository); | |
1001 | for (odb = the_repository->objects->odb; odb; odb = odb->next) | |
1002 | fsck_object_dir(odb->path); | |
1003 | ||
1004 | if (check_full) { | |
1005 | struct packed_git *p; | |
1006 | uint32_t total = 0, count = 0; | |
1007 | struct progress *progress = NULL; | |
1008 | ||
1009 | if (show_progress) { | |
1010 | for (p = get_all_packs(the_repository); p; | |
1011 | p = p->next) { | |
1012 | if (open_pack_index(p)) | |
1013 | continue; | |
1014 | total += p->num_objects; | |
1015 | } | |
1016 | ||
1017 | progress = start_progress(the_repository, | |
1018 | _("Checking objects"), total); | |
1019 | } | |
1020 | for (p = get_all_packs(the_repository); p; | |
1021 | p = p->next) { | |
1022 | /* verify gives error messages itself */ | |
1023 | if (verify_pack(the_repository, | |
1024 | p, fsck_obj_buffer, | |
1025 | progress, count)) | |
1026 | errors_found |= ERROR_PACK; | |
1027 | count += p->num_objects; | |
1028 | } | |
1029 | stop_progress(&progress); | |
1030 | } | |
1031 | ||
1032 | if (fsck_finish(&fsck_obj_options)) | |
1033 | errors_found |= ERROR_OBJECT; | |
1034 | } | |
1035 | ||
1036 | for (i = 0; i < argc; i++) { | |
1037 | const char *arg = argv[i]; | |
1038 | struct object_id oid; | |
1039 | if (!repo_get_oid(the_repository, arg, &oid)) { | |
1040 | struct object *obj = lookup_object(the_repository, | |
1041 | &oid); | |
1042 | ||
1043 | if (!obj || !(obj->flags & HAS_OBJ)) { | |
1044 | if (is_promisor_object(the_repository, &oid)) | |
1045 | continue; | |
1046 | error(_("%s: object missing"), oid_to_hex(&oid)); | |
1047 | errors_found |= ERROR_OBJECT; | |
1048 | continue; | |
1049 | } | |
1050 | ||
1051 | obj->flags |= USED; | |
1052 | fsck_put_object_name(&fsck_walk_options, &oid, | |
1053 | "%s", arg); | |
1054 | mark_object_reachable(obj); | |
1055 | continue; | |
1056 | } | |
1057 | error(_("invalid parameter: expected sha1, got '%s'"), arg); | |
1058 | errors_found |= ERROR_OBJECT; | |
1059 | } | |
1060 | ||
1061 | /* | |
1062 | * If we've not been given any explicit head information, do the | |
1063 | * default ones from .git/refs. We also consider the index file | |
1064 | * in this case (ie this implies --cache). | |
1065 | */ | |
1066 | if (!argc) { | |
1067 | get_default_heads(); | |
1068 | keep_cache_objects = 1; | |
1069 | } | |
1070 | ||
1071 | if (keep_cache_objects) { | |
1072 | struct worktree **worktrees, **p; | |
1073 | ||
1074 | verify_index_checksum = 1; | |
1075 | verify_ce_order = 1; | |
1076 | ||
1077 | worktrees = get_worktrees(); | |
1078 | for (p = worktrees; *p; p++) { | |
1079 | struct worktree *wt = *p; | |
1080 | struct index_state istate = | |
1081 | INDEX_STATE_INIT(the_repository); | |
1082 | char *path, *wt_gitdir; | |
1083 | ||
1084 | /* | |
1085 | * Make a copy since the buffer is reusable | |
1086 | * and may get overwritten by other calls | |
1087 | * while we're examining the index. | |
1088 | */ | |
1089 | path = xstrdup(worktree_git_path(the_repository, wt, "index")); | |
1090 | wt_gitdir = get_worktree_git_dir(wt); | |
1091 | ||
1092 | read_index_from(&istate, path, wt_gitdir); | |
1093 | fsck_index(&istate, path, wt->is_current); | |
1094 | ||
1095 | discard_index(&istate); | |
1096 | free(wt_gitdir); | |
1097 | free(path); | |
1098 | } | |
1099 | free_worktrees(worktrees); | |
1100 | } | |
1101 | ||
1102 | errors_found |= check_pack_rev_indexes(the_repository, show_progress); | |
1103 | if (verify_bitmap_files(the_repository)) | |
1104 | errors_found |= ERROR_BITMAP; | |
1105 | ||
1106 | check_connectivity(); | |
1107 | ||
1108 | if (the_repository->settings.core_commit_graph) { | |
1109 | struct child_process commit_graph_verify = CHILD_PROCESS_INIT; | |
1110 | ||
1111 | prepare_alt_odb(the_repository); | |
1112 | for (odb = the_repository->objects->odb; odb; odb = odb->next) { | |
1113 | child_process_init(&commit_graph_verify); | |
1114 | commit_graph_verify.git_cmd = 1; | |
1115 | strvec_pushl(&commit_graph_verify.args, "commit-graph", | |
1116 | "verify", "--object-dir", odb->path, NULL); | |
1117 | if (show_progress) | |
1118 | strvec_push(&commit_graph_verify.args, "--progress"); | |
1119 | else | |
1120 | strvec_push(&commit_graph_verify.args, "--no-progress"); | |
1121 | if (run_command(&commit_graph_verify)) | |
1122 | errors_found |= ERROR_COMMIT_GRAPH; | |
1123 | } | |
1124 | } | |
1125 | ||
1126 | if (the_repository->settings.core_multi_pack_index) { | |
1127 | struct child_process midx_verify = CHILD_PROCESS_INIT; | |
1128 | ||
1129 | prepare_alt_odb(the_repository); | |
1130 | for (odb = the_repository->objects->odb; odb; odb = odb->next) { | |
1131 | child_process_init(&midx_verify); | |
1132 | midx_verify.git_cmd = 1; | |
1133 | strvec_pushl(&midx_verify.args, "multi-pack-index", | |
1134 | "verify", "--object-dir", odb->path, NULL); | |
1135 | if (show_progress) | |
1136 | strvec_push(&midx_verify.args, "--progress"); | |
1137 | else | |
1138 | strvec_push(&midx_verify.args, "--no-progress"); | |
1139 | if (run_command(&midx_verify)) | |
1140 | errors_found |= ERROR_MULTI_PACK_INDEX; | |
1141 | } | |
1142 | } | |
1143 | ||
1144 | return errors_found; | |
1145 | } |