interpret-trailers: add an option to show only existing trailers

It can be useful to invoke interpret-trailers for the primary purpose of parsing existing trailers. But in that case, we don't want to apply existing ifMissing or ifExists rules from the config. Let's add a special mode where we avoid applying those rules. Coupled with --only-trailers, this gives us a reasonable parsing tool. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 15, 2017 at 06:23 UTC fdbdb64f49959f9c83329554080934895f02ae59
5 files changed +34 -4
Documentation/git-interpret-trailers.txt
+5
@@ -83,6 +83,11 @@ OPTIONS
83 --only-trailers::
84 Output only the trailers, not any other parts of the input.
85
86 +--only-input::
87 + Output only trailers that exist in the input; do not add any
88 + from the command-line or by following configured `trailer.*`
89 + rules.
90 +
91 CONFIGURATION VARIABLES
92 -----------------------
93
builtin/interpret-trailers.c
+7
@@ -25,6 +25,7 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
25 OPT_BOOL(0, "in-place", &opts.in_place, N_("edit files in place")),
26 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")),
28 + OPT_BOOL(0, "only-input", &opts.only_input, N_("do not apply config rules")),
29 OPT_STRING_LIST(0, "trailer", &trailers, N_("trailer"),
30 N_("trailer(s) to add")),
31 OPT_END()
@@ -33,6 +34,12 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
34 argc = parse_options(argc, argv, prefix, options,
35 git_interpret_trailers_usage, 0);
36
37 + if (opts.only_input && trailers.nr)
38 + usage_msg_opt(
39 + _("--trailer with --only-input does not make sense"),
40 + git_interpret_trailers_usage,
41 + options);
42 +
43 if (argc) {
44 int i;
45 for (i = 0; i < argc; i++)
t/t7513-interpret-trailers.sh
+16
@@ -1314,4 +1314,20 @@ test_expect_success 'only-trailers omits non-trailer in middle of block' '
1314 test_cmp expected actual
1315 '
1316
1317 +test_expect_success 'only input' '
1318 + git config trailer.sign.command "echo config-value" &&
1319 + cat >expected <<-\EOF &&
1320 + existing: existing-value
1321 + EOF
1322 + git interpret-trailers \
1323 + --only-trailers --only-input >actual <<-\EOF &&
1324 + my subject
1325 +
1326 + my body
1327 +
1328 + existing: existing-value
1329 + EOF
1330 + test_cmp expected actual
1331 +'
1332 +
1333 test_done
trailer.c
+5 -4
@@ -976,7 +976,6 @@ void process_trailers(const char *file,
976 struct string_list *trailers)
977 {
978 LIST_HEAD(head);
979 - LIST_HEAD(arg_head);
979 struct strbuf sb = STRBUF_INIT;
980 int trailer_end;
981 FILE *outfile = stdout;
@@ -991,9 +990,11 @@ void process_trailers(const char *file,
990 /* Print the lines before the trailers */
991 trailer_end = process_input_file(outfile, sb.buf, &head, opts);
992
994 - process_command_line_args(&arg_head, trailers);
995 -
996 - process_trailers_lists(&head, &arg_head);
993 + if (!opts->only_input) {
994 + LIST_HEAD(arg_head);
995 + process_command_line_args(&arg_head, trailers);
996 + process_trailers_lists(&head, &arg_head);
997 + }
998
999 print_all(outfile, &head, opts);
1000
trailer.h
+1
@@ -26,6 +26,7 @@ struct process_trailer_options {
26 int in_place;
27 int trim_empty;
28 int only_trailers;
29 + int only_input;
30 };
31
32 #define PROCESS_TRAILER_OPTIONS_INIT {0}