Merge branch 'jk/remote-wo-url'

Memory ownership rules for the in-core representation of
remote.*.url configuration values have been straightened out, which
resulted in a few leak fixes and code clarification.

* jk/remote-wo-url:
  remote: drop checks for zero-url case
  remote: always require at least one url in a remote
  t5801: test remote.*.vcs config
  t5801: make remote-testgit GIT_DIR setup more robust
  remote: allow resetting url list
  config: document remote.*.url/pushurl interaction
  remote: simplify url/pushurl selection
  remote: use strvecs to store remote url/pushurl
  remote: transfer ownership of memory in add_url(), etc
  remote: refactor alias_url() memory ownership
  archive: fix check for missing url
This commit is contained in:
Junio C Hamano
2024-07-02 09:59:01 -07:00
15 changed files with 174 additions and 162 deletions

View File

@ -34,10 +34,10 @@ struct counted_string {
static int valid_remote(const struct remote *remote)
{
return (!!remote->url) || (!!remote->foreign_vcs);
return !!remote->url.nr;
}
static const char *alias_url(const char *url, struct rewrites *r)
static char *alias_url(const char *url, struct rewrites *r)
{
int i, j;
struct counted_string *longest;
@ -58,36 +58,43 @@ static const char *alias_url(const char *url, struct rewrites *r)
}
}
if (!longest)
return url;
return NULL;
return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len);
}
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++] = url;
if (*url)
strvec_push(&remote->url, url);
else
strvec_clear(&remote->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++] = pushurl;
if (*pushurl)
strvec_push(&remote->pushurl, pushurl);
else
strvec_clear(&remote->pushurl);
}
static void add_pushurl_alias(struct remote_state *remote_state,
struct remote *remote, const char *url)
{
const char *pushurl = alias_url(url, &remote_state->rewrites_push);
if (pushurl != url)
add_pushurl(remote, pushurl);
char *alias = alias_url(url, &remote_state->rewrites_push);
if (alias)
add_pushurl(remote, alias);
free(alias);
}
static void add_url_alias(struct remote_state *remote_state,
struct remote *remote, const char *url)
{
add_url(remote, alias_url(url, &remote_state->rewrites));
char *alias = alias_url(url, &remote_state->rewrites);
add_url(remote, alias ? alias : url);
add_pushurl_alias(remote_state, remote, url);
free(alias);
}
struct remotes_hash_key {
@ -149,18 +156,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);
@ -294,7 +295,7 @@ static void read_remotes_file(struct remote_state *remote_state,
if (skip_prefix(buf.buf, "URL:", &v))
add_url_alias(remote_state, remote,
xstrdup(skip_spaces(v)));
skip_spaces(v));
else if (skip_prefix(buf.buf, "Push:", &v))
refspec_append(&remote->push, skip_spaces(v));
else if (skip_prefix(buf.buf, "Pull:", &v))
@ -337,7 +338,7 @@ static void read_branches_file(struct remote_state *remote_state,
else
frag = to_free = repo_default_branch_name(the_repository, 0);
add_url_alias(remote_state, remote, strbuf_detach(&buf, NULL));
add_url_alias(remote_state, remote, buf.buf);
refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
frag, remote->name);
@ -348,6 +349,7 @@ static void read_branches_file(struct remote_state *remote_state,
refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag);
remote->fetch_tags = 1; /* always auto-follow */
strbuf_release(&buf);
free(to_free);
}
@ -432,15 +434,13 @@ static int handle_config(const char *key, const char *value,
else if (!strcmp(subkey, "prunetags"))
remote->prune_tags = git_config_bool(key, value);
else if (!strcmp(subkey, "url")) {
char *v;
if (git_config_string(&v, key, value))
return -1;
add_url(remote, v);
if (!value)
return config_error_nonbool(key);
add_url(remote, value);
} else if (!strcmp(subkey, "pushurl")) {
char *v;
if (git_config_string(&v, key, value))
return -1;
add_pushurl(remote, v);
if (!value)
return config_error_nonbool(key);
add_pushurl(remote, value);
} else if (!strcmp(subkey, "push")) {
char *v;
if (git_config_string(&v, key, value))
@ -493,20 +493,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++) {
remote_state->remotes[i]->pushurl[j] =
alias_url(remote_state->remotes[i]->pushurl[j],
&remote_state->rewrites);
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)
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]);
remote_state->remotes[i]->url[j] =
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)
strvec_replace(&remote_state->remotes[i]->url,
j, alias);
}
}
}
@ -646,10 +651,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;
@ -823,13 +828,18 @@ 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;
}
struct strvec *push_url_of_remote(struct remote *remote)
{
return remote->pushurl.nr ? &remote->pushurl : &remote->url;
}
static int match_name_with_pattern(const char *key, const char *name,
const char *value, char **result)
{