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 <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Oct 13, 2014 at 20:16 UTC b1d78d77bfbf026e8222c02f1009767a466de0b1
2 files changed +75
trailer.c
+69
@@ -1,5 +1,6 @@
1 #include "cache.h"
2 #include "string-list.h"
3 +#include "trailer.h"
4 /*
5 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
6 */
@@ -87,6 +88,35 @@ static void free_trailer_item(struct trailer_item *item)
88 free(item);
89 }
90
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 +
120 static void update_last(struct trailer_item **last)
121 {
122 if (*last)
@@ -697,3 +727,42 @@ static int process_input_file(struct strbuf **lines,
727
728 return patch_start;
729 }
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 new
+6
@@ -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 */