threads / patch / 35819

v5, 14 partsAdd interpret-trailers builtin

Subject: [PATCH v5 00/14] Add interpret-trailers builtin

## tl;dr

48 messages between Feb 6, 2014 and Feb 23, 2014. Diffs are folded; open one to read it.

replies: 47people: 3as markdown or json

Christian Couder· Feb 6, 2014, 20:19 UTC · lore
This patch series implements a new command:
        git interpret-trailers

and an infrastructure to process trailers that can be reused, for example in "commit.c".

1) Rationale:

This command should help with RFC 822 style headers, called "trailers", that are found at the end of commit messages.

(Note that these headers do not follow and are not intended to follow many rules that are in RFC 822. For example they do not follow the line breaking rules, the encoding rules and probably many other rules.)

For a long time, these trailers have become a de facto standard way to add helpful information into commit messages.

Until now git commit has only supported the well known "Signed-off-by: " trailer, that is used by many projects like the Linux kernel and Git.

It is better to implement features for these trailers first in a new command rather than in builtin/commit.c, because this way the prepare-commit-msg and commit-msg hooks can reuse this command.

2) Current state:
Currently the usage string of this command is:
git interpret-trailers [--trim-empty] [--infile=<file>] [(<token>[(=|:)<value>])...]
The following features are implemented:
        - the result is printed on stdout
        - the [<token>[=<value>]>] arguments are interpreted
        - a commit message passed using the "--infile=file" option is interpreted
        - if "--infile" is not used, a commit message is read from stdin
        - the "trailer.<token>.key" options in the config are interpreted
        - the "trailer.<token>.where" options are interpreted
        - the "trailer.<token>.ifExist" options are interpreted
        - the "trailer.<token>.ifMissing" options are interpreted
        - the "trailer.<token>.command" config works
        - $ARG can be used in commands
        - ditto for GIT_{AUTHOR,COMMITTER}_{NAME,EMAIL} env variables
        - there are some tests
        - there is some documentation
The following features are planned but not yet implemented:
        - add more tests related to commands
        - add examples in documentation
        - integration with "git commit"
Possible improvements:
        - support GIT_COMMIT_PROTO env variable in commands
3) Changes since version 4, thanks to Eric:
* many small functions became 'static inline' instead of just 'static' 
* alnum_len() has an "int len" parameter instead of "size_t len"
* some redundant comments were removed
* some "const" have been added
* some "switch" replaced some "if ... else if ..."
* some string related functions have been removed from strbuf.{c,h}
* some memory leaks have been fixed (but see below)
* the refactoring patch 11/17 was squashed into a previous patch
* the documentation patch was improved
4) TODO from previous review:
- check for memory leaks
- add test for empty token
Christian Couder (14):
  Add data structures and basic functions for commit trailers
  trailer: process trailers from file and arguments
  trailer: read and process config information
  trailer: process command line trailer arguments
  trailer: parse trailers from input file
  trailer: put all the processing together and print
  trailer: add interpret-trailers command
  trailer: add tests for "git interpret-trailers"
  trailer: if no input file is passed, read from stdin
  trailer: execute command from 'trailer.<name>.command'
  trailer: add tests for trailer command
  trailer: set author and committer env variables
  trailer: add tests for commands using env variables
  Documentation: add documentation for 'git interpret-trailers'
 .gitignore                               |   1 +
 Documentation/git-interpret-trailers.txt | 132 +++++++
 Makefile                                 |   2 +
 builtin.h                                |   1 +
 builtin/interpret-trailers.c             |  36 ++
 git.c                                    |   1 +
 t/t7513-interpret-trailers.sh            | 262 ++++++++++++
 trailer.c                                | 658 +++++++++++++++++++++++++++++++
 trailer.h                                |   6 +
 9 files changed, 1099 insertions(+)
 create mode 100644 Documentation/git-interpret-trailers.txt
 create mode 100644 builtin/interpret-trailers.c
 create mode 100755 t/t7513-interpret-trailers.sh
 create mode 100644 trailer.c
 create mode 100644 trailer.h
-- 
1.8.5.2.206.g98f5689.dirty
Christian Couder· Feb 6, 2014, 20:19 UTC · re: Christian Couder · lore

[PATCH v5 01/14] Add data structures and basic functions for commit trailers

We will use a doubly linked list to store all information about trailers and their configuration.

This way we can easily remove or add trailers to or from trailer lists while traversing the lists in either direction.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 Makefile  |  1 +
 trailer.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
 create mode 100644 trailer.c
Show changes to 2 files +49 −0

Makefile, trailer.c

diff --git a/Makefile b/Makefile
index b4af1e2..ec90feb 100644
--- a/Makefile
+++ b/Makefile
@@ -871,6 +871,7 @@ LIB_OBJS += submodule.o
 LIB_OBJS += symlinks.o
 LIB_OBJS += tag.o
 LIB_OBJS += trace.o
+LIB_OBJS += trailer.o
 LIB_OBJS += transport.o
 LIB_OBJS += transport-helper.o
 LIB_OBJS += tree-diff.o
diff --git a/trailer.c b/trailer.c
new file mode 100644
index 0000000..f129b5a
--- /dev/null
+++ b/trailer.c
@@ -0,0 +1,48 @@
+#include "cache.h"
+/*
+ * Copyright (c) 2013 Christian Couder <chriscool@tuxfamily.org>
+ */
+
+enum action_where { WHERE_AFTER, WHERE_BEFORE };
+enum action_if_exist { EXIST_ADD_IF_DIFFERENT, EXIST_ADD_IF_DIFFERENT_NEIGHBOR,
+		       EXIST_ADD, EXIST_OVERWRITE, EXIST_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_exist if_exist;
+	enum action_if_missing if_missing;
+};
+
+struct trailer_item {
+	struct trailer_item *previous;
+	struct trailer_item *next;
+	const char *token;
+	const char *value;
+	struct conf_info *conf;
+};
+
+static inline int same_token(struct trailer_item *a, struct trailer_item *b, int alnum_len)
+{
+	return !strncasecmp(a->token, b->token, alnum_len);
+}
+
+static inline int same_value(struct trailer_item *a, struct trailer_item *b)
+{
+	return !strcasecmp(a->value, b->value);
+}
+
+static inline int same_trailer(struct trailer_item *a, struct trailer_item *b, int alnum_len)
+{
+	return same_token(a, b, alnum_len) && same_value(a, b);
+}
+
+/* Get the length of buf from its beginning until its last alphanumeric character */
+static inline size_t alnum_len(const char *buf, int len)
+{
+	while (--len >= 0 && !isalnum(buf[len]));
+	return (size_t) len + 1;
+}
-- 
1.8.5.2.206.g98f5689.dirty
Junio C Hamano· Feb 6, 2014, 23:44 UTC · re: Christian Couder · lore

Re: [PATCH v5 01/14] Add data structures and basic functions for commit trailers

Christian Couder <chriscool@tuxfamily.org> writes:
Show 40 quoted lines
> We will use a doubly linked list to store all information
> about trailers and their configuration.
>
> This way we can easily remove or add trailers to or from
> trailer lists while traversing the lists in either direction.
>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  Makefile  |  1 +
>  trailer.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 49 insertions(+)
>  create mode 100644 trailer.c
>
> diff --git a/Makefile b/Makefile
> index b4af1e2..ec90feb 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -871,6 +871,7 @@ LIB_OBJS += submodule.o
>  LIB_OBJS += symlinks.o
>  LIB_OBJS += tag.o
>  LIB_OBJS += trace.o
> +LIB_OBJS += trailer.o
>  LIB_OBJS += transport.o
>  LIB_OBJS += transport-helper.o
>  LIB_OBJS += tree-diff.o
> diff --git a/trailer.c b/trailer.c
> new file mode 100644
> index 0000000..f129b5a
> --- /dev/null
> +++ b/trailer.c
> @@ -0,0 +1,48 @@
> +#include "cache.h"
> +/*
> + * Copyright (c) 2013 Christian Couder <chriscool@tuxfamily.org>
> + */
> +
> +enum action_where { WHERE_AFTER, WHERE_BEFORE };
> +enum action_if_exist { EXIST_ADD_IF_DIFFERENT, EXIST_ADD_IF_DIFFERENT_NEIGHBOR,
> +		       EXIST_ADD, EXIST_OVERWRITE, EXIST_DO_NOTHING };
> +enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };

All these names and "conf_info" below are not named to be specific to this little tool. Can I assume that these will never be exposed to the rest of the system? If so, they are fine.

Show 7 quoted lines
> +struct conf_info {
> +	char *name;
> +	char *key;
> +	char *command;
> +	enum action_where where;
> +	enum action_if_exist if_exist;
> +	enum action_if_missing if_missing;

It still feels somewhat strange. It is true that an item can be either "exist" or "missing" and it is understandable that it tempts you to split that into two, but EXIST_OVERWRITE will not trigger either WHERE_AFTER or WHERE_BEFORE action.

Show 24 quoted lines
> +};
> +
> +struct trailer_item {
> +	struct trailer_item *previous;
> +	struct trailer_item *next;
> +	const char *token;
> +	const char *value;
> +	struct conf_info *conf;
> +};
> +
> +static inline int same_token(struct trailer_item *a, struct trailer_item *b, int alnum_len)
> +{
> +	return !strncasecmp(a->token, b->token, alnum_len);
> +}
> +
> +static inline int same_value(struct trailer_item *a, struct trailer_item *b)
> +{
> +	return !strcasecmp(a->value, b->value);
> +}
> +
> +static inline int same_trailer(struct trailer_item *a, struct trailer_item *b, int alnum_len)
> +{
> +	return same_token(a, b, alnum_len) && same_value(a, b);
> +}

All these "inlines" look premature optimization that can be delegated to any decent compiler, don't they?

> +/* Get the length of buf from its beginning until its last alphanumeric character */
> +static inline size_t alnum_len(const char *buf, int len)
> +{
> +	while (--len >= 0 && !isalnum(buf[len]));
Style:
	while (--len >= 0 && !isalnum(buf[len]))
        	;

You may add a comment on the empty statement to make it stand out even more, i.e.

		; /* nothing */
> +	return (size_t) len + 1;

This is somewhat unfortunate. if the caller wants to receive size_t, perhaps it should be passing in size_t (or ssize_t) to the function? Hard to guess without an actual caller, though.

> +}
Christian Couder· Feb 9, 2014, 13:48 UTC · re: Junio C Hamano · lore

Re: [PATCH v5 01/14] Add data structures and basic functions for commit trailers

From: Junio C Hamano <gitster@pobox.com>
Show 11 quoted lines
>
> Christian Couder <chriscool@tuxfamily.org> writes:
> 
>> +enum action_where { WHERE_AFTER, WHERE_BEFORE };
>> +enum action_if_exist { EXIST_ADD_IF_DIFFERENT, EXIST_ADD_IF_DIFFERENT_NEIGHBOR,
>> +		       EXIST_ADD, EXIST_OVERWRITE, EXIST_DO_NOTHING };
>> +enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };
> 
> All these names and "conf_info" below are not named to be specific
> to this little tool.  Can I assume that these will never be exposed
> to the rest of the system?  If so, they are fine.
Yeah, I don't plan them to be exposed to other files.
 
