Skip to content

Commit 6a8bb34

Browse files
committed
Merge branch 'la/trailer-cleanups'
Code clean-up. Keep only the first three clean-ups, and discard the rest to be replaced later. cf. <[email protected]> cf. <[email protected]> * la/trailer-cleanups: trailer: split process_command_line_args into separate functions trailer: split process_input_file into separate pieces trailer: separate public from internal portion of trailer_iterator
2 parents 6bdb5b1 + 94430d0 commit 6a8bb34

File tree

2 files changed

+52
-40
lines changed

2 files changed

+52
-40
lines changed

trailer.c

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -711,30 +711,35 @@ static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
711711
list_add_tail(&new_item->list, arg_head);
712712
}
713713

714-
static void process_command_line_args(struct list_head *arg_head,
715-
struct list_head *new_trailer_head)
714+
static void parse_trailers_from_config(struct list_head *config_head)
716715
{
717716
struct arg_item *item;
718-
struct strbuf tok = STRBUF_INIT;
719-
struct strbuf val = STRBUF_INIT;
720-
const struct conf_info *conf;
721717
struct list_head *pos;
722718

723-
/*
724-
* In command-line arguments, '=' is accepted (in addition to the
725-
* separators that are defined).
726-
*/
727-
char *cl_separators = xstrfmt("=%s", separators);
728-
729719
/* Add an arg item for each configured trailer with a command */
730720
list_for_each(pos, &conf_head) {
731721
item = list_entry(pos, struct arg_item, list);
732722
if (item->conf.command)
733-
add_arg_item(arg_head,
723+
add_arg_item(config_head,
734724
xstrdup(token_from_item(item, NULL)),
735725
xstrdup(""),
736726
&item->conf, NULL);
737727
}
728+
}
729+
730+
static void parse_trailers_from_command_line_args(struct list_head *arg_head,
731+
struct list_head *new_trailer_head)
732+
{
733+
struct strbuf tok = STRBUF_INIT;
734+
struct strbuf val = STRBUF_INIT;
735+
const struct conf_info *conf;
736+
struct list_head *pos;
737+
738+
/*
739+
* In command-line arguments, '=' is accepted (in addition to the
740+
* separators that are defined).
741+
*/
742+
char *cl_separators = xstrfmt("=%s", separators);
738743

739744
/* Add an arg item for each trailer on the command line */
740745
list_for_each(pos, new_trailer_head) {
@@ -961,28 +966,24 @@ static void unfold_value(struct strbuf *val)
961966
strbuf_release(&out);
962967
}
963968

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)
969+
/*
970+
* Parse trailers in "str", populating the trailer info and "head"
971+
* linked list structure.
972+
*/
973+
static void parse_trailers(struct trailer_info *info,
974+
const char *str,
975+
struct list_head *head,
976+
const struct process_trailer_options *opts)
968977
{
969-
struct trailer_info info;
970978
struct strbuf tok = STRBUF_INIT;
971979
struct strbuf val = STRBUF_INIT;
972980
size_t i;
973981

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);
982+
trailer_info_get(info, str, opts);
979983

980-
if (!opts->only_trailers && !info.blank_line_before_trailer)
981-
fprintf(outfile, "\n");
982-
983-
for (i = 0; i < info.trailer_nr; i++) {
984+
for (i = 0; i < info->trailer_nr; i++) {
984985
int separator_pos;
985-
char *trailer = info.trailers[i];
986+
char *trailer = info->trailers[i];
986987
if (trailer[0] == comment_line_char)
987988
continue;
988989
separator_pos = find_separator(trailer, separators);
@@ -1002,10 +1003,6 @@ static size_t process_input_file(FILE *outfile,
10021003
strbuf_detach(&val, NULL));
10031004
}
10041005
}
1005-
1006-
trailer_info_release(&info);
1007-
1008-
return info.trailer_end - str;
10091006
}
10101007

10111008
static void free_all(struct list_head *head)
@@ -1054,6 +1051,7 @@ void process_trailers(const char *file,
10541051
{
10551052
LIST_HEAD(head);
10561053
struct strbuf sb = STRBUF_INIT;
1054+
struct trailer_info info;
10571055
size_t trailer_end;
10581056
FILE *outfile = stdout;
10591057

@@ -1064,18 +1062,30 @@ void process_trailers(const char *file,
10641062
if (opts->in_place)
10651063
outfile = create_in_place_tempfile(file);
10661064

1065+
parse_trailers(&info, sb.buf, &head, opts);
1066+
trailer_end = info.trailer_end - sb.buf;
1067+
10671068
/* Print the lines before the trailers */
1068-
trailer_end = process_input_file(outfile, sb.buf, &head, opts);
1069+
if (!opts->only_trailers)
1070+
fwrite(sb.buf, 1, info.trailer_start - sb.buf, outfile);
1071+
1072+
if (!opts->only_trailers && !info.blank_line_before_trailer)
1073+
fprintf(outfile, "\n");
1074+
10691075

10701076
if (!opts->only_input) {
1077+
LIST_HEAD(config_head);
10711078
LIST_HEAD(arg_head);
1072-
process_command_line_args(&arg_head, new_trailer_head);
1079+
parse_trailers_from_config(&config_head);
1080+
parse_trailers_from_command_line_args(&arg_head, new_trailer_head);
1081+
list_splice(&config_head, &arg_head);
10731082
process_trailers_lists(&head, &arg_head);
10741083
}
10751084

10761085
print_all(outfile, &head, opts);
10771086

10781087
free_all(&head);
1088+
trailer_info_release(&info);
10791089

10801090
/* Print the lines after the trailers as is */
10811091
if (!opts->only_trailers)
@@ -1220,14 +1230,14 @@ void trailer_iterator_init(struct trailer_iterator *iter, const char *msg)
12201230
strbuf_init(&iter->key, 0);
12211231
strbuf_init(&iter->val, 0);
12221232
opts.no_divider = 1;
1223-
trailer_info_get(&iter->info, msg, &opts);
1224-
iter->cur = 0;
1233+
trailer_info_get(&iter->internal.info, msg, &opts);
1234+
iter->internal.cur = 0;
12251235
}
12261236

12271237
int trailer_iterator_advance(struct trailer_iterator *iter)
12281238
{
1229-
while (iter->cur < iter->info.trailer_nr) {
1230-
char *trailer = iter->info.trailers[iter->cur++];
1239+
while (iter->internal.cur < iter->internal.info.trailer_nr) {
1240+
char *trailer = iter->internal.info.trailers[iter->internal.cur++];
12311241
int separator_pos = find_separator(trailer, separators);
12321242

12331243
if (separator_pos < 1)
@@ -1245,7 +1255,7 @@ int trailer_iterator_advance(struct trailer_iterator *iter)
12451255

12461256
void trailer_iterator_release(struct trailer_iterator *iter)
12471257
{
1248-
trailer_info_release(&iter->info);
1258+
trailer_info_release(&iter->internal.info);
12491259
strbuf_release(&iter->val);
12501260
strbuf_release(&iter->key);
12511261
}

trailer.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ struct trailer_iterator {
119119
struct strbuf val;
120120

121121
/* private */
122-
struct trailer_info info;
123-
size_t cur;
122+
struct {
123+
struct trailer_info info;
124+
size_t cur;
125+
} internal;
124126
};
125127

126128
/*

0 commit comments

Comments
 (0)