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