trailer: put process_trailers() options into a struct
We already have two options and are about to add a few more. To avoid having a huge number of boolean arguments, let's convert to an options struct which can be passed in. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Aug 10, 2017 at 14:03 UTC
8abc89800c09cda7910c2211ebbbbb95a3008b63
3 files changed
+21
-12
builtin/interpret-trailers.c
+6
-7
@@ -18,13 +18,12 @@ static const char * const git_interpret_trailers_usage[] = {
18
19
int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
20
{
21
- int in_place = 0;
22
- int trim_empty = 0;
21
+ struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
22
struct string_list trailers = STRING_LIST_INIT_NODUP;
23
24
struct option options[] = {
26
- OPT_BOOL(0, "in-place", &in_place, N_("edit files in place")),
27
- OPT_BOOL(0, "trim-empty", &trim_empty, N_("trim empty trailers")),
25
+ OPT_BOOL(0, "in-place", &opts.in_place, N_("edit files in place")),
26
+ OPT_BOOL(0, "trim-empty", &opts.trim_empty, N_("trim empty trailers")),
27
OPT_STRING_LIST(0, "trailer", &trailers, N_("trailer"),
28
N_("trailer(s) to add")),
29
OPT_END()
@@ -36,11 +35,11 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
35
if (argc) {
36
int i;
37
for (i = 0; i < argc; i++)
39
- process_trailers(argv[i], in_place, trim_empty, &trailers);
38
+ process_trailers(argv[i], &opts, &trailers);
39
} else {
41
- if (in_place)
40
+ if (opts.in_place)
41
die(_("no input file given for in-place editing"));
43
- process_trailers(NULL, in_place, trim_empty, &trailers);
42
+ process_trailers(NULL, &opts, &trailers);
43
}
44
45
string_list_clear(&trailers, 0);
trailer.c
+6
-4
@@ -967,7 +967,9 @@ static FILE *create_in_place_tempfile(const char *file)
967
return outfile;
968
}
969
970
-void process_trailers(const char *file, int in_place, int trim_empty, struct string_list *trailers)
970
+void process_trailers(const char *file,
971
+ const struct process_trailer_options *opts,
972
+ struct string_list *trailers)
973
{
974
LIST_HEAD(head);
975
LIST_HEAD(arg_head);
@@ -979,7 +981,7 @@ void process_trailers(const char *file, int in_place, int trim_empty, struct str
981
982
read_input_file(&sb, file);
983
982
- if (in_place)
984
+ if (opts->in_place)
985
outfile = create_in_place_tempfile(file);
986
987
/* Print the lines before the trailers */
@@ -989,14 +991,14 @@ void process_trailers(const char *file, int in_place, int trim_empty, struct str
991
992
process_trailers_lists(&head, &arg_head);
993
992
- print_all(outfile, &head, trim_empty);
994
+ print_all(outfile, &head, opts->trim_empty);
995
996
free_all(&head);
997
998
/* Print the lines after the trailers as is */
999
fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
1000
999
- if (in_place)
1001
+ if (opts->in_place)
1002
if (rename_tempfile(&trailers_tempfile, file))
1003
die_errno(_("could not rename temporary file to %s"), file);
1004
trailer.h
+9
-1
@@ -22,7 +22,15 @@ struct trailer_info {
22
size_t trailer_nr;
23
};
24
25
-void process_trailers(const char *file, int in_place, int trim_empty,
25
+struct process_trailer_options {
26
+ int in_place;
27
+ int trim_empty;
28
+};
29
+
30
+#define PROCESS_TRAILER_OPTIONS_INIT {0}
31
+
32
+void process_trailers(const char *file,
33
+ const struct process_trailer_options *opts,
34
struct string_list *trailers);
35
36
void trailer_info_get(struct trailer_info *info, const char *str);