All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] builtin: add git-default-branch command
@ 2021-10-30 14:01 Thomas Weißschuh
  2021-10-30 17:18 ` Ævar Arnfjörð Bjarmason
  2021-11-02 13:39 ` Johannes Schindelin
  0 siblings, 2 replies; 18+ messages in thread
From: Thomas Weißschuh @ 2021-10-30 14:01 UTC (permalink / raw)
  To: git; +Cc: Thomas Weißschuh

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`.

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

Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
---
 .gitignore                           |  1 +
 Documentation/git-default-branch.txt | 15 +++++++++++++
 Makefile                             |  1 +
 builtin.h                            |  1 +
 builtin/default-branch.c             | 33 ++++++++++++++++++++++++++++
 command-list.txt                     |  1 +
 git.c                                |  1 +
 t/t1417-default-branch.sh            | 21 ++++++++++++++++++
 8 files changed, 74 insertions(+)
 create mode 100644 Documentation/git-default-branch.txt
 create mode 100644 builtin/default-branch.c
 create mode 100755 t/t1417-default-branch.sh

diff --git a/.gitignore b/.gitignore
index 311841f9be..d9b969dc7e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -53,6 +53,7 @@
 /git-cvsimport
 /git-cvsserver
 /git-daemon
+/git-default-branch
 /git-diff
 /git-diff-files
 /git-diff-index
diff --git a/Documentation/git-default-branch.txt b/Documentation/git-default-branch.txt
new file mode 100644
index 0000000000..d5b891e5b4
--- /dev/null
+++ b/Documentation/git-default-branch.txt
@@ -0,0 +1,15 @@
+git-default-branch(1)
+===================
+
+NAME
+----
+git-default-branch - Read the default branch ref used by git
+
+SYNOPSIS
+--------
+[verse]
+'git default-branch'
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/Makefile b/Makefile
index d1feab008f..49276359ab 100644
--- a/Makefile
+++ b/Makefile
@@ -1091,6 +1091,7 @@ BUILTIN_OBJS += builtin/credential-cache.o
 BUILTIN_OBJS += builtin/credential-store.o
 BUILTIN_OBJS += builtin/credential.o
 BUILTIN_OBJS += builtin/describe.o
+BUILTIN_OBJS += builtin/default-branch.o
 BUILTIN_OBJS += builtin/diff-files.o
 BUILTIN_OBJS += builtin/diff-index.o
 BUILTIN_OBJS += builtin/diff-tree.o
diff --git a/builtin.h b/builtin.h
index 16ecd5586f..65b41e4c1f 100644
--- a/builtin.h
+++ b/builtin.h
@@ -143,6 +143,7 @@ int cmd_credential(int argc, const char **argv, const char *prefix);
 int cmd_credential_cache(int argc, const char **argv, const char *prefix);
 int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix);
 int cmd_credential_store(int argc, const char **argv, const char *prefix);
+int cmd_default_branch(int argc, const char **argv, const char *prefix);
 int cmd_describe(int argc, const char **argv, const char *prefix);
 int cmd_diff_files(int argc, const char **argv, const char *prefix);
 int cmd_diff_index(int argc, const char **argv, const char *prefix);
diff --git a/builtin/default-branch.c b/builtin/default-branch.c
new file mode 100644
index 0000000000..e74c078926
--- /dev/null
+++ b/builtin/default-branch.c
@@ -0,0 +1,33 @@
+#include "builtin.h"
+#include "config.h"
+#include "refs.h"
+#include "parse-options.h"
+
+static const char * const git_default_branch_usage[] = {
+	N_("git default-branch"),
+	NULL
+};
+
+
+int cmd_default_branch(int argc, const char **argv, const char *prefix)
+{
+	char *name;
+
+	struct option options[] = {
+		OPT_END(),
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_default_branch_usage, 0);
+
+	if (argc != 0)
+		usage_with_options(git_default_branch_usage, options);
+
+	name = repo_default_branch_name(the_repository, 1);
+
+	if (!name)
+		die("Could not fetch default branch name");
+
+	puts(name);
+	return 0;
+}
diff --git a/command-list.txt b/command-list.txt
index a289f09ed6..950fa9a993 100644
--- a/command-list.txt
+++ b/command-list.txt
@@ -81,6 +81,7 @@ git-cvsexportcommit                     foreignscminterface
 git-cvsimport                           foreignscminterface
 git-cvsserver                           foreignscminterface
 git-daemon                              synchingrepositories
+git-default-branch                      plumbinginterrogators
 git-describe                            mainporcelain
 git-diff                                mainporcelain           info
 git-diff-files                          plumbinginterrogators
diff --git a/git.c b/git.c
index 18bed9a996..112f37a7f3 100644
--- a/git.c
+++ b/git.c
@@ -516,6 +516,7 @@ static struct cmd_struct commands[] = {
 	{ "credential-cache", cmd_credential_cache },
 	{ "credential-cache--daemon", cmd_credential_cache_daemon },
 	{ "credential-store", cmd_credential_store },
+	{ "default-branch", cmd_default_branch, RUN_SETUP_GENTLY },
 	{ "describe", cmd_describe, RUN_SETUP },
 	{ "diff", cmd_diff, NO_PARSEOPT },
 	{ "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
diff --git a/t/t1417-default-branch.sh b/t/t1417-default-branch.sh
new file mode 100755
index 0000000000..d81f1ee214
--- /dev/null
+++ b/t/t1417-default-branch.sh
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+test_description='Test git default-branch'
+
+. ./test-lib.sh
+
+unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+test_expect_success 'without configuration' '
+	b=$(git default-branch) &&
+	verbose test "$b" = master
+'
+
+test_expect_success 'with configuration' '
+	git config init.defaultbranch foo &&
+	b=$(git default-branch) &&
+	echo $b &&
+	verbose test "$b" = foo
+'
+
+test_done

base-commit: 6c40894d2466d4e7fddc047a05116aa9d14712ee
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] builtin: add git-default-branch command
  2021-10-30 14:01 [PATCH] builtin: add git-default-branch command Thomas Weißschuh
@ 2021-10-30 17:18 ` Ævar Arnfjörð Bjarmason
  2021-11-02 13:39 ` Johannes Schindelin
  1 sibling, 0 replies; 18+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-10-30 17:18 UTC (permalink / raw)
  To: Thomas Weißschuh; +Cc: git


On Sat, Oct 30 2021, Thomas Weißschuh wrote:

> 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`.
>
> By providing this command ahead of time users of git can make their
> code forward-compatible.

