git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/1] config: support a default remote tracking setup upon branch creation

From
Philippe Blain <levraiphilippeblain@gmail.com>
Date
Jul 30, 2021, 13:35 UTC
Message-ID
<bcbe13a8-7f22-7564-77cd-674c7b2acbf6@gmail.com>
In-Reply-To
<20210729020125.746206-2-mathstuf@gmail.com>
Le 2021-07-28 à 22:01, Ben Boeckel a écrit :
Show 28 quoted lines
> The `branch.autoSetupMerge` works well today for setting up tracking a
> local branch, but there is no easy mechanism to automatically set up a
> remote tracking situation for workflows which use a single set of
> branches for integration without specifying `--track` to every branch
> creation command or branching directly from the remote ref. This patch
> adds the following configuration values:
> 
>    - `branch.defaultRemote`: initializes `branch.<name>.remote` if not
>      otherwise given;
>    - `branch.defaultMerge`: initializes `branch.<name>.merge` if not
>      otherwise given.
> 
> These effectively make branch creation commands such as `git branch`,
> `git switch`, and `git checkout -b` have an implicit `-t
> ${branch.defaultRemote}/${branch.defaultMerge}` argument and is
> equivalent to doing:
> 
>      $ git branch new-topic      # or another branch creation command
>      $ git branch --set-upstream-to=${branch.defaultRemote}/${branch.defaultMerge} new-topic
> 
> In a fork-based workflow where contributions to the main project flow in
> through forks of the main repository rather than pushing directly to it
> (not uncommon of forge-hosted projects), the following setup may be
> used:
> 
>      $ git config remote.pushDefault myfork    # push to `myfork` by default
>      $ git config push.default simple          # the default
>      $ git config branch.autoSetupMerge always # always setup tracking

OK, so if I understand correctly this exisiting setting has to be changed to 'always' for the new settings you are adding to take effect, right ? I think this does make sense, reading the description of 'branch.autoSetupMerge', but maybe it should be spelled explicitely in the text of the commit message, and not just mentioned here in this terminal session excerpt.

>      $ git config branch.defaultRemote origin  # track from `origin`
>      $ git config branch.defaultMerge main     # track the `main` branch
Small nit: maybe I would invert these two, so it can read:
       $ git config branch.defaultMerge main     # track the `main` branch ...
       $ git config branch.defaultRemote origin  # ... from `origin`
