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 <chrisc...@tuxfamily.org>
---
 trailer.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/trailer.c b/trailer.c
index cc87918..97a0fe7 100644
--- a/trailer.c
+++ b/trailer.c
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "run-command.h"
 #include "trailer.h"
 /*
  * Copyright (c) 2013, 2014 Christian Couder <chrisc...@tuxfamily.org>
@@ -13,11 +14,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;
@@ -59,6 +63,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);
@@ -389,6 +400,7 @@ static int git_trailer_config(const char *conf_key, const 
char *value, void *cb)
                        if (conf->command)
                                warning(_("more than one %s"), orig_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))
@@ -423,6 +435,44 @@ static void parse_trailer(struct strbuf *tok, struct 
strbuf *val, const char *tr
        }
 }
 
+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);
+       else
+               result = strbuf_detach(&buf, NULL);
+
+       strbuf_release(&cmd);
+       return result;
+}
 
 static void duplicate_conf(struct conf_info *dst, struct conf_info *src)
 {
@@ -445,6 +495,10 @@ static struct trailer_item *new_trailer_item(struct 
trailer_item *conf_item,
                duplicate_conf(&new->conf, &conf_item->conf);
                new->token = xstrdup(conf_item->conf.key);
                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;
 
@@ -497,12 +551,21 @@ static struct trailer_item *process_command_line_args(int 
argc, const char **arg
        int i;
        struct trailer_item *arg_tok_first = NULL;
        struct trailer_item *arg_tok_last = NULL;
+       struct trailer_item *item;
 
        for (i = 0; i < argc; i++) {
                struct trailer_item *new = create_trailer_item(argv[i]);
                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.8.5.2.204.gcfe299d.dirty


--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to