Recently there was a discussion on a similar topic, i.e. to have git
explicitly aware of "default config" as far as "git config -l" etc. go:

    https://lore.kernel.org/git/87czvoowg2.fsf@evledraar.gmail.com/

I'd much rather see as add this as some mode of git-config, even if it's
a new --get-or-git-default switch:

    git config --get-or-git-default init.defaultBranch

That would just currently die if you fed it any other value than
init.defaultBranch, i.e. that (or similar) would be a generic enough
interface that we could expand on it.

Whereas having a new-built in just for this one config variable...

> +test_expect_success 'without configuration' '
> +	b=$(git default-branch) &&
> +	verbose test "$b" = master
> +'
> +
> +test_expect_success 'with configuration' '
> +	git config init.defaultbranch foo &&
> +	b=$(git default-branch) &&
> +	echo $b &&
> +	verbose test "$b" = foo
> +'
> +

Should lose the echo, and these comparisons should be using test_cmp,
also use "test_config" not "git config" unless in a sub-repo or similar.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] builtin: add git-default-branch command
  2021-10-30 14:01 [PATCH] builtin: add git-default-branch command Thomas Weißschuh
  2021-10-30 17:18 ` Ævar Arnfjörð Bjarmason
@ 2021-11-02 13:39 ` Johannes Schindelin
  2021-11-02 16:44   ` [PATCH v2] var: add GIT_DEFAULT_BRANCH variable Thomas Weißschuh
  2021-11-03 17:22   ` [PATCH] builtin: add git-default-branch command Junio C Hamano
  1 sibling, 2 replies; 18+ messages in thread
From: Johannes Schindelin @ 2021-11-02 13:39 UTC (permalink / raw)
  To: Thomas Weißschuh; +Cc: git

[-- Attachment #1: Type: text/plain, Size: 1967 bytes --]

Hi Thomas,

On Sat, 30 Oct 2021, Thomas Weißschuh wrote:

> 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

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2] var: add GIT_DEFAULT_BRANCH variable
  2021-11-02 13:39 ` Johannes Schindelin
@ 2021-11-02 16:44   ` Thomas Weißschuh
  2021-11-02 16:53     ` Ævar Arnfjörð Bjarmason
  2021-11-03 18:21     ` Junio C Hamano
  2021-11-03 17:22   ` [PATCH] builtin: add git-default-branch command Junio C Hamano
  1 sibling, 2 replies; 18+ messages in thread
From: Thomas Weißschuh @ 2021-11-02 16:44 UTC (permalink / raw)
  To: git
  Cc: Thomas Weißschuh, Johannes Schindelin,
	Ævar Arnfjörð Bjarmason

Introduce the builtin variable GIT_DEFAULT_BRANCH which represents the
the default branch name that will be used by git-init.

Currently this variable 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`.

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

Co-developed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
---

Changes from v1 ( https://lore.kernel.org/git/20211030140112.834650-1-thomas@t-8ch.de/ ):
* Replaced the custom subcommand with an internal variable
* Cleaned up the tests

@Johannes: I replaced BUG() with die() from your example because that seems to be
nicer for user facing messages.

 Documentation/git-var.txt |  3 +++
 builtin/var.c             | 13 +++++++++++++
 t/t0007-git-var.sh        | 19 +++++++++++++++++++
 3 files changed, 35 insertions(+)

diff --git a/Documentation/git-var.txt b/Documentation/git-var.txt
index 6072f936ab..387cc1b914 100644
--- a/Documentation/git-var.txt
+++ b/Documentation/git-var.txt
@@ -59,6 +59,9 @@ ifdef::git-default-pager[]
     The build you are using chose '{git-default-pager}' as the default.
 endif::git-default-pager[]
 
+GIT_DEFAULT_BRANCH::
+    The name of the first branch created in newly initialized repositories.
+
 SEE ALSO
 --------
 linkgit:git-commit-tree[1]
diff --git a/builtin/var.c b/builtin/var.c
index 6c6f46b4ae..d1d82b6c93 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,17 @@ 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)
+		die("could not determine the default branch name");
+
+	return name;
+}
+
+
 struct git_var {
 	const char *name;
 	const char *(*read)(int);
@@ -36,6 +48,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 },
 };
 
diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh
index 53af92d571..6b6852e35e 100755
--- a/t/t0007-git-var.sh
+++ b/t/t0007-git-var.sh
@@ -27,6 +27,25 @@ test_expect_success !FAIL_PREREQS,!AUTOIDENT 'requested identities are strict' '
 	)
 '
 
