trailer: split process_input_file into separate pieces

Currently, process_input_file does three things: (1) parse the input string for trailers, (2) print text before the trailers, and (3) calculate the position of the input where the trailers end. Rename this function to parse_trailers(), and make it only do (1). The caller of this function, process_trailers, becomes responsible for (2) and (3). These items belong inside process_trailers because they are both concerned with printing the surrounding text around trailers (which is already one of the immediate concerns of process_trailers). Signed-off-by: Linus Arver <linusa@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Arver committed Sep 9, 2023 at 06:16 UTC c2a8edf997a8d9f3f005666f63105172a23bd3a0
1 file changed +22 -20
trailer.c
+22 -20
@@ -961,28 +961,24 @@ static void unfold_value(struct strbuf *val)
961 strbuf_release(&out);
962 }
963
964 -static size_t process_input_file(FILE *outfile,
965 - const char *str,
966 - struct list_head *head,
967 - const struct process_trailer_options *opts)
964 +/*
965 + * Parse trailers in "str", populating the trailer info and "head"
966 + * linked list structure.
967 + */
968 +static void parse_trailers(struct trailer_info *info,
969 + const char *str,
970 + struct list_head *head,
971 + const struct process_trailer_options *opts)
972 {
969 - struct trailer_info info;
973 struct strbuf tok = STRBUF_INIT;
974 struct strbuf val = STRBUF_INIT;
975 size_t i;
976
974 - trailer_info_get(&info, str, opts);
975 -
976 - /* Print lines before the trailers as is */
977 - if (!opts->only_trailers)
978 - fwrite(str, 1, info.trailer_start - str, outfile);
977 + trailer_info_get(info, str, opts);
978
980 - if (!opts->only_trailers && !info.blank_line_before_trailer)
981 - fprintf(outfile, "\n");
982 -
983 - for (i = 0; i < info.trailer_nr; i++) {
979 + for (i = 0; i < info->trailer_nr; i++) {
980 int separator_pos;
985 - char *trailer = info.trailers[i];
981 + char *trailer = info->trailers[i];
982 if (trailer[0] == comment_line_char)
983 continue;
984 separator_pos = find_separator(trailer, separators);
@@ -1002,10 +998,6 @@ static size_t process_input_file(FILE *outfile,
998 strbuf_detach(&val, NULL));
999 }
1000 }
1005 -
1006 - trailer_info_release(&info);
1007 -
1008 - return info.trailer_end - str;
1001 }
1002
1003 static void free_all(struct list_head *head)
@@ -1054,6 +1046,7 @@ void process_trailers(const char *file,
1046 {
1047 LIST_HEAD(head);
1048 struct strbuf sb = STRBUF_INIT;
1049 + struct trailer_info info;
1050 size_t trailer_end;
1051 FILE *outfile = stdout;
1052
@@ -1064,8 +1057,16 @@ void process_trailers(const char *file,
1057 if (opts->in_place)
1058 outfile = create_in_place_tempfile(file);
1059
1060 + parse_trailers(&info, sb.buf, &head, opts);
1061 + trailer_end = info.trailer_end - sb.buf;
1062 +
1063 /* Print the lines before the trailers */
1068 - trailer_end = process_input_file(outfile, sb.buf, &head, opts);
1064 + if (!opts->only_trailers)
1065 + fwrite(sb.buf, 1, info.trailer_start - sb.buf, outfile);
1066 +
1067 + if (!opts->only_trailers && !info.blank_line_before_trailer)
1068 + fprintf(outfile, "\n");
1069 +
1070
1071 if (!opts->only_input) {
1072 LIST_HEAD(arg_head);
@@ -1076,6 +1077,7 @@ void process_trailers(const char *file,
1077 print_all(outfile, &head, opts);
1078
1079 free_all(&head);
1080 + trailer_info_release(&info);
1081
1082 /* Print the lines after the trailers as is */
1083 if (!opts->only_trailers)