2

I am developing simple hook for git. It should cancel commit / push if Acrobat is opened on Windows Machine. Since Git hooks can not find windows process directly i am running next Powershell script named pre-commit.ps1

UPD No solution so far. Powershell exit 1 works fine with GitBash but ignored when called via TortoiseGit. It stated as fixes in Aug 2014 but does not look so for me

See https://gitlab.com/tortoisegit/tortoisegit/issues/2143

I am using TortoiseGit 1.8.15.2 from Sep 2015

$acrobat = Get-Process Acrobat -ErrorAction SilentlyContinue
if ($acrobat) {
 "Acrobat is Running"
  exit 1
} else {
 "Not running"
  exit 0
}
Remove-Variable acrobat

I placed file named pre-commit in .git\hooks folder of my project. It has next content

#!/bin/sh
# An  hook script to verify that Acrobat is closed. 


powershell.exe -ExecutionPolicy RemoteSigned -File   '.\.\.\common\external\hooks\pre-commit.ps1'
ret=$?;
#echo "Acrobat check: $ret"
if [ $ret == 1 ]
then
    echo "Commit canceled: Acrobat is open"
    exit 1
fi
exit 0

The issues is: when i run pre-commit.ps1 directly it gives correct return 1 once Acrobat is open and 0 if not open. But in hook it always gives me 0.

Could you please give me a hint what is problem here? Or may be there is easier solution to return 1 in git hook without Powershell?

Lev Savranskiy
  • 422
  • 2
  • 7
  • 19

1 Answers1

2

There are a number of things wrong with that script.

First off you aren't actually assigning anything to the runString variable. Assignments cannot have spaces on either side of the =. Your assignment there should look like this:

runString="C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -Command -File 'C:/project/common/external/hooks/pre-commit.ps1'"

Assuming you had the assignment correct you aren't running it correctly. exec replaces the current shell script with the command on that line so your script will replace itself with the powershell command and not run the code after that line.

That all said, and assuming powershell.exe exits with the return status of your script, then your script can be much simpler than this.

Just this should work:

#!/bin/sh
# A hook script to verify that Acrobat is closed.
exec C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -File 'C:/project/common/external/hooks/pre-commit.ps1'
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • thanks for you notes. so only thing is to figure out - how to exit pre-commit.ps1 and expose proper message "Running" in TortioseGit window. Nothing happens now - no errors, return 1 looks skipped – Lev Savranskiy Oct 01 '15 at 17:54
  • 1
    Try `exit` in powershell instead of `return`? `return` in powershell uses the output stream not the exit status. – Etan Reisner Oct 01 '15 at 18:02
  • `$acrobat = Get-Process Acrobat -ErrorAction SilentlyContinue if ($acrobat) { echo "Acrobat is running"` ` exit 1` ` }else{` ` exit 0` ` }` ` Remove-Variable acrobat` – Lev Savranskiy Oct 01 '15 at 18:07
  • Still - exit 1 does nothing. If it worked - "Acrobat is running" would be exposed to user? right? – Lev Savranskiy Oct 01 '15 at 18:11
  • Formatting got screwed up there so I'm not sure what was going on with those `exit` bits, but yes, that looks roughly like what it should be and I would expect that to output something somewhere visible. Are you sure that script is executing? If you touch a file in the powershell script does it get created? `$acrobat = Get-Process Acrobat -ErrorAction SilentlyContinue if ($acrobat) { echo "Acrobat is running"; exit 1}else{exit 0}` – Etan Reisner Oct 01 '15 at 18:14
  • not sure what means _get created_ here is screenshot of PS file http://i6.imageban.ru/out/2015/10/01/0be348dc2f6dfba3b5fb164317362150.jpg – Lev Savranskiy Oct 01 '15 at 18:21
  • I mean if you use `set-content` or similar in that script to write to a file on your file system/disk does that file get created when the script runs? As a test to make sure that the powershell script is running correctly in the first place. – Etan Reisner Oct 01 '15 at 18:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91124/discussion-between-lev-and-etan-reisner). – Lev Savranskiy Oct 01 '15 at 18:26