Git GUI/TortoiseGit Not calling hooks that use powershell correctly? (Windows)

2.1k views Asked by At

I'm seeing some strange stuff in how Git GUI and TortoiseGit call Git hooks. Given the following post-checkout hook, one would expect git to execute the powershell command and create the file:

#!/bin/sh
c:/windows/system32/WindowsPowerShell/v1.0/PowerShell.exe -Command "New-Item File.txt -type file"

If I run this hook using the Git Bash, the file is created in my repository. If I run this hook using Atalassian's SourceTree, I also get the file. The hook starts to act strange when calling a checkout from either TortoiseGit or GitGui. Neither produce the file.

I have been able to get the hook (bash) to call a simple exe that I created. The problem seems to be with powershell or how i'm invoking powershell. I have tried to call powershell with -Sta, -ExecutionPolicy RemoteSigned and -NoProfile, but nothing seems to work in Git Gui or TortoiseGit.

The big surprise for me was the hook not working correctly with GitGui. Does anyone know what could be causing this?

How to reproduce:

  1. Running on Win7
  2. Have Git-Gui Version 0.17.GITGUI
  3. Have Git Version 1.8.3.msysgit.0
  4. Init a new repo
  5. Create a file in the new repo under .git/hooks called post-checkout
  6. Put the following script in the newly created post-checkout hook:

--

#!/bin/sh
c:/windows/system32/WindowsPowerShell/v1.0/PowerShell.exe -Command "New-Item File.txt -type file"

--

  1. Add/commit any file to your new repo
  2. run git checkout master in your new repo using git bash
  3. Look for a file named File.txt (Confirms that git bash is calling powershell correctly)
  4. Delete the new file
  5. Open GitGui or TortoiseGit in the same repo and run a checkout on master
  6. Look for the file called File.txt (Won't exist)

Any help, thoughts? I don't know TCL, so I couldn't really understand how GitGui was calling hooks.

2

There are 2 answers

0
Eris On

I think I saw this happen once before. The ExecutionPolicy is different when in an interactive shell vs a shell spawned by a script. Try this:

c:/windows/system32/WindowsPowerShell/v1.0/PowerShell.exe -ExecutionPolicy:Bypass -Command "New-Item File.txt -type file" 
1
areyling On

I tried out the steps to reproduce and get the same result. Changing the powershell path to /c/... did nothing, but I was able to get the PowerShell script to be called by using the start command. Looking at the file for start (C:\Program Files (x86)\Git\bin\start on my system), it seems to simply be wrapping the call with the following:

cmd //c start "$@"

I've tried several variations, but cannot for the life of me seem to get a PowerShell script to run as well as return output (any console/error messages or an exit code).

Long story short, if you're just needing to make sure the script is ran you can use:

start PowerShell.exe [params]

If anyone has ideas on how to get output and an exit code, I'd love to hear it.