Merge branch 'tw/var-default-branch'

"git var GIT_DEFAULT_BRANCH" is a way to see what name is used for
the newly created branch if "git init" is run.

* tw/var-default-branch:
  var: add GIT_DEFAULT_BRANCH variable
This commit is contained in:
Junio C Hamano 2021-12-10 14:35:07 -08:00
commit 03194a1afa
3 changed files with 30 additions and 0 deletions

View file

@ -59,6 +59,9 @@ ifdef::git-default-pager[]
The build you are using chose '{git-default-pager}' as the default. The build you are using chose '{git-default-pager}' as the default.
endif::git-default-pager[] endif::git-default-pager[]
GIT_DEFAULT_BRANCH::
The name of the first branch created in newly initialized repositories.
SEE ALSO SEE ALSO
-------- --------
linkgit:git-commit-tree[1] linkgit:git-commit-tree[1]

View file

@ -5,6 +5,7 @@
*/ */
#include "builtin.h" #include "builtin.h"
#include "config.h" #include "config.h"
#include "refs.h"
static const char var_usage[] = "git var (-l | <variable>)"; static const char var_usage[] = "git var (-l | <variable>)";
@ -27,6 +28,11 @@ static const char *pager(int flag)
return pgm; return pgm;
} }
static const char *default_branch(int flag)
{
return git_default_branch_name(1);
}
struct git_var { struct git_var {
const char *name; const char *name;
const char *(*read)(int); const char *(*read)(int);
@ -36,6 +42,7 @@ static struct git_var git_vars[] = {
{ "GIT_AUTHOR_IDENT", git_author_info }, { "GIT_AUTHOR_IDENT", git_author_info },
{ "GIT_EDITOR", editor }, { "GIT_EDITOR", editor },
{ "GIT_PAGER", pager }, { "GIT_PAGER", pager },
{ "GIT_DEFAULT_BRANCH", default_branch },
{ "", NULL }, { "", NULL },
}; };

View file

@ -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; # For git var -l, we check only a representative variable;
# testing the whole output would make our test too brittle with # testing the whole output would make our test too brittle with
# respect to unrelated changes in the test suite's environment. # respect to unrelated changes in the test suite's environment.