]> git.ipfire.org Git - thirdparty/git.git/commitdiff
var: add GIT_DEFAULT_BRANCH variable
authorThomas Weißschuh <thomas@t-8ch.de>
Wed, 3 Nov 2021 20:17:02 +0000 (21:17 +0100)
committerJunio C Hamano <gitster@pobox.com>
Wed, 3 Nov 2021 20:25:36 +0000 (13:25 -0700)
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>
Documentation/git-var.txt
builtin/var.c
t/t0007-git-var.sh

index 6072f936ab5e3a8b3aeafcaeddd81b7b6d5e0b01..387cc1b91420f78da6d16016af8cdc757db6dd44 100644 (file)
@@ -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]
index 6c6f46b4aeaf658f4de9dc02895a2b19a5c7c5a8..491db2742926dbd6fd08f681817821addcfafab7 100644 (file)
@@ -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 },
 };
 
index 88b9ae81588d1cd8581c29faf67ee8fe4a4e9a6d..3d6b71c1e55b716da9f620a86ec6ecd3be11edd5 100755 (executable)
@@ -25,6 +25,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.