Skip to content

Commit 636a0ae

Browse files
harry-hovgitster
authored andcommitted
pretty.c: capture invalid trailer argument
As we would like to use this trailers logic in the ref-filter, it's nice to get an invalid trailer argument. This will allow us to print precise error message while using `format_set_trailers_options()` in ref-filter. For capturing the invalid argument, we changed the working of `format_set_trailers_options()` a little bit. Original logic does "break" and fell through in mainly 2 cases - 1. unknown/invalid argument 2. end of the arg string But now instead of "break", we capture invalid argument and return non-zero. And non-zero is handled by the caller. (We prepared the caller to handle non-zero in the previous commit). Capturing invalid arguments this way will also affects the working of current logic. As at the end of the arg string it will return non-zero. So in order to make things correct, introduced an additional conditional statement i.e if encounter ")", do 'break'. Mentored-by: Christian Couder <[email protected]> Mentored-by: Heba Waly <[email protected]> Signed-off-by: Hariom Verma <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 90563ae commit 636a0ae

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

pretty.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,12 +1152,16 @@ int format_set_trailers_options(struct process_trailer_options *opts,
11521152
struct string_list *filter_list,
11531153
struct strbuf *sepbuf,
11541154
struct strbuf *kvsepbuf,
1155-
const char **arg)
1155+
const char **arg,
1156+
char **invalid_arg)
11561157
{
11571158
for (;;) {
11581159
const char *argval;
11591160
size_t arglen;
11601161

1162+
if (**arg == ')')
1163+
break;
1164+
11611165
if (match_placeholder_arg_value(*arg, "key", arg, &argval, &arglen)) {
11621166
uintptr_t len = arglen;
11631167

@@ -1190,8 +1194,13 @@ int format_set_trailers_options(struct process_trailer_options *opts,
11901194
} else if (!match_placeholder_bool_arg(*arg, "only", arg, &opts->only_trailers) &&
11911195
!match_placeholder_bool_arg(*arg, "unfold", arg, &opts->unfold) &&
11921196
!match_placeholder_bool_arg(*arg, "keyonly", arg, &opts->key_only) &&
1193-
!match_placeholder_bool_arg(*arg, "valueonly", arg, &opts->value_only))
1194-
break;
1197+
!match_placeholder_bool_arg(*arg, "valueonly", arg, &opts->value_only)) {
1198+
if (invalid_arg) {
1199+
size_t len = strcspn(*arg, ",)");
1200+
*invalid_arg = xstrndup(*arg, len);
1201+
}
1202+
return -1;
1203+
}
11951204
}
11961205
return 0;
11971206
}
@@ -1473,7 +1482,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
14731482

14741483
if (*arg == ':') {
14751484
arg++;
1476-
if (format_set_trailers_options(&opts, &filter_list, &sepbuf, &kvsepbuf, &arg))
1485+
if (format_set_trailers_options(&opts, &filter_list, &sepbuf, &kvsepbuf, &arg, NULL))
14771486
goto trailer_out;
14781487
}
14791488
if (*arg == ')') {

pretty.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ int format_set_trailers_options(struct process_trailer_options *opts,
151151
struct string_list *filter_list,
152152
struct strbuf *sepbuf,
153153
struct strbuf *kvsepbuf,
154-
const char **arg);
154+
const char **arg,
155+
char **invalid_arg);
155156

156157
#endif /* PRETTY_H */

0 commit comments

Comments
 (0)