Show 12 quoted lines
>> +struct conf_info {
>> +	char *name;
>> +	char *key;
>> +	char *command;
>> +	enum action_where where;
>> +	enum action_if_exist if_exist;
>> +	enum action_if_missing if_missing;
> 
> It still feels somewhat strange.  It is true that an item can be
> either "exist" or "missing" and it is understandable that it tempts
> you to split that into two, but EXIST_OVERWRITE will not trigger
> either WHERE_AFTER or WHERE_BEFORE action.

Yeah, it's true that WHERE_AFTER/WHERE_BEFORE does not make sense for EXIST_OVERWRITE, EXIST_DO_NOTHING and MISSING_DO_NOTHING, but it's a fact of life that sometimes some options do not make sense with others.

Show 17 quoted lines
>> +static inline int same_token(struct trailer_item *a, struct trailer_item *b, int alnum_len)
>> +{
>> +	return !strncasecmp(a->token, b->token, alnum_len);
>> +}
>> +
>> +static inline int same_value(struct trailer_item *a, struct trailer_item *b)
>> +{
>> +	return !strcasecmp(a->value, b->value);
>> +}
>> +
>> +static inline int same_trailer(struct trailer_item *a, struct trailer_item *b, int alnum_len)
>> +{
>> +	return same_token(a, b, alnum_len) && same_value(a, b);
>> +}
> 
> All these "inlines" look premature optimization that can be
> delegated to any decent compiler, don't they?

Yeah, but as Eric suggested to add them like in header files and you did not reply to him, I thought you agreed with him. I will remove them.

Show 14 quoted lines
>> +/* Get the length of buf from its beginning until its last alphanumeric character */
>> +static inline size_t alnum_len(const char *buf, int len)
>> +{
>> +	while (--len >= 0 && !isalnum(buf[len]));
> 
> Style:
> 
> 	while (--len >= 0 && !isalnum(buf[len]))
>         	;
> 
> You may add a comment on the empty statement to make it stand out
> even more, i.e.
> 
> 		; /* nothing */
Ok, I will do that.
Show 5 quoted lines
>> +	return (size_t) len + 1;
> 
> This is somewhat unfortunate.  if the caller wants to receive
> size_t, perhaps it should be passing in size_t (or ssize_t) to the
> function?  Hard to guess without an actual caller, though.
Ok, I will make it return an int.

Thanks, Christian.

Eric Sunshine· Feb 10, 2014, 07:27 UTC · re: Christian Couder · lore

Re: [PATCH v5 01/14] Add data structures and basic functions for commit trailers

On Sun, Feb 9, 2014 at 8:48 AM, Christian Couder <chriscool@tuxfamily.org> wrote:

Show 15 quoted lines
> From: Junio C Hamano <gitster@pobox.com>
>>
>> Christian Couder <chriscool@tuxfamily.org> writes:
>>
>>> +static inline int same_trailer(struct trailer_item *a, struct trailer_item *b, int alnum_len)
>>> +{
>>> +    return same_token(a, b, alnum_len) && same_value(a, b);
>>> +}
>>
>> All these "inlines" look premature optimization that can be
>> delegated to any decent compiler, don't they?
>
> Yeah, but as Eric suggested to add them like in header files and you
> did not reply to him, I thought you agreed with him.
> I will remove them.

If these functions are used only by trailer.c, then it would make sense to move them from trailer.h to trailer.c so that they don't get defined unnecessarily by each .c file which includes trailer.h.

Show 11 quoted lines
>>> +/* Get the length of buf from its beginning until its last alphanumeric character */
>>> +static inline size_t alnum_len(const char *buf, int len)
>>> +{
>>> +    while (--len >= 0 && !isalnum(buf[len]));
>>> +    return (size_t) len + 1;
>>
>> This is somewhat unfortunate.  if the caller wants to receive
>> size_t, perhaps it should be passing in size_t (or ssize_t) to the
>> function?  Hard to guess without an actual caller, though.
>
> Ok, I will make it return an int.

Why not adjust the loop condition slightly instead so that you can continue to accept and return size_t?

Junio C Hamano· Feb 6, 2014, 23:46 UTC · re: Christian Couder · lore

Re: [PATCH v5 01/14] Add data structures and basic functions for commit trailers

Christian Couder <chriscool@tuxfamily.org> writes:
> +	enum action_if_exist if_exist;
> +	enum action_if_missing if_missing;
Probably "if_exists" is more gramatically correct.
	if (x->if_exists) {
        	... do this ...
	}
would read well, but not "x->if_exist".                
Christian Couder· Feb 9, 2014, 13:51 UTC · re: Junio C Hamano · lore

Re: [PATCH v5 01/14] Add data structures and basic functions for commit trailers

From: Junio C Hamano <gitster@pobox.com>
Show 13 quoted lines
>
> Christian Couder <chriscool@tuxfamily.org> writes:
> 
>> +	enum action_if_exist if_exist;
>> +	enum action_if_missing if_missing;
> 
> Probably "if_exists" is more gramatically correct.
> 
> 	if (x->if_exists) {
>         	... do this ...
> 	}
> 
> would read well, but not "x->if_exist".                
Ok, I will use "if_exists" instead of "if_exist" and also:
enum action_if_exists { EXISTS_ADD_IF_DIFFERENT, EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,
			EXISTS_ADD, EXISTS_OVERWRITE, EXISTS_DO_NOTHING };
instead of:
enum action_if_exist { EXIST_ADD_IF_DIFFERENT, EXIST_ADD_IF_DIFFERENT_NEIGHBOR,
			EXIST_ADD, EXIST_OVERWRITE, EXIST_DO_NOTHING };
to be consistent.

Thanks, Christian.

Christian Couder· Feb 6, 2014, 20:19 UTC · re: Christian Couder · lore

[PATCH v5 02/14] trailer: process trailers from file and arguments

This patch implements the logic that process trailers from file and arguments.

At the beginning trailers from file are in their own infile_tok doubly linked list, and trailers from arguments are in their own arg_tok doubly linked list.

The lists are traversed and when an arg_tok should be "applied", it is removed from its list and inserted into the infile_tok list.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 trailer.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 187 insertions(+)
Show changes to trailer.c +187 −0
diff --git a/trailer.c b/trailer.c
index f129b5a..ba0cfe0 100644
--- a/trailer.c
+++ b/trailer.c
@@ -46,3 +46,190 @@ static inline size_t alnum_len(const char *buf, int len)
 	while (--len >= 0 && !isalnum(buf[len]));
 	return (size_t) len + 1;
 }
+
+static void add_arg_to_infile(struct trailer_item *infile_tok,
+			      struct trailer_item *arg_tok)
+{
+	if (arg_tok->conf->where == WHERE_AFTER) {
+		arg_tok->next = infile_tok->next;
+		infile_tok->next = arg_tok;
+		arg_tok->previous = infile_tok;
+		if (arg_tok->next)
+			arg_tok->next->previous = arg_tok;
+	} else {
+		arg_tok->previous = infile_tok->previous;
+		infile_tok->previous = arg_tok;
+		arg_tok->next = infile_tok;
+		if (arg_tok->previous)
+			arg_tok->previous->next = arg_tok;
+	}
+}
+
+static int check_if_different(struct trailer_item *infile_tok,
+			      struct trailer_item *arg_tok,
+			      int alnum_len, int check_all)
+{
+	enum action_where where = arg_tok->conf->where;
+	do {
+		if (!infile_tok)
+			return 1;
+		if (same_trailer(infile_tok, arg_tok, alnum_len))
+			return 0;
+		/*
+		 * if we want to add a trailer after another one,
+		 * we have to check those before this one
+		 */
+		infile_tok = (where == WHERE_AFTER) ? infile_tok->previous : infile_tok->next;
+	} while (check_all);
+	return 1;
+}
+
+static void apply_arg_if_exist(struct trailer_item *infile_tok,
+			       struct trailer_item *arg_tok,
+			       int alnum_len)
+{
+	switch (arg_tok->conf->if_exist) {
+	case EXIST_DO_NOTHING:
+		free(arg_tok);
+		break;
+	case EXIST_OVERWRITE:
+		free((char *)infile_tok->value);
+		infile_tok->value = xstrdup(arg_tok->value);
+		free(arg_tok);
+		break;
+	case EXIST_ADD:
+		add_arg_to_infile(infile_tok, arg_tok);
+		break;
+	case EXIST_ADD_IF_DIFFERENT:
+		if (check_if_different(infile_tok, arg_tok, alnum_len, 1))
+			add_arg_to_infile(infile_tok, arg_tok);
+		else
+			free(arg_tok);
+		break;
+	case EXIST_ADD_IF_DIFFERENT_NEIGHBOR:
+		if (check_if_different(infile_tok, arg_tok, alnum_len, 0))
+			add_arg_to_infile(infile_tok, arg_tok);
+		else
+			free(arg_tok);
+		break;
+	}
+}
+
+static void remove_from_list(struct trailer_item *item,
+			     struct trailer_item **first)
+{
+	if (item->next)
+		item->next->previous = item->previous;
+	if (item->previous)
+		item->previous->next = item->next;
+	else
+		*first = item->next;
+}
+
+static struct trailer_item *remove_first(struct trailer_item **first)
+{
+	struct trailer_item *item = *first;
+	*first = item->next;
+	if (item->next) {
+		item->next->previous = NULL;
+		item->next = NULL;
+	}
+	return item;
+}
+
+static void process_infile_tok(struct trailer_item *infile_tok,
+			       struct trailer_item **arg_tok_first,
+			       enum action_where where)
+{
+	struct trailer_item *arg_tok;
+	struct trailer_item *next_arg;
+
+	int tok_alnum_len = alnum_len(infile_tok->token, strlen(infile_tok->token));
+	for (arg_tok = *arg_tok_first; arg_tok; arg_tok = next_arg) {
+		next_arg = arg_tok->next;
+		if (same_token(infile_tok, arg_tok, tok_alnum_len) &&
+		    arg_tok->conf->where == where) {
+			remove_from_list(arg_tok, arg_tok_first);
+			apply_arg_if_exist(infile_tok, arg_tok, tok_alnum_len);
+			/*
+			 * If arg has been added to infile,
+			 * then we need to process it too now.
+			 */
+			if ((where == WHERE_AFTER ? infile_tok->next : infile_tok->previous) == arg_tok)
+				infile_tok = arg_tok;
+		}
+	}
+}
+
+static void update_last(struct trailer_item **last)
+{
+	if (*last)
+		while((*last)->next != NULL)
+			*last = (*last)->next;
+}
+
+static void update_first(struct trailer_item **first)
+{
+	if (*first)
+		while((*first)->previous != NULL)
+			*first = (*first)->previous;
+}
+
+static void apply_arg_if_missing(struct trailer_item **infile_tok_first,
+				 struct trailer_item **infile_tok_last,
+				 struct trailer_item *arg_tok)
+{
+	struct trailer_item **infile_tok;
+	enum action_where where;
+
+	switch (arg_tok->conf->if_missing) {
+	case MISSING_DO_NOTHING:
+		free(arg_tok);
+		break;
+	case MISSING_ADD:
+		where = arg_tok->conf->where;
+		infile_tok = (where == WHERE_AFTER) ? infile_tok_last : infile_tok_first;
+		if (*infile_tok) {
+			add_arg_to_infile(*infile_tok, arg_tok);
+			*infile_tok = arg_tok;
+		} else {
+			*infile_tok_first = arg_tok;
+			*infile_tok_last = arg_tok;
+		}
+		break;
+	}
+}
+
+static void process_trailers_lists(struct trailer_item **infile_tok_first,
+				   struct trailer_item **infile_tok_last,
+				   struct trailer_item **arg_tok_first)
+{
+	struct trailer_item *infile_tok;
+	struct trailer_item *arg_tok;
+
+	if (!*arg_tok_first)
+		return;
+
+	/* Process infile from end to start */
+	for (infile_tok = *infile_tok_last; infile_tok; infile_tok = infile_tok->previous) {
+		process_infile_tok(infile_tok, arg_tok_first, WHERE_AFTER);
+	}
+
+	update_last(infile_tok_last);
+
+	if (!*arg_tok_first)
+		return;
+
+	/* Process infile from start to end */
+	for (infile_tok = *infile_tok_first; infile_tok; infile_tok = infile_tok->next) {
+		process_infile_tok(infile_tok, arg_tok_first, WHERE_BEFORE);
+	}
+
+	update_first(infile_tok_first);
+
+	/* Process args left */
+	while (*arg_tok_first) {
+		arg_tok = remove_first(arg_tok_first);
+		apply_arg_if_missing(infile_tok_first, infile_tok_last, arg_tok);
+	}
+}
-- 
1.8.5.2.206.g98f5689.dirty
Junio C Hamano· Feb 7, 2014, 00:03 UTC · re: Christian Couder · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

Christian Couder <chriscool@tuxfamily.org> writes:
Show 90 quoted lines
> This patch implements the logic that process trailers
> from file and arguments.
>
> At the beginning trailers from file are in their own
> infile_tok doubly linked list, and trailers from
> arguments are in their own arg_tok doubly linked list.
>
> The lists are traversed and when an arg_tok should be
> "applied", it is removed from its list and inserted
> into the infile_tok list.
>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  trailer.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 187 insertions(+)
>
> diff --git a/trailer.c b/trailer.c
> index f129b5a..ba0cfe0 100644
> --- a/trailer.c
> +++ b/trailer.c
> @@ -46,3 +46,190 @@ static inline size_t alnum_len(const char *buf, int len)
>  	while (--len >= 0 && !isalnum(buf[len]));
>  	return (size_t) len + 1;
>  }
> +
> +static void add_arg_to_infile(struct trailer_item *infile_tok,
> +			      struct trailer_item *arg_tok)
> +{
> +	if (arg_tok->conf->where == WHERE_AFTER) {
> +		arg_tok->next = infile_tok->next;
> +		infile_tok->next = arg_tok;
> +		arg_tok->previous = infile_tok;
> +		if (arg_tok->next)
> +			arg_tok->next->previous = arg_tok;
> +	} else {
> +		arg_tok->previous = infile_tok->previous;
> +		infile_tok->previous = arg_tok;
> +		arg_tok->next = infile_tok;
> +		if (arg_tok->previous)
> +			arg_tok->previous->next = arg_tok;
> +	}
> +}
> +
> +static int check_if_different(struct trailer_item *infile_tok,
> +			      struct trailer_item *arg_tok,
> +			      int alnum_len, int check_all)
> +{
> +	enum action_where where = arg_tok->conf->where;
> +	do {
> +		if (!infile_tok)
> +			return 1;
> +		if (same_trailer(infile_tok, arg_tok, alnum_len))
> +			return 0;
> +		/*
> +		 * if we want to add a trailer after another one,
> +		 * we have to check those before this one
> +		 */
> +		infile_tok = (where == WHERE_AFTER) ? infile_tok->previous : infile_tok->next;
> +	} while (check_all);
> +	return 1;
> +}
> +
> +static void apply_arg_if_exist(struct trailer_item *infile_tok,
> +			       struct trailer_item *arg_tok,
> +			       int alnum_len)
> +{
> +	switch (arg_tok->conf->if_exist) {
> +	case EXIST_DO_NOTHING:
> +		free(arg_tok);
> +		break;
> +	case EXIST_OVERWRITE:
> +		free((char *)infile_tok->value);
> +		infile_tok->value = xstrdup(arg_tok->value);
> +		free(arg_tok);
> +		break;
> +	case EXIST_ADD:
> +		add_arg_to_infile(infile_tok, arg_tok);
> +		break;
> +	case EXIST_ADD_IF_DIFFERENT:
> +		if (check_if_different(infile_tok, arg_tok, alnum_len, 1))
> +			add_arg_to_infile(infile_tok, arg_tok);
> +		else
> +			free(arg_tok);
> +		break;
> +	case EXIST_ADD_IF_DIFFERENT_NEIGHBOR:
> +		if (check_if_different(infile_tok, arg_tok, alnum_len, 0))
> +			add_arg_to_infile(infile_tok, arg_tok);
> +		else
> +			free(arg_tok);
> +		break;

Makes me wonder if people want a rule to say "if the same key already exists, regardless of the value".

Show 13 quoted lines
> +	}
> +}
> +
> +static void remove_from_list(struct trailer_item *item,
> +			     struct trailer_item **first)
> +{
> +	if (item->next)
> +		item->next->previous = item->previous;
> +	if (item->previous)
> +		item->previous->next = item->next;
> +	else
> +		*first = item->next;
> +}
Will callers free the item that now is not on the list?
Show 53 quoted lines
> +static struct trailer_item *remove_first(struct trailer_item **first)
> +{
> +	struct trailer_item *item = *first;
> +	*first = item->next;
> +	if (item->next) {
> +		item->next->previous = NULL;
> +		item->next = NULL;
> +	}
> +	return item;
> +}
> +
> +static void process_infile_tok(struct trailer_item *infile_tok,
> +			       struct trailer_item **arg_tok_first,
> +			       enum action_where where)
> +{
> +	struct trailer_item *arg_tok;
> +	struct trailer_item *next_arg;
> +
> +	int tok_alnum_len = alnum_len(infile_tok->token, strlen(infile_tok->token));
> +	for (arg_tok = *arg_tok_first; arg_tok; arg_tok = next_arg) {
> +		next_arg = arg_tok->next;
> +		if (same_token(infile_tok, arg_tok, tok_alnum_len) &&
> +		    arg_tok->conf->where == where) {
> +			remove_from_list(arg_tok, arg_tok_first);
> +			apply_arg_if_exist(infile_tok, arg_tok, tok_alnum_len);
> +			/*
> +			 * If arg has been added to infile,
> +			 * then we need to process it too now.
> +			 */
> +			if ((where == WHERE_AFTER ? infile_tok->next : infile_tok->previous) == arg_tok)
> +				infile_tok = arg_tok;
> +		}
> +	}
> +}
> +
> +static void update_last(struct trailer_item **last)
> +{
> +	if (*last)
> +		while((*last)->next != NULL)
> +			*last = (*last)->next;
> +}
> +
> +static void update_first(struct trailer_item **first)
> +{
> +	if (*first)
> +		while((*first)->previous != NULL)
> +			*first = (*first)->previous;
> +}
> +
> +static void apply_arg_if_missing(struct trailer_item **infile_tok_first,
> +				 struct trailer_item **infile_tok_last,
> +				 struct trailer_item *arg_tok)
> +{

Makes me wonder if it would make the code simpler to keep an anchor item "struct trailer_item" that is off heap, and pass that single anchor item around, using its next/prev fields as the first and the last. Wouldn't it let you remove the special cases for the first and last item?

Show 18 quoted lines
> +	struct trailer_item **infile_tok;
> +	enum action_where where;
> +
> +	switch (arg_tok->conf->if_missing) {
> +	case MISSING_DO_NOTHING:
> +		free(arg_tok);
> +		break;
> +	case MISSING_ADD:
> +		where = arg_tok->conf->where;
> +		infile_tok = (where == WHERE_AFTER) ? infile_tok_last : infile_tok_first;
> +		if (*infile_tok) {
> +			add_arg_to_infile(*infile_tok, arg_tok);
> +			*infile_tok = arg_tok;
> +		} else {
> +			*infile_tok_first = arg_tok;
> +			*infile_tok_last = arg_tok;
> +		}
> +		break;

This piece makes me wonder if "after" is a good name. prepend and append, perhaps?

Christian Couder· Feb 9, 2014, 13:52 UTC · re: Junio C Hamano · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

From: Junio C Hamano <gitster@pobox.com>
Show 34 quoted lines
>
> Christian Couder <chriscool@tuxfamily.org> writes:
> 
>> +static void apply_arg_if_exist(struct trailer_item *infile_tok,
>> +			       struct trailer_item *arg_tok,
>> +			       int alnum_len)
>> +{
>> +	switch (arg_tok->conf->if_exist) {
>> +	case EXIST_DO_NOTHING:
>> +		free(arg_tok);
>> +		break;
>> +	case EXIST_OVERWRITE:
>> +		free((char *)infile_tok->value);
>> +		infile_tok->value = xstrdup(arg_tok->value);
>> +		free(arg_tok);
>> +		break;
>> +	case EXIST_ADD:
>> +		add_arg_to_infile(infile_tok, arg_tok);
>> +		break;
>> +	case EXIST_ADD_IF_DIFFERENT:
>> +		if (check_if_different(infile_tok, arg_tok, alnum_len, 1))
>> +			add_arg_to_infile(infile_tok, arg_tok);
>> +		else
>> +			free(arg_tok);
>> +		break;
>> +	case EXIST_ADD_IF_DIFFERENT_NEIGHBOR:
>> +		if (check_if_different(infile_tok, arg_tok, alnum_len, 0))
>> +			add_arg_to_infile(infile_tok, arg_tok);
>> +		else
>> +			free(arg_tok);
>> +		break;
> 
> Makes me wonder if people want a rule to say "if the same key
> already exists, regardless of the value".
This is what "if_exists" and "if_missing" are all about.
Either:
	the same key already exists regardless of the value

and, in this case, what happens depends on what has been specified using the "if_exists" configuration variable.

Or:
	the same key DOES NOT already exists regardless of the value

and in this case, what happens depends on what has been specified using the "if_missing" configuration variable.

Show 12 quoted lines
>> +static void remove_from_list(struct trailer_item *item,
>> +			     struct trailer_item **first)
>> +{
>> +	if (item->next)
>> +		item->next->previous = item->previous;
>> +	if (item->previous)
>> +		item->previous->next = item->next;
>> +	else
>> +		*first = item->next;
>> +}
> 
> Will callers free the item that now is not on the list?
Yes, or the item will be inserted into another list.
Show 59 quoted lines
>> +static struct trailer_item *remove_first(struct trailer_item **first)
>> +{
>> +	struct trailer_item *item = *first;
>> +	*first = item->next;
>> +	if (item->next) {
>> +		item->next->previous = NULL;
>> +		item->next = NULL;
>> +	}
>> +	return item;
>> +}
>> +
>> +static void process_infile_tok(struct trailer_item *infile_tok,
>> +			       struct trailer_item **arg_tok_first,
>> +			       enum action_where where)
>> +{
>> +	struct trailer_item *arg_tok;
>> +	struct trailer_item *next_arg;
>> +
>> +	int tok_alnum_len = alnum_len(infile_tok->token, strlen(infile_tok->token));
>> +	for (arg_tok = *arg_tok_first; arg_tok; arg_tok = next_arg) {
>> +		next_arg = arg_tok->next;
>> +		if (same_token(infile_tok, arg_tok, tok_alnum_len) &&
>> +		    arg_tok->conf->where == where) {
>> +			remove_from_list(arg_tok, arg_tok_first);
>> +			apply_arg_if_exist(infile_tok, arg_tok, tok_alnum_len);
>> +			/*
>> +			 * If arg has been added to infile,
>> +			 * then we need to process it too now.
>> +			 */
>> +			if ((where == WHERE_AFTER ? infile_tok->next : infile_tok->previous) == arg_tok)
>> +				infile_tok = arg_tok;
>> +		}
>> +	}
>> +}
>> +
>> +static void update_last(struct trailer_item **last)
>> +{
>> +	if (*last)
>> +		while((*last)->next != NULL)
>> +			*last = (*last)->next;
>> +}
>> +
>> +static void update_first(struct trailer_item **first)
>> +{
>> +	if (*first)
>> +		while((*first)->previous != NULL)
>> +			*first = (*first)->previous;
>> +}
>> +
>> +static void apply_arg_if_missing(struct trailer_item **infile_tok_first,
>> +				 struct trailer_item **infile_tok_last,
>> +				 struct trailer_item *arg_tok)
>> +{
> 
> Makes me wonder if it would make the code simpler to keep an anchor
> item "struct trailer_item" that is off heap, and pass that single
> anchor item around, using its next/prev fields as the first and the
> last.  Wouldn't it let you remove the special cases for the first
> and last item?

Yeah, that could work. On the other hand the other fields of this special item would not be used for anything. I will have a look at it.

Show 21 quoted lines
>> +	struct trailer_item **infile_tok;
>> +	enum action_where where;
>> +
>> +	switch (arg_tok->conf->if_missing) {
>> +	case MISSING_DO_NOTHING:
>> +		free(arg_tok);
>> +		break;
>> +	case MISSING_ADD:
>> +		where = arg_tok->conf->where;
>> +		infile_tok = (where == WHERE_AFTER) ? infile_tok_last : infile_tok_first;
>> +		if (*infile_tok) {
>> +			add_arg_to_infile(*infile_tok, arg_tok);
>> +			*infile_tok = arg_tok;
>> +		} else {
>> +			*infile_tok_first = arg_tok;
>> +			*infile_tok_last = arg_tok;
>> +		}
>> +		break;
> 
> This piece makes me wonder if "after" is a good name.  prepend and
> append, perhaps?

The problem is that "prepend" and "append" could be confusing when related to EXISTS_DO_NOTHING, MISSING_DO_NOTHING and EXISTS_OVERWRITE.

Also WHERE_MIDDLE and WHERE_SORTED could perhaps be added later in the same enum as WHERE_AFTER and WHERE_BEFORE, and in this case, we would need to find names for those that are like "prepend" and "append", but that are also difficult to confuse with the EXISTS_XXX and MISSING_XXX names.

Thanks, Christian.

Junio C Hamano· Feb 10, 2014, 18:14 UTC · re: Christian Couder · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

Christian Couder <chriscool@tuxfamily.org> writes:
Show 15 quoted lines
> This is what "if_exists" and "if_missing" are all about.
>
> Either:
>
> 	the same key already exists regardless of the value
>
> and, in this case, what happens depends on what has been specified using
> the "if_exists" configuration variable.
>
> Or:
>
> 	the same key DOES NOT already exists regardless of the value
>
> and in this case, what happens depends on what has been specified
> using the "if_missing" configuration variable.

Hmm, how should things like signed-off-by be handled? You may want to allow many entries with the same key with distinct values, but duplicated values may want to be handled differently (currently we only avoid to place a duplicate <key, value> consecutively, but keys with different semantics (e.g. "fixes-bug: #bugid") may want to say "unless the same key with the same value exists, append it at the end".

Christian Couder· Feb 10, 2014, 19:59 UTC · re: Junio C Hamano · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

From: Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH v5 02/14] trailer: process trailers from file and arguments
Date: Mon, 10 Feb 2014 10:14:34 -0800
Show 25 quoted lines
> Christian Couder <chriscool@tuxfamily.org> writes:
> 
>> This is what "if_exists" and "if_missing" are all about.
>>
>> Either:
>>
>> 	the same key already exists regardless of the value
>>
>> and, in this case, what happens depends on what has been specified using
>> the "if_exists" configuration variable.
>>
>> Or:
>>
>> 	the same key DOES NOT already exists regardless of the value
>>
>> and in this case, what happens depends on what has been specified
>> using the "if_missing" configuration variable.
> 
> Hmm, how should things like signed-off-by be handled?  You may want
> to allow many entries with the same key with distinct values, but
> duplicated values may want to be handled differently (currently we
> only avoid to place a duplicate <key, value> consecutively, but keys
> with different semantics (e.g. "fixes-bug: #bugid") may want to say
> "unless the same key with the same value exists, append it at the
> end".

Many entries with the same key but distinct values can be configured using:

if_exists = add_if_different if_missing = add

Many entries with the same key but values that can be the same can be configured using:

if_exists = add if_missing = add

The place where the trailers are added, if they should be added, can be selected using either "where = after", the default, or "where = before".

"where = after" means just after trailers with the same key, or, if there are no trailers with the same key, after all the existing trailers.

"where = before" means just before trailers with the same key, or, if there are no trailers with the same key, before all the existing trailers.

I think that this is enough to handle most of the usual cases, but we can add other more complex options later, as I said in the last message of this thread:

http://thread.gmane.org/gmane.comp.version-control.git/237278/

Thanks, Christian.

Junio C Hamano· Feb 10, 2014, 20:51 UTC · re: Christian Couder · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

Christian Couder <chriscool@tuxfamily.org> writes:
Show 11 quoted lines
> Many entries with the same key but distinct values can be configured
> using:
>
> if_exists = add_if_different
> if_missing = add
>
> Many entries with the same key but values that can be the same can be
> configured using:
>
> if_exists = add
> if_missing = add

While the above certainly can express the combinations, I am still puzzled about the value of having "under this condition" (i.e. if-exists/if-missing) and "do this" (i.e. add/add-if-different) as two separate concepts.

If you do not have an existing entry with the same key, no new entry can be the same as an existing one---therefore, the former "allow only distinct values for the same key" can just say

  trailer."Fixes".action = add_if_different

or something, without any if_exists/if_missing. In a similar way, the latter "duplicated values allowed for the same key" can say

  trailer."Sob".action = add

You can throw into the mix other actions like "add_if_missing", you can also express "only one value allowed for the same key---the first one wins", "replace" to mean "replace if there is one with the same key", and "replace_or_add" to mean "same as 'replace', but add if there is no existing entries with the same key". Will we lose expressiveness by simplifying the semantics, getting rid of this "under this condition" part and instead making "do this" part more intelligent?

Even if we assume, for the sake of discussion, that it *is* a good idea to separate "under this condition" part and "do this" part, I do not think the above is the only or the best way to express "distinct values allowed for the same key". How do we argue that this from your example

  if_exists = add_if_different
  if_missing = add
is a better design than this, for example?
  if_key_value_exists = ignore ; exact matching <key,val> exists
  if_key_exists = add          ; otherwise
  if_missing = add

The latter splits remaining conditional logic from "do this" part (no more "add_if_different" that causes "add" action to see if there is the same key and act differently) and moves it to "under this condition" part.

I am trying to illustrate that the line to split the "under this condition" and "do this" looks arbitrary and fuzzy here, and because of this arbitrary-ness and fuzziness, it is not very obvious to me why it is a good idea to have "under this condition" as a separate concept from "do this" settings.

What is the advantage of having such an extra axis? Does it make it easier to manage? Does it allow us to express more?

I can see that the location where a new entry (or a duplicated one) is added (i.e. do we prepend? do we append?) can be orthogonal to any of the above; no need to bring up 'where' in the discussion.

Christian Couder· Feb 11, 2014, 15:02 UTC · re: Junio C Hamano · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

On Mon, Feb 10, 2014 at 9:51 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 18 quoted lines
> Christian Couder <chriscool@tuxfamily.org> writes:
>
>> Many entries with the same key but distinct values can be configured
>> using:
>>
>> if_exists = add_if_different
>> if_missing = add
>>
>> Many entries with the same key but values that can be the same can be
>> configured using:
>>
>> if_exists = add
>> if_missing = add
>
> While the above certainly can express the combinations, I am still
> puzzled about the value of having "under this condition" (i.e.
> if-exists/if-missing) and "do this" (i.e. add/add-if-different) as
> two separate concepts.

It is because there are many possible conditions under which the user might want to do different things, and the syntax of our configuration files is not a programming language, so it is not well suited to nicely express what the user might want under different conditions.

That's what I tried to show in my last message in the thread I sent the link to in my previous message.

(http://thread.gmane.org/gmane.comp.version-control.git/237278/)

So we have to choose a few conditions to avoid possible combinatorial explosion in the number of possible values.

Show 19 quoted lines
> If you do not have an existing entry with the same key, no new entry
> can be the same as an existing one---therefore, the former "allow
> only distinct values for the same key" can just say
>
>   trailer."Fixes".action = add_if_different
>
> or something, without any if_exists/if_missing.  In a similar way,
> the latter "duplicated values allowed for the same key" can say
>
>   trailer."Sob".action = add
>
> You can throw into the mix other actions like "add_if_missing", you
> can also express "only one value allowed for the same key---the
> first one wins", "replace" to mean "replace if there is one with the
> same key", and "replace_or_add" to mean "same as 'replace', but add
> if there is no existing entries with the same key".  Will we lose
> expressiveness by simplifying the semantics, getting rid of this
> "under this condition" part and instead making "do this" part more
> intelligent?

I don't think we will lose expressiveness, but I think there might be a combinatorial explosion in the number of choices.

Right now there are 2 choices for "if_missing" and 5 choices for "if_exists". This means that if we use only one "action" config variable and want to have the same expressiveness, we need 10 choices.

It doesn't seem a big difference now, but, if we add more choices, then the difference will increase a lot.

Show 14 quoted lines
> Even if we assume, for the sake of discussion, that it *is* a good
> idea to separate "under this condition" part and "do this" part, I
> do not think the above is the only or the best way to express
> "distinct values allowed for the same key".  How do we argue that
> this from your example
>
>   if_exists = add_if_different
>   if_missing = add
>
> is a better design than this, for example?
>
>   if_key_value_exists = ignore ; exact matching <key,val> exists
>   if_key_exists = add          ; otherwise
>   if_missing = add

The question is what happens if we want to say "if the same <key, value> pair exists but not near where we would add.

With the solution I implemented, that is:
if_exists = add_if_different_neighbor
With the solution you suggest, should we have:
  if_key_value_exists_not_neighbor = add ; exact matching <key,val>
exists but not near where we would add
  if_key_value_exists = ignore ; exact matching <key,val> exists
  if_key_exists = add          ; otherwise
  if_missing = add
or:
  if_key_value_exists = ignore_if_neighbor ; exact matching <key,val> exists
  if_key_exists = add          ; otherwise
  if_missing = add

And what happens if we want to say "if key exists and value matches <regex>", how do we express that then? Or what happens when we want to say "if key exists and <command> succeeds"?

With what I suggest, we can still say:
if_exists = add_if_values_matches <regex>
or
if_exists = add_if_succeeds <cmd>
With the solution you suggest, should it be:
  if_key_value_exists = ignore
  if_key_exists = add_if_values_matches <regex>
and
  if_key_value_exists = ignore
  if_key_exists = add_if_succeeds <cmd>
?
Doesn't it look like redondant between the 2 lines?
And then people might ask if they can also use something like this:
  if_key_value_exists = add_if_succeeds <cmd1>
  if_key_exists = add_if_succeeds <cmd2>
  if_key_missing = add_if_succeeds <cmd3>

and it will look like we are trying too much to do what should be done in only one script.

Show 13 quoted lines
> The latter splits remaining conditional logic from "do this" part
> (no more "add_if_different" that causes "add" action to see if there
> is the same key and act differently) and moves it to "under this
> condition" part.
>
> I am trying to illustrate that the line to split the "under this
> condition" and "do this" looks arbitrary and fuzzy here, and because
> of this arbitrary-ness and fuzziness, it is not very obvious to me
> why it is a good idea to have "under this condition" as a separate
> concept from "do this" settings.
>
> What is the advantage of having such an extra axis?  Does it make it
> easier to manage?  Does it allow us to express more?

It will be complex anyway, but at least, with what I implemented, there are only 2 cases in the "under this condition part" and they are completely disjoint, which means that they are hopefully not very difficult to understand.

I agree that it may look arbitrary and fuzzy to split things this way, but I think it's a good compromise between avoiding combinatorial explosion in the number of choices, keeping it easy to understand and allowing many features now and more in the future.

Thanks, Christian.

Junio C Hamano· Feb 11, 2014, 18:07 UTC · re: Christian Couder · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

Christian Couder <christian.couder@gmail.com> writes:
Show 38 quoted lines
>> Even if we assume, for the sake of discussion, that it *is* a good
>> idea to separate "under this condition" part and "do this" part, I
>> do not think the above is the only or the best way to express
>> "distinct values allowed for the same key".  How do we argue that
>> this from your example
>>
>>   if_exists = add_if_different
>>   if_missing = add
>>
>> is a better design than this, for example?
>>
>>   if_key_value_exists = ignore ; exact matching <key,val> exists
>>   if_key_exists = add          ; otherwise
>>   if_missing = add
>
> The question is what happens if we want to say "if the same <key,
> value> pair exists but not near where we would add.
>
> With the solution I implemented, that is:
> ...
> With the solution you suggest, should we have:
> ...
>   if_key_value_exists_not_neighbor = add ; exact matching <key,val>
> ...
> or:
>   if_key_value_exists = ignore_if_neighbor ; exact matching <key,val> exists
> ...
>
> And what happens if we want to say "if key exists and value matches
> <regex>", how do we express that then?
> Or what happens when we want to say "if key exists and <command> succeeds"?
> ...
> With what I suggest, we can still say:
> ...
> And then people might ask if they can also use something like this:
> ...
> and it will look like we are trying too much to do what should be done
> in only one script.
I think the above illustrates my point very clearly.

These numerous questions you have to ask are indications why choosing "this condition goes to the left hand side of the equal sign (e.g. exists)" and "this condition goes to the right hand side (e.g. do-this-if_neighbor)" is not working well. The user has to remember which conditions go to the variable name and which conditions go to the action part.

And the made-up alternative you listed above is not a solution I suggest to begin with---it is a strawman "if we assume such a splitting were a good idea" in the first place ;-).

What I was wondering if it is an better alternative was the below.
Show 10 quoted lines
>> The latter splits remaining conditional logic from "do this" part
>> (no more "add_if_different" that causes "add" action to see if there
>> is the same key and act differently) and moves it to "under this
>> condition" part.
>> ...
>> I am trying to illustrate that the line to split the "under this
>> condition" and "do this" looks arbitrary and fuzzy here, and because
>> of this arbitrary-ness and fuzziness, it is not very obvious to me
>> why it is a good idea to have "under this condition" as a separate
>> concept from "do this" settings.

That is, not splitting the logic into two parts like you did, i.e. "if_X = do_Y_if_Z", which invites "why is it not if_true = do_Y_if_X_and_Z, if_X_and_Z = do_Y, if_Z = do_Y_if_X"?

One possible way to avoid the confusion is to say "do_Y_if_X_and_Z" without making the rule split into conditions and actions---I am NOT saying that is _better_, there may be other solutions to avoid this two-part logic in a cleaner way.

Christian Couder· Feb 14, 2014, 21:41 UTC · re: Junio C Hamano · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

From: Junio C Hamano <gitster@pobox.com>
Show 7 quoted lines
>
> These numerous questions you have to ask are indications why
> choosing "this condition goes to the left hand side of the equal
> sign (e.g. exists)" and "this condition goes to the right hand side
> (e.g. do-this-if_neighbor)" is not working well.  The user has to
> remember which conditions go to the variable name and which
> conditions go to the action part.

If we limit it to "if_exists" and "if_missing", the user can remember that without things becoming too complex.

> That is, not splitting the logic into two parts like you did,
> i.e. "if_X = do_Y_if_Z", which invites "why is it not if_true =
> do_Y_if_X_and_Z, if_X_and_Z = do_Y, if_Z = do_Y_if_X"?

It perhaps invite it, but there are reasons why splitting the logic too much is not a good idea and why limiting the split to "if_exists" and "if_missing" is a good tradeoff.

> One possible way to avoid the confusion is to say "do_Y_if_X_and_Z"
> without making the rule split into conditions and actions---I am
> NOT saying that is _better_, there may be other solutions to avoid
> this two-part logic in a cleaner way.

This has been discussed first last November and I don't think that a solution better than what I implemented has been suggested.

The problem is we not only want to say:
    action = do_Y_if_X_and_Z
but we also want to say:
    action = do_Y_if_X_and_Z AND do_U_if_V
For example some people might want:
    if_exists = overwrite
    if_missing = add
while others might want:
    if_exists = overwrite
    if_missing = do_nothing
and I don't see how we can say that with just:
    action = do_Y_if_X_and_Z

Thanks, Christian.

Junio C Hamano· Feb 14, 2014, 21:46 UTC · re: Christian Couder · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

Christian Couder <chriscool@tuxfamily.org> writes:
Show 13 quoted lines
> For example some people might want:
>
>     if_exists = overwrite
>     if_missing = add
>
> while others might want:
>
>     if_exists = overwrite
>     if_missing = do_nothing
>
> and I don't see how we can say that with just:
>
>     action = do_Y_if_X_and_Z

Yes, but then we go back to my original question: why exists and missing are so special, and why there aren't two kinds of exists (i.e. "there exists an entry with the same <key, value>" vs "there exists an entry with the same <key>"). I would have understood your "this is not too hard to understand for users" if you had three (i.e. "missing", in addition to these two flavours of "exists"), but with only two, I do not see how it is useful in a hypothetical situation like above.

For example, how would you express something like this only with "if-exists" vs "if-missing"?

	if_exists_exactly = ignore
        if_exists_with_different_value = append
        if_missng = prepend_to_the_beginning
Christian Couder· Feb 14, 2014, 23:29 UTC · re: Junio C Hamano · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

From: Junio C Hamano <gitster@pobox.com>
Show 19 quoted lines
>
> Christian Couder <chriscool@tuxfamily.org> writes:
> 
>> For example some people might want:
>>
>>     if_exists = overwrite
>>     if_missing = add
>>
>> while others might want:
>>
>>     if_exists = overwrite
>>     if_missing = do_nothing
>>
>> and I don't see how we can say that with just:
>>
>>     action = do_Y_if_X_and_Z
> 
> Yes, but then we go back to my original question: why exists and
> missing are so special,

Because they are completely disjoint, easy to understand, and they can avoid a lot of combinatorial explosion we would have if we used only one "action" variable, while still providing lot of expressiveness.

They are just a good tradeoff for the special problem we have.
> and why there aren't two kinds of exists
> (i.e. "there exists an entry with the same <key, value>" vs "there
> exists an entry with the same <key>").

Because it doesn't improve expressiveness much, doesn't remove much combinatorial explosion and make it significantly more difficult to understand, compared to only "if_exists" and "if_missing".

Show 5 quoted lines
> I would have understood your
> "this is not too hard to understand for users" if you had three
> (i.e. "missing", in addition to these two flavours of "exists"), but
> with only two, I do not see how it is useful in a hypothetical
> situation like above.
You mean that you do not see why:
     if_exists = overwrite
     if_missing = do_nothing
is simple and expressive?
Show 6 quoted lines
> For example, how would you express something like this only with
> "if-exists" vs "if-missing"?
> 
> 	if_exists_exactly = ignore
>         if_exists_with_different_value = append
>         if_missng = prepend_to_the_beginning

First, previously in the discussion you said that you didn't want us to talk about the "where = (after | before)" part, because you could see that it was orthogonal to the other stuff, but now it looks like you want again to put that on the table.

Then yes, it is not possible to express the above with what I implemented. But it could be possible with only "if-exists" vs "if-missing" like this:

	if_exists = append_if_different
	if_missing = prepend
And yes I think it is much better than:
	if_exists_exactly = ignore
	if_exists_with_different_value = append
	if_missng = prepend_to_the_beginning
because we can still easily express things like:
	if_exists = append_if_different_neighbor
	if_missing = prepend

while it would be more difficult to understand with "if_exists_exactly", "if_exists_with_different_value" and "if_missing".

Thanks, Christian.

Junio C Hamano· Feb 14, 2014, 23:39 UTC · re: Christian Couder · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

Show 11 quoted lines
>> For example, how would you express something like this only with
>> "if-exists" vs "if-missing"?
>>
>>       if_exists_exactly = ignore
>>         if_exists_with_different_value = append
>>         if_missng = prepend_to_the_beginning
>
> First, previously in the discussion you said that you didn't want us
> to talk about the "where = (after | before)" part, because you could
> see that it was orthogonal to the other stuff, but now it looks like
> you want again to put that on the table.

Oh, then replace both "append" and "prepend" with "append" (it was a mistake). Can you express that without having two kinds of if-exists?

Show 9 quoted lines
> But it could be possible with only "if-exists" vs
> "if-missing" like this:
>
>         if_exists = append_if_different
>         if_missing = prepend
> ...
> because we can still easily express things like:
>
>         if_exists = append_if_different_neighbor

The proliferation of these random "if_X" on the action part _is_ exactly what I find the proposal confusing.

Junio C Hamano· Feb 14, 2014, 23:57 UTC · re: Christian Couder · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

Christian Couder <chriscool@tuxfamily.org> writes:
Show 17 quoted lines
> but we also want to say:
>
>     action = do_Y_if_X_and_Z AND do_U_if_V
>
> For example some people might want:
>
>     if_exists = overwrite
>     if_missing = add
>
> while others might want:
>
>     if_exists = overwrite
>     if_missing = do_nothing
>
> and I don't see how we can say that with just:
>
>     action = do_Y_if_X_and_Z

That is a very relevant illustration that makes me realize why I found your "if-exists/if-missing = do-Y-if-Z" somewhat distasteful.

Your
     if_exists = add_if_different

says "if the same key is there, add it if the value is different", but it also implicitly says "donothing if the value is the same".

That is, you are saying with the above
     if_exists = add_if_different AND ignore_if_same

So you already have to support more than one actions depending on the condition, unless you want to limit the actions for all the cases other than X to be only "ignore" when you invent your next "do_Y_if_X", X being "different" in this case, but you support (and need to support) "different-neighbour" and other random collections of conditions, I think. Which is essentially the same as saying that you need this:

>     action = do_Y_if_X_and_Z AND do_U_if_V
Again, unless all the U's are limited to "ignore", that is.
Junio C Hamano· Feb 15, 2014, 00:16 UTC · re: Junio C Hamano · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

Junio C Hamano <gitster@pobox.com> writes:
Show 12 quoted lines
> That is, you are saying with the above
>
>      if_exists = add_if_different AND ignore_if_same
>
> So you already have to support more than one actions depending on
> the condition, ...
> of conditions, I think.  Which is essentially the same as saying
> that you need this:
>
>>     action = do_Y_if_X_and_Z AND do_U_if_V
>
> Again, unless all the U's are limited to "ignore", that is.

Oh by the way, don't get me wrong. I am not saying that the last one is necessarily better or worse. I am only saying that the semantics proposed seems to be hard to explain and we need to do find a way to do better.

If you have these existing ones:
	Sob: A
        Sob: B
        Sob: C
        Sob: D

and you have "Sob: B" at hand, "Sob.if-missing" would not fire (because if-exists/if-missing is only about keys) ans "Sob.if-exists" will. What happens is now up to the action part (i.e. what follows "if_exists =", e.g. "add_if_different").

The conditional part of "add_if_different" action is explainable as a conditon on the value (as opposed to condition on keys, which is the left-hand-side). But what does a condition with "neighbour" in its name really mean? It is not a condition about the value, but also is a condition about the order of the existing records.

What is the right mental model the end-user needs to form when understanding these? Conditions on keys go on the left, and any other random conditions can come as a modifier after action e.g. add_if_same_value_is_not_at_the_end?

Junio C Hamano· Feb 21, 2014, 00:22 UTC · re: Junio C Hamano · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

Junio C Hamano <gitster@pobox.com> writes:
Show 6 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
>
> What is the right mental model the end-user needs to form when
> understanding these?  Conditions on keys go on the left, and any
> other random conditions can come as a modifier after action
> e.g. add_if_same_value_is_not_at_the_end?

Having said all that, it appears that nobody seems to be able to come up with a saner arrangement that would not paint us into a tough corner that we would not be able to later escape from without being backward incompatible---I certainly didn't.

So... let's take this from your earlier message:
>> If we limit it to "if_exists" and "if_missing", the user can remember
>> that without things becoming too complex.

and go with the semantics the posted patches (I believe I have the latest from you on 'pu') attempt to implement, at least for now.

IOW, when re-rolling, let's not try changing the arrangement to use if-exists/if-missing (configuration variable names) for keys' existence and include chosen set of conditions on values as modifiers to the action (i.e. X in "do_Y_in_X").

Christian Couder· Feb 23, 2014, 10:44 UTC · re: Junio C Hamano · lore

Re: [PATCH v5 02/14] trailer: process trailers from file and arguments

From: Junio C Hamano <gitster@pobox.com>
Show 20 quoted lines
>
> Junio C Hamano <gitster@pobox.com> writes:
>
> Having said all that, it appears that nobody seems to be able to
> come up with a saner arrangement that would not paint us into a
> tough corner that we would not be able to later escape from without
> being backward incompatible---I certainly didn't.
> 
> So... let's take this from your earlier message:
> 
>>> If we limit it to "if_exists" and "if_missing", the user can remember
>>> that without things becoming too complex.
> 
> and go with the semantics the posted patches (I believe I have the
> latest from you on 'pu') attempt to implement, at least for now.
> 
> IOW, when re-rolling, let's not try changing the arrangement to use
> if-exists/if-missing (configuration variable names) for keys'
> existence and include chosen set of conditions on values as
> modifiers to the action (i.e. X in "do_Y_in_X").
Ok, will re-roll soon.

Thanks, Christian.

Christian Couder· Feb 6, 2014, 20:19 UTC · re: Christian Couder · lore

[PATCH v5 03/14] trailer: read and process config information

This patch implements reading the configuration to get trailer information, and then processing it and storing it in a doubly linked list.

The config information is stored in the list whose first item is pointed to by:

static struct trailer_item *first_conf_item;
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 trailer.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 134 insertions(+)
Show changes to trailer.c +134 −0
diff --git a/trailer.c b/trailer.c
index ba0cfe0..f844f46 100644
--- a/trailer.c
+++ b/trailer.c
@@ -25,6 +25,8 @@ struct trailer_item {
 	struct conf_info *conf;
 };
 
+static struct trailer_item *first_conf_item;
+
 static inline int same_token(struct trailer_item *a, struct trailer_item *b, int alnum_len)
 {
 	return !strncasecmp(a->token, b->token, alnum_len);
@@ -233,3 +235,135 @@ static void process_trailers_lists(struct trailer_item **infile_tok_first,
 		apply_arg_if_missing(infile_tok_first, infile_tok_last, arg_tok);
 	}
 }
+
+static int set_where(struct conf_info *item, const char *value)
+{
+	if (!strcasecmp("after", value))
+		item->where = WHERE_AFTER;
+	else if (!strcasecmp("before", value))
+		item->where = WHERE_BEFORE;
+	else
+		return 1;
+	return 0;
+}
+
+static int set_if_exist(struct conf_info *item, const char *value)
+{
+	if (!strcasecmp("addIfDifferent", value))
+		item->if_exist = EXIST_ADD_IF_DIFFERENT;
+	else if (!strcasecmp("addIfDifferentNeighbor", value))
+		item->if_exist = EXIST_ADD_IF_DIFFERENT_NEIGHBOR;
+	else if (!strcasecmp("add", value))
+		item->if_exist = EXIST_ADD;
+	else if (!strcasecmp("overwrite", value))
+		item->if_exist = EXIST_OVERWRITE;
+	else if (!strcasecmp("doNothing", value))
+		item->if_exist = EXIST_DO_NOTHING;
+	else
+		return 1;
+	return 0;
+}
+
+static int set_if_missing(struct conf_info *item, const char *value)
+{
+	if (!strcasecmp("doNothing", value))
+		item->if_missing = MISSING_DO_NOTHING;
+	else if (!strcasecmp("add", value))
+		item->if_missing = MISSING_ADD;
+	else
+		return 1;
+	return 0;
+}
+
+enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_WHERE,
+			 TRAILER_IF_EXIST, TRAILER_IF_MISSING };
+
+static int set_name_and_type(const char *conf_key, const char *suffix,
+			     enum trailer_info_type type,
+			     char **pname, enum trailer_info_type *ptype)
+{
+	int ret = ends_with(conf_key, suffix);
+	if (ret) {
+		*pname = xstrndup(conf_key, strlen(conf_key) - strlen(suffix));
+		*ptype = type;
+	}
+	return ret;
+}
+
+static struct trailer_item *get_conf_item(const char *name)
+{
+	struct trailer_item *item;
+	struct trailer_item *previous;
+
+	/* Look up item with same name */
+	for (previous = NULL, item = first_conf_item;
+	     item;
+	     previous = item, item = item->next) {
+		if (!strcasecmp(item->conf->name, name))
+			return item;
+	}
+
+	/* Item does not already exists, create it */
+	item = xcalloc(sizeof(struct trailer_item), 1);
+	item->conf = xcalloc(sizeof(struct conf_info), 1);
+	item->conf->name = xstrdup(name);
+
+	if (!previous)
+		first_conf_item = item;
+	else {
+		previous->next = item;
+		item->previous = previous;
+	}
+
+	return item;
+}
+
+static int git_trailer_config(const char *conf_key, const char *value, void *cb)
+{
+	if (starts_with(conf_key, "trailer.")) {
+		const char *orig_conf_key = conf_key;
+		struct trailer_item *item;
+		struct conf_info *conf;
+		char *name;
+		enum trailer_info_type type;
+
+		conf_key += 8;
+		if (!set_name_and_type(conf_key, ".key", TRAILER_KEY, &name, &type) &&
+		    !set_name_and_type(conf_key, ".command", TRAILER_COMMAND, &name, &type) &&
+		    !set_name_and_type(conf_key, ".where", TRAILER_WHERE, &name, &type) &&
+		    !set_name_and_type(conf_key, ".ifexist", TRAILER_IF_EXIST, &name, &type) &&
+		    !set_name_and_type(conf_key, ".ifmissing", TRAILER_IF_MISSING, &name, &type))
+			return 0;
+
+		item = get_conf_item(name);
+		conf = item->conf;
+
+		switch (type) {
+		case TRAILER_KEY:
+			if (conf->key)
+				warning(_("more than one %s"), orig_conf_key);
+			conf->key = xstrdup(value);
+			break;
+		case TRAILER_COMMAND:
+			if (conf->command)
+				warning(_("more than one %s"), orig_conf_key);
+			conf->command = xstrdup(value);
+			break;
+		case TRAILER_WHERE:
+			if (set_where(conf, value))
+				warning(_("unknown value '%s' for key '%s'"), value, orig_conf_key);
+			break;
+		case TRAILER_IF_EXIST:
+			if (set_if_exist(conf, value))
+				warning(_("unknown value '%s' for key '%s'"), value, orig_conf_key);
+			break;
+		case TRAILER_IF_MISSING:
+			if (set_if_missing(conf, value))
+				warning(_("unknown value '%s' for key '%s'"), value, orig_conf_key);
+			break;
+		default:
+			die("internal bug in trailer.c");
+		}
+	}
+	return 0;
+}
-- 
1.8.5.2.206.g98f5689.dirty
Christian Couder· Feb 6, 2014, 20:19 UTC · re: Christian Couder · lore

[PATCH v5 04/14] trailer: process command line trailer arguments

This patch parses the trailer command line arguments and put the result into an arg_tok doubly linked list.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 trailer.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 84 insertions(+)
Show changes to trailer.c +84 −0
diff --git a/trailer.c b/trailer.c
index f844f46..3fde21d 100644
--- a/trailer.c
+++ b/trailer.c
@@ -367,3 +367,87 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)
 	}
 	return 0;
 }
+
+static void parse_trailer(struct strbuf *tok, struct strbuf *val, const char *trailer)
+{
+	const char *end = strchr(trailer, '=');
+	if (!end)
+		end = strchr(trailer, ':');
+	if (end) {
+		strbuf_add(tok, trailer, end - trailer);
+		strbuf_trim(tok);
+		strbuf_addstr(val, end + 1);
+		strbuf_trim(val);
+	} else {
+		strbuf_addstr(tok, trailer);
+		strbuf_trim(tok);
+	}
+}
+
+static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,
+					     const char* tok, const char* val)
+{
+	struct trailer_item *new = xcalloc(sizeof(*new), 1);
+	new->value = val;
+
+	if (conf_item) {
+		new->conf = conf_item->conf;
+		new->token = xstrdup(conf_item->conf->key);
+	} else {
+		new->conf = xcalloc(sizeof(struct conf_info), 1);
+		new->token = tok;
+	}
+
+	return new;
+}
+
+static struct trailer_item *create_trailer_item(const char *string)
+{
+	struct strbuf tok = STRBUF_INIT;
+	struct strbuf val = STRBUF_INIT;
+	struct trailer_item *item;
+	int tok_alnum_len;
+
+	parse_trailer(&tok, &val, string);
+
+	tok_alnum_len = alnum_len(tok.buf, tok.len);
+
+	/* Lookup if the token matches something in the config */
+	for (item = first_conf_item; item; item = item->next) {
+		if (!strncasecmp(tok.buf, item->conf->key, tok_alnum_len) ||
+		    !strncasecmp(tok.buf, item->conf->name, tok_alnum_len)) {
+			strbuf_release(&tok);
+			return new_trailer_item(item, NULL, strbuf_detach(&val, NULL));
+		}
+	}
+
+	return new_trailer_item(NULL, strbuf_detach(&tok, NULL), strbuf_detach(&val, NULL));;
+}
+
+static void add_trailer_item(struct trailer_item **first,
+			     struct trailer_item **last,
+			     struct trailer_item *new)
+{
+	if (!*last) {
+		*first = new;
+		*last = new;
+	} else {
+		(*last)->next = new;
+		new->previous = *last;
+		*last = new;
+	}
+}
+
+static struct trailer_item *process_command_line_args(int argc, const char **argv)
+{
+	int i;
+	struct trailer_item *arg_tok_first = NULL;
+	struct trailer_item *arg_tok_last = NULL;
+
+	for (i = 0; i < argc; i++) {
+		struct trailer_item *new = create_trailer_item(argv[i]);
+		add_trailer_item(&arg_tok_first, &arg_tok_last, new);
+	}
+
+	return arg_tok_first;
+}
-- 
1.8.5.2.206.g98f5689.dirty
Junio C Hamano· Feb 7, 2014, 00:08 UTC · re: Christian Couder · lore

Re: [PATCH v5 04/14] trailer: process command line trailer arguments

Christian Couder <chriscool@tuxfamily.org> writes:
> This patch parses the trailer command line arguments
> and put the result into an arg_tok doubly linked
> list.
No the patch doesn't parse anything ;-).
"Parse the command line arguments and ...".
Show 5 quoted lines
> +static void parse_trailer(struct strbuf *tok, struct strbuf *val, const char *trailer)
> +{
> +	const char *end = strchr(trailer, '=');
> +	if (!end)
> +		end = strchr(trailer, ':');

How would you explain the behaviour of the above code for this input?

	frotz: nitfol=xyzzy
Perhaps strcspn()?
Christian Couder· Feb 9, 2014, 14:06 UTC · re: Junio C Hamano · lore

Re: [PATCH v5 04/14] trailer: process command line trailer arguments

From: Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH v5 04/14] trailer: process command line trailer arguments
Date: Thu, 06 Feb 2014 16:08:24 -0800
Show 22 quoted lines
> Christian Couder <chriscool@tuxfamily.org> writes:
> 
>> This patch parses the trailer command line arguments
>> and put the result into an arg_tok doubly linked
>> list.
> 
> No the patch doesn't parse anything ;-).
> 
> "Parse the command line arguments and ...".
> 
>> +static void parse_trailer(struct strbuf *tok, struct strbuf *val, const char *trailer)
>> +{
>> +	const char *end = strchr(trailer, '=');
>> +	if (!end)
>> +		end = strchr(trailer, ':');
> 
> How would you explain the behaviour of the above code for this
> input?
> 
> 	frotz: nitfol=xyzzy
> 
> Perhaps strcspn()?
Ok to use strcspn().

Thanks, Christian.

Christian Couder· Feb 6, 2014, 20:19 UTC · re: Christian Couder · lore

[PATCH v5 05/14] trailer: parse trailers from input file

This patch reads trailers from an input file, parses them and puts the result into a doubly linked list.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 trailer.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 71 insertions(+)
Show changes to trailer.c +71 −0
diff --git a/trailer.c b/trailer.c
index 3fde21d..f59efd1 100644
--- a/trailer.c
+++ b/trailer.c
@@ -49,6 +49,13 @@ static inline size_t alnum_len(const char *buf, int len)
 	return (size_t) len + 1;
 }
 
+static inline int contains_only_spaces(const char *str)
+{
+	const char *s;
+	for (s = str; *s && isspace(*s); s++);
+	return !*s;
+}
+
 static void add_arg_to_infile(struct trailer_item *infile_tok,
 			      struct trailer_item *arg_tok)
 {
@@ -451,3 +458,67 @@ static struct trailer_item *process_command_line_args(int argc, const char **arg
 
 	return arg_tok_first;
 }
+
+static struct strbuf **read_input_file(const char *infile)
+{
+	struct strbuf sb = STRBUF_INIT;
+
+	if (strbuf_read_file(&sb, infile, 0) < 0)
+		die_errno(_("could not read input file '%s'"), infile);
+
+	return strbuf_split(&sb, '\n');
+}
+
+/*
+ * Return the the (0 based) index of the first trailer line
+ * or the line count if there are no trailers.
+ */
+static int find_trailer_start(struct strbuf **lines)
+{
+	int count, start, empty = 1;
+
+	/* Get the line count */
+	for (count = 0; lines[count]; count++);
+
+	/*
+	 * Get the start of the trailers by looking starting from the end
+	 * for a line with only spaces before lines with one ':'.
+	 */
+	for (start = count - 1; start >= 0; start--) {
+		if (contains_only_spaces(lines[start]->buf)) {
+			if (empty)
+				continue;
+			return start + 1;
+		}
+		if (strchr(lines[start]->buf, ':')) {
+			if (empty)
+				empty = 0;
+			continue;
+		}
+		return count;
+	}
+
+	return empty ? count : start + 1;
+}
+
+static void process_input_file(const char *infile,
+			       struct trailer_item **infile_tok_first,
+			       struct trailer_item **infile_tok_last)
+{
+	struct strbuf **lines = read_input_file(infile);
+	int start = find_trailer_start(lines);
+	int i;
+
+	/* Print non trailer lines as is */
+	for (i = 0; lines[i] && i < start; i++) {
+		printf("%s", lines[i]->buf);
+	}
+
+	/* Parse trailer lines */
+	for (i = start; lines[i]; i++) {
+		struct trailer_item *new = create_trailer_item(lines[i]->buf);
+		add_trailer_item(infile_tok_first, infile_tok_last, new);
+	}
+
+	strbuf_list_free(lines);
+}
-- 
1.8.5.2.206.g98f5689.dirty
Christian Couder· Feb 6, 2014, 20:19 UTC · re: Christian Couder · lore

[PATCH v5 06/14] trailer: put all the processing together and print

This patch adds the process_trailers() function that calls all the previously added processing functions and then prints the results on the standard output.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 trailer.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
Show changes to trailer.c +40 −0
diff --git a/trailer.c b/trailer.c
index f59efd1..186316f 100644
--- a/trailer.c
+++ b/trailer.c
@@ -56,6 +56,26 @@ static inline int contains_only_spaces(const char *str)
 	return !*s;
 }
 
+static void print_tok_val(const char *tok, const char *val)
+{
+	char c = tok[strlen(tok) - 1];
+	if (isalnum(c))
+		printf("%s: %s\n", tok, val);
+	else if (isspace(c) || c == '#')
+		printf("%s%s\n", tok, val);
+	else
+		printf("%s %s\n", tok, val);
+}
+
+static void print_all(struct trailer_item *first, int trim_empty)
+{
+	struct trailer_item *item;
+	for (item = first; item; item = item->next) {
+		if (!trim_empty || strlen(item->value) > 0)
+			print_tok_val(item->token, item->value);
+	}
+}
+
 static void add_arg_to_infile(struct trailer_item *infile_tok,
 			      struct trailer_item *arg_tok)
 {
@@ -522,3 +542,23 @@ static void process_input_file(const char *infile,
 
 	strbuf_list_free(lines);
 }
+
+void process_trailers(const char *infile, int trim_empty, int argc, const char **argv)
+{
+	struct trailer_item *infile_tok_first = NULL;
+	struct trailer_item *infile_tok_last = NULL;
+	struct trailer_item *arg_tok_first;
+
+	git_config(git_trailer_config, NULL);
+
+	/* Print the non trailer part of infile */
+	if (infile) {
+		process_input_file(infile, &infile_tok_first, &infile_tok_last);
+	}
+
+	arg_tok_first = process_command_line_args(argc, argv);
+
+	process_trailers_lists(&infile_tok_first, &infile_tok_last, &arg_tok_first);
+
+	print_all(infile_tok_first, trim_empty);
+}
-- 
1.8.5.2.206.g98f5689.dirty
Christian Couder· Feb 6, 2014, 20:19 UTC · re: Christian Couder · lore

[PATCH v5 07/14] trailer: add interpret-trailers command

This patch adds the "git interpret-trailers" command. This command uses the previously added process_trailers() function in trailer.c.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 .gitignore                   |  1 +
 Makefile                     |  1 +
 builtin.h                    |  1 +
 builtin/interpret-trailers.c | 36 ++++++++++++++++++++++++++++++++++++
 git.c                        |  1 +
 trailer.h                    |  6 ++++++
 6 files changed, 46 insertions(+)
 create mode 100644 builtin/interpret-trailers.c
 create mode 100644 trailer.h
Show changes to 6 files +46 −0

.gitignore, Makefile, builtin.h, builtin/interpret-trailers.c, git.c, trailer.h

diff --git a/.gitignore b/.gitignore
index b5f9def..c870ada 100644
--- a/.gitignore
+++ b/.gitignore
@@ -74,6 +74,7 @@
 /git-index-pack
 /git-init
 /git-init-db
+/git-interpret-trailers
 /git-instaweb
 /git-log
 /git-ls-files
diff --git a/Makefile b/Makefile
index ec90feb..a91465e 100644
--- a/Makefile
+++ b/Makefile
@@ -935,6 +935,7 @@ BUILTIN_OBJS += builtin/hash-object.o
 BUILTIN_OBJS += builtin/help.o
 BUILTIN_OBJS += builtin/index-pack.o
 BUILTIN_OBJS += builtin/init-db.o
+BUILTIN_OBJS += builtin/interpret-trailers.o
 BUILTIN_OBJS += builtin/log.o
 BUILTIN_OBJS += builtin/ls-files.o
 BUILTIN_OBJS += builtin/ls-remote.o
diff --git a/builtin.h b/builtin.h
index d4afbfe..30f4c30 100644
--- a/builtin.h
+++ b/builtin.h
@@ -71,6 +71,7 @@ extern int cmd_hash_object(int argc, const char **argv, const char *prefix);
 extern int cmd_help(int argc, const char **argv, const char *prefix);
 extern int cmd_index_pack(int argc, const char **argv, const char *prefix);
 extern int cmd_init_db(int argc, const char **argv, const char *prefix);
+extern int cmd_interpret_trailers(int argc, const char **argv, const char *prefix);
 extern int cmd_log(int argc, const char **argv, const char *prefix);
 extern int cmd_log_reflog(int argc, const char **argv, const char *prefix);
 extern int cmd_ls_files(int argc, const char **argv, const char *prefix);
diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c
new file mode 100644
index 0000000..04b0ae2
--- /dev/null
+++ b/builtin/interpret-trailers.c
@@ -0,0 +1,36 @@
+/*
+ * Builtin "git interpret-trailers"
+ *
+ * Copyright (c) 2013 Christian Couder <chriscool@tuxfamily.org>
+ *
+ */
+
+#include "cache.h"
+#include "builtin.h"
+#include "parse-options.h"
+#include "strbuf.h"
+#include "trailer.h"
+
+static const char * const git_interpret_trailers_usage[] = {
+	N_("git interpret-trailers [--trim-empty] [--infile=<file>] [(<token>[(=|:)<value>])...]"),
+	NULL
+};
+
+int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
+{
+	const char *infile = NULL;
+	int trim_empty = 0;
+
+	struct option options[] = {
+		OPT_BOOL(0, "trim-empty", &trim_empty, N_("trim empty trailers")),
+		OPT_FILENAME(0, "infile", &infile, N_("use message from file")),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_interpret_trailers_usage, 0);
+
+	process_trailers(infile, trim_empty, argc, argv);
+
+	return 0;
+}
diff --git a/git.c b/git.c
index 3799514..1420b58 100644
--- a/git.c
+++ b/git.c
@@ -383,6 +383,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
 		{ "init", cmd_init_db },
 		{ "init-db", cmd_init_db },
+		{ "interpret-trailers", cmd_interpret_trailers, RUN_SETUP },
 		{ "log", cmd_log, RUN_SETUP },
 		{ "ls-files", cmd_ls_files, RUN_SETUP },
 		{ "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
diff --git a/trailer.h b/trailer.h
new file mode 100644
index 0000000..9db4459
--- /dev/null
+++ b/trailer.h
@@ -0,0 +1,6 @@
+#ifndef TRAILER_H
+#define TRAILER_H
+
+void process_trailers(const char *infile, int trim_empty, int argc, const char **argv);
+
+#endif /* TRAILER_H */
-- 
1.8.5.2.206.g98f5689.dirty
Junio C Hamano· Feb 7, 2014, 00:10 UTC · re: Christian Couder · lore

Re: [PATCH v5 07/14] trailer: add interpret-trailers command

Christian Couder <chriscool@tuxfamily.org> writes:
Show 12 quoted lines
> diff --git a/git.c b/git.c
> index 3799514..1420b58 100644
> --- a/git.c
> +++ b/git.c
> @@ -383,6 +383,7 @@ static void handle_internal_command(int argc, const char **argv)
>  		{ "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
>  		{ "init", cmd_init_db },
>  		{ "init-db", cmd_init_db },
> +		{ "interpret-trailers", cmd_interpret_trailers, RUN_SETUP },
>  		{ "log", cmd_log, RUN_SETUP },
>  		{ "ls-files", cmd_ls_files, RUN_SETUP },
>  		{ "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },

Does this even need to have a git repository? What is the RUN_SETUP for?

Christian Couder· Feb 7, 2014, 08:34 UTC · re: Junio C Hamano · lore

Re: [PATCH v5 07/14] trailer: add interpret-trailers command

On Fri, Feb 7, 2014 at 1:10 AM, Junio C Hamano <gitster@pobox.com> wrote:
Show 17 quoted lines
> Christian Couder <chriscool@tuxfamily.org> writes:
>
>> diff --git a/git.c b/git.c
>> index 3799514..1420b58 100644
>> --- a/git.c
>> +++ b/git.c
>> @@ -383,6 +383,7 @@ static void handle_internal_command(int argc, const char **argv)
>>               { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
>>               { "init", cmd_init_db },
>>               { "init-db", cmd_init_db },
>> +             { "interpret-trailers", cmd_interpret_trailers, RUN_SETUP },
>>               { "log", cmd_log, RUN_SETUP },
>>               { "ls-files", cmd_ls_files, RUN_SETUP },
>>               { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
>
> Does this even need to have a git repository?  What is the RUN_SETUP
> for?

It needs to read git config files, but it could work without reading them too. I will have another look at it.

Thanks, Christian.

Junio C Hamano· Feb 7, 2014, 18:09 UTC · re: Christian Couder · lore

Re: [PATCH v5 07/14] trailer: add interpret-trailers command

Christian Couder <christian.couder@gmail.com> writes:
Show 21 quoted lines
> On Fri, Feb 7, 2014 at 1:10 AM, Junio C Hamano <gitster@pobox.com> wrote:
>> Christian Couder <chriscool@tuxfamily.org> writes:
>>
>>> diff --git a/git.c b/git.c
>>> index 3799514..1420b58 100644
>>> --- a/git.c
>>> +++ b/git.c
>>> @@ -383,6 +383,7 @@ static void handle_internal_command(int argc, const char **argv)
>>>               { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
>>>               { "init", cmd_init_db },
>>>               { "init-db", cmd_init_db },
>>> +             { "interpret-trailers", cmd_interpret_trailers, RUN_SETUP },
>>>               { "log", cmd_log, RUN_SETUP },
>>>               { "ls-files", cmd_ls_files, RUN_SETUP },
>>>               { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
>>
>> Does this even need to have a git repository?  What is the RUN_SETUP
>> for?
>
> It needs to read git config files, but it could work without reading them too.
> I will have another look at it.

Of course. At this point in the series while reviewing 7/14 there was no config [*1*] and that was why I was scratching my head.

[Footnote]

*1* Flipping the series structure to a top-down fashion, having an almost no-op command that fails all the new tests in the beginning and then building the internal incrementally, might be a worthwhile change, but it is *not* worth the effort to add the command without RUN_SETUP at 7/14 and then change the same line to have RUN_SETUP when you start to need it could be an option; I am *not* suggesting that.

Junio C Hamano· Feb 7, 2014, 20:35 UTC · re: Junio C Hamano · lore

Re: [PATCH v5 07/14] trailer: add interpret-trailers command

Junio C Hamano <gitster@pobox.com> writes:
> ... RUN_SETUP at 7/14 and then change the same line to have RUN_SETUP
> when you start to need it could be an option; I am *not* suggesting
> that.
Sorry, typo.  s/could be an option;/;/
Christian Couder· Feb 6, 2014, 20:19 UTC · re: Christian Couder · lore

[PATCH v5 08/14] trailer: add tests for "git interpret-trailers"

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 t/t7513-interpret-trailers.sh | 208 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 208 insertions(+)
 create mode 100755 t/t7513-interpret-trailers.sh
Show changes to t/t7513-interpret-trailers.sh +208 −0
diff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh
new file mode 100755
index 0000000..8be333c
--- /dev/null
+++ b/t/t7513-interpret-trailers.sh
@@ -0,0 +1,208 @@
+#!/bin/sh
+#
+# Copyright (c) 2013 Christian Couder
+#
+
+test_description='git interpret-trailers'
+
+. ./test-lib.sh
+
+cat >basic_message <<'EOF'
+subject
+
+body
+EOF
+
+cat >complex_message_body <<'EOF'
+my subject
+
+my body which is long
+and contains some special
+chars like : = ? !
+
+EOF
+
+# We want one trailing space at the end of each line.
+# Let's use sed to make sure that these spaces are not removed
+# by any automatic tool.
+sed -e 's/ Z$/ /' >complex_message_trailers <<-\EOF
+Fixes: Z
+Acked-by: Z
+Reviewed-by: Z
+Signed-off-by: Z
+EOF
+
+test_expect_success 'without config' '
+	printf "ack: Peff\nReviewed-by: \nAcked-by: Johan\n" >expected &&
+	git interpret-trailers "ack = Peff" "Reviewed-by" "Acked-by: Johan" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success '--trim-empty without config' '
+	printf "ack: Peff\nAcked-by: Johan\n" >expected &&
+	git interpret-trailers --trim-empty "ack = Peff" "Reviewed-by" "Acked-by: Johan" "sob:" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'with config setup' '
+	git config trailer.ack.key "Acked-by: " &&
+	printf "Acked-by: Peff\n" >expected &&
+	git interpret-trailers --trim-empty "ack = Peff" >actual &&
+	test_cmp expected actual &&
+	git interpret-trailers --trim-empty "Acked-by = Peff" >actual &&
+	test_cmp expected actual &&
+	git interpret-trailers --trim-empty "Acked-by :Peff" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'with config setup and = sign' '
+	git config trailer.ack.key "Acked-by= " &&
+	printf "Acked-by= Peff\n" >expected &&
+	git interpret-trailers --trim-empty "ack = Peff" >actual &&
+	test_cmp expected actual &&
+	git interpret-trailers --trim-empty "Acked-by= Peff" >actual &&
+	test_cmp expected actual &&
+	git interpret-trailers --trim-empty "Acked-by : Peff" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'with config setup and # sign' '
+	git config trailer.bug.key "Bug #" &&
+	printf "Bug #42\n" >expected &&
+	git interpret-trailers --trim-empty "bug = 42" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'with commit basic message' '
+	git interpret-trailers --infile basic_message >actual &&
+	test_cmp basic_message actual
+'
+
+test_expect_success 'with commit complex message' '
+	cat complex_message_body complex_message_trailers >complex_message &&
+	cat complex_message_body >expected &&
+	printf "Fixes: \nAcked-by= \nReviewed-by: \nSigned-off-by: \n" >>expected &&
+	git interpret-trailers --infile complex_message >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'with commit complex message and args' '
+	cat complex_message_body >expected &&
+	printf "Fixes: \nAcked-by= \nAcked-by= Peff\nReviewed-by: \nSigned-off-by: \nBug #42\n" >>expected &&
+	git interpret-trailers --infile complex_message "ack: Peff" "bug: 42" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'with commit complex message, args and --trim-empty' '
+	cat complex_message_body >expected &&
+	printf "Acked-by= Peff\nBug #42\n" >>expected &&
+	git interpret-trailers --trim-empty --infile complex_message "ack: Peff" "bug: 42" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'using "where = before"' '
+	git config trailer.bug.where "before" &&
+	cat complex_message_body >expected &&
+	printf "Bug #42\nFixes: \nAcked-by= \nAcked-by= Peff\nReviewed-by: \nSigned-off-by: \n" >>expected &&
+	git interpret-trailers --infile complex_message "ack: Peff" "bug: 42" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'using "where = before" for a token in the middle of infile' '
+	git config trailer.review.key "Reviewed-by:" &&
+	git config trailer.review.where "before" &&
+	cat complex_message_body >expected &&
+	printf "Bug #42\nFixes: \nAcked-by= \nAcked-by= Peff\nReviewed-by: Johan\nReviewed-by: \nSigned-off-by: \n" >>expected &&
+	git interpret-trailers --infile complex_message "ack: Peff" "bug: 42" "review: Johan" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'using "where = before" and --trim-empty' '
+	cat complex_message_body >expected &&
+	printf "Bug #46\nBug #42\nAcked-by= Peff\nReviewed-by: Johan\n" >>expected &&
+	git interpret-trailers --infile complex_message --trim-empty "ack: Peff" "bug: 42" "review: Johan" "Bug: 46"  >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'the default is "ifExist = addIfDifferent"' '
+	cat complex_message_body >expected &&
+	printf "Bug #42\nFixes: \nAcked-by= \nAcked-by= Peff\nReviewed-by: \nSigned-off-by: \n" >>expected &&
+	git interpret-trailers --infile complex_message "ack: Peff" "review:" "bug: 42" "ack: Peff" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'using "ifExist = addIfDifferent"' '
+	git config trailer.review.ifExist "addIfDifferent" &&
+	cat complex_message_body >expected &&
+	printf "Bug #42\nFixes: \nAcked-by= \nAcked-by= Peff\nReviewed-by: \nSigned-off-by: \n" >>expected &&
+	git interpret-trailers --infile complex_message "ack: Peff" "review:" "bug: 42" "ack: Peff" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'using "ifExist = addIfDifferentNeighbor"' '
+	git config trailer.ack.ifExist "addIfDifferentNeighbor" &&
+	cat complex_message_body >expected &&
+	printf "Bug #42\nFixes: \nAcked-by= \nAcked-by= Peff\nAcked-by= Junio\nAcked-by= Peff\nReviewed-by: \nSigned-off-by: \n" >>expected &&
+	git interpret-trailers --infile complex_message "ack: Peff" "review:" "ack: Junio" "bug: 42" "ack: Peff" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'using "ifExist = addIfDifferentNeighbor" and --trim-empty' '
+	git config trailer.ack.ifExist "addIfDifferentNeighbor" &&
+	cat complex_message_body >expected &&
+	printf "Bug #42\nAcked-by= Peff\nAcked-by= Junio\nAcked-by= Peff\n" >>expected &&
+	git interpret-trailers --infile complex_message --trim-empty "ack: Peff" "Acked-by= Peff" "review:" "ack: Junio" "bug: 42" "ack: Peff" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'using "ifExist = add"' '
+	git config trailer.ack.ifExist "add" &&
+	cat complex_message_body >expected &&
+	printf "Bug #42\nFixes: \nAcked-by= \nAcked-by= Peff\nAcked-by= Peff\nAcked-by= Junio\nAcked-by= Peff\nReviewed-by: \nSigned-off-by: \n" >>expected &&
+	git interpret-trailers --infile complex_message "ack: Peff" "Acked-by= Peff" "review:" "ack: Junio" "bug: 42" "ack: Peff" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'using "ifExist = overwrite"' '
+	git config trailer.fix.key "Fixes:" &&
+	git config trailer.fix.ifExist "overwrite" &&
+	cat complex_message_body >expected &&
+	printf "Bug #42\nFixes: 22\nAcked-by= \nAcked-by= Junio\nAcked-by= Peff\nReviewed-by: \nSigned-off-by: \n" >>expected &&
+	git interpret-trailers --infile complex_message "review:" "fix=53" "ack: Junio" "fix=22" "bug: 42" "ack: Peff" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'using "ifExist = doNothing"' '
+	git config trailer.fix.ifExist "doNothing" &&
+	cat complex_message_body >expected &&
+	printf "Bug #42\nFixes: \nAcked-by= \nAcked-by= Junio\nAcked-by= Peff\nReviewed-by: \nSigned-off-by: \n" >>expected &&
+	git interpret-trailers --infile complex_message "review:" "fix=53" "ack: Junio" "fix=22" "bug: 42" "ack: Peff" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'the default is "ifMissing = add"' '
+	git config trailer.cc.key "Cc: " &&
+	git config trailer.Cc.where "before" &&
+	cat complex_message_body >expected &&
+	printf "Bug #42\nCc: Linus\nFixes: \nAcked-by= \nAcked-by= Junio\nAcked-by= Peff\nReviewed-by: \nSigned-off-by: \n" >>expected &&
+	git interpret-trailers --infile complex_message "review:" "fix=53" "cc=Linus" "ack: Junio" "fix=22" "bug: 42" "ack: Peff" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'using "ifMissing = add"' '
+	git config trailer.Cc.ifMissing "add" &&
+	cat complex_message_body >expected &&
+	printf "Cc: Linus\nBug #42\nFixes: \nAcked-by= \nAcked-by= Junio\nAcked-by= Peff\nReviewed-by: \nSigned-off-by: \n" >>expected &&
+	git interpret-trailers --infile complex_message "review:" "fix=53" "ack: Junio" "fix=22" "bug: 42" "cc=Linus" "ack: Peff" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'using "ifMissing = doNothing"' '
+	git config trailer.Cc.ifMissing "doNothing" &&
+	cat complex_message_body >expected &&
+	printf "Bug #42\nFixes: \nAcked-by= \nAcked-by= Junio\nAcked-by= Peff\nReviewed-by: \nSigned-off-by: \n" >>expected &&
+	git interpret-trailers --infile complex_message "review:" "fix=53" "cc=Linus" "ack: Junio" "fix=22" "bug: 42" "ack: Peff" >actual &&
+	test_cmp expected actual
+'
+
+test_done
-- 
1.8.5.2.206.g98f5689.dirty
Junio C Hamano· Feb 7, 2014, 00:11 UTC · re: Christian Couder · lore

Re: [PATCH v5 08/14] trailer: add tests for "git interpret-trailers"

Christian Couder <chriscool@tuxfamily.org> writes:
Show 25 quoted lines
> +
> +cat >basic_message <<'EOF'
> +subject
> +
> +body
> +EOF
> +
> +cat >complex_message_body <<'EOF'
> +my subject
> +
> +my body which is long
> +and contains some special
> +chars like : = ? !
> +
> +EOF
> +
> +# We want one trailing space at the end of each line.
> +# Let's use sed to make sure that these spaces are not removed
> +# by any automatic tool.
> +sed -e 's/ Z$/ /' >complex_message_trailers <<-\EOF
> +Fixes: Z
> +Acked-by: Z
> +Reviewed-by: Z
> +Signed-off-by: Z
> +EOF
Please put things like these inside the first "setup" test.
Christian Couder· Feb 6, 2014, 20:19 UTC · re: Christian Couder · lore

[PATCH v5 09/14] trailer: if no input file is passed, read from stdin

It is simpler and more natural if the "git interpret-trailers" is made a filter as its output already goes to sdtout.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/interpret-trailers.c  |  2 +-
 t/t7513-interpret-trailers.sh |  7 +++++++
 trailer.c                     | 15 +++++++++------
 3 files changed, 17 insertions(+), 7 deletions(-)
Show changes to 3 files +17 −7

builtin/interpret-trailers.c, t/t7513-interpret-trailers.sh, trailer.c

diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c
index 04b0ae2..ae8e561 100644
--- a/builtin/interpret-trailers.c
+++ b/builtin/interpret-trailers.c
@@ -23,7 +23,7 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
 
 	struct option options[] = {
 		OPT_BOOL(0, "trim-empty", &trim_empty, N_("trim empty trailers")),
-		OPT_FILENAME(0, "infile", &infile, N_("use message from file")),
+		OPT_FILENAME(0, "infile", &infile, N_("use message from file, instead of stdin")),
 		OPT_END()
 	};
 
diff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh
index 8be333c..f5ef81f 100755
--- a/t/t7513-interpret-trailers.sh
+++ b/t/t7513-interpret-trailers.sh
@@ -205,4 +205,11 @@ test_expect_success 'using "ifMissing = doNothing"' '
 	test_cmp expected actual
 '
 
+test_expect_success 'with input from stdin' '
+	cat complex_message_body >expected &&
+	printf "Bug #42\nFixes: \nAcked-by= \nAcked-by= Junio\nAcked-by= Peff\nReviewed-by: \nSigned-off-by: \n" >>expected &&
+	git interpret-trailers "review:" "fix=53" "cc=Linus" "ack: Junio" "fix=22" "bug: 42" "ack: Peff" < complex_message >actual &&
+	test_cmp expected actual
+'
+
 test_done
diff --git a/trailer.c b/trailer.c
index 186316f..108e104 100644
--- a/trailer.c
+++ b/trailer.c
@@ -483,8 +483,13 @@ static struct strbuf **read_input_file(const char *infile)
 {
 	struct strbuf sb = STRBUF_INIT;
 
-	if (strbuf_read_file(&sb, infile, 0) < 0)
-		die_errno(_("could not read input file '%s'"), infile);
+	if (infile) {
+		if (strbuf_read_file(&sb, infile, 0) < 0)
+			die_errno(_("could not read input file '%s'"), infile);
+	} else {
+		if (strbuf_read(&sb, fileno(stdin), 0) < 0)
+			die_errno(_("could not read from stdin"));
+	}
 
 	return strbuf_split(&sb, '\n');
 }
@@ -551,10 +556,8 @@ void process_trailers(const char *infile, int trim_empty, int argc, const char *
 
 	git_config(git_trailer_config, NULL);
 
-	/* Print the non trailer part of infile */
-	if (infile) {
-		process_input_file(infile, &infile_tok_first, &infile_tok_last);
-	}
+	/* Print the non trailer part of infile (or stdin if infile is NULL) */
+	process_input_file(infile, &infile_tok_first, &infile_tok_last);
 
 	arg_tok_first = process_command_line_args(argc, argv);
 
-- 
1.8.5.2.206.g98f5689.dirty
Junio C Hamano· Feb 7, 2014, 00:12 UTC · re: Christian Couder · lore

Re: [PATCH v5 09/14] trailer: if no input file is passed, read from stdin

Christian Couder <chriscool@tuxfamily.org> writes:
> It is simpler and more natural if the "git interpret-trailers"
> is made a filter as its output already goes to sdtout.
sdtout?

Why isn't this a pure filter without any "infile" parameter in the first place?

Show 68 quoted lines
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  builtin/interpret-trailers.c  |  2 +-
>  t/t7513-interpret-trailers.sh |  7 +++++++
>  trailer.c                     | 15 +++++++++------
>  3 files changed, 17 insertions(+), 7 deletions(-)
>
> diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c
> index 04b0ae2..ae8e561 100644
> --- a/builtin/interpret-trailers.c
> +++ b/builtin/interpret-trailers.c
> @@ -23,7 +23,7 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
>  
>  	struct option options[] = {
>  		OPT_BOOL(0, "trim-empty", &trim_empty, N_("trim empty trailers")),
> -		OPT_FILENAME(0, "infile", &infile, N_("use message from file")),
> +		OPT_FILENAME(0, "infile", &infile, N_("use message from file, instead of stdin")),
>  		OPT_END()
>  	};
>  
> diff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh
> index 8be333c..f5ef81f 100755
> --- a/t/t7513-interpret-trailers.sh
> +++ b/t/t7513-interpret-trailers.sh
> @@ -205,4 +205,11 @@ test_expect_success 'using "ifMissing = doNothing"' '
>  	test_cmp expected actual
>  '
>  
> +test_expect_success 'with input from stdin' '
> +	cat complex_message_body >expected &&
> +	printf "Bug #42\nFixes: \nAcked-by= \nAcked-by= Junio\nAcked-by= Peff\nReviewed-by: \nSigned-off-by: \n" >>expected &&
> +	git interpret-trailers "review:" "fix=53" "cc=Linus" "ack: Junio" "fix=22" "bug: 42" "ack: Peff" < complex_message >actual &&
> +	test_cmp expected actual
> +'
> +
>  test_done
> diff --git a/trailer.c b/trailer.c
> index 186316f..108e104 100644
> --- a/trailer.c
> +++ b/trailer.c
> @@ -483,8 +483,13 @@ static struct strbuf **read_input_file(const char *infile)
>  {
>  	struct strbuf sb = STRBUF_INIT;
>  
> -	if (strbuf_read_file(&sb, infile, 0) < 0)
> -		die_errno(_("could not read input file '%s'"), infile);
> +	if (infile) {
> +		if (strbuf_read_file(&sb, infile, 0) < 0)
> +			die_errno(_("could not read input file '%s'"), infile);
> +	} else {
> +		if (strbuf_read(&sb, fileno(stdin), 0) < 0)
> +			die_errno(_("could not read from stdin"));
> +	}
>  
>  	return strbuf_split(&sb, '\n');
>  }
> @@ -551,10 +556,8 @@ void process_trailers(const char *infile, int trim_empty, int argc, const char *
>  
>  	git_config(git_trailer_config, NULL);
>  
> -	/* Print the non trailer part of infile */
> -	if (infile) {
> -		process_input_file(infile, &infile_tok_first, &infile_tok_last);
> -	}
> +	/* Print the non trailer part of infile (or stdin if infile is NULL) */
> +	process_input_file(infile, &infile_tok_first, &infile_tok_last);
>  
>  	arg_tok_first = process_command_line_args(argc, argv);
Christian Couder· Feb 6, 2014, 20:19 UTC · re: Christian Couder · lore

[PATCH v5 10/14] trailer: execute command from 'trailer.<name>.command'

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 trailer.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
Show changes to trailer.c +63 −0
diff --git a/trailer.c b/trailer.c
index 108e104..98187fc 100644
--- a/trailer.c
+++ b/trailer.c
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "run-command.h"
 /*
  * Copyright (c) 2013 Christian Couder <chriscool@tuxfamily.org>
  */
@@ -12,11 +13,14 @@ struct conf_info {
 	char *name;
 	char *key;
 	char *command;
+	unsigned command_uses_arg : 1;
 	enum action_where where;
 	enum action_if_exist if_exist;
 	enum action_if_missing if_missing;
 };
 
+#define TRAILER_ARG_STRING "$ARG"
+
 struct trailer_item {
 	struct trailer_item *previous;
 	struct trailer_item *next;
@@ -56,6 +60,13 @@ static inline int contains_only_spaces(const char *str)
 	return !*s;
 }
 
+static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b)
+{
+	const char *ptr = strstr(sb->buf, a);
+	if (ptr)
+		strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));
+}
+
 static void print_tok_val(const char *tok, const char *val)
 {
 	char c = tok[strlen(tok) - 1];
@@ -375,6 +386,7 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)
 			if (conf->command)
 				warning(_("more than one %s"), orig_conf_key);
 			conf->command = xstrdup(value);
+			conf->command_uses_arg = !!strstr(conf->command, TRAILER_ARG_STRING);
 			break;
 		case TRAILER_WHERE:
 			if (set_where(conf, value))
@@ -411,6 +423,45 @@ static void parse_trailer(struct strbuf *tok, struct strbuf *val, const char *tr
 	}
 }
 
+static int read_from_command(struct child_process *cp, struct strbuf *buf)
+{
+	if (run_command(cp))
+		return error("running trailer command '%s' failed", cp->argv[0]);
+	if (strbuf_read(buf, cp->out, 1024) < 1)
+		return error("reading from trailer command '%s' failed", cp->argv[0]);
+	strbuf_trim(buf);
+	return 0;
+}
+
+static const char *apply_command(const char *command, const char *arg)
+{
+	struct strbuf cmd = STRBUF_INIT;
+	struct strbuf buf = STRBUF_INIT;
+	struct child_process cp;
+	const char *argv[] = {NULL, NULL};
+	const char *result = "";
+
+	strbuf_addstr(&cmd, command);
+	if (arg)
+		strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
+
+	argv[0] = cmd.buf;
+	memset(&cp, 0, sizeof(cp));
+	cp.argv = argv;
+	cp.env = local_repo_env;
+	cp.no_stdin = 1;
+	cp.out = -1;
+	cp.use_shell = 1;
+
+	if (read_from_command(&cp, &buf))
+		strbuf_release(&buf);
+	else
+		result = strbuf_detach(&buf, NULL);
+
+	strbuf_release(&cmd);
+	return result;
+}
+
 static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,
 					     const char* tok, const char* val)
 {
@@ -420,6 +471,8 @@ static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,
 	if (conf_item) {
 		new->conf = conf_item->conf;
 		new->token = xstrdup(conf_item->conf->key);
+		if (conf_item->conf->command_uses_arg || !val)
+			new->value = apply_command(conf_item->conf->command, val);
 	} else {
 		new->conf = xcalloc(sizeof(struct conf_info), 1);
 		new->token = tok;
@@ -470,12 +523,22 @@ static struct trailer_item *process_command_line_args(int argc, const char **arg
 	int i;
 	struct trailer_item *arg_tok_first = NULL;
 	struct trailer_item *arg_tok_last = NULL;
+	struct trailer_item *item;
 
 	for (i = 0; i < argc; i++) {
 		struct trailer_item *new = create_trailer_item(argv[i]);
 		add_trailer_item(&arg_tok_first, &arg_tok_last, new);
 	}
 
+	/* Add conf commands that don't use $ARG */
+	for (item = first_conf_item; item; item = item->next) {
+		if (item->conf->command && !item->conf->command_uses_arg)
+		{
+			struct trailer_item *new = new_trailer_item(item, NULL, NULL);
+			add_trailer_item(&arg_tok_first, &arg_tok_last, new);
+		}
+	}
+
 	return arg_tok_first;
 }
 
-- 
1.8.5.2.206.g98f5689.dirty
Junio C Hamano· Feb 7, 2014, 00:18 UTC · re: Christian Couder · lore

Re: [PATCH v5 10/14] trailer: execute command from 'trailer.<name>.command'

Christian Couder <chriscool@tuxfamily.org> writes:
> +#define TRAILER_ARG_STRING "$ARG"

No need to support users who may want to use a string that happens to match this substring literally as part of the command line?

Show 8 quoted lines
>  struct trailer_item {
>  	struct trailer_item *previous;
>  	struct trailer_item *next;
> @@ -56,6 +60,13 @@ static inline int contains_only_spaces(const char *str)
>  	return !*s;
>  }
>  
> +static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b)
Same comment about "inline" for an earlier patch applies.
Junio C Hamano· Feb 7, 2014, 18:17 UTC · re: Christian Couder · lore

Re: [PATCH v5 10/14] trailer: execute command from 'trailer.<name>.command'

Christian Couder <chriscool@tuxfamily.org> writes:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>

"execute command from ..." is fine, but I wish there were more details before S-o-b line. Is is not worth explaining what happens to the output, and what the facility is used for in general?

Is it to give a string used for the value part? "Key: Value" string? Is the command allowed to say "Put an entry with this string before the existing one, after the existing one, or replace the existing one"? Can it say "Upon inspection of the existing entry, it is no longer necessary to have it in the footer---remove it"?

Christian Couder· Feb 6, 2014, 20:20 UTC · re: Christian Couder · lore

[PATCH v5 11/14] trailer: add tests for trailer command

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 t/t7513-interpret-trailers.sh | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
Show changes to t/t7513-interpret-trailers.sh +27 −0
diff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh
index f5ef81f..2d50b7a 100755
--- a/t/t7513-interpret-trailers.sh
+++ b/t/t7513-interpret-trailers.sh
@@ -212,4 +212,31 @@ test_expect_success 'with input from stdin' '
 	test_cmp expected actual
 '
 
+test_expect_success 'with simple command' '
+	git config trailer.sign.key "Signed-off-by: " &&
+	git config trailer.sign.where "after" &&
+	git config trailer.sign.ifExist "addIfDifferentNeighbor" &&
+	git config trailer.sign.command "echo \"A U Thor <author@example.com>\"" &&
+	cat complex_message_body >expected &&
+	printf "Fixes: \nAcked-by= \nReviewed-by: \nSigned-off-by: \nSigned-off-by: A U Thor <author@example.com>\n" >>expected &&
+	git interpret-trailers "review:" "fix=22" < complex_message >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'setup a commit' '
+	echo "Content of the first commit." > a.txt &&
+	git add a.txt &&
+	git commit -m "Add file a.txt"
+'
+
+test_expect_success 'with command using $ARG' '
+	git config trailer.fix.ifExist "overwrite" &&
+	git config trailer.fix.command "git log -1 --oneline --format=\"%h (%s)\" --abbrev-commit --abbrev=14 \$ARG" &&
+	FIXED=$(git log -1 --oneline --format="%h (%s)" --abbrev-commit --abbrev=14 HEAD) &&
+	cat complex_message_body >expected &&
+	printf "Fixes: $FIXED\nAcked-by= \nReviewed-by: \nSigned-off-by: \nSigned-off-by: A U Thor <author@example.com>\n" >>expected &&
+	git interpret-trailers "review:" "fix=HEAD" < complex_message >actual &&
+	test_cmp expected actual
+'
+
 test_done
-- 
1.8.5.2.206.g98f5689.dirty
Christian Couder· Feb 6, 2014, 20:20 UTC · re: Christian Couder · lore

[PATCH v5 12/14] trailer: set author and committer env variables

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 trailer.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
Show changes to trailer.c +29 −1
diff --git a/trailer.c b/trailer.c
index 98187fc..b5de616 100644
--- a/trailer.c
+++ b/trailer.c
@@ -1,5 +1,6 @@
 #include "cache.h"
 #include "run-command.h"
+#include "argv-array.h"
 /*
  * Copyright (c) 2013 Christian Couder <chriscool@tuxfamily.org>
  */
@@ -433,14 +434,40 @@ static int read_from_command(struct child_process *cp, struct strbuf *buf)
 	return 0;
 }
 
+static void setup_ac_env(struct argv_array *env, const char *ac_name, const char *ac_mail, const char *(*read)(int))
+{
+	if (!getenv(ac_name) || !getenv(ac_mail)) {
+		struct ident_split ident;
+		const char *namebuf, *mailbuf;
+		int namelen, maillen;
+		const char *ac_info = read(IDENT_NO_DATE);
+
+		if (split_ident_line(&ident, ac_info, strlen(ac_info)))
+			return;
+
+		namelen = ident.name_end - ident.name_begin;
+		namebuf = ident.name_begin;
+
+		maillen = ident.mail_end - ident.mail_begin;
+		mailbuf = ident.mail_begin;
+
+		argv_array_pushf(env, "%s=%.*s", ac_name, namelen, namebuf);
+		argv_array_pushf(env, "%s=%.*s", ac_mail, maillen, mailbuf);
+	}
+}
+
 static const char *apply_command(const char *command, const char *arg)
 {
+	struct argv_array env = ARGV_ARRAY_INIT;
 	struct strbuf cmd = STRBUF_INIT;
 	struct strbuf buf = STRBUF_INIT;
 	struct child_process cp;
 	const char *argv[] = {NULL, NULL};
 	const char *result = "";
 
+	setup_ac_env(&env, "GIT_AUTHOR_NAME", "GIT_AUTHOR_EMAIL", git_author_info);
+	setup_ac_env(&env, "GIT_COMMITTER_NAME", "GIT_COMMITTER_EMAIL", git_committer_info);
+
 	strbuf_addstr(&cmd, command);
 	if (arg)
 		strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
@@ -448,7 +475,7 @@ static const char *apply_command(const char *command, const char *arg)
 	argv[0] = cmd.buf;
 	memset(&cp, 0, sizeof(cp));
 	cp.argv = argv;
-	cp.env = local_repo_env;
+	cp.env = env.argv;
 	cp.no_stdin = 1;
 	cp.out = -1;
 	cp.use_shell = 1;
@@ -459,6 +486,7 @@ static const char *apply_command(const char *command, const char *arg)
 		result = strbuf_detach(&buf, NULL);
 
 	strbuf_release(&cmd);
+	argv_array_clear(&env);
 	return result;
 }
 
-- 
1.8.5.2.206.g98f5689.dirty
Junio C Hamano· Feb 7, 2014, 00:20 UTC · re: Christian Couder · lore

Re: [PATCH v5 12/14] trailer: set author and committer env variables

Christian Couder <chriscool@tuxfamily.org> writes:
Show 27 quoted lines
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  trailer.c | 30 +++++++++++++++++++++++++++++-
>  1 file changed, 29 insertions(+), 1 deletion(-)
>
> diff --git a/trailer.c b/trailer.c
> index 98187fc..b5de616 100644
> --- a/trailer.c
> +++ b/trailer.c
> @@ -1,5 +1,6 @@
>  #include "cache.h"
>  #include "run-command.h"
> +#include "argv-array.h"
>  /*
>   * Copyright (c) 2013 Christian Couder <chriscool@tuxfamily.org>
>   */
> @@ -433,14 +434,40 @@ static int read_from_command(struct child_process *cp, struct strbuf *buf)
>  	return 0;
>  }
>  
> +static void setup_ac_env(struct argv_array *env, const char *ac_name, const char *ac_mail, const char *(*read)(int))
> +{
> +	if (!getenv(ac_name) || !getenv(ac_mail)) {
> +		struct ident_split ident;
> +		const char *namebuf, *mailbuf;
> +		int namelen, maillen;
> +		const char *ac_info = read(IDENT_NO_DATE);
This is far too confusing.  Name it read_fn() or something.
Show 45 quoted lines
> +		if (split_ident_line(&ident, ac_info, strlen(ac_info)))
> +			return;
> +
> +		namelen = ident.name_end - ident.name_begin;
> +		namebuf = ident.name_begin;
> +
> +		maillen = ident.mail_end - ident.mail_begin;
> +		mailbuf = ident.mail_begin;
> +
> +		argv_array_pushf(env, "%s=%.*s", ac_name, namelen, namebuf);
> +		argv_array_pushf(env, "%s=%.*s", ac_mail, maillen, mailbuf);
> +	}
> +}
> +
>  static const char *apply_command(const char *command, const char *arg)
>  {
> +	struct argv_array env = ARGV_ARRAY_INIT;
>  	struct strbuf cmd = STRBUF_INIT;
>  	struct strbuf buf = STRBUF_INIT;
>  	struct child_process cp;
>  	const char *argv[] = {NULL, NULL};
>  	const char *result = "";
>  
> +	setup_ac_env(&env, "GIT_AUTHOR_NAME", "GIT_AUTHOR_EMAIL", git_author_info);
> +	setup_ac_env(&env, "GIT_COMMITTER_NAME", "GIT_COMMITTER_EMAIL", git_committer_info);
> +
>  	strbuf_addstr(&cmd, command);
>  	if (arg)
>  		strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
> @@ -448,7 +475,7 @@ static const char *apply_command(const char *command, const char *arg)
>  	argv[0] = cmd.buf;
>  	memset(&cp, 0, sizeof(cp));
>  	cp.argv = argv;
> -	cp.env = local_repo_env;
> +	cp.env = env.argv;
>  	cp.no_stdin = 1;
>  	cp.out = -1;
>  	cp.use_shell = 1;
> @@ -459,6 +486,7 @@ static const char *apply_command(const char *command, const char *arg)
>  		result = strbuf_detach(&buf, NULL);
>  
>  	strbuf_release(&cmd);
> +	argv_array_clear(&env);
>  	return result;
>  }
Junio C Hamano· Feb 7, 2014, 00:26 UTC · re: Christian Couder · lore

Re: [PATCH v5 12/14] trailer: set author and committer env variables

Christian Couder <chriscool@tuxfamily.org> writes:
Show 23 quoted lines
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  trailer.c | 30 +++++++++++++++++++++++++++++-
>  1 file changed, 29 insertions(+), 1 deletion(-)
>
> diff --git a/trailer.c b/trailer.c
> index 98187fc..b5de616 100644
> --- a/trailer.c
> +++ b/trailer.c
> @@ -1,5 +1,6 @@
>  #include "cache.h"
>  #include "run-command.h"
> +#include "argv-array.h"
>  /*
>   * Copyright (c) 2013 Christian Couder <chriscool@tuxfamily.org>
>   */
> @@ -433,14 +434,40 @@ static int read_from_command(struct child_process *cp, struct strbuf *buf)
>  	return 0;
>  }
>  
> +static void setup_ac_env(struct argv_array *env, const char *ac_name, const char *ac_mail, const char *(*read)(int))
> +{
> +	if (!getenv(ac_name) || !getenv(ac_mail)) {

Whoever is using this tool while replaying an existing commit likely wants to export these two variables _into_ this command from the outside, and this function will not interfere with it.

But for other uses, do we need to export these variables? After all, this matters _only_ when the command we spawn wants to know the idents to be used, but they can ask git-var themselves to cover both cases, whether the environment that called this tool already had the ident environment variables or it didn't.

So I am not sure what value this step is adding to the series.
Show 31 quoted lines
> +		struct ident_split ident;
> +		const char *namebuf, *mailbuf;
> +		int namelen, maillen;
> +		const char *ac_info = read(IDENT_NO_DATE);
> +
> +		if (split_ident_line(&ident, ac_info, strlen(ac_info)))
> +			return;
> +
> +		namelen = ident.name_end - ident.name_begin;
> +		namebuf = ident.name_begin;
> +
> +		maillen = ident.mail_end - ident.mail_begin;
> +		mailbuf = ident.mail_begin;
> +
> +		argv_array_pushf(env, "%s=%.*s", ac_name, namelen, namebuf);
> +		argv_array_pushf(env, "%s=%.*s", ac_mail, maillen, mailbuf);
> +	}
> +}
> +
>  static const char *apply_command(const char *command, const char *arg)
>  {
> +	struct argv_array env = ARGV_ARRAY_INIT;
>  	struct strbuf cmd = STRBUF_INIT;
>  	struct strbuf buf = STRBUF_INIT;
>  	struct child_process cp;
>  	const char *argv[] = {NULL, NULL};
>  	const char *result = "";
>  
> +	setup_ac_env(&env, "GIT_AUTHOR_NAME", "GIT_AUTHOR_EMAIL", git_author_info);
> +	setup_ac_env(&env, "GIT_COMMITTER_NAME", "GIT_COMMITTER_EMAIL", git_committer_info);
> +
Show 19 quoted lines
>  	strbuf_addstr(&cmd, command);
>  	if (arg)
>  		strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
> @@ -448,7 +475,7 @@ static const char *apply_command(const char *command, const char *arg)
>  	argv[0] = cmd.buf;
>  	memset(&cp, 0, sizeof(cp));
>  	cp.argv = argv;
> -	cp.env = local_repo_env;
> +	cp.env = env.argv;
>  	cp.no_stdin = 1;
>  	cp.out = -1;
>  	cp.use_shell = 1;
> @@ -459,6 +486,7 @@ static const char *apply_command(const char *command, const char *arg)
>  		result = strbuf_detach(&buf, NULL);
>  
>  	strbuf_release(&cmd);
> +	argv_array_clear(&env);
>  	return result;
>  }
Christian Couder· Feb 6, 2014, 20:20 UTC · re: Christian Couder · lore

[PATCH v5 13/14] trailer: add tests for commands using env variables

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 t/t7513-interpret-trailers.sh | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
Show changes to t/t7513-interpret-trailers.sh +20 −0
diff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh
index 2d50b7a..00894a8 100755
--- a/t/t7513-interpret-trailers.sh
+++ b/t/t7513-interpret-trailers.sh
@@ -223,6 +223,26 @@ test_expect_success 'with simple command' '
 	test_cmp expected actual
 '
 
+test_expect_success 'with command using commiter information' '
+	git config trailer.sign.ifExist "addIfDifferent" &&
+	git config trailer.sign.command "echo \"\$GIT_COMMITTER_NAME <\$GIT_COMMITTER_EMAIL>\"" &&
+	cat complex_message_body >expected &&
+	printf "Fixes: \nAcked-by= \nReviewed-by: \nSigned-off-by: \nSigned-off-by: C O Mitter <committer@example.com>\n" >>expected &&
+	git interpret-trailers "review:" "fix=22" < complex_message >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'with command using author information' '
+	git config trailer.sign.key "Signed-off-by: " &&
+	git config trailer.sign.where "after" &&
+	git config trailer.sign.ifExist "addIfDifferentNeighbor" &&
+	git config trailer.sign.command "echo \"\$GIT_AUTHOR_NAME <\$GIT_AUTHOR_EMAIL>\"" &&
+	cat complex_message_body >expected &&
+	printf "Fixes: \nAcked-by= \nReviewed-by: \nSigned-off-by: \nSigned-off-by: A U Thor <author@example.com>\n" >>expected &&
+	git interpret-trailers "review:" "fix=22" < complex_message >actual &&
+	test_cmp expected actual
+'
+
 test_expect_success 'setup a commit' '
 	echo "Content of the first commit." > a.txt &&
 	git add a.txt &&
-- 
1.8.5.2.206.g98f5689.dirty
Christian Couder· Feb 6, 2014, 20:20 UTC · re: Christian Couder · lore

[PATCH v5 14/14] Documentation: add documentation for 'git interpret-trailers'

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 Documentation/git-interpret-trailers.txt | 132 +++++++++++++++++++++++++++++++
 1 file changed, 132 insertions(+)
 create mode 100644 Documentation/git-interpret-trailers.txt
Show changes to Documentation/git-interpret-trailers.txt +132 −0
diff --git a/Documentation/git-interpret-trailers.txt b/Documentation/git-interpret-trailers.txt
new file mode 100644
index 0000000..0617941
--- /dev/null
+++ b/Documentation/git-interpret-trailers.txt
@@ -0,0 +1,132 @@
+git-interpret-trailers(1)
+=========================
+
+NAME
+----
+git-interpret-trailers - help add stuctured information into commit messages
+
+SYNOPSIS
+--------
+[verse]
+'git interpret-trailers' [--trim-empty] [--infile=<file>] [(<token>[(=|:)<value>])...]
+
+DESCRIPTION
+-----------
+Help add RFC 822-like headers, called 'trailers', at the end of the
+otherwise free-form part of a commit message.
+
+Unless `--infile=<file>` is used, this command is a filter. It reads
+the standard input for a commit message and applies the `token`
+arguments, if any, to this message. The resulting message is emited on
+the standard output.
+
+Some configuration variables control the way the `token` arguments are
+applied to the message and the way any existing trailer in the message
+is changed. They also make it possible to automatically add some
+trailers.
+
+By default, a 'token=value' or 'token:value' argument will be added
+only if no trailer with the same (token, value) pair is already in the
+message. The 'token' and 'value' parts will be trimmed to remove
+starting and trailing whitespace, and the resulting trimmed 'token'
+and 'value' will appear in the message like this:
+
+------------------------------------------------
+token: value
+------------------------------------------------
+
+By default, if there are already trailers with the same 'token' the
+new trailer will appear just after the last trailer with the same
+'token'. Otherwise it will appear at the end of the message.
+
+Note that 'trailers' do not follow and are not intended to follow many
+rules that are in RFC 822. For example they do not follow the line
+breaking rules, the encoding rules and probably many other rules.
+
+OPTIONS
+-------
+--trim-empty::
+	If the 'value' part of any trailer contains only whitespace,
+	the whole trailer will be removed from the resulting message.
+
+----infile=<file>::
+	Read the commit message from `file` instead of the standard
+	input.
+
+CONFIGURATION VARIABLES
+-----------------------
+
+trailer.<token>.key::
+	This 'key' will be used instead of 'token' in the
+	trailer. After some alphanumeric characters, it can contain
+	some non alphanumeric characters like ':', '=' or '#' that will
+	be used instead of ':' to separate the token from the value in
+	the trailer, though the default ':' is more standard.
+
+trailer.<token>.where::
+	This can be either `after`, which is the default, or
+	`before`. If it is `before`, then a trailer with the specified
+	token, will appear before, instead of after, other trailers
+	with the same token, or otherwise at the beginning, instead of
+	at the end, of all the trailers.
+
+trailer.<token>.ifexist::
+	This option makes it possible to choose what action will be
+	performed when there is already at least one trailer with the
+	same token in the message.
++
+The valid values for this option are: `addIfDifferent` (this is the
+default), `addIfDifferentNeighbor`, `add`, `overwrite` or `doNothing`.
++
+With `addIfDifferent`, a new trailer will be added only if no trailer
+with the same (token, value) pair is already in the message.
++
+With `addIfDifferentNeighbor`, a new trailer will be added only if no
+trailer with the same (token, value) pair is above or below the line
+where the new trailer will be added.
++
+With `add`, a new trailer will be added, even if some trailers with
+the same (token, value) pair are already in the message.
++
+With `overwrite`, the new trailer will overwrite an existing trailer
+with the same token.
++
+With `doNothing`, nothing will be done, that is no new trailer will be
+added if there is already one with the same token in the message.
+
+trailer.<token>.ifmissing::
+	This option makes it possible to choose what action will be
+	performed when there is not yet any trailer with the same
+	token in the message.
++
+The valid values for this option are: `add` (this is the default) and
+`doNothing`.
++
+With `add`, a new trailer will be added.
++
+With `doNothing`, nothing will be done.
+
+trailer.<token>.command::
+	This option can be used to specify a shell command that will
+	be used to automatically add or modify a trailer with the
+	specified 'token'.
++
+When this option is specified, it is like if a special 'token=value'
+argument is added at the end of the command line, where 'value' will
+be given by the standard output of the specified command.
++
+If the command contains the `$ARG` string, this string will be
+replaced with the 'value' part of an existing trailer with the same
+token, if any, before the command is launched.
++
+The following environment variables are set when the command is run:
+GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, GIT_COMMITTER_NAME,
+GIT_COMMITTER_EMAIL.
+
+SEE ALSO
+--------
+linkgit:git-commit[1]
+
+GIT
+---
+Part of the linkgit:git[1] suite
-- 
1.8.5.2.206.g98f5689.dirty
Eric Sunshine· Feb 10, 2014, 07:17 UTC · re: Christian Couder · lore

Re: [PATCH v5 14/14] Documentation: add documentation for 'git interpret-trailers'

On Thu, Feb 6, 2014 at 3:20 PM, Christian Couder <chriscool@tuxfamily.org> wrote:

Show 17 quoted lines
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/Documentation/git-interpret-trailers.txt b/Documentation/git-interpret-trailers.txt
> new file mode 100644
> index 0000000..0617941
> --- /dev/null
> +++ b/Documentation/git-interpret-trailers.txt
> @@ -0,0 +1,132 @@
> +OPTIONS
> +-------
> +--trim-empty::
> +       If the 'value' part of any trailer contains only whitespace,
> +       the whole trailer will be removed from the resulting message.
> +
> +----infile=<file>::
> +       Read the commit message from `file` instead of the standard
> +       input.
s/----/--/
> +
> +CONFIGURATION VARIABLES
> +-----------------------

← back to recent threads