Skip to content

Commit 2da08f2

Browse files
gitsterttaylorr
authored andcommitted
parseopt: values of pathname type can be prefixed with :(optional)
In the previous step, we introduced an optional filename that can be given to a configuration variable, and nullify the fact that such a configuration setting even existed if the named path is missing or empty. Let's do the same for command line options that name a pathname. Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent dbafaff commit 2da08f2

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

parse-options.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
7575
{
7676
const char *s, *arg;
7777
const int unset = flags & OPT_UNSET;
78-
int err;
7978

8079
if (unset && p->opt)
8180
return error(_("%s takes no value"), optname(opt, flags));
@@ -131,21 +130,31 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
131130
case OPTION_FILENAME:
132131
{
133132
const char *value;
134-
135-
FREE_AND_NULL(*(char **)opt->value);
136-
137-
err = 0;
133+
int is_optional;
138134

139135
if (unset)
140136
value = NULL;
141137
else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
142-
value = (const char *) opt->defval;
143-
else
144-
err = get_arg(p, opt, flags, &value);
138+
value = (char *)opt->defval;
139+
else {
140+
int err = get_arg(p, opt, flags, &value);
141+
if (err)
142+
return err;
143+
}
144+
if (!value)
145+
return 0;
145146

146-
if (!err)
147-
*(char **)opt->value = fix_filename(p->prefix, value);
148-
return err;
147+
is_optional = skip_prefix(value, ":(optional)", &value);
148+
if (!value)
149+
is_optional = 0;
150+
value = fix_filename(p->prefix, value);
151+
if (is_optional && is_empty_or_missing_file(value)) {
152+
free((char *)value);
153+
} else {
154+
FREE_AND_NULL(*(char **)opt->value);
155+
*(const char **)opt->value = value;
156+
}
157+
return 0;
149158
}
150159
case OPTION_CALLBACK:
151160
{

t/t7500-commit-template-squash-signoff.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,22 @@ test_expect_success 'nonexistent template file should return error' '
3737
)
3838
'
3939

40+
test_expect_success 'nonexistent optional template file on command line' '
41+
echo changes >> foo &&
42+
git add foo &&
43+
(
44+
GIT_EDITOR="echo hello >\"\$1\"" &&
45+
export GIT_EDITOR &&
46+
git commit --template ":(optional)$PWD/notexist"
47+
)
48+
'
49+
4050
test_expect_success 'nonexistent template file in config should return error' '
4151
test_config commit.template "$PWD"/notexist &&
4252
(
4353
GIT_EDITOR="echo hello >\"\$1\"" &&
4454
export GIT_EDITOR &&
45-
test_must_fail git commit
55+
test_must_fail git commit --allow-empty
4656
)
4757
'
4858

0 commit comments

Comments
 (0)