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

Re: [PATCH] builtin: add git-default-branch command

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Nov 2, 2021, 13:39 UTC
Message-ID
<nycvar.QRO.7.76.6.2111021433010.56@tvgsbejvaqbjf.bet>
In-Reply-To
<20211030140112.834650-1-thomas@t-8ch.de>
Hi Thomas,
On Sat, 30 Oct 2021, Thomas Weißschuh wrote:
Show 8 quoted lines
> Introduce command `default-branch` which allows to retrieve the branch
> that will be used by git-init.
>
> Currently this command is equivalent to
> 	git config init.defaultbranch || 'master'
>
> This however will break if at one point the default branch is changed as
> indicated by `default_branch_name_advice` in `refs.c`.

I am very sympathetic to the motivation for your patch, I have had to resort to an ugly hack in Git for Windows' script that generates the installer: the script creates a throw-away repository _just_ to determine said branch name.

>
> By providing this command ahead of time users of git can make their
> code forward-compatible.

It is probably overkill to introduce a whole new command for just this single purpose.

But we do have prior art in Git how to display similar information: `git var -l` will list e.g. `GIT_PAGER`, even if it is not configured explicitly.

Something like this should be a good start along those lines:
-- snip --
diff --git a/builtin/var.c b/builtin/var.c
index 6c6f46b4aea..937c63939d9 100644
--- a/builtin/var.c
+++ b/builtin/var.c
@@ -5,6 +5,7 @@
  */
 #include "builtin.h"
 #include "config.h"
+#include "refs.h"

 static const char var_usage[] = "git var (-l | <variable>)";

@@ -27,6 +28,16 @@ static const char *pager(int flag)
 	return pgm;
 }

+static const char *default_branch(int flag)
+{
+	const char *name = repo_default_branch_name(the_repository, 1);
+
+	if (!name)
+		BUG("could not determine the default branch name");
+
+	return name;
+}
+
 struct git_var {
 	const char *name;
 	const char *(*read)(int);
@@ -36,6 +47,7 @@ static struct git_var git_vars[] = {
 	{ "GIT_AUTHOR_IDENT",   git_author_info },
 	{ "GIT_EDITOR", editor },
 	{ "GIT_PAGER", pager },
+	{ "GIT_DEFAULT_BRANCH", default_branch },
 	{ "", NULL },
 };

-- snap --

Thanks,
Johannes
Previous: Ævar Arnfjörð BjarmasonNext: Thomas Weißschuh
Message 3 of 18 in “builtin: add git-default-branch command”
  1. builtin: add git-default-branch commandThomas Weißschuh, Oct 30, 2021
  2. Ævar Arnfjörð BjarmasonOct 30, 2021
  3. Johannes SchindelinNov 2, 2021
  4. var: add GIT_DEFAULT_BRANCH variableThomas Weißschuh, Nov 2, 2021
  5. Ævar Arnfjörð BjarmasonNov 2, 2021
  6. Thomas WeißschuhNov 2, 2021
  7. Ævar Arnfjörð BjarmasonNov 2, 2021
  8. Thomas WeißschuhNov 2, 2021
  9. Jeff KingNov 3, 2021
  10. Eric SunshineNov 3, 2021
  11. Junio C HamanoNov 3, 2021
  12. var: add GIT_DEFAULT_BRANCH variableThomas Weißschuh, Nov 3, 2021
  13. Eric SunshineNov 3, 2021
  14. Junio C HamanoNov 3, 2021
  15. var: add GIT_DEFAULT_BRANCH variableThomas Weißschuh, Nov 3, 2021
  16. Junio C HamanoNov 3, 2021
  17. Junio C HamanoNov 3, 2021
  18. Johannes SchindelinNov 3, 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.