Skip to content

Commit 8e80441

Browse files
peffgitster
authored andcommitted
remote: use strvecs to store remote url/pushurl
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 52595c1 commit 8e80441

File tree

10 files changed

+68
-84
lines changed

10 files changed

+68
-84
lines changed

builtin/archive.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ static int run_remote_archiver(int argc, const char **argv,
3131
struct packet_reader reader;
3232

3333
_remote = remote_get(remote);
34-
if (!_remote->url_nr)
34+
if (!_remote->url.nr)
3535
die(_("git archive: Remote with no URL"));
36-
transport = transport_get(_remote, _remote->url[0]);
36+
transport = transport_get(_remote, _remote->url.v[0]);
3737
transport_connect(transport, "git-upload-archive", exec, fd);
3838

3939
/*

builtin/clone.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
12901290
refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix,
12911291
branch_top.buf);
12921292

1293-
path = get_repo_path(remote->url[0], &is_bundle);
1293+
path = get_repo_path(remote->url.v[0], &is_bundle);
12941294
is_local = option_local != 0 && path && !is_bundle;
12951295
if (is_local) {
12961296
if (option_depth)
@@ -1312,7 +1312,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
13121312
if (option_local > 0 && !is_local)
13131313
warning(_("--local is ignored"));
13141314

1315-
transport = transport_get(remote, path ? path : remote->url[0]);
1315+
transport = transport_get(remote, path ? path : remote->url.v[0]);
13161316
transport_set_verbosity(transport, option_verbosity, option_progress);
13171317
transport->family = family;
13181318
transport->cloning = 1;

builtin/ls-remote.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
109109
die("bad repository '%s'", dest);
110110
die("No remote configured to list refs from.");
111111
}
112-
if (!remote->url_nr)
112+
if (!remote->url.nr)
113113
die("remote %s has no configured URL", dest);
114114

115115
if (get_url) {
116-
printf("%s\n", *remote->url);
116+
printf("%s\n", remote->url.v[0]);
117117
return 0;
118118
}
119119

@@ -130,7 +130,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
130130
}
131131

132132
if (!dest && !quiet)
133-
fprintf(stderr, "From %s\n", *remote->url);
133+
fprintf(stderr, "From %s\n", remote->url.v[0]);
134134
for ( ; ref; ref = ref->next) {
135135
struct ref_array_item *item;
136136
if (!check_ref_type(ref, flags))

builtin/push.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ static void set_refspecs(const char **refs, int nr, const char *repo)
143143

144144
static int push_url_of_remote(struct remote *remote, const char ***url_p)
145145
{
146-
if (remote->pushurl_nr) {
147-
*url_p = remote->pushurl;
148-
return remote->pushurl_nr;
146+
if (remote->pushurl.nr) {
147+
*url_p = remote->pushurl.v;
148+
return remote->pushurl.nr;
149149
}
150-
*url_p = remote->url;
151-
return remote->url_nr;
150+
*url_p = remote->url.v;
151+
return remote->url.nr;
152152
}
153153

154154
static NORETURN void die_push_simple(struct branch *branch,

builtin/remote.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,8 @@ static int migrate_file(struct remote *remote)
619619
int i;
620620

621621
strbuf_addf(&buf, "remote.%s.url", remote->name);
622-
for (i = 0; i < remote->url_nr; i++)
623-
git_config_set_multivar(buf.buf, remote->url[i], "^$", 0);
622+
for (i = 0; i < remote->url.nr; i++)
623+
git_config_set_multivar(buf.buf, remote->url.v[i], "^$", 0);
624624
strbuf_reset(&buf);
625625
strbuf_addf(&buf, "remote.%s.push", remote->name);
626626
for (i = 0; i < remote->push.raw_nr; i++)
@@ -1002,8 +1002,8 @@ static int get_remote_ref_states(const char *name,
10021002
struct transport *transport;
10031003
const struct ref *remote_refs;
10041004

1005-
transport = transport_get(states->remote, states->remote->url_nr > 0 ?
1006-
states->remote->url[0] : NULL);
1005+
transport = transport_get(states->remote, states->remote->url.nr > 0 ?
1006+
states->remote->url.v[0] : NULL);
10071007
remote_refs = transport_get_remote_refs(transport, NULL);
10081008

10091009
states->queried = 1;
@@ -1216,12 +1216,12 @@ static int get_one_entry(struct remote *remote, void *priv)
12161216
const char **url;
12171217
int i, url_nr;
12181218

1219-
if (remote->url_nr > 0) {
1219+
if (remote->url.nr > 0) {
12201220
struct strbuf promisor_config = STRBUF_INIT;
12211221
const char *partial_clone_filter = NULL;
12221222

12231223
strbuf_addf(&promisor_config, "remote.%s.partialclonefilter", remote->name);
1224-
strbuf_addf(&remote_info_buf, "%s (fetch)", remote->url[0]);
1224+
strbuf_addf(&remote_info_buf, "%s (fetch)", remote->url.v[0]);
12251225
if (!git_config_get_string_tmp(promisor_config.buf, &partial_clone_filter))
12261226
strbuf_addf(&remote_info_buf, " [%s]", partial_clone_filter);
12271227

@@ -1230,12 +1230,12 @@ static int get_one_entry(struct remote *remote, void *priv)
12301230
strbuf_detach(&remote_info_buf, NULL);
12311231
} else
12321232
string_list_append(list, remote->name)->util = NULL;
1233-
if (remote->pushurl_nr) {
1234-
url = remote->pushurl;
1235-
url_nr = remote->pushurl_nr;
1233+
if (remote->pushurl.nr) {
1234+
url = remote->pushurl.v;
1235+
url_nr = remote->pushurl.nr;
12361236
} else {
1237-
url = remote->url;
1238-
url_nr = remote->url_nr;
1237+
url = remote->url.v;
1238+
url_nr = remote->url.nr;
12391239
}
12401240
for (i = 0; i < url_nr; i++)
12411241
{
@@ -1301,14 +1301,14 @@ static int show(int argc, const char **argv, const char *prefix)
13011301
get_remote_ref_states(*argv, &info.states, query_flag);
13021302

13031303
printf_ln(_("* remote %s"), *argv);
1304-
printf_ln(_(" Fetch URL: %s"), info.states.remote->url_nr > 0 ?
1305-
info.states.remote->url[0] : _("(no URL)"));
1306-
if (info.states.remote->pushurl_nr) {
1307-
url = info.states.remote->pushurl;
1308-
url_nr = info.states.remote->pushurl_nr;
1304+
printf_ln(_(" Fetch URL: %s"), info.states.remote->url.nr > 0 ?
1305+
info.states.remote->url.v[0] : _("(no URL)"));
1306+
if (info.states.remote->pushurl.nr) {
1307+
url = info.states.remote->pushurl.v;
1308+
url_nr = info.states.remote->pushurl.nr;
13091309
} else {
1310-
url = info.states.remote->url;
1311-
url_nr = info.states.remote->url_nr;
1310+
url = info.states.remote->url.v;
1311+
url_nr = info.states.remote->url.nr;
13121312
}
13131313
for (i = 0; i < url_nr; i++)
13141314
/*
@@ -1454,8 +1454,8 @@ static int prune_remote(const char *remote, int dry_run)
14541454

14551455
printf_ln(_("Pruning %s"), remote);
14561456
printf_ln(_("URL: %s"),
1457-
states.remote->url_nr
1458-
? states.remote->url[0]
1457+
states.remote->url.nr
1458+
? states.remote->url.v[0]
14591459
: _("(no URL)"));
14601460

14611461
for_each_string_list_item(item, &states.stale)
@@ -1647,15 +1647,15 @@ static int get_url(int argc, const char **argv, const char *prefix)
16471647

16481648
url_nr = 0;
16491649
if (push_mode) {
1650-
url = remote->pushurl;
1651-
url_nr = remote->pushurl_nr;
1650+
url = remote->pushurl.v;
1651+
url_nr = remote->pushurl.nr;
16521652
}
16531653
/* else fetch mode */
16541654

16551655
/* Use the fetch URL when no push URLs were found or requested. */
16561656
if (!url_nr) {
1657-
url = remote->url;
1658-
url_nr = remote->url_nr;
1657+
url = remote->url.v;
1658+
url_nr = remote->url.nr;
16591659
}
16601660

16611661
if (!url_nr)
@@ -1718,12 +1718,12 @@ static int set_url(int argc, const char **argv, const char *prefix)
17181718

17191719
if (push_mode) {
17201720
strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1721-
urlset = remote->pushurl;
1722-
urlset_nr = remote->pushurl_nr;
1721+
urlset = remote->pushurl.v;
1722+
urlset_nr = remote->pushurl.nr;
17231723
} else {
17241724
strbuf_addf(&name_buf, "remote.%s.url", remotename);
1725-
urlset = remote->url;
1726-
urlset_nr = remote->url_nr;
1725+
urlset = remote->url.v;
1726+
urlset_nr = remote->url.nr;
17271727
}
17281728

17291729
/* Special cases that add new entry. */

remote-curl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,7 @@ int cmd_main(int argc, const char **argv)
15741574
if (argc > 2) {
15751575
end_url_with_slash(&url, argv[2]);
15761576
} else {
1577-
end_url_with_slash(&url, remote->url[0]);
1577+
end_url_with_slash(&url, remote->url.v[0]);
15781578
}
15791579

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

remote.c

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct counted_string {
3232

3333
static int valid_remote(const struct remote *remote)
3434
{
35-
return (!!remote->url) || (!!remote->foreign_vcs);
35+
return (!!remote->url.nr) || (!!remote->foreign_vcs);
3636
}
3737

3838
static char *alias_url(const char *url, struct rewrites *r)
@@ -63,14 +63,12 @@ static char *alias_url(const char *url, struct rewrites *r)
6363

6464
static void add_url(struct remote *remote, const char *url)
6565
{
66-
ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
67-
remote->url[remote->url_nr++] = xstrdup(url);
66+
strvec_push(&remote->url, url);
6867
}
6968

7069
static void add_pushurl(struct remote *remote, const char *pushurl)
7170
{
72-
ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc);
73-
remote->pushurl[remote->pushurl_nr++] = xstrdup(pushurl);
71+
strvec_push(&remote->pushurl, pushurl);
7472
}
7573

7674
static void add_pushurl_alias(struct remote_state *remote_state,
@@ -150,18 +148,12 @@ static struct remote *make_remote(struct remote_state *remote_state,
150148

151149
static void remote_clear(struct remote *remote)
152150
{
153-
int i;
154-
155151
free((char *)remote->name);
156152
free((char *)remote->foreign_vcs);
157153

158-
for (i = 0; i < remote->url_nr; i++)
159-
free((char *)remote->url[i]);
160-
FREE_AND_NULL(remote->url);
154+
strvec_clear(&remote->url);
155+
strvec_clear(&remote->pushurl);
161156

162-
for (i = 0; i < remote->pushurl_nr; i++)
163-
free((char *)remote->pushurl[i]);
164-
FREE_AND_NULL(remote->pushurl);
165157
free((char *)remote->receivepack);
166158
free((char *)remote->uploadpack);
167159
FREE_AND_NULL(remote->http_proxy);
@@ -493,27 +485,25 @@ static void alias_all_urls(struct remote_state *remote_state)
493485
int add_pushurl_aliases;
494486
if (!remote_state->remotes[i])
495487
continue;
496-
for (j = 0; j < remote_state->remotes[i]->pushurl_nr; j++) {
497-
char *alias = alias_url(remote_state->remotes[i]->pushurl[j],
488+
for (j = 0; j < remote_state->remotes[i]->pushurl.nr; j++) {
489+
char *alias = alias_url(remote_state->remotes[i]->pushurl.v[j],
498490
&remote_state->rewrites);
499-
if (alias) {
500-
free((char *)remote_state->remotes[i]->pushurl[j]);
501-
remote_state->remotes[i]->pushurl[j] = alias;
502-
}
491+
if (alias)
492+
strvec_replace(&remote_state->remotes[i]->pushurl,
493+
j, alias);
503494
}
504-
add_pushurl_aliases = remote_state->remotes[i]->pushurl_nr == 0;
505-
for (j = 0; j < remote_state->remotes[i]->url_nr; j++) {
495+
add_pushurl_aliases = remote_state->remotes[i]->pushurl.nr == 0;
496+
for (j = 0; j < remote_state->remotes[i]->url.nr; j++) {
506497
char *alias;
507498
if (add_pushurl_aliases)
508499
add_pushurl_alias(
509500
remote_state, remote_state->remotes[i],
510-
remote_state->remotes[i]->url[j]);
511-
alias = alias_url(remote_state->remotes[i]->url[j],
501+
remote_state->remotes[i]->url.v[j]);
502+
alias = alias_url(remote_state->remotes[i]->url.v[j],
512503
&remote_state->rewrites);
513-
if (alias) {
514-
free((char *)remote_state->remotes[i]->url[j]);
515-
remote_state->remotes[i]->url[j] = alias;
516-
}
504+
if (alias)
505+
strvec_replace(&remote_state->remotes[i]->url,
506+
j, alias);
517507
}
518508
}
519509
}
@@ -653,10 +643,10 @@ static void validate_remote_url(struct remote *remote)
653643
else
654644
die(_("unrecognized value transfer.credentialsInUrl: '%s'"), value);
655645

656-
for (i = 0; i < remote->url_nr; i++) {
646+
for (i = 0; i < remote->url.nr; i++) {
657647
struct url_info url_info = { 0 };
658648

659-
if (!url_normalize(remote->url[i], &url_info) ||
649+
if (!url_normalize(remote->url.v[i], &url_info) ||
660650
!url_info.passwd_off)
661651
goto loop_cleanup;
662652

@@ -830,8 +820,8 @@ struct ref *ref_remove_duplicates(struct ref *ref_map)
830820
int remote_has_url(struct remote *remote, const char *url)
831821
{
832822
int i;
833-
for (i = 0; i < remote->url_nr; i++) {
834-
if (!strcmp(remote->url[i], url))
823+
for (i = 0; i < remote->url.nr; i++) {
824+
if (!strcmp(remote->url.v[i], url))
835825
return 1;
836826
}
837827
return 0;

remote.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "hash-ll.h"
55
#include "hashmap.h"
66
#include "refspec.h"
7+
#include "strvec.h"
78

89
struct option;
910
struct transport_ls_refs_options;
@@ -68,16 +69,9 @@ struct remote {
6869
char *foreign_vcs;
6970

7071
/* An array of all of the url_nr URLs configured for the remote */
71-
const char **url;
72-
73-
int url_nr;
74-
int url_alloc;
75-
72+
struct strvec url;
7673
/* An array of all of the pushurl_nr push URLs configured for the remote */
77-
const char **pushurl;
78-
79-
int pushurl_nr;
80-
int pushurl_alloc;
74+
struct strvec pushurl;
8175

8276
struct refspec push;
8377

t/helper/test-bundle-uri.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static int cmd_ls_remote(int argc, const char **argv)
8888
die(_("bad repository '%s'"), dest);
8989
die(_("no remote configured to get bundle URIs from"));
9090
}
91-
if (!remote->url_nr)
91+
if (!remote->url.nr)
9292
die(_("remote '%s' has no configured URL"), dest);
9393

9494
transport = transport_get(remote, NULL);

transport.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,8 +1127,8 @@ struct transport *transport_get(struct remote *remote, const char *url)
11271127
ret->remote = remote;
11281128
helper = remote->foreign_vcs;
11291129

1130-
if (!url && remote->url)
1131-
url = remote->url[0];
1130+
if (!url && remote->url.nr)
1131+
url = remote->url.v[0];
11321132
ret->url = url;
11331133

11341134
/* maybe it is a foreign URL? */

0 commit comments

Comments
 (0)