From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christian Couder Subject: [PATCH v11 09/11] trailer: execute command from 'trailer..command' Date: Fri, 25 Apr 2014 21:07:00 +0200 Message-ID: <20140425190703.28550.47508.chriscool@tuxfamily.org> References: <20140425185719.28550.27059.chriscool@tuxfamily.org> Cc: git@vger.kernel.org, Johan Herland , Josh Triplett , Thomas Rast , Michael Haggerty , Dan Carpenter , Greg Kroah-Hartman , Jeff King , Eric Sunshine , Ramsay Jones , Jonathan Nieder To: Junio C Hamano X-From: git-owner@vger.kernel.org Fri Apr 25 21:12:19 2014 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WdlXm-0001yz-Kq for gcvg-git-2@plane.gmane.org; Fri, 25 Apr 2014 21:12:19 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754676AbaDYTME (ORCPT ); Fri, 25 Apr 2014 15:12:04 -0400 Received: from mail-2y.bbox.fr ([194.158.98.15]:43448 "EHLO mail-2y.bbox.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932101AbaDYTHt (ORCPT ); Fri, 25 Apr 2014 15:07:49 -0400 Received: from [127.0.1.1] (cha92-h01-128-78-31-246.dsl.sta.abo.bbox.fr [128.78.31.246]) by mail-2y.bbox.fr (Postfix) with ESMTP id 5B78951; Fri, 25 Apr 2014 21:07:48 +0200 (CEST) X-git-sha1: fe5187480d69c4772b6907271c9a9582c48aa977 X-Mailer: git-mail-commits v0.5.2 In-Reply-To: <20140425185719.28550.27059.chriscool@tuxfamily.org> Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: 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 Signed-off-by: Junio C Hamano --- 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 @@ -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