Skip to content

Commit d818458

Browse files
committed
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
2 parents cb3d2e5 + 2ad150e commit d818458

File tree

3 files changed

+78
-16
lines changed

3 files changed

+78
-16
lines changed

Documentation/git-var.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ SYNOPSIS
1313

1414
DESCRIPTION
1515
-----------
16-
Prints a Git logical variable.
16+
Prints a Git logical variable. Exits with code 1 if the variable has
17+
no value.
1718

1819
OPTIONS
1920
-------

builtin/var.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ static const char var_usage[] = "git var (-l | <variable>)";
1111

1212
static const char *editor(int flag)
1313
{
14-
const char *pgm = git_editor();
15-
16-
if (!pgm && flag & IDENT_STRICT)
17-
die("Terminal is dumb, but EDITOR unset");
18-
19-
return pgm;
14+
return git_editor();
2015
}
2116

2217
static const char *pager(int flag)
@@ -56,18 +51,15 @@ static void list_vars(void)
5651
printf("%s=%s\n", ptr->name, val);
5752
}
5853

59-
static const char *read_var(const char *var)
54+
static const struct git_var *get_git_var(const char *var)
6055
{
6156
struct git_var *ptr;
62-
const char *val;
63-
val = NULL;
6457
for (ptr = git_vars; ptr->read; ptr++) {
6558
if (strcmp(var, ptr->name) == 0) {
66-
val = ptr->read(IDENT_STRICT);
67-
break;
59+
return ptr;
6860
}
6961
}
70-
return val;
62+
return NULL;
7163
}
7264

7365
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)
8173

8274
int cmd_var(int argc, const char **argv, const char *prefix)
8375
{
84-
const char *val = NULL;
76+
const struct git_var *git_var;
77+
const char *val;
78+
8579
if (argc != 2)
8680
usage(var_usage);
8781

@@ -91,10 +85,15 @@ int cmd_var(int argc, const char **argv, const char *prefix)
9185
return 0;
9286
}
9387
git_config(git_default_config, NULL);
94-
val = read_var(argv[1]);
95-
if (!val)
88+
89+
git_var = get_git_var(argv[1]);
90+
if (!git_var)
9691
usage(var_usage);
9792

93+
val = git_var->read(IDENT_STRICT);
94+
if (!val)
95+
return 1;
96+
9897
printf("%s\n", val);
9998

10099
return 0;

t/t0007-git-var.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ test_description='basic sanity checks for git var'
55
TEST_PASSES_SANITIZE_LEAK=true
66
. ./test-lib.sh
77

8+
sane_unset_all_editors () {
9+
sane_unset GIT_EDITOR &&
10+
sane_unset VISUAL &&
11+
sane_unset EDITOR
12+
}
13+
814
test_expect_success 'get GIT_AUTHOR_IDENT' '
915
test_tick &&
1016
echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
@@ -47,6 +53,62 @@ test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '
4753
)
4854
'
4955

56+
test_expect_success 'get GIT_EDITOR without configuration' '
57+
(
58+
sane_unset_all_editors &&
59+
test_expect_code 1 git var GIT_EDITOR >out &&
60+
test_must_be_empty out
61+
)
62+
'
63+
64+
test_expect_success 'get GIT_EDITOR with configuration' '
65+
test_config core.editor foo &&
66+
(
67+
sane_unset_all_editors &&
68+
echo foo >expect &&
69+
git var GIT_EDITOR >actual &&
70+
test_cmp expect actual
71+
)
72+
'
73+
74+
test_expect_success 'get GIT_EDITOR with environment variable GIT_EDITOR' '
75+
(
76+
sane_unset_all_editors &&
77+
echo bar >expect &&
78+
GIT_EDITOR=bar git var GIT_EDITOR >actual &&
79+
test_cmp expect actual
80+
)
81+
'
82+
83+
test_expect_success 'get GIT_EDITOR with environment variable EDITOR' '
84+
(
85+
sane_unset_all_editors &&
86+
echo bar >expect &&
87+
EDITOR=bar git var GIT_EDITOR >actual &&
88+
test_cmp expect actual
89+
)
90+
'
91+
92+
test_expect_success 'get GIT_EDITOR with configuration and environment variable GIT_EDITOR' '
93+
test_config core.editor foo &&
94+
(
95+
sane_unset_all_editors &&
96+
echo bar >expect &&
97+
GIT_EDITOR=bar git var GIT_EDITOR >actual &&
98+
test_cmp expect actual
99+
)
100+
'
101+
102+
test_expect_success 'get GIT_EDITOR with configuration and environment variable EDITOR' '
103+
test_config core.editor foo &&
104+
(
105+
sane_unset_all_editors &&
106+
echo foo >expect &&
107+
EDITOR=bar git var GIT_EDITOR >actual &&
108+
test_cmp expect actual
109+
)
110+
'
111+
50112
# For git var -l, we check only a representative variable;
51113
# testing the whole output would make our test too brittle with
52114
# respect to unrelated changes in the test suite's environment.

0 commit comments

Comments
 (0)