Skip to content

Commit 51166b8

Browse files
bonzinigitster
authored andcommitted
trailers: introduce struct new_trailer_item
This will provide a place to store the current state of the --where, --if-exists and --if-missing options. Signed-off-by: Paolo Bonzini <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 52fc319 commit 51166b8

File tree

3 files changed

+61
-13
lines changed

3 files changed

+61
-13
lines changed

builtin/interpret-trailers.c

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,50 @@ static const char * const git_interpret_trailers_usage[] = {
1616
NULL
1717
};
1818

19+
static void new_trailers_clear(struct list_head *trailers)
20+
{
21+
struct list_head *pos, *tmp;
22+
struct new_trailer_item *item;
23+
24+
list_for_each_safe(pos, tmp, trailers) {
25+
item = list_entry(pos, struct new_trailer_item, list);
26+
list_del(pos);
27+
free(item);
28+
}
29+
}
30+
31+
static int option_parse_trailer(const struct option *opt,
32+
const char *arg, int unset)
33+
{
34+
struct list_head *trailers = opt->value;
35+
struct new_trailer_item *item;
36+
37+
if (unset) {
38+
new_trailers_clear(trailers);
39+
return 0;
40+
}
41+
42+
if (!arg)
43+
return -1;
44+
45+
item = xmalloc(sizeof(*item));
46+
item->text = arg;
47+
list_add_tail(&item->list, trailers);
48+
return 0;
49+
}
50+
1951
int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
2052
{
2153
int in_place = 0;
2254
int trim_empty = 0;
23-
struct string_list trailers = STRING_LIST_INIT_NODUP;
55+
LIST_HEAD(trailers);
2456

2557
struct option options[] = {
2658
OPT_BOOL(0, "in-place", &in_place, N_("edit files in place")),
2759
OPT_BOOL(0, "trim-empty", &trim_empty, N_("trim empty trailers")),
28-
OPT_STRING_LIST(0, "trailer", &trailers, N_("trailer"),
29-
N_("trailer(s) to add")),
60+
61+
OPT_CALLBACK(0, "trailer", &trailers, N_("trailer"),
62+
N_("trailer(s) to add"), option_parse_trailer),
3063
OPT_END()
3164
};
3265

@@ -43,7 +76,7 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
4376
process_trailers(NULL, in_place, trim_empty, &trailers);
4477
}
4578

46-
string_list_clear(&trailers, 0);
79+
new_trailers_clear(&trailers);
4780

4881
return 0;
4982
}

trailer.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,8 @@ static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
669669
}
670670

671671
static void process_command_line_args(struct list_head *arg_head,
672-
struct string_list *trailers)
672+
struct list_head *new_trailer_head)
673673
{
674-
struct string_list_item *tr;
675674
struct arg_item *item;
676675
struct strbuf tok = STRBUF_INIT;
677676
struct strbuf val = STRBUF_INIT;
@@ -695,17 +694,20 @@ static void process_command_line_args(struct list_head *arg_head,
695694
}
696695

697696
/* Add an arg item for each trailer on the command line */
698-
for_each_string_list_item(tr, trailers) {
699-
int separator_pos = find_separator(tr->string, cl_separators);
697+
list_for_each(pos, new_trailer_head) {
698+
struct new_trailer_item *tr =
699+
list_entry(pos, struct new_trailer_item, list);
700+
int separator_pos = find_separator(tr->text, cl_separators);
701+
700702
if (separator_pos == 0) {
701703
struct strbuf sb = STRBUF_INIT;
702-
strbuf_addstr(&sb, tr->string);
704+
strbuf_addstr(&sb, tr->text);
703705
strbuf_trim(&sb);
704706
error(_("empty trailer token in trailer '%.*s'"),
705707
(int) sb.len, sb.buf);
706708
strbuf_release(&sb);
707709
} else {
708-
parse_trailer(&tok, &val, &conf, tr->string,
710+
parse_trailer(&tok, &val, &conf, tr->text,
709711
separator_pos);
710712
add_arg_item(arg_head,
711713
strbuf_detach(&tok, NULL),
@@ -969,7 +971,8 @@ static FILE *create_in_place_tempfile(const char *file)
969971
return outfile;
970972
}
971973

972-
void process_trailers(const char *file, int in_place, int trim_empty, struct string_list *trailers)
974+
void process_trailers(const char *file, int in_place, int trim_empty,
975+
struct list_head *new_trailer_head)
973976
{
974977
LIST_HEAD(head);
975978
LIST_HEAD(arg_head);
@@ -987,7 +990,7 @@ void process_trailers(const char *file, int in_place, int trim_empty, struct str
987990
/* Print the lines before the trailers */
988991
trailer_end = process_input_file(outfile, sb.buf, &head);
989992

990-
process_command_line_args(&arg_head, trailers);
993+
process_command_line_args(&arg_head, new_trailer_head);
991994

992995
process_trailers_lists(&head, &arg_head);
993996

trailer.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef TRAILER_H
22
#define TRAILER_H
33

4+
#include "list.h"
5+
46
enum trailer_where {
57
WHERE_END,
68
WHERE_AFTER,
@@ -44,8 +46,18 @@ struct trailer_info {
4446
size_t trailer_nr;
4547
};
4648

49+
/*
50+
* A list that represents newly-added trailers, such as those provided
51+
* with the --trailer command line option of git-interpret-trailers.
52+
*/
53+
struct new_trailer_item {
54+
struct list_head list;
55+
56+
const char *text;
57+
};
58+
4759
void process_trailers(const char *file, int in_place, int trim_empty,
48-
struct string_list *trailers);
60+
struct list_head *new_trailer_head);
4961

5062
void trailer_info_get(struct trailer_info *info, const char *str);
5163

0 commit comments

Comments
 (0)