Skip to content

Commit

Permalink
remote-curl: store url as a strbuf
Browse files Browse the repository at this point in the history
We use a strbuf to generate the string containing the remote
URL, but then detach it to a bare pointer. This makes it
harder to later manipulate the URL, as we have forgotten the
length (and the allocation semantics are not as clear).

Let's instead keep the strbuf around. As a bonus, this
eliminates a confusing double-use of the "buf" strbuf in
main(). Prior to this, it was used both for constructing the
url, and for reading commands from stdin.

The downside is that we have to update each call site to
refer to "url.buf" rather than just "url" when they want the
C string.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
  • Loading branch information
Jeff King authored and Jonathan Nieder committed Oct 15, 2013
1 parent c65d569 commit b227bbc
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions remote-curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#include "credential.h"

static struct remote *remote;
static const char *url; /* always ends with a trailing slash */
/* always ends with a trailing slash */
static struct strbuf url = STRBUF_INIT;

struct options {
int verbosity;
Expand Down Expand Up @@ -112,7 +113,8 @@ static struct ref *parse_info_refs(struct discovery *heads)
mid = &data[i];
if (data[i] == '\n') {
if (mid - start != 40)
die("%sinfo/refs not valid: is this a git repository?", url);
die("%sinfo/refs not valid: is this a git repository?",
url.buf);
data[i] = 0;
ref_name = mid + 1;
ref = xmalloc(sizeof(struct ref) +
Expand All @@ -131,7 +133,7 @@ static struct ref *parse_info_refs(struct discovery *heads)
}

ref = alloc_ref("HEAD");
if (!http_fetch_ref(url, ref) &&
if (!http_fetch_ref(url.buf, ref) &&
!resolve_remote_symref(ref, refs)) {
ref->next = refs;
refs = ref;
Expand Down Expand Up @@ -194,11 +196,11 @@ static struct discovery* discover_refs(const char *service, int for_push)
return last;
free_discovery(last);

strbuf_addf(&refs_url, "%sinfo/refs", url);
if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
if ((!prefixcmp(url.buf, "http://") || !prefixcmp(url.buf, "https://")) &&
git_env_bool("GIT_SMART_HTTP", 1)) {
maybe_smart = 1;
if (!strchr(url, '?'))
if (!strchr(url.buf, '?'))
strbuf_addch(&refs_url, '?');
else
strbuf_addch(&refs_url, '&');
Expand All @@ -216,13 +218,13 @@ static struct discovery* discover_refs(const char *service, int for_push)
break;
case HTTP_MISSING_TARGET:
show_http_message(&type, &buffer);
die("repository '%s' not found", url);
die("repository '%s' not found", url.buf);
case HTTP_NOAUTH:
show_http_message(&type, &buffer);
die("Authentication failed for '%s'", url);
die("Authentication failed for '%s'", url.buf);
default:
show_http_message(&type, &buffer);
die("unable to access '%s': %s", url, curl_errorstr);
die("unable to access '%s': %s", url.buf, curl_errorstr);
}

last= xcalloc(1, sizeof(*last_discovery));
Expand Down Expand Up @@ -588,7 +590,7 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
rpc->out = client.out;
strbuf_init(&rpc->result, 0);

strbuf_addf(&buf, "%s%s", url, svc);
strbuf_addf(&buf, "%s%s", url.buf, svc);
rpc->service_url = strbuf_detach(&buf, NULL);

strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
Expand Down Expand Up @@ -640,7 +642,7 @@ static int fetch_dumb(int nr_heads, struct ref **to_fetch)
for (i = 0; i < nr_heads; i++)
targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));

walker = get_http_walker(url);
walker = get_http_walker(url.buf);
walker->get_all = 1;
walker->get_tree = 1;
walker->get_history = 1;
Expand Down Expand Up @@ -685,7 +687,7 @@ static int fetch_git(struct discovery *heads,
depth_arg = strbuf_detach(&buf, NULL);
argv[argc++] = depth_arg;
}
argv[argc++] = url;
argv[argc++] = url.buf;
argv[argc++] = NULL;

for (i = 0; i < nr_heads; i++) {
Expand Down Expand Up @@ -783,7 +785,7 @@ static int push_dav(int nr_spec, char **specs)
argv[argc++] = "--dry-run";
if (options.verbosity > 1)
argv[argc++] = "--verbose";
argv[argc++] = url;
argv[argc++] = url.buf;
for (i = 0; i < nr_spec; i++)
argv[argc++] = specs[i];
argv[argc++] = NULL;
Expand Down Expand Up @@ -813,7 +815,7 @@ static int push_git(struct discovery *heads, int nr_spec, char **specs)
else if (options.verbosity > 1)
argv_array_push(&args, "--verbose");
argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
argv_array_push(&args, url);
argv_array_push(&args, url.buf);
for (i = 0; i < nr_spec; i++)
argv_array_push(&args, specs[i]);

Expand Down Expand Up @@ -894,14 +896,12 @@ int main(int argc, const char **argv)
remote = remote_get(argv[1]);

if (argc > 2) {
end_url_with_slash(&buf, argv[2]);
end_url_with_slash(&url, argv[2]);
} else {
end_url_with_slash(&buf, remote->url[0]);
end_url_with_slash(&url, remote->url[0]);
}

url = strbuf_detach(&buf, NULL);

http_init(remote, url, 0);
http_init(remote, url.buf, 0);

do {
if (strbuf_getline(&buf, stdin, '\n') == EOF) {
Expand Down

0 comments on commit b227bbc

Please sign in to comment.