Skip to content

Commit e06c9e1

Browse files
t-8chgitster
authored andcommitted
var: add GIT_DEFAULT_BRANCH variable
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent af6d1d6 commit e06c9e1

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

Documentation/git-var.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ ifdef::git-default-pager[]
5959
The build you are using chose '{git-default-pager}' as the default.
6060
endif::git-default-pager[]
6161

62+
GIT_DEFAULT_BRANCH::
63+
The name of the first branch created in newly initialized repositories.
64+
6265
SEE ALSO
6366
--------
6467
linkgit:git-commit-tree[1]

builtin/var.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
#include "builtin.h"
77
#include "config.h"
8+
#include "refs.h"
89

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

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

31+
static const char *default_branch(int flag)
32+
{
33+
return git_default_branch_name(1);
34+
}
35+
3036
struct git_var {
3137
const char *name;
3238
const char *(*read)(int);
@@ -36,6 +42,7 @@ static struct git_var git_vars[] = {
3642
{ "GIT_AUTHOR_IDENT", git_author_info },
3743
{ "GIT_EDITOR", editor },
3844
{ "GIT_PAGER", pager },
45+
{ "GIT_DEFAULT_BRANCH", default_branch },
3946
{ "", NULL },
4047
};
4148

t/t0007-git-var.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ test_expect_success !FAIL_PREREQS,!AUTOIDENT 'requested identities are strict' '
2525
)
2626
'
2727

28+
test_expect_success 'get GIT_DEFAULT_BRANCH without configuration' '
29+
(
30+
sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
31+
git init defbranch &&
32+
git -C defbranch symbolic-ref --short HEAD >expect &&
33+
git var GIT_DEFAULT_BRANCH >actual &&
34+
test_cmp expect actual
35+
)
36+
'
37+
38+
test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '
39+
test_config init.defaultbranch foo &&
40+
(
41+
sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
42+
echo foo >expect &&
43+
git var GIT_DEFAULT_BRANCH >actual &&
44+
test_cmp expect actual
45+
)
46+
'
47+
2848
# For git var -l, we check only a representative variable;
2949
# testing the whole output would make our test too brittle with
3050
# respect to unrelated changes in the test suite's environment.

0 commit comments

Comments
 (0)