1
0
mirror of https://github.com/git/git synced 2024-07-05 00:58:49 +00:00
git/builtin/var.c
brian m. carlson 1e65721227 var: add support for listing the shell
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>
2023-06-27 11:31:05 -07:00

116 lines
2.1 KiB
C

/*
* GIT - The information manager from hell
*
* Copyright (C) Eric Biederman, 2005
*/
#include "builtin.h"
#include "config.h"
#include "editor.h"
#include "ident.h"
#include "pager.h"
#include "refs.h"
static const char var_usage[] = "git var (-l | <variable>)";
static const char *editor(int ident_flag UNUSED)
{
return git_editor();
}
static const char *sequence_editor(int ident_flag UNUSED)
{
return git_sequence_editor();
}
static const char *pager(int ident_flag UNUSED)
{
const char *pgm = git_pager(1);
if (!pgm)
pgm = "cat";
return pgm;
}
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);
};
static struct git_var git_vars[] = {
{ "GIT_COMMITTER_IDENT", git_committer_info },
{ "GIT_AUTHOR_IDENT", git_author_info },
{ "GIT_EDITOR", editor },
{ "GIT_SEQUENCE_EDITOR", sequence_editor },
{ "GIT_PAGER", pager },
{ "GIT_DEFAULT_BRANCH", default_branch },
{ "GIT_SHELL_PATH", shell_path },
{ "", NULL },
};
static void list_vars(void)
{
struct git_var *ptr;
const char *val;
for (ptr = git_vars; ptr->read; ptr++)
if ((val = ptr->read(0)))
printf("%s=%s\n", ptr->name, val);
}
static const struct git_var *get_git_var(const char *var)
{
struct git_var *ptr;
for (ptr = git_vars; ptr->read; ptr++) {
if (strcmp(var, ptr->name) == 0) {
return ptr;
}
}
return NULL;
}
static int show_config(const char *var, const char *value, void *cb)
{
if (value)
printf("%s=%s\n", var, value);
else
printf("%s\n", var);
return git_default_config(var, value, cb);
}
int cmd_var(int argc, const char **argv, const char *prefix UNUSED)
{
const struct git_var *git_var;
const char *val;
if (argc != 2)
usage(var_usage);
if (strcmp(argv[1], "-l") == 0) {
git_config(show_config, NULL);
list_vars();
return 0;
}
git_config(git_default_config, NULL);
git_var = get_git_var(argv[1]);
if (!git_var)
usage(var_usage);
val = git_var->read(IDENT_STRICT);
if (!val)
return 1;
printf("%s\n", val);
return 0;
}