builtin / interpret-trailers.con commit trailers: introduce struct new_trailer_item (51166b8)
   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 void new_trailers_clear(struct list_head *trailers)
  20{
  21        struct list_head *pos, *tmp;
  22        struct new_trailer_item *item;
  23
  24        list_for_each_safe(pos, tmp, trailers) {
  25                item = list_entry(pos, struct new_trailer_item, list);
  26                list_del(pos);
  27                free(item);
  28        }
  29}
  30
  31static int option_parse_trailer(const struct option *opt,
  32                                   const char *arg, int unset)
  33{
  34        struct list_head *trailers = opt->value;
  35        struct new_trailer_item *item;
  36
  37        if (unset) {
  38                new_trailers_clear(trailers);
  39                return 0;
  40        }
  41
  42        if (!arg)
  43                return -1;
  44
  45        item = xmalloc(sizeof(*item));
  46        item->text = arg;
  47        list_add_tail(&item->list, trailers);
  48        return 0;
  49}
  50
  51int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
  52{
  53        int in_place = 0;
  54        int trim_empty = 0;
  55        LIST_HEAD(trailers);
  56
  57        struct option options[] = {
  58                OPT_BOOL(0, "in-place", &in_place, N_("edit files in place")),
  59                OPT_BOOL(0, "trim-empty", &trim_empty, N_("trim empty trailers")),
  60
  61                OPT_CALLBACK(0, "trailer", &trailers, N_("trailer"),
  62                                N_("trailer(s) to add"), option_parse_trailer),
  63                OPT_END()
  64        };
  65
  66        argc = parse_options(argc, argv, prefix, options,
  67                             git_interpret_trailers_usage, 0);
  68
  69        if (argc) {
  70                int i;
  71                for (i = 0; i < argc; i++)
  72                        process_trailers(argv[i], in_place, trim_empty, &trailers);
  73        } else {
  74                if (in_place)
  75                        die(_("no input file given for in-place editing"));
  76                process_trailers(NULL, in_place, trim_empty, &trailers);
  77        }
  78
  79        new_trailers_clear(&trailers);
  80
  81        return 0;
  82}