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