4

I want to deploy a hook script to control format of commit message. For this I use the prepare-commit-msg script.

I want to check if the commit message starts with a number of 3 digits. If this is the case, the script returns 0, otherwise 1.

It works fine for an utilisation with the git command, but it doesn't when used with gitk. When script returns 1, the commit is not aborded. I don't manage to display message either (an information message explaining why commit will be abort).

Is there a way to use hook script with gitk?

Thanks.

PS: I use git version 1.8.5.3

  • The `prepare-commit-msg` prepares a commit message for new commit. (Thus on calling `git commit`). `gitk` is used only for viewing the git history. No hooks can be executed then. – petrpulc Sep 11 '17 at 10:33
  • What are you trying to achieve? – petrpulc Sep 11 '17 at 10:34

1 Answers1

1

As mentioned in the Git client-side hooks, the prepare-commit-msg hook is run before the commit message editor is fired up but after the default message is created.
So that would not feed your requirement. It would be better to use a commit-msg hook.

The commit-msg hook takes one parameter, which again is the path to a temporary file that contains the commit message written by the developer.
If this script exits non-zero, Git aborts the commit process, so you can use it to validate your project state or commit message before allowing a commit to go through.

After that, it depends on how your script is written: see "How do I prompt the user from within a commit-msg hook?", and this answer in particular:

if you use gitk or git-gui you won't be able to prompt because you get an error on the "exec < /dev/tty" line.

You can replace that by a function defined in your hook bash script

function f_askContinue {
  local myQuestion=$1

  while true; do
     read -p "${myQuestion} " -n 1 -r answer
     case $answer in
        [Yy]* ) printf "\nOK\n"; break;;
        [Nn]* )   printf "\nAbandon\n";
                  exit;;
        * ) printf "\nAnswer with Yes or No.\n";;
     esac
  done
}

f_askContinue "Do you want to continue ?"

(Adapt that to the logic you want to implement in your control)

That would work from gitk.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250