git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Jacob Keller <jacob.e.keller@intel.com>
Cc: git@vger.kernel.org,  Jacob Keller <jacob.keller@gmail.com>,
	 Lidong Yan <yldhome2d2@gmail.com>,
	 Patrick Steinhardt <ps@pks.im>
Subject: Re: [PATCH v2 4/6] submodule--helper: improve logic for fallback remote name
Date: Tue, 17 Jun 2025 16:12:48 -0700	[thread overview]
Message-ID: <xmqqv7out11r.fsf@gitster.g> (raw)
In-Reply-To: <20250617-jk-submodule-helper-use-url-v2-4-04cbb003177d@gmail.com> (Jacob Keller's message of "Tue, 17 Jun 2025 14:30:44 -0700")

Jacob Keller <jacob.e.keller@intel.com> writes:

> From: Jacob Keller <jacob.keller@gmail.com>
>
> The repo_get_default_remote() function in submodule--helper currently
> tries to figure out the proper remote name to use for a submodule based
> on a few factors.
>
> First, it tries to find the remote for the currently checked out branch.
> This works if the submodule is configured to checkout to a branch
> instead of a detached HEAD state.
>
> In the detached HEAD state, the code calls back to using "origin", on

"calls back" -> "falls back".

> the assumption that this is the default remote name. Some users may
> change this, such as by setting clone.defaultRemoteName, or by changing
> the remote name manually within the submodule repository.
>
> As a first step to improving this situation, refactor to reuse the logic
> from remotes_remote_for_branch(). This function uses the remote from the
> branch if it has one. If it doesn't then it checks to see if there is
> exactly one remote. It uses this remote first before attempting to fall
> back to "origin".

Designed wtih good taste.

> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index 9e8cdfe1b2a8c2985d9c1b8ad6f1b0d1f9401714..4aa237033a526fca29cce2926419462179d40ee3 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -41,61 +41,25 @@
>  typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
>  				  void *cb_data);
>  
> -static int repo_get_default_remote(struct repository *repo, char **default_remote)
> -{
> -	char *dest = NULL;
> -	struct strbuf sb = STRBUF_INIT;
> -	struct ref_store *store = get_main_ref_store(repo);
> -	const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL,
> -						      NULL);
> -
> -	if (!refname)
> -		return die_message(_("No such ref: %s"), "HEAD");
> -
> -	/* detached HEAD */
> -	if (!strcmp(refname, "HEAD")) {
> -		*default_remote = xstrdup("origin");
> -		return 0;
> -	}
> -
> -	if (!skip_prefix(refname, "refs/heads/", &refname))
> -		return die_message(_("Expecting a full ref name, got %s"),
> -				   refname);
> -
> -	strbuf_addf(&sb, "branch.%s.remote", refname);
> -	if (repo_config_get_string(repo, sb.buf, &dest))
> -		*default_remote = xstrdup("origin");
> -	else
> -		*default_remote = dest;
> -
> -	strbuf_release(&sb);
> -	return 0;
> -}

We will lose two callers of this function, so we can safely remove
it.

>  static int get_default_remote_submodule(const char *module_path, char **default_remote)
>  static char *get_default_remote(void)

These callers that used to call the removed helper now call
repo_default_remote() instread.  Good.


> diff --git a/remote.c b/remote.c
> index b3a9881a6eacf90bee71d6760858b37d68263502..94b31f4c23057a247a968fc0ebe2e5170e99614d 100644
> --- a/remote.c
> +++ b/remote.c
> @@ -1767,20 +1767,35 @@ static void set_merge(struct repository *repo, struct branch *ret)
>  	}
>  }
>  
> -struct branch *branch_get(const char *name)
> +static struct branch *repo_branch_get(struct repository *repo, const char *name)
>  {
>  	struct branch *ret;
>  
> -	read_config(the_repository, 0);
> +	read_config(repo, 0);
>  	if (!name || !*name || !strcmp(name, "HEAD"))
> -		ret = the_repository->remote_state->current_branch;
> +		ret = repo->remote_state->current_branch;
>  	else
> -		ret = make_branch(the_repository->remote_state, name,
> +		ret = make_branch(repo->remote_state, name,
>  				  strlen(name));
> -	set_merge(the_repository, ret);
> +	set_merge(repo, ret);
>  	return ret;
>  }
>  
> +struct branch *branch_get(const char *name)
> +{
> +	return repo_branch_get(the_repository, name);
> +}

Nice to see how the dependency to the_repository is lifted for new
callers while retaining the same interface for existing ones.

> +const char *repo_default_remote(struct repository *repo)
> +{
> +	struct branch *branch;
> +
> +	read_config(repo, 0);
> +	branch = repo_branch_get(repo, "HEAD");
> +
> +	return remotes_remote_for_branch(repo->remote_state, branch, NULL);
> +}

OK.  read_config() is a safe no-op if repo has already been
initialized, so this would give us what we want.  Nicely done.


  reply	other threads:[~2025-06-17 23:12 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-17 21:30 [PATCH v2 0/6] submodule: improve remote lookup logic Jacob Keller
2025-06-17 21:30 ` [PATCH v2 1/6] remote: fix tear down of struct branch and struct remote Jacob Keller
2025-06-17 22:25   ` Junio C Hamano
2025-06-17 23:25     ` Jacob Keller
2025-06-18  1:30       ` Junio C Hamano
2025-06-18 11:20         ` Junio C Hamano
2025-06-18 17:41           ` Jacob Keller
2025-06-18  2:44       ` Lidong Yan
2025-06-17 23:45     ` Jacob Keller
2025-06-17 21:30 ` [PATCH v2 2/6] dir: move starts_with_dot(_dot)_slash to dir.h Jacob Keller
2025-06-17 21:30 ` [PATCH v2 3/6] remote: remove the_repository from some functions Jacob Keller
2025-06-17 22:46   ` Junio C Hamano
2025-06-17 23:19     ` Jacob Keller
2025-06-17 21:30 ` [PATCH v2 4/6] submodule--helper: improve logic for fallback remote name Jacob Keller
2025-06-17 23:12   ` Junio C Hamano [this message]
2025-06-17 23:21     ` Jacob Keller
2025-06-17 21:30 ` [PATCH v2 5/6] submodule: move get_default_remote_submodule() Jacob Keller
2025-06-17 21:30 ` [PATCH v2 6/6] submodule: look up remotes by URL first Jacob Keller

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=xmqqv7out11r.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=jacob.e.keller@intel.com \
    --cc=jacob.keller@gmail.com \
    --cc=ps@pks.im \
    --cc=yldhome2d2@gmail.com \
    /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).