Let the user specify a command that will give on its standard output the value to use for the specified trailer. Signed-off-by: Christian Couder <chriscool@xxxxxxxxxxxxx> Signed-off-by: Junio C Hamano <gitster@xxxxxxxxx> --- trailer.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/trailer.c b/trailer.c index feadd3a..4d32b42 100644 --- a/trailer.c +++ b/trailer.c @@ -1,5 +1,7 @@ #include "cache.h" #include "string-list.h" +#include "run-command.h" +#include "string-list.h" #include "trailer.h" /* * Copyright (c) 2013, 2014 Christian Couder <chriscool@xxxxxxxxxxxxx> @@ -14,11 +16,14 @@ struct conf_info { char *name; char *key; char *command; + unsigned command_uses_arg : 1; enum action_where where; enum action_if_exists if_exists; enum action_if_missing if_missing; }; +#define TRAILER_ARG_STRING "$ARG" + struct trailer_item { struct trailer_item *previous; struct trailer_item *next; @@ -60,6 +65,13 @@ static inline int contains_only_spaces(const char *str) return !*s; } +static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b) +{ + const char *ptr = strstr(sb->buf, a); + if (ptr) + strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b)); +} + static void free_trailer_item(struct trailer_item *item) { free(item->conf.name); @@ -403,6 +415,7 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb) if (conf->command) warning(_("more than one %s"), conf_key); conf->command = xstrdup(value); + conf->command_uses_arg = !!strstr(conf->command, TRAILER_ARG_STRING); break; case TRAILER_WHERE: if (set_where(conf, value)) @@ -439,6 +452,45 @@ static int parse_trailer(struct strbuf *tok, struct strbuf *val, const char *tra return 0; } +static int read_from_command(struct child_process *cp, struct strbuf *buf) +{ + if (run_command(cp)) + return error("running trailer command '%s' failed", cp->argv[0]); + if (strbuf_read(buf, cp->out, 1024) < 1) + return error("reading from trailer command '%s' failed", cp->argv[0]); + strbuf_trim(buf); + return 0; +} + +static const char *apply_command(const char *command, const char *arg) +{ + struct strbuf cmd = STRBUF_INIT; + struct strbuf buf = STRBUF_INIT; + struct child_process cp; + const char *argv[] = {NULL, NULL}; + const char *result; + + strbuf_addstr(&cmd, command); + if (arg) + strbuf_replace(&cmd, TRAILER_ARG_STRING, arg); + + argv[0] = cmd.buf; + memset(&cp, 0, sizeof(cp)); + cp.argv = argv; + cp.env = local_repo_env; + cp.no_stdin = 1; + cp.out = -1; + cp.use_shell = 1; + + if (read_from_command(&cp, &buf)) { + strbuf_release(&buf); + result = xstrdup(""); + } else + result = strbuf_detach(&buf, NULL); + + strbuf_release(&cmd); + return result; +} static void duplicate_conf(struct conf_info *dst, struct conf_info *src) { @@ -469,6 +521,10 @@ static struct trailer_item *new_trailer_item(struct trailer_item *conf_item, duplicate_conf(&new->conf, &conf_item->conf); new->token = xstrdup(token_from_item(conf_item)); free(tok); + if (conf_item->conf.command_uses_arg || !val) { + new->value = apply_command(conf_item->conf.command, val); + free(val); + } } else new->token = tok; @@ -530,12 +586,21 @@ static struct trailer_item *process_command_line_args(struct string_list *traile struct trailer_item *arg_tok_first = NULL; struct trailer_item *arg_tok_last = NULL; struct string_list_item *tr; + struct trailer_item *item; for_each_string_list_item(tr, trailers) { struct trailer_item *new = create_trailer_item(tr->string); add_trailer_item(&arg_tok_first, &arg_tok_last, new); } + /* Add conf commands that don't use $ARG */ + for (item = first_conf_item; item; item = item->next) { + if (item->conf.command && !item->conf.command_uses_arg) { + struct trailer_item *new = new_trailer_item(item, NULL, NULL); + add_trailer_item(&arg_tok_first, &arg_tok_last, new); + } + } + return arg_tok_first; } -- 1.9.1.636.g20d5f34 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html