+test_expect_success 'get GIT_DEFAULT_BRANCH without configuration' '
+	(
+		sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
+		echo master >expect &&
+		git var GIT_DEFAULT_BRANCH >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '
+	test_config init.defaultbranch foo &&
+	(
+		sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
+		echo foo >expect &&
+		git var GIT_DEFAULT_BRANCH >actual &&
+		test_cmp expect actual
+	)
+'
+
 # For git var -l, we check only a representative variable;
 # testing the whole output would make our test too brittle with
 # respect to unrelated changes in the test suite's environment.

base-commit: 0cddd84c9f3e9c3d793ec93034ef679335f35e49
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] var: add GIT_DEFAULT_BRANCH variable
  2021-11-02 16:44   ` [PATCH v2] var: add GIT_DEFAULT_BRANCH variable Thomas Weißschuh
@ 2021-11-02 16:53     ` Ævar Arnfjörð Bjarmason
  2021-11-02 17:35       ` Thomas Weißschuh
  2021-11-03 11:37       ` Jeff King
  2021-11-03 18:21     ` Junio C Hamano
  1 sibling, 2 replies; 18+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-11-02 16:53 UTC (permalink / raw)
  To: Thomas Weißschuh; +Cc: git, Johannes Schindelin


On Tue, Nov 02 2021, Thomas Weißschuh wrote:

> Introduce the builtin variable GIT_DEFAULT_BRANCH which represents the
> the default branch name that will be used by git-init.
>
> Currently this variable 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`.
>
> By providing this command ahead of time users of git can make their
> code forward-compatible.
>
> Co-developed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
> ---
>
> Changes from v1 ( https://lore.kernel.org/git/20211030140112.834650-1-thomas@t-8ch.de/ ):
> * Replaced the custom subcommand with an internal variable
> * Cleaned up the tests
>
> @Johannes: I replaced BUG() with die() from your example because that seems to be
> nicer for user facing messages.
>
>  Documentation/git-var.txt |  3 +++
>  builtin/var.c             | 13 +++++++++++++
>  t/t0007-git-var.sh        | 19 +++++++++++++++++++
>  3 files changed, 35 insertions(+)
>
> diff --git a/Documentation/git-var.txt b/Documentation/git-var.txt
> index 6072f936ab..387cc1b914 100644
> --- a/Documentation/git-var.txt
> +++ b/Documentation/git-var.txt
> @@ -59,6 +59,9 @@ ifdef::git-default-pager[]
>      The build you are using chose '{git-default-pager}' as the default.
>  endif::git-default-pager[]
>  
> +GIT_DEFAULT_BRANCH::
> +    The name of the first branch created in newly initialized repositories.
> +
>  SEE ALSO
>  --------
>  linkgit:git-commit-tree[1]
> diff --git a/builtin/var.c b/builtin/var.c
> index 6c6f46b4ae..d1d82b6c93 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,17 @@ 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)
> +		die("could not determine the default branch name");

Isn't this die() unrechable given the similar logic in
repo_default_branch_name()? Hence the previous BUG(...)?

I really don't see how it makes sense to add this to "git var", we have
that to correspond to environment variables we use.

*Maybe* if we renamed GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME to
GIT_INITIAL_BRANCH_NAME and made it a non-test thing like
GIT_TEMPLATE_DIR, but even then shouldn't we be adding
"GIT_TEMPLATE_DIR" and any number of other things to this as well?

I'm not saying that your patch needs to do that, but we really should
think about the interface & future implications if we're going in this
direction.

The reason I suggested extending "git config" in [1] is because it seems
like a natural thing for "git config" to learn to spew out our idea of
default hardcoded config values to the user.

But creating a variable form of that existing config just so we can have
"git var" spew it out just seems weird.

We don't have or need such a variable now for anything else, so why go
through that indirection, instead of something that closes the feature
gap of asking what a config variable default is?

In any case whatever we do here this really should be updating the
documentation of init.defaultbranch & the relevant bits in the "git
init" manpage to add cross-references, similar to how we discuss
GIT_TEMPLATE_DIR now.

1. https://lore.kernel.org/git/211030.86ilxe4edm.gmgdl@evledraar.gmail.com/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] var: add GIT_DEFAULT_BRANCH variable
  2021-11-02 16:53     ` Ævar Arnfjörð Bjarmason
@ 2021-11-02 17:35       ` Thomas Weißschuh
  2021-11-02 19:14         ` Ævar Arnfjörð Bjarmason
  2021-11-03 11:37       ` Jeff King
  1 sibling, 1 reply; 18+ messages in thread
From: Thomas Weißschuh @ 2021-11-02 17:35 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: git, Johannes Schindelin

On 2021-11-02 17:53+0100, Ævar Arnfjörð Bjarmason wrote:
> On Tue, Nov 02 2021, Thomas Weißschuh wrote:
> 
> > Introduce the builtin variable GIT_DEFAULT_BRANCH which represents the
> > the default branch name that will be used by git-init.
> >
> > Currently this variable 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`.
> >
> > By providing this command ahead of time users of git can make their
> > code forward-compatible.
> >
> > Co-developed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> > Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
> > ---
> >
> > Changes from v1 ( https://lore.kernel.org/git/20211030140112.834650-1-thomas@t-8ch.de/ ):
> > * Replaced the custom subcommand with an internal variable
> > * Cleaned up the tests
> >
> > @Johannes: I replaced BUG() with die() from your example because that seems to be
> > nicer for user facing messages.
> >
> >  Documentation/git-var.txt |  3 +++
> >  builtin/var.c             | 13 +++++++++++++
> >  t/t0007-git-var.sh        | 19 +++++++++++++++++++
> >  3 files changed, 35 insertions(+)
> >
> >  
> > +static const char *default_branch(int flag)
> > +{
> > +	const char *name = repo_default_branch_name(the_repository, 1);
> > +
> > +	if (!name)
> > +		die("could not determine the default branch name");
> 
> Isn't this die() unrechable given the similar logic in
> repo_default_branch_name()? Hence the previous BUG(...)?

Ok. Good point.

> I really don't see how it makes sense to add this to "git var", we have
> that to correspond to environment variables we use.
> 
> *Maybe* if we renamed GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME to
> GIT_INITIAL_BRANCH_NAME and made it a non-test thing like
> GIT_TEMPLATE_DIR, but even then shouldn't we be adding
> "GIT_TEMPLATE_DIR" and any number of other things to this as well?
> 
> I'm not saying that your patch needs to do that, but we really should
> think about the interface & future implications if we're going in this
> direction.
> 
> The reason I suggested extending "git config" in [1] is because it seems
> like a natural thing for "git config" to learn to spew out our idea of
> default hardcoded config values to the user.
> 
> But creating a variable form of that existing config just so we can have
> "git var" spew it out just seems weird.
> 
> We don't have or need such a variable now for anything else, so why go
> through that indirection, instead of something that closes the feature
> gap of asking what a config variable default is?
> 
> In any case whatever we do here this really should be updating the
> documentation of init.defaultbranch & the relevant bits in the "git
> init" manpage to add cross-references, similar to how we discuss
> GIT_TEMPLATE_DIR now.
>
> 1. https://lore.kernel.org/git/211030.86ilxe4edm.gmgdl@evledraar.gmail.com/

I'll then wait for a consensus of the git devs. The actual implementation
shouldn't be the issue afterwards.

Thanks for looking into this!

Thomas

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] var: add GIT_DEFAULT_BRANCH variable
  2021-11-02 17:35       ` Thomas Weißschuh
