git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Jeff King <peff@peff.net>
Cc: Mathew George <mathewegeorge@gmail.com>,  git@vger.kernel.org
Subject: Re: [PATCH 02/11] remote: refactor alias_url() memory ownership
Date: Fri, 14 Jun 2024 10:05:01 -0700	[thread overview]
Message-ID: <xmqqtthvfoaq.fsf@gitster.g> (raw)
In-Reply-To: <20240614102616.GB222445@coredump.intra.peff.net> (Jeff King's message of "Fri, 14 Jun 2024 06:26:16 -0400")

Jeff King <peff@peff.net> writes:

> ... So instead of
> returning the original string, return NULL, forcing callers to decide
> what to do with it explicitly. We can then build further cleanups on top
> of that.

OK.

> @@ -76,15 +76,16 @@ static void add_pushurl(struct remote *remote, const char *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);

OK, that's an obviously equivalent rewrite.

>  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);
>  }

This is also an obviously equivalent rewrite.

Looking at how remote_clear() deals with the .url[] and .pushurl[]
elements, add_url() makes the remote structure take ownership, which
is perfectly fine when we got a non-NULL alias back (i.e. it is a
new piece of string allocated just for us).  Depending on who owns
the incoming url parameter, we might need strdup(url) here, but
since we haven't heard crashes due to freeing remote->url[] elements
that should not be freed, presumably url is a piece memory the
caller is giving us the ownership?  In any case, I imagine that
untangling that ownership mess is left to the later steps of the
series.

> @@ -492,19 +493,22 @@ static void alias_all_urls(struct remote_state *remote_state)
>  		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);
> +			char *alias = alias_url(remote_state->remotes[i]->pushurl[j],
> +						&remote_state->rewrites);
> +			if (alias)
> +				remote_state->remotes[i]->pushurl[j] = alias;

Does this change behaviour?  No.  We used to (1) grab the current
value, (2) replace it if it is an alias, or (3) otherwise replace it
with itself.  We just do not do the obvious noop anymore.

The story is the same with the last remaing hunk of this patch.
Looking good.

Thanks.

>  		}
>  		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],
> +			alias = alias_url(remote_state->remotes[i]->url[j],
>  					  &remote_state->rewrites);
> +			if (alias)
> +				remote_state->remotes[i]->url[j] = alias;
>  		}
>  	}
>  }

  reply	other threads:[~2024-06-14 17:05 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-09  6:51 Cannot override `remote.origin.url` with `-c` option Mathew George
2024-06-11  7:51 ` Jeff King
2024-06-11 15:28   ` Junio C Hamano
2024-06-13 10:24     ` Jeff King
2024-06-14 10:24       ` [PATCH 0/11] allow overriding remote.*.url Jeff King
2024-06-14 10:25         ` [PATCH 01/11] archive: fix check for missing url Jeff King
2024-06-14 10:26         ` [PATCH 02/11] remote: refactor alias_url() memory ownership Jeff King
2024-06-14 17:05           ` Junio C Hamano [this message]
2024-06-14 10:27         ` [PATCH 03/11] remote: transfer ownership of memory in add_url(), etc Jeff King
2024-06-14 17:04           ` Junio C Hamano
2024-06-16  4:59             ` Jeff King
2024-06-17 17:42               ` Junio C Hamano
2024-06-25 17:30           ` Elijah Newren
2024-06-14 10:28         ` [PATCH 04/11] remote: use strvecs to store remote url/pushurl Jeff King
2024-06-25 17:32           ` Elijah Newren
2024-06-14 10:29         ` [PATCH 05/11] remote: simplify url/pushurl selection Jeff King
2024-06-25 17:33           ` Elijah Newren
2024-06-14 10:30         ` [PATCH 06/11] config: document remote.*.url/pushurl interaction Jeff King
2024-06-25 17:34           ` Elijah Newren
2024-06-14 10:31         ` [PATCH 07/11] remote: allow resetting url list Jeff King
2024-06-25 17:35           ` Elijah Newren
2024-06-14 10:31         ` [PATCH 08/11] t5801: make remote-testgit GIT_DIR setup more robust Jeff King
2024-06-25 17:36           ` Elijah Newren
2024-06-14 10:34         ` [PATCH 09/11] t5801: test remote.*.vcs config Jeff King
2024-06-14 10:37         ` [PATCH 10/11] remote: always require at least one url in a remote Jeff King
2024-06-14 10:42         ` [PATCH 11/11] remote: drop checks for zero-url case Jeff King
2024-06-25 17:37           ` Elijah Newren
2024-06-25 17:44         ` [PATCH 0/11] allow overriding remote.*.url Elijah Newren
2024-06-26 20:40           ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqtthvfoaq.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=mathewegeorge@gmail.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).