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>
Thomas Weißschuh committed
Nov 3, 2021 at 21:17 UTC
e06c9e1df28e04bbf013840c6c50ce1be41b9583
3 files changed
+30
Documentation/git-var.txt
+3
@@ -59,6 +59,9 @@ ifdef::git-default-pager[]
59
The build you are using chose '{git-default-pager}' as the default.
60
endif::git-default-pager[]
61
62
+GIT_DEFAULT_BRANCH::
63
+ The name of the first branch created in newly initialized repositories.
64
+
65
SEE ALSO
66
--------
67
linkgit:git-commit-tree[1]
builtin/var.c
+7
@@ -5,6 +5,7 @@
5
*/
6
#include "builtin.h"
7
#include "config.h"
8
+#include "refs.h"
9
10
static const char var_usage[] = "git var (-l | <variable>)";
11
@@ -27,6 +28,11 @@ static const char *pager(int flag)
28
return pgm;
29
}
30
31
+static const char *default_branch(int flag)
32
+{
33
+ return git_default_branch_name(1);
34
+}
35
+
36
struct git_var {
37
const char *name;
38
const char *(*read)(int);
@@ -36,6 +42,7 @@ static struct git_var git_vars[] = {
42
{ "GIT_AUTHOR_IDENT", git_author_info },
43
{ "GIT_EDITOR", editor },
44
{ "GIT_PAGER", pager },
45
+ { "GIT_DEFAULT_BRANCH", default_branch },
46
{ "", NULL },
47
};
48
t/t0007-git-var.sh
+20
@@ -25,6 +25,26 @@ test_expect_success !FAIL_PREREQS,!AUTOIDENT 'requested identities are strict' '
25
)
26
'
27
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
+
48
# For git var -l, we check only a representative variable;
49
# testing the whole output would make our test too brittle with
50
# respect to unrelated changes in the test suite's environment.