Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org,  Ramsay Jones <ramsay@ramsayjones.plus.com>,
	 "D. Ben Knoble" <ben.knoble@gmail.com>,
	 Kristoffer Haugsbakk <kristofferhaugsbakk@fastmail.com>,
	 Marc Branchaud <marcnarc@gmail.com>,
	Phillip Wood <phillip.wood123@gmail.com>,
	 Harald Nordgren <haraldnordgren@gmail.com>
Subject: Re: [PATCH v8] checkout: extend --track with a "fetch" mode to refresh start-point
Date: Tue, 12 May 2026 09:32:09 +0900	[thread overview]
Message-ID: <xmqqh5odqxh2.fsf@gitster.g> (raw)
In-Reply-To: <pull.2281.v8.git.git.1778507225500.gitgitgadget@gmail.com> (Harald Nordgren via GitGitGadget's message of "Mon, 11 May 2026 13:47:05 +0000")

"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:

> diff --git a/Documentation/git-checkout.adoc b/Documentation/git-checkout.adoc
> index 43ccf47cf6..28f17f427e 100644
> --- a/Documentation/git-checkout.adoc
> +++ b/Documentation/git-checkout.adoc
> @@ -158,11 +158,22 @@ of it").
>  	resets _<branch>_ to the start point instead of failing.
>  
>  `-t`::
> -`--track[=(direct|inherit)]`::
> +`--track[=(direct|inherit|fetch)[,...]]`::
>  	When creating a new branch, set up "upstream" configuration. See
>  	`--track` in linkgit:git-branch[1] for details. As a convenience,
>  	--track without -b implies branch creation.
>  +
> +The argument is a comma-separated list. `direct` (the default) and
> +`inherit` select the tracking mode and are mutually exclusive. Adding
> +`fetch` requests that the remote be fetched before _<start-point>_ is
> +resolved, so the new branch starts from a fresh tip: when
> +_<start-point>_ is in _<remote>/<branch>_ form, only that branch is
> +updated; when _<start-point>_ is a bare remote name (e.g. `origin`),
> +only the remote's default branch is updated.

The latter is because "checkout -t -b new remote/origin" makes "new"
track "remote/origin/HEAD", which makes sense.  And of course this
fetch can fail.

> +static int resolve_fetch_target(const char *arg, char **remote_out,
> +				char **src_ref_out)
> +{
> +	const char *slash;
> +	char *remote_name;
> +	struct remote *remote;
> +	struct refspec_item query = { 0 };
> +	struct strbuf dst = STRBUF_INIT;
> +	const char *rest;
> +
> +	*remote_out = NULL;
> +	*src_ref_out = NULL;
> +
> +	if (!arg || !*arg)
> +		return -1;
> +
> +	slash = strchr(arg, '/');
> +	if (slash == arg)
> +		return -1;
> +	remote_name = slash ? xstrndup(arg, slash - arg) : xstrdup(arg);
> +
> +	remote = remote_get(remote_name);
> +	if (!remote || !remote_is_configured(remote, 1)) {
> +		free(remote_name);
> +		return -1;
> +	}
> +
> +	rest = (slash && slash[1]) ? slash + 1 : NULL;

There is no slash when asking for "origin", and the control goes
into the "if (!rest)" block.

> +	if (!rest) {
> +		struct object_id oid;
> +		const char *head_target;
> +		const char *short_target;
> +
> +		strbuf_addf(&dst, "refs/remotes/%s/HEAD", remote_name);
> +		head_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
> +						      dst.buf,
> +						      RESOLVE_REF_READING |
> +						      RESOLVE_REF_NO_RECURSE,
> +						      &oid, NULL);
> +		strbuf_reset(&dst);
> +		if (head_target &&
> +		    skip_prefix(head_target, "refs/remotes/", &short_target) &&
> +		    skip_prefix(short_target, remote_name, &short_target) &&
> +		    *short_target == '/')
> +			rest = short_target + 1;
> +	}

If refs/remotes/origin/HEAD points at refs/remotes/origin/main, rest
gets "main".  Otherwise (i.e. unexpected contents in HEAD or lacking
HEAD), rest remains NULL.  And then we have the "if (rest)" block.

> +	if (rest) {
> +		strbuf_addf(&dst, "refs/remotes/%s/%s", remote_name, rest);
> +		query.dst = dst.buf;
> +		if (!remote_find_tracking(remote, &query) && query.src) {
> +			*src_ref_out = xstrdup(query.src);
> +			free(query.src);
> +		} else {
> +			*src_ref_out = xstrdup(rest);
> +		}
> +	}
> +
> +	strbuf_release(&dst);
> +	*remote_out = remote_name;
> +	return 0;
> +}

It is not a new problem but I do not remember how we explicitly
forbid "hierarchical" remote names.  refs/remotes/a/b/c might be
their branch "b/c" at remote we call "a", or branch "c" at remote
"a/b".  Unless we forbid slashes in remote names, the "there is no
slash so we got only the name of the remote to mean its HEAD" logic
would not work well, so we may want to double check.

