2 * Builtin "git interpret-trailers"
4 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
10 #include "parse-options.h"
11 #include "string-list.h"
14 static const char * const git_interpret_trailers_usage[] = {
15 N_("git interpret-trailers [--in-place] [--trim-empty] [(--trailer <token>[(=|:)<value>])...] [<file>...]"),
19 static enum trailer_where where;
20 static enum trailer_if_exists if_exists;
21 static enum trailer_if_missing if_missing;
23 static int option_parse_where(const struct option *opt,
24 const char *arg, int unset)
26 return trailer_set_where(&where, arg);
29 static int option_parse_if_exists(const struct option *opt,
30 const char *arg, int unset)
32 return trailer_set_if_exists(&if_exists, arg);
35 static int option_parse_if_missing(const struct option *opt,
36 const char *arg, int unset)
38 return trailer_set_if_missing(&if_missing, arg);
41 static void new_trailers_clear(struct list_head *trailers)
43 struct list_head *pos, *tmp;
44 struct new_trailer_item *item;
46 list_for_each_safe(pos, tmp, trailers) {
47 item = list_entry(pos, struct new_trailer_item, list);
53 static int option_parse_trailer(const struct option *opt,
54 const char *arg, int unset)
56 struct list_head *trailers = opt->value;
57 struct new_trailer_item *item;
60 new_trailers_clear(trailers);
67 item = xmalloc(sizeof(*item));
70 item->if_exists = if_exists;
71 item->if_missing = if_missing;
72 list_add_tail(&item->list, trailers);
76 static int parse_opt_parse(const struct option *opt, const char *arg,
79 struct process_trailer_options *v = opt->value;
86 int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
88 struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
91 struct option options[] = {
92 OPT_BOOL(0, "in-place", &opts.in_place, N_("edit files in place")),
93 OPT_BOOL(0, "trim-empty", &opts.trim_empty, N_("trim empty trailers")),
95 OPT_CALLBACK(0, "where", NULL, N_("action"),
96 N_("where to place the new trailer"), option_parse_where),
97 OPT_CALLBACK(0, "if-exists", NULL, N_("action"),
98 N_("action if trailer already exists"), option_parse_if_exists),
99 OPT_CALLBACK(0, "if-missing", NULL, N_("action"),
100 N_("action if trailer is missing"), option_parse_if_missing),
102 OPT_BOOL(0, "only-trailers", &opts.only_trailers, N_("output only the trailers")),
103 OPT_BOOL(0, "only-input", &opts.only_input, N_("do not apply config rules")),
104 OPT_BOOL(0, "unfold", &opts.unfold, N_("join whitespace-continued values")),
105 { OPTION_CALLBACK, 0, "parse", &opts, NULL, N_("set parsing options"),
106 PARSE_OPT_NOARG | PARSE_OPT_NONEG, parse_opt_parse },
107 OPT_CALLBACK(0, "trailer", &trailers, N_("trailer"),
108 N_("trailer(s) to add"), option_parse_trailer),
112 argc = parse_options(argc, argv, prefix, options,
113 git_interpret_trailers_usage, 0);
115 if (opts.only_input && !list_empty(&trailers))
117 _("--trailer with --only-input does not make sense"),
118 git_interpret_trailers_usage,
123 for (i = 0; i < argc; i++)
124 process_trailers(argv[i], &opts, &trailers);
127 die(_("no input file given for in-place editing"));
128 process_trailers(NULL, &opts, &trailers);
131 new_trailers_clear(&trailers);