Skip to content

Commit 56c493e

Browse files
peffgitster
authored andcommitted
interpret-trailers: add an option to show only the trailers
In theory it's easy for any reader who wants to parse trailers to do so. But there are a lot of subtle corner cases around what counts as a trailer, when the trailer block begins and ends, etc. Since interpret-trailers already has our parsing logic, let's let callers ask it to just output the trailers. They still have to parse the "key: value" lines, but at least they can ignore all of the other corner cases. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8abc898 commit 56c493e

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

Documentation/git-interpret-trailers.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ OPTIONS
8080
trailer to the input messages. See the description of this
8181
command.
8282

83+
--only-trailers::
84+
Output only the trailers, not any other parts of the input.
85+
8386
CONFIGURATION VARIABLES
8487
-----------------------
8588

builtin/interpret-trailers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
2424
struct option options[] = {
2525
OPT_BOOL(0, "in-place", &opts.in_place, N_("edit files in place")),
2626
OPT_BOOL(0, "trim-empty", &opts.trim_empty, N_("trim empty trailers")),
27+
OPT_BOOL(0, "only-trailers", &opts.only_trailers, N_("output only the trailers")),
2728
OPT_STRING_LIST(0, "trailer", &trailers, N_("trailer"),
2829
N_("trailer(s) to add")),
2930
OPT_END()

t/t7513-interpret-trailers.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,4 +1275,43 @@ test_expect_success 'with cut line' '
12751275
test_cmp expected actual
12761276
'
12771277

1278+
test_expect_success 'only trailers' '
1279+
git config trailer.sign.command "echo config-value" &&
1280+
cat >expected <<-\EOF &&
1281+
existing: existing-value
1282+
sign: config-value
1283+
added: added-value
1284+
EOF
1285+
git interpret-trailers \
1286+
--trailer added:added-value \
1287+
--only-trailers >actual <<-\EOF &&
1288+
my subject
1289+
1290+
my body
1291+
1292+
existing: existing-value
1293+
EOF
1294+
test_cmp expected actual
1295+
'
1296+
1297+
test_expect_success 'only-trailers omits non-trailer in middle of block' '
1298+
git config trailer.sign.command "echo config-value" &&
1299+
cat >expected <<-\EOF &&
1300+
Signed-off-by: nobody <nobody@nowhere>
1301+
Signed-off-by: somebody <somebody@somewhere>
1302+
sign: config-value
1303+
EOF
1304+
git interpret-trailers --only-trailers >actual <<-\EOF &&
1305+
subject
1306+
1307+
it is important that the trailers below are signed-off-by
1308+
so that they meet the "25% trailers Git knows about" heuristic
1309+
1310+
Signed-off-by: nobody <nobody@nowhere>
1311+
this is not a trailer
1312+
Signed-off-by: somebody <somebody@somewhere>
1313+
EOF
1314+
test_cmp expected actual
1315+
'
1316+
12781317
test_done

trailer.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,15 @@ static void print_tok_val(FILE *outfile, const char *tok, const char *val)
163163
fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
164164
}
165165

166-
static void print_all(FILE *outfile, struct list_head *head, int trim_empty)
166+
static void print_all(FILE *outfile, struct list_head *head,
167+
const struct process_trailer_options *opts)
167168
{
168169
struct list_head *pos;
169170
struct trailer_item *item;
170171
list_for_each(pos, head) {
171172
item = list_entry(pos, struct trailer_item, list);
172-
if (!trim_empty || strlen(item->value) > 0)
173+
if ((!opts->trim_empty || strlen(item->value) > 0) &&
174+
(!opts->only_trailers || item->token))
173175
print_tok_val(outfile, item->token, item->value);
174176
}
175177
}
@@ -886,7 +888,8 @@ static int ends_with_blank_line(const char *buf, size_t len)
886888

887889
static int process_input_file(FILE *outfile,
888890
const char *str,
889-
struct list_head *head)
891+
struct list_head *head,
892+
const struct process_trailer_options *opts)
890893
{
891894
struct trailer_info info;
892895
struct strbuf tok = STRBUF_INIT;
@@ -896,9 +899,10 @@ static int process_input_file(FILE *outfile,
896899
trailer_info_get(&info, str);
897900

898901
/* Print lines before the trailers as is */
899-
fwrite(str, 1, info.trailer_start - str, outfile);
902+
if (!opts->only_trailers)
903+
fwrite(str, 1, info.trailer_start - str, outfile);
900904

901-
if (!info.blank_line_before_trailer)
905+
if (!opts->only_trailers && !info.blank_line_before_trailer)
902906
fprintf(outfile, "\n");
903907

904908
for (i = 0; i < info.trailer_nr; i++) {
@@ -913,7 +917,7 @@ static int process_input_file(FILE *outfile,
913917
add_trailer_item(head,
914918
strbuf_detach(&tok, NULL),
915919
strbuf_detach(&val, NULL));
916-
} else {
920+
} else if (!opts->only_trailers) {
917921
strbuf_addstr(&val, trailer);
918922
strbuf_strip_suffix(&val, "\n");
919923
add_trailer_item(head,
@@ -985,18 +989,19 @@ void process_trailers(const char *file,
985989
outfile = create_in_place_tempfile(file);
986990

987991
/* Print the lines before the trailers */
988-
trailer_end = process_input_file(outfile, sb.buf, &head);
992+
trailer_end = process_input_file(outfile, sb.buf, &head, opts);
989993

990994
process_command_line_args(&arg_head, trailers);
991995

992996
process_trailers_lists(&head, &arg_head);
993997

994-
print_all(outfile, &head, opts->trim_empty);
998+
print_all(outfile, &head, opts);
995999

9961000
free_all(&head);
9971001

9981002
/* Print the lines after the trailers as is */
999-
fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
1003+
if (!opts->only_trailers)
1004+
fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
10001005

10011006
if (opts->in_place)
10021007
if (rename_tempfile(&trailers_tempfile, file))

trailer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct trailer_info {
2525
struct process_trailer_options {
2626
int in_place;
2727
int trim_empty;
28+
int only_trailers;
2829
};
2930

3031
#define PROCESS_TRAILER_OPTIONS_INIT {0}

0 commit comments

Comments
 (0)