git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v2 0/3] interpret-trailers: add --where, --if-exists, --if-missing
@ 2017-07-12 22:21 Paolo Bonzini
  2017-07-12 22:21 ` [PATCH v2 1/3] trailers: create struct trailer_opts Paolo Bonzini
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Paolo Bonzini @ 2017-07-12 22:21 UTC (permalink / raw)
  To: git; +Cc: Jonathan Tan, Paolo Bonzini

From: Paolo Bonzini <pbonzini@redhat.com>

These options are useful to experiment with "git interpret-trailers"
without having to tinker with .gitconfig.  It can also be useful in the
oddball case where you want a different placement for the trailer.

Compared to "git -c", they are more easily discoverable, and also have
slightly different behavior because they override all trailer.*
configuration keys.

Paolo

v1->v2: support --no-* options, minor code fixes

Paolo Bonzini (3):
  trailers: create struct trailer_opts
  trailers: export action enums and corresponding lookup functions
  interpret-trailers: add options for actions

 Documentation/git-interpret-trailers.txt |  16 +++++
 builtin/interpret-trailers.c             |  44 ++++++++++---
 t/t7513-interpret-trailers.sh            |  66 ++++++++++++++++++++
 trailer.c                                | 102 ++++++++++++++++++++-----------
 trailer.h                                |  35 ++++++++++-
 5 files changed, 218 insertions(+), 45 deletions(-)

-- 
2.13.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 1/3] trailers: create struct trailer_opts
  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
  2017-07-12 22:21 ` [PATCH v2 2/3] trailers: export action enums and corresponding lookup functions Paolo Bonzini
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2017-07-12 22:21 UTC (permalink / raw)
  To: git; +Cc: Jonathan Tan, Paolo Bonzini

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



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 2/3] trailers: export action enums and corresponding lookup functions
  2017-07-12 22:21 [PATCH v2 0/3] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
  2017-07-12 22:21 ` [PATCH v2 1/3] trailers: create struct trailer_opts Paolo Bonzini
@ 2017-07-12 22:21 ` Paolo Bonzini
  2017-07-13  6:00   ` Christian Couder
  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
  3 siblings, 1 reply; 8+ messages in thread
From: Paolo Bonzini @ 2017-07-12 22:21 UTC (permalink / raw)
  To: git; +Cc: Jonathan Tan, Paolo Bonzini

From: Paolo Bonzini <pbonzini@redhat.com>

Separate the mechanical changes out of the next patch.  The functions
are changed to take a pointer to enum, because struct conf_info is not
going to be public.

Set the default values explicitly in default_conf_info, since they are
not anymore close to default_conf_info and it's not obvious which
constant has value 0.  With the next patch, in fact, the values will
not be zero anymore!

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
	v1->v2: avoid use of designated initializers

 trailer.c | 48 +++++++++++++++++++++++-------------------------
 trailer.h | 22 ++++++++++++++++++++++
 2 files changed, 45 insertions(+), 25 deletions(-)

diff --git a/trailer.c b/trailer.c
index a3eb42818..a371c7b45 100644
--- a/trailer.c
+++ b/trailer.c
@@ -10,11 +10,6 @@
  * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
  */
 
-enum action_where { WHERE_END, WHERE_AFTER, WHERE_BEFORE, WHERE_START };
-enum action_if_exists { EXISTS_ADD_IF_DIFFERENT_NEIGHBOR, EXISTS_ADD_IF_DIFFERENT,
-			EXISTS_ADD, EXISTS_REPLACE, EXISTS_DO_NOTHING };
-enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };
-
 struct conf_info {
 	char *name;
 	char *key;
@@ -374,44 +369,44 @@ static void process_trailers_lists(struct list_head *head,
 	}
 }
 
-static int set_where(struct conf_info *item, const char *value)
+int set_where(enum action_where *item, const char *value)
 {
 	if (!strcasecmp("after", value))
-		item->where = WHERE_AFTER;
+		*item = WHERE_AFTER;
 	else if (!strcasecmp("before", value))
-		item->where = WHERE_BEFORE;
+		*item = WHERE_BEFORE;
 	else if (!strcasecmp("end", value))
-		item->where = WHERE_END;
+		*item = WHERE_END;
 	else if (!strcasecmp("start", value))
-		item->where = WHERE_START;
+		*item = WHERE_START;
 	else
 		return -1;
 	return 0;
 }
 
-static int set_if_exists(struct conf_info *item, const char *value)
+int set_if_exists(enum action_if_exists *item, const char *value)
 {
 	if (!strcasecmp("addIfDifferent", value))
-		item->if_exists = EXISTS_ADD_IF_DIFFERENT;
+		*item = EXISTS_ADD_IF_DIFFERENT;
 	else if (!strcasecmp("addIfDifferentNeighbor", value))
-		item->if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
+		*item = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
 	else if (!strcasecmp("add", value))
-		item->if_exists = EXISTS_ADD;
+		*item = EXISTS_ADD;
 	else if (!strcasecmp("replace", value))
-		item->if_exists = EXISTS_REPLACE;
+		*item = EXISTS_REPLACE;
 	else if (!strcasecmp("doNothing", value))
-		item->if_exists = EXISTS_DO_NOTHING;
+		*item = EXISTS_DO_NOTHING;
 	else
 		return -1;
 	return 0;
 }
 
-static int set_if_missing(struct conf_info *item, const char *value)
+int set_if_missing(enum action_if_missing *item, const char *value)
 {
 	if (!strcasecmp("doNothing", value))
-		item->if_missing = MISSING_DO_NOTHING;
+		*item = MISSING_DO_NOTHING;
 	else if (!strcasecmp("add", value))
-		item->if_missing = MISSING_ADD;
+		*item = MISSING_ADD;
 	else
 		return -1;
 	return 0;
@@ -471,15 +466,15 @@ static int git_trailer_default_config(const char *conf_key, const char *value, v
 	variable_name = strrchr(trailer_item, '.');
 	if (!variable_name) {
 		if (!strcmp(trailer_item, "where")) {
-			if (set_where(&default_conf_info, value) < 0)
+			if (set_where(&default_conf_info.where, value) < 0)
 				warning(_("unknown value '%s' for key '%s'"),
 					value, conf_key);
 		} else if (!strcmp(trailer_item, "ifexists")) {
-			if (set_if_exists(&default_conf_info, value) < 0)
+			if (set_if_exists(&default_conf_info.if_exists, value) < 0)
 				warning(_("unknown value '%s' for key '%s'"),
 					value, conf_key);
 		} else if (!strcmp(trailer_item, "ifmissing")) {
-			if (set_if_missing(&default_conf_info, value) < 0)
+			if (set_if_missing(&default_conf_info.if_missing, value) < 0)
 				warning(_("unknown value '%s' for key '%s'"),
 					value, conf_key);
 		} else if (!strcmp(trailer_item, "separators")) {
@@ -533,15 +528,15 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)
 		conf->command = xstrdup(value);
 		break;
 	case TRAILER_WHERE:
-		if (set_where(conf, value))
+		if (set_where(&conf->where, value))
 			warning(_("unknown value '%s' for key '%s'"), value, conf_key);
 		break;
 	case TRAILER_IF_EXISTS:
-		if (set_if_exists(conf, value))
+		if (set_if_exists(&conf->if_exists, value))
 			warning(_("unknown value '%s' for key '%s'"), value, conf_key);
 		break;
 	case TRAILER_IF_MISSING:
-		if (set_if_missing(conf, value))
+		if (set_if_missing(&conf->if_missing, value))
 			warning(_("unknown value '%s' for key '%s'"), value, conf_key);
 		break;
 	default:
@@ -556,6 +551,9 @@ static void ensure_configured(void)
 		return;
 
 	/* Default config must be setup first */
+	default_conf_info.where = WHERE_END;
+	default_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
+	default_conf_info.if_missing = MISSING_ADD;
 	git_config(git_trailer_default_config, NULL);
 	git_config(git_trailer_config, NULL);
 	configured = 1;
diff --git a/trailer.h b/trailer.h
index e90ba1270..f306bf059 100644
--- a/trailer.h
+++ b/trailer.h
@@ -1,11 +1,33 @@
 #ifndef TRAILER_H
 #define TRAILER_H
 
+enum action_where {
+	WHERE_END,
+	WHERE_AFTER,
+	WHERE_BEFORE,
+	WHERE_START
+};
+enum action_if_exists {
+	EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,
+	EXISTS_ADD_IF_DIFFERENT,
+	EXISTS_ADD,
+	EXISTS_REPLACE,
+	EXISTS_DO_NOTHING
+};
+enum action_if_missing {
+	MISSING_ADD,
+	MISSING_DO_NOTHING
+};
+
 struct trailer_opts {
 	int in_place;
 	int trim_empty;
 };
 
+int set_where(enum action_where *item, const char *value);
+int set_if_exists(enum action_if_exists *item, const char *value);
+int set_if_missing(enum action_if_missing *item, const char *value);
+
 struct trailer_info {
 	/*
 	 * True if there is a blank line before the location pointed to by
-- 
2.13.0



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 3/3] interpret-trailers: add options for actions
  2017-07-12 22:21 [PATCH v2 0/3] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
  2017-07-12 22:21 ` [PATCH v2 1/3] trailers: create struct trailer_opts Paolo Bonzini
  2017-07-12 22:21 ` [PATCH v2 2/3] trailers: export action enums and corresponding lookup functions Paolo Bonzini
@ 2017-07-12 22:21 ` Paolo Bonzini
  2017-07-12 23:02 ` [PATCH v2 0/3] interpret-trailers: add --where, --if-exists, --if-missing Junio C Hamano
  3 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2017-07-12 22:21 UTC (permalink / raw)
  To: git; +Cc: Jonathan Tan, Paolo Bonzini

From: Paolo Bonzini <pbonzini@redhat.com>

Allow using non-default values for trailers without having to set
them up in .gitconfig first.  For example, if you have the following
configuration

     trailer.signed-off-by.where = end

you may use "--where before" when a patch author forgets his
Signed-off-by and provides it in a separate email.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
	v1->v2: fix and test behavior of --no-where and similar

 Documentation/git-interpret-trailers.txt | 16 ++++++++
 builtin/interpret-trailers.c             | 31 +++++++++++++++
 t/t7513-interpret-trailers.sh            | 66 ++++++++++++++++++++++++++++++++
 trailer.c                                | 40 ++++++++++++++++---
 trailer.h                                |  6 +++
 5 files changed, 153 insertions(+), 6 deletions(-)

diff --git a/Documentation/git-interpret-trailers.txt b/Documentation/git-interpret-trailers.txt
index 31cdeaecd..f4d67b8a1 100644
--- a/Documentation/git-interpret-trailers.txt
+++ b/Documentation/git-interpret-trailers.txt
@@ -80,6 +80,22 @@ OPTIONS
 	trailer to the input messages. See the description of this
 	command.
 
+--where <placement>::
+	Specify where all new trailers will be added.  This option
+	overrides all configuration variables.
+
+--if-exists <action>::
+	Specify what action will be performed when there is already at
+	least one trailer with the same <token> in the message.  This
+	option applies to all '--trailer' options and overrides all
+	configuration variables.
+
+--if-missing <action>::
+	Specify what action will be performed when there is no other
+	trailer with the same <token> in the message.  This option
+	applies to all '--trailer' options and overrides all
+	configuration variables.
+
 CONFIGURATION VARIABLES
 -----------------------
 
diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c
index 6528680b5..bb93412ac 100644
--- a/builtin/interpret-trailers.c
+++ b/builtin/interpret-trailers.c
@@ -16,6 +16,30 @@ static const char * const git_interpret_trailers_usage[] = {
 	NULL
 };
 
+static int option_parse_where(const struct option *opt,
+			      const char *arg, int unset)
+{
+	enum action_where *where = opt->value;
+
+	return set_where(where, arg);
+}
+
+static int option_parse_if_exists(const struct option *opt,
+				  const char *arg, int unset)
+{
+	enum action_if_exists *if_exists = opt->value;
+
+	return set_if_exists(if_exists, arg);
+}
+
+static int option_parse_if_missing(const struct option *opt,
+				   const char *arg, int unset)
+{
+	enum action_if_missing *if_missing = opt->value;
+
+	return set_if_missing(if_missing, arg);
+}
+
 int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
 {
 	struct trailer_opts opts = { 0 };
@@ -24,6 +48,13 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
 	struct option options[] = {
 		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_CALLBACK(0, "where", &opts.where, N_("action"),
+			     N_("where to place the new trailer"), option_parse_where),
+		OPT_CALLBACK(0, "if-exists", &opts.if_exists, N_("action"),
+			     N_("action if trailer already exists"), option_parse_if_exists),
+		OPT_CALLBACK(0, "if-missing", &opts.if_missing, N_("action"),
+			     N_("action if trailer is missing"), option_parse_if_missing),
+
 		OPT_STRING_LIST(0, "trailer", &trailers, N_("trailer"),
 				N_("trailer(s) to add")),
 		OPT_END()
diff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh
index 0c6f91c43..adbdf54f8 100755
--- a/t/t7513-interpret-trailers.sh
+++ b/t/t7513-interpret-trailers.sh
@@ -681,6 +681,36 @@ test_expect_success 'using "where = before"' '
 	test_cmp expected actual
 '
 
+test_expect_success 'overriding configuration with "--where after"' '
+	git config trailer.ack.where "before" &&
+	cat complex_message_body >expected &&
+	sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+		Fixes: Z
+		Acked-by= Z
+		Acked-by= Peff
+		Reviewed-by: Z
+		Signed-off-by: Z
+	EOF
+	git interpret-trailers --where after --trailer "ack: Peff" \
+		complex_message >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'using "where = before" with "--no-where"' '
+	cat complex_message_body >expected &&
+	sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+		Bug #42
+		Fixes: Z
+		Acked-by= Peff
+		Acked-by= Z
+		Reviewed-by: Z
+		Signed-off-by: Z
+	EOF
+	git interpret-trailers --where after --no-where --trailer "ack: Peff" \
+		--trailer "bug: 42" complex_message >actual &&
+	test_cmp expected actual
+'
+
 test_expect_success 'using "where = after"' '
 	git config trailer.ack.where "after" &&
 	cat complex_message_body >expected &&
@@ -947,6 +977,23 @@ test_expect_success 'using "ifExists = add" with "where = after"' '
 	test_cmp expected actual
 '
 
+test_expect_success 'overriding configuration with "--if-exists replace"' '
+	git config trailer.fix.key "Fixes: " &&
+	git config trailer.fix.ifExists "add" &&
+	cat complex_message_body >expected &&
+	sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+		Bug #42
+		Acked-by= Z
+		Reviewed-by:
+		Signed-off-by: Z
+		Fixes: 22
+	EOF
+	git interpret-trailers --if-exists replace --trailer "review:" \
+		--trailer "fix=53" --trailer "fix=22" --trailer "bug: 42" \
+		<complex_message >actual &&
+	test_cmp expected actual
+'
+
 test_expect_success 'using "ifExists = replace"' '
 	git config trailer.fix.key "Fixes: " &&
 	git config trailer.fix.ifExists "replace" &&
@@ -1026,6 +1073,25 @@ test_expect_success 'the default is "ifMissing = add"' '
 	test_cmp expected actual
 '
 
+test_expect_success 'overriding configuration with "--if-missing doNothing"' '
+	git config trailer.ifmissing "add" &&
+	cat complex_message_body >expected &&
+	sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+		Fixes: Z
+		Acked-by= Z
+		Acked-by= Junio
+		Acked-by= Peff
+		Reviewed-by:
+		Signed-off-by: Z
+	EOF
+	git interpret-trailers --if-missing doNothing \
+		--trailer "review:" --trailer "fix=53" \
+		--trailer "cc=Linus" --trailer "ack: Junio" \
+		--trailer "fix=22" --trailer "bug: 42" --trailer "ack: Peff" \
+		<complex_message >actual &&
+	test_cmp expected actual
+'
+
 test_expect_success 'when default "ifMissing" is "doNothing"' '
 	git config trailer.ifmissing "doNothing" &&
 	cat complex_message_body >expected &&
diff --git a/trailer.c b/trailer.c
index a371c7b45..0a3d207d9 100644
--- a/trailer.c
+++ b/trailer.c
@@ -371,7 +371,9 @@ static void process_trailers_lists(struct list_head *head,
 
 int set_where(enum action_where *item, const char *value)
 {
-	if (!strcasecmp("after", value))
+	if (!value)
+		*item = WHERE_DEFAULT;
+	else if (!strcasecmp("after", value))
 		*item = WHERE_AFTER;
 	else if (!strcasecmp("before", value))
 		*item = WHERE_BEFORE;
@@ -386,7 +388,9 @@ int set_where(enum action_where *item, const char *value)
 
 int set_if_exists(enum action_if_exists *item, const char *value)
 {
-	if (!strcasecmp("addIfDifferent", value))
+	if (!value)
+		*item = EXISTS_DEFAULT;
+	else if (!strcasecmp("addIfDifferent", value))
 		*item = EXISTS_ADD_IF_DIFFERENT;
 	else if (!strcasecmp("addIfDifferentNeighbor", value))
 		*item = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
@@ -403,7 +407,9 @@ int set_if_exists(enum action_if_exists *item, const char *value)
 
 int set_if_missing(enum action_if_missing *item, const char *value)
 {
-	if (!strcasecmp("doNothing", value))
+	if (!value)
+		*item = MISSING_DEFAULT;
+	else if (!strcasecmp("doNothing", value))
 		*item = MISSING_DO_NOTHING;
 	else if (!strcasecmp("add", value))
 		*item = MISSING_ADD;
@@ -545,8 +551,24 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)
 	return 0;
 }
 
-static void ensure_configured(void)
+static void apply_config_overrides(struct conf_info *conf,
+				   const struct trailer_opts *opts)
 {
+	if (!opts)
+		return;
+	if (opts->where != WHERE_DEFAULT)
+		conf->where = opts->where;
+	if (opts->if_exists != EXISTS_DEFAULT)
+		conf->if_exists = opts->if_exists;
+	if (opts->if_missing != MISSING_DEFAULT)
+		conf->if_missing = opts->if_missing;
+}
+
+static void ensure_configured(const struct trailer_opts *opts)
+{
+	struct arg_item *item;
+	struct list_head *pos;
+
 	if (configured)
 		return;
 
@@ -555,7 +576,14 @@ static void ensure_configured(void)
 	default_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
 	default_conf_info.if_missing = MISSING_ADD;
 	git_config(git_trailer_default_config, NULL);
+	apply_config_overrides(&default_conf_info, opts);
+
 	git_config(git_trailer_config, NULL);
+	list_for_each(pos, &conf_head) {
+		item = list_entry(pos, struct arg_item, list);
+		apply_config_overrides(&item->conf, opts);
+	}
+
 	configured = 1;
 }
 
@@ -976,7 +1004,7 @@ void process_trailers(const char *file, const struct trailer_opts *opts,
 	int trailer_end;
 	FILE *outfile = stdout;
 
-	ensure_configured();
+	ensure_configured(opts);
 
 	read_input_file(&sb, file);
 
@@ -1012,7 +1040,7 @@ void trailer_info_get(struct trailer_info *info, const char *str)
 	size_t nr = 0, alloc = 0;
 	char **last = NULL;
 
-	ensure_configured();
+	ensure_configured(NULL);
 
 	patch_start = find_patch_start(str);
 	trailer_end = find_trailer_end(str, patch_start);
diff --git a/trailer.h b/trailer.h
index f306bf059..634643c9d 100644
--- a/trailer.h
+++ b/trailer.h
@@ -2,12 +2,14 @@
 #define TRAILER_H
 
 enum action_where {
+	WHERE_DEFAULT,
 	WHERE_END,
 	WHERE_AFTER,
 	WHERE_BEFORE,
 	WHERE_START
 };
 enum action_if_exists {
+	EXISTS_DEFAULT,
 	EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,
 	EXISTS_ADD_IF_DIFFERENT,
 	EXISTS_ADD,
@@ -15,6 +17,7 @@ enum action_if_exists {
 	EXISTS_DO_NOTHING
 };
 enum action_if_missing {
+	MISSING_DEFAULT,
 	MISSING_ADD,
 	MISSING_DO_NOTHING
 };
@@ -22,6 +25,9 @@ enum action_if_missing {
 struct trailer_opts {
 	int in_place;
 	int trim_empty;
+	enum action_where where;
+	enum action_if_exists if_exists;
+	enum action_if_missing if_missing;
 };
 
 int set_where(enum action_where *item, const char *value);
-- 
2.13.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 0/3] interpret-trailers: add --where, --if-exists, --if-missing
  2017-07-12 22:21 [PATCH v2 0/3] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
                   ` (2 preceding siblings ...)
  2017-07-12 22:21 ` [PATCH v2 3/3] interpret-trailers: add options for actions Paolo Bonzini
@ 2017-07-12 23:02 ` Junio C Hamano
  2017-07-12 23:42   ` Paolo Bonzini
  3 siblings, 1 reply; 8+ messages in thread
From: Junio C Hamano @ 2017-07-12 23:02 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: git, Jonathan Tan, Paolo Bonzini

Paolo Bonzini <bonzini@gnu.org> writes:

> From: Paolo Bonzini <pbonzini@redhat.com>
>
> These options are useful to experiment with "git interpret-trailers"
> without having to tinker with .gitconfig.  It can also be useful in the
> oddball case where you want a different placement for the trailer.
>
> Compared to "git -c", they are more easily discoverable, and also have
> slightly different behavior because they override all trailer.*
> configuration keys.

I think this is a very good idea (we shouldn't have started the
command only with the configurations; we rather should have done
this first and then added configuration after that).

Looking forward to reviewing them, but I am cutting a maint release
now, so it may have to wait a bit.

Thanks.

>
> Paolo
>
> v1->v2: support --no-* options, minor code fixes
>
> Paolo Bonzini (3):
>   trailers: create struct trailer_opts
>   trailers: export action enums and corresponding lookup functions
>   interpret-trailers: add options for actions
>
>  Documentation/git-interpret-trailers.txt |  16 +++++
>  builtin/interpret-trailers.c             |  44 ++++++++++---
>  t/t7513-interpret-trailers.sh            |  66 ++++++++++++++++++++
>  trailer.c                                | 102 ++++++++++++++++++++-----------
>  trailer.h                                |  35 ++++++++++-
>  5 files changed, 218 insertions(+), 45 deletions(-)

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 0/3] interpret-trailers: add --where, --if-exists, --if-missing
  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
  0 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2017-07-12 23:42 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jonathan Tan, Paolo Bonzini

On 13/07/2017 01:02, Junio C Hamano wrote:
> Paolo Bonzini <bonzini@gnu.org> writes:
> 
>> From: Paolo Bonzini <pbonzini@redhat.com>
>>
>> These options are useful to experiment with "git interpret-trailers"
>> without having to tinker with .gitconfig.  It can also be useful in the
>> oddball case where you want a different placement for the trailer.
>>
>> Compared to "git -c", they are more easily discoverable, and also have
>> slightly different behavior because they override all trailer.*
>> configuration keys.
> 
> I think this is a very good idea (we shouldn't have started the
> command only with the configurations; we rather should have done
> this first and then added configuration after that).

Actually we can do better: we can have --where only refer to
*subsequent* --trailer options.  This will require more refactoring
(probably making a new struct that can be passed to process_trailers),
but the first two patches should be the same.

I'll have time for this only after vacation (so at the end of July), and
it's backwards-incompatible with this series.  Reviews are welcome
anyway. :)

Thanks for encouraging me.  It's always a pleasure when I can scratch my
git itches!

Paolo

> Looking forward to reviewing them, but I am cutting a maint release
> now, so it may have to wait a bit.
> 
> Thanks.
> 
>>
>> Paolo
>>
>> v1->v2: support --no-* options, minor code fixes
>>
>> Paolo Bonzini (3):
>>   trailers: create struct trailer_opts
>>   trailers: export action enums and corresponding lookup functions
>>   interpret-trailers: add options for actions
>>
>>  Documentation/git-interpret-trailers.txt |  16 +++++
>>  builtin/interpret-trailers.c             |  44 ++++++++++---
>>  t/t7513-interpret-trailers.sh            |  66 ++++++++++++++++++++
>>  trailer.c                                | 102 ++++++++++++++++++++-----------
>>  trailer.h                                |  35 ++++++++++-
>>  5 files changed, 218 insertions(+), 45 deletions(-)
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 2/3] trailers: export action enums and corresponding lookup functions
  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
  0 siblings, 1 reply; 8+ messages in thread
From: Christian Couder @ 2017-07-13  6:00 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: git, Jonathan Tan, Paolo Bonzini

On Thu, Jul 13, 2017 at 12:21 AM, Paolo Bonzini <bonzini@gnu.org> wrote:

> diff --git a/trailer.h b/trailer.h
> index e90ba1270..f306bf059 100644
> --- a/trailer.h
> +++ b/trailer.h
> @@ -1,11 +1,33 @@
>  #ifndef TRAILER_H
>  #define TRAILER_H
>
> +enum action_where {
> +       WHERE_END,
> +       WHERE_AFTER,
> +       WHERE_BEFORE,
> +       WHERE_START
> +};
> +enum action_if_exists {
> +       EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,
> +       EXISTS_ADD_IF_DIFFERENT,
> +       EXISTS_ADD,
> +       EXISTS_REPLACE,
> +       EXISTS_DO_NOTHING
> +};
> +enum action_if_missing {
> +       MISSING_ADD,
> +       MISSING_DO_NOTHING
> +};

As these enums are now in trailer.h, maybe more specific names like
"trailer_action_where" instead of "action_where" would be better.

>  struct trailer_opts {
>         int in_place;
>         int trim_empty;
>  };
>
> +int set_where(enum action_where *item, const char *value);
> +int set_if_exists(enum action_if_exists *item, const char *value);
> +int set_if_missing(enum action_if_missing *item, const char *value);

"trailer_" should perhaps be added at the beginning of the names of
the above functions too.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 2/3] trailers: export action enums and corresponding lookup functions
  2017-07-13  6:00   ` Christian Couder
