Commit b49c8f71 by Carlos Martín Nieto

remote: use the same code to control git and http

This allows us to add capabilitites to both at the same time, keeps
them in sync and removes a lot of code.

gitno_buffer now uses a callback to fill its buffer, allowing us to
use the same interface for git and http (which uses callbacks).
parent 114dc6e1
...@@ -79,7 +79,7 @@ static int recv_pkt(git_pkt **out, gitno_buffer *buf) ...@@ -79,7 +79,7 @@ static int recv_pkt(git_pkt **out, gitno_buffer *buf)
{ {
const char *ptr = buf->data, *line_end = ptr; const char *ptr = buf->data, *line_end = ptr;
git_pkt *pkt; git_pkt *pkt;
int pkt_type, error = 0; int pkt_type, error = 0, ret;
do { do {
if (buf->offset > 0) if (buf->offset > 0)
...@@ -106,7 +106,7 @@ static int recv_pkt(git_pkt **out, gitno_buffer *buf) ...@@ -106,7 +106,7 @@ static int recv_pkt(git_pkt **out, gitno_buffer *buf)
return GIT_PKT_NAK; return GIT_PKT_NAK;
} }
if ((error = gitno_recv(buf)) < 0) if ((ret = gitno_recv(buf)) < 0)
return -1; return -1;
} while (error); } while (error);
...@@ -122,7 +122,6 @@ static int recv_pkt(git_pkt **out, gitno_buffer *buf) ...@@ -122,7 +122,6 @@ static int recv_pkt(git_pkt **out, gitno_buffer *buf)
static int store_common(git_transport *t) static int store_common(git_transport *t)
{ {
int done = 0;
git_pkt *pkt = NULL; git_pkt *pkt = NULL;
gitno_buffer *buf = &t->buffer; gitno_buffer *buf = &t->buffer;
...@@ -219,12 +218,42 @@ int git_fetch_negotiate(git_remote *remote) ...@@ -219,12 +218,42 @@ int git_fetch_negotiate(git_remote *remote)
if (t->common.length > 0) if (t->common.length > 0)
break; 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) if (error < 0 && error != GIT_REVWALKOVER)
goto on_error; goto on_error;
/* Tell the other end that we're done negotiating */ /* 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); git_pkt_buffer_done(&data);
if (t->negotiation_step(t, data.ptr, data.size) < 0) if (t->negotiation_step(t, data.ptr, data.size) < 0)
goto on_error; goto on_error;
...@@ -233,10 +262,26 @@ int git_fetch_negotiate(git_remote *remote) ...@@ -233,10 +262,26 @@ int git_fetch_negotiate(git_remote *remote)
git_revwalk_free(walk); git_revwalk_free(walk);
/* Now let's eat up whatever the server gives us */ /* Now let's eat up whatever the server gives us */
pkt_type = recv_pkt(NULL, buf); if (!t->caps.multi_ack) {
if (pkt_type != GIT_PKT_ACK && pkt_type != GIT_PKT_NAK) { pkt_type = recv_pkt(NULL, buf);
giterr_set(GITERR_NET, "Unexpected pkt type"); if (pkt_type != GIT_PKT_ACK && pkt_type != GIT_PKT_NAK) {
return -1; 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; return 0;
...@@ -257,50 +302,39 @@ int git_fetch_download_pack(git_remote *remote, git_off_t *bytes, git_indexer_st ...@@ -257,50 +302,39 @@ int git_fetch_download_pack(git_remote *remote, git_off_t *bytes, git_indexer_st
if (t->own_logic) if (t->own_logic)
return t->download_pack(t, remote->repo, bytes, stats); return t->download_pack(t, remote->repo, bytes, stats);
return git_fetch__download_pack(NULL, 0, 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 */ /* Receiving data from a socket and storing it is pretty much the same for git and HTTP */
int git_fetch__download_pack( int git_fetch__download_pack(
const char *buffered,
size_t buffered_size,
git_transport *t, git_transport *t,
git_repository *repo, git_repository *repo,
git_off_t *bytes, git_off_t *bytes,
git_indexer_stats *stats) git_indexer_stats *stats)
{ {
int recvd; int recvd;
char buff[1024];
gitno_buffer buf;
git_buf path = GIT_BUF_INIT; git_buf path = GIT_BUF_INIT;
gitno_buffer *buf = &t->buffer;
git_indexer_stream *idx = NULL; git_indexer_stream *idx = NULL;
gitno_buffer_setup(t, &buf, buff, sizeof(buff));
if (buffered && 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) if (git_buf_joinpath(&path, git_repository_path(repo), "objects/pack") < 0)
return -1; return -1;
if (git_indexer_stream_new(&idx, git_buf_cstr(&path)) < 0) if (git_indexer_stream_new(&idx, git_buf_cstr(&path)) < 0)
goto on_error; goto on_error;
git_buf_free(&path);
memset(stats, 0, sizeof(git_indexer_stats)); memset(stats, 0, sizeof(git_indexer_stats));
if (buffered && git_indexer_stream_add(idx, buffered, buffered_size, stats) < 0) *bytes = 0;
goto on_error;
*bytes = buffered_size;
do { 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; goto on_error;
gitno_consume_n(&buf, buf.offset); gitno_consume_n(buf, buf->offset);
if ((recvd = gitno_recv(&buf)) < 0)
if ((recvd = gitno_recv(buf)) < 0)
goto on_error; goto on_error;
*bytes += recvd; *bytes += recvd;
......
...@@ -12,8 +12,7 @@ ...@@ -12,8 +12,7 @@
int git_fetch_negotiate(git_remote *remote); int git_fetch_negotiate(git_remote *remote);
int git_fetch_download_pack(git_remote *remote, git_off_t *bytes, git_indexer_stats *stats); int git_fetch_download_pack(git_remote *remote, git_off_t *bytes, git_indexer_stats *stats);
int git_fetch__download_pack(const char *buffered, size_t buffered_size, git_transport *t, int git_fetch__download_pack(git_transport *t, git_repository *repo, git_off_t *bytes, git_indexer_stats *stats);
git_repository *repo, git_off_t *bytes, git_indexer_stats *stats);
int git_fetch_setup_walk(git_revwalk **out, git_repository *repo); int git_fetch_setup_walk(git_revwalk **out, git_repository *repo);
#endif #endif
...@@ -61,7 +61,7 @@ static int ssl_set_error(gitno_ssl *ssl, int error) ...@@ -61,7 +61,7 @@ static int ssl_set_error(gitno_ssl *ssl, int error)
} }
#endif #endif
void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigned int len) void gitno_buffer_setup_callback(git_transport *t, gitno_buffer *buf, char *data, unsigned int len, int (*recv)(gitno_buffer *buf), void *cb_data)
{ {
memset(buf, 0x0, sizeof(gitno_buffer)); memset(buf, 0x0, sizeof(gitno_buffer));
memset(data, 0x0, len); memset(data, 0x0, len);
...@@ -73,6 +73,19 @@ void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigne ...@@ -73,6 +73,19 @@ void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigne
if (t->encrypt) if (t->encrypt)
buf->ssl = &t->ssl; buf->ssl = &t->ssl;
#endif #endif
buf->recv = recv;
buf->cb_data = cb_data;
}
void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigned int len)
{
gitno_buffer_setup_callback(t, buf, data, len, gitno__recv, NULL);
}
int gitno_recv(gitno_buffer *buf)
{
return buf->recv(buf);
} }
#ifdef GIT_SSL #ifdef GIT_SSL
...@@ -91,7 +104,7 @@ static int ssl_recv(gitno_ssl *ssl, void *data, size_t len) ...@@ -91,7 +104,7 @@ static int ssl_recv(gitno_ssl *ssl, void *data, size_t len)
} }
#endif #endif
int gitno_recv(gitno_buffer *buf) int gitno__recv(gitno_buffer *buf)
{ {
int ret; int ret;
......
...@@ -18,10 +18,14 @@ struct gitno_buffer { ...@@ -18,10 +18,14 @@ struct gitno_buffer {
#ifdef GIT_SSL #ifdef GIT_SSL
struct gitno_ssl *ssl; struct gitno_ssl *ssl;
#endif #endif
int (*recv)(gitno_buffer *buffer);
void *cb_data;
}; };
void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigned int len); void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigned int len);
void gitno_buffer_setup_callback(git_transport *t, gitno_buffer *buf, char *data, unsigned int len, int (*recv)(gitno_buffer *buf), void *cb_data);
int gitno_recv(gitno_buffer *buf); int gitno_recv(gitno_buffer *buf);
int gitno__recv(gitno_buffer *buf);
void gitno_consume(gitno_buffer *buf, const char *ptr); void gitno_consume(gitno_buffer *buf, const char *ptr);
void gitno_consume_n(gitno_buffer *buf, size_t cons); void gitno_consume_n(gitno_buffer *buf, size_t cons);
......
...@@ -42,15 +42,29 @@ static int flush_pkt(git_pkt **out) ...@@ -42,15 +42,29 @@ static int flush_pkt(git_pkt **out)
/* the rest of the line will be useful for multi_ack */ /* the rest of the line will be useful for multi_ack */
static int ack_pkt(git_pkt **out, const char *line, size_t len) static int ack_pkt(git_pkt **out, const char *line, size_t len)
{ {
git_pkt *pkt; git_pkt_ack *pkt;
GIT_UNUSED(line); GIT_UNUSED(line);
GIT_UNUSED(len); GIT_UNUSED(len);
pkt = git__malloc(sizeof(git_pkt)); pkt = git__calloc(1, sizeof(git_pkt_ack));
GITERR_CHECK_ALLOC(pkt); GITERR_CHECK_ALLOC(pkt);
pkt->type = GIT_PKT_ACK; pkt->type = GIT_PKT_ACK;
*out = pkt; line += 3;
len -= 3;
if (len >= GIT_OID_HEXSZ) {
git_oid_fromstr(&pkt->oid, line + 1);
line += GIT_OID_HEXSZ + 1;
len -= GIT_OID_HEXSZ + 1;
}
if (len >= 7) {
if (!git__prefixcmp(line + 1, "continue"))
pkt->status = GIT_ACK_CONTINUE;
}
*out = (git_pkt *) pkt;
return 0; return 0;
} }
......
...@@ -56,3 +56,35 @@ int git_protocol_store_refs(git_protocol *p, const char *data, size_t len) ...@@ -56,3 +56,35 @@ int git_protocol_store_refs(git_protocol *p, const char *data, size_t len)
return 0; return 0;
} }
int git_protocol_detect_caps(git_pkt_ref *pkt, git_transport_caps *caps)
{
const char *ptr;
/* No refs or capabilites, odd but not a problem */
if (pkt == NULL || pkt->capabilities == NULL)
return 0;
ptr = pkt->capabilities;
while (ptr != NULL && *ptr != '\0') {
if (*ptr == ' ')
ptr++;
if(!git__prefixcmp(ptr, GIT_CAP_OFS_DELTA)) {
caps->common = caps->ofs_delta = 1;
ptr += strlen(GIT_CAP_OFS_DELTA);
continue;
}
if(!git__prefixcmp(ptr, GIT_CAP_MULTI_ACK)) {
caps->common = caps->multi_ack = 1;
ptr += strlen(GIT_CAP_MULTI_ACK);
continue;
}
/* We don't know this capability, so skip it */
ptr = strchr(ptr, ' ');
}
return 0;
}
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "transport.h" #include "transport.h"
#include "buffer.h" #include "buffer.h"
#include "pkt.h"
typedef struct { typedef struct {
git_transport *transport; git_transport *transport;
...@@ -19,5 +20,6 @@ typedef struct { ...@@ -19,5 +20,6 @@ typedef struct {
} git_protocol; } git_protocol;
int git_protocol_store_refs(git_protocol *p, const char *data, size_t len); int git_protocol_store_refs(git_protocol *p, const char *data, size_t len);
int git_protocol_detect_caps(git_pkt_ref *pkt, git_transport_caps *caps);
#endif #endif
...@@ -74,7 +74,8 @@ struct git_transport { ...@@ -74,7 +74,8 @@ struct git_transport {
connected : 1, connected : 1,
check_cert: 1, check_cert: 1,
encrypt : 1, encrypt : 1,
own_logic: 1; /* transitional */ own_logic: 1, /* transitional */
rpc: 1; /* git-speak for the HTTP transport */
#ifdef GIT_SSL #ifdef GIT_SSL
struct gitno_ssl ssl; struct gitno_ssl ssl;
#endif #endif
......
...@@ -156,42 +156,6 @@ static int store_refs(transport_git *t) ...@@ -156,42 +156,6 @@ static int store_refs(transport_git *t)
} }
} }
static int detect_caps(transport_git *t)
{
git_vector *refs = &t->refs;
git_pkt_ref *pkt;
git_transport_caps *caps = &t->parent.caps;
const char *ptr;
pkt = git_vector_get(refs, 0);
/* No refs or capabilites, odd but not a problem */
if (pkt == NULL || pkt->capabilities == NULL)
return 0;
ptr = pkt->capabilities;
while (ptr != NULL && *ptr != '\0') {
if (*ptr == ' ')
ptr++;
if(!git__prefixcmp(ptr, GIT_CAP_OFS_DELTA)) {
caps->common = caps->ofs_delta = 1;
ptr += strlen(GIT_CAP_OFS_DELTA);
continue;
}
if(!git__prefixcmp(ptr, GIT_CAP_MULTI_ACK)) {
caps->common = caps->multi_ack = 1;
ptr += strlen(GIT_CAP_MULTI_ACK);
continue;
}
/* We don't know this capability, so skip it */
ptr = strchr(ptr, ' ');
}
return 0;
}
/* /*
* Since this is a network connection, we need to parse and store the * Since this is a network connection, we need to parse and store the
* pkt-lines at this stage and keep them there. * pkt-lines at this stage and keep them there.
...@@ -219,7 +183,7 @@ static int git_connect(git_transport *transport, int direction) ...@@ -219,7 +183,7 @@ static int git_connect(git_transport *transport, int direction)
if (store_refs(t) < 0) if (store_refs(t) < 0)
goto cleanup; goto cleanup;
if (detect_caps(t) < 0) if (git_protocol_detect_caps(git_vector_get(&t->refs, 0), &transport->caps) < 0)
goto cleanup; goto cleanup;
return 0; return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment