Skip to content

Commit 9221ca2

Browse files
committed
config: format expiry dates gently
Move the logic for formatting expiry date config values into a helper method and use gentle parsing when needed. There is an unfortunate asymmetry in these two parsing methods, but we need to treat a positive response from parse_expiry_date() as an error or we will get incorrect values. This updates the behavior of 'git config list --type=expiry-date' to be quiet when attempting parsing on non-date values. Signed-off-by: Derrick Stolee <stolee@gmail.com>
1 parent d1cfa0c commit 9221ca2

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

builtin/config.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "abspath.h"
44
#include "config.h"
55
#include "color.h"
6+
#include "date.h"
67
#include "editor.h"
78
#include "environment.h"
89
#include "gettext.h"
@@ -336,6 +337,23 @@ static int format_config_path(struct strbuf *buf,
336337
return 0;
337338
}
338339

340+
static int format_config_expiry_date(struct strbuf *buf,
341+
const char *key_,
342+
const char *value_,
343+
int gently)
344+
{
345+
timestamp_t t;
346+
if (gently) {
347+
if (parse_expiry_date(value_, &t))
348+
return -1;
349+
} else if (git_config_expiry_date(&t, key_, value_) < 0) {
350+
return -1;
351+
}
352+
353+
strbuf_addf(buf, "%"PRItime, t);
354+
return 0;
355+
}
356+
339357
/*
340358
* Format the configuration key-value pair (`key_`, `value_`) and
341359
* append it into strbuf `buf`. Returns a negative value on failure,
@@ -371,12 +389,9 @@ static int format_config(const struct config_display_options *opts,
371389
res = format_config_bool_or_str(buf, value_);
372390
else if (opts->type == TYPE_PATH)
373391
res = format_config_path(buf, key_, value_, gently);
374-
else if (opts->type == TYPE_EXPIRY_DATE) {
375-
timestamp_t t;
376-
if (git_config_expiry_date(&t, key_, value_) < 0)
377-
return -1;
378-
strbuf_addf(buf, "%"PRItime, t);
379-
} else if (opts->type == TYPE_COLOR) {
392+
else if (opts->type == TYPE_EXPIRY_DATE)
393+
res = format_config_expiry_date(buf, key_, value_, gently);
394+
else if (opts->type == TYPE_COLOR) {
380395
char v[COLOR_MAXLEN];
381396
if (git_config_color(v, key_, value_) < 0)
382397
return -1;

t/t1300-config.sh

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,21 +2536,14 @@ test_expect_success 'list --type=path shows only canonicalizable path values' '
25362536
'
25372537

25382538
test_expect_success 'list --type=expiry-date shows only canonicalizable dates' '
2539-
cat >expecterr <<-EOF &&
2540-
error: '\''True'\'' for '\''section.foo'\'' is not a valid timestamp
2541-
error: '\''~/dir'\'' for '\''section.path'\'' is not a valid timestamp
2542-
error: '\''red'\'' for '\''section.red'\'' is not a valid timestamp
2543-
error: '\''Blue'\'' for '\''section.blue'\'' is not a valid timestamp
2544-
EOF
2545-
25462539
git config ${mode_prefix}list --type=expiry-date >actual 2>err &&
25472540
25482541
# section.number and section.big parse as relative dates that could
25492542
# have clock skew in their results.
25502543
test_grep section.big actual &&
25512544
test_grep section.number actual &&
25522545
test_grep "section.date=$(git config --type=expiry-date section.$key)" actual &&
2553-
test_cmp expecterr err
2546+
test_must_be_empty err
25542547
'
25552548

25562549
test_expect_success 'list --type=color shows only canonicalizable color values' '

0 commit comments

Comments
 (0)