Skip to content

Commit b227bbc

Browse files
peffjrn
authored andcommitted
remote-curl: store url as a strbuf
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 <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]>
1 parent c65d569 commit b227bbc

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

remote-curl.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
#include "credential.h"
1212

1313
static struct remote *remote;
14-
static const char *url; /* always ends with a trailing slash */
14+
/* always ends with a trailing slash */
15+
static struct strbuf url = STRBUF_INIT;
1516

1617
struct options {
1718
int verbosity;
@@ -112,7 +113,8 @@ static struct ref *parse_info_refs(struct discovery *heads)
112113
mid = &data[i];
113114
if (data[i] == '\n') {
114115
if (mid - start != 40)
115-
die("%sinfo/refs not valid: is this a git repository?", url);
116+
die("%sinfo/refs not valid: is this a git repository?",
117+
url.buf);
116118
data[i] = 0;
117119
ref_name = mid + 1;
118120
ref = xmalloc(sizeof(struct ref) +
@@ -131,7 +133,7 @@ static struct ref *parse_info_refs(struct discovery *heads)
131133
}
132134

133135
ref = alloc_ref("HEAD");
134-
if (!http_fetch_ref(url, ref) &&
136+
if (!http_fetch_ref(url.buf, ref) &&
135137
!resolve_remote_symref(ref, refs)) {
136138
ref->next = refs;
137139
refs = ref;
@@ -194,11 +196,11 @@ static struct discovery* discover_refs(const char *service, int for_push)
194196
return last;
195197
free_discovery(last);
196198

197-
strbuf_addf(&refs_url, "%sinfo/refs", url);
198-
if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
199+
strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
200+
if ((!prefixcmp(url.buf, "http://") || !prefixcmp(url.buf, "https://")) &&
199201
git_env_bool("GIT_SMART_HTTP", 1)) {
200202
maybe_smart = 1;
201-
if (!strchr(url, '?'))
203+
if (!strchr(url.buf, '?'))
202204
strbuf_addch(&refs_url, '?');
203205
else
204206
strbuf_addch(&refs_url, '&');
@@ -216,13 +218,13 @@ static struct discovery* discover_refs(const char *service, int for_push)
216218
break;
217219
case HTTP_MISSING_TARGET:
218220
show_http_message(&type, &buffer);
219-
die("repository '%s' not found", url);
221+
die("repository '%s' not found", url.buf);
220222
case HTTP_NOAUTH:
221223
show_http_message(&type, &buffer);
222-
die("Authentication failed for '%s'", url);
224+
die("Authentication failed for '%s'", url.buf);
223225
default:
224226
show_http_message(&type, &buffer);
225-
die("unable to access '%s': %s", url, curl_errorstr);
227+
die("unable to access '%s': %s", url.buf, curl_errorstr);
226228
}
227229

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

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

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

643-
walker = get_http_walker(url);
645+
walker = get_http_walker(url.buf);
644646
walker->get_all = 1;
645647
walker->get_tree = 1;
646648
walker->get_history = 1;
@@ -685,7 +687,7 @@ static int fetch_git(struct discovery *heads,
685687
depth_arg = strbuf_detach(&buf, NULL);
686688
argv[argc++] = depth_arg;
687689
}
688-
argv[argc++] = url;
690+
argv[argc++] = url.buf;
689691
argv[argc++] = NULL;
690692

691693
for (i = 0; i < nr_heads; i++) {
@@ -783,7 +785,7 @@ static int push_dav(int nr_spec, char **specs)
783785
argv[argc++] = "--dry-run";
784786
if (options.verbosity > 1)
785787
argv[argc++] = "--verbose";
786-
argv[argc++] = url;
788+
argv[argc++] = url.buf;
787789
for (i = 0; i < nr_spec; i++)
788790
argv[argc++] = specs[i];
789791
argv[argc++] = NULL;
@@ -813,7 +815,7 @@ static int push_git(struct discovery *heads, int nr_spec, char **specs)
813815
else if (options.verbosity > 1)
814816
argv_array_push(&args, "--verbose");
815817
argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
816-
argv_array_push(&args, url);
818+
argv_array_push(&args, url.buf);
817819
for (i = 0; i < nr_spec; i++)
818820
argv_array_push(&args, specs[i]);
819821

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

896898
if (argc > 2) {
897-
end_url_with_slash(&buf, argv[2]);
899+
end_url_with_slash(&url, argv[2]);
898900
} else {
899-
end_url_with_slash(&buf, remote->url[0]);
901+
end_url_with_slash(&url, remote->url[0]);
900902
}
901903

902-
url = strbuf_detach(&buf, NULL);
903-
904-
http_init(remote, url, 0);
904+
http_init(remote, url.buf, 0);
905905

906906
do {
907907
if (strbuf_getline(&buf, stdin, '\n') == EOF) {

0 commit comments

Comments
 (0)