@ 2021-11-02 19:14         ` Ævar Arnfjörð Bjarmason
  2021-11-02 20:08           ` Thomas Weißschuh
  0 siblings, 1 reply; 18+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-11-02 19:14 UTC (permalink / raw)
  To: Thomas Weißschuh; +Cc: git, Johannes Schindelin


On Tue, Nov 02 2021, Thomas Weißschuh wrote:

> On 2021-11-02 17:53+0100, Ævar Arnfjörð Bjarmason wrote:
>> On Tue, Nov 02 2021, Thomas Weißschuh wrote:
>> 
>> > Introduce the builtin variable GIT_DEFAULT_BRANCH which represents the
>> > the default branch name that will be used by git-init.
>> >
>> > Currently this variable 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`.
>> >
>> > By providing this command ahead of time users of git can make their
>> > code forward-compatible.
>> >
>> > Co-developed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
>> > Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
>> > ---
>> >
>> > Changes from v1 ( https://lore.kernel.org/git/20211030140112.834650-1-thomas@t-8ch.de/ ):
>> > * Replaced the custom subcommand with an internal variable
>> > * Cleaned up the tests
>> >
>> > @Johannes: I replaced BUG() with die() from your example because that seems to be
>> > nicer for user facing messages.
>> >
>> >  Documentation/git-var.txt |  3 +++
>> >  builtin/var.c             | 13 +++++++++++++
>> >  t/t0007-git-var.sh        | 19 +++++++++++++++++++
>> >  3 files changed, 35 insertions(+)
>> >
>> >  
>> > +static const char *default_branch(int flag)
>> > +{
>> > +	const char *name = repo_default_branch_name(the_repository, 1);
>> > +
>> > +	if (!name)
>> > +		die("could not determine the default branch name");
>> 
>> Isn't this die() unrechable given the similar logic in
>> repo_default_branch_name()? Hence the previous BUG(...)?
>
> Ok. Good point.
>
>> I really don't see how it makes sense to add this to "git var", we have
>> that to correspond to environment variables we use.
>> 
>> *Maybe* if we renamed GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME to
>> GIT_INITIAL_BRANCH_NAME and made it a non-test thing like
>> GIT_TEMPLATE_DIR, but even then shouldn't we be adding
>> "GIT_TEMPLATE_DIR" and any number of other things to this as well?
>> 
>> I'm not saying that your patch needs to do that, but we really should
>> think about the interface & future implications if we're going in this
>> direction.
>> 
>> The reason I suggested extending "git config" in [1] is because it seems
>> like a natural thing for "git config" to learn to spew out our idea of
>> default hardcoded config values to the user.
>> 
>> But creating a variable form of that existing config just so we can have
>> "git var" spew it out just seems weird.
>> 
>> We don't have or need such a variable now for anything else, so why go
>> through that indirection, instead of something that closes the feature
>> gap of asking what a config variable default is?
>> 
>> In any case whatever we do here this really should be updating the
>> documentation of init.defaultbranch & the relevant bits in the "git
>> init" manpage to add cross-references, similar to how we discuss
>> GIT_TEMPLATE_DIR now.
>>
>> 1. https://lore.kernel.org/git/211030.86ilxe4edm.gmgdl@evledraar.gmail.com/
>
> I'll then wait for a consensus of the git devs. The actual implementation
> shouldn't be the issue afterwards.
>
> Thanks for looking into this!

Please don't take that message as me or anyone else "pulling rank" just
because we've got some previous commits in git.git. That applies to both
me and Johannes, and clearly we disagree on this minor bit of UX
direction.

I'd think if anything the opinion of someone who's not overly familiar
with the system would be more valuable, i.e. yours, especially since you
tried & failed to find a way to do this recently. Would you find it more
intuitive to look in say "git var" over "git config" for this sort of
information?

A further weirdness is that another effective source of config for this
is the "unborn" ls-refs feature[1]. I'm not sure what that means for any
query interface, i.e. would a user want to know what branch a freshly
cloned repo would end up with in advance, taking into account all of the
local config, remote "unborn" etc?

1. https://lore.kernel.org/git/878s8apthr.fsf@evledraar.gmail.com/

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] var: add GIT_DEFAULT_BRANCH variable
  2021-11-02 19:14         ` Ævar Arnfjörð Bjarmason
