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 <chriscool@tuxfamily.org> Mentored-by: Heba Waly <heba.waly@gmail.com> Signed-off-by: Hariom Verma <hariom18599@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Hariom Verma committed Feb 13, 2021 at 01:52 UTC 90563aedcab92b75d4b5f6d7aa43d6a98aaccde6
2 files changed +61 -39
pretty.c
+50 -39
@@ -1148,6 +1148,54 @@ static int format_trailer_match_cb(const struct strbuf *key, void *ud)
1148 return 0;
1149 }
1150
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 +
1199 static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1200 const char *placeholder,
1201 void *context)
@@ -1425,45 +1473,8 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1473
1474 if (*arg == ':') {
1475 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;
1478 }
1479 if (*arg == ')') {
1480 format_trailers_from_commit(sb, msg + c->subject_off, &opts);
pretty.h
+11
@@ -6,6 +6,7 @@
6
7 struct commit;
8 struct strbuf;
9 +struct process_trailer_options;
10
11 /* Commit formats */
12 enum cmit_fmt {
@@ -142,4 +143,14 @@ int commit_format_is_empty(enum cmit_fmt);
143 /* Make subject of commit message suitable for filename */
144 void format_sanitized_subject(struct strbuf *sb, const char *msg, size_t len);
145
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 +
156 #endif /* PRETTY_H */