]>
Commit | Line | Data |
---|---|---|
6634f054 CC |
1 | /* |
2 | * Builtin "git interpret-trailers" | |
3 | * | |
4 | * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org> | |
5 | * | |
6 | */ | |
7 | ||
8 | #include "cache.h" | |
9 | #include "builtin.h" | |
10 | #include "parse-options.h" | |
11 | #include "string-list.h" | |
12 | #include "trailer.h" | |
29c83fc2 | 13 | #include "config.h" |
6634f054 CC |
14 | |
15 | static const char * const git_interpret_trailers_usage[] = { | |
e1f89863 | 16 | N_("git interpret-trailers [--in-place] [--trim-empty] [(--trailer <token>[(=|:)<value>])...] [<file>...]"), |
6634f054 CC |
17 | NULL |
18 | }; | |
19 | ||
0ea5292e PB |
20 | static enum trailer_where where; |
21 | static enum trailer_if_exists if_exists; | |
22 | static enum trailer_if_missing if_missing; | |
23 | ||
24 | static int option_parse_where(const struct option *opt, | |
25 | const char *arg, int unset) | |
26 | { | |
27 | return trailer_set_where(&where, arg); | |
28 | } | |
29 | ||
30 | static int option_parse_if_exists(const struct option *opt, | |
31 | const char *arg, int unset) | |
32 | { | |
33 | return trailer_set_if_exists(&if_exists, arg); | |
34 | } | |
35 | ||
36 | static int option_parse_if_missing(const struct option *opt, | |
37 | const char *arg, int unset) | |
38 | { | |
39 | return trailer_set_if_missing(&if_missing, arg); | |
40 | } | |
41 | ||
51166b87 PB |
42 | static void new_trailers_clear(struct list_head *trailers) |
43 | { | |
44 | struct list_head *pos, *tmp; | |
45 | struct new_trailer_item *item; | |
46 | ||
47 | list_for_each_safe(pos, tmp, trailers) { | |
48 | item = list_entry(pos, struct new_trailer_item, list); | |
49 | list_del(pos); | |
50 | free(item); | |
51 | } | |
52 | } | |
53 | ||
54 | static int option_parse_trailer(const struct option *opt, | |
55 | const char *arg, int unset) | |
56 | { | |
57 | struct list_head *trailers = opt->value; | |
58 | struct new_trailer_item *item; | |
59 | ||
60 | if (unset) { | |
61 | new_trailers_clear(trailers); | |
62 | return 0; | |
63 | } | |
64 | ||
65 | if (!arg) | |
66 | return -1; | |
67 | ||
68 | item = xmalloc(sizeof(*item)); | |
69 | item->text = arg; | |
0ea5292e PB |
70 | item->where = where; |
71 | item->if_exists = if_exists; | |
72 | item->if_missing = if_missing; | |
51166b87 PB |
73 | list_add_tail(&item->list, trailers); |
74 | return 0; | |
75 | } | |
76 | ||
99e09daf JK |
77 | static int parse_opt_parse(const struct option *opt, const char *arg, |
78 | int unset) | |
79 | { | |
80 | struct process_trailer_options *v = opt->value; | |
81 | v->only_trailers = 1; | |
82 | v->only_input = 1; | |
83 | v->unfold = 1; | |
517fe807 JK |
84 | BUG_ON_OPT_NEG(unset); |
85 | BUG_ON_OPT_ARG(arg); | |
99e09daf JK |
86 | return 0; |
87 | } | |
88 | ||
6634f054 CC |
89 | int cmd_interpret_trailers(int argc, const char **argv, const char *prefix) |
90 | { | |
8abc8980 | 91 | struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; |
51166b87 | 92 | LIST_HEAD(trailers); |
6634f054 CC |
93 | |
94 | struct option options[] = { | |
8abc8980 JK |
95 | OPT_BOOL(0, "in-place", &opts.in_place, N_("edit files in place")), |
96 | OPT_BOOL(0, "trim-empty", &opts.trim_empty, N_("trim empty trailers")), | |
51166b87 | 97 | |
0ea5292e PB |
98 | OPT_CALLBACK(0, "where", NULL, N_("action"), |
99 | N_("where to place the new trailer"), option_parse_where), | |
100 | OPT_CALLBACK(0, "if-exists", NULL, N_("action"), | |
101 | N_("action if trailer already exists"), option_parse_if_exists), | |
102 | OPT_CALLBACK(0, "if-missing", NULL, N_("action"), | |
103 | N_("action if trailer is missing"), option_parse_if_missing), | |
104 | ||
56c493ed | 105 | OPT_BOOL(0, "only-trailers", &opts.only_trailers, N_("output only the trailers")), |
fdbdb64f | 106 | OPT_BOOL(0, "only-input", &opts.only_input, N_("do not apply config rules")), |
00002396 | 107 | OPT_BOOL(0, "unfold", &opts.unfold, N_("join whitespace-continued values")), |
203c8533 DL |
108 | OPT_CALLBACK_F(0, "parse", &opts, NULL, N_("set parsing options"), |
109 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, parse_opt_parse), | |
1688c9a4 | 110 | OPT_BOOL(0, "no-divider", &opts.no_divider, N_("do not treat --- specially")), |
51166b87 PB |
111 | OPT_CALLBACK(0, "trailer", &trailers, N_("trailer"), |
112 | N_("trailer(s) to add"), option_parse_trailer), | |
6634f054 CC |
113 | OPT_END() |
114 | }; | |
115 | ||
29c83fc2 JK |
116 | git_config(git_default_config, NULL); |
117 | ||
6634f054 CC |
118 | argc = parse_options(argc, argv, prefix, options, |
119 | git_interpret_trailers_usage, 0); | |
120 | ||
06cf4f2d | 121 | if (opts.only_input && !list_empty(&trailers)) |
fdbdb64f JK |
122 | usage_msg_opt( |
123 | _("--trailer with --only-input does not make sense"), | |
124 | git_interpret_trailers_usage, | |
125 | options); | |
126 | ||
6634f054 CC |
127 | if (argc) { |
128 | int i; | |
129 | for (i = 0; i < argc; i++) | |
8abc8980 | 130 | process_trailers(argv[i], &opts, &trailers); |
e1f89863 | 131 | } else { |
8abc8980 | 132 | if (opts.in_place) |
e1f89863 | 133 | die(_("no input file given for in-place editing")); |
8abc8980 | 134 | process_trailers(NULL, &opts, &trailers); |
e1f89863 | 135 | } |
6634f054 | 136 | |
51166b87 | 137 | new_trailers_clear(&trailers); |
6634f054 CC |
138 | |
139 | return 0; | |
140 | } |