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"
15 static const char * const git_interpret_trailers_usage
[] = {
16 N_("git interpret-trailers [--in-place] [--trim-empty]\n"
17 " [(--trailer <token>[(=|:)<value>])...]\n"
18 " [--parse] [<file>...]"),
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
)
29 return trailer_set_where(&where
, arg
);
32 static int option_parse_if_exists(const struct option
*opt
,
33 const char *arg
, int unset
)
35 return trailer_set_if_exists(&if_exists
, arg
);
38 static int option_parse_if_missing(const struct option
*opt
,
39 const char *arg
, int unset
)
41 return trailer_set_if_missing(&if_missing
, arg
);
44 static void new_trailers_clear(struct list_head
*trailers
)
46 struct list_head
*pos
, *tmp
;
47 struct new_trailer_item
*item
;
49 list_for_each_safe(pos
, tmp
, trailers
) {
50 item
= list_entry(pos
, struct new_trailer_item
, list
);
56 static int option_parse_trailer(const struct option
*opt
,
57 const char *arg
, int unset
)
59 struct list_head
*trailers
= opt
->value
;
60 struct new_trailer_item
*item
;
63 new_trailers_clear(trailers
);
70 item
= xmalloc(sizeof(*item
));
73 item
->if_exists
= if_exists
;
74 item
->if_missing
= if_missing
;
75 list_add_tail(&item
->list
, trailers
);
79 static int parse_opt_parse(const struct option
*opt
, const char *arg
,
82 struct process_trailer_options
*v
= opt
->value
;
86 BUG_ON_OPT_NEG(unset
);
91 int cmd_interpret_trailers(int argc
, const char **argv
, const char *prefix
)
93 struct process_trailer_options opts
= PROCESS_TRAILER_OPTIONS_INIT
;
96 struct option options
[] = {
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")),
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
),
107 OPT_BOOL(0, "only-trailers", &opts
.only_trailers
, N_("output only the trailers")),
108 OPT_BOOL(0, "only-input", &opts
.only_input
, N_("do not apply config rules")),
109 OPT_BOOL(0, "unfold", &opts
.unfold
, N_("join whitespace-continued values")),
110 OPT_CALLBACK_F(0, "parse", &opts
, NULL
, N_("set parsing options"),
111 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, parse_opt_parse
),
112 OPT_BOOL(0, "no-divider", &opts
.no_divider
, N_("do not treat --- specially")),
113 OPT_CALLBACK(0, "trailer", &trailers
, N_("trailer"),
114 N_("trailer(s) to add"), option_parse_trailer
),
118 git_config(git_default_config
, NULL
);
120 argc
= parse_options(argc
, argv
, prefix
, options
,
121 git_interpret_trailers_usage
, 0);
123 if (opts
.only_input
&& !list_empty(&trailers
))
125 _("--trailer with --only-input does not make sense"),
126 git_interpret_trailers_usage
,
131 for (i
= 0; i
< argc
; i
++)
132 process_trailers(argv
[i
], &opts
, &trailers
);
135 die(_("no input file given for in-place editing"));
136 process_trailers(NULL
, &opts
, &trailers
);
139 new_trailers_clear(&trailers
);