]> git.ipfire.org Git - thirdparty/git.git/commitdiff
var: add support for listing the shell
authorbrian m. carlson <bk2204@github.com>
Tue, 27 Jun 2023 16:18:57 +0000 (16:18 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 27 Jun 2023 18:31:05 +0000 (11:31 -0700)
On most Unix systems, finding a suitable shell is easy: one simply uses
"sh" with an appropriate PATH value.  However, in many Windows
environments, the shell is shipped alongside Git, and it may or may not
be in PATH, even if Git is.

In such an environment, it can be very helpful to query Git for the
shell it's using, since other tools may want to use the same shell as
well.  To help them out, let's add a variable, GIT_SHELL_PATH, that
points to the location of the shell.

On Unix, we know our shell must be executable to be functional, so
assume that the distributor has correctly configured their environment,
and use that as a basic test.  On Git for Windows, we know that our
shell will be one of a few fixed values, all of which end in "sh" (such
as "bash").  This seems like it might be a nice test on Unix as well,
since it is customary for all shells to end in "sh", but there probably
exist such systems that don't have such a configuration, so be careful
here not to break them.

Signed-off-by: brian m. carlson <bk2204@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-var.txt
builtin/var.c
t/t0007-git-var.sh

index f40202b8e3ab521ea22c78986d3bf5bb44a67f09..f0f647e14ab2aa239b70954bb577899961ee1e0c 100644 (file)
@@ -71,6 +71,9 @@ endif::git-default-pager[]
 GIT_DEFAULT_BRANCH::
     The name of the first branch created in newly initialized repositories.
 
+GIT_SHELL_PATH::
+    The path of the binary providing the POSIX shell for commands which use the shell.
+
 SEE ALSO
 --------
 linkgit:git-commit-tree[1]
index 10ee62f84c48481ea1e88c6045a9c21b76bc761c..bd340c5717dcab6b00c546e96dbb8b0759ae322b 100644 (file)
@@ -36,6 +36,11 @@ static const char *default_branch(int ident_flag UNUSED)
        return git_default_branch_name(1);
 }
 
+static const char *shell_path(int ident_flag UNUSED)
+{
+       return SHELL_PATH;
+}
+
 struct git_var {
        const char *name;
        const char *(*read)(int);
@@ -47,6 +52,7 @@ static struct git_var git_vars[] = {
        { "GIT_SEQUENCE_EDITOR", sequence_editor },
        { "GIT_PAGER", pager },
        { "GIT_DEFAULT_BRANCH", default_branch },
+       { "GIT_SHELL_PATH", shell_path },
        { "", NULL },
 };
 
index eeb8539c1bcb2df86a5aff1fa2c78fc555598d14..e35f07afcb97b66be4abb624e7469af4b97658d7 100755 (executable)
@@ -147,6 +147,21 @@ test_expect_success 'get GIT_SEQUENCE_EDITOR with configuration and environment
        )
 '
 
+test_expect_success POSIXPERM 'GIT_SHELL_PATH points to a valid executable' '
+       shellpath=$(git var GIT_SHELL_PATH) &&
+       test_path_is_executable "$shellpath"
+'
+
+# We know in this environment that our shell will be one of a few fixed values
+# that all end in "sh".
+test_expect_success MINGW 'GIT_SHELL_PATH points to a suitable shell' '
+       shellpath=$(git var GIT_SHELL_PATH) &&
+       case "$shellpath" in
+       *sh) ;;
+       *) return 1;;
+       esac
+'
+
 # 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.