Skip to content

Commit c9f1f88

Browse files
committed
Merge branch 'la/format-trailer-info'
The code to format trailers have been cleaned up. * la/format-trailer-info: trailer: finish formatting unification trailer: begin formatting unification format_trailer_info(): append newline for non-trailer lines format_trailer_info(): drop redundant unfold_value() format_trailer_info(): use trailer_item objects
2 parents b258237 + 3452d17 commit c9f1f88

File tree

2 files changed

+32
-62
lines changed

2 files changed

+32
-62
lines changed

trailer.c

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -144,38 +144,6 @@ static char last_non_space_char(const char *s)
144144
return '\0';
145145
}
146146

147-
static void print_tok_val(struct strbuf *out, const char *tok, const char *val)
148-
{
149-
char c;
150-
151-
if (!tok) {
152-
strbuf_addf(out, "%s\n", val);
153-
return;
154-
}
155-
156-
c = last_non_space_char(tok);
157-
if (!c)
158-
return;
159-
if (strchr(separators, c))
160-
strbuf_addf(out, "%s%s\n", tok, val);
161-
else
162-
strbuf_addf(out, "%s%c %s\n", tok, separators[0], val);
163-
}
164-
165-
void format_trailers(const struct process_trailer_options *opts,
166-
struct list_head *trailers,
167-
struct strbuf *out)
168-
{
169-
struct list_head *pos;
170-
struct trailer_item *item;
171-
list_for_each(pos, trailers) {
172-
item = list_entry(pos, struct trailer_item, list);
173-
if ((!opts->trim_empty || strlen(item->value) > 0) &&
174-
(!opts->only_trailers || item->token))
175-
print_tok_val(out, item->token, item->value);
176-
}
177-
}
178-
179147
static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok)
180148
{
181149
struct trailer_item *new_item = xcalloc(1, sizeof(*new_item));
@@ -1084,35 +1052,44 @@ void trailer_info_release(struct trailer_info *info)
10841052
free(info->trailers);
10851053
}
10861054

1087-
static void format_trailer_info(const struct process_trailer_options *opts,
1088-
const struct trailer_info *info,
1089-
struct strbuf *out)
1055+
void format_trailers(const struct process_trailer_options *opts,
1056+
struct list_head *trailers,
1057+
struct strbuf *out)
10901058
{
10911059
size_t origlen = out->len;
1092-
size_t i;
1093-
1094-
for (i = 0; i < info->trailer_nr; i++) {
1095-
char *trailer = info->trailers[i];
1096-
ssize_t separator_pos = find_separator(trailer, separators);
1060+
struct list_head *pos;
1061+
struct trailer_item *item;
10971062

1098-
if (separator_pos >= 1) {
1063+
list_for_each(pos, trailers) {
1064+
item = list_entry(pos, struct trailer_item, list);
1065+
if (item->token) {
10991066
struct strbuf tok = STRBUF_INIT;
11001067
struct strbuf val = STRBUF_INIT;
1068+
strbuf_addstr(&tok, item->token);
1069+
strbuf_addstr(&val, item->value);
1070+
1071+
/*
1072+
* Skip key/value pairs where the value was empty. This
1073+
* can happen from trailers specified without a
1074+
* separator, like `--trailer "Reviewed-by"` (no
1075+
* corresponding value).
1076+
*/
1077+
if (opts->trim_empty && !strlen(item->value))
1078+
continue;
11011079

1102-
parse_trailer(&tok, &val, NULL, trailer, separator_pos);
11031080
if (!opts->filter || opts->filter(&tok, opts->filter_data)) {
1104-
if (opts->unfold)
1105-
unfold_value(&val);
1106-
11071081
if (opts->separator && out->len != origlen)
11081082
strbuf_addbuf(out, opts->separator);
11091083
if (!opts->value_only)
11101084
strbuf_addbuf(out, &tok);
11111085
if (!opts->key_only && !opts->value_only) {
11121086
if (opts->key_value_separator)
11131087
strbuf_addbuf(out, opts->key_value_separator);
1114-
else
1115-
strbuf_addstr(out, ": ");
1088+
else {
1089+
char c = last_non_space_char(tok.buf);
1090+
if (c && !strchr(separators, c))
1091+
strbuf_addf(out, "%c ", separators[0]);
1092+
}
11161093
}
11171094
if (!opts->key_only)
11181095
strbuf_addbuf(out, &val);
@@ -1126,13 +1103,13 @@ static void format_trailer_info(const struct process_trailer_options *opts,
11261103
if (opts->separator && out->len != origlen) {
11271104
strbuf_addbuf(out, opts->separator);
11281105
}
1129-
strbuf_addstr(out, trailer);
1130-
if (opts->separator) {
1106+
strbuf_addstr(out, item->value);
1107+
if (opts->separator)
11311108
strbuf_rtrim(out);
1132-
}
1109+
else
1110+
strbuf_addch(out, '\n');
11331111
}
11341112
}
1135-
11361113
}
11371114

11381115
void format_trailers_from_commit(const struct process_trailer_options *opts,
@@ -1151,7 +1128,7 @@ void format_trailers_from_commit(const struct process_trailer_options *opts,
11511128
strbuf_add(out, msg + info.trailer_block_start,
11521129
info.trailer_block_end - info.trailer_block_start);
11531130
} else
1154-
format_trailer_info(opts, &info, out);
1131+
format_trailers(opts, &trailer_objects, out);
11551132

11561133
free_trailers(&trailer_objects);
11571134
trailer_info_release(&info);

trailer.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,10 @@ void format_trailers(const struct process_trailer_options *,
107107
void free_trailers(struct list_head *);
108108

109109
/*
110-
* Format the trailers from the commit msg "msg" into the strbuf "out".
111-
* Note two caveats about "opts":
112-
*
113-
* - this is primarily a helper for pretty.c, and not
114-
* all of the flags are supported.
115-
*
116-
* - this differs from process_trailers slightly in that we always format
117-
* only the trailer block itself, even if the "only_trailers" option is not
118-
* set.
110+
* Convenience function to format the trailers from the commit msg "msg" into
111+
* the strbuf "out". Reuses format_trailers() internally.
119112
*/
120-
void format_trailers_from_commit(const struct process_trailer_options *opts,
113+
void format_trailers_from_commit(const struct process_trailer_options *,
121114
const char *msg,
122115
struct strbuf *out);
123116

0 commit comments

Comments
 (0)