git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v4 0/4] interpret-trailers: add --where, --if-exists, --if-missing
@ 2017-08-01  9:03 Paolo Bonzini
  2017-08-01  9:03 ` [PATCH v4 1/4] trailers: export action enums and corresponding lookup functions Paolo Bonzini
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Paolo Bonzini @ 2017-08-01  9:03 UTC (permalink / raw)
  To: git; +Cc: Christian Couder, 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 (Junio said git should ahve
done this first and only added configuration afterwards).  It can
be useful in the case where you want a different placement for the trailer,
or for scripts/aliases that don't want to rely on specific .gitconfig
settings.

Compared to v2, the main change is that option order on the command-line
is respected.  That is,

	--trailer 'acked-by: foo' --where end --trailer 'signed-off-by: me'

will only apply where=end to the second trailer.  Likewise,

	--where end --trailer 'signed-off-by: me' --no-where \
	--trailer 'acked-by: foo'

will only apply it to the first, reverting to trailer.*.where for the
"acked-by" trailer.

Paolo

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

v2->v3: largely rewritten to respect option order on the command-line;
	keep trailer.h namespace clean (Christian)

v3->v4: fix compilation warnings (Junio), added documentation fix

Paolo Bonzini (4):
  trailers: export action enums and corresponding lookup functions
  trailers: introduce struct new_trailer_item
  interpret-trailers: add options for actions
  interpret-trailers: fix documentation typo

 Documentation/git-interpret-trailers.txt |  27 ++++++-
 builtin/interpret-trailers.c             |  73 +++++++++++++++++--
 t/t7513-interpret-trailers.sh            |  66 +++++++++++++++++
 trailer.c                                | 118 +++++++++++++++++++------------
 trailer.h                                |  43 ++++++++++-
 5 files changed, 274 insertions(+), 53 deletions(-)

-- 
2.13.3


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

* [PATCH v4 1/4] trailers: export action enums and corresponding lookup functions
  2017-08-01  9:03 [PATCH v4 0/4] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
@ 2017-08-01  9:03 ` Paolo Bonzini
  2017-08-01  9:03 ` [PATCH v4 2/4] trailers: introduce struct new_trailer_item Paolo Bonzini
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Paolo Bonzini @ 2017-08-01  9:03 UTC (permalink / raw)
  To: git; +Cc: Christian Couder, 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 patches, in fact, the values will
not be zero anymore!

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 trailer.c | 65 ++++++++++++++++++++++++++++++++-------------------------------
 trailer.h | 22 +++++++++++++++++++++
 2 files changed, 55 insertions(+), 32 deletions(-)

diff --git a/trailer.c b/trailer.c
index 751b56c00..f02895373 100644
--- a/trailer.c
+++ b/trailer.c
@@ -10,18 +10,13 @@
  * 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;
 	char *command;
-	enum action_where where;
-	enum action_if_exists if_exists;
-	enum action_if_missing if_missing;
+	enum trailer_where where;
+	enum trailer_if_exists if_exists;
+	enum trailer_if_missing if_missing;
 };
 
 static struct conf_info default_conf_info;
@@ -63,7 +58,7 @@ static const char *git_generated_prefixes[] = {
 		pos != (head); \
 		pos = is_reverse ? pos->prev : pos->next)
 
-static int after_or_end(enum action_where where)
+static int after_or_end(enum trailer_where where)
 {
 	return (where == WHERE_AFTER) || (where == WHERE_END);
 }
@@ -201,7 +196,7 @@ static int check_if_different(struct trailer_item *in_tok,
 			      int check_all,
 			      struct list_head *head)
 {
-	enum action_where where = arg_tok->conf.where;
+	enum trailer_where where = arg_tok->conf.where;
 	struct list_head *next_head;
 	do {
 		if (same_trailer(in_tok, arg_tok))
@@ -306,7 +301,7 @@ static void apply_arg_if_exists(struct trailer_item *in_tok,
 static void apply_arg_if_missing(struct list_head *head,
 				 struct arg_item *arg_tok)
 {
-	enum action_where where;
+	enum trailer_where where;
 	struct trailer_item *to_add;
 
 	switch (arg_tok->conf.if_missing) {
@@ -331,7 +326,7 @@ static int find_same_and_apply_arg(struct list_head *head,
 	struct trailer_item *in_tok;
 	struct trailer_item *on_tok;
 
-	enum action_where where = arg_tok->conf.where;
+	enum trailer_where where = arg_tok->conf.where;
 	int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
 	int backwards = after_or_end(where);
 	struct trailer_item *start_tok;
@@ -373,44 +368,44 @@ static void process_trailers_lists(struct list_head *head,
 	}
 }
 
-static int set_where(struct conf_info *item, const char *value)
+int trailer_set_where(enum trailer_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 trailer_set_if_exists(enum trailer_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 trailer_set_if_missing(enum trailer_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;
@@ -470,15 +465,18 @@ 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 (trailer_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 (trailer_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 (trailer_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")) {
@@ -532,15 +530,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 (trailer_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 (trailer_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 (trailer_set_if_missing(&conf->if_missing, value))
 			warning(_("unknown value '%s' for key '%s'"), value, conf_key);
 		break;
 	default:
@@ -555,6 +553,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 65cc5d79c..2b39a1bee 100644
--- a/trailer.h
+++ b/trailer.h
@@ -1,6 +1,28 @@
 #ifndef TRAILER_H
 #define TRAILER_H
 
+enum trailer_where {
+	WHERE_END,
+	WHERE_AFTER,
+	WHERE_BEFORE,
+	WHERE_START
+};
+enum trailer_if_exists {
+	EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,
+	EXISTS_ADD_IF_DIFFERENT,
+	EXISTS_ADD,
+	EXISTS_REPLACE,
+	EXISTS_DO_NOTHING
+};
+enum trailer_if_missing {
+	MISSING_ADD,
+	MISSING_DO_NOTHING
+};
+
+int trailer_set_where(enum trailer_where *item, const char *value);
+int trailer_set_if_exists(enum trailer_if_exists *item, const char *value);
+int trailer_set_if_missing(enum trailer_if_missing *item, const char *value);
+
 struct trailer_info {
 	/*
 	 * True if there is a blank line before the location pointed to by
-- 
2.13.3



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

* [PATCH v4 2/4] trailers: introduce struct new_trailer_item
  2017-08-01  9:03 [PATCH v4 0/4] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
  2017-08-01  9:03 ` [PATCH v4 1/4] trailers: export action enums and corresponding lookup functions Paolo Bonzini
