]>
Commit | Line | Data |
---|---|---|
03eae9af | 1 | #define USE_THE_REPOSITORY_VARIABLE |
baffc0e7 | 2 | #include "builtin.h" |
f394e093 | 3 | #include "gettext.h" |
41771fa4 | 4 | #include "hex.h" |
b2141fc1 | 5 | #include "config.h" |
ff5ebe39 DB |
6 | #include "commit.h" |
7 | #include "tree.h" | |
8 | #include "blob.h" | |
c418eda4 | 9 | #include "tag.h" |
944d8589 | 10 | #include "refs.h" |
f9253394 | 11 | #include "pack.h" |
53dc3f3e | 12 | #include "cache-tree.h" |
271b8d25 | 13 | #include "fsck.h" |
5ac0a206 | 14 | #include "parse-options.h" |
1e49f22f | 15 | #include "progress.h" |
6f7f3beb | 16 | #include "streaming.h" |
0317f455 | 17 | #include "packfile.h" |
87bed179 | 18 | #include "object-file.h" |
dabab1d6 | 19 | #include "object-name.h" |
68cd492a | 20 | #include "object-store.h" |
c339932b | 21 | #include "path.h" |
08c46a49 | 22 | #include "read-cache-ll.h" |
cbeab747 | 23 | #include "replace-object.h" |
5a5ea141 | 24 | #include "resolve-undo.h" |
e0fd51e1 | 25 | #include "run-command.h" |
baf889c2 | 26 | #include "sparse-index.h" |
b29759d8 | 27 | #include "worktree.h" |
0d30feef | 28 | #include "pack-revindex.h" |
756f1bcd | 29 | #include "pack-bitmap.h" |
ff5ebe39 DB |
30 | |
31 | #define REACHABLE 0x0001 | |
2d9c58c6 | 32 | #define SEEN 0x0002 |
6e454b9a | 33 | #define HAS_OBJ 0x0004 |
092c55d0 JT |
34 | /* This flag is set if something points to this object. */ |
35 | #define USED 0x0008 | |
d9839e03 | 36 | |
96f1e58f DR |
37 | static int show_root; |
38 | static int show_tags; | |
39 | static int show_unreachable; | |
566842f6 | 40 | static int include_reflogs = 1; |
f29cd393 | 41 | static int check_full = 1; |
02976bf8 | 42 | static int connectivity_only; |
96f1e58f DR |
43 | static int check_strict; |
44 | static int keep_cache_objects; | |
22410549 JS |
45 | static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT; |
46 | static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT; | |
e2b4f635 | 47 | static int errors_found; |
68f6c019 | 48 | static int write_lost_and_found; |
20f1eb6b | 49 | static int verbose; |
1e49f22f | 50 | static int show_progress = -1; |
c6a13b2c | 51 | static int show_dangling = 1; |
90cf590f | 52 | static int name_objects; |
c1cf918d | 53 | static int check_references = 1; |
e2b4f635 JH |
54 | #define ERROR_OBJECT 01 |
55 | #define ERROR_REACHABLE 02 | |
a3ed7552 | 56 | #define ERROR_PACK 04 |
122f76f5 | 57 | #define ERROR_REFS 010 |
e0fd51e1 | 58 | #define ERROR_COMMIT_GRAPH 020 |
e68a5272 | 59 | #define ERROR_MULTI_PACK_INDEX 040 |
0d30feef | 60 | #define ERROR_PACK_REV_INDEX 0100 |
756f1bcd | 61 | #define ERROR_BITMAP 0200 |
d9839e03 | 62 | |
82ef89b3 | 63 | static const char *describe_object(const struct object_id *oid) |
993a21b0 | 64 | { |
82ef89b3 | 65 | return fsck_describe_object(&fsck_walk_options, oid); |
993a21b0 JS |
66 | } |
67 | ||
82ef89b3 JK |
68 | static const char *printable_type(const struct object_id *oid, |
69 | enum object_type type) | |
97ca7ca8 JK |
70 | { |
71 | const char *ret; | |
72 | ||
82ef89b3 JK |
73 | if (type == OBJ_NONE) |
74 | type = oid_object_info(the_repository, oid, NULL); | |
a2b22854 | 75 | |
82ef89b3 | 76 | ret = type_name(type); |
97ca7ca8 | 77 | if (!ret) |
674ba340 | 78 | ret = _("unknown"); |
97ca7ca8 JK |
79 | |
80 | return ret; | |
81 | } | |
82 | ||
c99ba492 | 83 | static int objerror(struct object *obj, const char *err) |
f1f0d088 | 84 | { |
e2b4f635 | 85 | errors_found |= ERROR_OBJECT; |
674ba340 NTND |
86 | /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */ |
87 | fprintf_ln(stderr, _("error in %s %s: %s"), | |
82ef89b3 JK |
88 | printable_type(&obj->oid, obj->type), |
89 | describe_object(&obj->oid), err); | |
f1f0d088 PB |
90 | return -1; |
91 | } | |
92 | ||
8cd4a447 | 93 | static int fsck_objects_error_func(struct fsck_options *o UNUSED, |
0ec5dfe8 | 94 | void *fsck_report, |
8cd4a447 | 95 | enum fsck_msg_type msg_type, |
96 | enum fsck_msg_id msg_id UNUSED, | |
97 | const char *message) | |
f1f0d088 | 98 | { |
0ec5dfe8 | 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 | ||
5afc4b1d | 103 | switch (msg_type) { |
bbb15c51 | 104 | case FSCK_WARN: |
674ba340 NTND |
105 | /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */ |
106 | fprintf_ln(stderr, _("warning in %s %s: %s"), | |
5afc4b1d JK |
107 | printable_type(oid, object_type), |
108 | describe_object(oid), message); | |
bbb15c51 NTND |
109 | return 0; |
110 | case FSCK_ERROR: | |
674ba340 NTND |
111 | /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */ |
112 | fprintf_ln(stderr, _("error in %s %s: %s"), | |
5afc4b1d JK |
113 | printable_type(oid, object_type), |
114 | describe_object(oid), message); | |
bbb15c51 NTND |
115 | return 1; |
116 | default: | |
5afc4b1d JK |
117 | BUG("%d (FSCK_IGNORE?) should never trigger this callback", |
118 | msg_type); | |
bbb15c51 | 119 | } |
f1f0d088 PB |
120 | } |
121 | ||
04d39759 LT |
122 | static struct object_array pending; |
123 | ||
28abf260 | 124 | static int mark_object(struct object *obj, enum object_type type, |
0b4e9013 | 125 | void *data, struct fsck_options *options UNUSED) |
271b8d25 | 126 | { |
271b8d25 | 127 | struct object *parent = data; |
271b8d25 | 128 | |
a1cdc251 JH |
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 | */ | |
271b8d25 | 134 | if (!obj) { |
a1cdc251 | 135 | /* ... these references to parent->fld are safe here */ |
674ba340 | 136 | printf_ln(_("broken link from %7s %s"), |
82ef89b3 JK |
137 | printable_type(&parent->oid, parent->type), |
138 | describe_object(&parent->oid)); | |
674ba340 NTND |
139 | printf_ln(_("broken link from %7s %s"), |
140 | (type == OBJ_ANY ? _("unknown") : type_name(type)), | |
141 | _("unknown")); | |
271b8d25 MK |
142 | errors_found |= ERROR_REACHABLE; |
143 | return 1; | |
144 | } | |
145 | ||
146 | if (type != OBJ_ANY && obj->type != type) | |
a1cdc251 | 147 | /* ... and the reference to parent is safe here */ |
674ba340 | 148 | objerror(parent, _("wrong object type in link")); |
271b8d25 MK |
149 | |
150 | if (obj->flags & REACHABLE) | |
151 | return 0; | |
152 | obj->flags |= REACHABLE; | |
caba7fc3 | 153 | |
c87910b9 | 154 | if (is_promisor_object(the_repository, &obj->oid)) |
caba7fc3 JT |
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 | ||
6e454b9a | 162 | if (!(obj->flags & HAS_OBJ)) { |
9eb86f41 | 163 | if (parent && !has_object(the_repository, &obj->oid, 1)) { |
674ba340 NTND |
164 | printf_ln(_("broken link from %7s %s\n" |
165 | " to %7s %s"), | |
82ef89b3 JK |
166 | printable_type(&parent->oid, parent->type), |
167 | describe_object(&parent->oid), | |
168 | printable_type(&obj->oid, obj->type), | |
169 | describe_object(&obj->oid)); | |
271b8d25 MK |
170 | errors_found |= ERROR_REACHABLE; |
171 | } | |
172 | return 1; | |
173 | } | |
174 | ||
16aa3bfc | 175 | add_object_array(obj, NULL, &pending); |
04d39759 LT |
176 | return 0; |
177 | } | |
178 | ||
179 | static void mark_object_reachable(struct object *obj) | |
180 | { | |
22410549 | 181 | mark_object(obj, OBJ_ANY, NULL, NULL); |
04d39759 LT |
182 | } |
183 | ||
a1cdc251 | 184 | static int traverse_one_object(struct object *obj) |
04d39759 | 185 | { |
ba3a08ca EW |
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; | |
271b8d25 MK |
193 | } |
194 | ||
04d39759 | 195 | static int traverse_reachable(void) |
271b8d25 | 196 | { |
1e49f22f NTND |
197 | struct progress *progress = NULL; |
198 | unsigned int nr = 0; | |
04d39759 | 199 | int result = 0; |
1e49f22f | 200 | if (show_progress) |
1f7e6478 PS |
201 | progress = start_delayed_progress(the_repository, |
202 | _("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 | ||
2bbeddee | 211 | static int mark_used(struct object *obj, enum object_type type UNUSED, |
0b4e9013 | 212 | void *data UNUSED, struct fsck_options *options UNUSED) |
271b8d25 MK |
213 | { |
214 | if (!obj) | |
215 | return 1; | |
092c55d0 | 216 | obj->flags |= USED; |
271b8d25 | 217 | return 0; |
f1f0d088 PB |
218 | } |
219 | ||
8d8c2a5a JK |
220 | static void mark_unreachable_referents(const struct object_id *oid) |
221 | { | |
222 | struct fsck_options options = FSCK_OPTIONS_DEFAULT; | |
d0229abd | 223 | struct object *obj = lookup_object(the_repository, oid); |
8d8c2a5a JK |
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) | |
6da43d93 | 238 | object_as_type(obj, type, 0); |
8d8c2a5a JK |
239 | } |
240 | ||
241 | options.walk = mark_used; | |
242 | fsck_walk(obj, NULL, &options); | |
fbce4fa9 JK |
243 | if (obj->type == OBJ_TREE) |
244 | free_tree_buffer((struct tree *)obj); | |
8d8c2a5a JK |
245 | } |
246 | ||
247 | static int mark_loose_unreachable_referents(const struct object_id *oid, | |
be252d33 JK |
248 | const char *path UNUSED, |
249 | void *data UNUSED) | |
8d8c2a5a JK |
250 | { |
251 | mark_unreachable_referents(oid); | |
252 | return 0; | |
253 | } | |
254 | ||
255 | static int mark_packed_unreachable_referents(const struct object_id *oid, | |
be252d33 JK |
256 | struct packed_git *pack UNUSED, |
257 | uint32_t pos UNUSED, | |
258 | void *data UNUSED) | |
8d8c2a5a JK |
259 | { |
260 | mark_unreachable_referents(oid); | |
261 | return 0; | |
262 | } | |
263 | ||
18af29f2 LT |
264 | /* |
265 | * Check a single reachable object | |
266 | */ | |
267 | static void check_reachable_object(struct object *obj) | |
268 | { | |
18af29f2 LT |
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 | */ | |
6e454b9a | 274 | if (!(obj->flags & HAS_OBJ)) { |
c87910b9 | 275 | if (is_promisor_object(the_repository, &obj->oid)) |
caba7fc3 | 276 | return; |
cc656f4e | 277 | if (has_object_pack(the_repository, &obj->oid)) |
18af29f2 | 278 | return; /* it is in pack - forget about it */ |
82ef89b3 JK |
279 | printf_ln(_("missing %s %s"), |
280 | printable_type(&obj->oid, obj->type), | |
281 | describe_object(&obj->oid)); | |
e2b4f635 | 282 | errors_found |= ERROR_REACHABLE; |
18af29f2 LT |
283 | return; |
284 | } | |
18af29f2 LT |
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 | */ | |
b4584e4f | 298 | if (!(obj->flags & HAS_OBJ)) |
18af29f2 LT |
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) { | |
82ef89b3 JK |
306 | printf_ln(_("unreachable %s %s"), |
307 | printable_type(&obj->oid, obj->type), | |
308 | describe_object(&obj->oid)); | |
18af29f2 LT |
309 | return; |
310 | } | |
311 | ||
312 | /* | |
092c55d0 | 313 | * "!USED" means that nothing at all points to it, including |
3dff5379 | 314 | * other unreachable objects. In other words, it's the "tip" |
18af29f2 LT |
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 | */ | |
092c55d0 | 324 | if (!(obj->flags & USED)) { |
c6a13b2c | 325 | if (show_dangling) |
82ef89b3 JK |
326 | printf_ln(_("dangling %s %s"), |
327 | printable_type(&obj->oid, obj->type), | |
328 | describe_object(&obj->oid)); | |
68f6c019 | 329 | if (write_lost_and_found) { |
bba59f58 | 330 | char *filename = repo_git_path(the_repository, "lost-found/%s/%s", |
68f6c019 | 331 | obj->type == OBJ_COMMIT ? "commit" : "other", |
82ef89b3 | 332 | describe_object(&obj->oid)); |
68f6c019 JS |
333 | FILE *f; |
334 | ||
1a99fe80 | 335 | if (safe_create_leading_directories_const(the_repository, filename)) { |
674ba340 | 336 | error(_("could not create lost-found")); |
fcd12db6 | 337 | free(filename); |
68f6c019 JS |
338 | return; |
339 | } | |
23a9e071 | 340 | f = xfopen(filename, "w"); |
16a7fcfe | 341 | if (obj->type == OBJ_BLOB) { |
7eda0e4f | 342 | if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1)) |
674ba340 | 343 | die_errno(_("could not write '%s'"), filename); |
16a7fcfe | 344 | } else |
82ef89b3 | 345 | fprintf(f, "%s\n", describe_object(&obj->oid)); |
47d32af2 | 346 | if (fclose(f)) |
674ba340 | 347 | die_errno(_("could not finish '%s'"), |
d824cbba | 348 | filename); |
fcd12db6 | 349 | free(filename); |
68f6c019 | 350 | } |
18af29f2 LT |
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 | { | |
20f1eb6b | 363 | if (verbose) |
82ef89b3 | 364 | fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid)); |
20f1eb6b | 365 | |
18af29f2 LT |
366 | if (obj->flags & REACHABLE) |
367 | check_reachable_object(obj); | |
368 | else | |
369 | check_unreachable_object(obj); | |
370 | } | |
f1f0d088 | 371 | |
8ba0bbb2 LT |
372 | static void check_connectivity(void) |
373 | { | |
fc046a75 | 374 | int i, max; |
8ba0bbb2 | 375 | |
04d39759 LT |
376 | /* Traverse the pending reachable objects */ |
377 | traverse_reachable(); | |
378 | ||
8d8c2a5a JK |
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); | |
c87910b9 KN |
396 | for_each_packed_object(the_repository, |
397 | mark_packed_unreachable_referents, | |
398 | NULL, | |
399 | 0); | |
8d8c2a5a JK |
400 | } |
401 | ||
8ba0bbb2 | 402 | /* Look up all the requirements, warn about missing objects.. */ |
74d414c9 | 403 | max = get_max_object_index(the_repository); |
20f1eb6b | 404 | if (verbose) |
674ba340 | 405 | fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max); |
20f1eb6b | 406 | |
fc046a75 | 407 | for (i = 0; i < max; i++) { |
74d414c9 | 408 | struct object *obj = get_indexed_object(the_repository, i); |
8ba0bbb2 | 409 | |
18af29f2 LT |
410 | if (obj) |
411 | check_object(obj); | |
8ba0bbb2 LT |
412 | } |
413 | } | |
414 | ||
7ac4f3a0 | 415 | static int fsck_obj(struct object *obj, void *buffer, unsigned long size) |
85003492 | 416 | { |
83cd6f90 RS |
417 | int err; |
418 | ||
ba002f3b | 419 | if (obj->flags & SEEN) |
85003492 | 420 | return 0; |
ba002f3b | 421 | obj->flags |= SEEN; |
85003492 | 422 | |
20f1eb6b | 423 | if (verbose) |
674ba340 | 424 | fprintf_ln(stderr, _("Checking %s %s"), |
82ef89b3 JK |
425 | printable_type(&obj->oid, obj->type), |
426 | describe_object(&obj->oid)); | |
42ea9cb2 | 427 | |
22410549 | 428 | if (fsck_walk(obj, NULL, &fsck_obj_options)) |
674ba340 | 429 | objerror(obj, _("broken links")); |
7ac4f3a0 | 430 | err = fsck_object(obj, buffer, size, &fsck_obj_options); |
83cd6f90 RS |
431 | if (err) |
432 | goto out; | |
1ea34e36 | 433 | |
ba002f3b MK |
434 | if (obj->type == OBJ_COMMIT) { |
435 | struct commit *commit = (struct commit *) obj; | |
de2eb7f6 | 436 | |
ba002f3b | 437 | if (!commit->parents && show_root) |
674ba340 | 438 | printf_ln(_("root %s"), |
82ef89b3 | 439 | describe_object(&commit->object.oid)); |
ba002f3b | 440 | } |
92d4c85d | 441 | |
ba002f3b MK |
442 | if (obj->type == OBJ_TAG) { |
443 | struct tag *tag = (struct tag *) obj; | |
20f1eb6b | 444 | |
ba002f3b | 445 | if (show_tags && tag->tagged) { |
674ba340 | 446 | printf_ln(_("tagged %s %s (%s) in %s"), |
82ef89b3 JK |
447 | printable_type(&tag->tagged->oid, tag->tagged->type), |
448 | describe_object(&tag->tagged->oid), | |
bbb15c51 | 449 | tag->tag, |
82ef89b3 | 450 | describe_object(&tag->object.oid)); |
ba002f3b | 451 | } |
92d4c85d | 452 | } |
889262ea | 453 | |
83cd6f90 RS |
454 | out: |
455 | if (obj->type == OBJ_TREE) | |
456 | free_tree_buffer((struct tree *)obj); | |
83cd6f90 | 457 | return err; |
20222118 LT |
458 | } |
459 | ||
9fd75046 | 460 | static int fsck_obj_buffer(const struct object_id *oid, enum object_type type, |
c9486eb0 NTND |
461 | unsigned long size, void *buffer, int *eaten) |
462 | { | |
ec9d2249 NTND |
463 | /* |
464 | * Note, buffer may be NULL if type is OBJ_BLOB. See | |
465 | * verify_packfile(), data_valid variable for details. | |
466 | */ | |
c9486eb0 | 467 | struct object *obj; |
1ec5bfd2 SB |
468 | obj = parse_object_buffer(the_repository, oid, type, size, buffer, |
469 | eaten); | |
c9486eb0 NTND |
470 | if (!obj) { |
471 | errors_found |= ERROR_OBJECT; | |
674ba340 NTND |
472 | return error(_("%s: object corrupt or missing"), |
473 | oid_to_hex(oid)); | |
c9486eb0 | 474 | } |
092c55d0 JT |
475 | obj->flags &= ~(REACHABLE | SEEN); |
476 | obj->flags |= HAS_OBJ; | |
7ac4f3a0 | 477 | return fsck_obj(obj, buffer, size); |
c9486eb0 NTND |
478 | } |
479 | ||
96f1e58f | 480 | static int default_refs; |
944d8589 | 481 | |
9461d272 | 482 | static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid, |
dddbad72 | 483 | timestamp_t timestamp) |
55dd5526 JH |
484 | { |
485 | struct object *obj; | |
486 | ||
9461d272 | 487 | if (!is_null_oid(oid)) { |
d0229abd | 488 | obj = lookup_object(the_repository, oid); |
c2d17b3b | 489 | if (obj && (obj->flags & HAS_OBJ)) { |
a59cfb32 | 490 | if (timestamp) |
73390290 | 491 | fsck_put_object_name(&fsck_walk_options, oid, |
a59cfb32 JK |
492 | "%s@{%"PRItime"}", |
493 | refname, timestamp); | |
092c55d0 | 494 | obj->flags |= USED; |
271b8d25 | 495 | mark_object_reachable(obj); |
c87910b9 | 496 | } else if (!is_promisor_object(the_repository, oid)) { |
674ba340 NTND |
497 | error(_("%s: invalid reflog entry %s"), |
498 | refname, oid_to_hex(oid)); | |
19bf6c9b | 499 | errors_found |= ERROR_REACHABLE; |
55dd5526 JH |
500 | } |
501 | } | |
d66ae59b MH |
502 | } |
503 | ||
9461d272 | 504 | static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid, |
5cf88fd8 ÆAB |
505 | const char *email UNUSED, |
506 | timestamp_t timestamp, int tz UNUSED, | |
507 | const char *message UNUSED, void *cb_data) | |
55dd5526 | 508 | { |
19bf6c9b | 509 | const char *refname = cb_data; |
55dd5526 | 510 | |
20f1eb6b | 511 | if (verbose) |
674ba340 NTND |
512 | fprintf_ln(stderr, _("Checking reflog %s->%s"), |
513 | oid_to_hex(ooid), oid_to_hex(noid)); | |
20f1eb6b | 514 | |
9461d272 | 515 | fsck_handle_reflog_oid(refname, ooid, 0); |
516 | fsck_handle_reflog_oid(refname, noid, timestamp); | |
55dd5526 JH |
517 | return 0; |
518 | } | |
519 | ||
31f89839 | 520 | static int fsck_handle_reflog(const char *logname, void *cb_data) |
eb8381c8 | 521 | { |
b29759d8 NTND |
522 | struct strbuf refname = STRBUF_INIT; |
523 | ||
524 | strbuf_worktree_ref(cb_data, &refname, logname); | |
2e5c4758 PS |
525 | refs_for_each_reflog_ent(get_main_ref_store(the_repository), |
526 | refname.buf, fsck_handle_reflog_ent, | |
527 | refname.buf); | |
b29759d8 | 528 | strbuf_release(&refname); |
eb8381c8 NP |
529 | return 0; |
530 | } | |
531 | ||
e8207717 | 532 | static int fsck_handle_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid, |
5cf88fd8 | 533 | int flag UNUSED, void *cb_data UNUSED) |
1024932f | 534 | { |
1024932f LT |
535 | struct object *obj; |
536 | ||
109cd76d | 537 | obj = parse_object(the_repository, oid); |
8a498a05 | 538 | if (!obj) { |
c87910b9 | 539 | if (is_promisor_object(the_repository, oid)) { |
43f25158 JT |
540 | /* |
541 | * Increment default_refs anyway, because this is a | |
542 | * valid ref. | |
543 | */ | |
544 | default_refs++; | |
545 | return 0; | |
546 | } | |
674ba340 NTND |
547 | error(_("%s: invalid sha1 pointer %s"), |
548 | refname, oid_to_hex(oid)); | |
30d1038d | 549 | errors_found |= ERROR_REACHABLE; |
944d8589 LT |
550 | /* We'll continue with the rest despite the error.. */ |
551 | return 0; | |
8a498a05 | 552 | } |
122f76f5 | 553 | if (obj->type != OBJ_COMMIT && is_branch(refname)) { |
674ba340 | 554 | error(_("%s: not a commit"), refname); |
122f76f5 JH |
555 | errors_found |= ERROR_REFS; |
556 | } | |
944d8589 | 557 | default_refs++; |
092c55d0 | 558 | obj->flags |= USED; |
a59cfb32 | 559 | fsck_put_object_name(&fsck_walk_options, |
73390290 | 560 | oid, "%s", refname); |
271b8d25 | 561 | mark_object_reachable(obj); |
55dd5526 | 562 | |
7c4d07c7 | 563 | return 0; |
1024932f LT |
564 | } |
565 | ||
b29759d8 NTND |
566 | static int fsck_head_link(const char *head_ref_name, |
567 | const char **head_points_at, | |
a8c754d4 EN |
568 | struct object_id *head_oid); |
569 | ||
1024932f LT |
570 | static void get_default_heads(void) |
571 | { | |
b29759d8 | 572 | struct worktree **worktrees, **p; |
a8c754d4 EN |
573 | const char *head_points_at; |
574 | struct object_id head_oid; | |
575 | ||
2e5c4758 PS |
576 | refs_for_each_rawref(get_main_ref_store(the_repository), |
577 | fsck_handle_ref, NULL); | |
b29759d8 | 578 | |
03f2465b | 579 | worktrees = get_worktrees(); |
b29759d8 NTND |
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)) | |
e8207717 | 587 | fsck_handle_ref(ref.buf, NULL, &head_oid, 0, NULL); |
b29759d8 NTND |
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); | |
071fa89e LT |
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) { | |
674ba340 | 609 | fprintf_ln(stderr, _("notice: No default references")); |
071fa89e LT |
610 | show_unreachable = 0; |
611 | } | |
1024932f LT |
612 | } |
613 | ||
31deb28f ÆAB |
614 | struct for_each_loose_cb |
615 | { | |
616 | struct progress *progress; | |
31deb28f ÆAB |
617 | }; |
618 | ||
4ae0e942 JK |
619 | static int fsck_loose(const struct object_id *oid, const char *path, |
620 | void *data UNUSED) | |
c68b489e JK |
621 | { |
622 | struct object *obj; | |
31deb28f | 623 | enum object_type type = OBJ_NONE; |
c68b489e | 624 | unsigned long size; |
16235e3b | 625 | void *contents = NULL; |
c68b489e | 626 | int eaten; |
31deb28f | 627 | struct object_info oi = OBJECT_INFO_INIT; |
7d70b29c | 628 | struct object_id real_oid = *null_oid(the_hash_algo); |
31deb28f ÆAB |
629 | int err = 0; |
630 | ||
31deb28f ÆAB |
631 | oi.sizep = &size; |
632 | oi.typep = &type; | |
633 | ||
96e41f58 ÆAB |
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 | } | |
31deb28f | 642 | if (err < 0) { |
7ac4f3a0 | 643 | errors_found |= ERROR_OBJECT; |
16235e3b | 644 | free(contents); |
7ac4f3a0 JK |
645 | return 0; /* keep checking other objects */ |
646 | } | |
c68b489e JK |
647 | |
648 | if (!contents && type != OBJ_BLOB) | |
7ac4f3a0 | 649 | BUG("read_loose_object streamed a non-blob"); |
c68b489e | 650 | |
1ec5bfd2 SB |
651 | obj = parse_object_buffer(the_repository, oid, type, size, |
652 | contents, &eaten); | |
653 | ||
c68b489e JK |
654 | if (!obj) { |
655 | errors_found |= ERROR_OBJECT; | |
674ba340 | 656 | error(_("%s: object could not be parsed: %s"), |
76c1d9a0 | 657 | oid_to_hex(oid), path); |
7ac4f3a0 JK |
658 | if (!eaten) |
659 | free(contents); | |
c68b489e JK |
660 | return 0; /* keep checking other objects */ |
661 | } | |
662 | ||
092c55d0 JT |
663 | obj->flags &= ~(REACHABLE | SEEN); |
664 | obj->flags |= HAS_OBJ; | |
7ac4f3a0 | 665 | if (fsck_obj(obj, contents, size)) |
f0766bf9 | 666 | errors_found |= ERROR_OBJECT; |
7ac4f3a0 JK |
667 | |
668 | if (!eaten) | |
669 | free(contents); | |
670 | return 0; /* keep checking other objects, even if we saw an error */ | |
f0766bf9 JK |
671 | } |
672 | ||
be252d33 JK |
673 | static int fsck_cruft(const char *basename, const char *path, |
674 | void *data UNUSED) | |
f0766bf9 JK |
675 | { |
676 | if (!starts_with(basename, "tmp_obj_")) | |
674ba340 | 677 | fprintf_ln(stderr, _("bad sha1 file: %s"), path); |
f0766bf9 JK |
678 | return 0; |
679 | } | |
680 | ||
be252d33 | 681 | static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data) |
f0766bf9 | 682 | { |
31deb28f ÆAB |
683 | struct for_each_loose_cb *cb_data = data; |
684 | struct progress *progress = cb_data->progress; | |
f0766bf9 JK |
685 | display_progress(progress, nr + 1); |
686 | return 0; | |
687 | } | |
688 | ||
8a498a05 JH |
689 | static void fsck_object_dir(const char *path) |
690 | { | |
1e49f22f | 691 | struct progress *progress = NULL; |
31deb28f | 692 | struct for_each_loose_cb cb_data = { |
31deb28f ÆAB |
693 | .progress = progress, |
694 | }; | |
20f1eb6b JS |
695 | |
696 | if (verbose) | |
674ba340 | 697 | fprintf_ln(stderr, _("Checking object directory")); |
20f1eb6b | 698 | |
1e49f22f | 699 | if (show_progress) |
1f7e6478 PS |
700 | progress = start_progress(the_repository, |
701 | _("Checking object directories"), 256); | |
f0766bf9 JK |
702 | |
703 | for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir, | |
31deb28f | 704 | &cb_data); |
f0766bf9 | 705 | display_progress(progress, 256); |
1e49f22f | 706 | stop_progress(&progress); |
8a498a05 JH |
707 | } |
708 | ||
b29759d8 NTND |
709 | static int fsck_head_link(const char *head_ref_name, |
710 | const char **head_points_at, | |
a8c754d4 | 711 | struct object_id *head_oid) |
c3330383 | 712 | { |
8eb2d0be | 713 | int null_is_error = 0; |
8eb2d0be | 714 | |
20f1eb6b | 715 | if (verbose) |
3813a89f | 716 | fprintf_ln(stderr, _("Checking %s link"), head_ref_name); |
20f1eb6b | 717 | |
2e5c4758 PS |
718 | *head_points_at = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), |
719 | head_ref_name, 0, head_oid, | |
720 | NULL); | |
a8c754d4 | 721 | if (!*head_points_at) { |
122f76f5 | 722 | errors_found |= ERROR_REFS; |
3813a89f | 723 | return error(_("invalid %s"), head_ref_name); |
122f76f5 | 724 | } |
b29759d8 | 725 | if (!strcmp(*head_points_at, head_ref_name)) |
8eb2d0be JH |
726 | /* detached HEAD */ |
727 | null_is_error = 1; | |
a8c754d4 | 728 | else if (!starts_with(*head_points_at, "refs/heads/")) { |
122f76f5 | 729 | errors_found |= ERROR_REFS; |
3813a89f | 730 | return error(_("%s points to something strange (%s)"), |
b29759d8 | 731 | head_ref_name, *head_points_at); |
122f76f5 | 732 | } |
a8c754d4 | 733 | if (is_null_oid(head_oid)) { |
122f76f5 JH |
734 | if (null_is_error) { |
735 | errors_found |= ERROR_REFS; | |
3813a89f | 736 | return error(_("%s: detached HEAD points at nothing"), |
b29759d8 | 737 | head_ref_name); |
122f76f5 | 738 | } |
3813a89f JH |
739 | fprintf_ln(stderr, |
740 | _("notice: %s points to an unborn branch (%s)"), | |
741 | head_ref_name, *head_points_at + 11); | |
8eb2d0be | 742 | } |
c3330383 LT |
743 | return 0; |
744 | } | |
745 | ||
592ec63b | 746 | static int fsck_cache_tree(struct cache_tree *it, const char *index_path) |
53dc3f3e JH |
747 | { |
748 | int i; | |
749 | int err = 0; | |
750 | ||
20f1eb6b | 751 | if (verbose) |
592ec63b | 752 | fprintf_ln(stderr, _("Checking cache tree of %s"), index_path); |
20f1eb6b | 753 | |
53dc3f3e | 754 | if (0 <= it->entry_count) { |
109cd76d | 755 | struct object *obj = parse_object(the_repository, &it->oid); |
6d60bbef | 756 | if (!obj) { |
592ec63b JK |
757 | error(_("%s: invalid sha1 pointer in cache-tree of %s"), |
758 | oid_to_hex(&it->oid), index_path); | |
122f76f5 | 759 | errors_found |= ERROR_REFS; |
6d60bbef JH |
760 | return 1; |
761 | } | |
092c55d0 | 762 | obj->flags |= USED; |
73390290 | 763 | fsck_put_object_name(&fsck_walk_options, &it->oid, ":"); |
a1cdc251 | 764 | mark_object_reachable(obj); |
1974632c | 765 | if (obj->type != OBJ_TREE) |
674ba340 | 766 | err |= objerror(obj, _("non-tree in cache-tree")); |
53dc3f3e JH |
767 | } |
768 | for (i = 0; i < it->subtree_nr; i++) | |
592ec63b | 769 | err |= fsck_cache_tree(it->down[i]->cache_tree, index_path); |
53dc3f3e JH |
770 | return err; |
771 | } | |
772 | ||
592ec63b JK |
773 | static int fsck_resolve_undo(struct index_state *istate, |
774 | const char *index_path) | |
5a5ea141 JH |
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) { | |
592ec63b JK |
797 | error(_("%s: invalid sha1 pointer in resolve-undo of %s"), |
798 | oid_to_hex(&ru->oid[i]), | |
799 | index_path); | |
5a5ea141 | 800 | errors_found |= ERROR_REFS; |
e0ad1397 | 801 | continue; |
5a5ea141 JH |
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 | ||
592ec63b | 812 | static void fsck_index(struct index_state *istate, const char *index_path, |
6e6a529b | 813 | int is_current_worktree) |
8840069a JK |
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, | |
592ec63b | 834 | "%s:%s", |
6e6a529b | 835 | is_current_worktree ? "" : index_path, |
592ec63b | 836 | istate->cache[i]->name); |
8840069a JK |
837 | mark_object_reachable(obj); |
838 | } | |
839 | if (istate->cache_tree) | |
592ec63b JK |
840 | fsck_cache_tree(istate->cache_tree, index_path); |
841 | fsck_resolve_undo(istate, index_path); | |
8840069a JK |
842 | } |
843 | ||
76c1d9a0 | 844 | static void mark_object_for_connectivity(const struct object_id *oid) |
3e3f8bd6 | 845 | { |
45a187cc | 846 | struct object *obj = lookup_unknown_object(the_repository, oid); |
3e3f8bd6 JK |
847 | obj->flags |= HAS_OBJ; |
848 | } | |
849 | ||
76c1d9a0 | 850 | static int mark_loose_for_connectivity(const struct object_id *oid, |
be252d33 JK |
851 | const char *path UNUSED, |
852 | void *data UNUSED) | |
3e3f8bd6 | 853 | { |
76c1d9a0 | 854 | mark_object_for_connectivity(oid); |
3e3f8bd6 JK |
855 | return 0; |
856 | } | |
857 | ||
76c1d9a0 | 858 | static int mark_packed_for_connectivity(const struct object_id *oid, |
be252d33 JK |
859 | struct packed_git *pack UNUSED, |
860 | uint32_t pos UNUSED, | |
861 | void *data UNUSED) | |
3e3f8bd6 | 862 | { |
76c1d9a0 | 863 | mark_object_for_connectivity(oid); |
3e3f8bd6 JK |
864 | return 0; |
865 | } | |
866 | ||
0d30feef DS |
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) { | |
cf9cd8b5 | 874 | for (struct packed_git *p = get_all_packs(r); p; p = p->next) |
0d30feef | 875 | pack_count++; |
1f7e6478 PS |
876 | progress = start_delayed_progress(the_repository, |
877 | "Verifying reverse pack-indexes", pack_count); | |
0d30feef DS |
878 | pack_count = 0; |
879 | } | |
880 | ||
cf9cd8b5 | 881 | for (struct packed_git *p = get_all_packs(r); p; p = p->next) { |
5a6072f6 DS |
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 && | |
cf9cd8b5 | 888 | !load_pack_revindex(r, p) && |
5a6072f6 | 889 | verify_pack_revindex(p)) { |
0d30feef DS |
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 | ||
c1cf918d | 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 | ||
5ac0a206 | 926 | static char const * const fsck_usage[] = { |
d9054a19 ÆAB |
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" | |
c1cf918d | 930 | " [--[no-]name-objects] [--[no-]references] [<object>...]"), |
5ac0a206 PH |
931 | NULL |
932 | }; | |
933 | ||
934 | static struct option fsck_opts[] = { | |
cf8fe315 | 935 | OPT__VERBOSE(&verbose, N_("be verbose")), |
d5d09d47 | 936 | OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")), |
cf8fe315 | 937 | OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")), |
d5d09d47 SB |
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")), | |
02976bf8 | 943 | OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")), |
d5d09d47 SB |
944 | OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")), |
945 | OPT_BOOL(0, "lost-found", &write_lost_and_found, | |
cf8fe315 NTND |
946 | N_("write dangling objects in .git/lost-found")), |
947 | OPT_BOOL(0, "progress", &show_progress, N_("show progress")), | |
90cf590f | 948 | OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")), |
c1cf918d | 949 | OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")), |
5ac0a206 PH |
950 | OPT_END(), |
951 | }; | |
e2b4f635 | 952 | |
9b1cb507 JC |
953 | int cmd_fsck(int argc, |
954 | const char **argv, | |
955 | const char *prefix, | |
956 | struct repository *repo UNUSED) | |
20222118 | 957 | { |
78e7b98f | 958 | int i; |
263db403 | 959 | struct object_directory *odb; |
20222118 | 960 | |
8b4c0103 JT |
961 | /* fsck knows how to handle missing promisor objects */ |
962 | fetch_if_missing = 0; | |
963 | ||
e2b4f635 | 964 | errors_found = 0; |
d24eda4e | 965 | disable_replace_refs(); |
069e4452 | 966 | save_commit_buffer = 0; |
61e2b015 | 967 | |
37782920 | 968 | argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0); |
1e49f22f | 969 | |
22410549 JS |
970 | fsck_walk_options.walk = mark_object; |
971 | fsck_obj_options.walk = mark_used; | |
8cd4a447 | 972 | fsck_obj_options.error_func = fsck_objects_error_func; |
22410549 JS |
973 | if (check_strict) |
974 | fsck_obj_options.strict = 1; | |
975 | ||
1e49f22f NTND |
976 | if (show_progress == -1) |
977 | show_progress = isatty(2); | |
978 | if (verbose) | |
979 | show_progress = 0; | |
980 | ||
5ac0a206 PH |
981 | if (write_lost_and_found) { |
982 | check_full = 1; | |
983 | include_reflogs = 0; | |
889262ea LT |
984 | } |
985 | ||
90cf590f | 986 | if (name_objects) |
a59cfb32 | 987 | fsck_enable_object_names(&fsck_walk_options); |
90cf590f | 988 | |
fb79f5bf | 989 | git_config(git_fsck_config, &fsck_obj_options); |
f30e4d85 | 990 | prepare_repo_settings(the_repository); |
2becf00f | 991 | |
c1cf918d | 992 | if (check_references) |
993 | fsck_refs(the_repository); | |
994 | ||
3e3f8bd6 JK |
995 | if (connectivity_only) { |
996 | for_each_loose_object(mark_loose_for_connectivity, NULL, 0); | |
c87910b9 KN |
997 | for_each_packed_object(the_repository, |
998 | mark_packed_for_connectivity, NULL, 0); | |
3e3f8bd6 | 999 | } else { |
0b209034 | 1000 | prepare_alt_odb(the_repository); |
f0eaf638 | 1001 | for (odb = the_repository->objects->odb; odb; odb = odb->next) |
263db403 | 1002 | fsck_object_dir(odb->path); |
e15ef669 | 1003 | |
3e3f8bd6 JK |
1004 | if (check_full) { |
1005 | struct packed_git *p; | |
1006 | uint32_t total = 0, count = 0; | |
1007 | struct progress *progress = NULL; | |
e15ef669 | 1008 | |
3e3f8bd6 | 1009 | if (show_progress) { |
454ea2e4 | 1010 | for (p = get_all_packs(the_repository); p; |
a80d72db | 1011 | p = p->next) { |
3e3f8bd6 JK |
1012 | if (open_pack_index(p)) |
1013 | continue; | |
1014 | total += p->num_objects; | |
1015 | } | |
1e49f22f | 1016 | |
1f7e6478 PS |
1017 | progress = start_progress(the_repository, |
1018 | _("Checking objects"), total); | |
3e3f8bd6 | 1019 | } |
454ea2e4 | 1020 | for (p = get_all_packs(the_repository); p; |
a80d72db | 1021 | p = p->next) { |
3e3f8bd6 | 1022 | /* verify gives error messages itself */ |
94e10825 NTND |
1023 | if (verify_pack(the_repository, |
1024 | p, fsck_obj_buffer, | |
3e3f8bd6 JK |
1025 | progress, count)) |
1026 | errors_found |= ERROR_PACK; | |
1027 | count += p->num_objects; | |
1e49f22f | 1028 | } |
3e3f8bd6 | 1029 | stop_progress(&progress); |
1e49f22f | 1030 | } |
1995b5e0 JK |
1031 | |
1032 | if (fsck_finish(&fsck_obj_options)) | |
1033 | errors_found |= ERROR_OBJECT; | |
bcee6fd8 LT |
1034 | } |
1035 | ||
3aed2fda | 1036 | for (i = 0; i < argc; i++) { |
a6080a0a | 1037 | const char *arg = argv[i]; |
aca6065c | 1038 | struct object_id oid; |
d850b7a5 | 1039 | if (!repo_get_oid(the_repository, arg, &oid)) { |
5abddd1e | 1040 | struct object *obj = lookup_object(the_repository, |
d0229abd | 1041 | &oid); |
e1a1388d | 1042 | |
c2d17b3b | 1043 | if (!obj || !(obj->flags & HAS_OBJ)) { |
c87910b9 | 1044 | if (is_promisor_object(the_repository, &oid)) |
096c9b8b | 1045 | continue; |
674ba340 | 1046 | error(_("%s: object missing"), oid_to_hex(&oid)); |
c6c7b16d | 1047 | errors_found |= ERROR_OBJECT; |
e1a1388d | 1048 | continue; |
c6c7b16d | 1049 | } |
e1a1388d | 1050 | |
092c55d0 | 1051 | obj->flags |= USED; |
73390290 | 1052 | fsck_put_object_name(&fsck_walk_options, &oid, |
a59cfb32 | 1053 | "%s", arg); |
271b8d25 | 1054 | mark_object_reachable(obj); |
d9839e03 LT |
1055 | continue; |
1056 | } | |
674ba340 | 1057 | error(_("invalid parameter: expected sha1, got '%s'"), arg); |
c6c7b16d | 1058 | errors_found |= ERROR_OBJECT; |
d9839e03 | 1059 | } |
d9839e03 | 1060 | |
1024932f | 1061 | /* |
d1af002d | 1062 | * If we've not been given any explicit head information, do the |
e7bd907d LT |
1063 | * default ones from .git/refs. We also consider the index file |
1064 | * in this case (ie this implies --cache). | |
1024932f | 1065 | */ |
c3271a0e | 1066 | if (!argc) { |
1024932f LT |
1067 | get_default_heads(); |
1068 | keep_cache_objects = 1; | |
1069 | } | |
1070 | ||
ae7c0c92 | 1071 | if (keep_cache_objects) { |
fb64ca52 JK |
1072 | struct worktree **worktrees, **p; |
1073 | ||
a33fc72f | 1074 | verify_index_checksum = 1; |
00ec50e5 | 1075 | verify_ce_order = 1; |
fb64ca52 JK |
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); | |
8e4710f0 | 1082 | char *path, *wt_gitdir; |
fb64ca52 | 1083 | |
592ec63b JK |
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 | */ | |
a973f60d | 1089 | path = xstrdup(worktree_git_path(the_repository, wt, "index")); |
8e4710f0 PS |
1090 | wt_gitdir = get_worktree_git_dir(wt); |
1091 | ||
1092 | read_index_from(&istate, path, wt_gitdir); | |
8d3e7eac | 1093 | fsck_index(&istate, path, wt->is_current); |
8e4710f0 | 1094 | |
fb64ca52 | 1095 | discard_index(&istate); |
8e4710f0 | 1096 | free(wt_gitdir); |
592ec63b | 1097 | free(path); |
fb64ca52 JK |
1098 | } |
1099 | free_worktrees(worktrees); | |
ae7c0c92 JH |
1100 | } |
1101 | ||
0d30feef | 1102 | errors_found |= check_pack_rev_indexes(the_repository, show_progress); |
756f1bcd DS |
1103 | if (verify_bitmap_files(the_repository)) |
1104 | errors_found |= ERROR_BITMAP; | |
0d30feef | 1105 | |
8ba0bbb2 | 1106 | check_connectivity(); |
e0fd51e1 | 1107 | |
f30e4d85 | 1108 | if (the_repository->settings.core_commit_graph) { |
e0fd51e1 | 1109 | struct child_process commit_graph_verify = CHILD_PROCESS_INIT; |
e0fd51e1 | 1110 | |
e0fd51e1 | 1111 | prepare_alt_odb(the_repository); |
f0eaf638 | 1112 | for (odb = the_repository->objects->odb; odb; odb = odb->next) { |
4d0984be | 1113 | child_process_init(&commit_graph_verify); |
4d0984be | 1114 | commit_graph_verify.git_cmd = 1; |
2b709893 ÆAB |
1115 | strvec_pushl(&commit_graph_verify.args, "commit-graph", |
1116 | "verify", "--object-dir", odb->path, NULL); | |
eda206f6 TB |
1117 | if (show_progress) |
1118 | strvec_push(&commit_graph_verify.args, "--progress"); | |
1119 | else | |
1120 | strvec_push(&commit_graph_verify.args, "--no-progress"); | |
e0fd51e1 DS |
1121 | if (run_command(&commit_graph_verify)) |
1122 | errors_found |= ERROR_COMMIT_GRAPH; | |
1123 | } | |
1124 | } | |
1125 | ||
dc557087 | 1126 | if (the_repository->settings.core_multi_pack_index) { |
66ec0390 | 1127 | struct child_process midx_verify = CHILD_PROCESS_INIT; |
66ec0390 | 1128 | |
66ec0390 | 1129 | prepare_alt_odb(the_repository); |
f0eaf638 | 1130 | for (odb = the_repository->objects->odb; odb; odb = odb->next) { |
4d0984be | 1131 | child_process_init(&midx_verify); |
4d0984be | 1132 | midx_verify.git_cmd = 1; |
2b709893 ÆAB |
1133 | strvec_pushl(&midx_verify.args, "multi-pack-index", |
1134 | "verify", "--object-dir", odb->path, NULL); | |
39bdd303 TB |
1135 | if (show_progress) |
1136 | strvec_push(&midx_verify.args, "--progress"); | |
1137 | else | |
1138 | strvec_push(&midx_verify.args, "--no-progress"); | |
66ec0390 | 1139 | if (run_command(&midx_verify)) |
e68a5272 | 1140 | errors_found |= ERROR_MULTI_PACK_INDEX; |
66ec0390 DS |
1141 | } |
1142 | } | |
1143 | ||
e2b4f635 | 1144 | return errors_found; |
20222118 | 1145 | } |