]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/interpret-trailers.c
Merge branch 'es/add-doc-list-short-form-of-all-in-synopsis'
[thirdparty/git.git] / builtin / interpret-trailers.c
1 /*
2 * Builtin "git interpret-trailers"
3 *
4 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
5 *
6 */
7
8 #include "builtin.h"
9 #include "gettext.h"
10 #include "parse-options.h"
11 #include "string-list.h"
12 #include "trailer.h"
13 #include "config.h"
14
15 static const char * const git_interpret_trailers_usage[] = {
16 N_("git interpret-trailers [--in-place] [--trim-empty]\n"
17 " [(--trailer (<key>|<keyAlias>)[(=|:)<value>])...]\n"
18 " [--parse] [<file>...]"),
19 NULL
20 };
21
22 static enum trailer_where where;
23 static enum trailer_if_exists if_exists;
24 static enum trailer_if_missing if_missing;
25
26 static int option_parse_where(const struct option *opt,
27 const char *arg, int unset UNUSED)
28 {
29 /* unset implies NULL arg, which is handled in our helper */
30 return trailer_set_where(opt->value, arg);
31 }
32
33 static int option_parse_if_exists(const struct option *opt,
34 const char *arg, int unset UNUSED)
35 {
36 /* unset implies NULL arg, which is handled in our helper */
37 return trailer_set_if_exists(opt->value, arg);
38 }
39
40 static int option_parse_if_missing(const struct option *opt,
41 const char *arg, int unset UNUSED)
42 {
43 /* unset implies NULL arg, which is handled in our helper */
44 return trailer_set_if_missing(opt->value, arg);
45 }
46
47 static void new_trailers_clear(struct list_head *trailers)
48 {
49 struct list_head *pos, *tmp;
50 struct new_trailer_item *item;
51
52 list_for_each_safe(pos, tmp, trailers) {
53 item = list_entry(pos, struct new_trailer_item, list);
54 list_del(pos);
55 free(item);
56 }
57 }
58
59 static int option_parse_trailer(const struct option *opt,
60 const char *arg, int unset)
61 {
62 struct list_head *trailers = opt->value;
63 struct new_trailer_item *item;
64
65 if (unset) {
66 new_trailers_clear(trailers);
67 return 0;
68 }
69
70 if (!arg)
71 return -1;
72
73 item = xmalloc(sizeof(*item));
74 item->text = arg;
75 item->where = where;
76 item->if_exists = if_exists;
77 item->if_missing = if_missing;
78 list_add_tail(&item->list, trailers);
79 return 0;
80 }
81
82 static int parse_opt_parse(const struct option *opt, const char *arg,
83 int unset)
84 {
85 struct process_trailer_options *v = opt->value;
86 v->only_trailers = 1;
87 v->only_input = 1;
88 v->unfold = 1;
89 BUG_ON_OPT_NEG(unset);
90 BUG_ON_OPT_ARG(arg);
91 return 0;
92 }
93
94 int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
95 {
96 struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
97 LIST_HEAD(trailers);
98
99 struct option options[] = {
100 OPT_BOOL(0, "in-place", &opts.in_place, N_("edit files in place")),
101 OPT_BOOL(0, "trim-empty", &opts.trim_empty, N_("trim empty trailers")),
102
103 OPT_CALLBACK(0, "where", &where, N_("placement"),
104 N_("where to place the new trailer"), option_parse_where),
105 OPT_CALLBACK(0, "if-exists", &if_exists, N_("action"),
106 N_("action if trailer already exists"), option_parse_if_exists),
107 OPT_CALLBACK(0, "if-missing", &if_missing, N_("action"),
108 N_("action if trailer is missing"), option_parse_if_missing),
109
110 OPT_BOOL(0, "only-trailers", &opts.only_trailers, N_("output only the trailers")),
111 OPT_BOOL(0, "only-input", &opts.only_input, N_("do not apply trailer.* configuration variables")),
112 OPT_BOOL(0, "unfold", &opts.unfold, N_("reformat multiline trailer values as single-line values")),
113 OPT_CALLBACK_F(0, "parse", &opts, NULL, N_("alias for --only-trailers --only-input --unfold"),
114 PARSE_OPT_NOARG | PARSE_OPT_NONEG, parse_opt_parse),
115 OPT_BOOL(0, "no-divider", &opts.no_divider, N_("do not treat \"---\" as the end of input")),
116 OPT_CALLBACK(0, "trailer", &trailers, N_("trailer"),
117 N_("trailer(s) to add"), option_parse_trailer),
118 OPT_END()
119 };
120
121 git_config(git_default_config, NULL);
122
123 argc = parse_options(argc, argv, prefix, options,
124 git_interpret_trailers_usage, 0);
125
126 if (opts.only_input && !list_empty(&trailers))
127 usage_msg_opt(
128 _("--trailer with --only-input does not make sense"),
129 git_interpret_trailers_usage,
130 options);
131
132 if (argc) {
133 int i;
134 for (i = 0; i < argc; i++)
135 process_trailers(argv[i], &opts, &trailers);
136 } else {
137 if (opts.in_place)
138 die(_("no input file given for in-place editing"));
139 process_trailers(NULL, &opts, &trailers);
140 }
141
142 new_trailers_clear(&trailers);
143
144 return 0;
145 }