@ 2021-11-02 20:08           ` Thomas Weißschuh
  0 siblings, 0 replies; 18+ messages in thread
From: Thomas Weißschuh @ 2021-11-02 20:08 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: git, Johannes Schindelin

On 2021-11-02 20:14+0100, Ævar Arnfjörð Bjarmason wrote:
> On Tue, Nov 02 2021, Thomas Weißschuh wrote:
> 
> > On 2021-11-02 17:53+0100, Ævar Arnfjörð Bjarmason wrote:
> >> On Tue, Nov 02 2021, Thomas Weißschuh wrote:
> >> 
> >> > Introduce the builtin variable GIT_DEFAULT_BRANCH which represents the
> >> > the default branch name that will be used by git-init.
> >> >
> >> > Currently this variable 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`.
> >> >
> >> > By providing this command ahead of time users of git can make their
> >> > code forward-compatible.
> >> >
> >> > Co-developed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> >> > Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
> >> > ---
> >> >
> >> > Changes from v1 ( https://lore.kernel.org/git/20211030140112.834650-1-thomas@t-8ch.de/ ):
> >> > * Replaced the custom subcommand with an internal variable
> >> > * Cleaned up the tests
> >> >
> >> > @Johannes: I replaced BUG() with die() from your example because that seems to be
> >> > nicer for user facing messages.
> >> >
> >> >  Documentation/git-var.txt |  3 +++
> >> >  builtin/var.c             | 13 +++++++++++++
> >> >  t/t0007-git-var.sh        | 19 +++++++++++++++++++
> >> >  3 files changed, 35 insertions(+)
> >> >
> >> >  
> >> > +static const char *default_branch(int flag)
> >> > +{
> >> > +	const char *name = repo_default_branch_name(the_repository, 1);
> >> > +
> >> > +	if (!name)
> >> > +		die("could not determine the default branch name");
> >> 
> >> Isn't this die() unrechable given the similar logic in
> >> repo_default_branch_name()? Hence the previous BUG(...)?
> >
> > Ok. Good point.
> >
> >> I really don't see how it makes sense to add this to "git var", we have
> >> that to correspond to environment variables we use.
> >> 
> >> *Maybe* if we renamed GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME to
> >> GIT_INITIAL_BRANCH_NAME and made it a non-test thing like
> >> GIT_TEMPLATE_DIR, but even then shouldn't we be adding
> >> "GIT_TEMPLATE_DIR" and any number of other things to this as well?
> >> 
> >> I'm not saying that your patch needs to do that, but we really should
> >> think about the interface & future implications if we're going in this
> >> direction.
> >> 
> >> The reason I suggested extending "git config" in [1] is because it seems
> >> like a natural thing for "git config" to learn to spew out our idea of
> >> default hardcoded config values to the user.
> >> 
> >> But creating a variable form of that existing config just so we can have
> >> "git var" spew it out just seems weird.
> >> 
> >> We don't have or need such a variable now for anything else, so why go
> >> through that indirection, instead of something that closes the feature
> >> gap of asking what a config variable default is?
> >> 
> >> In any case whatever we do here this really should be updating the
> >> documentation of init.defaultbranch & the relevant bits in the "git
> >> init" manpage to add cross-references, similar to how we discuss
> >> GIT_TEMPLATE_DIR now.
> >>
> >> 1. https://lore.kernel.org/git/211030.86ilxe4edm.gmgdl@evledraar.gmail.com/
> >
> > I'll then wait for a consensus of the git devs. The actual implementation
> > shouldn't be the issue afterwards.
> >
> > Thanks for looking into this!
> 
> Please don't take that message as me or anyone else "pulling rank" just
> because we've got some previous commits in git.git. That applies to both
> me and Johannes, and clearly we disagree on this minor bit of UX
> direction.

This was not my impression, although my message has a bit of a resignated
tone. That was not the intention.

> I'd think if anything the opinion of someone who's not overly familiar
> with the system would be more valuable, i.e. yours, especially since you
> tried & failed to find a way to do this recently. Would you find it more
> intuitive to look in say "git var" over "git config" for this sort of
> information?

To be honest I was not aware of "git var" before Johannes before proposed it.
And I am still not sure how to understand the "logical" aspect of "git-var".
(git-var - Show a Git logical variable)

A "git config" variable using a generic config default framework looks like the
generally cleanest interface.
The appeal of "git var" was the easy and quick implementation.

> A further weirdness is that another effective source of config for this
> is the "unborn" ls-refs feature[1]. I'm not sure what that means for any
> query interface, i.e. would a user want to know what branch a freshly
> cloned repo would end up with in advance, taking into account all of the
> local config, remote "unborn" etc?

I have no idea how that should work.

> 1. https://lore.kernel.org/git/878s8apthr.fsf@evledraar.gmail.com/

If you and Johannes think it would help the design-process I would also
volunteer to implement your original proposal:

    git config --get-or-git-default init.defaultBranch

Please note that I'm not doing (primarily) doing this to get commits into
git.git, so if somebody with more knowledge about the git architecture wants to
bring this forward, please feel free.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] var: add GIT_DEFAULT_BRANCH variable
  2021-11-02 16:53     ` Ævar Arnfjörð Bjarmason
  2021-11-02 17:35       ` Thomas Weißschuh
@ 2021-11-03 11:37       ` Jeff King
  2021-11-03 16:48         ` Eric Sunshine
  1 sibling, 1 reply; 18+ messages in thread
From: Jeff King @ 2021-11-03 11:37 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: Thomas Weißschuh, git, Johannes Schindelin

On Tue, Nov 02, 2021 at 05:53:46PM +0100, Ævar Arnfjörð Bjarmason wrote:

> The reason I suggested extending "git config" in [1] is because it seems
> like a natural thing for "git config" to learn to spew out our idea of
> default hardcoded config values to the user.
> 
> But creating a variable form of that existing config just so we can have
> "git var" spew it out just seems weird.
> 
> We don't have or need such a variable now for anything else, so why go
> through that indirection, instead of something that closes the feature
> gap of asking what a config variable default is?

To me, the point is that the user is not asking "what is the default
value of this config variable?". They are asking "if I were to init a
new repository, what would Git decide the default branch name is?".

Right now that is very tied to the config mechanism (modulo the
GIT_TEST_* bits, but I think we can ignore those for regular users), so
those two are basically the same question. But it doesn't have to be.
Abstracting it to the question the user actually wants to ask
future-proofs the mechanism.

I.e., I don't think introducing a new variable into "git var" is that
big a deal. They don't have to be related to an environment variable;
the documentation calls them "logical variables". This is exactly the
kind of thing it's designed for.

-Peff

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] var: add GIT_DEFAULT_BRANCH variable
  2021-11-03 11:37       ` Jeff King
@ 2021-11-03 16:48         ` Eric Sunshine
  0 siblings, 0 replies; 18+ messages in thread
From: Eric Sunshine @ 2021-11-03 16:48 UTC (permalink / raw)
  To: Jeff King
  Cc: Ævar Arnfjörð Bjarmason, Thomas Weißschuh,
	Git List, Johannes Schindelin

On Wed, Nov 3, 2021 at 7:37 AM Jeff King <peff@peff.net> wrote:
> I.e., I don't think introducing a new variable into "git var" is that
> big a deal. They don't have to be related to an environment variable;
> the documentation calls them "logical variables". This is exactly the
> kind of thing it's designed for.

