Raw
1 /*
2 * Builtin "git interpret-trailers"
3 *
4 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
5 *
6 */
7 #define USE_THE_REPOSITORY_VARIABLE
8 #include "builtin.h"
9 #include "environment.h"
10 #include "gettext.h"
11 #include "parse-options.h"
12 #include "string-list.h"
13 #include "tempfile.h"
14 #include "trailer.h"
15 #include "config.h"
16
17 static const char * const git_interpret_trailers_usage[] = {
18 N_("git interpret-trailers [--in-place] [--trim-empty]\n"
19 " [(--trailer (<key>|<key-alias>)[(=|:)<value>])...]\n"
20 " [--parse] [<file>...]"),
21 NULL
22 };
23
24 static enum trailer_where where;
25 static enum trailer_if_exists if_exists;
26 static enum trailer_if_missing if_missing;
27
28 static int option_parse_where(const struct option *opt,
29 const char *arg, int unset UNUSED)
30 {
31 /* unset implies NULL arg, which is handled in our helper */
32 return trailer_set_where(opt->value, arg);
33 }
34
35 static int option_parse_if_exists(const struct option *opt,
36 const char *arg, int unset UNUSED)
37 {
38 /* unset implies NULL arg, which is handled in our helper */
39 return trailer_set_if_exists(opt->value, arg);
40 }
41
42 static int option_parse_if_missing(const struct option *opt,
43 const char *arg, int unset UNUSED)
44 {
45 /* unset implies NULL arg, which is handled in our helper */
46 return trailer_set_if_missing(opt->value, arg);
47 }
48
49 static void new_trailers_clear(struct list_head *trailers)
50 {
51 struct list_head *pos, *tmp;
52 struct new_trailer_item *item;
53
54 list_for_each_safe(pos, tmp, trailers) {
55 item = list_entry(pos, struct new_trailer_item, list);
56 list_del(pos);
57 free(item);
58 }
59 }
60
61 static int option_parse_trailer(const struct option *opt,
62 const char *arg, int unset)
63 {
64 struct list_head *trailers = opt->value;
65 struct new_trailer_item *item;
66
67 if (unset) {
68 new_trailers_clear(trailers);
69 return 0;
70 }
71
72 if (!arg)
73 return -1;
74
75 item = xmalloc(sizeof(*item));
76 item->text = arg;
77 item->where = where;
78 item->if_exists = if_exists;
79 item->if_missing = if_missing;
80 list_add_tail(&item->list, trailers);
81 return 0;
82 }
83
84 static int parse_opt_parse(const struct option *opt, const char *arg,
85 int unset)
86 {
87 struct process_trailer_options *v = opt->value;
88 v->only_trailers = 1;
89 v->only_input = 1;
90 v->unfold = 1;
91 BUG_ON_OPT_NEG(unset);
92 BUG_ON_OPT_ARG(arg);
93 return 0;
94 }
95
96 static void read_input_file(struct strbuf *sb, const char *file)
97 {
98 if (file) {
99 if (strbuf_read_file(sb, file, 0) < 0)
100 die_errno(_("could not read input file '%s'"), file);
101 } else {
102 if (strbuf_read(sb, fileno(stdin), 0) < 0)
103 die_errno(_("could not read from stdin"));
104 }
105 strbuf_complete_line(sb);
106 }
107
108 static void interpret_trailers(const struct process_trailer_options *opts,
109 struct list_head *new_trailer_head,
110 const char *file)
111 {
112 struct strbuf input = STRBUF_INIT;
113 struct strbuf out = STRBUF_INIT;
114 struct tempfile *tempfile = NULL;
115 int fd = 1;
116
117 trailer_config_init();
118
119 read_input_file(&input, file);
120
121 if (opts->in_place) {
122 tempfile = trailer_create_in_place_tempfile(file);
123 if (!tempfile)
124 die(NULL);
125 fd = tempfile->fd;
126 }
127 process_trailers(opts, new_trailer_head, &input, &out);
128
129 if (write_in_full(fd, out.buf, out.len) < 0)
130 die_errno(_("could not write to temporary file '%s'"), file);
131 if (opts->in_place)
132 if (rename_tempfile(&tempfile, file))
133 die_errno(_("could not rename temporary file to %s"), file);
134
135 strbuf_release(&input);
136 strbuf_release(&out);
137 }
138
139 int cmd_interpret_trailers(int argc,
140 const char **argv,
141 const char *prefix,
142 struct repository *repo UNUSED)
143 {
144 struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
145 LIST_HEAD(trailers);
146
147 struct option options[] = {
148 OPT_BOOL(0, "in-place", &opts.in_place, N_("edit files in place")),
149 OPT_BOOL(0, "trim-empty", &opts.trim_empty, N_("trim empty trailers")),
150
151 OPT_CALLBACK(0, "where", &where, N_("placement"),
152 N_("where to place the new trailer"), option_parse_where),
153 OPT_CALLBACK(0, "if-exists", &if_exists, N_("action"),
154 N_("action if trailer already exists"), option_parse_if_exists),
155 OPT_CALLBACK(0, "if-missing", &if_missing, N_("action"),
156 N_("action if trailer is missing"), option_parse_if_missing),
157
158 OPT_BOOL(0, "only-trailers", &opts.only_trailers, N_("output only the trailers")),
159 OPT_BOOL(0, "only-input", &opts.only_input, N_("do not apply trailer.<key-alias> configuration variables")),
160 OPT_BOOL(0, "unfold", &opts.unfold, N_("reformat multiline trailer values as single-line values")),
161 OPT_CALLBACK_F(0, "parse", &opts, NULL, N_("alias for --only-trailers --only-input --unfold"),
162 PARSE_OPT_NOARG | PARSE_OPT_NONEG, parse_opt_parse),
163 OPT_BOOL(0, "no-divider", &opts.no_divider, N_("do not treat \"---\" as the end of input")),
164 OPT_CALLBACK(0, "trailer", &trailers, N_("trailer"),
165 N_("trailer(s) to add"), option_parse_trailer),
166 OPT_END()
167 };
168
169 repo_config(the_repository, git_default_config, NULL);
170
171 argc = parse_options(argc, argv, prefix, options,
172 git_interpret_trailers_usage, 0);
173
174 if (opts.only_input && !list_empty(&trailers))
175 usage_msg_opt(
176 _("--trailer with --only-input does not make sense"),
177 git_interpret_trailers_usage,
178 options);
179
180 if (argc) {
181 int i;
182 for (i = 0; i < argc; i++)
183 interpret_trailers(&opts, &trailers, argv[i]);
184 } else {
185 if (opts.in_place)
186 die(_("no input file given for in-place editing"));
187 interpret_trailers(&opts, &trailers, NULL);
188 }
189
190 new_trailers_clear(&trailers);
191
192 return 0;
193 }