2014-10-13 20:16:29 +02:00
|
|
|
/*
|
|
|
|
* Builtin "git interpret-trailers"
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cache.h"
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "parse-options.h"
|
|
|
|
#include "string-list.h"
|
|
|
|
#include "trailer.h"
|
2019-06-19 05:37:28 +02:00
|
|
|
#include "config.h"
|
2014-10-13 20:16:29 +02:00
|
|
|
|
|
|
|
static const char * const git_interpret_trailers_usage[] = {
|
2022-10-13 17:39:02 +02:00
|
|
|
N_("git interpret-trailers [--in-place] [--trim-empty]\n"
|
|
|
|
" [(--trailer <token>[(=|:)<value>])...]\n"
|
|
|
|
" [<file>...]"),
|
2014-10-13 20:16:29 +02:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2017-08-01 11:03:32 +02:00
|
|
|
static enum trailer_where where;
|
|
|
|
static enum trailer_if_exists if_exists;
|
|
|
|
static enum trailer_if_missing if_missing;
|
|
|
|
|
|
|
|
static int option_parse_where(const struct option *opt,
|
|
|
|
const char *arg, int unset)
|
|
|
|
{
|
|
|
|
return trailer_set_where(&where, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int option_parse_if_exists(const struct option *opt,
|
|
|
|
const char *arg, int unset)
|
|
|
|
{
|
|
|
|
return trailer_set_if_exists(&if_exists, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int option_parse_if_missing(const struct option *opt,
|
|
|
|
const char *arg, int unset)
|
|
|
|
{
|
|
|
|
return trailer_set_if_missing(&if_missing, arg);
|
|
|
|
}
|
|
|
|
|
2017-08-01 11:03:31 +02:00
|
|
|
static void new_trailers_clear(struct list_head *trailers)
|
|
|
|
{
|
|
|
|
struct list_head *pos, *tmp;
|
|
|
|
struct new_trailer_item *item;
|
|
|
|
|
|
|
|
list_for_each_safe(pos, tmp, trailers) {
|
|
|
|
item = list_entry(pos, struct new_trailer_item, list);
|
|
|
|
list_del(pos);
|
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int option_parse_trailer(const struct option *opt,
|
|
|
|
const char *arg, int unset)
|
|
|
|
{
|
|
|
|
struct list_head *trailers = opt->value;
|
|
|
|
struct new_trailer_item *item;
|
|
|
|
|
|
|
|
if (unset) {
|
|
|
|
new_trailers_clear(trailers);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!arg)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
item = xmalloc(sizeof(*item));
|
|
|
|
item->text = arg;
|
2017-08-01 11:03:32 +02:00
|
|
|
item->where = where;
|
|
|
|
item->if_exists = if_exists;
|
|
|
|
item->if_missing = if_missing;
|
2017-08-01 11:03:31 +02:00
|
|
|
list_add_tail(&item->list, trailers);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-15 12:23:34 +02:00
|
|
|
static int parse_opt_parse(const struct option *opt, const char *arg,
|
|
|
|
int unset)
|
|
|
|
{
|
|
|
|
struct process_trailer_options *v = opt->value;
|
|
|
|
v->only_trailers = 1;
|
|
|
|
v->only_input = 1;
|
|
|
|
v->unfold = 1;
|
assert NOARG/NONEG behavior of parse-options callbacks
When we define a parse-options callback, the flags we put in the option
struct must match what the callback expects. For example, a callback
which does not handle the "unset" parameter should only be used with
PARSE_OPT_NONEG. But since the callback and the option struct are not
defined next to each other, it's easy to get this wrong (as earlier
patches in this series show).
Fortunately, the compiler can help us here: compiling with
-Wunused-parameters can show us which callbacks ignore their "unset"
parameters (and likewise, ones that ignore "arg" expect to be triggered
with PARSE_OPT_NOARG).
But after we've inspected a callback and determined that all of its
callers use the right flags, what do we do next? We'd like to silence
the compiler warning, but do so in a way that will catch any wrong calls
in the future.
We can do that by actually checking those variables and asserting that
they match our expectations. Because this is such a common pattern,
we'll introduce some helper macros. The resulting messages aren't
as descriptive as we could make them, but the file/line information from
BUG() is enough to identify the problem (and anyway, the point is that
these should never be seen).
Each of the annotated callbacks in this patch triggers
-Wunused-parameters, and was manually inspected to make sure all callers
use the correct options (so none of these BUGs should be triggerable).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-11-05 07:45:42 +01:00
|
|
|
BUG_ON_OPT_NEG(unset);
|
|
|
|
BUG_ON_OPT_ARG(arg);
|
2017-08-15 12:23:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-13 20:16:29 +02:00
|
|
|
int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
2017-08-10 20:03:58 +02:00
|
|
|
struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
|
2017-08-01 11:03:31 +02:00
|
|
|
LIST_HEAD(trailers);
|
2014-10-13 20:16:29 +02:00
|
|
|
|
|
|
|
struct option options[] = {
|
2017-08-10 20:03:58 +02:00
|
|
|
OPT_BOOL(0, "in-place", &opts.in_place, N_("edit files in place")),
|
|
|
|
OPT_BOOL(0, "trim-empty", &opts.trim_empty, N_("trim empty trailers")),
|
2017-08-01 11:03:31 +02:00
|
|
|
|
2017-08-01 11:03:32 +02:00
|
|
|
OPT_CALLBACK(0, "where", NULL, N_("action"),
|
|
|
|
N_("where to place the new trailer"), option_parse_where),
|
|
|
|
OPT_CALLBACK(0, "if-exists", NULL, N_("action"),
|
|
|
|
N_("action if trailer already exists"), option_parse_if_exists),
|
|
|
|
OPT_CALLBACK(0, "if-missing", NULL, N_("action"),
|
|
|
|
N_("action if trailer is missing"), option_parse_if_missing),
|
|
|
|
|
2017-08-15 12:23:21 +02:00
|
|
|
OPT_BOOL(0, "only-trailers", &opts.only_trailers, N_("output only the trailers")),
|
2017-08-15 12:23:25 +02:00
|
|
|
OPT_BOOL(0, "only-input", &opts.only_input, N_("do not apply config rules")),
|
2017-08-15 12:23:29 +02:00
|
|
|
OPT_BOOL(0, "unfold", &opts.unfold, N_("join whitespace-continued values")),
|
Use OPT_CALLBACK and OPT_CALLBACK_F
In the codebase, there are many options which use OPTION_CALLBACK in a
plain ol' struct definition. However, we have the OPT_CALLBACK and
OPT_CALLBACK_F macros which are meant to abstract these plain struct
definitions away. These macros are useful as they semantically signal to
developers that these are just normal callback option with nothing fancy
happening.
Replace plain struct definitions of OPTION_CALLBACK with OPT_CALLBACK or
OPT_CALLBACK_F where applicable. The heavy lifting was done using the
following (disgusting) shell script:
#!/bin/sh
do_replacement () {
tr '\n' '\r' |
sed -e 's/{\s*OPTION_CALLBACK,\s*\([^,]*\),\([^,]*\),\([^,]*\),\([^,]*\),\([^,]*\),\s*0,\(\s*[^[:space:]}]*\)\s*}/OPT_CALLBACK(\1,\2,\3,\4,\5,\6)/g' |
sed -e 's/{\s*OPTION_CALLBACK,\s*\([^,]*\),\([^,]*\),\([^,]*\),\([^,]*\),\([^,]*\),\([^,]*\),\(\s*[^[:space:]}]*\)\s*}/OPT_CALLBACK_F(\1,\2,\3,\4,\5,\6,\7)/g' |
tr '\r' '\n'
}
for f in $(git ls-files \*.c)
do
do_replacement <"$f" >"$f.tmp"
mv "$f.tmp" "$f"
done
The result was manually inspected and then reformatted to match the
style of the surrounding code. Finally, using
`git grep OPTION_CALLBACK \*.c`, leftover results which were not handled
by the script were manually transformed.
Signed-off-by: Denton Liu <liu.denton@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-04-28 10:36:28 +02:00
|
|
|
OPT_CALLBACK_F(0, "parse", &opts, NULL, N_("set parsing options"),
|
|
|
|
PARSE_OPT_NOARG | PARSE_OPT_NONEG, parse_opt_parse),
|
2018-08-23 02:49:56 +02:00
|
|
|
OPT_BOOL(0, "no-divider", &opts.no_divider, N_("do not treat --- specially")),
|
2017-08-01 11:03:31 +02:00
|
|
|
OPT_CALLBACK(0, "trailer", &trailers, N_("trailer"),
|
|
|
|
N_("trailer(s) to add"), option_parse_trailer),
|
2014-10-13 20:16:29 +02:00
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
2019-06-19 05:37:28 +02:00
|
|
|
git_config(git_default_config, NULL);
|
|
|
|
|
2014-10-13 20:16:29 +02:00
|
|
|
argc = parse_options(argc, argv, prefix, options,
|
|
|
|
git_interpret_trailers_usage, 0);
|
|
|
|
|
2017-08-27 07:55:04 +02:00
|
|
|
if (opts.only_input && !list_empty(&trailers))
|
2017-08-15 12:23:25 +02:00
|
|
|
usage_msg_opt(
|
|
|
|
_("--trailer with --only-input does not make sense"),
|
|
|
|
git_interpret_trailers_usage,
|
|
|
|
options);
|
|
|
|
|
2014-10-13 20:16:29 +02:00
|
|
|
if (argc) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < argc; i++)
|
2017-08-10 20:03:58 +02:00
|
|
|
process_trailers(argv[i], &opts, &trailers);
|
2016-01-14 17:57:55 +01:00
|
|
|
} else {
|
2017-08-10 20:03:58 +02:00
|
|
|
if (opts.in_place)
|
2016-01-14 17:57:55 +01:00
|
|
|
die(_("no input file given for in-place editing"));
|
2017-08-10 20:03:58 +02:00
|
|
|
process_trailers(NULL, &opts, &trailers);
|
2016-01-14 17:57:55 +01:00
|
|
|
}
|
2014-10-13 20:16:29 +02:00
|
|
|
|
2017-08-01 11:03:31 +02:00
|
|
|
new_trailers_clear(&trailers);
|
2014-10-13 20:16:29 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|