Show 13 quoted lines
> 
> With this setup, branches will automatically be set up to track
> `origin/main` while being pushed to `myfork` by default. Some tools
> (at least Vim's fugitive plugin) will show the commit differences
> between both the tracking branch and the default push target. This
> allows such tools to easily show "what needs to be merged?", "what has
> happened on the target branch since this topic has been created?", and
> "what is my status against my fork's current status?" at a glance.
> 
> Additionally, with the `extensions.worktreeConfig` setting, when a
> separate work tree which is used for changes against a different branch
> (e.g., a branch tracking a prior release), the `branch.defaultMerge`
> setting may be changed independently, e.g., to `maint`.

This last paragraph is not explicitely needed, as nothing relating to 'extensions.worktreeConfig' is changed here right ? It's just the normal way that this setting works.

Show 29 quoted lines
> 
> Signed-off-by: Ben Boeckel <mathstuf@gmail.com>
> ---
>   Documentation/config/branch.txt | 14 ++++++++++++
>   branch.c                        | 22 ++++++++++---------
>   branch.h                        |  2 ++
>   config.c                        | 10 +++++++++
>   environment.c                   |  2 ++
>   t/t3200-branch.sh               | 39 +++++++++++++++++++++++++++++++++
>   6 files changed, 79 insertions(+), 10 deletions(-)
> 
> diff --git a/Documentation/config/branch.txt b/Documentation/config/branch.txt
> index cc5f3249fc..ab5cd2c1ed 100644
> --- a/Documentation/config/branch.txt
> +++ b/Documentation/config/branch.txt
> @@ -1,3 +1,17 @@
> +branch.defaultRemote::
> +	Tells 'git branch', 'git switch' and 'git checkout' to set up new branches
> +	so that it will track a branch on the specified remote. This can be
> +	used, in conjunction with `branch.defaultMerge`, in projects where
> +	branches tend to target a single branch. This will be used to
> +	initialize the "branch.<name>.remote" setting.
> +
> +branch.defaultMerge::
> +	Tells 'git branch', 'git switch' and 'git checkout' to set up new branches
> +	so that it will track a branch with this name. This can be used, in
> +	conjunction with `branch.defaultRemote` in projects where branches tend
> +	to target a single branch. This will be used to initialize the
> +	"branch.<name>.merge" setting.

For the two setting above, if 'branch.autoSetupMerge' must be set to 'always' for the settings to work, I think it should be explicitely mentioned.

Show 122 quoted lines
> +
>   branch.autoSetupMerge::
>   	Tells 'git branch', 'git switch' and 'git checkout' to set up new branches
>   	so that linkgit:git-pull[1] will appropriately merge from the
> diff --git a/branch.c b/branch.c
> index 7a88a4861e..097d5af647 100644
> --- a/branch.c
> +++ b/branch.c
> @@ -60,6 +60,8 @@ int install_branch_config(int flag, const char *local, const char *origin, const
>   	const char *shortname = NULL;
>   	struct strbuf key = STRBUF_INIT;
>   	int rebasing = should_setup_rebase(origin);
> +	const char *actual_origin = origin ? origin : git_branch_remote;
> +	const char *actual_remote = remote ? remote : git_branch_merge;
>   
>   	if (skip_prefix(remote, "refs/heads/", &shortname)
>   	    && !strcmp(local, shortname)
> @@ -70,12 +72,12 @@ int install_branch_config(int flag, const char *local, const char *origin, const
>   	}
>   
>   	strbuf_addf(&key, "branch.%s.remote", local);
> -	if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
> +	if (git_config_set_gently(key.buf, actual_origin ? actual_origin : ".") < 0)
>   		goto out_err;
>   
>   	strbuf_reset(&key);
>   	strbuf_addf(&key, "branch.%s.merge", local);
> -	if (git_config_set_gently(key.buf, remote) < 0)
> +	if (git_config_set_gently(key.buf, actual_remote) < 0)
>   		goto out_err;
>   
>   	if (rebasing) {
> @@ -88,27 +90,27 @@ int install_branch_config(int flag, const char *local, const char *origin, const
>   
>   	if (flag & BRANCH_CONFIG_VERBOSE) {
>   		if (shortname) {
> -			if (origin)
> +			if (actual_origin)
>   				printf_ln(rebasing ?
>   					  _("Branch '%s' set up to track remote branch '%s' from '%s' by rebasing.") :
>   					  _("Branch '%s' set up to track remote branch '%s' from '%s'."),
> -					  local, shortname, origin);
> +					  local, shortname, actual_origin);
>   			else
>   				printf_ln(rebasing ?
>   					  _("Branch '%s' set up to track local branch '%s' by rebasing.") :
>   					  _("Branch '%s' set up to track local branch '%s'."),
>   					  local, shortname);
>   		} else {
> -			if (origin)
> +			if (actual_origin)
>   				printf_ln(rebasing ?
>   					  _("Branch '%s' set up to track remote ref '%s' by rebasing.") :
>   					  _("Branch '%s' set up to track remote ref '%s'."),
> -					  local, remote);
> +					  local, actual_remote);
>   			else
>   				printf_ln(rebasing ?
>   					  _("Branch '%s' set up to track local ref '%s' by rebasing.") :
>   					  _("Branch '%s' set up to track local ref '%s'."),
> -					  local, remote);
> +					  local, actual_remote);
>   		}
>   	}
>   
> @@ -119,9 +121,9 @@ int install_branch_config(int flag, const char *local, const char *origin, const
>   	error(_("Unable to write upstream branch configuration"));
>   
>   	advise(_(tracking_advice),
> -	       origin ? origin : "",
> -	       origin ? "/" : "",
> -	       shortname ? shortname : remote);
> +	       actual_origin ? actual_origin : "",
> +	       actual_origin ? "/" : "",
> +	       shortname ? shortname : actual_remote);
>   
>   	return -1;
>   }
> diff --git a/branch.h b/branch.h
> index df0be61506..7d7990dda7 100644
> --- a/branch.h
> +++ b/branch.h
> @@ -14,6 +14,8 @@ enum branch_track {
>   };
>   
>   extern enum branch_track git_branch_track;
> +extern const char* git_branch_remote;
> +extern const char* git_branch_merge;
>   
>   /* Functions for acting on the information about branches. */
>   
> diff --git a/config.c b/config.c
> index f33abeab85..a46c5a43a1 100644
> --- a/config.c
> +++ b/config.c
> @@ -1599,6 +1599,16 @@ static int git_default_branch_config(const char *var, const char *value)
>   			return error(_("malformed value for %s"), var);
>   		return 0;
>   	}
> +	if (!strcmp(var, "branch.defaultremote")) {
> +		if (!value)
> +			return config_error_nonbool(var);
> +		return git_config_string(&git_branch_remote, var, value);
> +	}
> +	if (!strcmp(var, "branch.defaultmerge")) {
> +		if (!value)
> +			return config_error_nonbool(var);
> +		return git_config_string(&git_branch_merge, var, value);
> +	}
>   
>   	/* Add other config variables here and to Documentation/config.txt. */
>   	return 0;
> diff --git a/environment.c b/environment.c
> index 2f27008424..d550deabbd 100644
> --- a/environment.c
> +++ b/environment.c
> @@ -60,6 +60,8 @@ int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
>   char *check_roundtrip_encoding = "SHIFT-JIS";
>   unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
>   enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
> +const char* git_branch_remote = NULL;
> +const char* git_branch_merge = NULL;

