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