Git 挂钩 bash 以检查前缀

Git hook on bash to check prefix

我需要一些关于 bash 脚本的帮助,该脚本会自动在 commit -m 消息中添加前缀,它不是服务器端,只是 repo,我需要添加消息 "User:...",如果用户类型提交消息名称,例如 "Jhon" ,它将是 User:Jhon 。也许谁可以帮助为它写一个脚本?


这是我会做的。


  1. 编写如下所示的脚本(例如 prefix_commit.sh):

    #!/bin/bash
    git commit -m User:""
    

    注:

    • 在这里使用 git commit -m "User:" 也可以。

  1. 使脚本可执行(您只需执行一次):

    $ chmod +x ./prefix_commit.sh
    

  2. 从命令行调用脚本并像这样传递您的提交消息:

    $ ./prefix_commit.sh 'Sample commit message'
    

    注:

    • 如果您计划使用多个单词编写消息,请务必在您的提交消息周围使用 单引号


将参数传递给 bash 脚本

  • 可以在 bash 脚本或函数中接收参数,如下所示:

     # first argument
     # second argument
     # third argument
    
    ### , , ,  ... etc
    

  • 假设我有一个名为 echo_my_args.sh 的脚本,它会输出三个参数:

    #!/bin/bash
    echo 
    echo 
    echo 
    

  • 我可以将三个参数传递给脚本并查看它们的回显:

    $ ./echo_my_args.sh 'my first arg' 'my second arg' 'my third arg'
    my first arg
    my second arg
    my third arg
    

  • 这里再次注意,如果参数有多个单词,则必须使用单引号。如果传递单个单词参数,则不需要单引号:

    $ ./echo_my_args.sh first second third
    first
    second
    third
    

  • 这里有一些关于如何pass arguments to a bash script的更多信息。