I almost don't want to mention it since it's not very discoverable
(and is never the first place I think to look), but git-rev-parse
already contains a grab-bag of options unrelated to revision parsing.
Many of the options relate to querying paths (--show-toplevel,
--show-cdup, etc.) or other information related to a repository
(--local-env-vars, --show-object-format, etc.). So, adding
--show-default-branch may be one possibility.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] builtin: add git-default-branch command
  2021-11-02 13:39 ` Johannes Schindelin
  2021-11-02 16:44   ` [PATCH v2] var: add GIT_DEFAULT_BRANCH variable Thomas Weißschuh
@ 2021-11-03 17:22   ` Junio C Hamano
  2021-11-03 23:44     ` Johannes Schindelin
  1 sibling, 1 reply; 18+ messages in thread
From: Junio C Hamano @ 2021-11-03 17:22 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Thomas Weißschuh, git

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> 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.

Nice.  I was hoping that somebody would remember this one.

GIT_AUTHOR_IDENT and such that do not even exist as a variable is
part of "git var -l"; the name for the default initial branch falls
into the same category.

Thanks.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2] var: add GIT_DEFAULT_BRANCH variable
  2021-11-02 16:44   ` [PATCH v2] var: add GIT_DEFAULT_BRANCH variable Thomas Weißschuh
  2021-11-02 16:53     ` Ævar Arnfjörð Bjarmason
@ 2021-11-03 18:21     ` Junio C Hamano
  2021-11-03 18:53       ` [PATCH v3] " Thomas Weißschuh
  1 sibling, 1 reply; 18+ messages in thread
From: Junio C Hamano @ 2021-11-03 18:21 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: git, Johannes Schindelin, Ævar Arnfjörð Bjarmason

Thomas Weißschuh <thomas@t-8ch.de> writes:

> Introduce the builtin variable GIT_DEFAULT_BRANCH which represents the

"builtin" -> "logical", as that is how "git-var" describes these things.

It is totally outside the scope of this patch, but I think we'd
better think of a way to make it clear to the readers of the
documentation that it would not do anything if they did something
like:

    $ GIT_DEFAULT_BRANCH=foobar git init

I say this is outside the scope because there are other existing
logical variables that are different from the environment variables
that can affect the behaviour of git.

> the default branch name that will be used by git-init.

"git-init" -> "git init", or inside a pair of backquotes, i.e. "`git init`".

> Currently this variable 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`.
>
> By providing this command ahead of time users of git can make their
> code forward-compatible.

Makes sense.  Thanks for cleanly explaining the motivation.

> Co-developed-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>

I would use "Helped-by:" here, as I do not want to see one-off
trailers invented left and right.

> diff --git a/builtin/var.c b/builtin/var.c
> index 6c6f46b4ae..d1d82b6c93 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,17 @@ 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);

Calling

        git_default_branch_name(1)

is much shorter and clear.  It's not like using the_repository is
always better.  For a single and simple purpose command like "git
var" that does not run around multiple repositories and do things
in them, sticking to the "we work in _the_ repository given to us"
simple API is better.

> +	if (!name)
> +		die("could not determine the default branch name");
> +
> +	return name;

Should we even die?  What does "init" and "clone" do when they ask
for the same information and get a NULL pointer?

    ... goes and looks ...

They know the call cannot fail that way.  So I would do either

 (1) follow suit and just return whatever we get back from the API
     call to the caller (which knows how to handle a NULL return); or

 (2) call BUG("...")  instead of die().  The name being NULL at this
     point means that git_default_branch_name() returned NULL, which
     the callers do not allow to happen, so it is a BUG for it to
     return NULL, and this caller noticed it.

I only raise the latter as a possibility.  I think just assuming
that name is never NULL like other callers is fine.

Thanks.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v3] var: add GIT_DEFAULT_BRANCH variable
  2021-11-03 18:21     ` Junio C Hamano
@ 2021-11-03 18:53       ` Thomas Weißschuh
  2021-11-03 19:57         ` Eric Sunshine
  0 siblings, 1 reply; 18+ messages in thread
From: Thomas Weißschuh @ 2021-11-03 18:53 UTC (permalink / raw)
  To: git
  Cc: Thomas Weißschuh, Johannes Schindelin,
	Ævar Arnfjörð Bjarmason, Jeff King, Eric Sunshine,
	Junio C Hamano

Introduce the logical variable GIT_DEFAULT_BRANCH which represents the
the default branch name that will be used by "git init".

Currently this variable 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`.

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

Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
---

Changes from v1 ( https://lore.kernel.org/git/20211030140112.834650-1-thomas@t-8ch.de/ ):
* Replaced the custom subcommand with an internal variable
* Cleaned up the tests

Changes from v2 ( https://lore.kernel.org/git/20211102164434.1005707-1-thomas@t-8ch.de/ ):
* Removed superfluous error handling
* Switched to better fitting function
* Reworded commit message to be more consistent with the rest of git

 Documentation/git-var.txt |  3 +++
 builtin/var.c             |  7 +++++++
 t/t0007-git-var.sh        | 19 +++++++++++++++++++
 3 files changed, 29 insertions(+)

diff --git a/Documentation/git-var.txt b/Documentation/git-var.txt
index 6072f936ab..387cc1b914 100644
--- a/Documentation/git-var.txt
+++ b/Documentation/git-var.txt
@@ -59,6 +59,9 @@ ifdef::git-default-pager[]
     The build you are using chose '{git-default-pager}' as the default.
 endif::git-default-pager[]
 
+GIT_DEFAULT_BRANCH::
+    The name of the first branch created in newly initialized repositories.
+
 SEE ALSO
 --------
 linkgit:git-commit-tree[1]
diff --git a/builtin/var.c b/builtin/var.c
index 6c6f46b4ae..491db27429 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,11 @@ static const char *pager(int flag)
 	return pgm;
 }
 
+static const char *default_branch(int flag)
+{
+	return git_default_branch_name(1);
+}
+
 struct git_var {
 	const char *name;
 	const char *(*read)(int);
@@ -36,6 +42,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 },
 };
 
diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh
index 53af92d571..6b6852e35e 100755
--- a/t/t0007-git-var.sh
+++ b/t/t0007-git-var.sh
@@ -27,6 +27,25 @@ test_expect_success !FAIL_PREREQS,!AUTOIDENT 'requested identities are strict' '
 	)
 '
 
+test_expect_success 'get GIT_DEFAULT_BRANCH without configuration' '
+	(
+		sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
+		echo master >expect &&
+		git var GIT_DEFAULT_BRANCH >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '
+	test_config init.defaultbranch foo &&
+	(
+		sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
+		echo foo >expect &&
+		git var GIT_DEFAULT_BRANCH >actual &&
+		test_cmp expect actual
+	)
+'
+
 # For git var -l, we check only a representative variable;
 # testing the whole output would make our test too brittle with
 # respect to unrelated changes in the test suite's environment.

base-commit: 0cddd84c9f3e9c3d793ec93034ef679335f35e49
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v3] var: add GIT_DEFAULT_BRANCH variable
  2021-11-03 18:53       ` [PATCH v3] " Thomas Weißschuh
