From patchwork Wed Mar 31 10:05:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12174973 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9CABC433DB for ; Wed, 31 Mar 2021 10:06:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 852EC61964 for ; Wed, 31 Mar 2021 10:06:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234545AbhCaKFy (ORCPT ); Wed, 31 Mar 2021 06:05:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234666AbhCaKFv (ORCPT ); Wed, 31 Mar 2021 06:05:51 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CB22C06174A for ; Wed, 31 Mar 2021 03:05:51 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id b2-20020a7bc2420000b029010be1081172so857368wmj.1 for ; Wed, 31 Mar 2021 03:05:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=T0xjrZujx9SUCLKrBQ0Q0tNcS3+AJRic7R9alexEAb0=; b=DGBy6vFKeLsKjRLEE2cuJgt2NFwj1FSvQgXZQx8EqAIaSSj878eHWL5YXejQtStc1U WIewzdSKhpLlyOyn0tj0snxvBB1SDpiULLVrjljQGu6HRB1F/fN5H99BN7B1H4pjG0B8 bwEz7HRKbnzskNPnbtFWId2VXBQYh6S1C8sb5gkCul9IadPm1Gue6zU4B6eHbv2srSFQ VRlFw4tEVINtavzvKa2AEQuYz5eoH39jRdDHYHROZFSUU63f0UMN1LWcjRztKuz1E2t2 /3BvmJ7jk+tvOTe546ai5+ruY9aTCjn/GaB0FhzqdOa+OwnXRMsOMV3/9rakEhtb7xhl AH3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=T0xjrZujx9SUCLKrBQ0Q0tNcS3+AJRic7R9alexEAb0=; b=rpdkHESWAhK86jWmnDcHLwXH+2SdQsvfcSQK9Wu+1YXvi2PHDccn1JKt36ReqcbdUQ Zlv2A+gUCl4nXyKe5ryW5E9SWtw4/7XzMcvQ9E1sQSqjnRmjGYeul3EhRIs6HOkiguNm mHTOkX8aHRdbYKLjb9wv4W1UhAbN25XDjusPeym8NCqYvNFx3I2FjQ/u+dBr5ImB0yk8 JTbuYYEK+EfqmYOCliX2+EY12d8RUcvjTlUUzkPlWvGH8x+iYOVkTnKbZiGmoCvDcgWH vh7qHoNRYAP3oF7imOkdeY3wSegV2QjN5JD0Nzkqw0T1Cn7Y7N4FIvessmr9PjXFTvAO wdDQ== X-Gm-Message-State: AOAM531P+hE4KlAcJ+DHMvnMwoKYjwZBZQU8d8FkNAzzvuoyR9xwIS9/ lkmC1S4+WOnvUnY6ZvTvn+1+aSORcaA= X-Google-Smtp-Source: ABdhPJyg90SxfkRFQOXuYFx1bnvmfxF4/KLLsoG/BjFHciVSqd5gbUqpRTsWKbkRsbfQz2Pb7bSC/Q== X-Received: by 2002:a1c:8010:: with SMTP id b16mr2491061wmd.49.1617185149893; Wed, 31 Mar 2021 03:05:49 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j12sm3536291wrx.59.2021.03.31.03.05.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Mar 2021 03:05:49 -0700 (PDT) Message-Id: <4c59cab53a0d9bb7c9cccfaf5544ae5c904bb2ba.1617185147.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 31 Mar 2021 10:05:46 +0000 Subject: [PATCH v5 1/2] [GSOC] run-command: add shell_no_implicit_args option Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , Junio C Hamano , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu When we use subprocess to run a shell-script, if we have any args, git will add extra $@ to the end of the shell-script, This can pass positional parameters correctly, But if we just want to use some of these passed parameters, git will still add an extra "$@", which contains all positional parameters we passed. This does not meet our expectations. E.g. our shell-script is: "echo \"\$1\"" and pass $1 "abc", git will change our script to: "echo \"\$1\" \"$@\"" The positional parameters we entered will be printed repeatedly. So let add a new `shell_no_implicit_args` to `struct child_process`, which can suppress the joining of $@ if `shell_no_implicit_args` is set to 1, this will allow us to use only few of positional args in multi-parameter shell script, instead of using all of them. Signed-off-by: ZheNing Hu --- run-command.c | 8 ++++---- run-command.h | 1 + trailer.c | 1 + 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/run-command.c b/run-command.c index be6bc128cd9d..a2cf6177f522 100644 --- a/run-command.c +++ b/run-command.c @@ -264,7 +264,7 @@ int sane_execvp(const char *file, char * const argv[]) return -1; } -static const char **prepare_shell_cmd(struct strvec *out, const char **argv) +static const char **prepare_shell_cmd(struct strvec *out, const char **argv, int shell_no_implicit_args) { if (!argv[0]) BUG("shell command is empty"); @@ -281,7 +281,7 @@ static const char **prepare_shell_cmd(struct strvec *out, const char **argv) * If we have no extra arguments, we do not even need to * bother with the "$@" magic. */ - if (!argv[1]) + if (!argv[1] || shell_no_implicit_args) strvec_push(out, argv[0]); else strvec_pushf(out, "%s \"$@\"", argv[0]); @@ -416,7 +416,7 @@ static int prepare_cmd(struct strvec *out, const struct child_process *cmd) if (cmd->git_cmd) { prepare_git_cmd(out, cmd->argv); } else if (cmd->use_shell) { - prepare_shell_cmd(out, cmd->argv); + prepare_shell_cmd(out, cmd->argv, cmd->shell_no_implicit_args); } else { strvec_pushv(out, cmd->argv); } @@ -929,7 +929,7 @@ int start_command(struct child_process *cmd) if (cmd->git_cmd) cmd->argv = prepare_git_cmd(&nargv, cmd->argv); else if (cmd->use_shell) - cmd->argv = prepare_shell_cmd(&nargv, cmd->argv); + cmd->argv = prepare_shell_cmd(&nargv, cmd->argv, cmd->shell_no_implicit_args); cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, (char**) cmd->env, cmd->dir, fhin, fhout, fherr); diff --git a/run-command.h b/run-command.h index d08414a92e73..9597c987c5bb 100644 --- a/run-command.h +++ b/run-command.h @@ -133,6 +133,7 @@ struct child_process { * argv[1], etc, do not need to be shell-quoted. */ unsigned use_shell:1; + unsigned shell_no_implicit_args:1; unsigned stdout_to_stderr:1; unsigned clean_on_exit:1; diff --git a/trailer.c b/trailer.c index be4e9726421c..35dd0f4c8512 100644 --- a/trailer.c +++ b/trailer.c @@ -231,6 +231,7 @@ static char *apply_command(const char *command, const char *arg) cp.env = local_repo_env; cp.no_stdin = 1; cp.use_shell = 1; + cp.shell_no_implicit_args = 1; if (capture_command(&cp, &buf, 1024)) { error(_("running trailer command '%s' failed"), cmd.buf); From patchwork Wed Mar 31 10:05:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12174977 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 380A1C433C1 for ; Wed, 31 Mar 2021 10:07:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EF13061962 for ; Wed, 31 Mar 2021 10:06:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234773AbhCaKGZ (ORCPT ); Wed, 31 Mar 2021 06:06:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234764AbhCaKFw (ORCPT ); Wed, 31 Mar 2021 06:05:52 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC0D6C061574 for ; Wed, 31 Mar 2021 03:05:51 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id k8so19051156wrc.3 for ; Wed, 31 Mar 2021 03:05:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=c9tIz5EE8oCq1ynp0WJE8QCgdNDZmUoBg58/qUTggKY=; b=NWY2r7vX2eIEApipJ+PsyKQvJQlnwIT634q0VzHFGoHL0gq1L7Kdc3fK8E5ZA1KuNS iPrGMymsv4zilPyCXuqzzBxOE6qB8n94vtgkdT2a9nzK9IHenooSiscv0Hwa98ErIqkq 88HwM1iQ2QR1b2TsCW+sIA20SjwPlwTg54rHeVxhED/ft4Jwerv+wqmguQTP0KUv9ErP PplbYo0SrONoQ33VuibrKx29sN4BNTciOJx01fSJsSvQ/fWpCPLYrWGGUz9009fFsU5c ecrKX5XKolT7EO2SqWjQ5ZSwyOBbSpufi/qelkkI27KrRvPxC7wf5DPH6gnQ8aXRxHYP Jydg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=c9tIz5EE8oCq1ynp0WJE8QCgdNDZmUoBg58/qUTggKY=; b=ACYObuSn66weNsYh5ciHUOXCSSA93SqKNpLT4oTyHrkegOwTgnihzz4tfDVR3jNXbt iAabS5tQZCoDt8s3k4BYHJ99ojUU8bDbiE4F0qlEBlR2+wwk+5Q7JPuTo+pH8Y6U56ma IO7Cp5NppFJIGv6OE0sDcKuXtvtT1Z2jAlF3T4j7hsX+cwA1YQTuFfrITeuc4gCNlPqO xu+PfJNtzuZRDHw8gv0+UO61CZnQQg8aAvvnes9/jSTVuh4eRShB7UsvMYeZS5+pI7AU 0Q/hWKpuiiKPVEMe5eptDpUxHn5lfPUbJZMsgznzAX5tJ/3mUzOMzW93JQEnYScGM96z YZpA== X-Gm-Message-State: AOAM530fZXQ88Ba7k7dgIcPRovbvF71Y07qz9u2n1GdXO3JJxoGWIX3F cDk4shC5+AYtnr2lzgTRw//lcmHhOr0= X-Google-Smtp-Source: ABdhPJx4KsqpyRurPejB+0hDeYIlovp8Ni/cEXPg0ssvbxpxdBFlCaqG4l7UBllZdvVfhdDq5gyj4A== X-Received: by 2002:a5d:4d09:: with SMTP id z9mr2663120wrt.426.1617185150574; Wed, 31 Mar 2021 03:05:50 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u17sm3289951wrw.51.2021.03.31.03.05.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Mar 2021 03:05:50 -0700 (PDT) Message-Id: <5894d8c4b36466326b0427bfda0d6981e52a0907.1617185147.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 31 Mar 2021 10:05:47 +0000 Subject: [PATCH v5 2/2] [GSOC]trailer: pass arg as positional parameter Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , Junio C Hamano , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu The `trailer..command` configuration variable specifies a command (run via the shell, so it does not have to be a single name of or path to the command, but can be a shell script), and the first occurrence of substring $ARG is replaced with the value given to the `interpret-trailer` command for the token. This has two downsides: * The use of $ARG in the mechanism misleads the users that the value is passed in the shell variable, and tempt them to use $ARG more than once, but that would not work, as the second and subsequent $ARG are not replaced. * Because $ARG is textually replaced without regard to the shell language syntax, even '$ARG' (inside a single-quote pair), which a user would expect to stay intact, would be replaced, and worse, if the value had an unmatching single quote (imagine a name like "O'Connor", substituted into NAME='$ARG' to make it NAME='O'Connor), it would result in a broken command that is not syntactically correct (or worse). Introduce a new `trailer..cmd` configuration that takes higher precedence to deprecate and eventually remove `trailer..command`, which passes the value as a parameter to the command. Instead of "$ARG", the users will refer to the value as positional argument, $1, in their scripts. Helped-by: Junio C Hamano Helped-by: Christian Couder Signed-off-by: ZheNing Hu --- Documentation/git-interpret-trailers.txt | 75 ++++++++++++++++++++---- t/t7513-interpret-trailers.sh | 61 ++++++++++++++++++- trailer.c | 39 ++++++++---- 3 files changed, 152 insertions(+), 23 deletions(-) diff --git a/Documentation/git-interpret-trailers.txt b/Documentation/git-interpret-trailers.txt index 96ec6499f001..bbd1c9bfd65e 100644 --- a/Documentation/git-interpret-trailers.txt +++ b/Documentation/git-interpret-trailers.txt @@ -236,21 +236,34 @@ trailer..command:: be called to automatically add or modify a trailer with the specified . + -When this option is specified, the behavior is as if a special -'=' argument were added at the beginning of the command -line, where is taken to be the standard output of the -specified command with any leading and trailing whitespace trimmed -off. +When this option is specified, the first occurrence of substring $ARG is +replaced with the value given to the `interpret-trailer` command for the +same token. + -If the command contains the `$ARG` string, this string will be -replaced with the part of an existing trailer with the same -, if any, before the command is launched. +".command" has been deprecated due to the $ARG in the user's command can +only be replaced once and the original way of replacing $ARG was not safe. +Now the preferred option is using "trailer..cmd", which use position +argument to pass the value. ++ +When both .cmd and .command are given for the same , +.cmd is used and .command is ignored. + +trailer..cmd:: + The command specified by this configuration variable is run + with a single parameter, which is the part of an + existing trailer with the same . The output from the + command is then used as the value for the in the + resulting trailer. ++ +When this option is specified, If there is no trailer with same , +the behavior is as if a special '=' argument were added at +the beginning of the command, will be passed to the user's +command as an empty value. + If some '=' arguments are also passed on the command line, when a 'trailer..command' is configured, the command will also be executed for each of these arguments. And the part of -these arguments, if any, will be used to replace the `$ARG` string in -the command. +these arguments, if any, will be passed to the command as first parameter. EXAMPLES -------- @@ -333,6 +346,48 @@ subject Fix #42 ------------ +* Configure a 'see' trailer with a cmd to show the subject of a + commit that is related, and show how it works: ++ +------------ +$ git config trailer.see.key "See-also: " +$ git config trailer.see.ifExists "replace" +$ git config trailer.see.ifMissing "doNothing" +$ git config trailer.see.cmd "git show -s --pretty=reference \"\$1\"" +$ git interpret-trailers < subject +> +> message +> +> see: HEAD~2 +> EOF +subject + +message + +See-also: fe3187489d69c4 (subject of related commit, 2021-3-20) +------------ + +* Configure a 'bug' trailer with a cmd to show when and where + was the bug introduced, and show how it works: ++ +------------ +$ git config trailer.bug.key "Bug-from: " +$ git config trailer.bug.ifExists "replace" +$ git config trailer.bug.cmd "git log --grep \"\$1\" -1 --pretty=\"%h %aD\"" +$ git interpret-trailers --trailer="bug:the information manager from hell" < subject +> +> message +> +> EOF +subject + +message + +Bug-from: 57d84f8d93 Mon, 6 Aug 2012 18:27:09 +0700 +------------ + * Configure a 'see' trailer with a command to show the subject of a commit that is related, and show how it works: + diff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh index 6602790b5f4c..aec240f1dc05 100755 --- a/t/t7513-interpret-trailers.sh +++ b/t/t7513-interpret-trailers.sh @@ -51,6 +51,24 @@ test_expect_success 'setup' ' EOF ' +test_expect_success 'with cmd' ' + test_when_finished "git config --unset trailer.bug.key && \ + git config --unset trailer.bug.ifExists && \ + git config --unset trailer.bug.cmd" && + git config trailer.bug.key "Bug-maker: " && + git config trailer.bug.ifExists "add" && + git config trailer.bug.cmd "echo \"\$@\"" && + cat >>expected2 <<-EOF && + + Bug-maker: + Bug-maker: jocker + Bug-maker: batman + EOF + git interpret-trailers --trailer "bug: jocker" --trailer "bug:batman" \ + >actual2 && + test_cmp expected2 actual2 +' + test_expect_success 'without config' ' sed -e "s/ Z\$/ /" >expected <<-\EOF && @@ -1274,9 +1292,50 @@ test_expect_success 'setup a commit' ' git commit -m "Add file a.txt" ' +test_expect_success 'with cmd and $1' ' + test_when_finished "git config --unset trailer.fix.cmd" && + git config trailer.fix.ifExists "replace" && + git config trailer.fix.cmd "test -n \"\$1\" && git log -1 --oneline --format=\"%h (%s)\" \ + --abbrev-commit --abbrev=14 \"\$1\" || true" && + FIXED=$(git log -1 --oneline --format="%h (%s)" --abbrev-commit --abbrev=14 HEAD) && + cat complex_message_body >expected2 && + sed -e "s/ Z\$/ /" >>expected2 <<-EOF && + Fixes: $FIXED + Acked-by= Z + Reviewed-by: + Signed-off-by: Z + Signed-off-by: A U Thor + EOF + git interpret-trailers --trailer "review:" --trailer "fix=HEAD" \ + actual2 && + test_cmp expected2 actual2 +' + +test_expect_success 'cmd takes precedence over command' ' + test_when_finished "git config --unset trailer.fix.cmd" && + git config trailer.fix.ifExists "replace" && + git config trailer.fix.cmd "test -n \"\$1\" && git log -1 --oneline --format=\"%h (%aN)\" \ + --abbrev-commit --abbrev=14 \"\$1\" || true" && + git config trailer.fix.command "git log -1 --oneline --format=\"%h (%s)\" \ + --abbrev-commit --abbrev=14 \$ARG" && + FIXED=$(git log -1 --oneline --format="%h (%aN)" --abbrev-commit --abbrev=14 HEAD) && + cat complex_message_body >expected2 && + sed -e "s/ Z\$/ /" >>expected2 <<-EOF && + Fixes: $FIXED + Acked-by= Z + Reviewed-by: + Signed-off-by: Z + Signed-off-by: A U Thor + EOF + git interpret-trailers --trailer "review:" --trailer "fix=HEAD" \ + actual2 && + test_cmp expected2 actual2 +' + test_expect_success 'with command using $ARG' ' git config trailer.fix.ifExists "replace" && - git config trailer.fix.command "git log -1 --oneline --format=\"%h (%s)\" --abbrev-commit --abbrev=14 \$ARG" && + git config trailer.fix.command "git log -1 --oneline --format=\"%h (%s)\" \ + --abbrev-commit --abbrev=14 \$ARG" && FIXED=$(git log -1 --oneline --format="%h (%s)" --abbrev-commit --abbrev=14 HEAD) && cat complex_message_body >expected && sed -e "s/ Z\$/ /" >>expected <<-EOF && diff --git a/trailer.c b/trailer.c index 35dd0f4c8512..a000293d6e7e 100644 --- a/trailer.c +++ b/trailer.c @@ -14,6 +14,7 @@ struct conf_info { char *name; char *key; char *command; + char *cmd; enum trailer_where where; enum trailer_if_exists if_exists; enum trailer_if_missing if_missing; @@ -127,6 +128,7 @@ static void free_arg_item(struct arg_item *item) free(item->conf.name); free(item->conf.key); free(item->conf.command); + free(item->conf.cmd); free(item->token); free(item->value); free(item); @@ -216,22 +218,28 @@ static int check_if_different(struct trailer_item *in_tok, return 1; } -static char *apply_command(const char *command, const char *arg) +static char *apply_command(struct conf_info *conf, const char *arg) { struct strbuf cmd = STRBUF_INIT; struct strbuf buf = STRBUF_INIT; struct child_process cp = CHILD_PROCESS_INIT; char *result; - strbuf_addstr(&cmd, command); - if (arg) - strbuf_replace(&cmd, TRAILER_ARG_STRING, arg); - - strvec_push(&cp.args, cmd.buf); + if (conf->cmd) { + cp.shell_no_implicit_args = 1; + strbuf_addstr(&cmd, conf->cmd); + strvec_push(&cp.args, cmd.buf); + if (arg) + strvec_push(&cp.args, arg); + } else if (conf->command) { + strbuf_addstr(&cmd, conf->command); + strvec_push(&cp.args, cmd.buf); + if (arg) + strbuf_replace(&cmd, TRAILER_ARG_STRING, arg); + } cp.env = local_repo_env; cp.no_stdin = 1; cp.use_shell = 1; - cp.shell_no_implicit_args = 1; if (capture_command(&cp, &buf, 1024)) { error(_("running trailer command '%s' failed"), cmd.buf); @@ -248,7 +256,7 @@ static char *apply_command(const char *command, const char *arg) static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok) { - if (arg_tok->conf.command) { + if (arg_tok->conf.command || arg_tok->conf.cmd) { const char *arg; if (arg_tok->value && arg_tok->value[0]) { arg = arg_tok->value; @@ -258,7 +266,7 @@ static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg else arg = xstrdup(""); } - arg_tok->value = apply_command(arg_tok->conf.command, arg); + arg_tok->value = apply_command(&arg_tok->conf, arg); free((char *)arg); } } @@ -431,6 +439,7 @@ static void duplicate_conf(struct conf_info *dst, const struct conf_info *src) dst->name = xstrdup_or_null(src->name); dst->key = xstrdup_or_null(src->key); dst->command = xstrdup_or_null(src->command); + dst->cmd = xstrdup_or_null(src->cmd); } static struct arg_item *get_conf_item(const char *name) @@ -455,8 +464,8 @@ static struct arg_item *get_conf_item(const char *name) return item; } -enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_WHERE, - TRAILER_IF_EXISTS, TRAILER_IF_MISSING }; +enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_CMD, + TRAILER_WHERE, TRAILER_IF_EXISTS, TRAILER_IF_MISSING }; static struct { const char *name; @@ -464,6 +473,7 @@ static struct { } trailer_config_items[] = { { "key", TRAILER_KEY }, { "command", TRAILER_COMMAND }, + { "cmd", TRAILER_CMD }, { "where", TRAILER_WHERE }, { "ifexists", TRAILER_IF_EXISTS }, { "ifmissing", TRAILER_IF_MISSING } @@ -543,6 +553,11 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb) warning(_("more than one %s"), conf_key); conf->command = xstrdup(value); break; + case TRAILER_CMD: + if (conf->cmd) + warning(_("more than one %s"), conf_key); + conf->cmd = xstrdup(value); + break; case TRAILER_WHERE: if (trailer_set_where(&conf->where, value)) warning(_("unknown value '%s' for key '%s'"), value, conf_key); @@ -709,7 +724,7 @@ static void process_command_line_args(struct list_head *arg_head, /* Add an arg item for each configured trailer with a command */ list_for_each(pos, &conf_head) { item = list_entry(pos, struct arg_item, list); - if (item->conf.command) + if (item->conf.cmd || item->conf.command) add_arg_item(arg_head, xstrdup(token_from_item(item, NULL)), xstrdup(""),