{"thread":{"id":"46365","subject":"[PATCH v2 0/3] interpret-trailers: add --where, --if-exists, --if-missing","startedAt":"2017-07-12T22:21:36Z","lastAt":"2017-07-17T21:16:12Z","messageCount":8,"participants":["Paolo Bonzini","Junio C Hamano","Christian Couder"],"isPatch":true,"patchVersion":2,"patchTotal":3},"messages":[{"id":"324315","messageId":"20170712222116.7095-1-bonzini@gnu.org","threadId":"46365","inReplyTo":null,"subject":"[PATCH v2 0/3] interpret-trailers: add --where, --if-exists, --if-missing","fromName":"Paolo Bonzini","fromEmail":"bonzini@gnu.org","sentAt":"2017-07-12T22:21:13Z","receivedAt":"2017-07-12T22:21:36Z","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\nCompared to \"git -c\", they are more easily discoverable, and also have\nslightly different behavior because they override all trailer.*\nconfiguration keys.\n\nPaolo\n\nv1->v2: support --no-* options, minor code fixes\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             |  44 ++++++++++---\n t/t7513-interpret-trailers.sh            |  66 ++++++++++++++++++++\n trailer.c                                | 102 ++++++++++++++++++++-----------\n trailer.h                                |  35 ++++++++++-\n 5 files changed, 218 insertions(+), 45 deletions(-)\n\n-- \n2.13.0\n\n"},{"id":"324316","messageId":"20170712222116.7095-2-bonzini@gnu.org","threadId":"46365","inReplyTo":"20170712222116.7095-1-bonzini@gnu.org","subject":"[PATCH v2 1/3] trailers: create struct trailer_opts","fromName":"Paolo Bonzini","fromEmail":"bonzini@gnu.org","sentAt":"2017-07-12T22:21:14Z","receivedAt":"2017-07-12T22:21:38Z","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 in the next patch to include more options.\n\nSigned-off-by: Paolo Bonzini <pbonzini@redhat.com>\n---\n\tv1->v2: constify\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      const 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, const 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, const 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":"324317","messageId":"20170712222116.7095-3-bonzini@gnu.org","threadId":"46365","inReplyTo":"20170712222116.7095-1-bonzini@gnu.org","subject":"[PATCH v2 2/3] trailers: export action enums and corresponding lookup functions","fromName":"Paolo Bonzini","fromEmail":"bonzini@gnu.org","sentAt":"2017-07-12T22:21:15Z","receivedAt":"2017-07-12T22:21:39Z","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\nSet the default values 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.  With the next patch, in fact, the values will\nnot be zero anymore!\n\nSigned-off-by: Paolo Bonzini <pbonzini@redhat.com>\n---\n\tv1->v2: avoid use of designated initializers\n\n trailer.c | 48 +++++++++++++++++++++++-------------------------\n trailer.h | 22 ++++++++++++++++++++++\n 2 files changed, 45 insertions(+), 25 deletions(-)\n\ndiff --git a/trailer.c b/trailer.c\nindex a3eb42818..a371c7b45 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@@ -374,44 +369,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 +466,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 +528,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:\n@@ -556,6 +551,9 @@ static void ensure_configured(void)\n \t\treturn;\n \n \t/* Default config must be setup first */\n+\tdefault_conf_info.where = WHERE_END;\n+\tdefault_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;\n+\tdefault_conf_info.if_missing = MISSING_ADD;\n \tgit_config(git_trailer_default_config, NULL);\n \tgit_config(git_trailer_config, NULL);\n \tconfigured = 1;\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":"324318","messageId":"20170712222116.7095-4-bonzini@gnu.org","threadId":"46365","inReplyTo":"20170712222116.7095-1-bonzini@gnu.org","subject":"[PATCH v2 3/3] interpret-trailers: add options for actions","fromName":"Paolo Bonzini","fromEmail":"bonzini@gnu.org","sentAt":"2017-07-12T22:21:16Z","receivedAt":"2017-07-12T22:21:56Z","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\tv1->v2: fix and test behavior of --no-where and similar\n\n Documentation/git-interpret-trailers.txt | 16 ++++++++\n builtin/interpret-trailers.c             | 31 +++++++++++++++\n t/t7513-interpret-trailers.sh            | 66 ++++++++++++++++++++++++++++++++\n trailer.c                                | 40 ++++++++++++++++---\n trailer.h                                |  6 +++\n 5 files changed, 153 insertions(+), 6 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..bb93412ac 100644\n--- a/builtin/interpret-trailers.c\n+++ b/builtin/interpret-trailers.c\n@@ -16,6 +16,30 @@ 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+\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+\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+\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 +48,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..adbdf54f8 100755\n--- a/t/t7513-interpret-trailers.sh\n+++ b/t/t7513-interpret-trailers.sh\n@@ -681,6 +681,36 @@ 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 = before\" with \"--no-where\"' '\n+\tcat complex_message_body >expected &&\n+\tsed -e \"s/ Z\\$/ /\" >>expected <<-\\EOF &&\n+\t\tBug #42\n+\t\tFixes: Z\n+\t\tAcked-by= Peff\n+\t\tAcked-by= Z\n+\t\tReviewed-by: Z\n+\t\tSigned-off-by: Z\n+\tEOF\n+\tgit interpret-trailers --where after --no-where --trailer \"ack: Peff\" \\\n+\t\t--trailer \"bug: 42\" complex_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 +977,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 +1073,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 a371c7b45..0a3d207d9 100644\n--- a/trailer.c\n+++ b/trailer.c\n@@ -371,7 +371,9 @@ static void process_trailers_lists(struct list_head *head,\n \n int set_where(enum action_where *item, const char *value)\n {\n-\tif (!strcasecmp(\"after\", value))\n+\tif (!value)\n+\t\t*item = WHERE_DEFAULT;\n+\telse if (!strcasecmp(\"after\", value))\n \t\t*item = WHERE_AFTER;\n \telse if (!strcasecmp(\"before\", value))\n \t\t*item = WHERE_BEFORE;\n@@ -386,7 +388,9 @@ int set_where(enum action_where *item, const char *value)\n \n int set_if_exists(enum action_if_exists *item, const char *value)\n {\n-\tif (!strcasecmp(\"addIfDifferent\", value))\n+\tif (!value)\n+\t\t*item = EXISTS_DEFAULT;\n+\telse if (!strcasecmp(\"addIfDifferent\", value))\n \t\t*item = EXISTS_ADD_IF_DIFFERENT;\n \telse if (!strcasecmp(\"addIfDifferentNeighbor\", value))\n \t\t*item = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;\n@@ -403,7 +407,9 @@ int set_if_exists(enum action_if_exists *item, const char *value)\n \n int set_if_missing(enum action_if_missing *item, const char *value)\n {\n-\tif (!strcasecmp(\"doNothing\", value))\n+\tif (!value)\n+\t\t*item = MISSING_DEFAULT;\n+\telse if (!strcasecmp(\"doNothing\", value))\n \t\t*item = MISSING_DO_NOTHING;\n \telse if (!strcasecmp(\"add\", value))\n \t\t*item = MISSING_ADD;\n@@ -545,8 +551,24 @@ 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,\n+\t\t\t\t   const 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(const struct trailer_opts *opts)\n+{\n+\tstruct arg_item *item;\n+\tstruct list_head *pos;\n+\n \tif (configured)\n \t\treturn;\n \n@@ -555,7 +576,14 @@ static void ensure_configured(void)\n \tdefault_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;\n \tdefault_conf_info.if_missing = MISSING_ADD;\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@@ -976,7 +1004,7 @@ void process_trailers(const char *file, const 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@@ -1012,7 +1040,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":"324320","messageId":"xmqqmv89z2be.fsf@gitster.mtv.corp.google.com","threadId":"46365","inReplyTo":"20170712222116.7095-1-bonzini@gnu.org","subject":"Re: [PATCH v2 0/3] interpret-trailers: add --where, --if-exists, --if-missing","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-07-12T23:02:29Z","receivedAt":"2017-07-12T23:02:41Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Paolo Bonzini <bonzini@gnu.org> writes:\n\n> From: Paolo Bonzini <pbonzini@redhat.com>\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> Compared to \"git -c\", they are more easily discoverable, and also have\n> slightly different behavior because they override all trailer.*\n> configuration keys.\n\nI think this is a very good idea (we shouldn't have started the\ncommand only with the configurations; we rather should have done\nthis first and then added configuration after that).\n\nLooking forward to reviewing them, but I am cutting a maint release\nnow, so it may have to wait a bit.\n\nThanks.\n\n>\n> Paolo\n>\n> v1->v2: support --no-* options, minor code fixes\n>\n> Paolo 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             |  44 ++++++++++---\n>  t/t7513-interpret-trailers.sh            |  66 ++++++++++++++++++++\n>  trailer.c                                | 102 ++++++++++++++++++++-----------\n>  trailer.h                                |  35 ++++++++++-\n>  5 files changed, 218 insertions(+), 45 deletions(-)\n"},{"id":"324323","messageId":"d5e439c3-ba17-8efa-647d-eb64128064c8@gnu.org","threadId":"46365","inReplyTo":"xmqqmv89z2be.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v2 0/3] interpret-trailers: add --where, --if-exists, --if-missing","fromName":"Paolo Bonzini","fromEmail":"bonzini@gnu.org","sentAt":"2017-07-12T23:42:38Z","receivedAt":"2017-07-12T23:42:51Z","isPatch":true,"sender":{"key":"bonzini@gnu.org","avatar":"https://avatars.githubusercontent.com/u/42082?v=4"},"body":"On 13/07/2017 01:02, Junio C Hamano wrote:\n> Paolo Bonzini <bonzini@gnu.org> writes:\n> \n>> From: Paolo Bonzini <pbonzini@redhat.com>\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>> Compared to \"git -c\", they are more easily discoverable, and also have\n>> slightly different behavior because they override all trailer.*\n>> configuration keys.\n> \n> I think this is a very good idea (we shouldn't have started the\n> command only with the configurations; we rather should have done\n> this first and then added configuration after that).\n\nActually we can do better: we can have --where only refer to\n*subsequent* --trailer options.  This will require more refactoring\n(probably making a new struct that can be passed to process_trailers),\nbut the first two patches should be the same.\n\nI'll have time for this only after vacation (so at the end of July), and\nit's backwards-incompatible with this series.  Reviews are welcome\nanyway. :)\n\nThanks for encouraging me.  It's always a pleasure when I can scratch my\ngit itches!\n\nPaolo\n\n> Looking forward to reviewing them, but I am cutting a maint release\n> now, so it may have to wait a bit.\n> \n> Thanks.\n> \n>>\n>> Paolo\n>>\n>> v1->v2: support --no-* options, minor code fixes\n>>\n>> Paolo 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             |  44 ++++++++++---\n>>  t/t7513-interpret-trailers.sh            |  66 ++++++++++++++++++++\n>>  trailer.c                                | 102 ++++++++++++++++++++-----------\n>>  trailer.h                                |  35 ++++++++++-\n>>  5 files changed, 218 insertions(+), 45 deletions(-)\n> \n\n"},{"id":"324336","messageId":"CAP8UFD0LEHDutF83=V9zrDnFKy27dJSrq5DHV-8YV1=y=cPXqg@mail.gmail.com","threadId":"46365","inReplyTo":"20170712222116.7095-3-bonzini@gnu.org","subject":"Re: [PATCH v2 2/3] trailers: export action enums and corresponding lookup functions","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2017-07-13T06:00:22Z","receivedAt":"2017-07-13T06:00:28Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Thu, Jul 13, 2017 at 12:21 AM, Paolo Bonzini <bonzini@gnu.org> wrote:\n\n> diff --git a/trailer.h b/trailer.h\n> index 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> +       WHERE_END,\n> +       WHERE_AFTER,\n> +       WHERE_BEFORE,\n> +       WHERE_START\n> +};\n> +enum action_if_exists {\n> +       EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,\n> +       EXISTS_ADD_IF_DIFFERENT,\n> +       EXISTS_ADD,\n> +       EXISTS_REPLACE,\n> +       EXISTS_DO_NOTHING\n> +};\n> +enum action_if_missing {\n> +       MISSING_ADD,\n> +       MISSING_DO_NOTHING\n> +};\n\nAs these enums are now in trailer.h, maybe more specific names like\n\"trailer_action_where\" instead of \"action_where\" would be better.\n\n>  struct trailer_opts {\n>         int in_place;\n>         int 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\"trailer_\" should perhaps be added at the beginning of the names of\nthe above functions too.\n"},{"id":"324683","messageId":"xmqq379uhin0.fsf@gitster.mtv.corp.google.com","threadId":"46365","inReplyTo":"CAP8UFD0LEHDutF83=V9zrDnFKy27dJSrq5DHV-8YV1=y=cPXqg@mail.gmail.com","subject":"Re: [PATCH v2 2/3] trailers: export action enums and corresponding lookup functions","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-07-17T21:13:07Z","receivedAt":"2017-07-17T21:16:12Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Christian Couder <christian.couder@gmail.com> writes:\n\n> On Thu, Jul 13, 2017 at 12:21 AM, Paolo Bonzini <bonzini@gnu.org> wrote:\n>\n>> diff --git a/trailer.h b/trailer.h\n>> index 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>> +       WHERE_END,\n>> +       WHERE_AFTER,\n>> +       WHERE_BEFORE,\n>> +       WHERE_START\n>> +};\n>> +enum action_if_exists {\n>> +       EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,\n>> +       EXISTS_ADD_IF_DIFFERENT,\n>> +       EXISTS_ADD,\n>> +       EXISTS_REPLACE,\n>> +       EXISTS_DO_NOTHING\n>> +};\n>> +enum action_if_missing {\n>> +       MISSING_ADD,\n>> +       MISSING_DO_NOTHING\n>> +};\n>\n> As these enums are now in trailer.h, maybe more specific names like\n> \"trailer_action_where\" instead of \"action_where\" would be better.\n>\n>>  struct trailer_opts {\n>>         int in_place;\n>>         int 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> \"trailer_\" should perhaps be added at the beginning of the names of\n> the above functions too.\n\nAll sensible suggestions.  Thanks.\n"}]}