3

System description:

  • Windows 7
  • git version 2.10.1.windows.1
  • TortoiseGit 2.3.0.0


I want:

The merge commit message to be different in a fully automated manner (no manual amend)



Summary:

On windows, using tortoiseGit, there is no prepare-commit-msg hook, only start-commit-hook and no matter what the hook script is, I get a windows popup error:

%1 is not a valid Win32 application

Also, I would prefer to only change the commit messages which are "merges".



Steps:

setup the hook:

enter image description here

Set the working tree path to be same as my project's path,
and in "Command Line To Execute" I select the file I prepared:

.git\hooks\prepare-commit-msg

which its content is echo "test".

Then I try to do a commit and get this error:

enter image description here

I've made sure that this script file is indeed the file which is used,
because if I delete it, I get this message:

enter image description here

Community
  • 1
  • 1
vsync
  • 118,978
  • 58
  • 307
  • 400
  • What version of Windows, SourceTree and Git are you using? Is SourceTree using the embedded Git or the system Git (ie your own Git installed separately from SoruceTree) – VonC Oct 10 '16 at 13:29
  • @VonC - sorry, was about to add those then forgot and just posted. now it's there. – vsync Oct 10 '16 at 13:32
  • Related to - http://stackoverflow.com/q/38371758/104380 – vsync Oct 10 '16 at 14:58

1 Answers1

5

TortoiseGit hooks are fully independent of vanilla Git hooks.

You can use any script language or executable as TortoiseGit hook script. However, in the configuration you have to enter a a valid Windows executable as command, e.g. an .exe file, for .js or .vbs scripts you need to specify the interpreter (wscript or cscript) and the path to the script as the interpreter parameter (cf. here). For executing a bash script I think you need to execute bash.exe with the appropriate parameters to a script - I haven't tested this before.

The executed hook gets some parameters from TortoiseGit, those are documented here, e.g. the Start-commit is called when the commit dialog is opened and gets three cli parameters: PATH MESSAGEFILE CWD, whereas PATH is a path to a file containing all entries of the commit dialog, MESSAGEFILE is the path to a file where you can put the contents to which should be used as commit message and CWD is the working tree path.

Examples can be found on https://github.com/TortoiseGit/TortoiseGit/tree/master/contrib/hook-scripts/client-side

PS: In order to check whether this is a merge commit you can check for the existance of .git/MERGE_HEAD.

MrTux
  • 32,350
  • 30
  • 109
  • 146
  • How do I check `.git/MERGE_HEAD` ? – vsync Oct 10 '16 at 15:01
  • Sorry, was AFK: The hook script gets the path to the working tree as a parameter. Just append `.git/MERGE_HEAD` to it and check whether this file exists. If yes: it's a merge commit, otherwise not. – MrTux Oct 10 '16 at 15:58
  • how do I check? I don't know any of these things.. sorry, It's outside the scope of my work as a client-side developer.. I've never had to do these things or knew of their existence, and google has nearly zero answers on this topic of "tortoisegit hook examples" – vsync Oct 10 '16 at 18:31
  • Updated the answer – MrTux Oct 11 '16 at 15:53
  • @MrTux what should I do if TortoiseGit does not find the interpreter's environment variable? Under the System Variables I have put the PWSH variable that points to pwsh.exe. Then, in the Command Line To Execute in TortoiseGit, I wrote PWSH %root%\'myscript.ps1. After that, I got the message "The system cannot find the file specified". However, if I write the full path to pwsh.exe in the text box, myscript.ps1 is executed. – deralbert Dec 29 '20 at 21:18
  • @MrTux I got it myself. I cannot use any environment variable in this text box, I can only use all the things that are in my PATH environment variable. After I added the path to pwsh.exe to the PATH, my TortoiseGit .ps1 hook worked as expected. – deralbert Dec 30 '20 at 18:17