Merge branch 'sa/git-var-empty'

"git var UNKNOWN_VARIABLE" and "git var VARIABLE" with the variable
given an empty value used to behave identically.  Now the latter
just gives an empty output, while the former still gives an error
message.

* sa/git-var-empty:
  var: allow GIT_EDITOR to return null
  var: do not print usage() with a correct invocation
This commit is contained in:
Junio C Hamano
2022-12-14 15:55:47 +09:00
3 changed files with 78 additions and 16 deletions

View File

@ -13,7 +13,8 @@ SYNOPSIS
DESCRIPTION
-----------
Prints a Git logical variable.
Prints a Git logical variable. Exits with code 1 if the variable has
no value.
OPTIONS
-------

View File

@ -11,12 +11,7 @@ static const char var_usage[] = "git var (-l | <variable>)";
static const char *editor(int flag)
{
const char *pgm = git_editor();
if (!pgm && flag & IDENT_STRICT)
die("Terminal is dumb, but EDITOR unset");
return pgm;
return git_editor();
}
static const char *pager(int flag)
@ -56,18 +51,15 @@ static void list_vars(void)
printf("%s=%s\n", ptr->name, val);
}
static const char *read_var(const char *var)
static const struct git_var *get_git_var(const char *var)
{
struct git_var *ptr;
const char *val;
val = NULL;
for (ptr = git_vars; ptr->read; ptr++) {
if (strcmp(var, ptr->name) == 0) {
val = ptr->read(IDENT_STRICT);
break;
return ptr;
}
}
return val;
return NULL;
}
static int show_config(const char *var, const char *value, void *cb)
@ -81,7 +73,9 @@ static int show_config(const char *var, const char *value, void *cb)
int cmd_var(int argc, const char **argv, const char *prefix)
{
const char *val = NULL;
const struct git_var *git_var;
const char *val;
if (argc != 2)
usage(var_usage);
@ -91,10 +85,15 @@ int cmd_var(int argc, const char **argv, const char *prefix)
return 0;
}
git_config(git_default_config, NULL);
val = read_var(argv[1]);
if (!val)
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;

View File

@ -5,6 +5,12 @@ test_description='basic sanity checks for git var'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
sane_unset_all_editors () {
sane_unset GIT_EDITOR &&
sane_unset VISUAL &&
sane_unset EDITOR
}
test_expect_success 'get GIT_AUTHOR_IDENT' '
test_tick &&
echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
@ -47,6 +53,62 @@ test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '
)
'
test_expect_success 'get GIT_EDITOR without configuration' '
(
sane_unset_all_editors &&
test_expect_code 1 git var GIT_EDITOR >out &&
test_must_be_empty out
)
'
test_expect_success 'get GIT_EDITOR with configuration' '
test_config core.editor foo &&
(
sane_unset_all_editors &&
echo foo >expect &&
git var GIT_EDITOR >actual &&
test_cmp expect actual
)
'
test_expect_success 'get GIT_EDITOR with environment variable GIT_EDITOR' '
(
sane_unset_all_editors &&
echo bar >expect &&
GIT_EDITOR=bar git var GIT_EDITOR >actual &&
test_cmp expect actual
)
'
test_expect_success 'get GIT_EDITOR with environment variable EDITOR' '
(
sane_unset_all_editors &&
echo bar >expect &&
EDITOR=bar git var GIT_EDITOR >actual &&
test_cmp expect actual
)
'
test_expect_success 'get GIT_EDITOR with configuration and environment variable GIT_EDITOR' '
test_config core.editor foo &&
(
sane_unset_all_editors &&
echo bar >expect &&
GIT_EDITOR=bar git var GIT_EDITOR >actual &&
test_cmp expect actual
)
'
test_expect_success 'get GIT_EDITOR with configuration and environment variable EDITOR' '
test_config core.editor foo &&
(
sane_unset_all_editors &&
echo foo >expect &&
EDITOR=bar git var GIT_EDITOR >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.