1

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 ?

iehrlich
  • 3,572
  • 4
  • 34
  • 43
Андрей Ка
  • 756
  • 4
  • 14
  • 33
  • Literally [the first result](https://git-scm.com/book/gr/v2/Customizing-Git-Git-Hooks) from Google... – Sumi Straessle Jul 23 '17 at 20:40
  • Possible duplicate of [How can I change the default comments in the git commit message?](https://stackoverflow.com/questions/3966714/how-can-i-change-the-default-comments-in-the-git-commit-message) – Sumi Straessle Jul 23 '17 at 20:44

1 Answers1

1

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.

Dan Kreiger
  • 5,358
  • 2
  • 23
  • 27
  • Thanks , it works!! maybe you can explain what are "$1" is do ? – Андрей Ка Jul 23 '17 at 16:40
  • amazing! Thank you very much, Dan! now it's clear for me, finally i begin to understand a little bit how it works :) – Андрей Ка Jul 23 '17 at 17:10
  • Not a problem - I'm glad it helped! Bash scripting is a lot of fun. – Dan Kreiger Jul 23 '17 at 17:27
  • Dan , look , now i try to write a script , to check my prefix , if prefix = User:foo, allow commit , if prefix is something else , deny. and now then i commiting my script do nothing :( my code is : #!/bin/sh MSG_empty="--- You must give a meaningful comment for commits. Thanks! ---\n END \n" MSG_prefix="--- You must prefix your checkins with one of the following - User: or Name: echo -e $MSG_empty > /dev/stderr # Make sure that the log message contains prefix. grep -P "^(User|Name):[ \t]+.{2,}" > /dev/null || exit 1 # All checks passed, so allow the commit exit 0 – Андрей Ка Jul 25 '17 at 10:05
  • @АндрейКа I can have a look at this later today after work if that's ok – Dan Kreiger Jul 25 '17 at 10:11
  • Ye sure ,Dan,thanks for reply and help! if you don't understand what i whrote in comments , maybe i can send to you code in another social network? e.g mail or skype ? – Андрей Ка Jul 25 '17 at 10:47
  • You could make a [gist](https://gist.github.com/) and link it here - It sounds like you want to make a bash script that confirms that a commit message has a prefix, right? Does that mean that you want to type a prefix into your commit message? You could run a script that asks the user to choose one of the prefixes - here's an example - https://gist.github.com/dankreiger/8352fdf9ad8305742b97e26b3d7240ea – Dan Kreiger Jul 25 '17 at 18:55
  • hi again, Dan , now i want to write a script that checks to see if there is a word that should be in the commit message, for example if i write git commit -m "updated foo" and if word updated not in commit message commit will be stopped with an error message: please insert a word(updated) , but if word updated are present in commit -m message commit will pass. The problem is that i don't know how to make so that the script would check up my message which I transfer, i try to use git log -1 HEAD --pretty=format:%s , but it will check just my last commit. – Андрей Ка Jul 25 '17 at 19:27
  • My biggest problem is that i can't understand how it works :(, although I've read about it a lot, my most important task for now is to make the script who read my commit -m message word and if it has my word(in sample it is a "updated" it will pass the commit, If not, then not allowed. – Андрей Ка Jul 25 '17 at 19:27
  • If you want a script that requires a word, you could try something like [this](https://gist.github.com/dankreiger/3482ac9f1f4c13357d5cb693058577b5) - this one requires the word 'updated' – Dan Kreiger Jul 25 '17 at 19:44
  • Yesss!!! This is exactly what what i need!! and last question is how to make that the script does not allow the commit if there is no word "updated", and if there is it pass commit. something like this : https://gist.github.com/Zorro55/981f1c70b437e6ecd0f2628a8a69701f – Андрей Ка Jul 25 '17 at 19:55
  • The script I wrote does that, right? If the commit doesn't contain the word 'updated', it exits the script. If it does, it executes the `git commit` - here's a [video](https://www.dropbox.com/s/9ekl2g211ogpowe/sample_bash.mov?dl=0) – Dan Kreiger Jul 25 '17 at 20:37
  • Dan! finally it works!!!! thanks a lot!! you really very friendly and helpful, i glad that there are such wonderful people like you , who are ready to help! – Андрей Ка Jul 25 '17 at 20:46
  • if i use a Windows i don't need execute chmod +x ./prefix_commit.sh command yes? i just copy paste prefix_commit path and execute script yes? like : '/c/Users/Andrejka/Desktop/Git Test/.git/hooks/prefix_commit' – Андрей Ка Jul 25 '17 at 20:52
  • I'm glad I was able to help :) I don't have windows, so I'm not sure if it's necessary on windows - I think in PowerShell all you have to do is `sh` followed by the path to your script. [This link](https://stackoverflow.com/questions/1098786/run-bash-script-from-windows-powershell) is hopefully helpful. – Dan Kreiger Jul 25 '17 at 21:08
  • yes , i use cygwin64, its same, like linux :) how many years you working with shell scripting? – Андрей Ка Jul 25 '17 at 21:18
  • Cool! I guess I've been coding since 2015 - I'm in the terminal all the time when I'm coding, so I've found writing shell scripts to be very useful :) – Dan Kreiger Jul 25 '17 at 22:22
  • Dan , hi again , i install ubuntu and now try to run hook from it , then i run the hook , i get an: Permission denied error , maybe you know what happend? =/ – Андрей Ка Jul 28 '17 at 14:11
  • @АндрейКа I'm 99% sure that you need to `chmod +x ./your_file.sh` or `sudo chmod +x ./your_file.sh` - the location of a shell script always needs to be made executable, so when you use a new location, you need to run `chmod +x` on the file in that location. – Dan Kreiger Jul 28 '17 at 14:28
  • i try but i get chmod: cannot access './your_file.sh': No such file or directory. my scripts full path is : '/home/andrejka/Desktop/Git Test/.git/hooks/prefix-commit' , i try to: andrejka@root:~/Desktop/Git Test$ sudo chmod +x ./home/andrejka/Desktop/Git Test/.git/hooks/prefix-commit – Андрей Ка Jul 28 '17 at 15:14
  • now i set chmod and try to check git status and recieve this : andrejka@root:~/Desktop/Git Test$ git status fatal: unable to read 540505093e91a57d13e094118cdb8bcd75632a7a – Андрей Ка Jul 28 '17 at 19:28
  • i don't know how to run this script now , i set chmod -x secessfully – Андрей Ка Jul 28 '17 at 19:35
  • That is strange. The script is just doing a `git commit` and nothing else. Would you be able to start the project in a new directory and run the script there? I don't know why you are receiving this error. – Dan Kreiger Jul 28 '17 at 19:42
  • i found a solution , Den thanks for chmod!! it works not and it linux , the problem is be this ! ::: https://stackoverflow.com/questions/2920416/configure-bin-shm-bad-interpreter – Андрей Ка Jul 28 '17 at 19:49
  • i just type a sed -i 's/\r//' prefix-commit.sh and it start to work!!:) – Андрей Ка Jul 28 '17 at 19:50
  • Den , sorry again , but i have another same question:) Maybe you know how to make my hook automatic executable if i just type msg: git commit -m "my message" in console and if in my message aren't word "updated" , this commit has been rejected. it something like your first script with updated word , but it need to be auto executeble if i type git commit -m in console. is it passable ? – Андрей Ка Jul 31 '17 at 12:14
  • I would read up about [git hooks](http://githooks.com/) if you want to set a hook when you commit. I haven't done this before, so I'd have to learn it. You could also set a bash function that executes the script for you. Later I can try to explain this option if you're interested. However, I need to learn about making hooks if that's what you're after. – Dan Kreiger Jul 31 '17 at 14:42
  • Great to hear :) – Dan Kreiger Jul 31 '17 at 14:45