connected.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "cache.h"
  2. #include "object-store.h"
  3. #include "run-command.h"
  4. #include "sigchain.h"
  5. #include "connected.h"
  6. #include "transport.h"
  7. #include "packfile.h"
  8. #include "promisor-remote.h"
  9. /*
  10. * If we feed all the commits we want to verify to this command
  11. *
  12. * $ git rev-list --objects --stdin --not --all
  13. *
  14. * and if it does not error out, that means everything reachable from
  15. * these commits locally exists and is connected to our existing refs.
  16. * Note that this does _not_ validate the individual objects.
  17. *
  18. * Returns 0 if everything is connected, non-zero otherwise.
  19. */
  20. int check_connected(oid_iterate_fn fn, void *cb_data,
  21. struct check_connected_options *opt)
  22. {
  23. struct child_process rev_list = CHILD_PROCESS_INIT;
  24. FILE *rev_list_in;
  25. struct check_connected_options defaults = CHECK_CONNECTED_INIT;
  26. struct object_id oid;
  27. int err = 0;
  28. struct packed_git *new_pack = NULL;
  29. struct transport *transport;
  30. size_t base_len;
  31. if (!opt)
  32. opt = &defaults;
  33. transport = opt->transport;
  34. if (fn(cb_data, &oid)) {
  35. if (opt->err_fd)
  36. close(opt->err_fd);
  37. return err;
  38. }
  39. if (transport && transport->smart_options &&
  40. transport->smart_options->self_contained_and_connected &&
  41. transport->pack_lockfiles.nr == 1 &&
  42. strip_suffix(transport->pack_lockfiles.items[0].string,
  43. ".keep", &base_len)) {
  44. struct strbuf idx_file = STRBUF_INIT;
  45. strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
  46. base_len);
  47. strbuf_addstr(&idx_file, ".idx");
  48. new_pack = add_packed_git(idx_file.buf, idx_file.len, 1);
  49. strbuf_release(&idx_file);
  50. }
  51. if (has_promisor_remote()) {
  52. /*
  53. * For partial clones, we don't want to have to do a regular
  54. * connectivity check because we have to enumerate and exclude
  55. * all promisor objects (slow), and then the connectivity check
  56. * itself becomes a no-op because in a partial clone every
  57. * object is a promisor object. Instead, just make sure we
  58. * received, in a promisor packfile, the objects pointed to by
  59. * each wanted ref.
  60. *
  61. * Before checking for promisor packs, be sure we have the
  62. * latest pack-files loaded into memory.
  63. */
  64. reprepare_packed_git(the_repository);
  65. do {
  66. struct packed_git *p;
  67. for (p = get_all_packs(the_repository); p; p = p->next) {
  68. if (!p->pack_promisor)
  69. continue;
  70. if (find_pack_entry_one(oid.hash, p))
  71. goto promisor_pack_found;
  72. }
  73. /*
  74. * Fallback to rev-list with oid and the rest of the
  75. * object IDs provided by fn.
  76. */
  77. goto no_promisor_pack_found;
  78. promisor_pack_found:
  79. ;
  80. } while (!fn(cb_data, &oid));
  81. return 0;
  82. }
  83. no_promisor_pack_found:
  84. if (opt->shallow_file) {
  85. strvec_push(&rev_list.args, "--shallow-file");
  86. strvec_push(&rev_list.args, opt->shallow_file);
  87. }
  88. strvec_push(&rev_list.args,"rev-list");
  89. strvec_push(&rev_list.args, "--objects");
  90. strvec_push(&rev_list.args, "--stdin");
  91. if (has_promisor_remote())
  92. strvec_push(&rev_list.args, "--exclude-promisor-objects");
  93. if (!opt->is_deepening_fetch) {
  94. strvec_push(&rev_list.args, "--not");
  95. strvec_push(&rev_list.args, "--all");
  96. }
  97. strvec_push(&rev_list.args, "--quiet");
  98. strvec_push(&rev_list.args, "--alternate-refs");
  99. if (opt->progress)
  100. strvec_pushf(&rev_list.args, "--progress=%s",
  101. _("Checking connectivity"));
  102. rev_list.git_cmd = 1;
  103. rev_list.env = opt->env;
  104. rev_list.in = -1;
  105. rev_list.no_stdout = 1;
  106. if (opt->err_fd)
  107. rev_list.err = opt->err_fd;
  108. else
  109. rev_list.no_stderr = opt->quiet;
  110. if (start_command(&rev_list))
  111. return error(_("Could not run 'git rev-list'"));
  112. sigchain_push(SIGPIPE, SIG_IGN);
  113. rev_list_in = xfdopen(rev_list.in, "w");
  114. do {
  115. /*
  116. * If index-pack already checked that:
  117. * - there are no dangling pointers in the new pack
  118. * - the pack is self contained
  119. * Then if the updated ref is in the new pack, then we
  120. * are sure the ref is good and not sending it to
  121. * rev-list for verification.
  122. */
  123. if (new_pack && find_pack_entry_one(oid.hash, new_pack))
  124. continue;
  125. if (fprintf(rev_list_in, "%s\n", oid_to_hex(&oid)) < 0)
  126. break;
  127. } while (!fn(cb_data, &oid));
  128. if (ferror(rev_list_in) || fflush(rev_list_in)) {
  129. if (errno != EPIPE && errno != EINVAL)
  130. error_errno(_("failed write to rev-list"));
  131. err = -1;
  132. }
  133. if (fclose(rev_list_in))
  134. err = error_errno(_("failed to close rev-list's stdin"));
  135. sigchain_pop(SIGPIPE);
  136. return finish_command(&rev_list) || err;
  137. }