@ 2021-11-03 19:57         ` Eric Sunshine
  2021-11-03 20:04           ` Junio C Hamano
  2021-11-03 20:17           ` [PATCH v4] " Thomas Weißschuh
  0 siblings, 2 replies; 18+ messages in thread
From: Eric Sunshine @ 2021-11-03 19:57 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Git List, Johannes Schindelin,
	Ævar Arnfjörð Bjarmason, Jeff King,
	Junio C Hamano

On Wed, Nov 3, 2021 at 2:53 PM Thomas Weißschuh <thomas@t-8ch.de> wrote:
> Introduce the logical variable GIT_DEFAULT_BRANCH which represents the
> the default branch name that will be used by "git init".
> [...]
> Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
> ---
> diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh
> @@ -27,6 +27,25 @@ test_expect_success !FAIL_PREREQS,!AUTOIDENT 'requested identities are strict' '
> +test_expect_success 'get GIT_DEFAULT_BRANCH without configuration' '
> +       (
> +               sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
> +               echo master >expect &&
> +               git var GIT_DEFAULT_BRANCH >actual &&
> +               test_cmp expect actual
> +       )
> +'

So that we don't have to worry about this test breaking if the default
branch ever is changed, would it make sense to stop hard-coding
"master" here and instead employ the trick that Dscho mentioned
earlier in the thread (i.e. create a dummy repo and ask it for its
default branch)? Something like this (untested):

    (
        sane_unset GIT_TEST_... &&
        git init defbranch &&
        git -C defbranch symbolic-ref --short HEAD >expect &&
        git var GIT_DEFAULT_BRANCH >actual &&
        test_cmp expect actual
    )

> +test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '
> +       test_config init.defaultbranch foo &&
> +       (
> +               sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
> +               echo foo >expect &&
> +               git var GIT_DEFAULT_BRANCH >actual &&
> +               test_cmp expect actual
> +       )
> +'

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v3] var: add GIT_DEFAULT_BRANCH variable
  2021-11-03 19:57         ` Eric Sunshine
@ 2021-11-03 20:04           ` Junio C Hamano
  2021-11-03 20:17           ` [PATCH v4] " Thomas Weißschuh
  1 sibling, 0 replies; 18+ messages in thread
From: Junio C Hamano @ 2021-11-03 20:04 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Thomas Weißschuh, Git List, Johannes Schindelin,
	Ævar Arnfjörð Bjarmason, Jeff King

Eric Sunshine <sunshine@sunshineco.com> writes:

> earlier in the thread (i.e. create a dummy repo and ask it for its
> default branch)? Something like this (untested):
>
>     (
>         sane_unset GIT_TEST_... &&
>         git init defbranch &&
>         git -C defbranch symbolic-ref --short HEAD >expect &&
>         git var GIT_DEFAULT_BRANCH >actual &&
>         test_cmp expect actual
>     )

So, the idea is to "observe" what the init command actually does,
and see if that matches the behaviour of the var command?

Sounds like a good way to ensure correctness to me.

Thanks.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH v4] var: add GIT_DEFAULT_BRANCH variable
  2021-11-03 19:57         ` Eric Sunshine
  2021-11-03 20:04           ` Junio C Hamano
@ 2021-11-03 20:17           ` Thomas Weißschuh
  2021-11-03 20:23             ` Junio C Hamano
  1 sibling, 1 reply; 18+ messages in thread
From: Thomas Weißschuh @ 2021-11-03 20:17 UTC (permalink / raw)
  To: git
  Cc: Thomas Weißschuh, Johannes Schindelin,
	Ævar Arnfjörð Bjarmason, Jeff King, Eric Sunshine,
	Junio C Hamano

Introduce the logical variable GIT_DEFAULT_BRANCH which represents the
the default branch name that will be used by "git init".

