transports: buffer the git requests before sending them Trying to send every single line immediately won't give us any speed improvement and duplicates the code we need for other transports. Make the git transport use the same buffer functions as HTTP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
diff --git a/include/git2/indexer.h b/include/git2/indexer.h
index a70fab2..14bd0e4 100644
--- a/include/git2/indexer.h
+++ b/include/git2/indexer.h
@@ -41,7 +41,7 @@ GIT_EXTERN(int) git_indexer_stream_new(git_indexer_stream **out, const char *git
* @param size the size of the data
* @param stats stat storage
*/
-GIT_EXTERN(int) git_indexer_stream_add(git_indexer_stream *idx, void *data, size_t size, git_indexer_stats *stats);
+GIT_EXTERN(int) git_indexer_stream_add(git_indexer_stream *idx, const void *data, size_t size, git_indexer_stats *stats);
/**
* Finalize the pack and index
diff --git a/include/git2/remote.h b/include/git2/remote.h
index 576f584..8f49fdd 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -11,6 +11,7 @@
#include "repository.h"
#include "refspec.h"
#include "net.h"
+#include "indexer.h"
/**
* @file git2/remote.h
diff --git a/src/fetch.c b/src/fetch.c
index 8da4fd8..6fe1b56 100644
--- a/src/fetch.c
+++ b/src/fetch.c
@@ -161,3 +161,44 @@ on_error:
git_indexer_stream_free(idx);
return -1;
}
+
+int git_fetch_setup_walk(git_revwalk **out, git_repository *repo)
+{
+ git_revwalk *walk;
+ git_strarray refs;
+ unsigned int i;
+ git_reference *ref;
+
+ if (git_reference_listall(&refs, repo, GIT_REF_LISTALL) < 0)
+ return -1;
+
+ if (git_revwalk_new(&walk, repo) < 0)
+ return -1;
+
+ git_revwalk_sorting(walk, GIT_SORT_TIME);
+
+ for (i = 0; i < refs.count; ++i) {
+ /* No tags */
+ if (!git__prefixcmp(refs.strings[i], GIT_REFS_TAGS_DIR))
+ continue;
+
+ if (git_reference_lookup(&ref, repo, refs.strings[i]) < 0)
+ goto on_error;
+
+ if (git_reference_type(ref) == GIT_REF_SYMBOLIC)
+ continue;
+ if (git_revwalk_push(walk, git_reference_oid(ref)) < 0)
+ goto on_error;
+
+ git_reference_free(ref);
+ }
+
+ git_strarray_free(&refs);
+ *out = walk;
+ return 0;
+
+on_error:
+ git_reference_free(ref);
+ git_strarray_free(&refs);
+ return -1;
+}
diff --git a/src/fetch.h b/src/fetch.h
index 03767be..b3192a5 100644
--- a/src/fetch.h
+++ b/src/fetch.h
@@ -14,5 +14,6 @@ int git_fetch_download_pack(git_remote *remote, git_off_t *bytes, git_indexer_st
int git_fetch__download_pack(const char *buffered, size_t buffered_size, GIT_SOCKET fd,
git_repository *repo, git_off_t *bytes, git_indexer_stats *stats);
+int git_fetch_setup_walk(git_revwalk **out, git_repository *repo);
#endif
diff --git a/src/indexer.c b/src/indexer.c
index 1834d98..1f8b512 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -288,7 +288,7 @@ on_error:
return -1;
}
-int git_indexer_stream_add(git_indexer_stream *idx, void *data, size_t size, git_indexer_stats *stats)
+int git_indexer_stream_add(git_indexer_stream *idx, const void *data, size_t size, git_indexer_stats *stats)
{
int error;
struct git_pack_header hdr;
diff --git a/src/pkt.c b/src/pkt.c
index ee113cd..2c9fe27 100644
--- a/src/pkt.c
+++ b/src/pkt.c
@@ -287,19 +287,6 @@ static int buffer_want_with_caps(git_remote_head *head, git_transport_caps *caps
return git_buf_printf(buf, "%04xwant %s%c%s\n", len, oid, 0, capstr);
}
-static int send_want_with_caps(git_remote_head *head, git_transport_caps *caps, GIT_SOCKET fd)
-{
- git_buf buf = GIT_BUF_INIT;
- int ret;
-
- if (buffer_want_with_caps(head, caps, &buf) < 0)
- return -1;
-
- ret = gitno_send(fd, buf.ptr, buf.size, 0);
- git_buf_free(&buf);
- return ret;
-}
-
/*
* All "want" packets have the same length and format, so what we do
* is overwrite the OID each time.
@@ -341,47 +328,6 @@ int git_pkt_buffer_wants(const git_vector *refs, git_transport_caps *caps, git_b
return git_pkt_buffer_flush(buf);
}
-int git_pkt_send_wants(const git_vector *refs, git_transport_caps *caps, GIT_SOCKET fd)
-{
- unsigned int i = 0;
- char buf[sizeof(pkt_want_prefix) + GIT_OID_HEXSZ + 1];
- git_remote_head *head;
-
- memcpy(buf, pkt_want_prefix, strlen(pkt_want_prefix));
- buf[sizeof(buf) - 2] = '\n';
- buf[sizeof(buf) - 1] = '\0';
-
- /* If there are common caps, find the first one */
- if (caps->common) {
- for (; i < refs->length; ++i) {
- head = refs->contents[i];
- if (head->local)
- continue;
- else
- break;
- }
-
- if (send_want_with_caps(refs->contents[i], caps, fd) < 0)
- return -1;
-
- /* Increase it here so it's correct whether we run this or not */
- i++;
- }
-
- /* Continue from where we left off */
- for (; i < refs->length; ++i) {
- head = refs->contents[i];
- if (head->local)
- continue;
-
- git_oid_fmt(buf + strlen(pkt_want_prefix), &head->oid);
- if (gitno_send(fd, buf, strlen(buf), 0) < 0)
- return -1;
- }
-
- return git_pkt_send_flush(fd);
-}
-
int git_pkt_buffer_have(git_oid *oid, git_buf *buf)
{
char oidhex[GIT_OID_HEXSZ + 1];
@@ -391,21 +337,7 @@ int git_pkt_buffer_have(git_oid *oid, git_buf *buf)
return git_buf_printf(buf, "%s%s\n", pkt_have_prefix, oidhex);
}
-int git_pkt_send_have(git_oid *oid, GIT_SOCKET fd)
-{
- char buf[] = "0032have 0000000000000000000000000000000000000000\n";
-
- git_oid_fmt(buf + strlen(pkt_have_prefix), oid);
- return gitno_send(fd, buf, strlen(buf), 0);
-}
-
-
int git_pkt_buffer_done(git_buf *buf)
{
return git_buf_puts(buf, pkt_done_str);
}
-
-int git_pkt_send_done(GIT_SOCKET fd)
-{
- return gitno_send(fd, pkt_done_str, strlen(pkt_done_str), 0);
-}
diff --git a/src/pkt.h b/src/pkt.h
index 1f8d62e..7e696f7 100644
--- a/src/pkt.h
+++ b/src/pkt.h
@@ -68,11 +68,8 @@ int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_
int git_pkt_buffer_flush(git_buf *buf);
int git_pkt_send_flush(GIT_SOCKET s);
int git_pkt_buffer_done(git_buf *buf);
-int git_pkt_send_done(GIT_SOCKET s);
int git_pkt_buffer_wants(const git_vector *refs, git_transport_caps *caps, git_buf *buf);
-int git_pkt_send_wants(const git_vector *refs, git_transport_caps *caps, GIT_SOCKET fd);
int git_pkt_buffer_have(git_oid *oid, git_buf *buf);
-int git_pkt_send_have(git_oid *oid, GIT_SOCKET fd);
void git_pkt_free(git_pkt *pkt);
#endif
diff --git a/src/transport.h b/src/transport.h
index 1cea32b..9be96fe 100644
--- a/src/transport.h
+++ b/src/transport.h
@@ -8,6 +8,7 @@
#define INCLUDE_transport_h__
#include "git2/net.h"
+#include "git2/indexer.h"
#include "vector.h"
#define GIT_CAP_OFS_DELTA "ofs-delta"
@@ -66,19 +67,11 @@ struct git_transport {
*/
int (*push)(struct git_transport *transport);
/**
- * Send a 'done' message
- */
- int (*send_done)(struct git_transport *transport);
- /**
* Negotiate the minimal amount of objects that need to be
* retrieved
*/
int (*negotiate_fetch)(struct git_transport *transport, git_repository *repo, const git_vector *wants);
/**
- * Send a flush
- */
- int (*send_flush)(struct git_transport *transport);
- /**
* Download the packfile
*/
int (*download_pack)(struct git_transport *transport, git_repository *repo, git_off_t *bytes, git_indexer_stats *stats);
diff --git a/src/transports/git.c b/src/transports/git.c
index 62106de..31bc21c 100644
--- a/src/transports/git.c
+++ b/src/transports/git.c
@@ -293,41 +293,22 @@ static int git_negotiate_fetch(git_transport *transport, git_repository *repo, c
{
transport_git *t = (transport_git *) transport;
git_revwalk *walk;
- git_reference *ref;
- git_strarray refs;
git_oid oid;
int error;
unsigned int i;
+ git_buf data = GIT_BUF_INIT;
gitno_buffer *buf = &t->buf;
- if (git_pkt_send_wants(wants, &t->caps, t->socket) < 0)
+ if (git_pkt_buffer_wants(wants, &t->caps, &data) < 0)
return -1;
- if (git_reference_listall(&refs, repo, GIT_REF_LISTALL) < 0)
- return -1;
-
- if (git_revwalk_new(&walk, repo) < 0)
- return -1;
-
- git_revwalk_sorting(walk, GIT_SORT_TIME);
-
- for (i = 0; i < refs.count; ++i) {
- /* No tags */
- if (!git__prefixcmp(refs.strings[i], GIT_REFS_TAGS_DIR))
- continue;
-
- if (git_reference_lookup(&ref, repo, refs.strings[i]) < 0)
- goto on_error;
-
- if (git_reference_type(ref) == GIT_REF_SYMBOLIC)
- continue;
-
- if ((error = git_revwalk_push(walk, git_reference_oid(ref))) < 0)
- goto on_error;
+ if (git_fetch_setup_walk(&walk, repo) < 0)
+ goto on_error;
- }
- git_strarray_free(&refs);
+ if (gitno_send(t->socket, data.ptr, data.size, 0) < 0)
+ goto on_error;
+ git_buf_clear(&data);
/*
* We don't support any kind of ACK extensions, so the negotiation
* boils down to sending what we have and listening for an ACK
@@ -335,12 +316,18 @@ static int git_negotiate_fetch(git_transport *transport, git_repository *repo, c
*/
i = 0;
while ((error = git_revwalk_next(&oid, walk)) == 0) {
- error = git_pkt_send_have(&oid, t->socket);
+ git_pkt_buffer_have(&oid, &data);
i++;
if (i % 20 == 0) {
int pkt_type;
- git_pkt_send_flush(t->socket);
+ git_pkt_buffer_flush(&data);
+ if (git_buf_oom(&data))
+ goto on_error;
+
+ if (gitno_send(t->socket, data.ptr, data.size, 0) < 0)
+ goto on_error;
+
pkt_type = recv_pkt(buf);
if (pkt_type == GIT_PKT_ACK) {
@@ -354,34 +341,26 @@ static int git_negotiate_fetch(git_transport *transport, git_repository *repo, c
}
}
- if (error != GIT_EREVWALKOVER)
+ if (error < 0 && error != GIT_EREVWALKOVER)
goto on_error;
- git_pkt_send_flush(t->socket);
- git_pkt_send_done(t->socket);
+ /* Tell the other end that we're done negotiating */
+ git_buf_clear(&data);
+ git_pkt_buffer_flush(&data);
+ git_pkt_buffer_done(&data);
+ if (gitno_send(t->socket, data.ptr, data.size, 0) < 0)
+ goto on_error;
+ git_buf_free(&data);
git_revwalk_free(walk);
return 0;
on_error:
+ git_buf_free(&data);
git_revwalk_free(walk);
return -1;
}
-static int git_send_flush(git_transport *transport)
-{
- transport_git *t = (transport_git *) transport;
-
- return git_pkt_send_flush(t->socket);
-}
-
-static int git_send_done(git_transport *transport)
-{
- transport_git *t = (transport_git *) transport;
-
- return git_pkt_send_done(t->socket);
-}
-
static int git_download_pack(git_transport *transport, git_repository *repo, git_off_t *bytes, git_indexer_stats *stats)
{
transport_git *t = (transport_git *) transport;
@@ -424,7 +403,6 @@ static int git_download_pack(git_transport *transport, git_repository *repo, git
return read_bytes;
}
-
static int git_close(git_transport *transport)
{
transport_git *t = (transport_git*) transport;
@@ -476,8 +454,6 @@ int git_transport_git(git_transport **out)
t->parent.connect = git_connect;
t->parent.ls = git_ls;
t->parent.negotiate_fetch = git_negotiate_fetch;
- t->parent.send_flush = git_send_flush;
- t->parent.send_done = git_send_done;
t->parent.download_pack = git_download_pack;
t->parent.close = git_close;
t->parent.free = git_free;
diff --git a/src/transports/http.c b/src/transports/http.c
index e6a7094..012e8ff 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -410,44 +410,6 @@ static int parse_response(transport_http *t)
return ret;
}
-static int setup_walk(git_revwalk **out, git_repository *repo)
-{
- git_revwalk *walk;
- git_strarray refs;
- unsigned int i;
- git_reference *ref;
-
- if (git_reference_listall(&refs, repo, GIT_REF_LISTALL) < 0)
- return -1;
-
- if (git_revwalk_new(&walk, repo) < 0)
- return -1;
-
- git_revwalk_sorting(walk, GIT_SORT_TIME);
-
- for (i = 0; i < refs.count; ++i) {
- /* No tags */
- if (!git__prefixcmp(refs.strings[i], GIT_REFS_TAGS_DIR))
- continue;
-
- if (git_reference_lookup(&ref, repo, refs.strings[i]) < 0)
- goto on_error;
-
- if (git_reference_type(ref) == GIT_REF_SYMBOLIC)
- continue;
- if (git_revwalk_push(walk, git_reference_oid(ref)) < 0)
- goto on_error;
- }
-
- git_strarray_free(&refs);
- *out = walk;
- return 0;
-
-on_error:
- git_strarray_free(&refs);
- return -1;
-}
-
static int http_negotiate_fetch(git_transport *transport, git_repository *repo, const git_vector *wants)
{
transport_http *t = (transport_http *) transport;
@@ -470,7 +432,7 @@ static int http_negotiate_fetch(git_transport *transport, git_repository *repo,
if (git_vector_init(common, 16, NULL) < 0)
return -1;
- if (setup_walk(&walk, repo) < 0)
+ if (git_fetch_setup_walk(&walk, repo) < 0)
return -1;
do {