Skip to content

Commit e042457

Browse files
committed
Merge branch 'jh/optional-path' into jch
Teaches the configuration mechanism that values of type pathname may be prefixed with ':(optional)' to be treated as such. * jh/optional-path: parseopt: values of pathname type can be prefixed with :(optional) config: values of pathname type can be prefixed with :(optional) t7500: make each piece more independent
2 parents 418196a + 2da08f2 commit e042457

File tree

4 files changed

+65
-22
lines changed

4 files changed

+65
-22
lines changed

Documentation/config.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,10 @@ compiled without runtime prefix support, the compiled-in prefix will be
358358
substituted instead. In the unlikely event that a literal path needs to
359359
be specified that should _not_ be expanded, it needs to be prefixed by
360360
`./`, like so: `./%(prefix)/bin`.
361-
361+
+
362+
If prefixed with `:(optional)`, the configuration variable is treated
363+
as if it does not exist, if the named path does not exist or names an
364+
empty file.
362365

363366
Variables
364367
~~~~~~~~~

config.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,11 +1364,23 @@ int git_config_string(char **dest, const char *var, const char *value)
13641364

13651365
int git_config_pathname(char **dest, const char *var, const char *value)
13661366
{
1367+
int is_optional;
1368+
char *path;
1369+
13671370
if (!value)
13681371
return config_error_nonbool(var);
1369-
*dest = interpolate_path(value, 0);
1370-
if (!*dest)
1372+
1373+
is_optional = skip_prefix(value, ":(optional)", &value);
1374+
path = interpolate_path(value, 0);
1375+
if (!path)
13711376
die(_("failed to expand user dir in: '%s'"), value);
1377+
1378+
if (is_optional && is_empty_or_missing_file(path)) {
1379+
free(path);
1380+
return 0;
1381+
}
1382+
1383+
*dest = path;
13721384
return 0;
13731385
}
13741386

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: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,46 +37,65 @@ 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
56+
)
57+
'
58+
59+
test_expect_success 'nonexistent optional template file in config' '
60+
test_config commit.template ":(optional)$PWD"/notexist &&
61+
(
62+
GIT_EDITOR="echo hello >\"\$1\"" &&
63+
export GIT_EDITOR &&
64+
git commit --allow-empty
4665
)
4766
'
4867

4968
# From now on we'll use a template file that exists.
5069
TEMPLATE="$PWD"/template
5170

5271
test_expect_success 'unedited template should not commit' '
53-
echo "template line" > "$TEMPLATE" &&
54-
test_must_fail git commit --template "$TEMPLATE"
72+
echo "template line" >"$TEMPLATE" &&
73+
test_must_fail git commit --allow-empty --template "$TEMPLATE"
5574
'
5675

5776
test_expect_success 'unedited template with comments should not commit' '
58-
echo "# comment in template" >> "$TEMPLATE" &&
59-
test_must_fail git commit --template "$TEMPLATE"
77+
echo "# comment in template" >>"$TEMPLATE" &&
78+
test_must_fail git commit --allow-empty --template "$TEMPLATE"
6079
'
6180

6281
test_expect_success 'a Signed-off-by line by itself should not commit' '
6382
(
6483
test_set_editor "$TEST_DIRECTORY"/t7500/add-signed-off &&
65-
test_must_fail git commit --template "$TEMPLATE"
84+
test_must_fail git commit --allow-empty --template "$TEMPLATE"
6685
)
6786
'
6887

6988
test_expect_success 'adding comments to a template should not commit' '
7089
(
7190
test_set_editor "$TEST_DIRECTORY"/t7500/add-comments &&
72-
test_must_fail git commit --template "$TEMPLATE"
91+
test_must_fail git commit --allow-empty --template "$TEMPLATE"
7392
)
7493
'
7594

7695
test_expect_success 'adding real content to a template should commit' '
7796
(
7897
test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
79-
git commit --template "$TEMPLATE"
98+
git commit --allow-empty --template "$TEMPLATE"
8099
) &&
81100
commit_msg_is "template linecommit message"
82101
'

0 commit comments

Comments
 (0)