Skip to content

Commit d0d2344

Browse files
tklausergitster
authored andcommitted
trailer: allow to write to files other than stdout
Use fprintf instead of printf in trailer.c in order to allow printing to a file other than stdout. This will be needed to support in-place editing in git interpret-trailers. Signed-off-by: Tobias Klauser <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7548842 commit d0d2344

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

trailer.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,23 @@ static char last_non_space_char(const char *s)
108108
return '\0';
109109
}
110110

111-
static void print_tok_val(const char *tok, const char *val)
111+
static void print_tok_val(FILE *outfile, const char *tok, const char *val)
112112
{
113113
char c = last_non_space_char(tok);
114114
if (!c)
115115
return;
116116
if (strchr(separators, c))
117-
printf("%s%s\n", tok, val);
117+
fprintf(outfile, "%s%s\n", tok, val);
118118
else
119-
printf("%s%c %s\n", tok, separators[0], val);
119+
fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
120120
}
121121

122-
static void print_all(struct trailer_item *first, int trim_empty)
122+
static void print_all(FILE *outfile, struct trailer_item *first, int trim_empty)
123123
{
124124
struct trailer_item *item;
125125
for (item = first; item; item = item->next) {
126126
if (!trim_empty || strlen(item->value) > 0)
127-
print_tok_val(item->token, item->value);
127+
print_tok_val(outfile, item->token, item->value);
128128
}
129129
}
130130

@@ -795,14 +795,15 @@ static int has_blank_line_before(struct strbuf **lines, int start)
795795
return 0;
796796
}
797797

798-
static void print_lines(struct strbuf **lines, int start, int end)
798+
static void print_lines(FILE *outfile, struct strbuf **lines, int start, int end)
799799
{
800800
int i;
801801
for (i = start; lines[i] && i < end; i++)
802-
printf("%s", lines[i]->buf);
802+
fprintf(outfile, "%s", lines[i]->buf);
803803
}
804804

805-
static int process_input_file(struct strbuf **lines,
805+
static int process_input_file(FILE *outfile,
806+
struct strbuf **lines,
806807
struct trailer_item **in_tok_first,
807808
struct trailer_item **in_tok_last)
808809
{
@@ -818,10 +819,10 @@ static int process_input_file(struct strbuf **lines,
818819
trailer_start = find_trailer_start(lines, trailer_end);
819820

820821
/* Print lines before the trailers as is */
821-
print_lines(lines, 0, trailer_start);
822+
print_lines(outfile, lines, 0, trailer_start);
822823

823824
if (!has_blank_line_before(lines, trailer_start - 1))
824-
printf("\n");
825+
fprintf(outfile, "\n");
825826

826827
/* Parse trailer lines */
827828
for (i = trailer_start; i < trailer_end; i++) {
@@ -849,6 +850,7 @@ void process_trailers(const char *file, int trim_empty, struct string_list *trai
849850
struct trailer_item *arg_tok_first;
850851
struct strbuf **lines;
851852
int trailer_end;
853+
FILE *outfile = stdout;
852854

853855
/* Default config must be setup first */
854856
git_config(git_trailer_default_config, NULL);
@@ -857,18 +859,18 @@ void process_trailers(const char *file, int trim_empty, struct string_list *trai
857859
lines = read_input_file(file);
858860

859861
/* Print the lines before the trailers */
860-
trailer_end = process_input_file(lines, &in_tok_first, &in_tok_last);
862+
trailer_end = process_input_file(outfile, lines, &in_tok_first, &in_tok_last);
861863

862864
arg_tok_first = process_command_line_args(trailers);
863865

864866
process_trailers_lists(&in_tok_first, &in_tok_last, &arg_tok_first);
865867

866-
print_all(in_tok_first, trim_empty);
868+
print_all(outfile, in_tok_first, trim_empty);
867869

868870
free_all(&in_tok_first);
869871

870872
/* Print the lines after the trailers as is */
871-
print_lines(lines, trailer_end, INT_MAX);
873+
print_lines(outfile, lines, trailer_end, INT_MAX);
872874

873875
strbuf_list_free(lines);
874876
}

0 commit comments

Comments
 (0)