Can the new settings be implemented without adding more global variables ? I think we are trying to move away from these. Apart from that the code looks OK to me.

Show 13 quoted lines
>   enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
>   enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
>   #ifndef OBJECT_CREATION_MODE
> diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
> index cc4b10236e..82137e8451 100755
> --- a/t/t3200-branch.sh
> +++ b/t/t3200-branch.sh
> @@ -797,6 +797,45 @@ test_expect_success 'test tracking setup via --track but deeper' '
>   	test "$(git config branch.my7.merge)" = refs/heads/o/o
>   '
>   
> +test_expect_success 'test tracking setup via branch.default* and --track' '
> +	git config branch.autosetupmerge always &&

You can use 'test_config branch.autosetupmerge always' so that the config is only active for the duration of the 'test_expect_success' block (see t/test-lib-functions.sh).

Show 8 quoted lines
> +	git config branch.defaultremote local &&
> +	git config branch.defaultmerge main &&
> +	git config remote.local.url . &&
> +	git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
> +	(git show-ref -q refs/remotes/local/main || git fetch local) &&
> +	git branch --track other/foo my2 &&
> +	git config branch.autosetupmerge false &&
> +	test "$(git config branch.my2.remote)" = other &&
Here and for the following line you can use 'test_cmp_config'.
> +	test "$(git config branch.my2.merge)" = refs/heads/foo
> +'

This tests checks that an explicit '--track' argument overrides the new configs. I would name it something like "'--track overrides 'branch.default{merge,remote}'" or something like this. I would also add another test before this one that just checks that the new settings by themselves work as expected; i.e. a simple 'git checkout -b' and verifying that the tracking info is correctly configured according to 'branch.default{merge,remote}'.

Show 12 quoted lines
> +
> +test_expect_success 'test tracking setup via branch.default* and --no-track' '
> +	git config branch.autosetupmerge always &&
> +	git config branch.defaultremote local &&
> +	git config branch.defaultmerge main &&
> +	git config remote.local.url . &&
> +	git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
> +	(git show-ref -q refs/remotes/local/main || git fetch local) &&
> +	git branch --no-track my2 &&
> +	git config branch.autosetupmerge false &&
> +	! test "$(git config branch.my2.remote)" = local &&
> +	! test "$(git config branch.my2.merge)" = refs/heads/main

Here you expect the configs to be absent, so for more clarity you could do

     git config branch.my2.remote >remote &&
     test_must_be_empty remote
and the same for merge.
Show 19 quoted lines
> +'
> +
> +test_expect_success 'test tracking setup via branch.default*' '
> +	git config branch.autosetupmerge always &&
> +	git config branch.defaultremote local &&
> +	git config branch.defaultmerge main &&
> +	git config remote.local.url . &&
> +	git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
> +	(git show-ref -q refs/remotes/local/main || git fetch local) &&
> +	git branch my2 &&
> +	git config branch.autosetupmerge false &&
> +	test "$(git config branch.my2.remote)" = local &&
> +	test "$(git config branch.my2.merge)" = refs/heads/main
> +'
> +
>   test_expect_success 'test deleting branch deletes branch config' '
>   	git branch -d my7 &&
>   	test -z "$(git config branch.my7.remote)" &&
> 

Oh, so here is the 'just test the new settings' test. Let's move that one to be before the two others.

Another test that could be added is one that does not change 'branch.autosetupmerge' but sets the new configs, and checks that the tracking info is *not* set.

Cheers,
Philippe.
Previous: Ben BoeckelNext: Ben Boeckel
Message 8 of 20 in “Improve automatic setup of tracking for new branches”
  1. 0/1 Improve automatic setup of tracking for new branchesBen Boeckel, Jul 28, 2021
  2. 1/1 config: support setting up a remote tracking branch upon creationBen Boeckel, Jul 28, 2021
  3. Junio C HamanoJul 28, 2021
  4. Ben BoeckelJul 28, 2021
  5. Junio C HamanoJul 28, 2021
  6. 0/1 Improve automatic setup of tracking for new branchesBen Boeckel, Jul 29, 2021
  7. 1/1 config: support a default remote tracking setup upon branch creationBen Boeckel, Jul 29, 2021
  8. Philippe BlainJul 30, 2021
  9. Ben BoeckelJul 30, 2021
  10. Junio C HamanoJul 30, 2021
  11. Ævar Arnfjörð BjarmasonAug 2, 2021
  12. Ben BoeckelAug 2, 2021
  13. Ævar Arnfjörð BjarmasonAug 2, 2021
  14. Junio C HamanoJul 30, 2021
  15. Ben BoeckelJul 30, 2021
  16. Philippe BlainJul 30, 2021
  17. Ben BoeckelJul 30, 2021
  18. Philippe BlainJul 30, 2021
  19. Ben BoeckelJul 30, 2021
  20. Johannes SchindelinAug 2, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.