@ 2017-08-01  9:03 ` Paolo Bonzini
  2017-08-01  9:03 ` [PATCH v4 3/4] interpret-trailers: add options for actions Paolo Bonzini
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Paolo Bonzini @ 2017-08-01  9:03 UTC (permalink / raw)
  To: git; +Cc: Christian Couder, Jonathan Tan, Paolo Bonzini

From: Paolo Bonzini <pbonzini@redhat.com>

This will provide a place to store the current state of the
--where, --if-exists and --if-missing options.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 builtin/interpret-trailers.c | 41 +++++++++++++++++++++++++++++++++++++----
 trailer.c                    | 19 +++++++++++--------
 trailer.h                    | 14 +++++++++++++-
 3 files changed, 61 insertions(+), 13 deletions(-)

diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c
index 175f14797..8f38fa318 100644
--- a/builtin/interpret-trailers.c
+++ b/builtin/interpret-trailers.c
@@ -16,17 +16,50 @@ static const char * const git_interpret_trailers_usage[] = {
 	NULL
 };
 
+static void new_trailers_clear(struct list_head *trailers)
+{
+	struct list_head *pos, *tmp;
+	struct new_trailer_item *item;
+
+	list_for_each_safe(pos, tmp, trailers) {
+		item = list_entry(pos, struct new_trailer_item, list);
+		list_del(pos);
+		free(item);
+	}
+}
+
+static int option_parse_trailer(const struct option *opt,
+				   const char *arg, int unset)
+{
+	struct list_head *trailers = opt->value;
+	struct new_trailer_item *item;
+
+	if (unset) {
+		new_trailers_clear(trailers);
+		return 0;
+	}
+
+	if (!arg)
+		return -1;
+
+	item = xmalloc(sizeof *item);
+	item->text = arg;
+	list_add_tail(&item->list, trailers);
+	return 0;
+}
+
 int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
 {
 	int in_place = 0;
 	int trim_empty = 0;
-	struct string_list trailers = STRING_LIST_INIT_NODUP;
+	LIST_HEAD(trailers);
 
 	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_STRING_LIST(0, "trailer", &trailers, N_("trailer"),
-				N_("trailer(s) to add")),
+
+		OPT_CALLBACK(0, "trailer", &trailers, N_("trailer"),
+				N_("trailer(s) to add"), option_parse_trailer),
 		OPT_END()
 	};
 
