Skip to content

Commit 676c1db

Browse files
Linus Arvergitster
authored andcommitted
trailer: begin formatting unification
Now that the preparatory refactors are over, we can replace the call to format_trailers() in interpret-trailers with format_trailer_info(). This unifies the trailer formatting machinery In order to avoid breakages in t7502 and t7513, we have to steal the features present in format_trailers(). Namely, we have to teach format_trailer_info() as follows: (1) make it aware of opts->trim_empty, and (2) make it avoid hardcoding ": " as the separator and space (which can result in double-printing these characters). For (2), make it only print the separator and space if we cannot find any recognized separator somewhere in the key (yes, keys may have a trailing separator in it --- we will eventually fix this design but not now). Do so by copying the code out of print_tok_val(), and deleting the same function. Helped-by: Junio C Hamano <[email protected]> Helped-by: Christian Couder <[email protected]> Signed-off-by: Linus Arver <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9f0c970 commit 676c1db

File tree

3 files changed

+19
-39
lines changed

3 files changed

+19
-39
lines changed

builtin/interpret-trailers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static void interpret_trailers(const struct process_trailer_options *opts,
171171
}
172172

173173
/* Print trailer block. */
174-
format_trailers(opts, &head, &trailer_block);
174+
format_trailer_info(opts, &head, &trailer_block);
175175
free_trailers(&head);
176176
fwrite(trailer_block.buf, 1, trailer_block.len, outfile);
177177
strbuf_release(&trailer_block);

trailer.c

Lines changed: 17 additions & 37 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,9 +1052,9 @@ 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-
struct list_head *trailers,
1089-
struct strbuf *out)
1055+
void format_trailer_info(const struct process_trailer_options *opts,
1056+
struct list_head *trailers,
1057+
struct strbuf *out)
10901058
{
10911059
size_t origlen = out->len;
10921060
struct list_head *pos;
@@ -1100,6 +1068,15 @@ static void format_trailer_info(const struct process_trailer_options *opts,
11001068
strbuf_addstr(&tok, item->token);
11011069
strbuf_addstr(&val, item->value);
11021070

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;
1079+
11031080
if (!opts->filter || opts->filter(&tok, opts->filter_data)) {
11041081
if (opts->separator && out->len != origlen)
11051082
strbuf_addbuf(out, opts->separator);
@@ -1108,8 +1085,11 @@ static void format_trailer_info(const struct process_trailer_options *opts,
11081085
if (!opts->key_only && !opts->value_only) {
11091086
if (opts->key_value_separator)
11101087
strbuf_addbuf(out, opts->key_value_separator);
1111-
else
1112-
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+
}
11131093
}
11141094
if (!opts->key_only)
11151095
strbuf_addbuf(out, &val);

trailer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void trailer_info_get(const struct process_trailer_options *,
101101
void trailer_info_release(struct trailer_info *info);
102102

103103
void trailer_config_init(void);
104-
void format_trailers(const struct process_trailer_options *,
104+
void format_trailer_info(const struct process_trailer_options *,
105105
struct list_head *trailers,
106106
struct strbuf *out);
107107
void free_trailers(struct list_head *);

0 commit comments

Comments
 (0)