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