builtin / interpret-trailers.con commit interpret-trailers: add --parse convenience option (99e09da)
   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 "parse-options.h"
  11#include "string-list.h"
  12#include "trailer.h"
  13
  14static const char * const git_interpret_trailers_usage[] = {
  15        N_("git interpret-trailers [--in-place] [--trim-empty] [(--trailer <token>[(=|:)<value>])...] [<file>...]"),
  16        NULL
  17};
  18
  19static int parse_opt_parse(const struct option *opt, const char *arg,
  20                           int unset)
  21{
  22        struct process_trailer_options *v = opt->value;
  23        v->only_trailers = 1;
  24        v->only_input = 1;
  25        v->unfold = 1;
  26        return 0;
  27}
  28
  29int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
  30{
  31        struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
  32        struct string_list trailers = STRING_LIST_INIT_NODUP;
  33
  34        struct option options[] = {
  35                OPT_BOOL(0, "in-place", &opts.in_place, N_("edit files in place")),
  36                OPT_BOOL(0, "trim-empty", &opts.trim_empty, N_("trim empty trailers")),
  37                OPT_BOOL(0, "only-trailers", &opts.only_trailers, N_("output only the trailers")),
  38                OPT_BOOL(0, "only-input", &opts.only_input, N_("do not apply config rules")),
  39                OPT_BOOL(0, "unfold", &opts.unfold, N_("join whitespace-continued values")),
  40                { OPTION_CALLBACK, 0, "parse", &opts, NULL, N_("set parsing options"),
  41                        PARSE_OPT_NOARG | PARSE_OPT_NONEG, parse_opt_parse },
  42                OPT_STRING_LIST(0, "trailer", &trailers, N_("trailer"),
  43                                N_("trailer(s) to add")),
  44                OPT_END()
  45        };
  46
  47        argc = parse_options(argc, argv, prefix, options,
  48                             git_interpret_trailers_usage, 0);
  49
  50        if (opts.only_input && trailers.nr)
  51                usage_msg_opt(
  52                        _("--trailer with --only-input does not make sense"),
  53                        git_interpret_trailers_usage,
  54                        options);
  55
  56        if (argc) {
  57                int i;
  58                for (i = 0; i < argc; i++)
  59                        process_trailers(argv[i], &opts, &trailers);
  60        } else {
  61                if (opts.in_place)
  62                        die(_("no input file given for in-place editing"));
  63                process_trailers(NULL, &opts, &trailers);
  64        }
  65
  66        string_list_clear(&trailers, 0);
  67
  68        return 0;
  69}