{"thread":{"id":"56812","subject":"[PATCH] builtin: add git-default-branch command","startedAt":"2021-10-30T14:01:23Z","lastAt":"2021-11-03T23:44:55Z","messageCount":18,"participants":["Thomas Weißschuh","Ævar Arnfjörð Bjarmason","Johannes Schindelin","Jeff King","Eric Sunshine","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"440123","messageId":"20211030140112.834650-1-thomas@t-8ch.de","threadId":"56812","inReplyTo":null,"subject":"[PATCH] builtin: add git-default-branch command","fromName":"Thomas Weißschuh","fromEmail":"thomas@t-8ch.de","sentAt":"2021-10-30T14:01:12Z","receivedAt":"2021-10-30T14:01:23Z","isPatch":true,"sender":{"key":"thomas@t-8ch.de","avatar":"https://avatars.githubusercontent.com/u/717901?v=4"},"body":"Introduce command `default-branch` which allows to retrieve the branch\nthat will be used by git-init.\n\nCurrently this command is equivalent to\n\tgit config init.defaultbranch || 'master'\n\nThis however will break if at one point the default branch is changed as\nindicated by `default_branch_name_advice` in `refs.c`.\n\nBy providing this command ahead of time users of git can make their\ncode forward-compatible.\n\nSigned-off-by: Thomas Weißschuh <thomas@t-8ch.de>\n---\n .gitignore                           |  1 +\n Documentation/git-default-branch.txt | 15 +++++++++++++\n Makefile                             |  1 +\n builtin.h                            |  1 +\n builtin/default-branch.c             | 33 ++++++++++++++++++++++++++++\n command-list.txt                     |  1 +\n git.c                                |  1 +\n t/t1417-default-branch.sh            | 21 ++++++++++++++++++\n 8 files changed, 74 insertions(+)\n create mode 100644 Documentation/git-default-branch.txt\n create mode 100644 builtin/default-branch.c\n create mode 100755 t/t1417-default-branch.sh\n\ndiff --git a/.gitignore b/.gitignore\nindex 311841f9be..d9b969dc7e 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -53,6 +53,7 @@\n /git-cvsimport\n /git-cvsserver\n /git-daemon\n+/git-default-branch\n /git-diff\n /git-diff-files\n /git-diff-index\ndiff --git a/Documentation/git-default-branch.txt b/Documentation/git-default-branch.txt\nnew file mode 100644\nindex 0000000000..d5b891e5b4\n--- /dev/null\n+++ b/Documentation/git-default-branch.txt\n@@ -0,0 +1,15 @@\n+git-default-branch(1)\n+===================\n+\n+NAME\n+----\n+git-default-branch - Read the default branch ref used by git\n+\n+SYNOPSIS\n+--------\n+[verse]\n+'git default-branch'\n+\n+GIT\n+---\n+Part of the linkgit:git[1] suite\ndiff --git a/Makefile b/Makefile\nindex d1feab008f..49276359ab 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -1091,6 +1091,7 @@ BUILTIN_OBJS += builtin/credential-cache.o\n BUILTIN_OBJS += builtin/credential-store.o\n BUILTIN_OBJS += builtin/credential.o\n BUILTIN_OBJS += builtin/describe.o\n+BUILTIN_OBJS += builtin/default-branch.o\n BUILTIN_OBJS += builtin/diff-files.o\n BUILTIN_OBJS += builtin/diff-index.o\n BUILTIN_OBJS += builtin/diff-tree.o\ndiff --git a/builtin.h b/builtin.h\nindex 16ecd5586f..65b41e4c1f 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -143,6 +143,7 @@ int cmd_credential(int argc, const char **argv, const char *prefix);\n int cmd_credential_cache(int argc, const char **argv, const char *prefix);\n int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix);\n int cmd_credential_store(int argc, const char **argv, const char *prefix);\n+int cmd_default_branch(int argc, const char **argv, const char *prefix);\n int cmd_describe(int argc, const char **argv, const char *prefix);\n int cmd_diff_files(int argc, const char **argv, const char *prefix);\n int cmd_diff_index(int argc, const char **argv, const char *prefix);\ndiff --git a/builtin/default-branch.c b/builtin/default-branch.c\nnew file mode 100644\nindex 0000000000..e74c078926\n--- /dev/null\n+++ b/builtin/default-branch.c\n@@ -0,0 +1,33 @@\n+#include \"builtin.h\"\n+#include \"config.h\"\n+#include \"refs.h\"\n+#include \"parse-options.h\"\n+\n+static const char * const git_default_branch_usage[] = {\n+\tN_(\"git default-branch\"),\n+\tNULL\n+};\n+\n+\n+int cmd_default_branch(int argc, const char **argv, const char *prefix)\n+{\n+\tchar *name;\n+\n+\tstruct option options[] = {\n+\t\tOPT_END(),\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_default_branch_usage, 0);\n+\n+\tif (argc != 0)\n+\t\tusage_with_options(git_default_branch_usage, options);\n+\n+\tname = repo_default_branch_name(the_repository, 1);\n+\n+\tif (!name)\n+\t\tdie(\"Could not fetch default branch name\");\n+\n+\tputs(name);\n+\treturn 0;\n+}\ndiff --git a/command-list.txt b/command-list.txt\nindex a289f09ed6..950fa9a993 100644\n--- a/command-list.txt\n+++ b/command-list.txt\n@@ -81,6 +81,7 @@ git-cvsexportcommit                     foreignscminterface\n git-cvsimport                           foreignscminterface\n git-cvsserver                           foreignscminterface\n git-daemon                              synchingrepositories\n+git-default-branch                      plumbinginterrogators\n git-describe                            mainporcelain\n git-diff                                mainporcelain           info\n git-diff-files                          plumbinginterrogators\ndiff --git a/git.c b/git.c\nindex 18bed9a996..112f37a7f3 100644\n--- a/git.c\n+++ b/git.c\n@@ -516,6 +516,7 @@ static struct cmd_struct commands[] = {\n \t{ \"credential-cache\", cmd_credential_cache },\n \t{ \"credential-cache--daemon\", cmd_credential_cache_daemon },\n \t{ \"credential-store\", cmd_credential_store },\n+\t{ \"default-branch\", cmd_default_branch, RUN_SETUP_GENTLY },\n \t{ \"describe\", cmd_describe, RUN_SETUP },\n \t{ \"diff\", cmd_diff, NO_PARSEOPT },\n \t{ \"diff-files\", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },\ndiff --git a/t/t1417-default-branch.sh b/t/t1417-default-branch.sh\nnew file mode 100755\nindex 0000000000..d81f1ee214\n--- /dev/null\n+++ b/t/t1417-default-branch.sh\n@@ -0,0 +1,21 @@\n+#!/bin/sh\n+\n+test_description='Test git default-branch'\n+\n+. ./test-lib.sh\n+\n+unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+test_expect_success 'without configuration' '\n+\tb=$(git default-branch) &&\n+\tverbose test \"$b\" = master\n+'\n+\n+test_expect_success 'with configuration' '\n+\tgit config init.defaultbranch foo &&\n+\tb=$(git default-branch) &&\n+\techo $b &&\n+\tverbose test \"$b\" = foo\n+'\n+\n+test_done\n\nbase-commit: 6c40894d2466d4e7fddc047a05116aa9d14712ee\n-- \n2.33.1\n\n"},{"id":"440130","messageId":"211030.86ilxe4edm.gmgdl@evledraar.gmail.com","threadId":"56812","inReplyTo":"20211030140112.834650-1-thomas@t-8ch.de","subject":"Re: [PATCH] builtin: add git-default-branch command","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-10-30T17:18:26Z","receivedAt":"2021-10-30T17:22:03Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Sat, Oct 30 2021, Thomas Weißschuh wrote:\n\n> Introduce command `default-branch` which allows to retrieve the branch\n> that will be used by git-init.\n>\n> Currently this command is equivalent to\n> \tgit config init.defaultbranch || 'master'\n>\n> This however will break if at one point the default branch is changed as\n> indicated by `default_branch_name_advice` in `refs.c`.\n>\n> By providing this command ahead of time users of git can make their\n> code forward-compatible.\n\nRecently there was a discussion on a similar topic, i.e. to have git\nexplicitly aware of \"default config\" as far as \"git config -l\" etc. go:\n\n    https://lore.kernel.org/git/87czvoowg2.fsf@evledraar.gmail.com/\n\nI'd much rather see as add this as some mode of git-config, even if it's\na new --get-or-git-default switch:\n\n    git config --get-or-git-default init.defaultBranch\n\nThat would just currently die if you fed it any other value than\ninit.defaultBranch, i.e. that (or similar) would be a generic enough\ninterface that we could expand on it.\n\nWhereas having a new-built in just for this one config variable...\n\n> +test_expect_success 'without configuration' '\n> +\tb=$(git default-branch) &&\n> +\tverbose test \"$b\" = master\n> +'\n> +\n> +test_expect_success 'with configuration' '\n> +\tgit config init.defaultbranch foo &&\n> +\tb=$(git default-branch) &&\n> +\techo $b &&\n> +\tverbose test \"$b\" = foo\n> +'\n> +\n\nShould lose the echo, and these comparisons should be using test_cmp,\nalso use \"test_config\" not \"git config\" unless in a sub-repo or similar.\n"},{"id":"440305","messageId":"nycvar.QRO.7.76.6.2111021433010.56@tvgsbejvaqbjf.bet","threadId":"56812","inReplyTo":"20211030140112.834650-1-thomas@t-8ch.de","subject":"Re: [PATCH] builtin: add git-default-branch command","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2021-11-02T13:39:27Z","receivedAt":"2021-11-02T13:39:34Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Thomas,\n\nOn Sat, 30 Oct 2021, Thomas Weißschuh wrote:\n\n> Introduce command `default-branch` which allows to retrieve the branch\n> that will be used by git-init.\n>\n> Currently this command is equivalent to\n> \tgit config init.defaultbranch || 'master'\n>\n> This however will break if at one point the default branch is changed as\n> indicated by `default_branch_name_advice` in `refs.c`.\n\nI am very sympathetic to the motivation for your patch, I have had to\nresort to an ugly hack in Git for Windows' script that generates the\ninstaller: the script creates a throw-away repository _just_ to determine\nsaid branch name.\n\n>\n> By providing this command ahead of time users of git can make their\n> code forward-compatible.\n\nIt is probably overkill to introduce a whole new command for just this\nsingle purpose.\n\nBut we do have prior art in Git how to display similar information: `git\nvar -l` will list e.g. `GIT_PAGER`, even if it is not configured\nexplicitly.\n\nSomething like this should be a good start along those lines:\n\n-- snip --\ndiff --git a/builtin/var.c b/builtin/var.c\nindex 6c6f46b4aea..937c63939d9 100644\n--- a/builtin/var.c\n+++ b/builtin/var.c\n@@ -5,6 +5,7 @@\n  */\n #include \"builtin.h\"\n #include \"config.h\"\n+#include \"refs.h\"\n\n static const char var_usage[] = \"git var (-l | <variable>)\";\n\n@@ -27,6 +28,16 @@ static const char *pager(int flag)\n \treturn pgm;\n }\n\n+static const char *default_branch(int flag)\n+{\n+\tconst char *name = repo_default_branch_name(the_repository, 1);\n+\n+\tif (!name)\n+\t\tBUG(\"could not determine the default branch name\");\n+\n+\treturn name;\n+}\n+\n struct git_var {\n \tconst char *name;\n \tconst char *(*read)(int);\n@@ -36,6 +47,7 @@ static struct git_var git_vars[] = {\n \t{ \"GIT_AUTHOR_IDENT\",   git_author_info },\n \t{ \"GIT_EDITOR\", editor },\n \t{ \"GIT_PAGER\", pager },\n+\t{ \"GIT_DEFAULT_BRANCH\", default_branch },\n \t{ \"\", NULL },\n };\n\n-- snap --\n\nThanks,\nJohannes\n"},{"id":"440336","messageId":"20211102164434.1005707-1-thomas@t-8ch.de","threadId":"56812","inReplyTo":"nycvar.QRO.7.76.6.2111021433010.56@tvgsbejvaqbjf.bet","subject":"[PATCH v2] var: add GIT_DEFAULT_BRANCH variable","fromName":"Thomas Weißschuh","fromEmail":"thomas@t-8ch.de","sentAt":"2021-11-02T16:44:34Z","receivedAt":"2021-11-02T16:45:37Z","isPatch":true,"sender":{"key":"thomas@t-8ch.de","avatar":"https://avatars.githubusercontent.com/u/717901?v=4"},"body":"Introduce the builtin variable GIT_DEFAULT_BRANCH which represents the\nthe default branch name that will be used by git-init.\n\nCurrently this variable is equivalent to\n    git config init.defaultbranch || 'master'\n\nThis however will break if at one point the default branch is changed as\nindicated by `default_branch_name_advice` in `refs.c`.\n\nBy providing this command ahead of time users of git can make their\ncode forward-compatible.\n\nCo-developed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\nSigned-off-by: Thomas Weißschuh <thomas@t-8ch.de>\n---\n\nChanges from v1 ( https://lore.kernel.org/git/20211030140112.834650-1-thomas@t-8ch.de/ ):\n* Replaced the custom subcommand with an internal variable\n* Cleaned up the tests\n\n@Johannes: I replaced BUG() with die() from your example because that seems to be\nnicer for user facing messages.\n\n Documentation/git-var.txt |  3 +++\n builtin/var.c             | 13 +++++++++++++\n t/t0007-git-var.sh        | 19 +++++++++++++++++++\n 3 files changed, 35 insertions(+)\n\ndiff --git a/Documentation/git-var.txt b/Documentation/git-var.txt\nindex 6072f936ab..387cc1b914 100644\n--- a/Documentation/git-var.txt\n+++ b/Documentation/git-var.txt\n@@ -59,6 +59,9 @@ ifdef::git-default-pager[]\n     The build you are using chose '{git-default-pager}' as the default.\n endif::git-default-pager[]\n \n+GIT_DEFAULT_BRANCH::\n+    The name of the first branch created in newly initialized repositories.\n+\n SEE ALSO\n --------\n linkgit:git-commit-tree[1]\ndiff --git a/builtin/var.c b/builtin/var.c\nindex 6c6f46b4ae..d1d82b6c93 100644\n--- a/builtin/var.c\n+++ b/builtin/var.c\n@@ -5,6 +5,7 @@\n  */\n #include \"builtin.h\"\n #include \"config.h\"\n+#include \"refs.h\"\n \n static const char var_usage[] = \"git var (-l | <variable>)\";\n \n@@ -27,6 +28,17 @@ static const char *pager(int flag)\n \treturn pgm;\n }\n \n+static const char *default_branch(int flag)\n+{\n+\tconst char *name = repo_default_branch_name(the_repository, 1);\n+\n+\tif (!name)\n+\t\tdie(\"could not determine the default branch name\");\n+\n+\treturn name;\n+}\n+\n+\n struct git_var {\n \tconst char *name;\n \tconst char *(*read)(int);\n@@ -36,6 +48,7 @@ static struct git_var git_vars[] = {\n \t{ \"GIT_AUTHOR_IDENT\",   git_author_info },\n \t{ \"GIT_EDITOR\", editor },\n \t{ \"GIT_PAGER\", pager },\n+\t{ \"GIT_DEFAULT_BRANCH\", default_branch },\n \t{ \"\", NULL },\n };\n \ndiff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh\nindex 53af92d571..6b6852e35e 100755\n--- a/t/t0007-git-var.sh\n+++ b/t/t0007-git-var.sh\n@@ -27,6 +27,25 @@ test_expect_success !FAIL_PREREQS,!AUTOIDENT 'requested identities are strict' '\n \t)\n '\n \n+test_expect_success 'get GIT_DEFAULT_BRANCH without configuration' '\n+\t(\n+\t\tsane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&\n+\t\techo master >expect &&\n+\t\tgit var GIT_DEFAULT_BRANCH >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '\n+\ttest_config init.defaultbranch foo &&\n+\t(\n+\t\tsane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&\n+\t\techo foo >expect &&\n+\t\tgit var GIT_DEFAULT_BRANCH >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n # For git var -l, we check only a representative variable;\n # testing the whole output would make our test too brittle with\n # respect to unrelated changes in the test suite's environment.\n\nbase-commit: 0cddd84c9f3e9c3d793ec93034ef679335f35e49\n-- \n2.33.1\n\n"},{"id":"440343","messageId":"211102.86czni1o72.gmgdl@evledraar.gmail.com","threadId":"56812","inReplyTo":"20211102164434.1005707-1-thomas@t-8ch.de","subject":"Re: [PATCH v2] var: add GIT_DEFAULT_BRANCH variable","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-11-02T16:53:46Z","receivedAt":"2021-11-02T17:07:17Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Tue, Nov 02 2021, Thomas Weißschuh wrote:\n\n> Introduce the builtin variable GIT_DEFAULT_BRANCH which represents the\n> the default branch name that will be used by git-init.\n>\n> Currently this variable is equivalent to\n>     git config init.defaultbranch || 'master'\n>\n> This however will break if at one point the default branch is changed as\n> indicated by `default_branch_name_advice` in `refs.c`.\n>\n> By providing this command ahead of time users of git can make their\n> code forward-compatible.\n>\n> Co-developed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\n> Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>\n> ---\n>\n> Changes from v1 ( https://lore.kernel.org/git/20211030140112.834650-1-thomas@t-8ch.de/ ):\n> * Replaced the custom subcommand with an internal variable\n> * Cleaned up the tests\n>\n> @Johannes: I replaced BUG() with die() from your example because that seems to be\n> nicer for user facing messages.\n>\n>  Documentation/git-var.txt |  3 +++\n>  builtin/var.c             | 13 +++++++++++++\n>  t/t0007-git-var.sh        | 19 +++++++++++++++++++\n>  3 files changed, 35 insertions(+)\n>\n> diff --git a/Documentation/git-var.txt b/Documentation/git-var.txt\n> index 6072f936ab..387cc1b914 100644\n> --- a/Documentation/git-var.txt\n> +++ b/Documentation/git-var.txt\n> @@ -59,6 +59,9 @@ ifdef::git-default-pager[]\n>      The build you are using chose '{git-default-pager}' as the default.\n>  endif::git-default-pager[]\n>  \n> +GIT_DEFAULT_BRANCH::\n> +    The name of the first branch created in newly initialized repositories.\n> +\n>  SEE ALSO\n>  --------\n>  linkgit:git-commit-tree[1]\n> diff --git a/builtin/var.c b/builtin/var.c\n> index 6c6f46b4ae..d1d82b6c93 100644\n> --- a/builtin/var.c\n> +++ b/builtin/var.c\n> @@ -5,6 +5,7 @@\n>   */\n>  #include \"builtin.h\"\n>  #include \"config.h\"\n> +#include \"refs.h\"\n>  \n>  static const char var_usage[] = \"git var (-l | <variable>)\";\n>  \n> @@ -27,6 +28,17 @@ static const char *pager(int flag)\n>  \treturn pgm;\n>  }\n>  \n> +static const char *default_branch(int flag)\n> +{\n> +\tconst char *name = repo_default_branch_name(the_repository, 1);\n> +\n> +\tif (!name)\n> +\t\tdie(\"could not determine the default branch name\");\n\nIsn't this die() unrechable given the similar logic in\nrepo_default_branch_name()? Hence the previous BUG(...)?\n\nI really don't see how it makes sense to add this to \"git var\", we have\nthat to correspond to environment variables we use.\n\n*Maybe* if we renamed GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME to\nGIT_INITIAL_BRANCH_NAME and made it a non-test thing like\nGIT_TEMPLATE_DIR, but even then shouldn't we be adding\n\"GIT_TEMPLATE_DIR\" and any number of other things to this as well?\n\nI'm not saying that your patch needs to do that, but we really should\nthink about the interface & future implications if we're going in this\ndirection.\n\nThe reason I suggested extending \"git config\" in [1] is because it seems\nlike a natural thing for \"git config\" to learn to spew out our idea of\ndefault hardcoded config values to the user.\n\nBut creating a variable form of that existing config just so we can have\n\"git var\" spew it out just seems weird.\n\nWe don't have or need such a variable now for anything else, so why go\nthrough that indirection, instead of something that closes the feature\ngap of asking what a config variable default is?\n\nIn any case whatever we do here this really should be updating the\ndocumentation of init.defaultbranch & the relevant bits in the \"git\ninit\" manpage to add cross-references, similar to how we discuss\nGIT_TEMPLATE_DIR now.\n\n1. https://lore.kernel.org/git/211030.86ilxe4edm.gmgdl@evledraar.gmail.com/\n"},{"id":"440345","messageId":"06095eb4-df18-4f23-8e72-26882c49f25c@t-8ch.de","threadId":"56812","inReplyTo":"211102.86czni1o72.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH v2] var: add GIT_DEFAULT_BRANCH variable","fromName":"Thomas Weißschuh","fromEmail":"thomas@t-8ch.de","sentAt":"2021-11-02T17:35:08Z","receivedAt":"2021-11-02T17:35:11Z","isPatch":true,"sender":{"key":"thomas@t-8ch.de","avatar":"https://avatars.githubusercontent.com/u/717901?v=4"},"body":"On 2021-11-02 17:53+0100, Ævar Arnfjörð Bjarmason wrote:\n> On Tue, Nov 02 2021, Thomas Weißschuh wrote:\n> \n> > Introduce the builtin variable GIT_DEFAULT_BRANCH which represents the\n> > the default branch name that will be used by git-init.\n> >\n> > Currently this variable is equivalent to\n> >     git config init.defaultbranch || 'master'\n> >\n> > This however will break if at one point the default branch is changed as\n> > indicated by `default_branch_name_advice` in `refs.c`.\n> >\n> > By providing this command ahead of time users of git can make their\n> > code forward-compatible.\n> >\n> > Co-developed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\n> > Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>\n> > ---\n> >\n> > Changes from v1 ( https://lore.kernel.org/git/20211030140112.834650-1-thomas@t-8ch.de/ ):\n> > * Replaced the custom subcommand with an internal variable\n> > * Cleaned up the tests\n> >\n> > @Johannes: I replaced BUG() with die() from your example because that seems to be\n> > nicer for user facing messages.\n> >\n> >  Documentation/git-var.txt |  3 +++\n> >  builtin/var.c             | 13 +++++++++++++\n> >  t/t0007-git-var.sh        | 19 +++++++++++++++++++\n> >  3 files changed, 35 insertions(+)\n> >\n> >  \n> > +static const char *default_branch(int flag)\n> > +{\n> > +\tconst char *name = repo_default_branch_name(the_repository, 1);\n> > +\n> > +\tif (!name)\n> > +\t\tdie(\"could not determine the default branch name\");\n> \n> Isn't this die() unrechable given the similar logic in\n> repo_default_branch_name()? Hence the previous BUG(...)?\n\nOk. Good point.\n\n> I really don't see how it makes sense to add this to \"git var\", we have\n> that to correspond to environment variables we use.\n> \n> *Maybe* if we renamed GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME to\n> GIT_INITIAL_BRANCH_NAME and made it a non-test thing like\n> GIT_TEMPLATE_DIR, but even then shouldn't we be adding\n> \"GIT_TEMPLATE_DIR\" and any number of other things to this as well?\n> \n> I'm not saying that your patch needs to do that, but we really should\n> think about the interface & future implications if we're going in this\n> direction.\n> \n> The reason I suggested extending \"git config\" in [1] is because it seems\n> like a natural thing for \"git config\" to learn to spew out our idea of\n> default hardcoded config values to the user.\n> \n> But creating a variable form of that existing config just so we can have\n> \"git var\" spew it out just seems weird.\n> \n> We don't have or need such a variable now for anything else, so why go\n> through that indirection, instead of something that closes the feature\n> gap of asking what a config variable default is?\n> \n> In any case whatever we do here this really should be updating the\n> documentation of init.defaultbranch & the relevant bits in the \"git\n> init\" manpage to add cross-references, similar to how we discuss\n> GIT_TEMPLATE_DIR now.\n>\n> 1. https://lore.kernel.org/git/211030.86ilxe4edm.gmgdl@evledraar.gmail.com/\n\nI'll then wait for a consensus of the git devs. The actual implementation\nshouldn't be the issue afterwards.\n\nThanks for looking into this!\n\nThomas\n"},{"id":"440346","messageId":"211102.868ry61hy3.gmgdl@evledraar.gmail.com","threadId":"56812","inReplyTo":"06095eb4-df18-4f23-8e72-26882c49f25c@t-8ch.de","subject":"Re: [PATCH v2] var: add GIT_DEFAULT_BRANCH variable","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-11-02T19:14:12Z","receivedAt":"2021-11-02T19:22:16Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Tue, Nov 02 2021, Thomas Weißschuh wrote:\n\n> On 2021-11-02 17:53+0100, Ævar Arnfjörð Bjarmason wrote:\n>> On Tue, Nov 02 2021, Thomas Weißschuh wrote:\n>> \n>> > Introduce the builtin variable GIT_DEFAULT_BRANCH which represents the\n>> > the default branch name that will be used by git-init.\n>> >\n>> > Currently this variable is equivalent to\n>> >     git config init.defaultbranch || 'master'\n>> >\n>> > This however will break if at one point the default branch is changed as\n>> > indicated by `default_branch_name_advice` in `refs.c`.\n>> >\n>> > By providing this command ahead of time users of git can make their\n>> > code forward-compatible.\n>> >\n>> > Co-developed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\n>> > Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>\n>> > ---\n>> >\n>> > Changes from v1 ( https://lore.kernel.org/git/20211030140112.834650-1-thomas@t-8ch.de/ ):\n>> > * Replaced the custom subcommand with an internal variable\n>> > * Cleaned up the tests\n>> >\n>> > @Johannes: I replaced BUG() with die() from your example because that seems to be\n>> > nicer for user facing messages.\n>> >\n>> >  Documentation/git-var.txt |  3 +++\n>> >  builtin/var.c             | 13 +++++++++++++\n>> >  t/t0007-git-var.sh        | 19 +++++++++++++++++++\n>> >  3 files changed, 35 insertions(+)\n>> >\n>> >  \n>> > +static const char *default_branch(int flag)\n>> > +{\n>> > +\tconst char *name = repo_default_branch_name(the_repository, 1);\n>> > +\n>> > +\tif (!name)\n>> > +\t\tdie(\"could not determine the default branch name\");\n>> \n>> Isn't this die() unrechable given the similar logic in\n>> repo_default_branch_name()? Hence the previous BUG(...)?\n>\n> Ok. Good point.\n>\n>> I really don't see how it makes sense to add this to \"git var\", we have\n>> that to correspond to environment variables we use.\n>> \n>> *Maybe* if we renamed GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME to\n>> GIT_INITIAL_BRANCH_NAME and made it a non-test thing like\n>> GIT_TEMPLATE_DIR, but even then shouldn't we be adding\n>> \"GIT_TEMPLATE_DIR\" and any number of other things to this as well?\n>> \n>> I'm not saying that your patch needs to do that, but we really should\n>> think about the interface & future implications if we're going in this\n>> direction.\n>> \n>> The reason I suggested extending \"git config\" in [1] is because it seems\n>> like a natural thing for \"git config\" to learn to spew out our idea of\n>> default hardcoded config values to the user.\n>> \n>> But creating a variable form of that existing config just so we can have\n>> \"git var\" spew it out just seems weird.\n>> \n>> We don't have or need such a variable now for anything else, so why go\n>> through that indirection, instead of something that closes the feature\n>> gap of asking what a config variable default is?\n>> \n>> In any case whatever we do here this really should be updating the\n>> documentation of init.defaultbranch & the relevant bits in the \"git\n>> init\" manpage to add cross-references, similar to how we discuss\n>> GIT_TEMPLATE_DIR now.\n>>\n>> 1. https://lore.kernel.org/git/211030.86ilxe4edm.gmgdl@evledraar.gmail.com/\n>\n> I'll then wait for a consensus of the git devs. The actual implementation\n> shouldn't be the issue afterwards.\n>\n> Thanks for looking into this!\n\nPlease don't take that message as me or anyone else \"pulling rank\" just\nbecause we've got some previous commits in git.git. That applies to both\nme and Johannes, and clearly we disagree on this minor bit of UX\ndirection.\n\nI'd think if anything the opinion of someone who's not overly familiar\nwith the system would be more valuable, i.e. yours, especially since you\ntried & failed to find a way to do this recently. Would you find it more\nintuitive to look in say \"git var\" over \"git config\" for this sort of\ninformation?\n\nA further weirdness is that another effective source of config for this\nis the \"unborn\" ls-refs feature[1]. I'm not sure what that means for any\nquery interface, i.e. would a user want to know what branch a freshly\ncloned repo would end up with in advance, taking into account all of the\nlocal config, remote \"unborn\" etc?\n\n1. https://lore.kernel.org/git/878s8apthr.fsf@evledraar.gmail.com/\n"},{"id":"440347","messageId":"5a64a438-07c3-489a-aca5-d677ae15c3ca@t-8ch.de","threadId":"56812","inReplyTo":"211102.868ry61hy3.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH v2] var: add GIT_DEFAULT_BRANCH variable","fromName":"Thomas Weißschuh","fromEmail":"thomas@t-8ch.de","sentAt":"2021-11-02T20:08:16Z","receivedAt":"2021-11-02T20:08:23Z","isPatch":true,"sender":{"key":"thomas@t-8ch.de","avatar":"https://avatars.githubusercontent.com/u/717901?v=4"},"body":"On 2021-11-02 20:14+0100, Ævar Arnfjörð Bjarmason wrote:\n> On Tue, Nov 02 2021, Thomas Weißschuh wrote:\n> \n> > On 2021-11-02 17:53+0100, Ævar Arnfjörð Bjarmason wrote:\n> >> On Tue, Nov 02 2021, Thomas Weißschuh wrote:\n> >> \n> >> > Introduce the builtin variable GIT_DEFAULT_BRANCH which represents the\n> >> > the default branch name that will be used by git-init.\n> >> >\n> >> > Currently this variable is equivalent to\n> >> >     git config init.defaultbranch || 'master'\n> >> >\n> >> > This however will break if at one point the default branch is changed as\n> >> > indicated by `default_branch_name_advice` in `refs.c`.\n> >> >\n> >> > By providing this command ahead of time users of git can make their\n> >> > code forward-compatible.\n> >> >\n> >> > Co-developed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\n> >> > Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>\n> >> > ---\n> >> >\n> >> > Changes from v1 ( https://lore.kernel.org/git/20211030140112.834650-1-thomas@t-8ch.de/ ):\n> >> > * Replaced the custom subcommand with an internal variable\n> >> > * Cleaned up the tests\n> >> >\n> >> > @Johannes: I replaced BUG() with die() from your example because that seems to be\n> >> > nicer for user facing messages.\n> >> >\n> >> >  Documentation/git-var.txt |  3 +++\n> >> >  builtin/var.c             | 13 +++++++++++++\n> >> >  t/t0007-git-var.sh        | 19 +++++++++++++++++++\n> >> >  3 files changed, 35 insertions(+)\n> >> >\n> >> >  \n> >> > +static const char *default_branch(int flag)\n> >> > +{\n> >> > +\tconst char *name = repo_default_branch_name(the_repository, 1);\n> >> > +\n> >> > +\tif (!name)\n> >> > +\t\tdie(\"could not determine the default branch name\");\n> >> \n> >> Isn't this die() unrechable given the similar logic in\n> >> repo_default_branch_name()? Hence the previous BUG(...)?\n> >\n> > Ok. Good point.\n> >\n> >> I really don't see how it makes sense to add this to \"git var\", we have\n> >> that to correspond to environment variables we use.\n> >> \n> >> *Maybe* if we renamed GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME to\n> >> GIT_INITIAL_BRANCH_NAME and made it a non-test thing like\n> >> GIT_TEMPLATE_DIR, but even then shouldn't we be adding\n> >> \"GIT_TEMPLATE_DIR\" and any number of other things to this as well?\n> >> \n> >> I'm not saying that your patch needs to do that, but we really should\n> >> think about the interface & future implications if we're going in this\n> >> direction.\n> >> \n> >> The reason I suggested extending \"git config\" in [1] is because it seems\n> >> like a natural thing for \"git config\" to learn to spew out our idea of\n> >> default hardcoded config values to the user.\n> >> \n> >> But creating a variable form of that existing config just so we can have\n> >> \"git var\" spew it out just seems weird.\n> >> \n> >> We don't have or need such a variable now for anything else, so why go\n> >> through that indirection, instead of something that closes the feature\n> >> gap of asking what a config variable default is?\n> >> \n> >> In any case whatever we do here this really should be updating the\n> >> documentation of init.defaultbranch & the relevant bits in the \"git\n> >> init\" manpage to add cross-references, similar to how we discuss\n> >> GIT_TEMPLATE_DIR now.\n> >>\n> >> 1. https://lore.kernel.org/git/211030.86ilxe4edm.gmgdl@evledraar.gmail.com/\n> >\n> > I'll then wait for a consensus of the git devs. The actual implementation\n> > shouldn't be the issue afterwards.\n> >\n> > Thanks for looking into this!\n> \n> Please don't take that message as me or anyone else \"pulling rank\" just\n> because we've got some previous commits in git.git. That applies to both\n> me and Johannes, and clearly we disagree on this minor bit of UX\n> direction.\n\nThis was not my impression, although my message has a bit of a resignated\ntone. That was not the intention.\n\n> I'd think if anything the opinion of someone who's not overly familiar\n> with the system would be more valuable, i.e. yours, especially since you\n> tried & failed to find a way to do this recently. Would you find it more\n> intuitive to look in say \"git var\" over \"git config\" for this sort of\n> information?\n\nTo be honest I was not aware of \"git var\" before Johannes before proposed it.\nAnd I am still not sure how to understand the \"logical\" aspect of \"git-var\".\n(git-var - Show a Git logical variable)\n\nA \"git config\" variable using a generic config default framework looks like the\ngenerally cleanest interface.\nThe appeal of \"git var\" was the easy and quick implementation.\n\n> A further weirdness is that another effective source of config for this\n> is the \"unborn\" ls-refs feature[1]. I'm not sure what that means for any\n> query interface, i.e. would a user want to know what branch a freshly\n> cloned repo would end up with in advance, taking into account all of the\n> local config, remote \"unborn\" etc?\n\nI have no idea how that should work.\n\n> 1. https://lore.kernel.org/git/878s8apthr.fsf@evledraar.gmail.com/\n\nIf you and Johannes think it would help the design-process I would also\nvolunteer to implement your original proposal:\n\n    git config --get-or-git-default init.defaultBranch\n\nPlease note that I'm not doing (primarily) doing this to get commits into\ngit.git, so if somebody with more knowledge about the git architecture wants to\nbring this forward, please feel free.\n"},{"id":"440369","messageId":"YYJ0aQ11uHA/XUt3@coredump.intra.peff.net","threadId":"56812","inReplyTo":"211102.86czni1o72.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH v2] var: add GIT_DEFAULT_BRANCH variable","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2021-11-03T11:37:13Z","receivedAt":"2021-11-03T11:37:15Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, Nov 02, 2021 at 05:53:46PM +0100, Ævar Arnfjörð Bjarmason wrote:\n\n> The reason I suggested extending \"git config\" in [1] is because it seems\n> like a natural thing for \"git config\" to learn to spew out our idea of\n> default hardcoded config values to the user.\n> \n> But creating a variable form of that existing config just so we can have\n> \"git var\" spew it out just seems weird.\n> \n> We don't have or need such a variable now for anything else, so why go\n> through that indirection, instead of something that closes the feature\n> gap of asking what a config variable default is?\n\nTo me, the point is that the user is not asking \"what is the default\nvalue of this config variable?\". They are asking \"if I were to init a\nnew repository, what would Git decide the default branch name is?\".\n\nRight now that is very tied to the config mechanism (modulo the\nGIT_TEST_* bits, but I think we can ignore those for regular users), so\nthose two are basically the same question. But it doesn't have to be.\nAbstracting it to the question the user actually wants to ask\nfuture-proofs the mechanism.\n\nI.e., I don't think introducing a new variable into \"git var\" is that\nbig a deal. They don't have to be related to an environment variable;\nthe documentation calls them \"logical variables\". This is exactly the\nkind of thing it's designed for.\n\n-Peff\n"},{"id":"440383","messageId":"CAPig+cQ1fdU=YNc5jtT8oYaBMd8MP-j23hSsZ7GD-T99vDDeoA@mail.gmail.com","threadId":"56812","inReplyTo":"YYJ0aQ11uHA/XUt3@coredump.intra.peff.net","subject":"Re: [PATCH v2] var: add GIT_DEFAULT_BRANCH variable","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2021-11-03T16:48:19Z","receivedAt":"2021-11-03T16:48:33Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Wed, Nov 3, 2021 at 7:37 AM Jeff King <peff@peff.net> wrote:\n> I.e., I don't think introducing a new variable into \"git var\" is that\n> big a deal. They don't have to be related to an environment variable;\n> the documentation calls them \"logical variables\". This is exactly the\n> kind of thing it's designed for.\n\nI almost don't want to mention it since it's not very discoverable\n(and is never the first place I think to look), but git-rev-parse\nalready contains a grab-bag of options unrelated to revision parsing.\nMany of the options relate to querying paths (--show-toplevel,\n--show-cdup, etc.) or other information related to a repository\n(--local-env-vars, --show-object-format, etc.). So, adding\n--show-default-branch may be one possibility.\n"},{"id":"440387","messageId":"xmqqy2656tni.fsf@gitster.g","threadId":"56812","inReplyTo":"nycvar.QRO.7.76.6.2111021433010.56@tvgsbejvaqbjf.bet","subject":"Re: [PATCH] builtin: add git-default-branch command","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-11-03T17:22:41Z","receivedAt":"2021-11-03T17:22:47Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n\n> But we do have prior art in Git how to display similar information: `git\n> var -l` will list e.g. `GIT_PAGER`, even if it is not configured\n> explicitly.\n\nNice.  I was hoping that somebody would remember this one.\n\nGIT_AUTHOR_IDENT and such that do not even exist as a variable is\npart of \"git var -l\"; the name for the default initial branch falls\ninto the same category.\n\nThanks.\n"},{"id":"440393","messageId":"xmqqa6il6qxu.fsf@gitster.g","threadId":"56812","inReplyTo":"20211102164434.1005707-1-thomas@t-8ch.de","subject":"Re: [PATCH v2] var: add GIT_DEFAULT_BRANCH variable","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-11-03T18:21:17Z","receivedAt":"2021-11-03T18:21:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thomas Weißschuh <thomas@t-8ch.de> writes:\n\n> Introduce the builtin variable GIT_DEFAULT_BRANCH which represents the\n\n\"builtin\" -> \"logical\", as that is how \"git-var\" describes these things.\n\nIt is totally outside the scope of this patch, but I think we'd\nbetter think of a way to make it clear to the readers of the\ndocumentation that it would not do anything if they did something\nlike:\n\n    $ GIT_DEFAULT_BRANCH=foobar git init\n\nI say this is outside the scope because there are other existing\nlogical variables that are different from the environment variables\nthat can affect the behaviour of git.\n\n> the default branch name that will be used by git-init.\n\n\"git-init\" -> \"git init\", or inside a pair of backquotes, i.e. \"`git init`\".\n\n> Currently this variable is equivalent to\n>     git config init.defaultbranch || 'master'\n>\n> This however will break if at one point the default branch is changed as\n> indicated by `default_branch_name_advice` in `refs.c`.\n>\n> By providing this command ahead of time users of git can make their\n> code forward-compatible.\n\nMakes sense.  Thanks for cleanly explaining the motivation.\n\n> Co-developed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\n\nI would use \"Helped-by:\" here, as I do not want to see one-off\ntrailers invented left and right.\n\n> diff --git a/builtin/var.c b/builtin/var.c\n> index 6c6f46b4ae..d1d82b6c93 100644\n> --- a/builtin/var.c\n> +++ b/builtin/var.c\n> @@ -5,6 +5,7 @@\n>   */\n>  #include \"builtin.h\"\n>  #include \"config.h\"\n> +#include \"refs.h\"\n>  \n>  static const char var_usage[] = \"git var (-l | <variable>)\";\n>  \n> @@ -27,6 +28,17 @@ static const char *pager(int flag)\n>  \treturn pgm;\n>  }\n>  \n> +static const char *default_branch(int flag)\n> +{\n> +\tconst char *name = repo_default_branch_name(the_repository, 1);\n\nCalling\n\n        git_default_branch_name(1)\n\nis much shorter and clear.  It's not like using the_repository is\nalways better.  For a single and simple purpose command like \"git\nvar\" that does not run around multiple repositories and do things\nin them, sticking to the \"we work in _the_ repository given to us\"\nsimple API is better.\n\n> +\tif (!name)\n> +\t\tdie(\"could not determine the default branch name\");\n> +\n> +\treturn name;\n\nShould we even die?  What does \"init\" and \"clone\" do when they ask\nfor the same information and get a NULL pointer?\n\n    ... goes and looks ...\n\nThey know the call cannot fail that way.  So I would do either\n\n (1) follow suit and just return whatever we get back from the API\n     call to the caller (which knows how to handle a NULL return); or\n\n (2) call BUG(\"...\")  instead of die().  The name being NULL at this\n     point means that git_default_branch_name() returned NULL, which\n     the callers do not allow to happen, so it is a BUG for it to\n     return NULL, and this caller noticed it.\n\nI only raise the latter as a possibility.  I think just assuming\nthat name is never NULL like other callers is fine.\n\nThanks.\n"},{"id":"440394","messageId":"20211103185331.599463-1-thomas@t-8ch.de","threadId":"56812","inReplyTo":"xmqqa6il6qxu.fsf@gitster.g","subject":"[PATCH v3] var: add GIT_DEFAULT_BRANCH variable","fromName":"Thomas Weißschuh","fromEmail":"thomas@t-8ch.de","sentAt":"2021-11-03T18:53:31Z","receivedAt":"2021-11-03T18:53:41Z","isPatch":true,"sender":{"key":"thomas@t-8ch.de","avatar":"https://avatars.githubusercontent.com/u/717901?v=4"},"body":"Introduce the logical variable GIT_DEFAULT_BRANCH which represents the\nthe default branch name that will be used by \"git init\".\n\nCurrently this variable is equivalent to\n    git config init.defaultbranch || 'master'\n\nThis however will break if at one point the default branch is changed as\nindicated by `default_branch_name_advice` in `refs.c`.\n\nBy providing this command ahead of time users of git can make their\ncode forward-compatible.\n\nHelped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\nSigned-off-by: Thomas Weißschuh <thomas@t-8ch.de>\n---\n\nChanges from v1 ( https://lore.kernel.org/git/20211030140112.834650-1-thomas@t-8ch.de/ ):\n* Replaced the custom subcommand with an internal variable\n* Cleaned up the tests\n\nChanges from v2 ( https://lore.kernel.org/git/20211102164434.1005707-1-thomas@t-8ch.de/ ):\n* Removed superfluous error handling\n* Switched to better fitting function\n* Reworded commit message to be more consistent with the rest of git\n\n Documentation/git-var.txt |  3 +++\n builtin/var.c             |  7 +++++++\n t/t0007-git-var.sh        | 19 +++++++++++++++++++\n 3 files changed, 29 insertions(+)\n\ndiff --git a/Documentation/git-var.txt b/Documentation/git-var.txt\nindex 6072f936ab..387cc1b914 100644\n--- a/Documentation/git-var.txt\n+++ b/Documentation/git-var.txt\n@@ -59,6 +59,9 @@ ifdef::git-default-pager[]\n     The build you are using chose '{git-default-pager}' as the default.\n endif::git-default-pager[]\n \n+GIT_DEFAULT_BRANCH::\n+    The name of the first branch created in newly initialized repositories.\n+\n SEE ALSO\n --------\n linkgit:git-commit-tree[1]\ndiff --git a/builtin/var.c b/builtin/var.c\nindex 6c6f46b4ae..491db27429 100644\n--- a/builtin/var.c\n+++ b/builtin/var.c\n@@ -5,6 +5,7 @@\n  */\n #include \"builtin.h\"\n #include \"config.h\"\n+#include \"refs.h\"\n \n static const char var_usage[] = \"git var (-l | <variable>)\";\n \n@@ -27,6 +28,11 @@ static const char *pager(int flag)\n \treturn pgm;\n }\n \n+static const char *default_branch(int flag)\n+{\n+\treturn git_default_branch_name(1);\n+}\n+\n struct git_var {\n \tconst char *name;\n \tconst char *(*read)(int);\n@@ -36,6 +42,7 @@ static struct git_var git_vars[] = {\n \t{ \"GIT_AUTHOR_IDENT\",   git_author_info },\n \t{ \"GIT_EDITOR\", editor },\n \t{ \"GIT_PAGER\", pager },\n+\t{ \"GIT_DEFAULT_BRANCH\", default_branch },\n \t{ \"\", NULL },\n };\n \ndiff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh\nindex 53af92d571..6b6852e35e 100755\n--- a/t/t0007-git-var.sh\n+++ b/t/t0007-git-var.sh\n@@ -27,6 +27,25 @@ test_expect_success !FAIL_PREREQS,!AUTOIDENT 'requested identities are strict' '\n \t)\n '\n \n+test_expect_success 'get GIT_DEFAULT_BRANCH without configuration' '\n+\t(\n+\t\tsane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&\n+\t\techo master >expect &&\n+\t\tgit var GIT_DEFAULT_BRANCH >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '\n+\ttest_config init.defaultbranch foo &&\n+\t(\n+\t\tsane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&\n+\t\techo foo >expect &&\n+\t\tgit var GIT_DEFAULT_BRANCH >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n # For git var -l, we check only a representative variable;\n # testing the whole output would make our test too brittle with\n # respect to unrelated changes in the test suite's environment.\n\nbase-commit: 0cddd84c9f3e9c3d793ec93034ef679335f35e49\n-- \n2.33.1\n\n"},{"id":"440397","messageId":"CAPig+cR0PL-XtfC8v4-u1Taq6yOLO=2HSHsNBnzvzLCyM-whZA@mail.gmail.com","threadId":"56812","inReplyTo":"20211103185331.599463-1-thomas@t-8ch.de","subject":"Re: [PATCH v3] var: add GIT_DEFAULT_BRANCH variable","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2021-11-03T19:57:27Z","receivedAt":"2021-11-03T19:57:41Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Wed, Nov 3, 2021 at 2:53 PM Thomas Weißschuh <thomas@t-8ch.de> wrote:\n> Introduce the logical variable GIT_DEFAULT_BRANCH which represents the\n> the default branch name that will be used by \"git init\".\n> [...]\n> Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>\n> ---\n> diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh\n> @@ -27,6 +27,25 @@ test_expect_success !FAIL_PREREQS,!AUTOIDENT 'requested identities are strict' '\n> +test_expect_success 'get GIT_DEFAULT_BRANCH without configuration' '\n> +       (\n> +               sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&\n> +               echo master >expect &&\n> +               git var GIT_DEFAULT_BRANCH >actual &&\n> +               test_cmp expect actual\n> +       )\n> +'\n\nSo that we don't have to worry about this test breaking if the default\nbranch ever is changed, would it make sense to stop hard-coding\n\"master\" here and instead employ the trick that Dscho mentioned\nearlier in the thread (i.e. create a dummy repo and ask it for its\ndefault branch)? Something like this (untested):\n\n    (\n        sane_unset GIT_TEST_... &&\n        git init defbranch &&\n        git -C defbranch symbolic-ref --short HEAD >expect &&\n        git var GIT_DEFAULT_BRANCH >actual &&\n        test_cmp expect actual\n    )\n\n> +test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '\n> +       test_config init.defaultbranch foo &&\n> +       (\n> +               sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&\n> +               echo foo >expect &&\n> +               git var GIT_DEFAULT_BRANCH >actual &&\n> +               test_cmp expect actual\n> +       )\n> +'\n"},{"id":"440398","messageId":"xmqq5yt96m5h.fsf@gitster.g","threadId":"56812","inReplyTo":"CAPig+cR0PL-XtfC8v4-u1Taq6yOLO=2HSHsNBnzvzLCyM-whZA@mail.gmail.com","subject":"Re: [PATCH v3] var: add GIT_DEFAULT_BRANCH variable","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-11-03T20:04:42Z","receivedAt":"2021-11-03T20:04:48Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Eric Sunshine <sunshine@sunshineco.com> writes:\n\n> earlier in the thread (i.e. create a dummy repo and ask it for its\n> default branch)? Something like this (untested):\n>\n>     (\n>         sane_unset GIT_TEST_... &&\n>         git init defbranch &&\n>         git -C defbranch symbolic-ref --short HEAD >expect &&\n>         git var GIT_DEFAULT_BRANCH >actual &&\n>         test_cmp expect actual\n>     )\n\nSo, the idea is to \"observe\" what the init command actually does,\nand see if that matches the behaviour of the var command?\n\nSounds like a good way to ensure correctness to me.\n\nThanks.\n"},{"id":"440399","messageId":"20211103201702.117771-1-thomas@t-8ch.de","threadId":"56812","inReplyTo":"CAPig+cR0PL-XtfC8v4-u1Taq6yOLO=2HSHsNBnzvzLCyM-whZA@mail.gmail.com","subject":"[PATCH v4] var: add GIT_DEFAULT_BRANCH variable","fromName":"Thomas Weißschuh","fromEmail":"thomas@t-8ch.de","sentAt":"2021-11-03T20:17:02Z","receivedAt":"2021-11-03T20:17:10Z","isPatch":true,"sender":{"key":"thomas@t-8ch.de","avatar":"https://avatars.githubusercontent.com/u/717901?v=4"},"body":"Introduce the logical variable GIT_DEFAULT_BRANCH which represents the\nthe default branch name that will be used by \"git init\".\n\nCurrently this variable is equivalent to\n    git config init.defaultbranch || 'master'\n\nThis however will break if at one point the default branch is changed as\nindicated by `default_branch_name_advice` in `refs.c`.\n\nBy providing this command ahead of time users of git can make their\ncode forward-compatible.\n\nHelped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nHelped-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\nSigned-off-by: Thomas Weißschuh <thomas@t-8ch.de>\n---\n\nChanges from v1 ( https://lore.kernel.org/git/20211030140112.834650-1-thomas@t-8ch.de/ ):\n* Replaced the custom subcommand with an internal variable\n* Cleaned up the tests\n\nChanges from v2 ( https://lore.kernel.org/git/20211102164434.1005707-1-thomas@t-8ch.de/ ):\n* Removed superfluous error handling\n* Switched to better fitting function\n* Reworded commit message to be more consistent with the rest of git\n\nChanges from v3 ( https://lore.kernel.org/git/20211103185331.599463-1-thomas@t-8ch.de/ ):\n* Future-proof test by testing against the actual name of a new repository\n\n Documentation/git-var.txt |  3 +++\n builtin/var.c             |  7 +++++++\n t/t0007-git-var.sh        | 20 ++++++++++++++++++++\n 3 files changed, 30 insertions(+)\n\ndiff --git a/Documentation/git-var.txt b/Documentation/git-var.txt\nindex 6072f936ab..387cc1b914 100644\n--- a/Documentation/git-var.txt\n+++ b/Documentation/git-var.txt\n@@ -59,6 +59,9 @@ ifdef::git-default-pager[]\n     The build you are using chose '{git-default-pager}' as the default.\n endif::git-default-pager[]\n \n+GIT_DEFAULT_BRANCH::\n+    The name of the first branch created in newly initialized repositories.\n+\n SEE ALSO\n --------\n linkgit:git-commit-tree[1]\ndiff --git a/builtin/var.c b/builtin/var.c\nindex 6c6f46b4ae..491db27429 100644\n--- a/builtin/var.c\n+++ b/builtin/var.c\n@@ -5,6 +5,7 @@\n  */\n #include \"builtin.h\"\n #include \"config.h\"\n+#include \"refs.h\"\n \n static const char var_usage[] = \"git var (-l | <variable>)\";\n \n@@ -27,6 +28,11 @@ static const char *pager(int flag)\n \treturn pgm;\n }\n \n+static const char *default_branch(int flag)\n+{\n+\treturn git_default_branch_name(1);\n+}\n+\n struct git_var {\n \tconst char *name;\n \tconst char *(*read)(int);\n@@ -36,6 +42,7 @@ static struct git_var git_vars[] = {\n \t{ \"GIT_AUTHOR_IDENT\",   git_author_info },\n \t{ \"GIT_EDITOR\", editor },\n \t{ \"GIT_PAGER\", pager },\n+\t{ \"GIT_DEFAULT_BRANCH\", default_branch },\n \t{ \"\", NULL },\n };\n \ndiff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh\nindex 53af92d571..e56f4b9ac5 100755\n--- a/t/t0007-git-var.sh\n+++ b/t/t0007-git-var.sh\n@@ -27,6 +27,26 @@ test_expect_success !FAIL_PREREQS,!AUTOIDENT 'requested identities are strict' '\n \t)\n '\n \n+test_expect_success 'get GIT_DEFAULT_BRANCH without configuration' '\n+\t(\n+\t\tsane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&\n+\t\tgit init defbranch &&\n+\t\tgit -C defbranch symbolic-ref --short HEAD >expect &&\n+\t\tgit var GIT_DEFAULT_BRANCH >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '\n+\ttest_config init.defaultbranch foo &&\n+\t(\n+\t\tsane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&\n+\t\techo foo >expect &&\n+\t\tgit var GIT_DEFAULT_BRANCH >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n # For git var -l, we check only a representative variable;\n # testing the whole output would make our test too brittle with\n # respect to unrelated changes in the test suite's environment.\n\nbase-commit: 0cddd84c9f3e9c3d793ec93034ef679335f35e49\n-- \n2.33.1\n\n"},{"id":"440400","messageId":"xmqq1r3x6l9q.fsf@gitster.g","threadId":"56812","inReplyTo":"20211103201702.117771-1-thomas@t-8ch.de","subject":"Re: [PATCH v4] var: add GIT_DEFAULT_BRANCH variable","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-11-03T20:23:45Z","receivedAt":"2021-11-03T20:23:51Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thomas Weißschuh <thomas@t-8ch.de> writes:\n\n> Introduce the logical variable GIT_DEFAULT_BRANCH which represents the\n> the default branch name that will be used by \"git init\".\n\nSorry for not realizing this earlier, but we are not reporting the\n\"default\" with this feature.  If you have ~/.gitconfig with your\nfavourite configuration in it, what this reports is the name of the\nbranch created by \"git init\" without the \"--initial-branch=<name>\"\noption.\n\nSo GIT_INITIAL_BRANCH_NAME might be a more appropriate name for the\nvariable from that realization.  But I do not feel too strongly\nabout it, so let's not keep rerolling but see what others think\nfirst.\n\nThanks.  Will queue this version as-is.  The updated test looks good.\n\n> +test_expect_success 'get GIT_DEFAULT_BRANCH without configuration' '\n> +\t(\n> +\t\tsane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&\n> +\t\tgit init defbranch &&\n> +\t\tgit -C defbranch symbolic-ref --short HEAD >expect &&\n> +\t\tgit var GIT_DEFAULT_BRANCH >actual &&\n> +\t\ttest_cmp expect actual\n> +\t)\n> +'\n> +\n> +test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '\n> +\ttest_config init.defaultbranch foo &&\n> +\t(\n> +\t\tsane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&\n> +\t\techo foo >expect &&\n> +\t\tgit var GIT_DEFAULT_BRANCH >actual &&\n> +\t\ttest_cmp expect actual\n> +\t)\n> +'\n> +\n>  # For git var -l, we check only a representative variable;\n>  # testing the whole output would make our test too brittle with\n>  # respect to unrelated changes in the test suite's environment.\n>\n> base-commit: 0cddd84c9f3e9c3d793ec93034ef679335f35e49\n"},{"id":"440410","messageId":"nycvar.QRO.7.76.6.2111040040340.56@tvgsbejvaqbjf.bet","threadId":"56812","inReplyTo":"xmqqy2656tni.fsf@gitster.g","subject":"Re: [PATCH] builtin: add git-default-branch command","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2021-11-03T23:44:46Z","receivedAt":"2021-11-03T23:44:55Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Junio,\n\nOn Wed, 3 Nov 2021, Junio C Hamano wrote:\n\n> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n>\n> > But we do have prior art in Git how to display similar information: `git\n> > var -l` will list e.g. `GIT_PAGER`, even if it is not configured\n> > explicitly.\n>\n> Nice.  I was hoping that somebody would remember this one.\n\nI am glad that at least you remembered what I said ;-)\n\n> GIT_AUTHOR_IDENT and such that do not even exist as a variable is\n> part of \"git var -l\"; the name for the default initial branch falls\n> into the same category.\n\nYep. Likewise, `GIT_PAGER` does not have to exist as an environment\nvariable, and still would be listed in `git var -l`. And `GIT_EDITOR`. I\nknow there are scripts out there looking at `git var` for such (possibly\ndefault) values. That's why `git var -l` is a pretty good place for this\ninformation. Maybe the commit message could grow a paragraph with this\nline of reasoning. Thomas, what do you think?\n\nCiao,\nDscho\n"}]}