diff options
Diffstat (limited to 'src/fetch.c')
-rw-r--r-- | src/fetch.c | 222 |
1 files changed, 199 insertions, 23 deletions
diff --git a/src/fetch.c b/src/fetch.c index 603284842..f8f853fef 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -18,6 +18,7 @@ #include "pack.h" #include "fetch.h" #include "netops.h" +#include "pkt.h" struct filter_payload { git_remote *remote; @@ -70,7 +71,62 @@ static int filter_wants(git_remote *remote) if (git_repository_odb__weakptr(&p.odb, remote->repo) < 0) return -1; - return remote->transport->ls(remote->transport, &filter_ref__cb, &p); + return git_remote_ls(remote, filter_ref__cb, &p); +} + +/* Wait until we get an ack from the */ +static int recv_pkt(git_pkt **out, gitno_buffer *buf) +{ + const char *ptr = buf->data, *line_end = ptr; + git_pkt *pkt; + int pkt_type, error = 0, ret; + + do { + if (buf->offset > 0) + error = git_pkt_parse_line(&pkt, ptr, &line_end, buf->offset); + else + error = GIT_EBUFS; + + if (error == 0) + break; /* return the pkt */ + + if (error < 0 && error != GIT_EBUFS) + return -1; + + if ((ret = gitno_recv(buf)) < 0) + return -1; + } while (error); + + gitno_consume(buf, line_end); + pkt_type = pkt->type; + if (out != NULL) + *out = pkt; + else + git__free(pkt); + + return pkt_type; +} + +static int store_common(git_transport *t) +{ + git_pkt *pkt = NULL; + gitno_buffer *buf = &t->buffer; + + do { + if (recv_pkt(&pkt, buf) < 0) + return -1; + + if (pkt->type == GIT_PKT_ACK) { + if (git_vector_insert(&t->common, pkt) < 0) + return -1; + } else { + git__free(pkt); + return 0; + } + + } while (1); + + return 0; } /* @@ -81,6 +137,12 @@ static int filter_wants(git_remote *remote) int git_fetch_negotiate(git_remote *remote) { git_transport *t = remote->transport; + gitno_buffer *buf = &t->buffer; + git_buf data = GIT_BUF_INIT; + git_revwalk *walk = NULL; + int error, pkt_type; + unsigned int i; + git_oid oid; if (filter_wants(remote) < 0) { giterr_set(GITERR_NET, "Failed to filter the reference list for wants"); @@ -92,60 +154,174 @@ int git_fetch_negotiate(git_remote *remote) return 0; /* - * Now we have everything set up so we can start tell the server - * what we want and what we have. + * Now we have everything set up so we can start tell the + * server what we want and what we have. Call the function if + * the transport has its own logic. This is transitional and + * will be removed once this function can support git and http. */ - return t->negotiate_fetch(t, remote->repo, &remote->refs); + if (t->own_logic) + return t->negotiate_fetch(t, remote->repo, &remote->refs); + + /* No own logic, do our thing */ + if (git_pkt_buffer_wants(&remote->refs, &t->caps, &data) < 0) + return -1; + + if (git_fetch_setup_walk(&walk, remote->repo) < 0) + goto on_error; + /* + * We don't support any kind of ACK extensions, so the negotiation + * boils down to sending what we have and listening for an ACK + * every once in a while. + */ + i = 0; + while ((error = git_revwalk_next(&oid, walk)) == 0) { + git_pkt_buffer_have(&oid, &data); + i++; + if (i % 20 == 0) { + git_pkt_buffer_flush(&data); + if (git_buf_oom(&data)) + goto on_error; + + if (t->negotiation_step(t, data.ptr, data.size) < 0) + goto on_error; + + git_buf_clear(&data); + if (t->caps.multi_ack) { + if (store_common(t) < 0) + goto on_error; + } else { + pkt_type = recv_pkt(NULL, buf); + + if (pkt_type == GIT_PKT_ACK) { + break; + } else if (pkt_type == GIT_PKT_NAK) { + continue; + } else { + giterr_set(GITERR_NET, "Unexpected pkt type"); + goto on_error; + } + } + } + + if (t->common.length > 0) + break; + + if (i % 20 == 0 && t->rpc) { + git_pkt_ack *pkt; + unsigned int i; + + if (git_pkt_buffer_wants(&remote->refs, &t->caps, &data) < 0) + goto on_error; + + git_vector_foreach(&t->common, i, pkt) { + git_pkt_buffer_have(&pkt->oid, &data); + } + + if (git_buf_oom(&data)) + goto on_error; + } + } + + if (error < 0 && error != GIT_REVWALKOVER) + goto on_error; + + /* Tell the other end that we're done negotiating */ + if (t->rpc && t->common.length > 0) { + git_pkt_ack *pkt; + unsigned int i; + + if (git_pkt_buffer_wants(&remote->refs, &t->caps, &data) < 0) + goto on_error; + + git_vector_foreach(&t->common, i, pkt) { + git_pkt_buffer_have(&pkt->oid, &data); + } + + if (git_buf_oom(&data)) + goto on_error; + } + + git_pkt_buffer_done(&data); + if (t->negotiation_step(t, data.ptr, data.size) < 0) + goto on_error; + + git_buf_free(&data); + git_revwalk_free(walk); + + /* Now let's eat up whatever the server gives us */ + if (!t->caps.multi_ack) { + pkt_type = recv_pkt(NULL, buf); + if (pkt_type != GIT_PKT_ACK && pkt_type != GIT_PKT_NAK) { + giterr_set(GITERR_NET, "Unexpected pkt type"); + return -1; + } + } else { + git_pkt_ack *pkt; + do { + if (recv_pkt((git_pkt **)&pkt, buf) < 0) + return -1; + + if (pkt->type == GIT_PKT_NAK || + (pkt->type == GIT_PKT_ACK && pkt->status != GIT_ACK_CONTINUE)) { + git__free(pkt); + break; + } + + git__free(pkt); + } while (1); + } + + return 0; + +on_error: + git_revwalk_free(walk); + git_buf_free(&data); + return -1; } int git_fetch_download_pack(git_remote *remote, git_off_t *bytes, git_indexer_stats *stats) { + git_transport *t = remote->transport; + if(!remote->need_pack) return 0; - return remote->transport->download_pack(remote->transport, remote->repo, bytes, stats); + if (t->own_logic) + return t->download_pack(t, remote->repo, bytes, stats); + + return git_fetch__download_pack(t, remote->repo, bytes, stats); + } /* Receiving data from a socket and storing it is pretty much the same for git and HTTP */ int git_fetch__download_pack( - const char *buffered, - size_t buffered_size, git_transport *t, git_repository *repo, git_off_t *bytes, git_indexer_stats *stats) { int recvd; - char buff[1024]; - gitno_buffer buf; git_buf path = GIT_BUF_INIT; + gitno_buffer *buf = &t->buffer; git_indexer_stream *idx = NULL; - gitno_buffer_setup(t, &buf, buff, sizeof(buff)); - - if (memcmp(buffered, "PACK", strlen("PACK"))) { - giterr_set(GITERR_NET, "The pack doesn't start with the signature"); - return -1; - } - if (git_buf_joinpath(&path, git_repository_path(repo), "objects/pack") < 0) return -1; if (git_indexer_stream_new(&idx, git_buf_cstr(&path)) < 0) goto on_error; + git_buf_free(&path); memset(stats, 0, sizeof(git_indexer_stats)); - if (git_indexer_stream_add(idx, buffered, buffered_size, stats) < 0) - goto on_error; - - *bytes = buffered_size; + *bytes = 0; do { - if (git_indexer_stream_add(idx, buf.data, buf.offset, stats) < 0) + if (git_indexer_stream_add(idx, buf->data, buf->offset, stats) < 0) goto on_error; - gitno_consume_n(&buf, buf.offset); - if ((recvd = gitno_recv(&buf)) < 0) + gitno_consume_n(buf, buf->offset); + + if ((recvd = gitno_recv(buf)) < 0) goto on_error; *bytes += recvd; |