Currently this variable 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`.

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

Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Helped-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
---

Changes from v1 ( https://lore.kernel.org/git/20211030140112.834650-1-thomas@t-8ch.de/ ):
* Replaced the custom subcommand with an internal variable
* Cleaned up the tests

Changes from v2 ( https://lore.kernel.org/git/20211102164434.1005707-1-thomas@t-8ch.de/ ):
* Removed superfluous error handling
* Switched to better fitting function
* Reworded commit message to be more consistent with the rest of git

Changes from v3 ( https://lore.kernel.org/git/20211103185331.599463-1-thomas@t-8ch.de/ ):
* Future-proof test by testing against the actual name of a new repository

 Documentation/git-var.txt |  3 +++
 builtin/var.c             |  7 +++++++
 t/t0007-git-var.sh        | 20 ++++++++++++++++++++
 3 files changed, 30 insertions(+)

diff --git a/Documentation/git-var.txt b/Documentation/git-var.txt
index 6072f936ab..387cc1b914 100644
--- a/Documentation/git-var.txt
+++ b/Documentation/git-var.txt
@@ -59,6 +59,9 @@ ifdef::git-default-pager[]
     The build you are using chose '{git-default-pager}' as the default.
 endif::git-default-pager[]
 
+GIT_DEFAULT_BRANCH::
+    The name of the first branch created in newly initialized repositories.
+
 SEE ALSO
 --------
 linkgit:git-commit-tree[1]
diff --git a/builtin/var.c b/builtin/var.c
index 6c6f46b4ae..491db27429 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,11 @@ static const char *pager(int flag)
 	return pgm;
 }
 
+static const char *default_branch(int flag)
+{
+	return git_default_branch_name(1);
+}
+
 struct git_var {
 	const char *name;
 	const char *(*read)(int);
@@ -36,6 +42,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 },
 };
 
diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh
index 53af92d571..e56f4b9ac5 100755
--- a/t/t0007-git-var.sh
+++ b/t/t0007-git-var.sh
@@ -27,6 +27,26 @@ test_expect_success !FAIL_PREREQS,!AUTOIDENT 'requested identities are strict' '
 	)
 '
 
+test_expect_success 'get GIT_DEFAULT_BRANCH without configuration' '
+	(
+		sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
+		git init defbranch &&
+		git -C defbranch symbolic-ref --short HEAD >expect &&
+		git var GIT_DEFAULT_BRANCH >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '
+	test_config init.defaultbranch foo &&
+	(
+		sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
+		echo foo >expect &&
+		git var GIT_DEFAULT_BRANCH >actual &&
+		test_cmp expect actual
+	)
+'
+
 # For git var -l, we check only a representative variable;
 # testing the whole output would make our test too brittle with
 # respect to unrelated changes in the test suite's environment.

base-commit: 0cddd84c9f3e9c3d793ec93034ef679335f35e49
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v4] var: add GIT_DEFAULT_BRANCH variable
  2021-11-03 20:17           ` [PATCH v4] " Thomas Weißschuh
@ 2021-11-03 20:23             ` Junio C Hamano
  0 siblings, 0 replies; 18+ messages in thread
From: Junio C Hamano @ 2021-11-03 20:23 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: git, Johannes Schindelin, Ævar Arnfjörð Bjarmason,
	Jeff King, Eric Sunshine

Thomas Weißschuh <thomas@t-8ch.de> writes:

> Introduce the logical variable GIT_DEFAULT_BRANCH which represents the
> the default branch name that will be used by "git init".

Sorry for not realizing this earlier, but we are not reporting the
"default" with this feature.  If you have ~/.gitconfig with your
favourite configuration in it, what this reports is the name of the
branch created by "git init" without the "--initial-branch=<name>"
option.

So GIT_INITIAL_BRANCH_NAME might be a more appropriate name for the
variable from that realization.  But I do not feel too strongly
about it, so let's not keep rerolling but see what others think
first.

Thanks.  Will queue this version as-is.  The updated test looks good.

> +test_expect_success 'get GIT_DEFAULT_BRANCH without configuration' '
> +	(
> +		sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
> +		git init defbranch &&
> +		git -C defbranch symbolic-ref --short HEAD >expect &&
> +		git var GIT_DEFAULT_BRANCH >actual &&
> +		test_cmp expect actual
> +	)
> +'
> +
> +test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '
> +	test_config init.defaultbranch foo &&
> +	(
> +		sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
> +		echo foo >expect &&
> +		git var GIT_DEFAULT_BRANCH >actual &&
> +		test_cmp expect actual
> +	)
> +'
> +
>  # For git var -l, we check only a representative variable;
>  # testing the whole output would make our test too brittle with
>  # respect to unrelated changes in the test suite's environment.
>
> base-commit: 0cddd84c9f3e9c3d793ec93034ef679335f35e49

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] builtin: add git-default-branch command
  2021-11-03 17:22   ` [PATCH] builtin: add git-default-branch command Junio C Hamano
@ 2021-11-03 23:44     ` Johannes Schindelin
  0 siblings, 0 replies; 18+ messages in thread
From: Johannes Schindelin @ 2021-11-03 23:44 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Thomas Weißschuh, git

Hi Junio,

On Wed, 3 Nov 2021, Junio C Hamano wrote:

> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
>
> > 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.
>
> Nice.  I was hoping that somebody would remember this one.

I am glad that at least you remembered what I said ;-)

> GIT_AUTHOR_IDENT and such that do not even exist as a variable is
> part of "git var -l"; the name for the default initial branch falls
> into the same category.

Yep. Likewise, `GIT_PAGER` does not have to exist as an environment
variable, and still would be listed in `git var -l`. And `GIT_EDITOR`. I
know there are scripts out there looking at `git var` for such (possibly
default) values. That's why `git var -l` is a pretty good place for this
information. Maybe the commit message could grow a paragraph with this
line of reasoning. Thomas, what do you think?

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2021-11-03 23:44 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-30 14:01 [PATCH] builtin: add git-default-branch command Thomas Weißschuh
2021-10-30 17:18 ` Ævar Arnfjörð Bjarmason
2021-11-02 13:39 ` Johannes Schindelin
2021-11-02 16:44   ` [PATCH v2] var: add GIT_DEFAULT_BRANCH variable Thomas Weißschuh
2021-11-02 16:53     ` Ævar Arnfjörð Bjarmason
2021-11-02 17:35       ` Thomas Weißschuh
2021-11-02 19:14         ` Ævar Arnfjörð Bjarmason
2021-11-02 20:08           ` Thomas Weißschuh
2021-11-03 11:37       ` Jeff King
2021-11-03 16:48         ` Eric Sunshine
2021-11-03 18:21     ` Junio C Hamano
2021-11-03 18:53       ` [PATCH v3] " Thomas Weißschuh
2021-11-03 19:57         ` Eric Sunshine
2021-11-03 20:04           ` Junio C Hamano
2021-11-03 20:17           ` [PATCH v4] " Thomas Weißschuh
2021-11-03 20:23             ` Junio C Hamano
2021-11-03 17:22   ` [PATCH] builtin: add git-default-branch command Junio C Hamano
2021-11-03 23:44     ` Johannes Schindelin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.