Skip to content

Commit fdbdb64

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 56c493e commit fdbdb64

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

Documentation/git-interpret-trailers.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ OPTIONS
8383
--only-trailers::
8484
Output only the trailers, not any other parts of the input.
8585

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+
8691
CONFIGURATION VARIABLES
8792
-----------------------
8893

builtin/interpret-trailers.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
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")),
2727
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")),
2829
OPT_STRING_LIST(0, "trailer", &trailers, N_("trailer"),
2930
N_("trailer(s) to add")),
3031
OPT_END()
@@ -33,6 +34,12 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
3334
argc = parse_options(argc, argv, prefix, options,
3435
git_interpret_trailers_usage, 0);
3536

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+
3643
if (argc) {
3744
int i;
3845
for (i = 0; i < argc; i++)

t/t7513-interpret-trailers.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,4 +1314,20 @@ test_expect_success 'only-trailers omits non-trailer in middle of block' '
13141314
test_cmp expected actual
13151315
'
13161316

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+
13171333
test_done

trailer.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,6 @@ void process_trailers(const char *file,
976976
struct string_list *trailers)
977977
{
978978
LIST_HEAD(head);
979-
LIST_HEAD(arg_head);
980979
struct strbuf sb = STRBUF_INIT;
981980
int trailer_end;
982981
FILE *outfile = stdout;
@@ -991,9 +990,11 @@ void process_trailers(const char *file,
991990
/* Print the lines before the trailers */
992991
trailer_end = process_input_file(outfile, sb.buf, &head, opts);
993992

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+
}
997998

998999
print_all(outfile, &head, opts);
9991000

trailer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct process_trailer_options {
2626
int in_place;
2727
int trim_empty;
2828
int only_trailers;
29+
int only_input;
2930
};
3031

3132
#define PROCESS_TRAILER_OPTIONS_INIT {0}

0 commit comments

Comments
 (0)