diff options
| author | Carlos Martín Nieto <carlos@cmartin.tk> | 2011-06-08 19:11:38 +0200 |
|---|---|---|
| committer | Carlos Martín Nieto <carlos@cmartin.tk> | 2011-06-26 18:18:12 +0200 |
| commit | 6a9597c5b57456dfe02e4b5ba94ef437907fe1f1 (patch) | |
| tree | 364f652d94182515bc238b14f83ddc1f2123b668 /src/pkt.c | |
| parent | 8b9e8de5ce2fa4da75bf5b27a73f6d74140c6eaf (diff) | |
| download | libgit2-6a9597c5b57456dfe02e4b5ba94ef437907fe1f1.tar.gz | |
Add function to generate a request
Add git_pkt_gen_proto to crete a request from an url.
Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
Diffstat (limited to 'src/pkt.c')
| -rw-r--r-- | src/pkt.c | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -172,3 +172,42 @@ int git_pkt_parse_line(git_pkt **head, const char *line, const char **out) return error; } + +/* + * Create a git procol request. + * + * For example: 0035git-upload-pack /libgit2/libgit2\0host=github.com\0 + * + * TODO: the command should not be hard-coded + */ +int git_pkt_gen_proto(char **out, int *outlen, const char *url) +{ + char *delim, *repo, *ptr; + char command[] = "git-upload-pack"; + char host[] = "host="; + int len; + + delim = strchr(url, '/'); + if (delim == NULL) + return git__throw(GIT_EOBJCORRUPTED, "Failed to create proto-request: malformed URL"); + + repo = delim; + + delim = strchr(url, ':'); + if (delim == NULL) + delim = strchr(url, '/'); + + len = 4 + STRLEN(command) + 1 + strlen(repo) + 1 + STRLEN(host) + (delim - url) + 2; + + *out = git__malloc(len); + if (*out == NULL) + return GIT_ENOMEM; + + *outlen = len - 1; + ptr = *out; + memset(ptr, 0x0, len); + /* We expect the return value to be > len - 1 so don't bother checking it */ + snprintf(ptr, len -1, "%04x%s %s%c%s%s", len - 1, command, repo, 0, host, url); + + return GIT_SUCCESS; +} |
