From: Paolo Bonzini <bonzini@gnu.org>
To: git@vger.kernel.org
Cc: Jonathan Tan <jonathantanmy@google.com>,
Paolo Bonzini <pbonzini@redhat.com>
Subject: [PATCH v2 1/3] trailers: create struct trailer_opts
Date: Thu, 13 Jul 2017 00:21:14 +0200 [thread overview]
Message-ID: <20170712222116.7095-2-bonzini@gnu.org> (raw)
In-Reply-To: <20170712222116.7095-1-bonzini@gnu.org>
From: Paolo Bonzini <pbonzini@redhat.com>
Pass the command-line arguments as a pointer to a new struct. This
will be extended in the next patch to include more options.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
v1->v2: constify
builtin/interpret-trailers.c | 13 ++++++-------
trailer.c | 14 ++++++++------
trailer.h | 7 ++++++-
3 files changed, 20 insertions(+), 14 deletions(-)
diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c
index 175f14797..6528680b5 100644
--- a/builtin/interpret-trailers.c
+++ b/builtin/interpret-trailers.c
@@ -18,13 +18,12 @@ static const char * const git_interpret_trailers_usage[] = {
int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
{
- int in_place = 0;
- int trim_empty = 0;
+ struct trailer_opts opts = { 0 };
struct string_list trailers = STRING_LIST_INIT_NODUP;
struct option options[] = {
- OPT_BOOL(0, "in-place", &in_place, N_("edit files in place")),
- OPT_BOOL(0, "trim-empty", &trim_empty, N_("trim empty trailers")),
+ OPT_BOOL(0, "in-place", &opts.in_place, N_("edit files in place")),
+ OPT_BOOL(0, "trim-empty", &opts.trim_empty, N_("trim empty trailers")),
OPT_STRING_LIST(0, "trailer", &trailers, N_("trailer"),
N_("trailer(s) to add")),
OPT_END()
@@ -36,11 +35,11 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
if (argc) {
int i;
for (i = 0; i < argc; i++)
- process_trailers(argv[i], in_place, trim_empty, &trailers);
+ process_trailers(argv[i], &opts, &trailers);
} else {
- if (in_place)
+ if (opts.in_place)
die(_("no input file given for in-place editing"));
- process_trailers(NULL, in_place, trim_empty, &trailers);
+ process_trailers(NULL, &opts, &trailers);
}
string_list_clear(&trailers, 0);
diff --git a/trailer.c b/trailer.c
index 751b56c00..a3eb42818 100644
--- a/trailer.c
+++ b/trailer.c
@@ -164,13 +164,14 @@ static void print_tok_val(FILE *outfile, const char *tok, const char *val)
fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
}
-static void print_all(FILE *outfile, struct list_head *head, int trim_empty)
+static void print_all(FILE *outfile, struct list_head *head,
+ const struct trailer_opts *opts)
{
struct list_head *pos;
struct trailer_item *item;
list_for_each(pos, head) {
item = list_entry(pos, struct trailer_item, list);
- if (!trim_empty || strlen(item->value) > 0)
+ if (!opts->trim_empty || strlen(item->value) > 0)
print_tok_val(outfile, item->token, item->value);
}
}
@@ -968,7 +969,8 @@ static FILE *create_in_place_tempfile(const char *file)
return outfile;
}
-void process_trailers(const char *file, int in_place, int trim_empty, struct string_list *trailers)
+void process_trailers(const char *file, const struct trailer_opts *opts,
+ struct string_list *trailers)
{
LIST_HEAD(head);
LIST_HEAD(arg_head);
@@ -980,7 +982,7 @@ void process_trailers(const char *file, int in_place, int trim_empty, struct str
read_input_file(&sb, file);
- if (in_place)
+ if (opts->in_place)
outfile = create_in_place_tempfile(file);
/* Print the lines before the trailers */
@@ -990,14 +992,14 @@ void process_trailers(const char *file, int in_place, int trim_empty, struct str
process_trailers_lists(&head, &arg_head);
- print_all(outfile, &head, trim_empty);
+ print_all(outfile, &head, opts);
free_all(&head);
/* Print the lines after the trailers as is */
fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
- if (in_place)
+ if (opts->in_place)
if (rename_tempfile(&trailers_tempfile, file))
die_errno(_("could not rename temporary file to %s"), file);
diff --git a/trailer.h b/trailer.h
index 65cc5d79c..e90ba1270 100644
--- a/trailer.h
+++ b/trailer.h
@@ -1,6 +1,11 @@
#ifndef TRAILER_H
#define TRAILER_H
+struct trailer_opts {
+ int in_place;
+ int trim_empty;
+};
+
struct trailer_info {
/*
* True if there is a blank line before the location pointed to by
@@ -22,7 +27,7 @@ struct trailer_info {
size_t trailer_nr;
};
-void process_trailers(const char *file, int in_place, int trim_empty,
+void process_trailers(const char *file, const struct trailer_opts *opts,
struct string_list *trailers);
void trailer_info_get(struct trailer_info *info, const char *str);
--
2.13.0
next prev parent reply other threads:[~2017-07-12 22:21 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-12 22:21 [PATCH v2 0/3] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
2017-07-12 22:21 ` Paolo Bonzini [this message]
2017-07-12 22:21 ` [PATCH v2 2/3] trailers: export action enums and corresponding lookup functions Paolo Bonzini
2017-07-13 6:00 ` Christian Couder
2017-07-17 21:13 ` Junio C Hamano
2017-07-12 22:21 ` [PATCH v2 3/3] interpret-trailers: add options for actions Paolo Bonzini
2017-07-12 23:02 ` [PATCH v2 0/3] interpret-trailers: add --where, --if-exists, --if-missing Junio C Hamano
2017-07-12 23:42 ` Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170712222116.7095-2-bonzini@gnu.org \
--to=bonzini@gnu.org \
--cc=git@vger.kernel.org \
--cc=jonathantanmy@google.com \
--cc=pbonzini@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.