Message ID | 20211030140112.834650-1-thomas@t-8ch.de (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | builtin: add git-default-branch command | expand |
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.
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
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.
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
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
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 base-commit: 6c40894d2466d4e7fddc047a05116aa9d14712ee