使用 LazyGit 自定义命令向 git 提交消息添加前缀

问题描述 投票:0回答:1

我想将我的票证名称号码自动添加到我的提交中。票证值作为前缀包含在我的分支中,例如:TEST-12345-my-branch-name。

这是我要修复的命令:

git commit -m ''$(git branch --contains | grep '*' | awk '{print $2}' | awk -F'-' '{print $1"-"$2}' | awk '{print $1 ":{{.Form.Message}}"}')''

这是 LazyGit 配置文件中的整个自定义命令:

customCommands:
  - key: "<c-c>"
    context: "files"
    prompts:
      - type: "input"
        title: "Add Commit Message"
        key: "Message"

    command: git commit -m ''$(git branch --contains | grep '*' | awk '{print $2}' | awk -F'-' '{print $1"-"$2}' | awk '{print $1 ":{{.Form.Message}}"}')''
    subprocess: true

    description: "Custom commit message"

当消息中没有空格时,我可以使其工作(例如。

my-commit-message
->
TEST-12345-my-commit-message
)。但是,如果我尝试添加
my commit message
消息,它会给我下一个错误:

error: pathspec 'commit' did not match any file(s) known to git
error: pathspec 'message' did not match any file(s) known to git

我希望收到类似

TEST-12345 my commit message
的提交消息。

我认为问题出在最后一个管道的 awk

awk '{print $1 ":{{.Form.Message}}"}'
。你能帮我解决它吗?我不知道如何才能取得进步。谢谢。

awk lazygit
1个回答
0
投票

重复的单引号在这里绝对没有任何作用。相反,您希望将整个命令替换嵌入双引号内。

为了提高效率,我已将其重构为单个 Awk 进程。为了便于阅读,我将其显示在多行中。

git commit -m "$(git branch --contains |
    awk '/\*/{split("-", n, $2);
        print (n[1] "-" n[2]}  ":{{.Form.Message}}"}')"

另请参阅 何时在 shell 变量周围加引号? 以及也许 无用的使用

grep
|
awk

© www.soinside.com 2019 - 2024. All rights reserved.