interpret-trailers: add --where, --if-exists, --if-missing
v3 cover interpret-trailers: add --where, --if-exists, --if-missing
From: Paolo Bonzini <[email protected]>
These options are useful to experiment with "git interpret-trailers" without having to tinker with .gitconfig (Junio said git should ahve done this first and only added configuration afterwards). It can be useful in the case where you want a different placement for the trailer, or for scripts/aliases that don't want to rely on specific .gitconfig settings.
Compared to v2, the main change is that option order on the command-line is respected. That is,
--trailer 'acked-by: foo' --where end --trailer 'signed-off-by: me'
will only apply where=end to the second trailer. Likewise,
--where end --trailer 'signed-off-by: me' --no-where \ --trailer 'acked-by: foo'
will only apply it to the first, reverting to trailer.*.where for the "acked-by" trailer.
Paolo
v1->v2: support --no-* options, minor code fixes
v2->v3: largely rewritten to respect option order on the command-line;
keep trailer.h namespace clean (Christian)
Paolo Bonzini (3):
trailers: export action enums and corresponding lookup functions
trailers: introduce struct new_trailer_item
interpret-trailers: add options for actions
Documentation/git-interpret-trailers.txt | 24 +++++++ builtin/interpret-trailers.c | 73 ++++++++++++++++++-- t/t7513-interpret-trailers.sh | 66 ++++++++++++++++++ trailer.c | 113 ++++++++++++++++++------------- trailer.h | 43 +++++++++++- 5 files changed, 267 insertions(+), 52 deletions(-)
Signature · 1 line
2.13.3
PATCH v3 1/3 trailers: export action enums and corresponding lookup functions
From: Paolo Bonzini <[email protected]>
Separate the mechanical changes out of the next patch. The functions are changed to take a pointer to enum, because struct conf_info is not going to be public.
Set the default values explicitly in default_conf_info, since they are not anymore close to default_conf_info and it's not obvious which constant has value 0. With the next patches, in fact, the values will not be zero anymore!
--- trailer.c | 65 ++++++++++++++++++++++++++++++++------------------------------- trailer.h | 22 +++++++++++++++++++++ 2 files changed, 55 insertions(+), 32 deletions(-)
trailer.c +33 -32
@@ -10,18 +10,13 @@* Copyright (c) 2013, 2014 Christian Couder <[email protected]> */ -enum action_where {WHERE_END ,WHERE_AFTER ,WHERE_BEFORE ,WHERE_START }; -enum action_if_exists {EXISTS_ADD_IF_DIFFERENT_NEIGHBOR ,EXISTS_ADD_IF_DIFFERENT , -EXISTS_ADD ,EXISTS_REPLACE ,EXISTS_DO_NOTHING }; -enum action_if_missing {MISSING_ADD ,MISSING_DO_NOTHING }; -struct conf_info {char *name;char *key;char *command; -enum action_where where; -enum action_if_exists if_exists; -enum action_if_missing if_missing; +enum trailer_where where; +enum trailer_if_exists if_exists; +enum trailer_if_missing if_missing; };static struct conf_info default_conf_info;
@@ -63,7 +58,7 @@ static const char *git_generated_prefixes[] = { pos != (head); \ pos = is_reverse ? pos->prev : pos->next) -static int after_or_end (enum action_where where) +static int after_or_end (enum trailer_where where) {return (where ==WHERE_AFTER ) || (where ==WHERE_END ); }
@@ -201,7 +196,7 @@ static int check_if_different(struct trailer_item *in_tok,int check_all, struct list_head *head) { - enum action_where where = arg_tok->conf.where; + enum trailer_where where = arg_tok->conf.where;struct list_head *next_head; do { if (same_trailer(in_tok, arg_tok))
@@ -306,7 +301,7 @@ static void apply_arg_if_exists(struct trailer_item *in_tok,static void apply_arg_if_missing (struct list_head *head,struct arg_item *arg_tok) { -enum action_where where; +enum trailer_where where;struct trailer_item *to_add;switch (arg_tok->conf.if_missing) {
@@ -331,7 +326,7 @@ static int find_same_and_apply_arg(struct list_head *head,struct trailer_item *in_tok;struct trailer_item *on_tok; -enum action_where where = arg_tok->conf.where; +enum trailer_where where = arg_tok->conf.where;int middle = (where ==WHERE_AFTER ) || (where ==WHERE_BEFORE );int backwards =after_or_end (where);struct trailer_item *start_tok;
@@ -373,44 +368,44 @@ static void process_trailers_lists(struct list_head *head, } } -static int set_where (struct conf_info *item,const char *value) +int trailer_set_where (enum trailer_where *item,const char *value) {if (!strcasecmp ("after" , value)) - item->where =WHERE_AFTER ; + *item =WHERE_AFTER ;else if (!strcasecmp ("before" , value)) - item->where =WHERE_BEFORE ; + *item =WHERE_BEFORE ;else if (!strcasecmp ("end" , value)) - item->where =WHERE_END ; + *item =WHERE_END ;else if (!strcasecmp ("start" , value)) - item->where =WHERE_START ; + *item =WHERE_START ;else return -1 ;return 0 ; } -static int set_if_exists (struct conf_info *item,const char *value) +int trailer_set_if_exists (enum trailer_if_exists *item,const char *value) {if (!strcasecmp ("addIfDifferent" , value)) - item->if_exists =EXISTS_ADD_IF_DIFFERENT ; + *item =EXISTS_ADD_IF_DIFFERENT ;else if (!strcasecmp ("addIfDifferentNeighbor" , value)) - item->if_exists =EXISTS_ADD_IF_DIFFERENT_NEIGHBOR ; + *item =EXISTS_ADD_IF_DIFFERENT_NEIGHBOR ;else if (!strcasecmp ("add" , value)) - item->if_exists =EXISTS_ADD ; + *item =EXISTS_ADD ;else if (!strcasecmp ("replace" , value)) - item->if_exists =EXISTS_REPLACE ; + *item =EXISTS_REPLACE ;else if (!strcasecmp ("doNothing" , value)) - item->if_exists =EXISTS_DO_NOTHING ; + *item =EXISTS_DO_NOTHING ;else return -1 ;return 0 ; } -static int set_if_missing (struct conf_info *item,const char *value) +int trailer_set_if_missing (enum trailer_if_missing *item,const char *value) {if (!strcasecmp ("doNothing" , value)) - item->if_missing =MISSING_DO_NOTHING ; + *item =MISSING_DO_NOTHING ;else if (!strcasecmp ("add" , value)) - item->if_missing =MISSING_ADD ; + *item =MISSING_ADD ;else return -1 ;return 0 ;
@@ -470,15 +465,18 @@ static int git_trailer_default_config(const char *conf_key, const char *value, v variable_name =strrchr (trailer_item,'.' );if (!variable_name) {if (!strcmp (trailer_item,"where" )) { -if (set_where (&default_conf_info, value) <0 ) +if (trailer_set_where (&default_conf_info.where, + value) <0 )warning (_ ("unknown value '%s' for key '%s'" ), value, conf_key); }else if (!strcmp (trailer_item,"ifexists" )) { -if (set_if_exists (&default_conf_info, value) <0 ) +if (trailer_set_if_exists (&default_conf_info.if_exists, + value) <0 )warning (_ ("unknown value '%s' for key '%s'" ), value, conf_key); }else if (!strcmp (trailer_item,"ifmissing" )) { -if (set_if_missing (&default_conf_info, value) <0 ) +if (trailer_set_if_missing (&default_conf_info.if_missing, + value) <0 )warning (_ ("unknown value '%s' for key '%s'" ), value, conf_key); }else if (!strcmp (trailer_item,"separators" )) {
@@ -532,15 +530,15 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb) conf->command =xstrdup (value);break ;case TRAILER_WHERE : -if (set_where (conf, value)) +if (trailer_set_where (&conf->where, value))warning (_ ("unknown value '%s' for key '%s'" ), value, conf_key);break ;case TRAILER_IF_EXISTS : -if (set_if_exists (conf, value)) +if (trailer_set_if_exists (&conf->if_exists, value))warning (_ ("unknown value '%s' for key '%s'" ), value, conf_key);break ;case TRAILER_IF_MISSING : -if (set_if_missing (conf, value)) +if (trailer_set_if_missing (&conf->if_missing, value))warning (_ ("unknown value '%s' for key '%s'" ), value, conf_key);break ;default :
@@ -555,6 +553,9 @@ static void ensure_configured(void)return ;/* Default config must be setup first */ + default_conf_info.where =WHERE_END ; + default_conf_info.if_exists =EXISTS_ADD_IF_DIFFERENT_NEIGHBOR ; + default_conf_info.if_missing =MISSING_ADD ;git_config (git_trailer_default_config,NULL );git_config (git_trailer_config,NULL ); configured =1 ;
trailer.h +22 -0
@@ -1,6 +1,28 @@#ifndef TRAILER_H #define TRAILER_H +enum trailer_where { +WHERE_END , +WHERE_AFTER , +WHERE_BEFORE , +WHERE_START +}; +enum trailer_if_exists { +EXISTS_ADD_IF_DIFFERENT_NEIGHBOR , +EXISTS_ADD_IF_DIFFERENT , +EXISTS_ADD , +EXISTS_REPLACE , +EXISTS_DO_NOTHING +}; +enum trailer_if_missing { +MISSING_ADD , +MISSING_DO_NOTHING +}; + +int trailer_set_where (enum trailer_where *item,const char *value); +int trailer_set_if_exists (enum trailer_if_exists *item,const char *value); +int trailer_set_if_missing (enum trailer_if_missing *item,const char *value); + struct trailer_info {/* * True if there is a blank line before the location pointed to by
Signature · 1 line
2.13.3
PATCH v3 2/3 trailers: introduce struct new_trailer_item
In reply to Paolo BonziniFrom: Paolo Bonzini <[email protected]>
This will provide a place to store the current state of the --where, --if-exists and --if-missing options.
--- builtin/interpret-trailers.c | 41 +++++++++++++++++++++++++++++++++++++---- trailer.c | 21 ++++++++++++--------- trailer.h | 14 +++++++++++++- 3 files changed, 62 insertions(+), 14 deletions(-)
builtin/interpret-trailers.c +37 -4
@@ -16,17 +16,50 @@ static const char * const git_interpret_trailers_usage[] = {NULL }; +static void new_trailers_clear (struct list_head *trailers) +{ +struct list_head *pos, *tmp; +struct new_trailer_item *item; + +list_for_each_safe (pos, tmp, trailers) { + item = list_entry(pos, struct new_trailer_item, list); +list_del (pos); +free (item); + } +} + +static int option_parse_trailer (const struct option *opt, +const char *arg,int unset) +{ +struct list_head *trailers = opt->value; +struct new_trailer_item *item; + +if (unset) { +new_trailers_clear (trailers); +return 0 ; + } + +if (!arg) +return -1 ; + + item =xmalloc (sizeof *item); + item->text = arg; +list_add_tail (&item->list, trailers); +return 0 ; +} +int cmd_interpret_trailers (int argc,const char **argv,const char *prefix) {int in_place =0 ;int trim_empty =0 ; -struct string_list trailers =STRING_LIST_INIT_NODUP ; +LIST_HEAD (trailers);struct option options[] = {OPT_BOOL (0 ,"in-place" , &in_place,N_ ("edit files in place" )),OPT_BOOL (0 ,"trim-empty" , &trim_empty,N_ ("trim empty trailers" )), -OPT_STRING_LIST (0 ,"trailer" , &trailers,N_ ("trailer" ), -N_ ("trailer(s) to add" )), + +OPT_CALLBACK (0 ,"trailer" , &trailers,N_ ("trailer" ), +N_ ("trailer(s) to add" ), option_parse_trailer),OPT_END () };
@@ -43,7 +76,7 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)process_trailers (NULL , in_place, trim_empty, &trailers); } -string_list_clear (&trailers,0 ); +new_trailers_clear (&trailers);return 0 ; }
trailer.c +12 -9
@@ -669,14 +669,15 @@ static void add_arg_item(struct list_head *arg_head, char *tok, char *val, }static void process_command_line_args (struct list_head *arg_head, -struct string_list *trailers) +struct list_head *new_trailer_head) { -struct string_list_item *tr; +struct new_trailer_item *tr;struct arg_item *item;struct strbuf tok =STRBUF_INIT ;struct strbuf val =STRBUF_INIT ;const struct conf_info *conf;struct list_head *pos; +const char *string;/* * In command-line arguments, '=' is accepted (in addition to the
@@ -695,18 +696,19 @@ static void process_command_line_args(struct list_head *arg_head, }/* Add an arg item for each trailer on the command line */ -for_each_string_list_item (tr, trailers) { -int separator_pos =find_separator (tr->string, cl_separators); +list_for_each (pos, new_trailer_head) { + tr = list_entry(pos, struct new_trailer_item, list); + string = tr->text; +int separator_pos =find_separator (string, cl_separators);if (separator_pos ==0 ) {struct strbuf sb =STRBUF_INIT ; -strbuf_addstr (&sb, tr->string); +strbuf_addstr (&sb, string);strbuf_trim (&sb);error (_ ("empty trailer token in trailer '%.*s'" ), (int ) sb.len, sb.buf);strbuf_release (&sb); }else { -parse_trailer (&tok, &val, &conf, tr->string, - separator_pos); +parse_trailer (&tok, &val, &conf, string, separator_pos); add_arg_item(arg_head, strbuf_detach(&tok, NULL), strbuf_detach(&val, NULL),
@@ -969,7 +971,8 @@ static FILE *create_in_place_tempfile(const char *file)return outfile; } -void process_trailers (const char *file,int in_place,int trim_empty,struct string_list *trailers) +void process_trailers (const char *file,int in_place,int trim_empty, +struct list_head *new_trailer_head) {LIST_HEAD (head);LIST_HEAD (arg_head);
@@ -987,7 +990,7 @@ void process_trailers(const char *file, int in_place, int trim_empty, struct str/* Print the lines before the trailers */ trailer_end =process_input_file (outfile, sb.buf, &head); -process_command_line_args (&arg_head, trailers); +process_command_line_args (&arg_head, new_trailer_head);process_trailers_lists (&head, &arg_head);
trailer.h +13 -1
@@ -1,6 +1,8 @@#ifndef TRAILER_H #define TRAILER_H +#include "list.h" +enum trailer_where {WHERE_END , WHERE_AFTER,
@@ -44,8 +46,18 @@ struct trailer_info {size_t trailer_nr; }; +/* +* A list that represents newly-added trailers, such as those provided +* with the --trailer command line option of git-interpret-trailers. +*/ +struct new_trailer_item { +struct list_head list; + +const char *text; +}; +void process_trailers (const char *file,int in_place,int trim_empty, -struct string_list *trailers); +struct list_head *new_trailer_head);void trailer_info_get (struct trailer_info *info,const char *str);
Signature · 1 line
2.13.3
PATCH v3 3/3 interpret-trailers: add options for actions
In reply to Paolo BonziniFrom: Paolo Bonzini <[email protected]>
Allow using non-default values for trailers without having to set them up in .gitconfig first. For example, if you have the following configuration
trailer.signed-off-by.where = end
you may use "--where before" when a patch author forgets his Signed-off-by and provides it in a separate email. Likewise for --if-exists and --if-missing
Reverting to the behavior specified by .gitconfig is done with --no-where, --no-if-exists and --no-if-missing.
--- Documentation/git-interpret-trailers.txt | 23 +++++++++++ builtin/interpret-trailers.c | 32 ++++++++++++++++ t/t7513-interpret-trailers.sh | 66 ++++++++++++++++++++++++++++++++ trailer.c | 27 ++++++++++--- trailer.h | 7 ++++ 5 files changed, 149 insertions(+), 6 deletions(-)
Documentation/git-interpret-trailers.txt +23 -0
@@ -80,6 +80,29 @@ OPTIONS trailer to the input messages. See the description of this command. +--where <placement>:: +--no-where:: + Specify where all new trailers will be added. A setting + provided with '--where' overrides all configuration variables + and applies to all '--trailer' options until the next occurrence of + '--where' or '--no-where'. + +--if-exists <action>:: +--no-if-exists:: + Specify what action will be performed when there is already at + least one trailer with the same <token> in the message. A setting + provided with '--if-exists' overrides all configuration variables + and applies to all '--trailer' options until the next occurrence of + '--if-exists' or '--no-if-exists'. + +--if-missing <action>:: +--no-if-missing:: + Specify what action will be performed when there is no other + trailer with the same <token> in the message. A setting + provided with '--if-missing' overrides all configuration variables + and applies to all '--trailer' options until the next occurrence of + '--if-missing' or '--no-if-missing'. + CONFIGURATION VARIABLES -----------------------
builtin/interpret-trailers.c +32 -0
@@ -16,6 +16,28 @@ static const char * const git_interpret_trailers_usage[] = { NULL }; +static enum trailer_where where; +static enum trailer_if_exists if_exists; +static enum trailer_if_missing if_missing; + +static int option_parse_where (const struct option *opt, +const char *arg,int unset) +{ +return trailer_set_where (&where, arg); +} + +static int option_parse_if_exists (const struct option *opt, +const char *arg,int unset) +{ +return trailer_set_if_exists (&if_exists, arg); +} + +static int option_parse_if_missing (const struct option *opt, +const char *arg,int unset) +{ +return trailer_set_if_missing (&if_missing, arg); +} +static void new_trailers_clear (struct list_head *trailers) {struct list_head *pos, *tmp;
@@ -44,6 +66,9 @@ static int option_parse_trailer(const struct option *opt, item =xmalloc (sizeof *item); item->text = arg; + item->where = where; + item->if_exists = if_exists; + item->if_missing = if_missing;list_add_tail (&item->list, trailers);return 0 ; }
@@ -58,6 +83,13 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)OPT_BOOL (0 ,"in-place" , &in_place,N_ ("edit files in place" )),OPT_BOOL (0 ,"trim-empty" , &trim_empty,N_ ("trim empty trailers" )), +OPT_CALLBACK (0 ,"where" ,NULL ,N_ ("action" ), +N_ ("where to place the new trailer" ), option_parse_where), +OPT_CALLBACK (0 ,"if-exists" ,NULL ,N_ ("action" ), +N_ ("action if trailer already exists" ), option_parse_if_exists), +OPT_CALLBACK (0 ,"if-missing" ,NULL ,N_ ("action" ), +N_ ("action if trailer is missing" ), option_parse_if_missing), +OPT_CALLBACK (0 ,"trailer" , &trailers,N_ ("trailer" ),N_ ("trailer(s) to add" ), option_parse_trailer),OPT_END ()
t/t7513-interpret-trailers.sh +66 -0
84 lines not on this page · raw message
trailer.c +21 -6
@@ -370,7 +370,9 @@ static void process_trailers_lists(struct list_head *head,int trailer_set_where (enum trailer_where *item,const char *value) { -if (!strcasecmp ("after" , value)) +if (!value) + *item =WHERE_DEFAULT ; +else if (!strcasecmp ("after" , value)) *item =WHERE_AFTER ;else if (!strcasecmp ("before" , value)) *item =WHERE_BEFORE ;
@@ -385,7 +387,9 @@ int trailer_set_where(enum trailer_where *item, const char *value)int trailer_set_if_exists (enum trailer_if_exists *item,const char *value) { -if (!strcasecmp ("addIfDifferent" , value)) +if (!value) + *item =EXISTS_DEFAULT ; +else if (!strcasecmp ("addIfDifferent" , value)) *item =EXISTS_ADD_IF_DIFFERENT ;else if (!strcasecmp ("addIfDifferentNeighbor" , value)) *item =EXISTS_ADD_IF_DIFFERENT_NEIGHBOR ;
@@ -402,7 +406,9 @@ int trailer_set_if_exists(enum trailer_if_exists *item, const char *value)int trailer_set_if_missing (enum trailer_if_missing *item,const char *value) { -if (!strcasecmp ("doNothing" , value)) +if (!value) + *item =MISSING_DEFAULT ; +else if (!strcasecmp ("doNothing" , value)) *item =MISSING_DO_NOTHING ;else if (!strcasecmp ("add" , value)) *item =MISSING_ADD ;
@@ -659,12 +665,21 @@ static struct trailer_item *add_trailer_item(struct list_head *head, char *tok, }static void add_arg_item (struct list_head *arg_head,char *tok,char *val, -const struct conf_info *conf) +const struct conf_info *conf, +const struct new_trailer_item *new_trailer_item) {struct arg_item *new =xcalloc (sizeof (*new),1 ); new->token = tok; new->value = val;duplicate_conf (&new->conf, conf); +if (new_trailer_item) { +if (new_trailer_item->where !=WHERE_DEFAULT ) + new->conf.where = new_trailer_item->where; +if (new_trailer_item->if_exists !=EXISTS_DEFAULT ) + new->conf.if_exists = new_trailer_item->if_exists; +if (new_trailer_item->if_missing !=MISSING_DEFAULT ) + new->conf.if_missing = new_trailer_item->if_missing; + }list_add_tail (&new->list, arg_head); }
@@ -692,7 +707,7 @@ static void process_command_line_args(struct list_head *arg_head,add_arg_item (arg_head,xstrdup (token_from_item (item,NULL )),xstrdup ("" ), - &item->conf); + &item->conf,NULL ); }/* Add an arg item for each trailer on the command line */
@@ -712,7 +727,7 @@ static void process_command_line_args(struct list_head *arg_head,add_arg_item (arg_head,strbuf_detach (&tok,NULL ),strbuf_detach (&val,NULL ), - conf); + conf, tr); } }
trailer.h +7 -0
@@ -4,12 +4,14 @@#include "list.h" enum trailer_where { +WHERE_DEFAULT ,WHERE_END ,WHERE_AFTER ,WHERE_BEFORE ,WHERE_START };enum trailer_if_exists { +EXISTS_DEFAULT ,EXISTS_ADD_IF_DIFFERENT_NEIGHBOR ,EXISTS_ADD_IF_DIFFERENT ,EXISTS_ADD ,
@@ -17,6 +19,7 @@ enum trailer_if_exists { EXISTS_DO_NOTHING };enum trailer_if_missing { +MISSING_DEFAULT ,MISSING_ADD ,MISSING_DO_NOTHING };
@@ -54,6 +57,10 @@ struct new_trailer_item {struct list_head list;const char *text; + +enum trailer_where where; +enum trailer_if_exists if_exists; +enum trailer_if_missing if_missing; }; void process_trailers(const char *file, int in_place, int trim_empty,
Signature · 1 line
2.13.3
Quoted 14 lines · Paolo Bonzini
From: Paolo Bonzini <[email protected]>
These options are useful to experiment with "git interpret-trailers" without having to tinker with .gitconfig (Junio said git should ahve done this first and only added configuration afterwards). It can be useful in the case where you want a different placement for the trailer, or for
scripts/aliasesthat don't want to rely on specific .gitconfig settings.Compared to v2, the main change is that option order on the command-line is respected. That is,
--trailer 'acked-by: foo' --where end --trailer 'signed-off-by: me'
Quoted 7 lines
will only apply where=end to the second trailer. Likewise,
--where end --trailer 'signed-off-by: me' --no-where \ --trailer 'acked-by: foo'will only apply it to the first, reverting to trailer.*.where for the "acked-by" trailer.
I am getting the following in my build after merging these to 'pu'.
trailer.c: In function 'apply_arg_if_exists':
trailer.c:270:2: error: enumeration value 'EXISTS_DEFAULT' not handled in switch [-Werror=switch]
switch (arg_tok->conf.if_exists) {
^trailer.c: In function 'apply_arg_if_missing':
trailer.c:307:2: error: enumeration value 'MISSING_DEFAULT' not handled in switch [-Werror=switch]
switch (arg_tok->conf.if_missing) {
^trailer.c: In function 'process_command_line_args':
trailer.c:717:3: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
int separator_pos = find_separator(string, cl_separators);
^
cc1: all warnings being treated as errors
m