]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/interpret-trailers.c
trailers: introduce struct new_trailer_item
[thirdparty/git.git] / builtin / interpret-trailers.c
CommitLineData
6634f054
CC
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[] = {
e1f89863 15 N_("git interpret-trailers [--in-place] [--trim-empty] [(--trailer <token>[(=|:)<value>])...] [<file>...]"),
6634f054
CC
16 NULL
17};
18
51166b87
PB
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
6634f054
CC
51int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
52{
e1f89863 53 int in_place = 0;
6634f054 54 int trim_empty = 0;
51166b87 55 LIST_HEAD(trailers);
6634f054
CC
56
57 struct option options[] = {
e1f89863 58 OPT_BOOL(0, "in-place", &in_place, N_("edit files in place")),
6634f054 59 OPT_BOOL(0, "trim-empty", &trim_empty, N_("trim empty trailers")),
51166b87
PB
60
61 OPT_CALLBACK(0, "trailer", &trailers, N_("trailer"),
62 N_("trailer(s) to add"), option_parse_trailer),
6634f054
CC
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++)
e1f89863
TK
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 }
6634f054 78
51166b87 79 new_trailers_clear(&trailers);
6634f054
CC
80
81 return 0;
82}