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 <pbonzini@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Paolo Bonzini committed
Aug 1, 2017 at 11:03 UTC
51166b8754e0df4ed3ee559ddcc4641035ec98ec
3 files changed
+61
-13
builtin/interpret-trailers.c
+37
-4
@@ -16,17 +16,50 @@ static const char * const git_interpret_trailers_usage[] = {
16
NULL
17
};
18
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
+
51
int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
52
{
53
int in_place = 0;
54
int trim_empty = 0;
23
- struct string_list trailers = STRING_LIST_INIT_NODUP;
55
+ LIST_HEAD(trailers);
56
57
struct option options[] = {
58
OPT_BOOL(0, "in-place", &in_place, N_("edit files in place")),
59
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),
63
OPT_END()
64
};
65
@@ -43,7 +76,7 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
76
process_trailers(NULL, in_place, trim_empty, &trailers);
77
}
78
46
- string_list_clear(&trailers, 0);
79
+ new_trailers_clear(&trailers);
80
81
return 0;
82
}
trailer.c
+11
-8
@@ -669,9 +669,8 @@ static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
669
}
670
671
static void process_command_line_args(struct list_head *arg_head,
672
- struct string_list *trailers)
672
+ struct list_head *new_trailer_head)
673
{
674
- struct string_list_item *tr;
674
struct arg_item *item;
675
struct strbuf tok = STRBUF_INIT;
676
struct strbuf val = STRBUF_INIT;
@@ -695,17 +694,20 @@ static void process_command_line_args(struct list_head *arg_head,
694
}
695
696
/* 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
+
702
if (separator_pos == 0) {
703
struct strbuf sb = STRBUF_INIT;
702
- strbuf_addstr(&sb, tr->string);
704
+ strbuf_addstr(&sb, tr->text);
705
strbuf_trim(&sb);
706
error(_("empty trailer token in trailer '%.*s'"),
707
(int) sb.len, sb.buf);
708
strbuf_release(&sb);
709
} else {
708
- parse_trailer(&tok, &val, &conf, tr->string,
710
+ parse_trailer(&tok, &val, &conf, tr->text,
711
separator_pos);
712
add_arg_item(arg_head,
713
strbuf_detach(&tok, NULL),
@@ -969,7 +971,8 @@ static FILE *create_in_place_tempfile(const char *file)
971
return outfile;
972
}
973
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)
976
{
977
LIST_HEAD(head);
978
LIST_HEAD(arg_head);
@@ -987,7 +990,7 @@ void process_trailers(const char *file, int in_place, int trim_empty, struct str
990
/* Print the lines before the trailers */
991
trailer_end = process_input_file(outfile, sb.buf, &head);
992
990
- process_command_line_args(&arg_head, trailers);
993
+ process_command_line_args(&arg_head, new_trailer_head);
994
995
process_trailers_lists(&head, &arg_head);
996
trailer.h
+13
-1
@@ -1,6 +1,8 @@
1
#ifndef TRAILER_H
2
#define TRAILER_H
3
4
+#include "list.h"
5
+
6
enum trailer_where {
7
WHERE_END,
8
WHERE_AFTER,
@@ -44,8 +46,18 @@ struct trailer_info {
46
size_t trailer_nr;
47
};
48
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
+
59
void process_trailers(const char *file, int in_place, int trim_empty,
48
- struct string_list *trailers);
60
+ struct list_head *new_trailer_head);
61
62
void trailer_info_get(struct trailer_info *info, const char *str);
63