{"thread":{"id":"46363","subject":"[PATCH 0/3] interpret-trailers: add --where, --if-exists, --if-missing","startedAt":"2017-07-12T13:46:57Z","lastAt":"2017-07-12T21:34:35Z","messageCount":10,"participants":["Paolo Bonzini","Christian Couder","Jonathan Tan"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"324260","messageId":"20170712134646.17179-1-bonzini@gnu.org","threadId":"46363","inReplyTo":null,"subject":"[PATCH 0/3] interpret-trailers: add --where, --if-exists, --if-missing","fromName":"Paolo Bonzini","fromEmail":"bonzini@gnu.org","sentAt":"2017-07-12T13:46:43Z","receivedAt":"2017-07-12T13:46:57Z","isPatch":true,"sender":{"key":"bonzini@gnu.org","avatar":"https://avatars.githubusercontent.com/u/42082?v=4"},"body":"From: Paolo Bonzini <pbonzini@redhat.com>\n\nThese options are useful to experiment with \"git interpret-trailers\"\nwithout having to tinker with .gitconfig.  It can also be useful in the\noddball case where you want a different placement for the trailer.\n\nThe case that stimulated the creation of the patches was configuring\n\n     trailer.signed-off-by.where = end\n\nand then wanting \"--where before\" when a patch author forgets his\nSigned-off-by and provides it in a separate email.\n\nPaolo\n\nPaolo Bonzini (3):\n  trailers: create struct trailer_opts\n  trailers: export action enums and corresponding lookup functions\n  interpret-trailers: add options for actions\n\n Documentation/git-interpret-trailers.txt | 16 ++++++\n builtin/interpret-trailers.c             | 53 +++++++++++++++---\n t/t7513-interpret-trailers.sh            | 51 ++++++++++++++++++\n trailer.c                                | 93 ++++++++++++++++++++------------\n trailer.h                                | 35 +++++++++++-\n 5 files changed, 205 insertions(+), 43 deletions(-)\n\n-- \n2.13.0\n\n"},{"id":"324261","messageId":"20170712134646.17179-2-bonzini@gnu.org","threadId":"46363","inReplyTo":"20170712134646.17179-1-bonzini@gnu.org","subject":"[PATCH 1/3] trailers: create struct trailer_opts","fromName":"Paolo Bonzini","fromEmail":"bonzini@gnu.org","sentAt":"2017-07-12T13:46:44Z","receivedAt":"2017-07-12T13:46:59Z","isPatch":true,"sender":{"key":"bonzini@gnu.org","avatar":"https://avatars.githubusercontent.com/u/42082?v=4"},"body":"From: Paolo Bonzini <pbonzini@redhat.com>\n\nPass the command-line arguments as a pointer to a new struct.  This\nwill be extended soon to include more options.\n\nSigned-off-by: Paolo Bonzini <pbonzini@redhat.com>\n---\n builtin/interpret-trailers.c | 13 ++++++-------\n trailer.c                    | 14 ++++++++------\n trailer.h                    |  7 ++++++-\n 3 files changed, 20 insertions(+), 14 deletions(-)\n\ndiff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c\nindex 175f14797..6528680b5 100644\n--- a/builtin/interpret-trailers.c\n+++ b/builtin/interpret-trailers.c\n@@ -18,13 +18,12 @@ static const char * const git_interpret_trailers_usage[] = {\n \n int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)\n {\n-\tint in_place = 0;\n-\tint trim_empty = 0;\n+\tstruct trailer_opts opts = { 0 };\n \tstruct string_list trailers = STRING_LIST_INIT_NODUP;\n \n \tstruct option options[] = {\n-\t\tOPT_BOOL(0, \"in-place\", &in_place, N_(\"edit files in place\")),\n-\t\tOPT_BOOL(0, \"trim-empty\", &trim_empty, N_(\"trim empty trailers\")),\n+\t\tOPT_BOOL(0, \"in-place\", &opts.in_place, N_(\"edit files in place\")),\n+\t\tOPT_BOOL(0, \"trim-empty\", &opts.trim_empty, N_(\"trim empty trailers\")),\n \t\tOPT_STRING_LIST(0, \"trailer\", &trailers, N_(\"trailer\"),\n \t\t\t\tN_(\"trailer(s) to add\")),\n \t\tOPT_END()\n@@ -36,11 +35,11 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)\n \tif (argc) {\n \t\tint i;\n \t\tfor (i = 0; i < argc; i++)\n-\t\t\tprocess_trailers(argv[i], in_place, trim_empty, &trailers);\n+\t\t\tprocess_trailers(argv[i], &opts, &trailers);\n \t} else {\n-\t\tif (in_place)\n+\t\tif (opts.in_place)\n \t\t\tdie(_(\"no input file given for in-place editing\"));\n-\t\tprocess_trailers(NULL, in_place, trim_empty, &trailers);\n+\t\tprocess_trailers(NULL, &opts, &trailers);\n \t}\n \n \tstring_list_clear(&trailers, 0);\ndiff --git a/trailer.c b/trailer.c\nindex 751b56c00..a3eb42818 100644\n--- a/trailer.c\n+++ b/trailer.c\n@@ -164,13 +164,14 @@ static void print_tok_val(FILE *outfile, const char *tok, const char *val)\n \t\tfprintf(outfile, \"%s%c %s\\n\", tok, separators[0], val);\n }\n \n-static void print_all(FILE *outfile, struct list_head *head, int trim_empty)\n+static void print_all(FILE *outfile, struct list_head *head,\n+\t\t      struct trailer_opts *opts)\n {\n \tstruct list_head *pos;\n \tstruct trailer_item *item;\n \tlist_for_each(pos, head) {\n \t\titem = list_entry(pos, struct trailer_item, list);\n-\t\tif (!trim_empty || strlen(item->value) > 0)\n+\t\tif (!opts->trim_empty || strlen(item->value) > 0)\n \t\t\tprint_tok_val(outfile, item->token, item->value);\n \t}\n }\n@@ -968,7 +969,8 @@ static FILE *create_in_place_tempfile(const char *file)\n \treturn outfile;\n }\n \n-void process_trailers(const char *file, int in_place, int trim_empty, struct string_list *trailers)\n+void process_trailers(const char *file, struct trailer_opts *opts,\n+\t\t      struct string_list *trailers)\n {\n \tLIST_HEAD(head);\n \tLIST_HEAD(arg_head);\n@@ -980,7 +982,7 @@ void process_trailers(const char *file, int in_place, int trim_empty, struct str\n \n \tread_input_file(&sb, file);\n \n-\tif (in_place)\n+\tif (opts->in_place)\n \t\toutfile = create_in_place_tempfile(file);\n \n \t/* Print the lines before the trailers */\n@@ -990,14 +992,14 @@ void process_trailers(const char *file, int in_place, int trim_empty, struct str\n \n \tprocess_trailers_lists(&head, &arg_head);\n \n-\tprint_all(outfile, &head, trim_empty);\n+\tprint_all(outfile, &head, opts);\n \n \tfree_all(&head);\n \n \t/* Print the lines after the trailers as is */\n \tfwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);\n \n-\tif (in_place)\n+\tif (opts->in_place)\n \t\tif (rename_tempfile(&trailers_tempfile, file))\n \t\t\tdie_errno(_(\"could not rename temporary file to %s\"), file);\n \ndiff --git a/trailer.h b/trailer.h\nindex 65cc5d79c..e90ba1270 100644\n--- a/trailer.h\n+++ b/trailer.h\n@@ -1,6 +1,11 @@\n #ifndef TRAILER_H\n #define TRAILER_H\n \n+struct trailer_opts {\n+\tint in_place;\n+\tint trim_empty;\n+};\n+\n struct trailer_info {\n \t/*\n \t * True if there is a blank line before the location pointed to by\n@@ -22,7 +27,7 @@ struct trailer_info {\n \tsize_t trailer_nr;\n };\n \n-void process_trailers(const char *file, int in_place, int trim_empty,\n+void process_trailers(const char *file, struct trailer_opts *opts,\n \t\t      struct string_list *trailers);\n \n void trailer_info_get(struct trailer_info *info, const char *str);\n-- \n2.13.0\n\n\n"},{"id":"324262","messageId":"20170712134646.17179-3-bonzini@gnu.org","threadId":"46363","inReplyTo":"20170712134646.17179-1-bonzini@gnu.org","subject":"[PATCH 2/3] trailers: export action enums and corresponding lookup functions","fromName":"Paolo Bonzini","fromEmail":"bonzini@gnu.org","sentAt":"2017-07-12T13:46:45Z","receivedAt":"2017-07-12T13:47:01Z","isPatch":true,"sender":{"key":"bonzini@gnu.org","avatar":"https://avatars.githubusercontent.com/u/42082?v=4"},"body":"From: Paolo Bonzini <pbonzini@redhat.com>\n\nSeparate the mechanical changes out of the next patch.  The functions\nare changed to take a pointer to enum, because struct conf_info is not\ngoing to be public.\n\nWrite down the defaults explicitly in default_conf_info, since they are\nnot anymore close to default_conf_info and it's not obvious which\nconstant has value 0.\n\nSigned-off-by: Paolo Bonzini <pbonzini@redhat.com>\n---\n trailer.c | 51 +++++++++++++++++++++++++--------------------------\n trailer.h | 22 ++++++++++++++++++++++\n 2 files changed, 47 insertions(+), 26 deletions(-)\n\ndiff --git a/trailer.c b/trailer.c\nindex a3eb42818..558c52431 100644\n--- a/trailer.c\n+++ b/trailer.c\n@@ -10,11 +10,6 @@\n  * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>\n  */\n \n-enum action_where { WHERE_END, WHERE_AFTER, WHERE_BEFORE, WHERE_START };\n-enum action_if_exists { EXISTS_ADD_IF_DIFFERENT_NEIGHBOR, EXISTS_ADD_IF_DIFFERENT,\n-\t\t\tEXISTS_ADD, EXISTS_REPLACE, EXISTS_DO_NOTHING };\n-enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };\n-\n struct conf_info {\n \tchar *name;\n \tchar *key;\n@@ -24,7 +19,11 @@ struct conf_info {\n \tenum action_if_missing if_missing;\n };\n \n-static struct conf_info default_conf_info;\n+static struct conf_info default_conf_info = {\n+\t.where = WHERE_END,\n+\t.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,\n+\t.if_missing = MISSING_ADD,\n+};\n \n struct trailer_item {\n \tstruct list_head list;\n@@ -374,44 +373,44 @@ static void process_trailers_lists(struct list_head *head,\n \t}\n }\n \n-static int set_where(struct conf_info *item, const char *value)\n+int set_where(enum action_where *item, const char *value)\n {\n \tif (!strcasecmp(\"after\", value))\n-\t\titem->where = WHERE_AFTER;\n+\t\t*item = WHERE_AFTER;\n \telse if (!strcasecmp(\"before\", value))\n-\t\titem->where = WHERE_BEFORE;\n+\t\t*item = WHERE_BEFORE;\n \telse if (!strcasecmp(\"end\", value))\n-\t\titem->where = WHERE_END;\n+\t\t*item = WHERE_END;\n \telse if (!strcasecmp(\"start\", value))\n-\t\titem->where = WHERE_START;\n+\t\t*item = WHERE_START;\n \telse\n \t\treturn -1;\n \treturn 0;\n }\n \n-static int set_if_exists(struct conf_info *item, const char *value)\n+int set_if_exists(enum action_if_exists *item, const char *value)\n {\n \tif (!strcasecmp(\"addIfDifferent\", value))\n-\t\titem->if_exists = EXISTS_ADD_IF_DIFFERENT;\n+\t\t*item = EXISTS_ADD_IF_DIFFERENT;\n \telse if (!strcasecmp(\"addIfDifferentNeighbor\", value))\n-\t\titem->if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;\n+\t\t*item = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;\n \telse if (!strcasecmp(\"add\", value))\n-\t\titem->if_exists = EXISTS_ADD;\n+\t\t*item = EXISTS_ADD;\n \telse if (!strcasecmp(\"replace\", value))\n-\t\titem->if_exists = EXISTS_REPLACE;\n+\t\t*item = EXISTS_REPLACE;\n \telse if (!strcasecmp(\"doNothing\", value))\n-\t\titem->if_exists = EXISTS_DO_NOTHING;\n+\t\t*item = EXISTS_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+int set_if_missing(enum action_if_missing *item, const char *value)\n {\n \tif (!strcasecmp(\"doNothing\", value))\n-\t\titem->if_missing = MISSING_DO_NOTHING;\n+\t\t*item = MISSING_DO_NOTHING;\n \telse if (!strcasecmp(\"add\", value))\n-\t\titem->if_missing = MISSING_ADD;\n+\t\t*item = MISSING_ADD;\n \telse\n \t\treturn -1;\n \treturn 0;\n@@ -471,15 +470,15 @@ static int git_trailer_default_config(const char *conf_key, const char *value, v\n \tvariable_name = strrchr(trailer_item, '.');\n \tif (!variable_name) {\n \t\tif (!strcmp(trailer_item, \"where\")) {\n-\t\t\tif (set_where(&default_conf_info, value) < 0)\n+\t\t\tif (set_where(&default_conf_info.where, value) < 0)\n \t\t\t\twarning(_(\"unknown value '%s' for key '%s'\"),\n \t\t\t\t\tvalue, conf_key);\n \t\t} else if (!strcmp(trailer_item, \"ifexists\")) {\n-\t\t\tif (set_if_exists(&default_conf_info, value) < 0)\n+\t\t\tif (set_if_exists(&default_conf_info.if_exists, value) < 0)\n \t\t\t\twarning(_(\"unknown value '%s' for key '%s'\"),\n \t\t\t\t\tvalue, conf_key);\n \t\t} else if (!strcmp(trailer_item, \"ifmissing\")) {\n-\t\t\tif (set_if_missing(&default_conf_info, value) < 0)\n+\t\t\tif (set_if_missing(&default_conf_info.if_missing, value) < 0)\n \t\t\t\twarning(_(\"unknown value '%s' for key '%s'\"),\n \t\t\t\t\tvalue, conf_key);\n \t\t} else if (!strcmp(trailer_item, \"separators\")) {\n@@ -533,15 +532,15 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)\n \t\tconf->command = xstrdup(value);\n \t\tbreak;\n \tcase TRAILER_WHERE:\n-\t\tif (set_where(conf, value))\n+\t\tif (set_where(&conf->where, value))\n \t\t\twarning(_(\"unknown value '%s' for key '%s'\"), value, conf_key);\n \t\tbreak;\n \tcase TRAILER_IF_EXISTS:\n-\t\tif (set_if_exists(conf, value))\n+\t\tif (set_if_exists(&conf->if_exists, value))\n \t\t\twarning(_(\"unknown value '%s' for key '%s'\"), value, conf_key);\n \t\tbreak;\n \tcase TRAILER_IF_MISSING:\n-\t\tif (set_if_missing(conf, value))\n+\t\tif (set_if_missing(&conf->if_missing, value))\n \t\t\twarning(_(\"unknown value '%s' for key '%s'\"), value, conf_key);\n \t\tbreak;\n \tdefault:\ndiff --git a/trailer.h b/trailer.h\nindex e90ba1270..f306bf059 100644\n--- a/trailer.h\n+++ b/trailer.h\n@@ -1,11 +1,33 @@\n #ifndef TRAILER_H\n #define TRAILER_H\n \n+enum action_where {\n+\tWHERE_END,\n+\tWHERE_AFTER,\n+\tWHERE_BEFORE,\n+\tWHERE_START\n+};\n+enum action_if_exists {\n+\tEXISTS_ADD_IF_DIFFERENT_NEIGHBOR,\n+\tEXISTS_ADD_IF_DIFFERENT,\n+\tEXISTS_ADD,\n+\tEXISTS_REPLACE,\n+\tEXISTS_DO_NOTHING\n+};\n+enum action_if_missing {\n+\tMISSING_ADD,\n+\tMISSING_DO_NOTHING\n+};\n+\n struct trailer_opts {\n \tint in_place;\n \tint trim_empty;\n };\n \n+int set_where(enum action_where *item, const char *value);\n+int set_if_exists(enum action_if_exists *item, const char *value);\n+int set_if_missing(enum action_if_missing *item, const char *value);\n+\n struct trailer_info {\n \t/*\n \t * True if there is a blank line before the location pointed to by\n-- \n2.13.0\n\n\n"},{"id":"324263","messageId":"20170712134646.17179-4-bonzini@gnu.org","threadId":"46363","inReplyTo":"20170712134646.17179-1-bonzini@gnu.org","subject":"[PATCH 3/3] interpret-trailers: add options for actions","fromName":"Paolo Bonzini","fromEmail":"bonzini@gnu.org","sentAt":"2017-07-12T13:46:46Z","receivedAt":"2017-07-12T13:47:05Z","isPatch":true,"sender":{"key":"bonzini@gnu.org","avatar":"https://avatars.githubusercontent.com/u/42082?v=4"},"body":"From: Paolo Bonzini <pbonzini@redhat.com>\n\nAllow using non-default values for trailers without having to set\nthem up in .gitconfig first.  For example, if you have the following\nconfiguration\n\n     trailer.signed-off-by.where = end\n\nyou may use \"--where before\" when a patch author forgets his\nSigned-off-by and provides it in a separate email.\n\nSigned-off-by: Paolo Bonzini <pbonzini@redhat.com>\n---\n Documentation/git-interpret-trailers.txt | 16 ++++++++++\n builtin/interpret-trailers.c             | 40 +++++++++++++++++++++++++\n t/t7513-interpret-trailers.sh            | 51 ++++++++++++++++++++++++++++++++\n trailer.c                                | 28 ++++++++++++++++--\n trailer.h                                |  6 ++++\n 5 files changed, 138 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-interpret-trailers.txt b/Documentation/git-interpret-trailers.txt\nindex 31cdeaecd..f4d67b8a1 100644\n--- a/Documentation/git-interpret-trailers.txt\n+++ b/Documentation/git-interpret-trailers.txt\n@@ -80,6 +80,22 @@ OPTIONS\n \ttrailer to the input messages. See the description of this\n \tcommand.\n \n+--where <placement>::\n+\tSpecify where all new trailers will be added.  This option\n+\toverrides all configuration variables.\n+\n+--if-exists <action>::\n+\tSpecify what action will be performed when there is already at\n+\tleast one trailer with the same <token> in the message.  This\n+\toption applies to all '--trailer' options and overrides all\n+\tconfiguration variables.\n+\n+--if-missing <action>::\n+\tSpecify what action will be performed when there is no other\n+\ttrailer with the same <token> in the message.  This option\n+\tapplies to all '--trailer' options and overrides all\n+\tconfiguration variables.\n+\n CONFIGURATION VARIABLES\n -----------------------\n \ndiff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c\nindex 6528680b5..5f05f0af0 100644\n--- a/builtin/interpret-trailers.c\n+++ b/builtin/interpret-trailers.c\n@@ -16,6 +16,39 @@ static const char * const git_interpret_trailers_usage[] = {\n \tNULL\n };\n \n+static int option_parse_where(const struct option *opt,\n+\t\t\t      const char *arg, int unset)\n+{\n+\tenum action_where *where = opt->value;\n+\n+\tif (unset)\n+\t\treturn 0;\n+\n+\treturn set_where(where, arg);\n+}\n+\n+static int option_parse_if_exists(const struct option *opt,\n+\t\t\t\t  const char *arg, int unset)\n+{\n+\tenum action_if_exists *if_exists = opt->value;\n+\n+\tif (unset)\n+\t\treturn 0;\n+\n+\treturn set_if_exists(if_exists, arg);\n+}\n+\n+static int option_parse_if_missing(const struct option *opt,\n+\t\t\t\t   const char *arg, int unset)\n+{\n+\tenum action_if_missing *if_missing = opt->value;\n+\n+\tif (unset)\n+\t\treturn 0;\n+\n+\treturn set_if_missing(if_missing, arg);\n+}\n+\n int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)\n {\n \tstruct trailer_opts opts = { 0 };\n@@ -24,6 +57,13 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)\n \tstruct option options[] = {\n \t\tOPT_BOOL(0, \"in-place\", &opts.in_place, N_(\"edit files in place\")),\n \t\tOPT_BOOL(0, \"trim-empty\", &opts.trim_empty, N_(\"trim empty trailers\")),\n+\t\tOPT_CALLBACK(0, \"where\", &opts.where, N_(\"action\"),\n+\t\t\t     N_(\"where to place the new trailer\"), option_parse_where),\n+\t\tOPT_CALLBACK(0, \"if-exists\", &opts.if_exists, N_(\"action\"),\n+\t\t\t     N_(\"action if trailer already exists\"), option_parse_if_exists),\n+\t\tOPT_CALLBACK(0, \"if-missing\", &opts.if_missing, N_(\"action\"),\n+\t\t\t     N_(\"action if trailer is missing\"), option_parse_if_missing),\n+\n \t\tOPT_STRING_LIST(0, \"trailer\", &trailers, N_(\"trailer\"),\n \t\t\t\tN_(\"trailer(s) to add\")),\n \t\tOPT_END()\ndiff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh\nindex 0c6f91c43..49f4f823d 100755\n--- a/t/t7513-interpret-trailers.sh\n+++ b/t/t7513-interpret-trailers.sh\n@@ -681,6 +681,21 @@ test_expect_success 'using \"where = before\"' '\n \ttest_cmp expected actual\n '\n \n+test_expect_success 'overriding configuration with \"--where after\"' '\n+\tgit config trailer.ack.where \"before\" &&\n+\tcat complex_message_body >expected &&\n+\tsed -e \"s/ Z\\$/ /\" >>expected <<-\\EOF &&\n+\t\tFixes: Z\n+\t\tAcked-by= Z\n+\t\tAcked-by= Peff\n+\t\tReviewed-by: Z\n+\t\tSigned-off-by: Z\n+\tEOF\n+\tgit interpret-trailers --where after --trailer \"ack: Peff\" \\\n+\t\tcomplex_message >actual &&\n+\ttest_cmp expected actual\n+'\n+\n test_expect_success 'using \"where = after\"' '\n \tgit config trailer.ack.where \"after\" &&\n \tcat complex_message_body >expected &&\n@@ -947,6 +962,23 @@ test_expect_success 'using \"ifExists = add\" with \"where = after\"' '\n \ttest_cmp expected actual\n '\n \n+test_expect_success 'overriding configuration with \"--if-exists replace\"' '\n+\tgit config trailer.fix.key \"Fixes: \" &&\n+\tgit config trailer.fix.ifExists \"add\" &&\n+\tcat complex_message_body >expected &&\n+\tsed -e \"s/ Z\\$/ /\" >>expected <<-\\EOF &&\n+\t\tBug #42\n+\t\tAcked-by= Z\n+\t\tReviewed-by:\n+\t\tSigned-off-by: Z\n+\t\tFixes: 22\n+\tEOF\n+\tgit interpret-trailers --if-exists replace --trailer \"review:\" \\\n+\t\t--trailer \"fix=53\" --trailer \"fix=22\" --trailer \"bug: 42\" \\\n+\t\t<complex_message >actual &&\n+\ttest_cmp expected actual\n+'\n+\n test_expect_success 'using \"ifExists = replace\"' '\n \tgit config trailer.fix.key \"Fixes: \" &&\n \tgit config trailer.fix.ifExists \"replace\" &&\n@@ -1026,6 +1058,25 @@ test_expect_success 'the default is \"ifMissing = add\"' '\n \ttest_cmp expected actual\n '\n \n+test_expect_success 'overriding configuration with \"--if-missing doNothing\"' '\n+\tgit config trailer.ifmissing \"add\" &&\n+\tcat complex_message_body >expected &&\n+\tsed -e \"s/ Z\\$/ /\" >>expected <<-\\EOF &&\n+\t\tFixes: Z\n+\t\tAcked-by= Z\n+\t\tAcked-by= Junio\n+\t\tAcked-by= Peff\n+\t\tReviewed-by:\n+\t\tSigned-off-by: Z\n+\tEOF\n+\tgit interpret-trailers --if-missing doNothing \\\n+\t\t--trailer \"review:\" --trailer \"fix=53\" \\\n+\t\t--trailer \"cc=Linus\" --trailer \"ack: Junio\" \\\n+\t\t--trailer \"fix=22\" --trailer \"bug: 42\" --trailer \"ack: Peff\" \\\n+\t\t<complex_message >actual &&\n+\ttest_cmp expected actual\n+'\n+\n test_expect_success 'when default \"ifMissing\" is \"doNothing\"' '\n \tgit config trailer.ifmissing \"doNothing\" &&\n \tcat complex_message_body >expected &&\ndiff --git a/trailer.c b/trailer.c\nindex 558c52431..78e88f98d 100644\n--- a/trailer.c\n+++ b/trailer.c\n@@ -549,14 +549,36 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)\n \treturn 0;\n }\n \n-static void ensure_configured(void)\n+static void apply_config_overrides(struct conf_info *conf, struct trailer_opts *opts)\n {\n+\tif (!opts)\n+\t\treturn;\n+\tif (opts->where != WHERE_DEFAULT)\n+\t\tconf->where = opts->where;\n+\tif (opts->if_exists != EXISTS_DEFAULT)\n+\t\tconf->if_exists = opts->if_exists;\n+\tif (opts->if_missing != MISSING_DEFAULT)\n+\t\tconf->if_missing = opts->if_missing;\n+}\n+\n+static void ensure_configured(struct trailer_opts *opts)\n+{\n+\tstruct arg_item *item;\n+\tstruct list_head *pos;\n+\n \tif (configured)\n \t\treturn;\n \n \t/* Default config must be setup first */\n \tgit_config(git_trailer_default_config, NULL);\n+\tapply_config_overrides(&default_conf_info, opts);\n+\n \tgit_config(git_trailer_config, NULL);\n+\tlist_for_each(pos, &conf_head) {\n+\t\titem = list_entry(pos, struct arg_item, list);\n+\t\tapply_config_overrides(&item->conf, opts);\n+\t}\n+\n \tconfigured = 1;\n }\n \n@@ -977,7 +999,7 @@ void process_trailers(const char *file, struct trailer_opts *opts,\n \tint trailer_end;\n \tFILE *outfile = stdout;\n \n-\tensure_configured();\n+\tensure_configured(opts);\n \n \tread_input_file(&sb, file);\n \n@@ -1013,7 +1035,7 @@ void trailer_info_get(struct trailer_info *info, const char *str)\n \tsize_t nr = 0, alloc = 0;\n \tchar **last = NULL;\n \n-\tensure_configured();\n+\tensure_configured(NULL);\n \n \tpatch_start = find_patch_start(str);\n \ttrailer_end = find_trailer_end(str, patch_start);\ndiff --git a/trailer.h b/trailer.h\nindex f306bf059..634643c9d 100644\n--- a/trailer.h\n+++ b/trailer.h\n@@ -2,12 +2,14 @@\n #define TRAILER_H\n \n enum action_where {\n+\tWHERE_DEFAULT,\n \tWHERE_END,\n \tWHERE_AFTER,\n \tWHERE_BEFORE,\n \tWHERE_START\n };\n enum action_if_exists {\n+\tEXISTS_DEFAULT,\n \tEXISTS_ADD_IF_DIFFERENT_NEIGHBOR,\n \tEXISTS_ADD_IF_DIFFERENT,\n \tEXISTS_ADD,\n@@ -15,6 +17,7 @@ enum action_if_exists {\n \tEXISTS_DO_NOTHING\n };\n enum action_if_missing {\n+\tMISSING_DEFAULT,\n \tMISSING_ADD,\n \tMISSING_DO_NOTHING\n };\n@@ -22,6 +25,9 @@ enum action_if_missing {\n struct trailer_opts {\n \tint in_place;\n \tint trim_empty;\n+\tenum action_where where;\n+\tenum action_if_exists if_exists;\n+\tenum action_if_missing if_missing;\n };\n \n int set_where(enum action_where *item, const char *value);\n-- \n2.13.0\n\n"},{"id":"324264","messageId":"CAP8UFD0bwb+Zeqn8Xg24J_Z639NiT3Awjzstk6o_L-Q+Wh+c0g@mail.gmail.com","threadId":"46363","inReplyTo":"20170712134646.17179-1-bonzini@gnu.org","subject":"Re: [PATCH 0/3] interpret-trailers: add --where, --if-exists, --if-missing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2017-07-12T14:47:08Z","receivedAt":"2017-07-12T14:47:16Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Wed, Jul 12, 2017 at 3:46 PM, Paolo Bonzini <bonzini@gnu.org> wrote:\n>\n> These options are useful to experiment with \"git interpret-trailers\"\n> without having to tinker with .gitconfig.  It can also be useful in the\n> oddball case where you want a different placement for the trailer.\n>\n> The case that stimulated the creation of the patches was configuring\n>\n>      trailer.signed-off-by.where = end\n>\n> and then wanting \"--where before\" when a patch author forgets his\n> Signed-off-by and provides it in a separate email.\n\nMaybe you could have used the following to temporarily override the config:\n\ngit -c trailer.signed-off-by.where=before interpret-trailers ...\n\nBut it could be helpful and more straightforward to provide the\noptions you implemented.\n\nI am not sure also if --where should override both \"trailer.where\" and\n\"trailer.<token>.where\", or if should just override the former.\n"},{"id":"324265","messageId":"3be619bd-9018-f328-7cbe-1dd8c93b8d19@redhat.com","threadId":"46363","inReplyTo":"CAP8UFD0bwb+Zeqn8Xg24J_Z639NiT3Awjzstk6o_L-Q+Wh+c0g@mail.gmail.com","subject":"Re: [PATCH 0/3] interpret-trailers: add --where, --if-exists, --if-missing","fromName":"Paolo Bonzini","fromEmail":"pbonzini@redhat.com","sentAt":"2017-07-12T15:02:33Z","receivedAt":"2017-07-12T15:02:44Z","isPatch":true,"sender":{"key":"pbonzini@redhat.com","avatar":"https://avatars.githubusercontent.com/u/42082?v=4"},"body":"On 12/07/2017 16:47, Christian Couder wrote:\n> On Wed, Jul 12, 2017 at 3:46 PM, Paolo Bonzini <bonzini@gnu.org> wrote:\n>>\n>> These options are useful to experiment with \"git interpret-trailers\"\n>> without having to tinker with .gitconfig.  It can also be useful in the\n>> oddball case where you want a different placement for the trailer.\n>>\n>> The case that stimulated the creation of the patches was configuring\n>>\n>>      trailer.signed-off-by.where = end\n>>\n>> and then wanting \"--where before\" when a patch author forgets his\n>> Signed-off-by and provides it in a separate email.\n> \n> Maybe you could have used the following to temporarily override the config:\n> \n> git -c trailer.signed-off-by.where=before interpret-trailers ...\n> \n> But it could be helpful and more straightforward to provide the\n> options you implemented.\n\nThat works indeed---and I have now learnt that $GIT_CONFIG_PARAMETERS\nmakes \"git -c\" work across my thick layers of aliases!  The main\ndisadvantage is that it is harder to discover than a command-line option.\n\nAlso, I have scripts which pass the --trailer argument is passed\nunmodified to \"git interpret-trailers\", and the command-line argument\navoids the need to parse the trailer to figure out the -c option.  In\nparticular, in my case the separator is always \":\", but in general that\nmay not be the case.\n\n> I am not sure also if --where should override both \"trailer.where\" and\n> \"trailer.<token>.where\", or if should just override the former.\n\nI think it should override both, otherwise you have different behavior\ndepending on whether trailer.<token>.where is defined or not.\n\nPaolo\n"},{"id":"324304","messageId":"20170712141028.2d6beecc@twelve2.svl.corp.google.com","threadId":"46363","inReplyTo":"20170712134646.17179-4-bonzini@gnu.org","subject":"Re: [PATCH 3/3] interpret-trailers: add options for actions","fromName":"Jonathan Tan","fromEmail":"jonathantanmy@google.com","sentAt":"2017-07-12T21:10:28Z","receivedAt":"2017-07-12T21:10:36Z","isPatch":true,"sender":{"key":"jonathantanmy@fastmail.com","avatar":null},"body":"On Wed, 12 Jul 2017 15:46:46 +0200\nPaolo Bonzini <bonzini@gnu.org> wrote:\n\n> +static int option_parse_where(const struct option *opt,\n> +\t\t\t      const char *arg, int unset)\n> +{\n> +\tenum action_where *where = opt->value;\n> +\n> +\tif (unset)\n> +\t\treturn 0;\n> +\n> +\treturn set_where(where, arg);\n> +}\n\nThis means that we have the following:\n\n    $ cat message\n    Hello\n\n    a: a\n\n    $ ./git interpret-trailers --trailer a=b message\n    Hello\n\n    a: a\n    a: b\n\n    $ ./git interpret-trailers --where start --no-where --trailer a=b message\n    Hello\n\n    a: b\n    a: a\n\nWhen I would expect the last 2 commands to produce the same output. Maybe\ninvoke set_where(where, NULL) when \"unset\" is true? And change set_where()\naccordingly. Same for the other two option parsing functions.\n"},{"id":"324305","messageId":"20170712141351.73a8ea60@twelve2.svl.corp.google.com","threadId":"46363","inReplyTo":"20170712134646.17179-3-bonzini@gnu.org","subject":"Re: [PATCH 2/3] trailers: export action enums and corresponding lookup functions","fromName":"Jonathan Tan","fromEmail":"jonathantanmy@google.com","sentAt":"2017-07-12T21:13:51Z","receivedAt":"2017-07-12T21:14:00Z","isPatch":true,"sender":{"key":"jonathantanmy@fastmail.com","avatar":null},"body":"On Wed, 12 Jul 2017 15:46:45 +0200\nPaolo Bonzini <bonzini@gnu.org> wrote:\n\n> -static struct conf_info default_conf_info;\n> +static struct conf_info default_conf_info = {\n> +\t.where = WHERE_END,\n> +\t.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,\n> +\t.if_missing = MISSING_ADD,\n> +};\n\nI'm not sure if Git is ready to commit to using designated initializers,\nso maybe avoid those for now. More information here [1].\n\n[1] https://public-inbox.org/git/20170710070342.txmlwwq6gvjkwtw7@sigill.intra.peff.net/\n"},{"id":"324306","messageId":"20170712141905.14c812b4@twelve2.svl.corp.google.com","threadId":"46363","inReplyTo":"20170712134646.17179-2-bonzini@gnu.org","subject":"Re: [PATCH 1/3] trailers: create struct trailer_opts","fromName":"Jonathan Tan","fromEmail":"jonathantanmy@google.com","sentAt":"2017-07-12T21:19:05Z","receivedAt":"2017-07-12T21:19:13Z","isPatch":true,"sender":{"key":"jonathantanmy@fastmail.com","avatar":null},"body":"On Wed, 12 Jul 2017 15:46:44 +0200\nPaolo Bonzini <bonzini@gnu.org> wrote:\n\n> -static void print_all(FILE *outfile, struct list_head *head, int trim_empty)\n> +static void print_all(FILE *outfile, struct list_head *head,\n> +\t\t      struct trailer_opts *opts)\n\nThis can be \"const struct trailer_opts *\", I think. (Same for the other\nfunctions in this patch.)\n\n> +struct trailer_opts {\n> +\tint in_place;\n> +\tint trim_empty;\n> +};\n\nI was going to suggest that you make these \"unsigned in_place : 1\" etc.,\nbut I think OPT_BOOL doesn't support those. (And OPT_BIT is probably not\nworth it.)\n"},{"id":"324310","messageId":"02354996-dccc-b2fb-a3b5-aa819229caa0@gnu.org","threadId":"46363","inReplyTo":"20170712141028.2d6beecc@twelve2.svl.corp.google.com","subject":"Re: [PATCH 3/3] interpret-trailers: add options for actions","fromName":"Paolo Bonzini","fromEmail":"bonzini@gnu.org","sentAt":"2017-07-12T21:33:59Z","receivedAt":"2017-07-12T21:34:35Z","isPatch":true,"sender":{"key":"bonzini@gnu.org","avatar":"https://avatars.githubusercontent.com/u/42082?v=4"},"body":"On 12/07/2017 23:10, Jonathan Tan wrote:\n> When I would expect the last 2 commands to produce the same output. Maybe\n> invoke set_where(where, NULL) when \"unset\" is true? And change set_where()\n> accordingly. Same for the other two option parsing functions.\n\nSounds good, and I'll also add a test case of course.\n\nIn fact arg is already NULL if unset is true, so the code of the three\noption parsing functions becomes simpler.\n\nThanks!\n\nPaolo\n"}]}