Now that the url/pushurl fields of "struct remote" own their strings, we
can switch from bare arrays to strvecs. This has a few advantages:
- push/clear are now one-liners
- likewise the free+assigns in alias_all_urls() can use
strvec_replace()
- we now use size_t for storage, avoiding possible overflow
- this will enable some further cleanups in future patches
There's quite a bit of fallout in the code that reads these fields, as
it tends to access these arrays directly. But it's mostly a mechanical
replacement of "url_nr" with "url.nr", and "url[i]" with "url.v[i]",
with a few variations (e.g. "*url" could become "*url.v", but I used
"url.v[0]" for consistency).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committedJun 14, 2024 at 06:28 UTC8e804415fd3183f56ced0dcc168f8cb4aa790473
10 files changed+68-84
builtin/archive.c
+2-2
index f35560042e..0d9aff7e6f 100644--- a/builtin/archive.c+++ b/builtin/archive.c@@ -31,9 +31,9 @@ static int run_remote_archiver(int argc, const char **argv, struct packet_reader reader; _remote = remote_get(remote);- if (!_remote->url_nr)+ if (!_remote->url.nr) die(_("git archive: Remote with no URL"));- transport = transport_get(_remote, _remote->url[0]);+ transport = transport_get(_remote, _remote->url.v[0]); transport_connect(transport, "git-upload-archive", exec, fd); /*
builtin/clone.c
+2-2
index 730b3efae6..d3b70b49b0 100644--- a/builtin/clone.c+++ b/builtin/clone.c@@ -1290,7 +1290,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix) refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix, branch_top.buf);- path = get_repo_path(remote->url[0], &is_bundle);+ path = get_repo_path(remote->url.v[0], &is_bundle); is_local = option_local != 0 && path && !is_bundle; if (is_local) { if (option_depth)@@ -1312,7 +1312,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix) if (option_local > 0 && !is_local) warning(_("--local is ignored"));- transport = transport_get(remote, path ? path : remote->url[0]);+ transport = transport_get(remote, path ? path : remote->url.v[0]); transport_set_verbosity(transport, option_verbosity, option_progress); transport->family = family; transport->cloning = 1;
builtin/ls-remote.c
+3-3
index e8d65ebbdc..4c04dbbf19 100644--- a/builtin/ls-remote.c+++ b/builtin/ls-remote.c@@ -109,11 +109,11 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix) die("bad repository '%s'", dest); die("No remote configured to list refs from."); }- if (!remote->url_nr)+ if (!remote->url.nr) die("remote %s has no configured URL", dest); if (get_url) {- printf("%s\n", *remote->url);+ printf("%s\n", remote->url.v[0]); return 0; }@@ -130,7 +130,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix) } if (!dest && !quiet)- fprintf(stderr, "From %s\n", *remote->url);+ fprintf(stderr, "From %s\n", remote->url.v[0]); for ( ; ref; ref = ref->next) { struct ref_array_item *item; if (!check_ref_type(ref, flags))
index f7c846865f..76a3e41c73 100644--- a/remote.c+++ b/remote.c@@ -32,7 +32,7 @@ struct counted_string { static int valid_remote(const struct remote *remote) {- return (!!remote->url) || (!!remote->foreign_vcs);+ return (!!remote->url.nr) || (!!remote->foreign_vcs); } static char *alias_url(const char *url, struct rewrites *r)@@ -63,14 +63,12 @@ static char *alias_url(const char *url, struct rewrites *r) static void add_url(struct remote *remote, const char *url) {- ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);- remote->url[remote->url_nr++] = xstrdup(url);+ strvec_push(&remote->url, url); } static void add_pushurl(struct remote *remote, const char *pushurl) {- ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc);- remote->pushurl[remote->pushurl_nr++] = xstrdup(pushurl);+ strvec_push(&remote->pushurl, pushurl); } static void add_pushurl_alias(struct remote_state *remote_state,@@ -150,18 +148,12 @@ static struct remote *make_remote(struct remote_state *remote_state, static void remote_clear(struct remote *remote) {- int i;- free((char *)remote->name); free((char *)remote->foreign_vcs);- for (i = 0; i < remote->url_nr; i++)- free((char *)remote->url[i]);- FREE_AND_NULL(remote->url);+ strvec_clear(&remote->url);+ strvec_clear(&remote->pushurl);- for (i = 0; i < remote->pushurl_nr; i++)- free((char *)remote->pushurl[i]);- FREE_AND_NULL(remote->pushurl); free((char *)remote->receivepack); free((char *)remote->uploadpack); FREE_AND_NULL(remote->http_proxy);@@ -493,27 +485,25 @@ static void alias_all_urls(struct remote_state *remote_state) int add_pushurl_aliases; if (!remote_state->remotes[i]) continue;- for (j = 0; j < remote_state->remotes[i]->pushurl_nr; j++) {- char *alias = alias_url(remote_state->remotes[i]->pushurl[j],+ for (j = 0; j < remote_state->remotes[i]->pushurl.nr; j++) {+ char *alias = alias_url(remote_state->remotes[i]->pushurl.v[j], &remote_state->rewrites);- if (alias) {- free((char *)remote_state->remotes[i]->pushurl[j]);- remote_state->remotes[i]->pushurl[j] = alias;- }+ if (alias)+ strvec_replace(&remote_state->remotes[i]->pushurl,+ j, alias); }- add_pushurl_aliases = remote_state->remotes[i]->pushurl_nr == 0;- for (j = 0; j < remote_state->remotes[i]->url_nr; j++) {+ add_pushurl_aliases = remote_state->remotes[i]->pushurl.nr == 0;+ for (j = 0; j < remote_state->remotes[i]->url.nr; j++) { char *alias; if (add_pushurl_aliases) add_pushurl_alias( remote_state, remote_state->remotes[i],- remote_state->remotes[i]->url[j]);- alias = alias_url(remote_state->remotes[i]->url[j],+ remote_state->remotes[i]->url.v[j]);+ alias = alias_url(remote_state->remotes[i]->url.v[j], &remote_state->rewrites);- if (alias) {- free((char *)remote_state->remotes[i]->url[j]);- remote_state->remotes[i]->url[j] = alias;- }+ if (alias)+ strvec_replace(&remote_state->remotes[i]->url,+ j, alias); } } }@@ -653,10 +643,10 @@ static void validate_remote_url(struct remote *remote) else die(_("unrecognized value transfer.credentialsInUrl: '%s'"), value);- for (i = 0; i < remote->url_nr; i++) {+ for (i = 0; i < remote->url.nr; i++) { struct url_info url_info = { 0 };- if (!url_normalize(remote->url[i], &url_info) ||+ if (!url_normalize(remote->url.v[i], &url_info) || !url_info.passwd_off) goto loop_cleanup;@@ -830,8 +820,8 @@ struct ref *ref_remove_duplicates(struct ref *ref_map) int remote_has_url(struct remote *remote, const char *url) { int i;- for (i = 0; i < remote->url_nr; i++) {- if (!strcmp(remote->url[i], url))+ for (i = 0; i < remote->url.nr; i++) {+ if (!strcmp(remote->url.v[i], url)) return 1; } return 0;
remote.h
+3-9
index e8c6655e42..84dc91cca0 100644--- a/remote.h+++ b/remote.h@@ -4,6 +4,7 @@ #include "hash-ll.h" #include "hashmap.h" #include "refspec.h"+#include "strvec.h" struct option; struct transport_ls_refs_options;@@ -68,16 +69,9 @@ struct remote { char *foreign_vcs; /* An array of all of the url_nr URLs configured for the remote */- const char **url;-- int url_nr;- int url_alloc;-+ struct strvec url; /* An array of all of the pushurl_nr push URLs configured for the remote */- const char **pushurl;-- int pushurl_nr;- int pushurl_alloc;+ struct strvec pushurl; struct refspec push;
t/helper/test-bundle-uri.c
+1-1
index 09dc78733c..3285dd962e 100644--- a/t/helper/test-bundle-uri.c+++ b/t/helper/test-bundle-uri.c@@ -88,7 +88,7 @@ static int cmd_ls_remote(int argc, const char **argv) die(_("bad repository '%s'"), dest); die(_("no remote configured to get bundle URIs from")); }- if (!remote->url_nr)+ if (!remote->url.nr) die(_("remote '%s' has no configured URL"), dest); transport = transport_get(remote, NULL);
transport.c
+2-2
index 83ddea8fbc..eba92eb7e0 100644--- a/transport.c+++ b/transport.c@@ -1127,8 +1127,8 @@ struct transport *transport_get(struct remote *remote, const char *url) ret->remote = remote; helper = remote->foreign_vcs;- if (!url && remote->url)- url = remote->url[0];+ if (!url && remote->url.nr)+ url = remote->url.v[0]; ret->url = url; /* maybe it is a foreign URL? */