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 <peff@peff.net> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>

Jeff King committed Sep 28, 2013 at 04:35 UTC b227bbc43a568b282b5f8cb35e563d00d60b272d
1 file changed +19 -19
remote-curl.c
+19 -19
@@ -11,7 +11,8 @@
11 #include "credential.h"
12
13 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;
16
17 struct options {
18 int verbosity;
@@ -112,7 +113,8 @@ static struct ref *parse_info_refs(struct discovery *heads)
113 mid = &data[i];
114 if (data[i] == '\n') {
115 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);
118 data[i] = 0;
119 ref_name = mid + 1;
120 ref = xmalloc(sizeof(struct ref) +
@@ -131,7 +133,7 @@ static struct ref *parse_info_refs(struct discovery *heads)
133 }
134
135 ref = alloc_ref("HEAD");
134 - if (!http_fetch_ref(url, ref) &&
136 + if (!http_fetch_ref(url.buf, ref) &&
137 !resolve_remote_symref(ref, refs)) {
138 ref->next = refs;
139 refs = ref;
@@ -194,11 +196,11 @@ static struct discovery* discover_refs(const char *service, int for_push)
196 return last;
197 free_discovery(last);
198
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://")) &&
201 git_env_bool("GIT_SMART_HTTP", 1)) {
202 maybe_smart = 1;
201 - if (!strchr(url, '?'))
203 + if (!strchr(url.buf, '?'))
204 strbuf_addch(&refs_url, '?');
205 else
206 strbuf_addch(&refs_url, '&');
@@ -216,13 +218,13 @@ static struct discovery* discover_refs(const char *service, int for_push)
218 break;
219 case HTTP_MISSING_TARGET:
220 show_http_message(&type, &buffer);
219 - die("repository '%s' not found", url);
221 + die("repository '%s' not found", url.buf);
222 case HTTP_NOAUTH:
223 show_http_message(&type, &buffer);
222 - die("Authentication failed for '%s'", url);
224 + die("Authentication failed for '%s'", url.buf);
225 default:
226 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);
228 }
229
230 last= xcalloc(1, sizeof(*last_discovery));
@@ -588,7 +590,7 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
590 rpc->out = client.out;
591 strbuf_init(&rpc->result, 0);
592
591 - strbuf_addf(&buf, "%s%s", url, svc);
593 + strbuf_addf(&buf, "%s%s", url.buf, svc);
594 rpc->service_url = strbuf_detach(&buf, NULL);
595
596 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)
642 for (i = 0; i < nr_heads; i++)
643 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
644
643 - walker = get_http_walker(url);
645 + walker = get_http_walker(url.buf);
646 walker->get_all = 1;
647 walker->get_tree = 1;
648 walker->get_history = 1;
@@ -685,7 +687,7 @@ static int fetch_git(struct discovery *heads,
687 depth_arg = strbuf_detach(&buf, NULL);
688 argv[argc++] = depth_arg;
689 }
688 - argv[argc++] = url;
690 + argv[argc++] = url.buf;
691 argv[argc++] = NULL;
692
693 for (i = 0; i < nr_heads; i++) {
@@ -783,7 +785,7 @@ static int push_dav(int nr_spec, char **specs)
785 argv[argc++] = "--dry-run";
786 if (options.verbosity > 1)
787 argv[argc++] = "--verbose";
786 - argv[argc++] = url;
788 + argv[argc++] = url.buf;
789 for (i = 0; i < nr_spec; i++)
790 argv[argc++] = specs[i];
791 argv[argc++] = NULL;
@@ -813,7 +815,7 @@ static int push_git(struct discovery *heads, int nr_spec, char **specs)
815 else if (options.verbosity > 1)
816 argv_array_push(&args, "--verbose");
817 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
816 - argv_array_push(&args, url);
818 + argv_array_push(&args, url.buf);
819 for (i = 0; i < nr_spec; i++)
820 argv_array_push(&args, specs[i]);
821
@@ -894,14 +896,12 @@ int main(int argc, const char **argv)
896 remote = remote_get(argv[1]);
897
898 if (argc > 2) {
897 - end_url_with_slash(&buf, argv[2]);
899 + end_url_with_slash(&url, argv[2]);
900 } else {
899 - end_url_with_slash(&buf, remote->url[0]);
901 + end_url_with_slash(&url, remote->url[0]);
902 }
903
902 - url = strbuf_detach(&buf, NULL);
903 -
904 - http_init(remote, url, 0);
904 + http_init(remote, url.buf, 0);
905
906 do {
907 if (strbuf_getline(&buf, stdin, '\n') == EOF) {