Skip to content

Commit b1d78d7

Browse files
chriscoolgitster
authored andcommitted
trailer: put all the processing together and print
This patch adds the process_trailers() function that calls all the previously added processing functions and then prints the results on the standard output. Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2013d85 commit b1d78d7

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

trailer.c

+69
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "string-list.h"
3+
#include "trailer.h"
34
/*
45
* Copyright (c) 2013, 2014 Christian Couder <[email protected]>
56
*/
@@ -87,6 +88,35 @@ static void free_trailer_item(struct trailer_item *item)
8788
free(item);
8889
}
8990

91+
static char last_non_space_char(const char *s)
92+
{
93+
int i;
94+
for (i = strlen(s) - 1; i >= 0; i--)
95+
if (!isspace(s[i]))
96+
return s[i];
97+
return '\0';
98+
}
99+
100+
static void print_tok_val(const char *tok, const char *val)
101+
{
102+
char c = last_non_space_char(tok);
103+
if (!c)
104+
return;
105+
if (strchr(separators, c))
106+
printf("%s%s\n", tok, val);
107+
else
108+
printf("%s%c %s\n", tok, separators[0], val);
109+
}
110+
111+
static void print_all(struct trailer_item *first, int trim_empty)
112+
{
113+
struct trailer_item *item;
114+
for (item = first; item; item = item->next) {
115+
if (!trim_empty || strlen(item->value) > 0)
116+
print_tok_val(item->token, item->value);
117+
}
118+
}
119+
90120
static void update_last(struct trailer_item **last)
91121
{
92122
if (*last)
@@ -697,3 +727,42 @@ static int process_input_file(struct strbuf **lines,
697727

698728
return patch_start;
699729
}
730+
731+
static void free_all(struct trailer_item **first)
732+
{
733+
while (*first) {
734+
struct trailer_item *item = remove_first(first);
735+
free_trailer_item(item);
736+
}
737+
}
738+
739+
void process_trailers(const char *file, int trim_empty, struct string_list *trailers)
740+
{
741+
struct trailer_item *in_tok_first = NULL;
742+
struct trailer_item *in_tok_last = NULL;
743+
struct trailer_item *arg_tok_first;
744+
struct strbuf **lines;
745+
int patch_start;
746+
747+
/* Default config must be setup first */
748+
git_config(git_trailer_default_config, NULL);
749+
git_config(git_trailer_config, NULL);
750+
751+
lines = read_input_file(file);
752+
753+
/* Print the lines before the trailers */
754+
patch_start = process_input_file(lines, &in_tok_first, &in_tok_last);
755+
756+
arg_tok_first = process_command_line_args(trailers);
757+
758+
process_trailers_lists(&in_tok_first, &in_tok_last, &arg_tok_first);
759+
760+
print_all(in_tok_first, trim_empty);
761+
762+
free_all(&in_tok_first);
763+
764+
/* Print the lines after the trailers as is */
765+
print_lines(lines, patch_start, INT_MAX);
766+
767+
strbuf_list_free(lines);
768+
}

trailer.h

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef TRAILER_H
2+
#define TRAILER_H
3+
4+
void process_trailers(const char *file, int trim_empty, struct string_list *trailers);
5+
6+
#endif /* TRAILER_H */

0 commit comments

Comments
 (0)