@@ -43,7 +76,7 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
 		process_trailers(NULL, in_place, trim_empty, &trailers);
 	}
 
-	string_list_clear(&trailers, 0);
+	new_trailers_clear(&trailers);
 
 	return 0;
 }
diff --git a/trailer.c b/trailer.c
index f02895373..6941da799 100644
--- a/trailer.c
+++ b/trailer.c
@@ -669,9 +669,8 @@ static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
 }
 
 static void process_command_line_args(struct list_head *arg_head,
-				      struct string_list *trailers)
+				      struct list_head *new_trailer_head)
 {
-	struct string_list_item *tr;
 	struct arg_item *item;
 	struct strbuf tok = STRBUF_INIT;
 	struct strbuf val = STRBUF_INIT;
@@ -695,17 +694,20 @@ static void process_command_line_args(struct list_head *arg_head,
 	}
 
 	/* Add an arg item for each trailer on the command line */
-	for_each_string_list_item(tr, trailers) {
-		int separator_pos = find_separator(tr->string, cl_separators);
+	list_for_each(pos, new_trailer_head) {
+		struct new_trailer_item *tr =
+			list_entry(pos, struct new_trailer_item, list);
+		int separator_pos = find_separator(tr->text, cl_separators);
+
 		if (separator_pos == 0) {
 			struct strbuf sb = STRBUF_INIT;
-			strbuf_addstr(&sb, tr->string);
+			strbuf_addstr(&sb, tr->text);
 			strbuf_trim(&sb);
 			error(_("empty trailer token in trailer '%.*s'"),
 			      (int) sb.len, sb.buf);
 			strbuf_release(&sb);
 		} else {
-			parse_trailer(&tok, &val, &conf, tr->string,
+			parse_trailer(&tok, &val, &conf, tr->text,
 				      separator_pos);
 			add_arg_item(arg_head,
 				     strbuf_detach(&tok, NULL),
@@ -969,7 +971,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, int in_place, int trim_empty,
+		      struct list_head *new_trailer_head)
 {
 	LIST_HEAD(head);
 	LIST_HEAD(arg_head);
@@ -987,7 +990,7 @@ void process_trailers(const char *file, int in_place, int trim_empty, struct str
 	/* Print the lines before the trailers */
 	trailer_end = process_input_file(outfile, sb.buf, &head);
 
-	process_command_line_args(&arg_head, trailers);
+	process_command_line_args(&arg_head, new_trailer_head);
 
 	process_trailers_lists(&head, &arg_head);
 
diff --git a/trailer.h b/trailer.h
index 2b39a1bee..b83b249b6 100644
--- a/trailer.h
+++ b/trailer.h
@@ -1,6 +1,8 @@
 #ifndef TRAILER_H
 #define TRAILER_H
 
+#include "list.h"
+
 enum trailer_where {
 	WHERE_END,
 	WHERE_AFTER,
@@ -44,8 +46,18 @@ struct trailer_info {
 	size_t trailer_nr;
 };
 
+/*
+ * A list that represents newly-added trailers, such as those provided
+ * with the --trailer command line option of git-interpret-trailers.
+ */
+struct new_trailer_item {
+	struct list_head list;
+
+	const char *text;
+};
+
 void process_trailers(const char *file, int in_place, int trim_empty,
-		      struct string_list *trailers);
+		      struct list_head *new_trailer_head);
 
 void trailer_info_get(struct trailer_info *info, const char *str);
 
-- 
2.13.3



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

* [PATCH v4 3/4] interpret-trailers: add options for actions
  2017-08-01  9:03 [PATCH v4 0/4] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
  2017-08-01  9:03 ` [PATCH v4 1/4] trailers: export action enums and corresponding lookup functions Paolo Bonzini
  2017-08-01  9:03 ` [PATCH v4 2/4] trailers: introduce struct new_trailer_item Paolo Bonzini
@ 2017-08-01  9:03 ` Paolo Bonzini
  2017-08-01  9:03 ` [PATCH v4 4/4] interpret-trailers: fix documentation typo Paolo Bonzini
  2017-08-14  9:26 ` [PATCH v4 0/4] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
  4 siblings, 0 replies; 7+ messages in thread
From: Paolo Bonzini @ 2017-08-01  9:03 UTC (permalink / raw)
  To: git; +Cc: Christian Couder, 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.  Likewise for
--if-exists and --if-missing

Reverting to the behavior specified by .gitconfig is done with
--no-where, --no-if-exists and --no-if-missing.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 Documentation/git-interpret-trailers.txt | 23 +++++++++++
 builtin/interpret-trailers.c             | 32 ++++++++++++++++
 t/t7513-interpret-trailers.sh            | 66 ++++++++++++++++++++++++++++++++
 trailer.c                                | 34 +++++++++++++---
 trailer.h                                |  7 ++++
 5 files changed, 156 insertions(+), 6 deletions(-)

diff --git a/Documentation/git-interpret-trailers.txt b/Documentation/git-interpret-trailers.txt
index 31cdeaecd..76d5fdfaf 100644
--- a/Documentation/git-interpret-trailers.txt
+++ b/Documentation/git-interpret-trailers.txt
@@ -80,6 +80,29 @@ OPTIONS
 	trailer to the input messages. See the description of this
 	command.
 
+--where <placement>::
+--no-where::
+	Specify where all new trailers will be added.  A setting
+	provided with '--where' overrides all configuration variables
+	and applies to all '--trailer' options until the next occurrence of
+	'--where' or '--no-where'.
+
+--if-exists <action>::
+--no-if-exists::
+	Specify what action will be performed when there is already at
+	least one trailer with the same <token> in the message.  A setting
+	provided with '--if-exists' overrides all configuration variables
+	and applies to all '--trailer' options until the next occurrence of
+	'--if-exists' or '--no-if-exists'.
+
+--if-missing <action>::
+--no-if-missing::
+	Specify what action will be performed when there is no other
+	trailer with the same <token> in the message.  A setting
+	provided with '--if-missing' overrides all configuration variables
+	and applies to all '--trailer' options until the next occurrence of
+	'--if-missing' or '--no-if-missing'.
+
 CONFIGURATION VARIABLES
 -----------------------
 
diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c
index 8f38fa318..83249e3eb 100644
--- a/builtin/interpret-trailers.c
+++ b/builtin/interpret-trailers.c
@@ -16,6 +16,28 @@ static const char * const git_interpret_trailers_usage[] = {
 	NULL
 };
 
+static enum trailer_where where;
+static enum trailer_if_exists if_exists;
+static enum trailer_if_missing if_missing;
+
+static int option_parse_where(const struct option *opt,
+			      const char *arg, int unset)
+{
+	return trailer_set_where(&where, arg);
+}
+
+static int option_parse_if_exists(const struct option *opt,
+				  const char *arg, int unset)
+{
+	return trailer_set_if_exists(&if_exists, arg);
+}
+
+static int option_parse_if_missing(const struct option *opt,
+				   const char *arg, int unset)
+{
+	return trailer_set_if_missing(&if_missing, arg);
+}
+
 static void new_trailers_clear(struct list_head *trailers)
 {
 	struct list_head *pos, *tmp;
@@ -44,6 +66,9 @@ static int option_parse_trailer(const struct option *opt,
 
 	item = xmalloc(sizeof *item);
 	item->text = arg;
+	item->where = where;
+	item->if_exists = if_exists;
+	item->if_missing = if_missing;
 	list_add_tail(&item->list, trailers);
 	return 0;
 }
@@ -58,6 +83,13 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
 		OPT_BOOL(0, "in-place", &in_place, N_("edit files in place")),
 		OPT_BOOL(0, "trim-empty", &trim_empty, N_("trim empty trailers")),
 
+		OPT_CALLBACK(0, "where", NULL, N_("action"),
+			     N_("where to place the new trailer"), option_parse_where),
+		OPT_CALLBACK(0, "if-exists", NULL, N_("action"),
+			     N_("action if trailer already exists"), option_parse_if_exists),
+		OPT_CALLBACK(0, "if-missing", NULL, N_("action"),
+			     N_("action if trailer is missing"), option_parse_if_missing),
+
 		OPT_CALLBACK(0, "trailer", &trailers, N_("trailer"),
 				N_("trailer(s) to add"), option_parse_trailer),
 		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 6941da799..d441cd9ac 100644
--- a/trailer.c
+++ b/trailer.c
@@ -295,6 +295,9 @@ static void apply_arg_if_exists(struct trailer_item *in_tok,
 		else
 			free_arg_item(arg_tok);
 		break;
+	default:
+		die("BUG: trailer.c: unhandled value %d",
+		    arg_tok->conf.if_exists);
 	}
 }
 
@@ -316,6 +319,10 @@ static void apply_arg_if_missing(struct list_head *head,
 			list_add_tail(&to_add->list, head);
 		else
 			list_add(&to_add->list, head);
+		break;
+	default:
+		die("BUG: trailer.c: unhandled value %d",
+		    arg_tok->conf.if_missing);
 	}
 }
 
@@ -370,7 +377,9 @@ static void process_trailers_lists(struct list_head *head,
 
 int trailer_set_where(enum trailer_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;
@@ -385,7 +394,9 @@ int trailer_set_where(enum trailer_where *item, const char *value)
 
 int trailer_set_if_exists(enum trailer_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;
@@ -402,7 +413,9 @@ int trailer_set_if_exists(enum trailer_if_exists *item, const char *value)
 
 int trailer_set_if_missing(enum trailer_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;
@@ -659,12 +672,21 @@ static struct trailer_item *add_trailer_item(struct list_head *head, char *tok,
 }
 
 static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
-			 const struct conf_info *conf)
+			 const struct conf_info *conf,
+			 const struct new_trailer_item *new_trailer_item)
 {
 	struct arg_item *new = xcalloc(sizeof(*new), 1);
 	new->token = tok;
 	new->value = val;
 	duplicate_conf(&new->conf, conf);
+	if (new_trailer_item) {
+		if (new_trailer_item->where != WHERE_DEFAULT)
+			new->conf.where = new_trailer_item->where;
+		if (new_trailer_item->if_exists != EXISTS_DEFAULT)
+			new->conf.if_exists = new_trailer_item->if_exists;
+		if (new_trailer_item->if_missing != MISSING_DEFAULT)
+			new->conf.if_missing = new_trailer_item->if_missing;
+	}
 	list_add_tail(&new->list, arg_head);
 }
 
@@ -690,7 +712,7 @@ static void process_command_line_args(struct list_head *arg_head,
 			add_arg_item(arg_head,
 				     xstrdup(token_from_item(item, NULL)),
 				     xstrdup(""),
-				     &item->conf);
+				     &item->conf, NULL);
 	}
 
 	/* Add an arg item for each trailer on the command line */
@@ -712,7 +734,7 @@ static void process_command_line_args(struct list_head *arg_head,
 			add_arg_item(arg_head,
 				     strbuf_detach(&tok, NULL),
 				     strbuf_detach(&val, NULL),
-				     conf);
+				     conf, tr);
 		}
 	}
 
diff --git a/trailer.h b/trailer.h
index b83b249b6..973b533a1 100644
--- a/trailer.h
+++ b/trailer.h
@@ -4,12 +4,14 @@
 #include "list.h"
 
 enum trailer_where {
+	WHERE_DEFAULT,
 	WHERE_END,
 	WHERE_AFTER,
 	WHERE_BEFORE,
 	WHERE_START
 };
 enum trailer_if_exists {
+	EXISTS_DEFAULT,
 	EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,
 	EXISTS_ADD_IF_DIFFERENT,
 	EXISTS_ADD,
@@ -17,6 +19,7 @@ enum trailer_if_exists {
 	EXISTS_DO_NOTHING
 };
 enum trailer_if_missing {
+	MISSING_DEFAULT,
 	MISSING_ADD,
 	MISSING_DO_NOTHING
 };
@@ -54,6 +57,10 @@ struct new_trailer_item {
 	struct list_head list;
 
 	const char *text;
+
+	enum trailer_where where;
+	enum trailer_if_exists if_exists;
+	enum trailer_if_missing if_missing;
 };
 
 void process_trailers(const char *file, int in_place, int trim_empty,
-- 
2.13.3



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

* [PATCH v4 4/4] interpret-trailers: fix documentation typo
  2017-08-01  9:03 [PATCH v4 0/4] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
                   ` (2 preceding siblings ...)
  2017-08-01  9:03 ` [PATCH v4 3/4] interpret-trailers: add options for actions Paolo Bonzini
@ 2017-08-01  9:03 ` Paolo Bonzini
  2017-08-14  9:26 ` [PATCH v4 0/4] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
  4 siblings, 0 replies; 7+ messages in thread
From: Paolo Bonzini @ 2017-08-01  9:03 UTC (permalink / raw)
  To: git; +Cc: Christian Couder, Jonathan Tan, Paolo Bonzini

From: Paolo Bonzini <pbonzini@redhat.com>

Self-explanatory... trailer.ifexists is documented with the
right name, but after a while it switches to ifexist.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 Documentation/git-interpret-trailers.txt | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Documentation/git-interpret-trailers.txt b/Documentation/git-interpret-trailers.txt
index 76d5fdfaf..0ef93204f 100644
--- a/Documentation/git-interpret-trailers.txt
+++ b/Documentation/git-interpret-trailers.txt
@@ -193,8 +193,8 @@ trailer.<token>.where::
 	configuration variable and it overrides what is specified by
 	that option for trailers with the specified <token>.
 
-trailer.<token>.ifexist::
-	This option takes the same values as the 'trailer.ifexist'
+trailer.<token>.ifexists::
+	This option takes the same values as the 'trailer.ifexists'
 	configuration variable and it overrides what is specified by
 	that option for trailers with the specified <token>.
 
-- 
2.13.3


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

* Re: [PATCH v4 0/4] interpret-trailers: add --where, --if-exists, --if-missing
  2017-08-01  9:03 [PATCH v4 0/4] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
                   ` (3 preceding siblings ...)
  2017-08-01  9:03 ` [PATCH v4 4/4] interpret-trailers: fix documentation typo Paolo Bonzini
@ 2017-08-14  9:26 ` Paolo Bonzini
  2017-08-14 17:57   ` Junio C Hamano
  4 siblings, 1 reply; 7+ messages in thread
From: Paolo Bonzini @ 2017-08-14  9:26 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Christian Couder, Jonathan Tan, Paolo Bonzini

On 01/08/2017 11:03, Paolo Bonzini wrote:
> From: Paolo Bonzini <pbonzini@redhat.com>
> 
> These options are useful to experiment with "git interpret-trailers"
> without having to tinker with .gitconfig (Junio said git should ahve
> done this first and only added configuration afterwards).  It can
> be useful in the case where you want a different placement for the trailer,
> or for scripts/aliases that don't want to rely on specific .gitconfig
> settings.
> 
> Compared to v2, the main change is that option order on the command-line
> is respected.  That is,
> 
> 	--trailer 'acked-by: foo' --where end --trailer 'signed-off-by: me'
> 
> will only apply where=end to the second trailer.  Likewise,
> 
> 	--where end --trailer 'signed-off-by: me' --no-where \
> 	--trailer 'acked-by: foo'
> 
> will only apply it to the first, reverting to trailer.*.where for the
> "acked-by" trailer.

Junio, I see you haven't yet applied this v4 to origin/pu, did you miss it?

Thanks,

Paolo

> Paolo
> 
> v1->v2: support --no-* options, minor code fixes
> 
> v2->v3: largely rewritten to respect option order on the command-line;
> 	keep trailer.h namespace clean (Christian)
> 
> v3->v4: fix compilation warnings (Junio), added documentation fix
> 
> Paolo Bonzini (4):
>   trailers: export action enums and corresponding lookup functions
>   trailers: introduce struct new_trailer_item
>   interpret-trailers: add options for actions
>   interpret-trailers: fix documentation typo
> 
>  Documentation/git-interpret-trailers.txt |  27 ++++++-
>  builtin/interpret-trailers.c             |  73 +++++++++++++++++--
>  t/t7513-interpret-trailers.sh            |  66 +++++++++++++++++
>  trailer.c                                | 118 +++++++++++++++++++------------
>  trailer.h                                |  43 ++++++++++-
>  5 files changed, 274 insertions(+), 53 deletions(-)
> 


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

* Re: [PATCH v4 0/4] interpret-trailers: add --where, --if-exists, --if-missing
  2017-08-14  9:26 ` [PATCH v4 0/4] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
@ 2017-08-14 17:57   ` Junio C Hamano
  0 siblings, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2017-08-14 17:57 UTC (permalink / raw)
  To: Paolo Bonzini; +Cc: git, Christian Couder, Jonathan Tan, Paolo Bonzini

Paolo Bonzini <bonzini@gnu.org> writes:

> On 01/08/2017 11:03, Paolo Bonzini wrote:
>> From: Paolo Bonzini <pbonzini@redhat.com>
>> 
>> These options are useful to experiment with "git interpret-trailers"
>> without having to tinker with .gitconfig (Junio said git should ahve
>> done this first and only added configuration afterwards).  It can
>> be useful in the case where you want a different placement for the trailer,
>> or for scripts/aliases that don't want to rely on specific .gitconfig
>> settings.
>> 
>> Compared to v2, the main change is that option order on the command-line
>> is respected.  That is,
>> 
>> 	--trailer 'acked-by: foo' --where end --trailer 'signed-off-by: me'
>> 
>> will only apply where=end to the second trailer.  Likewise,
>> 
>> 	--where end --trailer 'signed-off-by: me' --no-where \
>> 	--trailer 'acked-by: foo'
>> 
>> will only apply it to the first, reverting to trailer.*.where for the
>> "acked-by" trailer.
>
> Junio, I see you haven't yet applied this v4 to origin/pu, did you miss it?

Thanks for pinging.  Either it was not noticed by mistake or was
deliberately ignored during the pre-release freeze, I do not
remember.

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

end of thread, other threads:[~2017-08-14 17:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-01  9:03 [PATCH v4 0/4] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
2017-08-01  9:03 ` [PATCH v4 1/4] trailers: export action enums and corresponding lookup functions Paolo Bonzini
2017-08-01  9:03 ` [PATCH v4 2/4] trailers: introduce struct new_trailer_item Paolo Bonzini
2017-08-01  9:03 ` [PATCH v4 3/4] interpret-trailers: add options for actions Paolo Bonzini
2017-08-01  9:03 ` [PATCH v4 4/4] interpret-trailers: fix documentation typo Paolo Bonzini
2017-08-14  9:26 ` [PATCH v4 0/4] interpret-trailers: add --where, --if-exists, --if-missing Paolo Bonzini
2017-08-14 17:57   ` Junio C Hamano

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).