{"thread":{"id":"35765","subject":"[PATCH v4 00/17] Add interpret-trailers builtin","startedAt":"2014-01-30T06:49:03Z","lastAt":"2014-02-09T13:55:45Z","messageCount":33,"participants":["Christian Couder","Eric Sunshine","Junio C Hamano"],"isPatch":true,"patchVersion":4,"patchTotal":17},"messages":[{"id":"233919","messageId":"20140130064217.7504.473.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":null,"subject":"[PATCH v4 00/17] Add interpret-trailers builtin","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:03Z","receivedAt":"2014-01-30T06:49:03Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This patch series implements a new command:\n\n        git interpret-trailers\n\nand an infrastructure to process trailers that can be reused,\nfor example in \"commit.c\".\n\n1) Rationale:\n\nThis command should help with RFC 822 style headers, called\n\"trailers\", that are found at the end of commit messages.\n\n(Note that these headers do not follow and are not intended to\nfollow many rules that are in RFC 822. For example they do not\nfollow the line breaking rules, the encoding rules and probably\nmany other rules.)\n\nFor a long time, these trailers have become a de facto standard\nway to add helpful information into commit messages.\n\nUntil now git commit has only supported the well known\n\"Signed-off-by: \" trailer, that is used by many projects like\nthe Linux kernel and Git.\n\nIt is better to implement features for these trailers first in a\nnew command rather than in builtin/commit.c, because this way the\nprepare-commit-msg and commit-msg hooks can reuse this command.\n\n2) Current state:\n\nCurrently the usage string of this command is:\n\ngit interpret-trailers [--trim-empty] [--infile=<file>] [(<token>[(=|:)<value>])...]\n\nThe following features are implemented:\n\n        - the result is printed on stdout\n        - the [<token>[=<value>]>] arguments are interpreted\n        - a commit message passed using the \"--infile=file\" option is interpreted\n        - if \"--infile\" is not used, a commit message is read from stdin\n        - the \"trailer.<token>.key\" options in the config are interpreted\n        - the \"trailer.<token>.where\" options are interpreted\n        - the \"trailer.<token>.ifExist\" options are interpreted\n        - the \"trailer.<token>.ifMissing\" options are interpreted\n        - the \"trailer.<token>.command\" config works\n        - $ARG can be used in commands\n        - ditto for GIT_{AUTHOR,COMMITTER}_{NAME,EMAIL} env variables\n        - there are some tests\n        - there is some documentation\n\nThe following features are planned but not yet implemented:\n        - add more tests related to commands\n        - add examples in documentation\n        - integration with \"git commit\"\n\nPossible improvements:\n        - support GIT_COMMIT_PROTO env variable in commands\n\n3) Changes since version 3, thanks to Eric and Junio:\n\n* the usage string/synopsis of the command was improved\n* some spelling/wording mistakes in the doc were fixed\n* some style issues were fixed\n\nChristian Couder (17):\n  Add data structures and basic functions for commit trailers\n  trailer: process trailers from file and arguments\n  trailer: read and process config information\n  trailer: process command line trailer arguments\n  strbuf: add strbuf_isspace()\n  trailer: parse trailers from input file\n  trailer: put all the processing together and print\n  trailer: add interpret-trailers command\n  trailer: add tests for \"git interpret-trailers\"\n  trailer: if no input file is passed, read from stdin\n  trailer: add new_trailer_item() function\n  strbuf: add strbuf_replace()\n  trailer: execute command from 'trailer.<name>.command'\n  trailer: add tests for trailer command\n  trailer: set author and committer env variables\n  trailer: add tests for commands using env variables\n  Documentation: add documentation for 'git interpret-trailers'\n\n .gitignore                               |   1 +\n Documentation/git-interpret-trailers.txt | 132 +++++++\n Makefile                                 |   2 +\n builtin.h                                |   1 +\n builtin/interpret-trailers.c             |  36 ++\n git.c                                    |   1 +\n strbuf.c                                 |  14 +\n strbuf.h                                 |   4 +\n t/t7513-interpret-trailers.sh            | 262 +++++++++++++\n trailer.c                                | 637 +++++++++++++++++++++++++++++++\n trailer.h                                |   6 +\n 11 files changed, 1096 insertions(+)\n create mode 100644 Documentation/git-interpret-trailers.txt\n create mode 100644 builtin/interpret-trailers.c\n create mode 100755 t/t7513-interpret-trailers.sh\n create mode 100644 trailer.c\n create mode 100644 trailer.h\n\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233920","messageId":"20140130064921.7504.87498.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 01/17] Add data structures and basic functions for commit trailers","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:04Z","receivedAt":"2014-01-30T06:49:04Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"We will use a doubly linked list to store all information\nabout trailers and their configuration.\n\nThis way we can easily remove or add trailers to or from\ntrailer lists while traversing the lists in either direction.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n Makefile  |  1 +\n trailer.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 49 insertions(+)\n create mode 100644 trailer.c\n\ndiff --git a/Makefile b/Makefile\nindex b4af1e2..ec90feb 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -871,6 +871,7 @@ LIB_OBJS += submodule.o\n LIB_OBJS += symlinks.o\n LIB_OBJS += tag.o\n LIB_OBJS += trace.o\n+LIB_OBJS += trailer.o\n LIB_OBJS += transport.o\n LIB_OBJS += transport-helper.o\n LIB_OBJS += tree-diff.o\ndiff --git a/trailer.c b/trailer.c\nnew file mode 100644\nindex 0000000..aed25e1\n--- /dev/null\n+++ b/trailer.c\n@@ -0,0 +1,48 @@\n+#include \"cache.h\"\n+/*\n+ * Copyright (c) 2013 Christian Couder <chriscool@tuxfamily.org>\n+ */\n+\n+enum action_where { WHERE_AFTER, WHERE_BEFORE };\n+enum action_if_exist { EXIST_ADD_IF_DIFFERENT, EXIST_ADD_IF_DIFFERENT_NEIGHBOR,\n+\t\t       EXIST_ADD, EXIST_OVERWRITE, EXIST_DO_NOTHING };\n+enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };\n+\n+struct conf_info {\n+\tchar *name;\n+\tchar *key;\n+\tchar *command;\n+\tenum action_where where;\n+\tenum action_if_exist if_exist;\n+\tenum action_if_missing if_missing;\n+};\n+\n+struct trailer_item {\n+\tstruct trailer_item *previous;\n+\tstruct trailer_item *next;\n+\tconst char *token;\n+\tconst char *value;\n+\tstruct conf_info *conf;\n+};\n+\n+static int same_token(struct trailer_item *a, struct trailer_item *b, int alnum_len)\n+{\n+\treturn !strncasecmp(a->token, b->token, alnum_len);\n+}\n+\n+static int same_value(struct trailer_item *a, struct trailer_item *b)\n+{\n+\treturn !strcasecmp(a->value, b->value);\n+}\n+\n+static int same_trailer(struct trailer_item *a, struct trailer_item *b, int alnum_len)\n+{\n+\treturn same_token(a, b, alnum_len) && same_value(a, b);\n+}\n+\n+/* Get the length of buf from its beginning until its last alphanumeric character */\n+static size_t alnum_len(const char *buf, size_t len)\n+{\n+\twhile (--len >= 0 && !isalnum(buf[len]));\n+\treturn len + 1;\n+}\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233921","messageId":"20140130064921.7504.60878.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 02/17] trailer: process trailers from file and arguments","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:05Z","receivedAt":"2014-01-30T06:49:05Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This patch implements the logic that process trailers\nfrom file and arguments.\n\nAt the beginning trailers from file are in their own\ninfile_tok doubly linked list, and trailers from\narguments are in their own arg_tok doubly linked list.\n\nThe lists are traversed and when an arg_tok should be\n\"applied\", it is removed from its list and inserted\ninto the infile_tok list.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n trailer.c | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 189 insertions(+)\n\ndiff --git a/trailer.c b/trailer.c\nindex aed25e1..e9ccfa5 100644\n--- a/trailer.c\n+++ b/trailer.c\n@@ -46,3 +46,192 @@ static size_t alnum_len(const char *buf, size_t len)\n \twhile (--len >= 0 && !isalnum(buf[len]));\n \treturn len + 1;\n }\n+\n+static void add_arg_to_infile(struct trailer_item *infile_tok,\n+\t\t\t      struct trailer_item *arg_tok)\n+{\n+\tif (arg_tok->conf->where == WHERE_AFTER) {\n+\t\targ_tok->next = infile_tok->next;\n+\t\tinfile_tok->next = arg_tok;\n+\t\targ_tok->previous = infile_tok;\n+\t\tif (arg_tok->next)\n+\t\t\targ_tok->next->previous = arg_tok;\n+\t} else {\n+\t\targ_tok->previous = infile_tok->previous;\n+\t\tinfile_tok->previous = arg_tok;\n+\t\targ_tok->next = infile_tok;\n+\t\tif (arg_tok->previous)\n+\t\t\targ_tok->previous->next = arg_tok;\n+\t}\n+}\n+\n+static int check_if_different(struct trailer_item *infile_tok,\n+\t\t\t      struct trailer_item *arg_tok,\n+\t\t\t      int alnum_len, int check_all)\n+{\n+\tenum action_where where = arg_tok->conf->where;\n+\tdo {\n+\t\tif (!infile_tok)\n+\t\t\treturn 1;\n+\t\tif (same_trailer(infile_tok, arg_tok, alnum_len))\n+\t\t\treturn 0;\n+\t\t/*\n+\t\t * if we want to add a trailer after another one,\n+\t\t * we have to check those before this one\n+\t\t */\n+\t\tinfile_tok = (where == WHERE_AFTER) ? infile_tok->previous : infile_tok->next;\n+\t} while (check_all);\n+\treturn 1;\n+}\n+\n+static void apply_arg_if_exist(struct trailer_item *infile_tok,\n+\t\t\t       struct trailer_item *arg_tok,\n+\t\t\t       int alnum_len)\n+{\n+\tswitch (arg_tok->conf->if_exist) {\n+\tcase EXIST_DO_NOTHING:\n+\t\tfree(arg_tok);\n+\t\tbreak;\n+\tcase EXIST_OVERWRITE:\n+\t\tfree((char *)infile_tok->value);\n+\t\tinfile_tok->value = xstrdup(arg_tok->value);\n+\t\tfree(arg_tok);\n+\t\tbreak;\n+\tcase EXIST_ADD:\n+\t\tadd_arg_to_infile(infile_tok, arg_tok);\n+\t\tbreak;\n+\tcase EXIST_ADD_IF_DIFFERENT:\n+\t\tif (check_if_different(infile_tok, arg_tok, alnum_len, 1))\n+\t\t\tadd_arg_to_infile(infile_tok, arg_tok);\n+\t\telse\n+\t\t\tfree(arg_tok);\n+\t\tbreak;\n+\tcase EXIST_ADD_IF_DIFFERENT_NEIGHBOR:\n+\t\tif (check_if_different(infile_tok, arg_tok, alnum_len, 0))\n+\t\t\tadd_arg_to_infile(infile_tok, arg_tok);\n+\t\telse\n+\t\t\tfree(arg_tok);\n+\t\tbreak;\n+\t}\n+}\n+\n+static void remove_from_list(struct trailer_item *item,\n+\t\t\t     struct trailer_item **first)\n+{\n+\tif (item->next)\n+\t\titem->next->previous = item->previous;\n+\tif (item->previous)\n+\t\titem->previous->next = item->next;\n+\telse\n+\t\t*first = item->next;\n+}\n+\n+static struct trailer_item *remove_first(struct trailer_item **first)\n+{\n+\tstruct trailer_item *item = *first;\n+\t*first = item->next;\n+\tif (item->next) {\n+\t\titem->next->previous = NULL;\n+\t\titem->next = NULL;\n+\t}\n+\treturn item;\n+}\n+\n+static void process_infile_tok(struct trailer_item *infile_tok,\n+\t\t\t       struct trailer_item **arg_tok_first,\n+\t\t\t       enum action_where where)\n+{\n+\tstruct trailer_item *arg_tok;\n+\tstruct trailer_item *next_arg;\n+\n+\tint tok_alnum_len = alnum_len(infile_tok->token, strlen(infile_tok->token));\n+\tfor (arg_tok = *arg_tok_first; arg_tok; arg_tok = next_arg) {\n+\t\tnext_arg = arg_tok->next;\n+\t\tif (same_token(infile_tok, arg_tok, tok_alnum_len) &&\n+\t\t    arg_tok->conf->where == where) {\n+\t\t\t/* Remove arg_tok from list */\n+\t\t\tremove_from_list(arg_tok, arg_tok_first);\n+\t\t\t/* Apply arg */\n+\t\t\tapply_arg_if_exist(infile_tok, arg_tok, tok_alnum_len);\n+\t\t\t/*\n+\t\t\t * If arg has been added to infile,\n+\t\t\t * then we need to process it too now.\n+\t\t\t */\n+\t\t\tif ((where == WHERE_AFTER ? infile_tok->next : infile_tok->previous) == arg_tok)\n+\t\t\t\tinfile_tok = arg_tok;\n+\t\t}\n+\t}\n+}\n+\n+static void update_last(struct trailer_item **last)\n+{\n+\tif (*last)\n+\t\twhile((*last)->next != NULL)\n+\t\t\t*last = (*last)->next;\n+}\n+\n+static void update_first(struct trailer_item **first)\n+{\n+\tif (*first)\n+\t\twhile((*first)->previous != NULL)\n+\t\t\t*first = (*first)->previous;\n+}\n+\n+static void apply_arg_if_missing(struct trailer_item **infile_tok_first,\n+\t\t\t\t struct trailer_item **infile_tok_last,\n+\t\t\t\t struct trailer_item *arg_tok)\n+{\n+\tstruct trailer_item **infile_tok;\n+\tenum action_where where;\n+\n+\tswitch (arg_tok->conf->if_missing) {\n+\tcase MISSING_DO_NOTHING:\n+\t\tfree(arg_tok);\n+\t\tbreak;\n+\tcase MISSING_ADD:\n+\t\twhere = arg_tok->conf->where;\n+\t\tinfile_tok = (where == WHERE_AFTER) ? infile_tok_last : infile_tok_first;\n+\t\tif (*infile_tok) {\n+\t\t\tadd_arg_to_infile(*infile_tok, arg_tok);\n+\t\t\t*infile_tok = arg_tok;\n+\t\t} else {\n+\t\t\t*infile_tok_first = arg_tok;\n+\t\t\t*infile_tok_last = arg_tok;\n+\t\t}\n+\t\tbreak;\n+\t}\n+}\n+\n+static void process_trailers_lists(struct trailer_item **infile_tok_first,\n+\t\t\t\t   struct trailer_item **infile_tok_last,\n+\t\t\t\t   struct trailer_item **arg_tok_first)\n+{\n+\tstruct trailer_item *infile_tok;\n+\tstruct trailer_item *arg_tok;\n+\n+\tif (!*arg_tok_first)\n+\t\treturn;\n+\n+\t/* Process infile from end to start */\n+\tfor (infile_tok = *infile_tok_last; infile_tok; infile_tok = infile_tok->previous) {\n+\t\tprocess_infile_tok(infile_tok, arg_tok_first, WHERE_AFTER);\n+\t}\n+\n+\tupdate_last(infile_tok_last);\n+\n+\tif (!*arg_tok_first)\n+\t\treturn;\n+\n+\t/* Process infile from start to end */\n+\tfor (infile_tok = *infile_tok_first; infile_tok; infile_tok = infile_tok->next) {\n+\t\tprocess_infile_tok(infile_tok, arg_tok_first, WHERE_BEFORE);\n+\t}\n+\n+\tupdate_first(infile_tok_first);\n+\n+\t/* Process args left */\n+\twhile (*arg_tok_first) {\n+\t\targ_tok = remove_first(arg_tok_first);\n+\t\tapply_arg_if_missing(infile_tok_first, infile_tok_last, arg_tok);\n+\t}\n+}\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233924","messageId":"20140130064921.7504.3711.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 03/17] trailer: read and process config information","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:06Z","receivedAt":"2014-01-30T06:49:06Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This patch implements reading the configuration\nto get trailer information, and then processing\nit and storing it in a doubly linked list.\n\nThe config information is stored in the list\nwhose first item is pointed to by:\n\nstatic struct trailer_item *first_conf_item;\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n trailer.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 127 insertions(+)\n\ndiff --git a/trailer.c b/trailer.c\nindex e9ccfa5..d979a0f 100644\n--- a/trailer.c\n+++ b/trailer.c\n@@ -25,6 +25,8 @@ struct trailer_item {\n \tstruct conf_info *conf;\n };\n \n+static struct trailer_item *first_conf_item;\n+\n static int same_token(struct trailer_item *a, struct trailer_item *b, int alnum_len)\n {\n \treturn !strncasecmp(a->token, b->token, alnum_len);\n@@ -235,3 +237,128 @@ static void process_trailers_lists(struct trailer_item **infile_tok_first,\n \t\tapply_arg_if_missing(infile_tok_first, infile_tok_last, arg_tok);\n \t}\n }\n+\n+static int set_where(struct conf_info *item, const char *value)\n+{\n+\tif (!strcasecmp(\"after\", value))\n+\t\titem->where = WHERE_AFTER;\n+\telse if (!strcasecmp(\"before\", value))\n+\t\titem->where = WHERE_BEFORE;\n+\telse\n+\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static int set_if_exist(struct conf_info *item, const char *value)\n+{\n+\tif (!strcasecmp(\"addIfDifferent\", value))\n+\t\titem->if_exist = EXIST_ADD_IF_DIFFERENT;\n+\telse if (!strcasecmp(\"addIfDifferentNeighbor\", value))\n+\t\titem->if_exist = EXIST_ADD_IF_DIFFERENT_NEIGHBOR;\n+\telse if (!strcasecmp(\"add\", value))\n+\t\titem->if_exist = EXIST_ADD;\n+\telse if (!strcasecmp(\"overwrite\", value))\n+\t\titem->if_exist = EXIST_OVERWRITE;\n+\telse if (!strcasecmp(\"doNothing\", value))\n+\t\titem->if_exist = EXIST_DO_NOTHING;\n+\telse\n+\t\treturn 1;\n+\treturn 0;\n+}\n+\n+static int set_if_missing(struct conf_info *item, const char *value)\n+{\n+\tif (!strcasecmp(\"doNothing\", value))\n+\t\titem->if_missing = MISSING_DO_NOTHING;\n+\telse if (!strcasecmp(\"add\", value))\n+\t\titem->if_missing = MISSING_ADD;\n+\telse\n+\t\treturn 1;\n+\treturn 0;\n+}\n+\n+enum trailer_info_type { TRAILER_VALUE, TRAILER_COMMAND, TRAILER_WHERE,\n+\t\t\t TRAILER_IF_EXIST, TRAILER_IF_MISSING };\n+\n+static int set_name_and_type(const char *conf_key, const char *suffix,\n+\t\t\t     enum trailer_info_type type,\n+\t\t\t     char **pname, enum trailer_info_type *ptype)\n+{\n+\tint ret = ends_with(conf_key, suffix);\n+\tif (ret) {\n+\t\t*pname = xstrndup(conf_key, strlen(conf_key) - strlen(suffix));\n+\t\t*ptype = type;\n+\t}\n+\treturn ret;\n+}\n+\n+static struct trailer_item *get_conf_item(char *name)\n+{\n+\tstruct trailer_item *item;\n+\tstruct trailer_item *previous;\n+\n+\t/* Look up item with same name */\n+\tfor (previous = NULL, item = first_conf_item;\n+\t     item;\n+\t     previous = item, item = item->next) {\n+\t\tif (!strcasecmp(item->conf->name, name))\n+\t\t\treturn item;\n+\t}\n+\n+\t/* Item does not already exists, create it */\n+\titem = xcalloc(sizeof(struct trailer_item), 1);\n+\titem->conf = xcalloc(sizeof(struct conf_info), 1);\n+\titem->conf->name = xstrdup(name);\n+\n+\tif (!previous)\n+\t\tfirst_conf_item = item;\n+\telse {\n+\t\tprevious->next = item;\n+\t\titem->previous = previous;\n+\t}\n+\n+\treturn item;\n+}\n+\n+static int git_trailer_config(const char *conf_key, const char *value, void *cb)\n+{\n+\tif (starts_with(conf_key, \"trailer.\")) {\n+\t\tconst char *orig_conf_key = conf_key;\n+\t\tstruct trailer_item *item;\n+\t\tstruct conf_info *conf;\n+\t\tchar *name;\n+\t\tenum trailer_info_type type;\n+\n+\t\tconf_key += 8;\n+\t\tif (!set_name_and_type(conf_key, \".key\", TRAILER_VALUE, &name, &type) &&\n+\t\t    !set_name_and_type(conf_key, \".command\", TRAILER_COMMAND, &name, &type) &&\n+\t\t    !set_name_and_type(conf_key, \".where\", TRAILER_WHERE, &name, &type) &&\n+\t\t    !set_name_and_type(conf_key, \".ifexist\", TRAILER_IF_EXIST, &name, &type) &&\n+\t\t    !set_name_and_type(conf_key, \".ifmissing\", TRAILER_IF_MISSING, &name, &type))\n+\t\t\treturn 0;\n+\n+\t\titem = get_conf_item(name);\n+\t\tconf = item->conf;\n+\n+\t\tif (type == TRAILER_VALUE) {\n+\t\t\tif (conf->key)\n+\t\t\t\twarning(_(\"more than one %s\"), orig_conf_key);\n+\t\t\tconf->key = xstrdup(value);\n+\t\t} else if (type == TRAILER_COMMAND) {\n+\t\t\tif (conf->command)\n+\t\t\t\twarning(_(\"more than one %s\"), orig_conf_key);\n+\t\t\tconf->command = xstrdup(value);\n+\t\t} else if (type == TRAILER_WHERE) {\n+\t\t\tif (set_where(conf, value))\n+\t\t\t\twarning(_(\"unknown value '%s' for key '%s'\"), value, orig_conf_key);\n+\t\t} else if (type == TRAILER_IF_EXIST) {\n+\t\t\tif (set_if_exist(conf, value))\n+\t\t\t\twarning(_(\"unknown value '%s' for key '%s'\"), value, orig_conf_key);\n+\t\t} else if (type == TRAILER_IF_MISSING) {\n+\t\t\tif (set_if_missing(conf, value))\n+\t\t\t\twarning(_(\"unknown value '%s' for key '%s'\"), value, orig_conf_key);\n+\t\t} else\n+\t\t\tdie(\"internal bug in trailer.c\");\n+\t}\n+\treturn 0;\n+}\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233934","messageId":"20140130064921.7504.55916.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 04/17] trailer: process command line trailer arguments","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:07Z","receivedAt":"2014-01-30T06:49:07Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This patch parses the trailer command line arguments\nand put the result into an arg_tok doubly linked\nlist.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n trailer.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 77 insertions(+)\n\ndiff --git a/trailer.c b/trailer.c\nindex d979a0f..f48fd94 100644\n--- a/trailer.c\n+++ b/trailer.c\n@@ -362,3 +362,80 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)\n \t}\n \treturn 0;\n }\n+\n+static void parse_trailer(struct strbuf *tok, struct strbuf *val, const char *trailer)\n+{\n+\tchar *end = strchr(trailer, '=');\n+\tif (!end)\n+\t\tend = strchr(trailer, ':');\n+\tif (end) {\n+\t\tstrbuf_add(tok, trailer, end - trailer);\n+\t\tstrbuf_trim(tok);\n+\t\tstrbuf_addstr(val, end + 1);\n+\t\tstrbuf_trim(val);\n+\t} else {\n+\t\tstrbuf_addstr(tok, trailer);\n+\t\tstrbuf_trim(tok);\n+\t}\n+}\n+\n+static struct trailer_item *create_trailer_item(const char *string)\n+{\n+\tstruct strbuf tok = STRBUF_INIT;\n+\tstruct strbuf val = STRBUF_INIT;\n+\tstruct trailer_item *new;\n+\tstruct trailer_item *item;\n+\tint tok_alnum_len;\n+\n+\tparse_trailer(&tok, &val, string);\n+\n+\ttok_alnum_len = alnum_len(tok.buf, tok.len);\n+\n+\t/* Lookup if the token matches something in the config */\n+\tfor (item = first_conf_item; item; item = item->next) {\n+\t\tif (!strncasecmp(tok.buf, item->conf->key, tok_alnum_len) ||\n+\t\t    !strncasecmp(tok.buf, item->conf->name, tok_alnum_len)) {\n+\t\t\tnew = xcalloc(sizeof(struct trailer_item), 1);\n+\t\t\tnew->conf = item->conf;\n+\t\t\tnew->token = xstrdup(item->conf->key);\n+\t\t\tnew->value = strbuf_detach(&val, NULL);\n+\t\t\tstrbuf_release(&tok);\n+\t\t\treturn new;\n+\t\t}\n+\t}\n+\n+\tnew = xcalloc(sizeof(struct trailer_item), 1);\n+\tnew->conf = xcalloc(sizeof(struct conf_info), 1);\n+\tnew->token = strbuf_detach(&tok, NULL);\n+\tnew->value = strbuf_detach(&val, NULL);\n+\n+\treturn new;\n+}\n+\n+static void add_trailer_item(struct trailer_item **first,\n+\t\t\t     struct trailer_item **last,\n+\t\t\t     struct trailer_item *new)\n+{\n+\tif (!*last) {\n+\t\t*first = new;\n+\t\t*last = new;\n+\t} else {\n+\t\t(*last)->next = new;\n+\t\tnew->previous = *last;\n+\t\t*last = new;\n+\t}\n+}\n+\n+static struct trailer_item *process_command_line_args(int argc, const char **argv)\n+{\n+\tint i;\n+\tstruct trailer_item *arg_tok_first = NULL;\n+\tstruct trailer_item *arg_tok_last = NULL;\n+\n+\tfor (i = 0; i < argc; i++) {\n+\t\tstruct trailer_item *new = create_trailer_item(argv[i]);\n+\t\tadd_trailer_item(&arg_tok_first, &arg_tok_last, new);\n+\t}\n+\n+\treturn arg_tok_first;\n+}\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233936","messageId":"20140130064921.7504.74582.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 05/17] strbuf: add strbuf_isspace()","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:08Z","receivedAt":"2014-01-30T06:49:08Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This helper function checks if a strbuf\ncontains only space chars or not.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n strbuf.c | 7 +++++++\n strbuf.h | 1 +\n 2 files changed, 8 insertions(+)\n\ndiff --git a/strbuf.c b/strbuf.c\nindex 83caf4a..2124bb8 100644\n--- a/strbuf.c\n+++ b/strbuf.c\n@@ -124,6 +124,13 @@ void strbuf_ltrim(struct strbuf *sb)\n \tsb->buf[sb->len] = '\\0';\n }\n \n+int strbuf_isspace(struct strbuf *sb)\n+{\n+\tchar *b;\n+\tfor (b = sb->buf; *b && isspace(*b); b++);\n+\treturn !*b;\n+}\n+\n struct strbuf **strbuf_split_buf(const char *str, size_t slen,\n \t\t\t\t int terminator, int max)\n {\ndiff --git a/strbuf.h b/strbuf.h\nindex 73e80ce..02bff3a 100644\n--- a/strbuf.h\n+++ b/strbuf.h\n@@ -42,6 +42,7 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len) {\n extern void strbuf_trim(struct strbuf *);\n extern void strbuf_rtrim(struct strbuf *);\n extern void strbuf_ltrim(struct strbuf *);\n+extern int strbuf_isspace(struct strbuf *);\n extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);\n \n /*\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233935","messageId":"20140130064921.7504.62053.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 06/17] trailer: parse trailers from input file","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:09Z","receivedAt":"2014-01-30T06:49:09Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This patch reads trailers from an input file, parses\nthem and puts the result into a doubly linked list.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n trailer.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 62 insertions(+)\n\ndiff --git a/trailer.c b/trailer.c\nindex f48fd94..084b3e1 100644\n--- a/trailer.c\n+++ b/trailer.c\n@@ -439,3 +439,65 @@ static struct trailer_item *process_command_line_args(int argc, const char **arg\n \n \treturn arg_tok_first;\n }\n+\n+static struct strbuf **read_input_file(const char *infile)\n+{\n+\tstruct strbuf sb = STRBUF_INIT;\n+\n+\tif (strbuf_read_file(&sb, infile, 0) < 0)\n+\t\tdie_errno(_(\"could not read input file '%s'\"), infile);\n+\n+\treturn strbuf_split(&sb, '\\n');\n+}\n+\n+/*\n+ * Return the the (0 based) index of the first trailer line\n+ * or the line count if there are no trailers.\n+ */\n+static int find_trailer_start(struct strbuf **lines)\n+{\n+\tint count, start, empty = 1;\n+\n+\t/* Get the line count */\n+\tfor (count = 0; lines[count]; count++);\n+\n+\t/*\n+\t * Get the start of the trailers by looking starting from the end\n+\t * for a line with only spaces before lines with one ':'.\n+\t */\n+\tfor (start = count - 1; start >= 0; start--) {\n+\t\tif (strbuf_isspace(lines[start])) {\n+\t\t\tif (empty)\n+\t\t\t\tcontinue;\n+\t\t\treturn start + 1;\n+\t\t}\n+\t\tif (strchr(lines[start]->buf, ':')) {\n+\t\t\tif (empty)\n+\t\t\t\tempty = 0;\n+\t\t\tcontinue;\n+\t\t}\n+\t\treturn count;\n+\t}\n+\n+\treturn empty ? count : start + 1;\n+}\n+\n+static void process_input_file(const char *infile,\n+\t\t\t       struct trailer_item **infile_tok_first,\n+\t\t\t       struct trailer_item **infile_tok_last)\n+{\n+\tstruct strbuf **lines = read_input_file(infile);\n+\tint start = find_trailer_start(lines);\n+\tint i;\n+\n+\t/* Print non trailer lines as is */\n+\tfor (i = 0; lines[i] && i < start; i++) {\n+\t\tprintf(\"%s\", lines[i]->buf);\n+\t}\n+\n+\t/* Parse trailer lines */\n+\tfor (i = start; lines[i]; i++) {\n+\t\tstruct trailer_item *new = create_trailer_item(lines[i]->buf);\n+\t\tadd_trailer_item(infile_tok_first, infile_tok_last, new);\n+\t}\n+}\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233932","messageId":"20140130064921.7504.20482.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 07/17] trailer: put all the processing together and print","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:10Z","receivedAt":"2014-01-30T06:49:10Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This patch adds the process_trailers() function that\ncalls all the previously added processing functions\nand then prints the results on the standard output.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n trailer.c | 40 ++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 40 insertions(+)\n\ndiff --git a/trailer.c b/trailer.c\nindex 084b3e1..8681aed 100644\n--- a/trailer.c\n+++ b/trailer.c\n@@ -49,6 +49,26 @@ static size_t alnum_len(const char *buf, size_t len)\n \treturn len + 1;\n }\n \n+static void print_tok_val(const char *tok, const char *val)\n+{\n+\tchar c = tok[strlen(tok) - 1];\n+\tif (isalnum(c))\n+\t\tprintf(\"%s: %s\\n\", tok, val);\n+\telse if (isspace(c) || c == '#')\n+\t\tprintf(\"%s%s\\n\", tok, val);\n+\telse\n+\t\tprintf(\"%s %s\\n\", tok, val);\n+}\n+\n+static void print_all(struct trailer_item *first, int trim_empty)\n+{\n+\tstruct trailer_item *item;\n+\tfor (item = first; item; item = item->next) {\n+\t\tif (!trim_empty || strlen(item->value) > 0)\n+\t\t\tprint_tok_val(item->token, item->value);\n+\t}\n+}\n+\n static void add_arg_to_infile(struct trailer_item *infile_tok,\n \t\t\t      struct trailer_item *arg_tok)\n {\n@@ -501,3 +521,23 @@ static void process_input_file(const char *infile,\n \t\tadd_trailer_item(infile_tok_first, infile_tok_last, new);\n \t}\n }\n+\n+void process_trailers(const char *infile, int trim_empty, int argc, const char **argv)\n+{\n+\tstruct trailer_item *infile_tok_first = NULL;\n+\tstruct trailer_item *infile_tok_last = NULL;\n+\tstruct trailer_item *arg_tok_first;\n+\n+\tgit_config(git_trailer_config, NULL);\n+\n+\t/* Print the non trailer part of infile */\n+\tif (infile) {\n+\t\tprocess_input_file(infile, &infile_tok_first, &infile_tok_last);\n+\t}\n+\n+\targ_tok_first = process_command_line_args(argc, argv);\n+\n+\tprocess_trailers_lists(&infile_tok_first, &infile_tok_last, &arg_tok_first);\n+\n+\tprint_all(infile_tok_first, trim_empty);\n+}\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233933","messageId":"20140130064921.7504.75845.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 08/17] trailer: add interpret-trailers command","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:11Z","receivedAt":"2014-01-30T06:49:11Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This patch adds the \"git interpret-trailers\" command.\nThis command uses the previously added process_trailers()\nfunction in trailer.c.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n .gitignore                   |  1 +\n Makefile                     |  1 +\n builtin.h                    |  1 +\n builtin/interpret-trailers.c | 36 ++++++++++++++++++++++++++++++++++++\n git.c                        |  1 +\n trailer.h                    |  6 ++++++\n 6 files changed, 46 insertions(+)\n create mode 100644 builtin/interpret-trailers.c\n create mode 100644 trailer.h\n\ndiff --git a/.gitignore b/.gitignore\nindex b5f9def..c870ada 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -74,6 +74,7 @@\n /git-index-pack\n /git-init\n /git-init-db\n+/git-interpret-trailers\n /git-instaweb\n /git-log\n /git-ls-files\ndiff --git a/Makefile b/Makefile\nindex ec90feb..a91465e 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -935,6 +935,7 @@ BUILTIN_OBJS += builtin/hash-object.o\n BUILTIN_OBJS += builtin/help.o\n BUILTIN_OBJS += builtin/index-pack.o\n BUILTIN_OBJS += builtin/init-db.o\n+BUILTIN_OBJS += builtin/interpret-trailers.o\n BUILTIN_OBJS += builtin/log.o\n BUILTIN_OBJS += builtin/ls-files.o\n BUILTIN_OBJS += builtin/ls-remote.o\ndiff --git a/builtin.h b/builtin.h\nindex d4afbfe..30f4c30 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -71,6 +71,7 @@ extern int cmd_hash_object(int argc, const char **argv, const char *prefix);\n extern int cmd_help(int argc, const char **argv, const char *prefix);\n extern int cmd_index_pack(int argc, const char **argv, const char *prefix);\n extern int cmd_init_db(int argc, const char **argv, const char *prefix);\n+extern int cmd_interpret_trailers(int argc, const char **argv, const char *prefix);\n extern int cmd_log(int argc, const char **argv, const char *prefix);\n extern int cmd_log_reflog(int argc, const char **argv, const char *prefix);\n extern int cmd_ls_files(int argc, const char **argv, const char *prefix);\ndiff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c\nnew file mode 100644\nindex 0000000..04b0ae2\n--- /dev/null\n+++ b/builtin/interpret-trailers.c\n@@ -0,0 +1,36 @@\n+/*\n+ * Builtin \"git interpret-trailers\"\n+ *\n+ * Copyright (c) 2013 Christian Couder <chriscool@tuxfamily.org>\n+ *\n+ */\n+\n+#include \"cache.h\"\n+#include \"builtin.h\"\n+#include \"parse-options.h\"\n+#include \"strbuf.h\"\n+#include \"trailer.h\"\n+\n+static const char * const git_interpret_trailers_usage[] = {\n+\tN_(\"git interpret-trailers [--trim-empty] [--infile=<file>] [(<token>[(=|:)<value>])...]\"),\n+\tNULL\n+};\n+\n+int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)\n+{\n+\tconst char *infile = NULL;\n+\tint trim_empty = 0;\n+\n+\tstruct option options[] = {\n+\t\tOPT_BOOL(0, \"trim-empty\", &trim_empty, N_(\"trim empty trailers\")),\n+\t\tOPT_FILENAME(0, \"infile\", &infile, N_(\"use message from file\")),\n+\t\tOPT_END()\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, options,\n+\t\t\t     git_interpret_trailers_usage, 0);\n+\n+\tprocess_trailers(infile, trim_empty, argc, argv);\n+\n+\treturn 0;\n+}\ndiff --git a/git.c b/git.c\nindex 3799514..1420b58 100644\n--- a/git.c\n+++ b/git.c\n@@ -383,6 +383,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"index-pack\", cmd_index_pack, RUN_SETUP_GENTLY },\n \t\t{ \"init\", cmd_init_db },\n \t\t{ \"init-db\", cmd_init_db },\n+\t\t{ \"interpret-trailers\", cmd_interpret_trailers, RUN_SETUP },\n \t\t{ \"log\", cmd_log, RUN_SETUP },\n \t\t{ \"ls-files\", cmd_ls_files, RUN_SETUP },\n \t\t{ \"ls-remote\", cmd_ls_remote, RUN_SETUP_GENTLY },\ndiff --git a/trailer.h b/trailer.h\nnew file mode 100644\nindex 0000000..9db4459\n--- /dev/null\n+++ b/trailer.h\n@@ -0,0 +1,6 @@\n+#ifndef TRAILER_H\n+#define TRAILER_H\n+\n+void process_trailers(const char *infile, int trim_empty, int argc, const char **argv);\n+\n+#endif /* TRAILER_H */\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233922","messageId":"20140130064921.7504.21063.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 09/17] trailer: add tests for \"git interpret-trailers\"","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:12Z","receivedAt":"2014-01-30T06:49:12Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n t/t7513-interpret-trailers.sh | 208 ++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 208 insertions(+)\n create mode 100755 t/t7513-interpret-trailers.sh\n\ndiff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh\nnew file mode 100755\nindex 0000000..8be333c\n--- /dev/null\n+++ b/t/t7513-interpret-trailers.sh\n@@ -0,0 +1,208 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2013 Christian Couder\n+#\n+\n+test_description='git interpret-trailers'\n+\n+. ./test-lib.sh\n+\n+cat >basic_message <<'EOF'\n+subject\n+\n+body\n+EOF\n+\n+cat >complex_message_body <<'EOF'\n+my subject\n+\n+my body which is long\n+and contains some special\n+chars like : = ? !\n+\n+EOF\n+\n+# We want one trailing space at the end of each line.\n+# Let's use sed to make sure that these spaces are not removed\n+# by any automatic tool.\n+sed -e 's/ Z$/ /' >complex_message_trailers <<-\\EOF\n+Fixes: Z\n+Acked-by: Z\n+Reviewed-by: Z\n+Signed-off-by: Z\n+EOF\n+\n+test_expect_success 'without config' '\n+\tprintf \"ack: Peff\\nReviewed-by: \\nAcked-by: Johan\\n\" >expected &&\n+\tgit interpret-trailers \"ack = Peff\" \"Reviewed-by\" \"Acked-by: Johan\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success '--trim-empty without config' '\n+\tprintf \"ack: Peff\\nAcked-by: Johan\\n\" >expected &&\n+\tgit interpret-trailers --trim-empty \"ack = Peff\" \"Reviewed-by\" \"Acked-by: Johan\" \"sob:\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'with config setup' '\n+\tgit config trailer.ack.key \"Acked-by: \" &&\n+\tprintf \"Acked-by: Peff\\n\" >expected &&\n+\tgit interpret-trailers --trim-empty \"ack = Peff\" >actual &&\n+\ttest_cmp expected actual &&\n+\tgit interpret-trailers --trim-empty \"Acked-by = Peff\" >actual &&\n+\ttest_cmp expected actual &&\n+\tgit interpret-trailers --trim-empty \"Acked-by :Peff\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'with config setup and = sign' '\n+\tgit config trailer.ack.key \"Acked-by= \" &&\n+\tprintf \"Acked-by= Peff\\n\" >expected &&\n+\tgit interpret-trailers --trim-empty \"ack = Peff\" >actual &&\n+\ttest_cmp expected actual &&\n+\tgit interpret-trailers --trim-empty \"Acked-by= Peff\" >actual &&\n+\ttest_cmp expected actual &&\n+\tgit interpret-trailers --trim-empty \"Acked-by : Peff\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'with config setup and # sign' '\n+\tgit config trailer.bug.key \"Bug #\" &&\n+\tprintf \"Bug #42\\n\" >expected &&\n+\tgit interpret-trailers --trim-empty \"bug = 42\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'with commit basic message' '\n+\tgit interpret-trailers --infile basic_message >actual &&\n+\ttest_cmp basic_message actual\n+'\n+\n+test_expect_success 'with commit complex message' '\n+\tcat complex_message_body complex_message_trailers >complex_message &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Fixes: \\nAcked-by= \\nReviewed-by: \\nSigned-off-by: \\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'with commit complex message and args' '\n+\tcat complex_message_body >expected &&\n+\tprintf \"Fixes: \\nAcked-by= \\nAcked-by= Peff\\nReviewed-by: \\nSigned-off-by: \\nBug #42\\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message \"ack: Peff\" \"bug: 42\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'with commit complex message, args and --trim-empty' '\n+\tcat complex_message_body >expected &&\n+\tprintf \"Acked-by= Peff\\nBug #42\\n\" >>expected &&\n+\tgit interpret-trailers --trim-empty --infile complex_message \"ack: Peff\" \"bug: 42\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'using \"where = before\"' '\n+\tgit config trailer.bug.where \"before\" &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Bug #42\\nFixes: \\nAcked-by= \\nAcked-by= Peff\\nReviewed-by: \\nSigned-off-by: \\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message \"ack: Peff\" \"bug: 42\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'using \"where = before\" for a token in the middle of infile' '\n+\tgit config trailer.review.key \"Reviewed-by:\" &&\n+\tgit config trailer.review.where \"before\" &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Bug #42\\nFixes: \\nAcked-by= \\nAcked-by= Peff\\nReviewed-by: Johan\\nReviewed-by: \\nSigned-off-by: \\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message \"ack: Peff\" \"bug: 42\" \"review: Johan\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'using \"where = before\" and --trim-empty' '\n+\tcat complex_message_body >expected &&\n+\tprintf \"Bug #46\\nBug #42\\nAcked-by= Peff\\nReviewed-by: Johan\\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message --trim-empty \"ack: Peff\" \"bug: 42\" \"review: Johan\" \"Bug: 46\"  >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'the default is \"ifExist = addIfDifferent\"' '\n+\tcat complex_message_body >expected &&\n+\tprintf \"Bug #42\\nFixes: \\nAcked-by= \\nAcked-by= Peff\\nReviewed-by: \\nSigned-off-by: \\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message \"ack: Peff\" \"review:\" \"bug: 42\" \"ack: Peff\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'using \"ifExist = addIfDifferent\"' '\n+\tgit config trailer.review.ifExist \"addIfDifferent\" &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Bug #42\\nFixes: \\nAcked-by= \\nAcked-by= Peff\\nReviewed-by: \\nSigned-off-by: \\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message \"ack: Peff\" \"review:\" \"bug: 42\" \"ack: Peff\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'using \"ifExist = addIfDifferentNeighbor\"' '\n+\tgit config trailer.ack.ifExist \"addIfDifferentNeighbor\" &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Bug #42\\nFixes: \\nAcked-by= \\nAcked-by= Peff\\nAcked-by= Junio\\nAcked-by= Peff\\nReviewed-by: \\nSigned-off-by: \\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message \"ack: Peff\" \"review:\" \"ack: Junio\" \"bug: 42\" \"ack: Peff\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'using \"ifExist = addIfDifferentNeighbor\" and --trim-empty' '\n+\tgit config trailer.ack.ifExist \"addIfDifferentNeighbor\" &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Bug #42\\nAcked-by= Peff\\nAcked-by= Junio\\nAcked-by= Peff\\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message --trim-empty \"ack: Peff\" \"Acked-by= Peff\" \"review:\" \"ack: Junio\" \"bug: 42\" \"ack: Peff\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'using \"ifExist = add\"' '\n+\tgit config trailer.ack.ifExist \"add\" &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Bug #42\\nFixes: \\nAcked-by= \\nAcked-by= Peff\\nAcked-by= Peff\\nAcked-by= Junio\\nAcked-by= Peff\\nReviewed-by: \\nSigned-off-by: \\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message \"ack: Peff\" \"Acked-by= Peff\" \"review:\" \"ack: Junio\" \"bug: 42\" \"ack: Peff\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'using \"ifExist = overwrite\"' '\n+\tgit config trailer.fix.key \"Fixes:\" &&\n+\tgit config trailer.fix.ifExist \"overwrite\" &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Bug #42\\nFixes: 22\\nAcked-by= \\nAcked-by= Junio\\nAcked-by= Peff\\nReviewed-by: \\nSigned-off-by: \\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message \"review:\" \"fix=53\" \"ack: Junio\" \"fix=22\" \"bug: 42\" \"ack: Peff\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'using \"ifExist = doNothing\"' '\n+\tgit config trailer.fix.ifExist \"doNothing\" &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Bug #42\\nFixes: \\nAcked-by= \\nAcked-by= Junio\\nAcked-by= Peff\\nReviewed-by: \\nSigned-off-by: \\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message \"review:\" \"fix=53\" \"ack: Junio\" \"fix=22\" \"bug: 42\" \"ack: Peff\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'the default is \"ifMissing = add\"' '\n+\tgit config trailer.cc.key \"Cc: \" &&\n+\tgit config trailer.Cc.where \"before\" &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Bug #42\\nCc: Linus\\nFixes: \\nAcked-by= \\nAcked-by= Junio\\nAcked-by= Peff\\nReviewed-by: \\nSigned-off-by: \\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message \"review:\" \"fix=53\" \"cc=Linus\" \"ack: Junio\" \"fix=22\" \"bug: 42\" \"ack: Peff\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'using \"ifMissing = add\"' '\n+\tgit config trailer.Cc.ifMissing \"add\" &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Cc: Linus\\nBug #42\\nFixes: \\nAcked-by= \\nAcked-by= Junio\\nAcked-by= Peff\\nReviewed-by: \\nSigned-off-by: \\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message \"review:\" \"fix=53\" \"ack: Junio\" \"fix=22\" \"bug: 42\" \"cc=Linus\" \"ack: Peff\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'using \"ifMissing = doNothing\"' '\n+\tgit config trailer.Cc.ifMissing \"doNothing\" &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Bug #42\\nFixes: \\nAcked-by= \\nAcked-by= Junio\\nAcked-by= Peff\\nReviewed-by: \\nSigned-off-by: \\n\" >>expected &&\n+\tgit interpret-trailers --infile complex_message \"review:\" \"fix=53\" \"cc=Linus\" \"ack: Junio\" \"fix=22\" \"bug: 42\" \"ack: Peff\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_done\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233927","messageId":"20140130064921.7504.57934.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 10/17] trailer: if no input file is passed, read from stdin","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:13Z","receivedAt":"2014-01-30T06:49:13Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"It is simpler and more natural if the \"git interpret-trailers\"\nis made a filter as its output already goes to sdtout.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/interpret-trailers.c  |  2 +-\n t/t7513-interpret-trailers.sh |  7 +++++++\n trailer.c                     | 15 +++++++++------\n 3 files changed, 17 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c\nindex 04b0ae2..ae8e561 100644\n--- a/builtin/interpret-trailers.c\n+++ b/builtin/interpret-trailers.c\n@@ -23,7 +23,7 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)\n \n \tstruct option options[] = {\n \t\tOPT_BOOL(0, \"trim-empty\", &trim_empty, N_(\"trim empty trailers\")),\n-\t\tOPT_FILENAME(0, \"infile\", &infile, N_(\"use message from file\")),\n+\t\tOPT_FILENAME(0, \"infile\", &infile, N_(\"use message from file, instead of stdin\")),\n \t\tOPT_END()\n \t};\n \ndiff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh\nindex 8be333c..f5ef81f 100755\n--- a/t/t7513-interpret-trailers.sh\n+++ b/t/t7513-interpret-trailers.sh\n@@ -205,4 +205,11 @@ test_expect_success 'using \"ifMissing = doNothing\"' '\n \ttest_cmp expected actual\n '\n \n+test_expect_success 'with input from stdin' '\n+\tcat complex_message_body >expected &&\n+\tprintf \"Bug #42\\nFixes: \\nAcked-by= \\nAcked-by= Junio\\nAcked-by= Peff\\nReviewed-by: \\nSigned-off-by: \\n\" >>expected &&\n+\tgit interpret-trailers \"review:\" \"fix=53\" \"cc=Linus\" \"ack: Junio\" \"fix=22\" \"bug: 42\" \"ack: Peff\" < complex_message >actual &&\n+\ttest_cmp expected actual\n+'\n+\n test_done\ndiff --git a/trailer.c b/trailer.c\nindex 8681aed..73a65e0 100644\n--- a/trailer.c\n+++ b/trailer.c\n@@ -464,8 +464,13 @@ static struct strbuf **read_input_file(const char *infile)\n {\n \tstruct strbuf sb = STRBUF_INIT;\n \n-\tif (strbuf_read_file(&sb, infile, 0) < 0)\n-\t\tdie_errno(_(\"could not read input file '%s'\"), infile);\n+\tif (infile) {\n+\t\tif (strbuf_read_file(&sb, infile, 0) < 0)\n+\t\t\tdie_errno(_(\"could not read input file '%s'\"), infile);\n+\t} else {\n+\t\tif (strbuf_read(&sb, fileno(stdin), 0) < 0)\n+\t\t\tdie_errno(_(\"could not read from stdin\"));\n+\t}\n \n \treturn strbuf_split(&sb, '\\n');\n }\n@@ -530,10 +535,8 @@ void process_trailers(const char *infile, int trim_empty, int argc, const char *\n \n \tgit_config(git_trailer_config, NULL);\n \n-\t/* Print the non trailer part of infile */\n-\tif (infile) {\n-\t\tprocess_input_file(infile, &infile_tok_first, &infile_tok_last);\n-\t}\n+\t/* Print the non trailer part of infile (or stdin if infile is NULL) */\n+\tprocess_input_file(infile, &infile_tok_first, &infile_tok_last);\n \n \targ_tok_first = process_command_line_args(argc, argv);\n \n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233931","messageId":"20140130064921.7504.42810.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 11/17] trailer: add new_trailer_item() function","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:14Z","receivedAt":"2014-01-30T06:49:14Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This is a small refactoring to prepare for the next steps.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n trailer.c | 31 +++++++++++++++++++------------\n 1 file changed, 19 insertions(+), 12 deletions(-)\n\ndiff --git a/trailer.c b/trailer.c\nindex 73a65e0..430ff39 100644\n--- a/trailer.c\n+++ b/trailer.c\n@@ -399,11 +399,27 @@ static void parse_trailer(struct strbuf *tok, struct strbuf *val, const char *tr\n \t}\n }\n \n+static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,\n+\t\t\t\t\t     const char* tok, const char* val)\n+{\n+\tstruct trailer_item *new = xcalloc(sizeof(struct trailer_item), 1);\n+\tnew->value = val;\n+\n+\tif (conf_item) {\n+\t\tnew->conf = conf_item->conf;\n+\t\tnew->token = xstrdup(conf_item->conf->key);\n+\t} else {\n+\t\tnew->conf = xcalloc(sizeof(struct conf_info), 1);\n+\t\tnew->token = tok;\n+\t}\n+\n+\treturn new;\n+}\n+\n static struct trailer_item *create_trailer_item(const char *string)\n {\n \tstruct strbuf tok = STRBUF_INIT;\n \tstruct strbuf val = STRBUF_INIT;\n-\tstruct trailer_item *new;\n \tstruct trailer_item *item;\n \tint tok_alnum_len;\n \n@@ -415,21 +431,12 @@ static struct trailer_item *create_trailer_item(const char *string)\n \tfor (item = first_conf_item; item; item = item->next) {\n \t\tif (!strncasecmp(tok.buf, item->conf->key, tok_alnum_len) ||\n \t\t    !strncasecmp(tok.buf, item->conf->name, tok_alnum_len)) {\n-\t\t\tnew = xcalloc(sizeof(struct trailer_item), 1);\n-\t\t\tnew->conf = item->conf;\n-\t\t\tnew->token = xstrdup(item->conf->key);\n-\t\t\tnew->value = strbuf_detach(&val, NULL);\n \t\t\tstrbuf_release(&tok);\n-\t\t\treturn new;\n+\t\t\treturn new_trailer_item(item, NULL, strbuf_detach(&val, NULL));\n \t\t}\n \t}\n \n-\tnew = xcalloc(sizeof(struct trailer_item), 1);\n-\tnew->conf = xcalloc(sizeof(struct conf_info), 1);\n-\tnew->token = strbuf_detach(&tok, NULL);\n-\tnew->value = strbuf_detach(&val, NULL);\n-\n-\treturn new;\n+\treturn new_trailer_item(NULL, strbuf_detach(&tok, NULL), strbuf_detach(&val, NULL));;\n }\n \n static void add_trailer_item(struct trailer_item **first,\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233928","messageId":"20140130064921.7504.3401.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 12/17] strbuf: add strbuf_replace()","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:15Z","receivedAt":"2014-01-30T06:49:15Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n strbuf.c | 7 +++++++\n strbuf.h | 3 +++\n 2 files changed, 10 insertions(+)\n\ndiff --git a/strbuf.c b/strbuf.c\nindex 2124bb8..e45e513 100644\n--- a/strbuf.c\n+++ b/strbuf.c\n@@ -197,6 +197,13 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,\n \tstrbuf_setlen(sb, sb->len + dlen - len);\n }\n \n+void strbuf_replace(struct strbuf *sb, const char *a, const char *b)\n+{\n+\tchar *ptr = strstr(sb->buf, a);\n+\tif (ptr)\n+\t\tstrbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));\n+}\n+\n void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)\n {\n \tstrbuf_splice(sb, pos, 0, data, len);\ndiff --git a/strbuf.h b/strbuf.h\nindex 02bff3a..38faf70 100644\n--- a/strbuf.h\n+++ b/strbuf.h\n@@ -111,6 +111,9 @@ extern void strbuf_remove(struct strbuf *, size_t pos, size_t len);\n extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,\n                           const void *, size_t);\n \n+/* first occurence of a replaced with b */\n+extern void strbuf_replace(struct strbuf *, const char *a, const char *b);\n+\n extern void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size);\n \n extern void strbuf_add(struct strbuf *, const void *, size_t);\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233929","messageId":"20140130064921.7504.39622.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 13/17] trailer: execute command from 'trailer.<name>.command'","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:16Z","receivedAt":"2014-01-30T06:49:16Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n trailer.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 56 insertions(+)\n\ndiff --git a/trailer.c b/trailer.c\nindex 430ff39..dc8908a 100644\n--- a/trailer.c\n+++ b/trailer.c\n@@ -1,4 +1,5 @@\n #include \"cache.h\"\n+#include \"run-command.h\"\n /*\n  * Copyright (c) 2013 Christian Couder <chriscool@tuxfamily.org>\n  */\n@@ -12,11 +13,14 @@ struct conf_info {\n \tchar *name;\n \tchar *key;\n \tchar *command;\n+\tunsigned command_uses_arg : 1;\n \tenum action_where where;\n \tenum action_if_exist if_exist;\n \tenum action_if_missing if_missing;\n };\n \n+#define TRAILER_ARG_STRING \"$ARG\"\n+\n struct trailer_item {\n \tstruct trailer_item *previous;\n \tstruct trailer_item *next;\n@@ -368,6 +372,7 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)\n \t\t\tif (conf->command)\n \t\t\t\twarning(_(\"more than one %s\"), orig_conf_key);\n \t\t\tconf->command = xstrdup(value);\n+\t\t\tconf->command_uses_arg = !!strstr(conf->command, TRAILER_ARG_STRING);\n \t\t} else if (type == TRAILER_WHERE) {\n \t\t\tif (set_where(conf, value))\n \t\t\t\twarning(_(\"unknown value '%s' for key '%s'\"), value, orig_conf_key);\n@@ -399,6 +404,45 @@ static void parse_trailer(struct strbuf *tok, struct strbuf *val, const char *tr\n \t}\n }\n \n+static int read_from_command(struct child_process *cp, struct strbuf *buf)\n+{\n+\tif (run_command(cp))\n+\t\treturn error(\"running trailer command '%s' failed\", cp->argv[0]);\n+\tif (strbuf_read(buf, cp->out, 1024) < 1)\n+\t\treturn error(\"reading from trailer command '%s' failed\", cp->argv[0]);\n+\tstrbuf_trim(buf);\n+\treturn 0;\n+}\n+\n+static const char *apply_command(const char *command, const char *arg)\n+{\n+\tstruct strbuf cmd = STRBUF_INIT;\n+\tstruct strbuf buf = STRBUF_INIT;\n+\tstruct child_process cp;\n+\tconst char *argv[] = {NULL, NULL};\n+\tconst char *result = \"\";\n+\n+\tstrbuf_addstr(&cmd, command);\n+\tif (arg)\n+\t\tstrbuf_replace(&cmd, TRAILER_ARG_STRING, arg);\n+\n+\targv[0] = cmd.buf;\n+\tmemset(&cp, 0, sizeof(cp));\n+\tcp.argv = argv;\n+\tcp.env = local_repo_env;\n+\tcp.no_stdin = 1;\n+\tcp.out = -1;\n+\tcp.use_shell = 1;\n+\n+\tif (read_from_command(&cp, &buf))\n+\t\tstrbuf_release(&buf);\n+\telse\n+\t\tresult = strbuf_detach(&buf, NULL);\n+\n+\tstrbuf_release(&cmd);\n+\treturn result;\n+}\n+\n static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,\n \t\t\t\t\t     const char* tok, const char* val)\n {\n@@ -408,6 +452,8 @@ static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,\n \tif (conf_item) {\n \t\tnew->conf = conf_item->conf;\n \t\tnew->token = xstrdup(conf_item->conf->key);\n+\t\tif (conf_item->conf->command_uses_arg || !val)\n+\t\t\tnew->value = apply_command(conf_item->conf->command, val);\n \t} else {\n \t\tnew->conf = xcalloc(sizeof(struct conf_info), 1);\n \t\tnew->token = tok;\n@@ -458,12 +504,22 @@ static struct trailer_item *process_command_line_args(int argc, const char **arg\n \tint i;\n \tstruct trailer_item *arg_tok_first = NULL;\n \tstruct trailer_item *arg_tok_last = NULL;\n+\tstruct trailer_item *item;\n \n \tfor (i = 0; i < argc; i++) {\n \t\tstruct trailer_item *new = create_trailer_item(argv[i]);\n \t\tadd_trailer_item(&arg_tok_first, &arg_tok_last, new);\n \t}\n \n+\t/* Add conf commands that don't use $ARG */\n+\tfor (item = first_conf_item; item; item = item->next) {\n+\t\tif (item->conf->command && !item->conf->command_uses_arg)\n+\t\t{\n+\t\t\tstruct trailer_item *new = new_trailer_item(item, NULL, NULL);\n+\t\t\tadd_trailer_item(&arg_tok_first, &arg_tok_last, new);\n+\t\t}\n+\t}\n+\n \treturn arg_tok_first;\n }\n \n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233923","messageId":"20140130064921.7504.1006.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 14/17] trailer: add tests for trailer command","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:17Z","receivedAt":"2014-01-30T06:49:17Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n t/t7513-interpret-trailers.sh | 27 +++++++++++++++++++++++++++\n 1 file changed, 27 insertions(+)\n\ndiff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh\nindex f5ef81f..2d50b7a 100755\n--- a/t/t7513-interpret-trailers.sh\n+++ b/t/t7513-interpret-trailers.sh\n@@ -212,4 +212,31 @@ test_expect_success 'with input from stdin' '\n \ttest_cmp expected actual\n '\n \n+test_expect_success 'with simple command' '\n+\tgit config trailer.sign.key \"Signed-off-by: \" &&\n+\tgit config trailer.sign.where \"after\" &&\n+\tgit config trailer.sign.ifExist \"addIfDifferentNeighbor\" &&\n+\tgit config trailer.sign.command \"echo \\\"A U Thor <author@example.com>\\\"\" &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Fixes: \\nAcked-by= \\nReviewed-by: \\nSigned-off-by: \\nSigned-off-by: A U Thor <author@example.com>\\n\" >>expected &&\n+\tgit interpret-trailers \"review:\" \"fix=22\" < complex_message >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'setup a commit' '\n+\techo \"Content of the first commit.\" > a.txt &&\n+\tgit add a.txt &&\n+\tgit commit -m \"Add file a.txt\"\n+'\n+\n+test_expect_success 'with command using $ARG' '\n+\tgit config trailer.fix.ifExist \"overwrite\" &&\n+\tgit config trailer.fix.command \"git log -1 --oneline --format=\\\"%h (%s)\\\" --abbrev-commit --abbrev=14 \\$ARG\" &&\n+\tFIXED=$(git log -1 --oneline --format=\"%h (%s)\" --abbrev-commit --abbrev=14 HEAD) &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Fixes: $FIXED\\nAcked-by= \\nReviewed-by: \\nSigned-off-by: \\nSigned-off-by: A U Thor <author@example.com>\\n\" >>expected &&\n+\tgit interpret-trailers \"review:\" \"fix=HEAD\" < complex_message >actual &&\n+\ttest_cmp expected actual\n+'\n+\n test_done\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233925","messageId":"20140130064921.7504.90926.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 15/17] trailer: set author and committer env variables","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:18Z","receivedAt":"2014-01-30T06:49:18Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n trailer.c | 30 +++++++++++++++++++++++++++++-\n 1 file changed, 29 insertions(+), 1 deletion(-)\n\ndiff --git a/trailer.c b/trailer.c\nindex dc8908a..e29b7f2 100644\n--- a/trailer.c\n+++ b/trailer.c\n@@ -1,5 +1,6 @@\n #include \"cache.h\"\n #include \"run-command.h\"\n+#include \"argv-array.h\"\n /*\n  * Copyright (c) 2013 Christian Couder <chriscool@tuxfamily.org>\n  */\n@@ -414,14 +415,40 @@ static int read_from_command(struct child_process *cp, struct strbuf *buf)\n \treturn 0;\n }\n \n+static void setup_ac_env(struct argv_array *env, const char *ac_name, const char *ac_mail, const char *(*read)(int))\n+{\n+\tif (!getenv(ac_name) || !getenv(ac_mail)) {\n+\t\tstruct ident_split ident;\n+\t\tconst char *namebuf, *mailbuf;\n+\t\tint namelen, maillen;\n+\t\tconst char *ac_info = read(IDENT_NO_DATE);\n+\n+\t\tif (split_ident_line(&ident, ac_info, strlen(ac_info)))\n+\t\t\treturn;\n+\n+\t\tnamelen = ident.name_end - ident.name_begin;\n+\t\tnamebuf = ident.name_begin;\n+\n+\t\tmaillen = ident.mail_end - ident.mail_begin;\n+\t\tmailbuf = ident.mail_begin;\n+\n+\t\targv_array_pushf(env, \"%s=%.*s\", ac_name, namelen, namebuf);\n+\t\targv_array_pushf(env, \"%s=%.*s\", ac_mail, maillen, mailbuf);\n+\t}\n+}\n+\n static const char *apply_command(const char *command, const char *arg)\n {\n+\tstruct argv_array env = ARGV_ARRAY_INIT;\n \tstruct strbuf cmd = STRBUF_INIT;\n \tstruct strbuf buf = STRBUF_INIT;\n \tstruct child_process cp;\n \tconst char *argv[] = {NULL, NULL};\n \tconst char *result = \"\";\n \n+\tsetup_ac_env(&env, \"GIT_AUTHOR_NAME\", \"GIT_AUTHOR_EMAIL\", git_author_info);\n+\tsetup_ac_env(&env, \"GIT_COMMITTER_NAME\", \"GIT_COMMITTER_EMAIL\", git_committer_info);\n+\n \tstrbuf_addstr(&cmd, command);\n \tif (arg)\n \t\tstrbuf_replace(&cmd, TRAILER_ARG_STRING, arg);\n@@ -429,7 +456,7 @@ static const char *apply_command(const char *command, const char *arg)\n \targv[0] = cmd.buf;\n \tmemset(&cp, 0, sizeof(cp));\n \tcp.argv = argv;\n-\tcp.env = local_repo_env;\n+\tcp.env = env.argv;\n \tcp.no_stdin = 1;\n \tcp.out = -1;\n \tcp.use_shell = 1;\n@@ -440,6 +467,7 @@ static const char *apply_command(const char *command, const char *arg)\n \t\tresult = strbuf_detach(&buf, NULL);\n \n \tstrbuf_release(&cmd);\n+\targv_array_clear(&env);\n \treturn result;\n }\n \n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233926","messageId":"20140130064921.7504.42424.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 16/17] trailer: add tests for commands using env variables","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:19Z","receivedAt":"2014-01-30T06:49:19Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n t/t7513-interpret-trailers.sh | 20 ++++++++++++++++++++\n 1 file changed, 20 insertions(+)\n\ndiff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh\nindex 2d50b7a..00894a8 100755\n--- a/t/t7513-interpret-trailers.sh\n+++ b/t/t7513-interpret-trailers.sh\n@@ -223,6 +223,26 @@ test_expect_success 'with simple command' '\n \ttest_cmp expected actual\n '\n \n+test_expect_success 'with command using commiter information' '\n+\tgit config trailer.sign.ifExist \"addIfDifferent\" &&\n+\tgit config trailer.sign.command \"echo \\\"\\$GIT_COMMITTER_NAME <\\$GIT_COMMITTER_EMAIL>\\\"\" &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Fixes: \\nAcked-by= \\nReviewed-by: \\nSigned-off-by: \\nSigned-off-by: C O Mitter <committer@example.com>\\n\" >>expected &&\n+\tgit interpret-trailers \"review:\" \"fix=22\" < complex_message >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'with command using author information' '\n+\tgit config trailer.sign.key \"Signed-off-by: \" &&\n+\tgit config trailer.sign.where \"after\" &&\n+\tgit config trailer.sign.ifExist \"addIfDifferentNeighbor\" &&\n+\tgit config trailer.sign.command \"echo \\\"\\$GIT_AUTHOR_NAME <\\$GIT_AUTHOR_EMAIL>\\\"\" &&\n+\tcat complex_message_body >expected &&\n+\tprintf \"Fixes: \\nAcked-by= \\nReviewed-by: \\nSigned-off-by: \\nSigned-off-by: A U Thor <author@example.com>\\n\" >>expected &&\n+\tgit interpret-trailers \"review:\" \"fix=22\" < complex_message >actual &&\n+\ttest_cmp expected actual\n+'\n+\n test_expect_success 'setup a commit' '\n \techo \"Content of the first commit.\" > a.txt &&\n \tgit add a.txt &&\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233930","messageId":"20140130064921.7504.17523.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"20140130064217.7504.473.chriscool@tuxfamily.org","subject":"[PATCH v4 17/17] Documentation: add documentation for 'git interpret-trailers'","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-01-30T06:49:20Z","receivedAt":"2014-01-30T06:49:20Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n Documentation/git-interpret-trailers.txt | 132 +++++++++++++++++++++++++++++++\n 1 file changed, 132 insertions(+)\n create mode 100644 Documentation/git-interpret-trailers.txt\n\ndiff --git a/Documentation/git-interpret-trailers.txt b/Documentation/git-interpret-trailers.txt\nnew file mode 100644\nindex 0000000..7683889\n--- /dev/null\n+++ b/Documentation/git-interpret-trailers.txt\n@@ -0,0 +1,132 @@\n+git-interpret-trailers(1)\n+=========================\n+\n+NAME\n+----\n+git-interpret-trailers - help add stuctured information into commit messages\n+\n+SYNOPSIS\n+--------\n+[verse]\n+'git interpret-trailers' [--trim-empty] [--infile=<file>] [(<token>[(=|:)<value>])...]\n+\n+DESCRIPTION\n+-----------\n+Help add RFC 822-like headers, called 'trailers', at the end of the\n+otherwise free-form part of a commit message.\n+\n+Unless `--infile=<file>` is used, this command is a filter. It reads\n+the standard input for a commit message and applies the `token`\n+arguments, if any, to this message. The resulting message is emited on\n+the standard output.\n+\n+Some configuration variables control the way the `token` arguments are\n+applied to the message and the way any existing trailer in the message\n+is changed. They also make it possible to automatically add some\n+trailers.\n+\n+By default, a 'token=value' or 'token:value' argument will be added\n+only if no trailer with the same (token, value) pair is already in the\n+message. The 'token' and 'value' parts will be trimmed to remove\n+starting and trailing whitespaces, and the resulting trimmed 'token'\n+and 'value' will appear in the message like this:\n+\n+------------------------------------------------\n+token: value\n+------------------------------------------------\n+\n+By default, if there are already trailers with the same 'token' the\n+new trailer will appear just after the last trailer with the same\n+'token'. Otherwise it will appear at the end of the message.\n+\n+Note that 'trailers' do not follow and are not intended to follow many\n+rules that are in RFC 822. For example they do not follow the line\n+breaking rules, the encoding rules and probably many other rules.\n+\n+OPTIONS\n+-------\n+--trim-empty::\n+\tIf the 'value' part of any trailer contains only whitespaces,\n+\tthe whole trailer will be removed from the resulting message.\n+\n+----infile=file::\n+\tRead the commit message from `file` instead of the standard\n+\tinput.\n+\n+CONFIGURATION VARIABLES\n+-----------------------\n+\n+trailer.<token>.key::\n+\tThis 'key' will be used instead of 'token' in the\n+\ttrailer. After some alphanumeric characters, it can contain\n+\tsome non alphanumeric characters like ':', '=' or '#' that will\n+\tbe used instead of ':' to separate the token from the value in\n+\tthe trailer, though the default ':' is more standard.\n+\n+trailer.<token>.where::\n+\tThis can be either `after`, which is the default, or\n+\t`before`. If it is `before`, then a trailer with the specified\n+\ttoken, will appear before, instead of after, other trailers\n+\twith the same token, or otherwise at the beginning, instead of\n+\tat the end, of all the trailers.\n+\n+trailer.<token>.ifexist::\n+\tThis option makes it possible to choose what action will be\n+\tperformed when there is already at least one trailer with the\n+\tsame token in the message.\n++\n+The valid values for this option are: `addIfDifferent` (this is the\n+default), `addIfDifferentNeighbor`, `add`, `overwrite` or `doNothing`.\n++\n+With `addIfDifferent`, a new trailer will be added only if no trailer\n+with the same (token, value) pair is already in the message.\n++\n+With `addIfDifferentNeighbor`, a new trailer will be added only if no\n+trailer with the same (token, value) pair is above or below the line\n+where the new trailer will be added.\n++\n+With `add`, a new trailer will be added, even if some trailers with\n+the same (token, value) pair are already in the message.\n++\n+With `overwrite`, the new trailer will overwrite an existing trailer\n+with the same token.\n++\n+With `doNothing`, nothing will be done, that is no new trailer will be\n+added if there is already one with the same token in the message.\n+\n+trailer.<token>.ifmissing::\n+\tThis option makes it possible to choose what action will be\n+\tperformed when there is not yet any trailer with the same\n+\ttoken in the message.\n++\n+The valid values for this option are: `add` (this is the default) and\n+`doNothing`.\n++\n+With `add`, a new trailer will be added.\n++\n+With `doNothing`, nothing will be done.\n+\n+trailer.<token>.command::\n+\tThis option can be used to specify a shell command that will\n+\tbe used to automatically add or modify a trailer with the\n+\tspecified 'token'.\n++\n+When this option is specified, it is like if a special 'token=value'\n+argument is added at the end of the command line, where 'value' will\n+be given by the standard output of the specified command.\n++\n+If the command contains the `$ARG` string, this string will be\n+replaced with the 'value' part of an existing trailer with the same\n+token, if any, before the command is launched.\n++\n+The following environment variables are set when the command is run:\n+GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, GIT_COMMITTER_NAME,\n+GIT_COMMITTER_EMAIL.\n+\n+SEE ALSO\n+--------\n+linkgit:git-commit[1]\n+\n+GIT\n+---\n+Part of the linkgit:git[1] suite\n-- \n1.8.5.2.201.gacc5987\n"},{"id":"233938","messageId":"CAPig+cSbF8hGuVtZfTFtqgeX1HBYfhMbEL-whzzRP3sEax0N9A@mail.gmail.com","threadId":"35765","inReplyTo":"20140130064921.7504.87498.chriscool@tuxfamily.org","subject":"Re: [PATCH v4 01/17] Add data structures and basic functions for commit trailers","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-01-30T09:10:10Z","receivedAt":"2014-01-30T09:10:10Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jan 30, 2014 at 1:49 AM, Christian Couder\n<chriscool@tuxfamily.org> wrote:\n> We will use a doubly linked list to store all information\n> about trailers and their configuration.\n>\n> This way we can easily remove or add trailers to or from\n> trailer lists while traversing the lists in either direction.\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/trailer.c b/trailer.c\n> new file mode 100644\n> index 0000000..aed25e1\n> --- /dev/null\n> +++ b/trailer.c\n> @@ -0,0 +1,48 @@\n> +#include \"cache.h\"\n> +/*\n> + * Copyright (c) 2013 Christian Couder <chriscool@tuxfamily.org>\n> + */\n> +\n> +static int same_token(struct trailer_item *a, struct trailer_item *b, int alnum_len)\n> +{\n> +       return !strncasecmp(a->token, b->token, alnum_len);\n> +}\n\nMaybe these functions defined in the header should all be 'static\ninline' rather than just 'static'? Making them inline would be\nconsistent with functions defined in other git headers.\n\n> +\n> +static int same_value(struct trailer_item *a, struct trailer_item *b)\n> +{\n> +       return !strcasecmp(a->value, b->value);\n> +}\n> +\n> +static int same_trailer(struct trailer_item *a, struct trailer_item *b, int alnum_len)\n> +{\n> +       return same_token(a, b, alnum_len) && same_value(a, b);\n> +}\n> +\n> +/* Get the length of buf from its beginning until its last alphanumeric character */\n> +static size_t alnum_len(const char *buf, size_t len)\n> +{\n> +       while (--len >= 0 && !isalnum(buf[len]));\n\n'len' has type size_t, which is unsigned, so the conditional '--len >=\n0' will always be true (which will result in a crash if 'buf' contains\nno alphanumerics).\n\n> +       return len + 1;\n> +}\n> --\n> 1.8.5.2.201.gacc5987\n"},{"id":"233966","messageId":"CAPig+cTyAMimdvzbX8hCHQfdd-DPKt4N8LThfYmRbvjeDk6adg@mail.gmail.com","threadId":"35765","inReplyTo":"20140130064921.7504.74582.chriscool@tuxfamily.org","subject":"Re: [PATCH v4 05/17] strbuf: add strbuf_isspace()","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-01-31T02:51:15Z","receivedAt":"2014-01-31T02:51:15Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jan 30, 2014 at 1:49 AM, Christian Couder\n<chriscool@tuxfamily.org> wrote:\n> This helper function checks if a strbuf\n> contains only space chars or not.\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/strbuf.c b/strbuf.c\n> index 83caf4a..2124bb8 100644\n> --- a/strbuf.c\n> +++ b/strbuf.c\n> @@ -124,6 +124,13 @@ void strbuf_ltrim(struct strbuf *sb)\n>         sb->buf[sb->len] = '\\0';\n>  }\n>\n> +int strbuf_isspace(struct strbuf *sb)\n> +{\n> +       char *b;\n> +       for (b = sb->buf; *b && isspace(*b); b++);\n\nQuoting from the strbuf documentation:\n\n    ... strbufs may have embedded NULs. An strbuf is NUL\n    terminated for convenience, but no function in the\n    strbuf API actually relies on the string being free of\n    NULs.\n\nSo, the termination condition (*b) of this loop is questionable.\nLooping from 0 to < sb->len makes more sense.\n\n> +       return !*b;\n\nDitto for the return. This will incorrectly return 'true' if an\nembedded NUL is encountered.\n\n> +}\n> +\n>  struct strbuf **strbuf_split_buf(const char *str, size_t slen,\n>                                  int terminator, int max)\n>  {\n"},{"id":"233967","messageId":"CAPig+cRutsBug5cg+Xb-4uxJyAKYz+n4ri80KTmu+kzq67opDg@mail.gmail.com","threadId":"35765","inReplyTo":"20140130064921.7504.74582.chriscool@tuxfamily.org","subject":"Re: [PATCH v4 05/17] strbuf: add strbuf_isspace()","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-01-31T03:09:58Z","receivedAt":"2014-01-31T03:09:58Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jan 30, 2014 at 1:49 AM, Christian Couder\n<chriscool@tuxfamily.org> wrote:\n> This helper function checks if a strbuf\n> contains only space chars or not.\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/strbuf.h b/strbuf.h\n> index 73e80ce..02bff3a 100644\n> --- a/strbuf.h\n> +++ b/strbuf.h\n> @@ -42,6 +42,7 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len) {\n>  extern void strbuf_trim(struct strbuf *);\n>  extern void strbuf_rtrim(struct strbuf *);\n>  extern void strbuf_ltrim(struct strbuf *);\n> +extern int strbuf_isspace(struct strbuf *);\n\nUpdating Documentation/technical/api-strbuf.txt to mention this new\nfunction would be appropriate.\n\n>  extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);\n>\n>  /*\n"},{"id":"233968","messageId":"CAPig+cRd4nTwt1T5GH5qZ04vhUQHbDCNGuO2aZ=DBHDLRQ6pDQ@mail.gmail.com","threadId":"35765","inReplyTo":"20140130064921.7504.3401.chriscool@tuxfamily.org","subject":"Re: [PATCH v4 12/17] strbuf: add strbuf_replace()","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-01-31T03:11:48Z","receivedAt":"2014-01-31T03:11:48Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jan 30, 2014 at 1:49 AM, Christian Couder\n<chriscool@tuxfamily.org> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/strbuf.c b/strbuf.c\n> index 2124bb8..e45e513 100644\n> --- a/strbuf.c\n> +++ b/strbuf.c\n> @@ -197,6 +197,13 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,\n>         strbuf_setlen(sb, sb->len + dlen - len);\n>  }\n>\n> +void strbuf_replace(struct strbuf *sb, const char *a, const char *b)\n> +{\n> +       char *ptr = strstr(sb->buf, a);\n\nThis could be 'const char *'.\n\n> +       if (ptr)\n> +               strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));\n> +}\n> +\n>  void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)\n>  {\n>         strbuf_splice(sb, pos, 0, data, len);\n> diff --git a/strbuf.h b/strbuf.h\n> index 02bff3a..38faf70 100644\n> --- a/strbuf.h\n> +++ b/strbuf.h\n> @@ -111,6 +111,9 @@ extern void strbuf_remove(struct strbuf *, size_t pos, size_t len);\n>  extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,\n>                            const void *, size_t);\n>\n> +/* first occurence of a replaced with b */\n> +extern void strbuf_replace(struct strbuf *, const char *a, const char *b);\n\nUpdating Documentation/technical/api-strbuf.txt to mention this new\nfunction would be appropriate.\n\n>  extern void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size);\n>\n>  extern void strbuf_add(struct strbuf *, const void *, size_t);\n"},{"id":"233969","messageId":"CAPig+cSaYfsyx2YmxOqLxxYLhpgqhznwOzP5CpY7jw=p4sGgpw@mail.gmail.com","threadId":"35765","inReplyTo":"20140130064921.7504.60878.chriscool@tuxfamily.org","subject":"Re: [PATCH v4 02/17] trailer: process trailers from file and arguments","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-01-31T04:02:18Z","receivedAt":"2014-01-31T04:02:18Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jan 30, 2014 at 1:49 AM, Christian Couder\n<chriscool@tuxfamily.org> wrote:\n> This patch implements the logic that process trailers\n> from file and arguments.\n>\n> At the beginning trailers from file are in their own\n> infile_tok doubly linked list, and trailers from\n> arguments are in their own arg_tok doubly linked list.\n>\n> The lists are traversed and when an arg_tok should be\n> \"applied\", it is removed from its list and inserted\n> into the infile_tok list.\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/trailer.c b/trailer.c\n> index aed25e1..e9ccfa5 100644\n> --- a/trailer.c\n> +++ b/trailer.c\n> @@ -46,3 +46,192 @@ static size_t alnum_len(const char *buf, size_t len)\n> +static void apply_arg_if_exist(struct trailer_item *infile_tok,\n> +                              struct trailer_item *arg_tok,\n> +                              int alnum_len)\n> +{\n> +       switch (arg_tok->conf->if_exist) {\n> +       case EXIST_DO_NOTHING:\n> +               free(arg_tok);\n\nThis is freeing arg_tok, but isn't it leaking arg_tok->conf, and\nconf->name, conf->key, conf->command? Ditto for all the other\nfree(arg_tok) invocations elsewhere in the file.\n\n > +               break;\n> +       case EXIST_OVERWRITE:\n> +               free((char *)infile_tok->value);\n> +               infile_tok->value = xstrdup(arg_tok->value);\n> +               free(arg_tok);\n> +               break;\n> +       case EXIST_ADD:\n> +               add_arg_to_infile(infile_tok, arg_tok);\n> +               break;\n> +       case EXIST_ADD_IF_DIFFERENT:\n> +               if (check_if_different(infile_tok, arg_tok, alnum_len, 1))\n> +                       add_arg_to_infile(infile_tok, arg_tok);\n> +               else\n> +                       free(arg_tok);\n> +               break;\n> +       case EXIST_ADD_IF_DIFFERENT_NEIGHBOR:\n> +               if (check_if_different(infile_tok, arg_tok, alnum_len, 0))\n> +                       add_arg_to_infile(infile_tok, arg_tok);\n> +               else\n> +                       free(arg_tok);\n> +               break;\n> +       }\n> +}\n> +\n> +static void process_infile_tok(struct trailer_item *infile_tok,\n> +                              struct trailer_item **arg_tok_first,\n> +                              enum action_where where)\n> +{\n> +       struct trailer_item *arg_tok;\n> +       struct trailer_item *next_arg;\n> +\n> +       int tok_alnum_len = alnum_len(infile_tok->token, strlen(infile_tok->token));\n> +       for (arg_tok = *arg_tok_first; arg_tok; arg_tok = next_arg) {\n> +               next_arg = arg_tok->next;\n> +               if (same_token(infile_tok, arg_tok, tok_alnum_len) &&\n> +                   arg_tok->conf->where == where) {\n> +                       /* Remove arg_tok from list */\n> +                       remove_from_list(arg_tok, arg_tok_first);\n> +                       /* Apply arg */\n> +                       apply_arg_if_exist(infile_tok, arg_tok, tok_alnum_len);\n\nRedundant comments (saying the same thing as the code) can make the\ncode slightly more difficult to read.\n\n> +                       /*\n> +                        * If arg has been added to infile,\n> +                        * then we need to process it too now.\n> +                        */\n> +                       if ((where == WHERE_AFTER ? infile_tok->next : infile_tok->previous) == arg_tok)\n> +                               infile_tok = arg_tok;\n> +               }\n> +       }\n> +}\n"},{"id":"233976","messageId":"CAPig+cSZhZXXDvrAQfsL5FYrSUro89mLa=DXvFGtCnemeM4oSg@mail.gmail.com","threadId":"35765","inReplyTo":"20140130064921.7504.3711.chriscool@tuxfamily.org","subject":"Re: [PATCH v4 03/17] trailer: read and process config information","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-01-31T15:43:47Z","receivedAt":"2014-01-31T15:43:47Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jan 30, 2014 at 1:49 AM, Christian Couder\n<chriscool@tuxfamily.org> wrote:\n> This patch implements reading the configuration\n> to get trailer information, and then processing\n> it and storing it in a doubly linked list.\n>\n> The config information is stored in the list\n> whose first item is pointed to by:\n>\n> static struct trailer_item *first_conf_item;\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/trailer.c b/trailer.c\n> index e9ccfa5..d979a0f 100644\n> --- a/trailer.c\n> +++ b/trailer.c\n> @@ -235,3 +237,128 @@ static void process_trailers_lists(struct trailer_item **infile_tok_first,\n> +static struct trailer_item *get_conf_item(char *name)\n\nShould this be 'const char *'?\n\n> +{\n> +       struct trailer_item *item;\n> +       struct trailer_item *previous;\n> +\n> +       /* Look up item with same name */\n> +       for (previous = NULL, item = first_conf_item;\n> +            item;\n> +            previous = item, item = item->next) {\n> +               if (!strcasecmp(item->conf->name, name))\n> +                       return item;\n> +       }\n> +\n> +       /* Item does not already exists, create it */\n> +       item = xcalloc(sizeof(struct trailer_item), 1);\n> +       item->conf = xcalloc(sizeof(struct conf_info), 1);\n> +       item->conf->name = xstrdup(name);\n> +\n> +       if (!previous)\n> +               first_conf_item = item;\n> +       else {\n> +               previous->next = item;\n> +               item->previous = previous;\n> +       }\n> +\n> +       return item;\n> +}\n> +\n> +static int git_trailer_config(const char *conf_key, const char *value, void *cb)\n> +{\n> +       if (starts_with(conf_key, \"trailer.\")) {\n> +               const char *orig_conf_key = conf_key;\n> +               struct trailer_item *item;\n> +               struct conf_info *conf;\n> +               char *name;\n> +               enum trailer_info_type type;\n> +\n> +               conf_key += 8;\n> +               if (!set_name_and_type(conf_key, \".key\", TRAILER_VALUE, &name, &type) &&\n> +                   !set_name_and_type(conf_key, \".command\", TRAILER_COMMAND, &name, &type) &&\n> +                   !set_name_and_type(conf_key, \".where\", TRAILER_WHERE, &name, &type) &&\n> +                   !set_name_and_type(conf_key, \".ifexist\", TRAILER_IF_EXIST, &name, &type) &&\n> +                   !set_name_and_type(conf_key, \".ifmissing\", TRAILER_IF_MISSING, &name, &type))\n> +                       return 0;\n> +\n> +               item = get_conf_item(name);\n> +               conf = item->conf;\n> +\n> +               if (type == TRAILER_VALUE) {\n> +                       if (conf->key)\n> +                               warning(_(\"more than one %s\"), orig_conf_key);\n> +                       conf->key = xstrdup(value);\n> +               } else if (type == TRAILER_COMMAND) {\n> +                       if (conf->command)\n> +                               warning(_(\"more than one %s\"), orig_conf_key);\n> +                       conf->command = xstrdup(value);\n> +               } else if (type == TRAILER_WHERE) {\n> +                       if (set_where(conf, value))\n> +                               warning(_(\"unknown value '%s' for key '%s'\"), value, orig_conf_key);\n> +               } else if (type == TRAILER_IF_EXIST) {\n> +                       if (set_if_exist(conf, value))\n> +                               warning(_(\"unknown value '%s' for key '%s'\"), value, orig_conf_key);\n> +               } else if (type == TRAILER_IF_MISSING) {\n> +                       if (set_if_missing(conf, value))\n> +                               warning(_(\"unknown value '%s' for key '%s'\"), value, orig_conf_key);\n> +               } else\n> +                       die(\"internal bug in trailer.c\");\n\nA 'switch' statement might be more idiomatic and easier to read.\n\n> +       }\n> +       return 0;\n> +}\n> --\n> 1.8.5.2.201.gacc5987\n"},{"id":"233977","messageId":"CAPig+cRMBDr1D+3ad6y95ucxynO1qbRnsbUAfjtLkoR3+mjxZg@mail.gmail.com","threadId":"35765","inReplyTo":"20140130064921.7504.75845.chriscool@tuxfamily.org","subject":"Re: [PATCH v4 08/17] trailer: add interpret-trailers command","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-01-31T16:10:31Z","receivedAt":"2014-01-31T16:10:31Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jan 30, 2014 at 1:49 AM, Christian Couder\n<chriscool@tuxfamily.org> wrote:\n> This patch adds the \"git interpret-trailers\" command.\n> This command uses the previously added process_trailers()\n> function in trailer.c.\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c\n> new file mode 100644\n> index 0000000..04b0ae2\n> --- /dev/null\n> +++ b/builtin/interpret-trailers.c\n> @@ -0,0 +1,36 @@\n> +int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)\n> +{\n> +       const char *infile = NULL;\n> +       int trim_empty = 0;\n> +\n> +       struct option options[] = {\n> +               OPT_BOOL(0, \"trim-empty\", &trim_empty, N_(\"trim empty trailers\")),\n> +               OPT_FILENAME(0, \"infile\", &infile, N_(\"use message from file\")),\n> +               OPT_END()\n> +       };\n> +\n> +       argc = parse_options(argc, argv, prefix, options,\n> +                            git_interpret_trailers_usage, 0);\n> +\n> +       process_trailers(infile, trim_empty, argc, argv);\n> +\n> +       return 0;\n> +}\n> diff --git a/trailer.h b/trailer.h\n> new file mode 100644\n> index 0000000..9db4459\n> --- /dev/null\n> +++ b/trailer.h\n> @@ -0,0 +1,6 @@\n> +#ifndef TRAILER_H\n> +#define TRAILER_H\n> +\n> +void process_trailers(const char *infile, int trim_empty, int argc, const char **argv);\n> +\n> +#endif /* TRAILER_H */\n\nOne might reasonably expect trailer.h and the process_trailers()\ndeclaration to be introduced by patch 7/17 (\"trailer: put all the\nprocessing together and print\") in which process_trailers() is defined\nin trailer.c.\n"},{"id":"233980","messageId":"CAPig+cS98QTH-CxpRtXUqf0jYPC5wEGcb-PEqdk8CQAtbxBkhQ@mail.gmail.com","threadId":"35765","inReplyTo":"20140130064921.7504.55916.chriscool@tuxfamily.org","subject":"Re: [PATCH v4 04/17] trailer: process command line trailer arguments","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-01-31T16:30:17Z","receivedAt":"2014-01-31T16:30:17Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jan 30, 2014 at 1:49 AM, Christian Couder\n<chriscool@tuxfamily.org> wrote:\n> This patch parses the trailer command line arguments\n> and put the result into an arg_tok doubly linked\n> list.\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/trailer.c b/trailer.c\n> index d979a0f..f48fd94 100644\n> --- a/trailer.c\n> +++ b/trailer.c\n> @@ -362,3 +362,80 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)\n>         }\n>         return 0;\n>  }\n> +\n> +static void parse_trailer(struct strbuf *tok, struct strbuf *val, const char *trailer)\n> +{\n> +       char *end = strchr(trailer, '=');\n\nThis can be 'const char *'.\n\n> +       if (!end)\n> +               end = strchr(trailer, ':');\n> +       if (end) {\n> +               strbuf_add(tok, trailer, end - trailer);\n> +               strbuf_trim(tok);\n> +               strbuf_addstr(val, end + 1);\n> +               strbuf_trim(val);\n> +       } else {\n> +               strbuf_addstr(tok, trailer);\n> +               strbuf_trim(tok);\n> +       }\n> +}\n> +\n> +static struct trailer_item *create_trailer_item(const char *string)\n> +{\n> +       struct strbuf tok = STRBUF_INIT;\n> +       struct strbuf val = STRBUF_INIT;\n> +       struct trailer_item *new;\n> +       struct trailer_item *item;\n> +       int tok_alnum_len;\n> +\n> +       parse_trailer(&tok, &val, string);\n> +\n> +       tok_alnum_len = alnum_len(tok.buf, tok.len);\n> +\n> +       /* Lookup if the token matches something in the config */\n> +       for (item = first_conf_item; item; item = item->next) {\n> +               if (!strncasecmp(tok.buf, item->conf->key, tok_alnum_len) ||\n> +                   !strncasecmp(tok.buf, item->conf->name, tok_alnum_len)) {\n> +                       new = xcalloc(sizeof(struct trailer_item), 1);\n\nsizeof(*new) would be more future-proof.\n\n> +                       new->conf = item->conf;\n> +                       new->token = xstrdup(item->conf->key);\n> +                       new->value = strbuf_detach(&val, NULL);\n> +                       strbuf_release(&tok);\n> +                       return new;\n> +               }\n> +       }\n> +\n> +       new = xcalloc(sizeof(struct trailer_item), 1);\n\nDitto.\n\n> +       new->conf = xcalloc(sizeof(struct conf_info), 1);\n> +       new->token = strbuf_detach(&tok, NULL);\n> +       new->value = strbuf_detach(&val, NULL);\n> +\n> +       return new;\n> +}\n"},{"id":"233981","messageId":"CAPig+cQu0ZPYH9xq6jvw3+FgsMy4T8jTP+2NMnS6qtdAqbcCiQ@mail.gmail.com","threadId":"35765","inReplyTo":"20140130064921.7504.62053.chriscool@tuxfamily.org","subject":"Re: [PATCH v4 06/17] trailer: parse trailers from input file","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-01-31T16:45:46Z","receivedAt":"2014-01-31T16:45:46Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jan 30, 2014 at 1:49 AM, Christian Couder\n<chriscool@tuxfamily.org> wrote:\n> This patch reads trailers from an input file, parses\n> them and puts the result into a doubly linked list.\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  trailer.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n>  1 file changed, 62 insertions(+)\n>\n> diff --git a/trailer.c b/trailer.c\n> index f48fd94..084b3e1 100644\n> --- a/trailer.c\n> +++ b/trailer.c\n> @@ -439,3 +439,65 @@ static struct trailer_item *process_command_line_args(int argc, const char **arg\n> +static void process_input_file(const char *infile,\n> +                              struct trailer_item **infile_tok_first,\n> +                              struct trailer_item **infile_tok_last)\n> +{\n> +       struct strbuf **lines = read_input_file(infile);\n> +       int start = find_trailer_start(lines);\n> +       int i;\n> +\n> +       /* Print non trailer lines as is */\n> +       for (i = 0; lines[i] && i < start; i++) {\n> +               printf(\"%s\", lines[i]->buf);\n> +       }\n> +\n> +       /* Parse trailer lines */\n> +       for (i = start; lines[i]; i++) {\n> +               struct trailer_item *new = create_trailer_item(lines[i]->buf);\n> +               add_trailer_item(infile_tok_first, infile_tok_last, new);\n\nLeaking 'lines'. Perhaps you want to invoke strbuf_list_free() here.\n\n> +       }\n> +}\n> --\n> 1.8.5.2.201.gacc5987\n"},{"id":"234030","messageId":"CAPig+cTVN+MqjhouCsE2gi4GWd14dLH6=5QrbEfTTgz5jLxtKA@mail.gmail.com","threadId":"35765","inReplyTo":"20140130064921.7504.57934.chriscool@tuxfamily.org","subject":"Re: [PATCH v4 10/17] trailer: if no input file is passed, read from stdin","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-02-02T09:50:36Z","receivedAt":"2014-02-02T09:50:36Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jan 30, 2014 at 1:49 AM, Christian Couder\n<chriscool@tuxfamily.org> wrote:\n> It is simpler and more natural if the \"git interpret-trailers\"\n> is made a filter as its output already goes to sdtout.\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/trailer.c b/trailer.c\n> index 8681aed..73a65e0 100644\n> --- a/trailer.c\n> +++ b/trailer.c\n> @@ -464,8 +464,13 @@ static struct strbuf **read_input_file(const char *infile)\n>  {\n>         struct strbuf sb = STRBUF_INIT;\n>\n> -       if (strbuf_read_file(&sb, infile, 0) < 0)\n> -               die_errno(_(\"could not read input file '%s'\"), infile);\n> +       if (infile) {\n> +               if (strbuf_read_file(&sb, infile, 0) < 0)\n> +                       die_errno(_(\"could not read input file '%s'\"), infile);\n> +       } else {\n> +               if (strbuf_read(&sb, fileno(stdin), 0) < 0)\n\nstrbuf_fread(), perhaps?\n\n> +                       die_errno(_(\"could not read from stdin\"));\n> +       }\n>\n>         return strbuf_split(&sb, '\\n');\n>  }\n> @@ -530,10 +535,8 @@ void process_trailers(const char *infile, int trim_empty, int argc, const char *\n>\n>         git_config(git_trailer_config, NULL);\n>\n> -       /* Print the non trailer part of infile */\n> -       if (infile) {\n> -               process_input_file(infile, &infile_tok_first, &infile_tok_last);\n> -       }\n> +       /* Print the non trailer part of infile (or stdin if infile is NULL) */\n> +       process_input_file(infile, &infile_tok_first, &infile_tok_last);\n>\n>         arg_tok_first = process_command_line_args(argc, argv);\n>\n> --\n> 1.8.5.2.201.gacc5987\n"},{"id":"234031","messageId":"CAPig+cQ6J1nEJhecmS_T8L5mxJj9WcSBQ7hFA=XKcDChGHNSnQ@mail.gmail.com","threadId":"35765","inReplyTo":"20140130064921.7504.42810.chriscool@tuxfamily.org","subject":"Re: [PATCH v4 11/17] trailer: add new_trailer_item() function","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-02-02T10:10:20Z","receivedAt":"2014-02-02T10:10:20Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jan 30, 2014 at 1:49 AM, Christian Couder\n<chriscool@tuxfamily.org> wrote:\n> This is a small refactoring to prepare for the next steps.\n\nSince this is all brand new code, wouldn't it make more sense to\nstructure it in this fashion in the first place when introduced in\npatch 4/17? It's not clear why it should be introduced with poorer\nstructure and then later cleaned up.\n\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  trailer.c | 31 +++++++++++++++++++------------\n>  1 file changed, 19 insertions(+), 12 deletions(-)\n>\n> diff --git a/trailer.c b/trailer.c\n> index 73a65e0..430ff39 100644\n> --- a/trailer.c\n> +++ b/trailer.c\n> @@ -399,11 +399,27 @@ static void parse_trailer(struct strbuf *tok, struct strbuf *val, const char *tr\n>         }\n>  }\n>\n> +static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,\n> +                                            const char* tok, const char* val)\n> +{\n> +       struct trailer_item *new = xcalloc(sizeof(struct trailer_item), 1);\n> +       new->value = val;\n> +\n> +       if (conf_item) {\n> +               new->conf = conf_item->conf;\n> +               new->token = xstrdup(conf_item->conf->key);\n> +       } else {\n> +               new->conf = xcalloc(sizeof(struct conf_info), 1);\n> +               new->token = tok;\n> +       }\n> +\n> +       return new;\n> +}\n> +\n>  static struct trailer_item *create_trailer_item(const char *string)\n>  {\n>         struct strbuf tok = STRBUF_INIT;\n>         struct strbuf val = STRBUF_INIT;\n> -       struct trailer_item *new;\n>         struct trailer_item *item;\n>         int tok_alnum_len;\n>\n> @@ -415,21 +431,12 @@ static struct trailer_item *create_trailer_item(const char *string)\n>         for (item = first_conf_item; item; item = item->next) {\n>                 if (!strncasecmp(tok.buf, item->conf->key, tok_alnum_len) ||\n>                     !strncasecmp(tok.buf, item->conf->name, tok_alnum_len)) {\n> -                       new = xcalloc(sizeof(struct trailer_item), 1);\n> -                       new->conf = item->conf;\n> -                       new->token = xstrdup(item->conf->key);\n> -                       new->value = strbuf_detach(&val, NULL);\n>                         strbuf_release(&tok);\n> -                       return new;\n> +                       return new_trailer_item(item, NULL, strbuf_detach(&val, NULL));\n>                 }\n>         }\n>\n> -       new = xcalloc(sizeof(struct trailer_item), 1);\n> -       new->conf = xcalloc(sizeof(struct conf_info), 1);\n> -       new->token = strbuf_detach(&tok, NULL);\n> -       new->value = strbuf_detach(&val, NULL);\n> -\n> -       return new;\n> +       return new_trailer_item(NULL, strbuf_detach(&tok, NULL), strbuf_detach(&val, NULL));;\n>  }\n>\n>  static void add_trailer_item(struct trailer_item **first,\n> --\n> 1.8.5.2.201.gacc5987\n>\n>\n"},{"id":"234395","messageId":"20140206.211228.1189883848366712745.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"CAPig+cRMBDr1D+3ad6y95ucxynO1qbRnsbUAfjtLkoR3+mjxZg@mail.gmail.com","subject":"Re: [PATCH v4 08/17] trailer: add interpret-trailers command","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-02-06T20:12:28Z","receivedAt":"2014-02-06T20:12:28Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"From: Eric Sunshine <sunshine@sunshineco.com>\n>\n> On Thu, Jan 30, 2014 at 1:49 AM, Christian Couder\n> <chriscool@tuxfamily.org> wrote:\n>> diff --git a/trailer.h b/trailer.h\n>> new file mode 100644\n>> index 0000000..9db4459\n>> --- /dev/null\n>> +++ b/trailer.h\n>> @@ -0,0 +1,6 @@\n>> +#ifndef TRAILER_H\n>> +#define TRAILER_H\n>> +\n>> +void process_trailers(const char *infile, int trim_empty, int argc, const char **argv);\n>> +\n>> +#endif /* TRAILER_H */\n> \n> One might reasonably expect trailer.h and the process_trailers()\n> declaration to be introduced by patch 7/17 (\"trailer: put all the\n> processing together and print\") in which process_trailers() is defined\n> in trailer.c.\n\nOn the other hand, I think that it is not so nice to add a header file\nlike trailer.h unless it is included at least once.\n\nMaybe I can squash this patch with the previous one, but then the\nseries might be a little bit more difficult to understand.\n\nThanks,\nChristian.\n"},{"id":"234396","messageId":"20140206.211639.74249240425700292.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"CAPig+cTVN+MqjhouCsE2gi4GWd14dLH6=5QrbEfTTgz5jLxtKA@mail.gmail.com","subject":"Re: [PATCH v4 10/17] trailer: if no input file is passed, read from stdin","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-02-06T20:16:39Z","receivedAt":"2014-02-06T20:16:39Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"From: Eric Sunshine <sunshine@sunshineco.com>\n>\n> On Thu, Jan 30, 2014 at 1:49 AM, Christian Couder\n> <chriscool@tuxfamily.org> wrote:\n>> It is simpler and more natural if the \"git interpret-trailers\"\n>> is made a filter as its output already goes to sdtout.\n>>\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>> diff --git a/trailer.c b/trailer.c\n>> index 8681aed..73a65e0 100644\n>> --- a/trailer.c\n>> +++ b/trailer.c\n>> @@ -464,8 +464,13 @@ static struct strbuf **read_input_file(const char *infile)\n>>  {\n>>         struct strbuf sb = STRBUF_INIT;\n>>\n>> -       if (strbuf_read_file(&sb, infile, 0) < 0)\n>> -               die_errno(_(\"could not read input file '%s'\"), infile);\n>> +       if (infile) {\n>> +               if (strbuf_read_file(&sb, infile, 0) < 0)\n>> +                       die_errno(_(\"could not read input file '%s'\"), infile);\n>> +       } else {\n>> +               if (strbuf_read(&sb, fileno(stdin), 0) < 0)\n> \n> strbuf_fread(), perhaps?\n\nI chose strbuf_read() because it can be passed 0 as a size hint, while\nstrbuf_fread() must be passed an exact size.\n\n(As we might read from stdin, we might not be able to know the exact\nsize before we start reading.)\n\nThanks,\nChristian.\n"},{"id":"234419","messageId":"xmqq4n4byknr.fsf@gitster.dls.corp.google.com","threadId":"35765","inReplyTo":"20140130064921.7504.57934.chriscool@tuxfamily.org","subject":"Re: [PATCH v4 10/17] trailer: if no input file is passed, read from stdin","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2014-02-06T21:51:36Z","receivedAt":"2014-02-06T21:51:36Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Christian Couder <chriscool@tuxfamily.org> writes:\n\n> It is simpler and more natural if the \"git interpret-trailers\"\n> is made a filter as its output already goes to sdtout.\n\nsdtout???\n\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  builtin/interpret-trailers.c  |  2 +-\n>  t/t7513-interpret-trailers.sh |  7 +++++++\n>  trailer.c                     | 15 +++++++++------\n>  3 files changed, 17 insertions(+), 7 deletions(-)\n>\n> diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c\n> index 04b0ae2..ae8e561 100644\n> --- a/builtin/interpret-trailers.c\n> +++ b/builtin/interpret-trailers.c\n> @@ -23,7 +23,7 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)\n>  \n>  \tstruct option options[] = {\n>  \t\tOPT_BOOL(0, \"trim-empty\", &trim_empty, N_(\"trim empty trailers\")),\n> -\t\tOPT_FILENAME(0, \"infile\", &infile, N_(\"use message from file\")),\n> +\t\tOPT_FILENAME(0, \"infile\", &infile, N_(\"use message from file, instead of stdin\")),\n>  \t\tOPT_END()\n>  \t};\n>  \n> diff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh\n> index 8be333c..f5ef81f 100755\n> --- a/t/t7513-interpret-trailers.sh\n> +++ b/t/t7513-interpret-trailers.sh\n> @@ -205,4 +205,11 @@ test_expect_success 'using \"ifMissing = doNothing\"' '\n>  \ttest_cmp expected actual\n>  '\n>  \n> +test_expect_success 'with input from stdin' '\n> +\tcat complex_message_body >expected &&\n> +\tprintf \"Bug #42\\nFixes: \\nAcked-by= \\nAcked-by= Junio\\nAcked-by= Peff\\nReviewed-by: \\nSigned-off-by: \\n\" >>expected &&\n> +\tgit interpret-trailers \"review:\" \"fix=53\" \"cc=Linus\" \"ack: Junio\" \"fix=22\" \"bug: 42\" \"ack: Peff\" < complex_message >actual &&\n> +\ttest_cmp expected actual\n> +'\n> +\n>  test_done\n> diff --git a/trailer.c b/trailer.c\n> index 8681aed..73a65e0 100644\n> --- a/trailer.c\n> +++ b/trailer.c\n> @@ -464,8 +464,13 @@ static struct strbuf **read_input_file(const char *infile)\n>  {\n>  \tstruct strbuf sb = STRBUF_INIT;\n>  \n> -\tif (strbuf_read_file(&sb, infile, 0) < 0)\n> -\t\tdie_errno(_(\"could not read input file '%s'\"), infile);\n> +\tif (infile) {\n> +\t\tif (strbuf_read_file(&sb, infile, 0) < 0)\n> +\t\t\tdie_errno(_(\"could not read input file '%s'\"), infile);\n> +\t} else {\n> +\t\tif (strbuf_read(&sb, fileno(stdin), 0) < 0)\n> +\t\t\tdie_errno(_(\"could not read from stdin\"));\n> +\t}\n>  \n>  \treturn strbuf_split(&sb, '\\n');\n>  }\n> @@ -530,10 +535,8 @@ void process_trailers(const char *infile, int trim_empty, int argc, const char *\n>  \n>  \tgit_config(git_trailer_config, NULL);\n>  \n> -\t/* Print the non trailer part of infile */\n> -\tif (infile) {\n> -\t\tprocess_input_file(infile, &infile_tok_first, &infile_tok_last);\n> -\t}\n> +\t/* Print the non trailer part of infile (or stdin if infile is NULL) */\n> +\tprocess_input_file(infile, &infile_tok_first, &infile_tok_last);\n>  \n>  \targ_tok_first = process_command_line_args(argc, argv);\n"},{"id":"234546","messageId":"20140209.145545.84719853892885831.chriscool@tuxfamily.org","threadId":"35765","inReplyTo":"xmqq4n4byknr.fsf@gitster.dls.corp.google.com","subject":"Re: [PATCH v4 10/17] trailer: if no input file is passed, read from stdin","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2014-02-09T13:55:45Z","receivedAt":"2014-02-09T13:55:45Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\nSubject: Re: [PATCH v4 10/17] trailer: if no input file is passed, read from stdin\nDate: Thu, 06 Feb 2014 13:51:36 -0800\n\n> Christian Couder <chriscool@tuxfamily.org> writes:\n> \n>> It is simpler and more natural if the \"git interpret-trailers\"\n>> is made a filter as its output already goes to sdtout.\n> \n> sdtout???\n\nSorry, I have a hard time spelling porprelly these days :-)\n\nThanks,\nChristian.\n"}]}