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