Skip to content

Commit 90563ae

Browse files
harry-hovgitster
authored andcommitted
pretty.c: refactor trailer logic to format_set_trailers_options()
Refactored trailers formatting logic inside pretty.c to a new function `format_set_trailers_options()`. This new function returns the non-zero in case of unusual. The caller handles the non-zero by "goto trailers_out". This change will allow us to reuse the same logic in other places. 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 727331d commit 90563ae

File tree

2 files changed

+61
-39
lines changed

2 files changed

+61
-39
lines changed

pretty.c

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,54 @@ static int format_trailer_match_cb(const struct strbuf *key, void *ud)
11481148
return 0;
11491149
}
11501150

1151+
int format_set_trailers_options(struct process_trailer_options *opts,
1152+
struct string_list *filter_list,
1153+
struct strbuf *sepbuf,
1154+
struct strbuf *kvsepbuf,
1155+
const char **arg)
1156+
{
1157+
for (;;) {
1158+
const char *argval;
1159+
size_t arglen;
1160+
1161+
if (match_placeholder_arg_value(*arg, "key", arg, &argval, &arglen)) {
1162+
uintptr_t len = arglen;
1163+
1164+
if (!argval)
1165+
return -1;
1166+
1167+
if (len && argval[len - 1] == ':')
1168+
len--;
1169+
string_list_append(filter_list, argval)->util = (char *)len;
1170+
1171+
opts->filter = format_trailer_match_cb;
1172+
opts->filter_data = filter_list;
1173+
opts->only_trailers = 1;
1174+
} else if (match_placeholder_arg_value(*arg, "separator", arg, &argval, &arglen)) {
1175+
char *fmt;
1176+
1177+
strbuf_reset(sepbuf);
1178+
fmt = xstrndup(argval, arglen);
1179+
strbuf_expand(sepbuf, fmt, strbuf_expand_literal_cb, NULL);
1180+
free(fmt);
1181+
opts->separator = sepbuf;
1182+
} else if (match_placeholder_arg_value(*arg, "key_value_separator", arg, &argval, &arglen)) {
1183+
char *fmt;
1184+
1185+
strbuf_reset(kvsepbuf);
1186+
fmt = xstrndup(argval, arglen);
1187+
strbuf_expand(kvsepbuf, fmt, strbuf_expand_literal_cb, NULL);
1188+
free(fmt);
1189+
opts->key_value_separator = kvsepbuf;
1190+
} else if (!match_placeholder_bool_arg(*arg, "only", arg, &opts->only_trailers) &&
1191+
!match_placeholder_bool_arg(*arg, "unfold", arg, &opts->unfold) &&
1192+
!match_placeholder_bool_arg(*arg, "keyonly", arg, &opts->key_only) &&
1193+
!match_placeholder_bool_arg(*arg, "valueonly", arg, &opts->value_only))
1194+
break;
1195+
}
1196+
return 0;
1197+
}
1198+
11511199
static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
11521200
const char *placeholder,
11531201
void *context)
@@ -1425,45 +1473,8 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
14251473

14261474
if (*arg == ':') {
14271475
arg++;
1428-
for (;;) {
1429-
const char *argval;
1430-
size_t arglen;
1431-
1432-
if (match_placeholder_arg_value(arg, "key", &arg, &argval, &arglen)) {
1433-
uintptr_t len = arglen;
1434-
1435-
if (!argval)
1436-
goto trailer_out;
1437-
1438-
if (len && argval[len - 1] == ':')
1439-
len--;
1440-
string_list_append(&filter_list, argval)->util = (char *)len;
1441-
1442-
opts.filter = format_trailer_match_cb;
1443-
opts.filter_data = &filter_list;
1444-
opts.only_trailers = 1;
1445-
} else if (match_placeholder_arg_value(arg, "separator", &arg, &argval, &arglen)) {
1446-
char *fmt;
1447-
1448-
strbuf_reset(&sepbuf);
1449-
fmt = xstrndup(argval, arglen);
1450-
strbuf_expand(&sepbuf, fmt, strbuf_expand_literal_cb, NULL);
1451-
free(fmt);
1452-
opts.separator = &sepbuf;
1453-
} else if (match_placeholder_arg_value(arg, "key_value_separator", &arg, &argval, &arglen)) {
1454-
char *fmt;
1455-
1456-
strbuf_reset(&kvsepbuf);
1457-
fmt = xstrndup(argval, arglen);
1458-
strbuf_expand(&kvsepbuf, fmt, strbuf_expand_literal_cb, NULL);
1459-
free(fmt);
1460-
opts.key_value_separator = &kvsepbuf;
1461-
} else if (!match_placeholder_bool_arg(arg, "only", &arg, &opts.only_trailers) &&
1462-
!match_placeholder_bool_arg(arg, "unfold", &arg, &opts.unfold) &&
1463-
!match_placeholder_bool_arg(arg, "keyonly", &arg, &opts.key_only) &&
1464-
!match_placeholder_bool_arg(arg, "valueonly", &arg, &opts.value_only))
1465-
break;
1466-
}
1476+
if (format_set_trailers_options(&opts, &filter_list, &sepbuf, &kvsepbuf, &arg))
1477+
goto trailer_out;
14671478
}
14681479
if (*arg == ')') {
14691480
format_trailers_from_commit(sb, msg + c->subject_off, &opts);

pretty.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
struct commit;
88
struct strbuf;
9+
struct process_trailer_options;
910

1011
/* Commit formats */
1112
enum cmit_fmt {
@@ -142,4 +143,14 @@ int commit_format_is_empty(enum cmit_fmt);
142143
/* Make subject of commit message suitable for filename */
143144
void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len);
144145

146+
/*
147+
* Set values of fields in "struct process_trailer_options"
148+
* according to trailers arguments.
149+
*/
150+
int format_set_trailers_options(struct process_trailer_options *opts,
151+
struct string_list *filter_list,
152+
struct strbuf *sepbuf,
153+
struct strbuf *kvsepbuf,
154+
const char **arg);
155+
145156
#endif /* PRETTY_H */

0 commit comments

Comments
 (0)