> +static void fetch_remote_for_start_point(const char *arg)
> +{
> +	char *remote_name = NULL;
> +	char *src_ref = NULL;
> +	struct child_process cmd = CHILD_PROCESS_INIT;
> +	struct strbuf dst_ref = STRBUF_INIT;
> +	int have_existing_ref = 0;
> +
> +	if (resolve_fetch_target(arg, &remote_name, &src_ref))
> +		return;
> +
> +	{
> +		struct object_id oid;
> +
> +		if (strchr(arg, '/'))
> +			strbuf_addf(&dst_ref, "refs/remotes/%s", arg);
> +		else
> +			strbuf_addf(&dst_ref, "refs/remotes/%s/HEAD", arg);
> +		if (!refs_read_ref(get_main_ref_store(the_repository),
> +				   dst_ref.buf, &oid))
> +			have_existing_ref = 1;
> +	}

I do not quite see the point of this extra block.  Can we do without
it (and move the def of oid up near the beginning of the function,
of course)?

Even better, as resolve_fetch_target() already looks at "arg" and
poked at the remote-tracking ref hierarchy, wouldn't it make more
sense to make that helper function responsible for finding out if
there already is a usable, albeit potentially stale, ref?  


> +	strvec_pushl(&cmd.args, "fetch", remote_name, NULL);
> +	if (src_ref)
> +		strvec_push(&cmd.args, src_ref);
> +	cmd.git_cmd = 1;
> +	if (run_command(&cmd)) {
> +		if (have_existing_ref)
> +			warning(_("failed to fetch start-point '%s'; "
> +				  "using existing '%s'"),
> +				arg, dst_ref.buf);
> +		else
> +			die(_("failed to fetch start-point '%s'"), arg);
> +	}
> +
> +	free(remote_name);
> +	free(src_ref);
> +	strbuf_release(&dst_ref);
> +}

> @@ -1244,7 +1398,6 @@ static int git_checkout_config(const char *var, const char *value,
>  		opts->dwim_new_local_branch = git_config_bool(var, value);
>  		return 0;
>  	}
> -
>  	if (starts_with(var, "submodule."))
>  		return git_default_submodule_config(var, value, NULL);

Unrelated patch noise?


  reply	other threads:[~2026-05-12  0:32 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-24 10:03 [PATCH] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren via GitGitGadget
2026-04-24 13:48 ` Ramsay Jones
2026-04-24 17:12 ` D. Ben Knoble
2026-04-25 17:24   ` Comments on Phillip's review Harald Nordgren
2026-04-25 17:44     ` Wrong subject line Harald Nordgren
2026-04-24 17:38 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Kristoffer Haugsbakk
2026-04-25 17:41   ` Comments on Phillip's review Harald Nordgren
2026-04-25 17:44     ` Wrong subject line Harald Nordgren
2026-04-26  7:07       ` Kristoffer Haugsbakk
2026-04-26 15:15         ` [PATCH] remote: add --set-head option to 'git remote add' Harald Nordgren
2026-04-24 17:42 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Marc Branchaud
2026-04-25 17:48   ` Wrong subject line Harald Nordgren
2026-04-24 22:21 ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Junio C Hamano
2026-04-25  2:54   ` Junio C Hamano
2026-04-25 17:58     ` Multiple remotes Harald Nordgren
2026-04-25 21:57       ` Ben Knoble
2026-04-25 22:54         ` gh Harald Nordgren
2026-04-25 18:12 ` [PATCH v2] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren via GitGitGadget
2026-04-26  7:24   ` [PATCH v3] " Harald Nordgren via GitGitGadget
2026-04-26 15:54     ` Ramsay Jones
2026-04-26 18:32     ` [PATCH v4] " Harald Nordgren via GitGitGadget
2026-04-28  1:47       ` Junio C Hamano
2026-04-28  8:44         ` [PATCH] " Harald Nordgren
2026-04-28  9:03       ` [PATCH v5] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget
2026-05-03 20:59         ` Junio C Hamano
2026-05-03 22:32           ` [PATCH] checkout: add --autostash option for branch switching Harald Nordgren
2026-05-03 22:31         ` [PATCH v6] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget
2026-05-07 20:12           ` Harald Nordgren
2026-05-08 13:15             ` Phillip Wood
2026-05-08 22:40               ` [PATCH] checkout: add --fetch to fetch remote before resolving start-point Harald Nordgren
2026-05-08 22:52           ` [PATCH v7] checkout: extend --track with a "fetch" mode to refresh start-point Harald Nordgren via GitGitGadget
2026-05-11 13:16             ` Phillip Wood
2026-05-11 13:47             ` [PATCH v8] " Harald Nordgren via GitGitGadget
2026-05-12  0:32               ` Junio C Hamano [this message]
2026-05-12 10:55               ` [PATCH v9] " Harald Nordgren via GitGitGadget

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=xmqqh5odqxh2.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=ben.knoble@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=haraldnordgren@gmail.com \
    --cc=kristofferhaugsbakk@fastmail.com \
    --cc=marcnarc@gmail.com \
    --cc=phillip.wood123@gmail.com \
    --cc=ramsay@ramsayjones.plus.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