@ 2017-07-17 21:13     ` Junio C Hamano
  0 siblings, 0 replies; 8+ messages in thread
From: Junio C Hamano @ 2017-07-17 21:13 UTC (permalink / raw)
  To: Christian Couder; +Cc: Paolo Bonzini, git, Jonathan Tan, Paolo Bonzini

Christian Couder <christian.couder@gmail.com> writes:

> On Thu, Jul 13, 2017 at 12:21 AM, Paolo Bonzini <bonzini@gnu.org> wrote:
>
>> diff --git a/trailer.h b/trailer.h
>> index e90ba1270..f306bf059 100644
>> --- a/trailer.h
>> +++ b/trailer.h
>> @@ -1,11 +1,33 @@
>>  #ifndef TRAILER_H
>>  #define TRAILER_H
>>
>> +enum action_where {
>> +       WHERE_END,
>> +       WHERE_AFTER,
>> +       WHERE_BEFORE,
>> +       WHERE_START
>> +};
>> +enum action_if_exists {
>> +       EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,
>> +       EXISTS_ADD_IF_DIFFERENT,
>> +       EXISTS_ADD,
>> +       EXISTS_REPLACE,
>> +       EXISTS_DO_NOTHING
>> +};
>> +enum action_if_missing {
>> +       MISSING_ADD,
>> +       MISSING_DO_NOTHING
>> +};
>
> As these enums are now in trailer.h, maybe more specific names like
> "trailer_action_where" instead of "action_where" would be better.
>
>>  struct trailer_opts {
>>         int in_place;
>>         int trim_empty;
>>  };
>>
>> +int set_where(enum action_where *item, const char *value);
>> +int set_if_exists(enum action_if_exists *item, const char *value);
>> +int set_if_missing(enum action_if_missing *item, const char *value);
>
> "trailer_" should perhaps be added at the beginning of the names of
> the above functions too.

All sensible suggestions.  Thanks.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2017-07-17 21:16 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-12 22:21 [PATCH v2 0/3] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
2017-07-12 22:21 ` [PATCH v2 1/3] trailers: create struct trailer_opts Paolo Bonzini
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

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).