git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christian Couder <chriscool@tuxfamily.org>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, Johan Herland <johan@herland.net>,
	Josh Triplett <josh@joshtriplett.org>,
	Thomas Rast <tr@thomasrast.ch>,
	Michael Haggerty <mhagger@alum.mit.edu>,
	Dan Carpenter <dan.carpenter@oracle.com>,
	Greg Kroah-Hartman <greg@kroah.com>, Jeff King <peff@peff.net>,
	Eric Sunshine <sunshine@sunshineco.com>,
	Ramsay Jones <ramsay@ramsay1.demon.co.uk>,
	Jonathan Nieder <jrnieder@gmail.com>,
	Marc Branchaud <marcnarc@xiplink.com>
Subject: [PATCH v14 09/11] trailer: execute command from 'trailer.<name>.command'
Date: Mon, 15 Sep 2014 07:31:39 +0200	[thread overview]
Message-ID: <20140915053142.26573.32983.chriscool@tuxfamily.org> (raw)
In-Reply-To: <20140915052330.26573.34823.chriscool@tuxfamily.org>

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@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 trailer.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 84 insertions(+), 1 deletion(-)

diff --git a/trailer.c b/trailer.c
index 8e41b8f..0e76310 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@tuxfamily.org>
@@ -33,6 +35,8 @@ static struct trailer_item *first_conf_item;
 
 static char *separators = ":";
 
+#define TRAILER_ARG_STRING "$ARG"
+
 static int after_or_end(enum action_where where)
 {
 	return (where == WHERE_AFTER) || (where == WHERE_END);
@@ -73,6 +77,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);
@@ -198,6 +209,63 @@ static struct trailer_item *remove_first(struct trailer_item **first)
 	return item;
 }
 
+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 apply_item_command(struct trailer_item *in_tok, struct trailer_item *arg_tok)
+{
+	if (arg_tok->conf.command) {
+		const char *arg;
+		if (arg_tok->value && arg_tok->value[0]) {
+			arg = arg_tok->value;
+		} else {
+			if (in_tok && in_tok->value)
+				arg = xstrdup(in_tok->value);
+			else
+				arg = xstrdup("");
+		}
+		arg_tok->value = apply_command(arg_tok->conf.command, arg);
+		free((char *)arg);
+	}
+}
+
 static void apply_arg_if_exists(struct trailer_item *in_tok,
 				struct trailer_item *arg_tok,
 				struct trailer_item *on_tok,
@@ -209,16 +277,19 @@ static void apply_arg_if_exists(struct trailer_item *in_tok,
 		free_trailer_item(arg_tok);
 		break;
 	case EXISTS_REPLACE:
+		apply_item_command(in_tok, arg_tok);
 		add_arg_to_input_list(on_tok, arg_tok,
 				      in_tok_first, in_tok_last);
 		remove_from_list(in_tok, in_tok_first, in_tok_last);
 		free_trailer_item(in_tok);
 		break;
 	case EXISTS_ADD:
+		apply_item_command(in_tok, arg_tok);
 		add_arg_to_input_list(on_tok, arg_tok,
 				      in_tok_first, in_tok_last);
 		break;
 	case EXISTS_ADD_IF_DIFFERENT:
+		apply_item_command(in_tok, arg_tok);
 		if (check_if_different(in_tok, arg_tok, 1))
 			add_arg_to_input_list(on_tok, arg_tok,
 					      in_tok_first, in_tok_last);
@@ -226,6 +297,7 @@ static void apply_arg_if_exists(struct trailer_item *in_tok,
 			free_trailer_item(arg_tok);
 		break;
 	case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR:
+		apply_item_command(in_tok, arg_tok);
 		if (check_if_different(on_tok, arg_tok, 0))
 			add_arg_to_input_list(on_tok, arg_tok,
 					      in_tok_first, in_tok_last);
@@ -249,6 +321,7 @@ static void apply_arg_if_missing(struct trailer_item **in_tok_first,
 	case MISSING_ADD:
 		where = arg_tok->conf.where;
 		in_tok = after_or_end(where) ? in_tok_last : in_tok_first;
+		apply_item_command(NULL, arg_tok);
 		if (*in_tok) {
 			add_arg_to_input_list(*in_tok, arg_tok,
 					      in_tok_first, in_tok_last);
@@ -531,7 +604,7 @@ static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,
 					     char *tok, char *val)
 {
 	struct trailer_item *new = xcalloc(sizeof(*new), 1);
-	new->value = val;
+	new->value = val ? val : xstrdup("");
 
 	if (conf_item) {
 		duplicate_conf(&new->conf, &conf_item->conf);
@@ -600,7 +673,17 @@ 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;
+
+	/* Add a trailer item for each configured trailer with a command */
+	for (item = first_conf_item; item; item = item->next) {
+		if (item->conf.command) {
+			struct trailer_item *new = new_trailer_item(item, NULL, NULL);
+			add_trailer_item(&arg_tok_first, &arg_tok_last, new);
+		}
+	}
 
+	/* Add a trailer item for each trailer on the command line */
 	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);
-- 
2.0.3.960.g41c6e4c

  parent reply	other threads:[~2014-09-15  5:51 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-15  5:31 [PATCH v14 00/11] Add interpret-trailers builtin Christian Couder
2014-09-15  5:31 ` [PATCH v14 01/11] trailer: add data structures and basic functions Christian Couder
2014-09-15 20:39   ` Junio C Hamano
2014-09-16  8:01     ` Christian Couder
2014-09-17  7:58       ` Jeff King
2014-09-17  8:26         ` Christian Couder
2014-09-15  5:31 ` [PATCH v14 02/11] trailer: process trailers from input message and arguments Christian Couder
2014-09-15  5:31 ` [PATCH v14 03/11] trailer: read and process config information Christian Couder
2014-09-15  5:31 ` [PATCH v14 04/11] trailer: process command line trailer arguments Christian Couder
2014-09-15  5:31 ` [PATCH v14 05/11] trailer: parse trailers from file or stdin Christian Couder
2014-09-15  5:31 ` [PATCH v14 06/11] trailer: put all the processing together and print Christian Couder
2014-09-15  5:31 ` [PATCH v14 07/11] trailer: add interpret-trailers command Christian Couder
2014-09-15  5:31 ` [PATCH v14 08/11] trailer: add tests for "git interpret-trailers" Christian Couder
2014-09-15  5:31 ` Christian Couder [this message]
2014-09-15  5:31 ` [PATCH v14 10/11] trailer: add tests for commands in config file Christian Couder
2014-09-15  5:31 ` [PATCH v14 11/11] Documentation: add documentation for 'git interpret-trailers' Christian Couder

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140915053142.26573.32983.chriscool@tuxfamily.org \
    --to=chriscool@tuxfamily.org \
    --cc=dan.carpenter@oracle.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=greg@kroah.com \
    --cc=johan@herland.net \
    --cc=josh@joshtriplett.org \
    --cc=jrnieder@gmail.com \
    --cc=marcnarc@xiplink.com \
    --cc=mhagger@alum.mit.edu \
    --cc=peff@peff.net \
    --cc=ramsay@ramsay1.demon.co.uk \
    --cc=sunshine@sunshineco.com \
    --cc=tr@thomasrast.ch \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).