reftable/merged: handle subiter cleanup on close only
[alt-git.git] / builtin / interpret-trailers.c
blob033bd1556cf9f8d6e56c2730a3e868500388144e
1 /*
2 * Builtin "git interpret-trailers"
4 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
6 */
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"
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
22 static enum trailer_where where;
23 static enum trailer_if_exists if_exists;
24 static enum trailer_if_missing if_missing;
26 static int option_parse_where(const struct option *opt,
27 const char *arg, int unset UNUSED)
29 /* unset implies NULL arg, which is handled in our helper */
30 return trailer_set_where(opt->value, arg);
33 static int option_parse_if_exists(const struct option *opt,
34 const char *arg, int unset UNUSED)
36 /* unset implies NULL arg, which is handled in our helper */
37 return trailer_set_if_exists(opt->value, arg);
40 static int option_parse_if_missing(const struct option *opt,
41 const char *arg, int unset UNUSED)
43 /* unset implies NULL arg, which is handled in our helper */
44 return trailer_set_if_missing(opt->value, arg);
47 static void new_trailers_clear(struct list_head *trailers)
49 struct list_head *pos, *tmp;
50 struct new_trailer_item *item;
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);
59 static int option_parse_trailer(const struct option *opt,
60 const char *arg, int unset)
62 struct list_head *trailers = opt->value;
63 struct new_trailer_item *item;
65 if (unset) {
66 new_trailers_clear(trailers);
67 return 0;
70 if (!arg)
71 return -1;
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;
82 static int parse_opt_parse(const struct option *opt, const char *arg,
83 int unset)
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;
94 int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
96 struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
97 LIST_HEAD(trailers);
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")),
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),
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()
121 git_config(git_default_config, NULL);
123 argc = parse_options(argc, argv, prefix, options,
124 git_interpret_trailers_usage, 0);
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);
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);
142 new_trailers_clear(&trailers);
144 return 0;