Skip to content

Commit

Permalink
ref-filter: 'contents:trailers' show error if : is missing
Browse files Browse the repository at this point in the history
The 'contents' atom does not show any error if used with 'trailers'
atom and semicolon is missing before trailers arguments.

e.g %(contents:trailersonly) works, while it shouldn't.

It is definitely not an expected behavior.

Let's fix this bug.

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Heba Waly <heba.waly@gmail.com>
Signed-off-by: Hariom Verma <hariom18599@gmail.com>
  • Loading branch information
harry-hov committed Aug 19, 2020
1 parent bd0bb8d commit 7daf933
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
21 changes: 18 additions & 3 deletions ref-filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,22 @@ static int trailers_atom_parser(const struct ref_format *format, struct used_ato
return 0;
}

static int check_format_field(const char *arg, const char *field, const char **option)
{
const char *opt;
if (skip_prefix(arg, field, &opt)) {
if (*opt == '\0') {
*option = NULL;
return 1;
}
else if (*opt == ':') {
*option = ++opt;
return 1;
}
}
return 0;
}

static int contents_atom_parser(const struct ref_format *format, struct used_atom *atom,
const char *arg, struct strbuf *err)
{
Expand All @@ -345,9 +361,8 @@ static int contents_atom_parser(const struct ref_format *format, struct used_ato
atom->u.contents.option = C_SIG;
else if (!strcmp(arg, "subject"))
atom->u.contents.option = C_SUB;
else if (skip_prefix(arg, "trailers", &arg)) {
skip_prefix(arg, ":", &arg);
if (trailers_atom_parser(format, atom, *arg ? arg : NULL, err))
else if (check_format_field(arg, "trailers", &arg)) {
if (trailers_atom_parser(format, atom, arg, err))
return -1;
} else if (skip_prefix(arg, "lines=", &arg)) {
atom->u.contents.option = C_LINES;
Expand Down
9 changes: 9 additions & 0 deletions t/t6300-for-each-ref.sh
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,15 @@ test_expect_success '%(trailers) rejects unknown trailers arguments' '
test_i18ncmp expect actual
'

test_expect_success 'if arguments, %(contents:trailers) shows error if semicolon is missing' '
# error message cannot be checked under i18n
cat >expect <<-EOF &&
fatal: unrecognized %(contents) argument: trailersonly
EOF
test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
test_i18ncmp expect actual
'

test_expect_success 'basic atom: head contents:trailers' '
git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
sanitize_pgp <actual >actual.clean &&
Expand Down

0 comments on commit 7daf933

Please sign in to comment.