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

Re: [PATCH v2 02/12] fmt-merge-msg: introduce a way to override the main branch name

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Jun 15, 2020, 15:00 UTC
Message-ID
<b530fe66-9cf6-ea63-e9e6-123448e2d978@gmail.com>
In-Reply-To
<f4d547391537e5c3b0b4a07adb41b6aa56541fc3.1592225416.git.gitgitgadget@gmail.com>
Hi dscho
On 15/06/2020 13:50, Johannes Schindelin via GitGitGadget wrote:
Show 6 quoted lines
> From: Johannes Schindelin <johannes.schindelin@gmx.de>
> 
> There is a growing number of projects and companies desiring to change
> the main branch name of their repositories (see e.g.
> https://twitter.com/mislav/status/1270388510684598272 for background on
> this).
I think this is a good way of phrasing the rationale for the change
Show 7 quoted lines
> However, there are a couple of hard-coded spots in Git's source code
> that make this endeavor harder than necessary. For example, when
> formatting the commit message for merge commits, Git appends "into
> <branch-name>" unless the current branch is the `master` branch.
> 
> Clearly, this is not what one wants when already having gone through all
> the steps to manually rename the main branch
This didn't quite scan for me maybe s/already having/one has already/ ?
Show 85 quoted lines
> (and taking care of all the
> fall-out such as re-targeting existing Pull Requests).
> 
> Let's introduce a way to override Git's hard-coded default:
> `core.mainBranch`.
> 
> We will start supporting this config option in the `git fmt-merge-msg`
> command and successively adjust all other places where the main branch
> name is hard-coded.
> 
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
>   Documentation/config/core.txt |  5 +++++
>   fmt-merge-msg.c               |  6 ++++--
>   refs.c                        | 27 +++++++++++++++++++++++++++
>   refs.h                        |  7 +++++++
>   t/t6200-fmt-merge-msg.sh      |  7 +++++++
>   5 files changed, 50 insertions(+), 2 deletions(-)
> 
> diff --git a/Documentation/config/core.txt b/Documentation/config/core.txt
> index 74619a9c03b..32bb5368ebb 100644
> --- a/Documentation/config/core.txt
> +++ b/Documentation/config/core.txt
> @@ -626,3 +626,8 @@ core.abbrev::
>   	in your repository, which hopefully is enough for
>   	abbreviated object names to stay unique for some time.
>   	The minimum length is 4.
> +
> +core.mainBranch::
> +	The name of the main (or: primary) branch in the current repository.
> +	For historical reasons, `master` is used as the fall-back for this
> +	setting.
> diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c
> index 72d32bd73b1..43f4f829242 100644
> --- a/fmt-merge-msg.c
> +++ b/fmt-merge-msg.c
> @@ -407,7 +407,7 @@ static void fmt_merge_msg_title(struct strbuf *out,
>   				const char *current_branch)
>   {
>   	int i = 0;
> -	char *sep = "";
> +	char *sep = "", *main_branch;
>   
>   	strbuf_addstr(out, "Merge ");
>   	for (i = 0; i < srcs.nr; i++) {
> @@ -451,10 +451,12 @@ static void fmt_merge_msg_title(struct strbuf *out,
>   			strbuf_addf(out, " of %s", srcs.items[i].string);
>   	}
>   
> -	if (!strcmp("master", current_branch))
> +	main_branch = git_main_branch_name();
> +	if (!strcmp(main_branch, current_branch))
>   		strbuf_addch(out, '\n');
>   	else
>   		strbuf_addf(out, " into %s\n", current_branch);
> +	free(main_branch);
>   }
>   
>   static void fmt_tag_signature(struct strbuf *tagbuf,
> diff --git a/refs.c b/refs.c
> index 224ff66c7bb..f1854cffa2f 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -560,6 +560,33 @@ void expand_ref_prefix(struct argv_array *prefixes, const char *prefix)
>   		argv_array_pushf(prefixes, *p, len, prefix);
>   }
>   
> +char *repo_main_branch_name(struct repository *r)
> +{
> +	const char *config_key = "core.mainbranch";
> +	const char *config_display_key = "core.mainBranch";
> +	const char *fall_back = "master";
> +	char *name = NULL, *ret;
> +
> +	if (repo_config_get_string(r, config_key, &name) < 0)
> +		die(_("could not retrieve `%s`"), config_display_key);
> +
> +	ret = name ? name : xstrdup(fall_back);
> +
> +	if (check_refname_format(ret, REFNAME_ALLOW_ONELEVEL))
> +		die(_("invalid branch name: %s = %s"),
> +		    config_display_key, name);
> +
> +	if (name != ret)
> +		free(name);

I'm struggling to come up with a scenario where name != NULL && name != ret here, however once we get to patch 4 that scenario definitely does exist.

Show 22 quoted lines
> +
> +	return ret;
> +}
> +
> +char *git_main_branch_name(void)
> +{
> +	return repo_main_branch_name(the_repository);
> +}
> +
>   /*
>    * *string and *len will only be substituted, and *string returned (for
>    * later free()ing) if the string passed in is a magic short-hand form
> diff --git a/refs.h b/refs.h
> index a92d2c74c83..a207ef01348 100644
> --- a/refs.h
> +++ b/refs.h
> @@ -154,6 +154,13 @@ int repo_dwim_log(struct repository *r, const char *str, int len, struct object_
>   int dwim_ref(const char *str, int len, struct object_id *oid, char **ref);
>   int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
>   
> +/*
> + * Retrieves the name of the main (or: primary) branch of the given
nit pick, I'm confused by the ':'
Best Wishes
Phillip
Show 27 quoted lines
> + * repository.
> + */
> +char *git_main_branch_name(void);
> +char *repo_main_branch_name(struct repository *r);
> +
>   /*
>    * A ref_transaction represents a collection of reference updates that
>    * should succeed or fail together.
> diff --git a/t/t6200-fmt-merge-msg.sh b/t/t6200-fmt-merge-msg.sh
> index e4c2a6eca43..7a873f4a05c 100755
> --- a/t/t6200-fmt-merge-msg.sh
> +++ b/t/t6200-fmt-merge-msg.sh
> @@ -158,6 +158,13 @@ test_expect_success 'setup FETCH_HEAD' '
>   	git fetch . left
>   '
>   
> +test_expect_success 'with overridden default branch name' '
> +	test_when_finished "git switch master" &&
> +	git switch -c default &&
> +	git -c core.mainBranch=default fmt-merge-msg <.git/FETCH_HEAD >actual &&
> +	! grep "into default" actual
> +'
> +
>   test_expect_success 'merge.log=3 limits shortlog length' '
>   	cat >expected <<-EOF &&
>   	Merge branch ${apos}left${apos}
> 
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin
Message 93 of 180 in “Allow overriding the default name of the default branch”
  1. 0/9 Allow overriding the default name of the default branchJohannes Schindelin via GitGitGadget, Jun 10, 2020
  2. 1/9 init: allow overriding the default branch name for new repositoriesDon Goodman-Wilson via GitGitGadget, Jun 10, 2020
  3. brian m. carlsonJun 10, 2020
  4. Eric SunshineJun 11, 2020
  5. Johannes SchindelinJun 11, 2020
  6. Junio C HamanoJun 11, 2020
  7. Jeff KingJun 16, 2020
  8. Jeff KingJun 16, 2020
  9. Johannes SchindelinJun 18, 2020
  10. Johannes SchindelinJun 23, 2020
  11. Phillip WoodJun 11, 2020
  12. Johannes SchindelinJun 12, 2020
  13. Junio C HamanoJun 12, 2020
  14. Phillip WoodJun 15, 2020
  15. Johannes SchindelinJun 14, 2020
  16. Alban GruinJun 11, 2020
  17. Junio C HamanoJun 11, 2020
  18. brian m. carlsonJun 11, 2020
  19. Johannes SchindelinJun 12, 2020
  20. Alban GruinJun 13, 2020
  21. Johannes SchindelinJun 14, 2020
  22. Jeff KingJun 16, 2020
  23. Johannes SchindelinJun 18, 2020
  24. 3/9 send-pack/transport-helper: respect `core.defaultBranchName`Johannes Schindelin via GitGitGadget, Jun 10, 2020
  25. 2/9 remote: respect `core.defaultBranchName`Johannes Schindelin via GitGitGadget, Jun 10, 2020
  26. Jeff KingJun 16, 2020
  27. Johannes SchindelinJun 18, 2020
  28. Jeff KingJun 18, 2020
  29. Johannes SchindelinJun 23, 2020
  30. 7/9 fmt-merge-msg: learn about the possibly-configured default branch nameJohannes Schindelin via GitGitGadget, Jun 10, 2020
  31. Junio C HamanoJun 10, 2020
  32. 6/9 clone: learn about the possibly-configured default branch nameJohannes Schindelin via GitGitGadget, Jun 10, 2020
  33. Junio C HamanoJun 10, 2020
  34. 8/9 fast-export: respect the possibly-overridden default branch nameJohannes Schindelin via GitGitGadget, Jun 10, 2020
  35. Matt RogersJun 10, 2020
  36. Junio C HamanoJun 10, 2020
  37. brian m. carlsonJun 10, 2020
  38. Matt RogersJun 11, 2020
  39. Junio C HamanoJun 11, 2020
  40. Johannes SchindelinJun 11, 2020
  41. Re* [PATCH 8/9] fast-export: respect the possibly-overridden default branch nameJunio C Hamano, Jun 11, 2020
  42. Junio C HamanoJun 11, 2020
  43. Junio C HamanoJun 11, 2020
  44. Johannes SchindelinJun 12, 2020
  45. Junio C HamanoJun 12, 2020
  46. Johannes SchindelinJun 12, 2020
  47. Junio C HamanoJun 12, 2020
  48. Johannes SchindelinJun 12, 2020
  49. Johannes SchindelinJun 12, 2020
  50. Junio C HamanoJun 12, 2020
  51. Junio C HamanoJun 12, 2020
  52. Johannes SchindelinJun 13, 2020
  53. Junio C HamanoJun 12, 2020
  54. Johannes SixtJun 13, 2020
  55. Junio C HamanoJun 13, 2020
  56. Johannes SchindelinJun 13, 2020
  57. Junio C HamanoJun 13, 2020
  58. Johannes SchindelinJun 14, 2020
  59. Junio C HamanoJun 17, 2020
  60. Johannes SchindelinJun 23, 2020
  61. Junio C HamanoJun 23, 2020
  62. Johannes SchindelinJun 13, 2020
  63. Johannes SchindelinJun 11, 2020
  64. Junio C HamanoJun 11, 2020
  65. Johannes SchindelinJun 12, 2020
  66. 9/9 Document how the default branch name can be overriddenJohannes Schindelin via GitGitGadget, Jun 10, 2020
  67. Junio C HamanoJun 11, 2020
  68. 5/9 submodule: use the (possibly overridden) default branch nameJohannes Schindelin via GitGitGadget, Jun 10, 2020
  69. Denton LiuJun 15, 2020
  70. 4/9 testsvn: respect `core.defaultBranchName`Johannes Schindelin via GitGitGadget, Jun 10, 2020
  71. Junio C HamanoJun 10, 2020
  72. Junio C HamanoJun 11, 2020
  73. Johannes SchindelinJun 11, 2020
  74. Junio C HamanoJun 11, 2020
  75. brian m. carlsonJun 10, 2020
  76. Taylor BlauJun 11, 2020
  77. Johannes SchindelinJun 11, 2020
  78. Pratyush YadavJun 15, 2020
  79. Johannes SchindelinJun 14, 2020
  80. Denton LiuJun 16, 2020
  81. Johannes SchindelinJun 23, 2020
  82. brian m. carlsonJun 15, 2020
  83. 00/12 Allow overriding the default name of the default branchJohannes Schindelin via GitGitGadget, Jun 15, 2020
  84. 01/12 fast-export: do anonymize the primary branch nameJunio C Hamano via GitGitGadget, Jun 15, 2020
  85. Jeff KingJun 16, 2020
  86. Junio C HamanoJun 17, 2020
  87. Jeff KingJun 17, 2020
  88. Elijah NewrenJun 18, 2020
  89. Junio C HamanoJun 18, 2020
  90. Elijah NewrenJun 18, 2020
  91. Jeff KingJun 18, 2020
  92. 02/12 fmt-merge-msg: introduce a way to override the main branch nameJohannes Schindelin via GitGitGadget, Jun 15, 2020
  93. Phillip WoodJun 15, 2020
  94. Johannes SchindelinJun 23, 2020
  95. Junio C HamanoJun 15, 2020
  96. Johannes SchindelinJun 23, 2020
  97. Ævar Arnfjörð BjarmasonJun 16, 2020
  98. Junio C HamanoJun 17, 2020
  99. Jeff KingJun 16, 2020
  100. Junio C HamanoJun 17, 2020
  101. Johannes SchindelinJun 18, 2020
  102. Johannes SixtJun 17, 2020
  103. Junio C HamanoJun 17, 2020
  104. Johannes SchindelinJun 23, 2020
  105. 05/12 fast-export: handle overridden main branch names correctlyJohannes Schindelin via GitGitGadget, Jun 15, 2020
  106. Phillip WoodJun 15, 2020
  107. Jeff KingJun 16, 2020
  108. Phillip WoodJun 16, 2020
  109. Johannes SchindelinJun 18, 2020
  110. Junio C HamanoJun 15, 2020
  111. Johannes SchindelinJun 23, 2020
  112. 04/12 git_main_branch_name(): optionally report the full ref nameJohannes Schindelin via GitGitGadget, Jun 15, 2020
  113. Phillip WoodJun 15, 2020
  114. Johannes SchindelinJun 23, 2020
  115. 03/12 send-pack/transport-helper: respect `core.mainBranch`Johannes Schindelin via GitGitGadget, Jun 15, 2020
  116. 06/12 branch -m: adjust `core.mainBranch` if necessaryJohannes Schindelin via GitGitGadget, Jun 15, 2020
  117. 12/12 testsvn: respect `init.defaultBranch`Johannes Schindelin via GitGitGadget, Jun 15, 2020
  118. Jeff KingJun 16, 2020
  119. Johannes SchindelinJun 23, 2020
  120. 10/12 remote: learn about the possibly-overridden default main branch nameJohannes Schindelin via GitGitGadget, Jun 15, 2020
  121. 11/12 submodule: use the correct default for the main branch nameJohannes Schindelin via GitGitGadget, Jun 15, 2020
  122. Jeff KingJun 16, 2020
  123. Johannes SchindelinJun 23, 2020
  124. Jeff KingJun 23, 2020
  125. 09/12 clone: handle overridden main branch namesJohannes Schindelin via GitGitGadget, Jun 15, 2020
  126. Jeff KingJun 16, 2020
  127. Johannes SchindelinJun 23, 2020
  128. 08/12 init: allow overriding the default main branch name via the configDon Goodman-Wilson via GitGitGadget, Jun 15, 2020
  129. 07/12 init: allow specifying the main branch name for the new repositoryJohannes Schindelin via GitGitGadget, Jun 15, 2020
  130. 0/8 Allow overriding the default name of the default branchJohannes Schindelin via GitGitGadget, Jun 23, 2020
  131. 2/8 send-pack/transport-helper: avoid mentioning a particular branchJohannes Schindelin via GitGitGadget, Jun 23, 2020
  132. Junio C HamanoJun 24, 2020
  133. Johannes SchindelinJun 24, 2020
  134. Junio C HamanoJun 24, 2020
  135. Johannes SchindelinJun 25, 2020
  136. 1/8 fmt-merge-msg: stop treating `master` speciallyJohannes Schindelin via GitGitGadget, Jun 23, 2020
  137. Junio C HamanoJun 24, 2020
  138. Johannes SchindelinJun 25, 2020
  139. 8/8 testsvn: respect `init.defaultBranch`Johannes Schindelin via GitGitGadget, Jun 23, 2020
  140. 4/8 init: allow specifying the initial branch name for the new repositoryJohannes Schindelin via GitGitGadget, Jun 23, 2020
  141. Junio C HamanoJun 24, 2020
  142. Johannes SchindelinJun 24, 2020
  143. 7/8 remote: use the configured default branch name when appropriateJohannes Schindelin via GitGitGadget, Jun 23, 2020
  144. Junio C HamanoJun 24, 2020
  145. Johannes SchindelinJun 24, 2020
  146. 3/8 submodule: use a better fall-back for missing remote.<name>.branchJohannes Schindelin via GitGitGadget, Jun 23, 2020
  147. Philippe BlainJun 24, 2020
  148. Johannes SchindelinJun 24, 2020
  149. 6/8 clone: use configured default branch name when appropriateJohannes Schindelin via GitGitGadget, Jun 23, 2020
  150. 5/8 init: allow setting the default for the initial branch name via the configDon Goodman-Wilson via GitGitGadget, Jun 23, 2020
  151. Junio C HamanoJun 24, 2020
  152. Johannes SchindelinJun 24, 2020
  153. Junio C HamanoJun 24, 2020
  154. 0/9 Allow overriding the default name of the default branchJohannes Schindelin via GitGitGadget, Jun 24, 2020
  155. 2/9 send-pack/transport-helper: avoid mentioning a particular branchJohannes Schindelin via GitGitGadget, Jun 24, 2020
  156. 7/9 clone: use configured default branch name when appropriateJohannes Schindelin via GitGitGadget, Jun 24, 2020
  157. 9/9 testsvn: respect `init.defaultBranch`Johannes Schindelin via GitGitGadget, Jun 24, 2020
  158. 8/9 remote: use the configured default branch name when appropriateJohannes Schindelin via GitGitGadget, Jun 24, 2020
  159. 4/9 docs: add missing diamond bracketsJohannes Schindelin via GitGitGadget, Jun 24, 2020
  160. 3/9 submodule: fall back to remote's HEAD for missing remote.<name>.branchJohannes Schindelin via GitGitGadget, Jun 24, 2020
  161. Junio C HamanoJun 24, 2020
  162. 1/9 fmt-merge-msg: stop treating `master` speciallyJohannes Schindelin via GitGitGadget, Jun 24, 2020
  163. Đoàn Trần Công DanhJun 29, 2020
  164. Johannes SchindelinJun 29, 2020
  165. Đoàn Trần Công DanhJun 30, 2020
  166. Johannes SchindelinJul 1, 2020
  167. Junio C HamanoJul 1, 2020
  168. 6/9 init: allow setting the default for the initial branch name via the configDon Goodman-Wilson via GitGitGadget, Jun 24, 2020
  169. 5/9 init: allow specifying the initial branch name for the new repositoryJohannes Schindelin via GitGitGadget, Jun 24, 2020
  170. Junio C HamanoJun 24, 2020
  171. Johannes SchindelinJun 25, 2020
  172. brian m. carlsonJun 29, 2020
  173. Edward ThomsonJul 12, 2020
  174. Johannes SchindelinJul 12, 2020
  175. Ævar Arnfjörð BjarmasonJun 16, 2020
  176. Jeff KingJun 16, 2020
  177. Jeff KingJun 16, 2020
  178. Johannes SchindelinJun 23, 2020
  179. Junio C HamanoJun 17, 2020
  180. Johannes SchindelinJul 14, 2020

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.