transport-git: Encapsulation ist gut
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
diff --git a/src/transport_git.c b/src/transport_git.c
index 8529fd4..b9fe658 100644
--- a/src/transport_git.c
+++ b/src/transport_git.c
@@ -36,13 +36,12 @@ typedef struct {
*
* For example: 0035git-upload-pack /libgit2/libgit2\0host=github.com\0
*/
-static int gen_proto(char **out, int *outlen, const char *cmd, const char *url)
+static int gen_proto(git_buf *request, const char *cmd, const char *url)
{
char *delim, *repo;
char default_command[] = "git-upload-pack";
char host[] = "host=";
int len;
- git_buf buf = GIT_BUF_INIT;
delim = strchr(url, '/');
if (delim == NULL)
@@ -59,31 +58,27 @@ static int gen_proto(char **out, int *outlen, const char *cmd, const char *url)
len = 4 + strlen(cmd) + 1 + strlen(repo) + 1 + strlen(host) + (delim - url) + 1 + 1;
- git_buf_grow(&buf, len);
+ git_buf_grow(request, len);
+ git_buf_printf(request, "%04x%s %s%c%s", len, cmd, repo, 0, host);
+ git_buf_put(request, url, delim - url);
+ git_buf_putc(request, '\0');
- git_buf_printf(&buf, "%04x%s %s%c%s", len, cmd, repo, 0, host);
- git_buf_put(&buf, url, delim - url);
- git_buf_putc(&buf, '\0');
-
- *outlen = len;
- *out = buf.ptr;
-
- return GIT_SUCCESS;
+ return git_buf_oom(request);
}
static int send_request(GIT_SOCKET s, const char *cmd, const char *url)
{
- int error, len;
- char *msg = NULL;
+ int error;
+ git_buf request = GIT_BUF_INIT;
- error = gen_proto(&msg, &len, cmd, url);
+ error = gen_proto(&request, cmd, url);
if (error < GIT_SUCCESS)
goto cleanup;
- error = gitno_send(s, msg, len, 0);
+ error = gitno_send(s, request.ptr, request.size, 0);
cleanup:
- free(msg);
+ git_buf_free(&request);
return error;
}