Merge branch 'la/trailer-cleanups'

Code clean-up.

Keep only the first three clean-ups, and discard the rest to be replaced later.
cf. <owly1qetjqo1.fsf@fine.c.googlers.com>
cf. <owlyzg1dsswr.fsf@fine.c.googlers.com>

* 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
maint
Junio C Hamano 2023-09-22 17:01:36 -07:00
commit 6a8bb340f2
2 changed files with 52 additions and 40 deletions

View File

@ -711,10 +711,25 @@ static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
list_add_tail(&new_item->list, arg_head);
}

static void process_command_line_args(struct list_head *arg_head,
struct list_head *new_trailer_head)
static void parse_trailers_from_config(struct list_head *config_head)
{
struct arg_item *item;
struct list_head *pos;

/* Add an arg item for each configured trailer with a command */
list_for_each(pos, &conf_head) {
item = list_entry(pos, struct arg_item, list);
if (item->conf.command)
add_arg_item(config_head,
xstrdup(token_from_item(item, NULL)),
xstrdup(""),
&item->conf, NULL);
}
}

static void parse_trailers_from_command_line_args(struct list_head *arg_head,
struct list_head *new_trailer_head)
{
struct strbuf tok = STRBUF_INIT;
struct strbuf val = STRBUF_INIT;
const struct conf_info *conf;
@ -726,16 +741,6 @@ static void process_command_line_args(struct list_head *arg_head,
*/
char *cl_separators = xstrfmt("=%s", separators);

/* Add an arg item for each configured trailer with a command */
list_for_each(pos, &conf_head) {
item = list_entry(pos, struct arg_item, list);
if (item->conf.command)
add_arg_item(arg_head,
xstrdup(token_from_item(item, NULL)),
xstrdup(""),
&item->conf, NULL);
}

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

static size_t process_input_file(FILE *outfile,
const char *str,
struct list_head *head,
const struct process_trailer_options *opts)
/*
* Parse trailers in "str", populating the trailer info and "head"
* linked list structure.
*/
static void parse_trailers(struct trailer_info *info,
const char *str,
struct list_head *head,
const struct process_trailer_options *opts)
{
struct trailer_info info;
struct strbuf tok = STRBUF_INIT;
struct strbuf val = STRBUF_INIT;
size_t i;

trailer_info_get(&info, str, opts);
trailer_info_get(info, str, opts);

/* Print lines before the trailers as is */
if (!opts->only_trailers)
fwrite(str, 1, info.trailer_start - str, outfile);

if (!opts->only_trailers && !info.blank_line_before_trailer)
fprintf(outfile, "\n");

for (i = 0; i < info.trailer_nr; i++) {
for (i = 0; i < info->trailer_nr; i++) {
int separator_pos;
char *trailer = info.trailers[i];
char *trailer = info->trailers[i];
if (trailer[0] == comment_line_char)
continue;
separator_pos = find_separator(trailer, separators);
@ -1002,10 +1003,6 @@ static size_t process_input_file(FILE *outfile,
strbuf_detach(&val, NULL));
}
}

trailer_info_release(&info);

return info.trailer_end - str;
}

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

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

parse_trailers(&info, sb.buf, &head, opts);
trailer_end = info.trailer_end - sb.buf;

/* Print the lines before the trailers */
trailer_end = process_input_file(outfile, sb.buf, &head, opts);
if (!opts->only_trailers)
fwrite(sb.buf, 1, info.trailer_start - sb.buf, outfile);

if (!opts->only_trailers && !info.blank_line_before_trailer)
fprintf(outfile, "\n");


if (!opts->only_input) {
LIST_HEAD(config_head);
LIST_HEAD(arg_head);
process_command_line_args(&arg_head, new_trailer_head);
parse_trailers_from_config(&config_head);
parse_trailers_from_command_line_args(&arg_head, new_trailer_head);
list_splice(&config_head, &arg_head);
process_trailers_lists(&head, &arg_head);
}

print_all(outfile, &head, opts);

free_all(&head);
trailer_info_release(&info);

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

int trailer_iterator_advance(struct trailer_iterator *iter)
{
while (iter->cur < iter->info.trailer_nr) {
char *trailer = iter->info.trailers[iter->cur++];
while (iter->internal.cur < iter->internal.info.trailer_nr) {
char *trailer = iter->internal.info.trailers[iter->internal.cur++];
int separator_pos = find_separator(trailer, separators);

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

void trailer_iterator_release(struct trailer_iterator *iter)
{
trailer_info_release(&iter->info);
trailer_info_release(&iter->internal.info);
strbuf_release(&iter->val);
strbuf_release(&iter->key);
}

View File

@ -119,8 +119,10 @@ struct trailer_iterator {
struct strbuf val;

/* private */
struct trailer_info info;
size_t cur;
struct {
struct trailer_info info;
size_t cur;
} internal;
};

/*