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 <tklauser@distanz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Tobias Klauser committed
Jan 14, 2016 at 17:57 UTC
d0d2344ad84cde7fddedc1e141296607af673454
1 file changed
+15
-13
trailer.c
+15
-13
@@ -108,23 +108,23 @@ static char last_non_space_char(const char *s)
108
return '\0';
109
}
110
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)
112
{
113
char c = last_non_space_char(tok);
114
if (!c)
115
return;
116
if (strchr(separators, c))
117
- printf("%s%s\n", tok, val);
117
+ fprintf(outfile, "%s%s\n", tok, val);
118
else
119
- printf("%s%c %s\n", tok, separators[0], val);
119
+ fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
120
}
121
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)
123
{
124
struct trailer_item *item;
125
for (item = first; item; item = item->next) {
126
if (!trim_empty || strlen(item->value) > 0)
127
- print_tok_val(item->token, item->value);
127
+ print_tok_val(outfile, item->token, item->value);
128
}
129
}
130
@@ -795,14 +795,15 @@ static int has_blank_line_before(struct strbuf **lines, int start)
795
return 0;
796
}
797
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)
799
{
800
int i;
801
for (i = start; lines[i] && i < end; i++)
802
- printf("%s", lines[i]->buf);
802
+ fprintf(outfile, "%s", lines[i]->buf);
803
}
804
805
-static int process_input_file(struct strbuf **lines,
805
+static int process_input_file(FILE *outfile,
806
+ struct strbuf **lines,
807
struct trailer_item **in_tok_first,
808
struct trailer_item **in_tok_last)
809
{
@@ -818,10 +819,10 @@ static int process_input_file(struct strbuf **lines,
819
trailer_start = find_trailer_start(lines, trailer_end);
820
821
/* Print lines before the trailers as is */
821
- print_lines(lines, 0, trailer_start);
822
+ print_lines(outfile, lines, 0, trailer_start);
823
824
if (!has_blank_line_before(lines, trailer_start - 1))
824
- printf("\n");
825
+ fprintf(outfile, "\n");
826
827
/* Parse trailer lines */
828
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
850
struct trailer_item *arg_tok_first;
851
struct strbuf **lines;
852
int trailer_end;
853
+ FILE *outfile = stdout;
854
855
/* Default config must be setup first */
856
git_config(git_trailer_default_config, NULL);
@@ -857,18 +859,18 @@ void process_trailers(const char *file, int trim_empty, struct string_list *trai
859
lines = read_input_file(file);
860
861
/* 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);
863
864
arg_tok_first = process_command_line_args(trailers);
865
866
process_trailers_lists(&in_tok_first, &in_tok_last, &arg_tok_first);
867
866
- print_all(in_tok_first, trim_empty);
868
+ print_all(outfile, in_tok_first, trim_empty);
869
870
free_all(&in_tok_first);
871
872
/* 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);
874
875
strbuf_list_free(lines);
876
}