]>
Commit | Line | Data |
---|---|---|
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 | ||
14 | static 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 | ||
0ea5292e PB |
19 | static enum trailer_where where; |
20 | static enum trailer_if_exists if_exists; | |
21 | static enum trailer_if_missing if_missing; | |
22 | ||
23 | static int option_parse_where(const struct option *opt, | |
24 | const char *arg, int unset) | |
25 | { | |
26 | return trailer_set_where(&where, arg); | |
27 | } | |
28 | ||
29 | static 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 | ||
35 | static 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 | ||
51166b87 PB |
41 | static 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 | ||
53 | static 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; | |
0ea5292e PB |
69 | item->where = where; |
70 | item->if_exists = if_exists; | |
71 | item->if_missing = if_missing; | |
51166b87 PB |
72 | list_add_tail(&item->list, trailers); |
73 | return 0; | |
74 | } | |
75 | ||
6634f054 CC |
76 | int cmd_interpret_trailers(int argc, const char **argv, const char *prefix) |
77 | { | |
e1f89863 | 78 | int in_place = 0; |
6634f054 | 79 | int trim_empty = 0; |
51166b87 | 80 | LIST_HEAD(trailers); |
6634f054 CC |
81 | |
82 | struct option options[] = { | |
e1f89863 | 83 | OPT_BOOL(0, "in-place", &in_place, N_("edit files in place")), |
6634f054 | 84 | OPT_BOOL(0, "trim-empty", &trim_empty, N_("trim empty trailers")), |
51166b87 | 85 | |
0ea5292e PB |
86 | OPT_CALLBACK(0, "where", NULL, N_("action"), |
87 | N_("where to place the new trailer"), option_parse_where), | |
88 | OPT_CALLBACK(0, "if-exists", NULL, N_("action"), | |
89 | N_("action if trailer already exists"), option_parse_if_exists), | |
90 | OPT_CALLBACK(0, "if-missing", NULL, N_("action"), | |
91 | N_("action if trailer is missing"), option_parse_if_missing), | |
92 | ||
51166b87 PB |
93 | OPT_CALLBACK(0, "trailer", &trailers, N_("trailer"), |
94 | N_("trailer(s) to add"), option_parse_trailer), | |
6634f054 CC |
95 | OPT_END() |
96 | }; | |
97 | ||
98 | argc = parse_options(argc, argv, prefix, options, | |
99 | git_interpret_trailers_usage, 0); | |
100 | ||
101 | if (argc) { | |
102 | int i; | |
103 | for (i = 0; i < argc; i++) | |
e1f89863 TK |
104 | process_trailers(argv[i], in_place, trim_empty, &trailers); |
105 | } else { | |
106 | if (in_place) | |
107 | die(_("no input file given for in-place editing")); | |
108 | process_trailers(NULL, in_place, trim_empty, &trailers); | |
109 | } | |
6634f054 | 110 | |
51166b87 | 111 | new_trailers_clear(&trailers); |
6634f054 CC |
112 | |
113 | return 0; | |
114 | } |