fsck.c 25 KB

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