Андрей Ка
Андрей Ка

Reputation: 774

Git hook on bash to check prefix

I need some help with a bash script that automatically add prefix in commit -m message ,it's not a server side ,just repo, i need to add message "User:..." , and if a user type commit message name for example "Jhon" , it will be User:Jhon . Maybe who can help to write a script for it ?

Upvotes: 1

Views: 552

Answers (1)

Dan Kreiger
Dan Kreiger

Reputation: 5516


Here's what I would do.


  1. Write a script (e.g. prefix_commit.sh) that looks like this:

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

    Note:

    • Using git commit -m "User:$1" will also work here.

  1. Make the script executable (you only need to do this once):

    $ chmod +x ./prefix_commit.sh
    

  2. Call the script from the command line and pass your commit message like so:

    $ ./prefix_commit.sh 'Sample commit message'
    

    Note:

    • Be sure to use single quotes around your commit message if you plan on writing messages with multiple words.


Passing arguments to a bash script

  • Arguments can be received in a bash script or function like so:

    $1 # first argument
    $2 # second argument
    $3 # third argument
    
    ### $4, $5, $6, $7 ... etc
    

  • So let's say I have a script that's called echo_my_args.sh that echoes out three arguments:

    #!/bin/bash
    echo $1
    echo $2
    echo $3
    

  • I can pass three arguments to the script and see them echoed out:

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

  • Note here again that the argument must use single quotes if it has multiple words. There is no need for single quotes if you pass a single word argument:

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

  • Here's some more information about how to pass arguments to a bash script.

Upvotes: 1

Related Questions