Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Tobias Klauser authored and Junio C Hamano committed Jan 14, 2016
1 parent 7548842 commit d0d2344
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions trailer.c
Expand Up @@ -108,23 +108,23 @@ static char last_non_space_char(const char *s)
return '\0';
}

static void print_tok_val(const char *tok, const char *val)
static void print_tok_val(FILE *outfile, const char *tok, const char *val)
{
char c = last_non_space_char(tok);
if (!c)
return;
if (strchr(separators, c))
printf("%s%s\n", tok, val);
fprintf(outfile, "%s%s\n", tok, val);
else
printf("%s%c %s\n", tok, separators[0], val);
fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
}

static void print_all(struct trailer_item *first, int trim_empty)
static void print_all(FILE *outfile, struct trailer_item *first, int trim_empty)
{
struct trailer_item *item;
for (item = first; item; item = item->next) {
if (!trim_empty || strlen(item->value) > 0)
print_tok_val(item->token, item->value);
print_tok_val(outfile, item->token, item->value);
}
}

Expand Down Expand Up @@ -795,14 +795,15 @@ static int has_blank_line_before(struct strbuf **lines, int start)
return 0;
}

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

static int process_input_file(struct strbuf **lines,
static int process_input_file(FILE *outfile,
struct strbuf **lines,
struct trailer_item **in_tok_first,
struct trailer_item **in_tok_last)
{
Expand All @@ -818,10 +819,10 @@ static int process_input_file(struct strbuf **lines,
trailer_start = find_trailer_start(lines, trailer_end);

/* Print lines before the trailers as is */
print_lines(lines, 0, trailer_start);
print_lines(outfile, lines, 0, trailer_start);

if (!has_blank_line_before(lines, trailer_start - 1))
printf("\n");
fprintf(outfile, "\n");

/* Parse trailer lines */
for (i = trailer_start; i < trailer_end; i++) {
Expand Down Expand Up @@ -849,6 +850,7 @@ void process_trailers(const char *file, int trim_empty, struct string_list *trai
struct trailer_item *arg_tok_first;
struct strbuf **lines;
int trailer_end;
FILE *outfile = stdout;

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

/* Print the lines before the trailers */
trailer_end = process_input_file(lines, &in_tok_first, &in_tok_last);
trailer_end = process_input_file(outfile, lines, &in_tok_first, &in_tok_last);

arg_tok_first = process_command_line_args(trailers);

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

print_all(in_tok_first, trim_empty);
print_all(outfile, in_tok_first, trim_empty);

free_all(&in_tok_first);

/* Print the lines after the trailers as is */
print_lines(lines, trailer_end, INT_MAX);
print_lines(outfile, lines, trailer_end, INT_MAX);

strbuf_list_free(lines);
}

0 comments on commit d0d2344

Please sign in to comment.