#author("2019-10-06T11:55:10+09:00","default:ryuichi","ryuichi")
#author("2019-10-06T12:00:49+09:00","default:ryuichi","ryuichi")
* Windowsでpre-commitを実行する [#a0367d26]
** Bash(Git Bash)スクリプトとして書く [#od71fd35]
''.git\hooks\pre-commit''
#!/bin/sh
echo OK # (1)
php.exe -r 'print "PHP!";' # (2)
- (1) pre-commitはBash(Git Bash)スクリプトとして実行されるので、シェバングに#!/bin/shを指定すれば普通のシェルスクリプトとして'''echo OK'''が実行される
- (2) また、Git BashからWindowsのexeファイルを呼び出せるので、'''php.exe'''を記述すればPHPのワンライナーとして'''print "PHP!;'''が実行される
- なお、php.exeをフルパスで指定する場合は、'''C:/PHP/bin/php.exe'''または'''C:\\PHP\\bin\\php.exe'''または'''/c/PHP/bin/php.exe'''とする
** Perlスクリプトとして書く [#uf3fa634]
''.git\hooks\pre-commit''
#!/usr/bin/perl
print "Perl!";
- GitおよびGit for Windowsには必ずPerlがインストールされているので、Bashで記述が難しい複雑なスクリプトはPerlで書くことができ、WindowsのGitでも動く
** Windows側のPHPスクリプトとして書く [#id2c9034]
''.git\hooks\pre-commit''
#!/usr/bin/env php
<?php print "PHP!\n";
- Git Bash側にPHPが入ってる場合は、'''#!/usr/bin/env php.exe'''とすればWindows側のPHPを指定できる
** PowerShellスクリプトを呼び出す [#w705068e]
*** ワンライナー [#v5e0a82b]
''.git\hooks\pre-commit''
#!/bin/sh
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "echo PowerShell!"
*** ps1ファイル [#j2c41a64]
''.git\hooks\pre-commit''
#!/bin/sh
./test.ps1
''test.ps1''
echo "PowerShell!"
- pre-commitの中でPowerShellスクリプトを呼び出す(この場合test.ps1)
- なお、この場合はtest.ps1はプロジェクトフォルダー直下に置く
** Windowsとそれ以外のOSで実行する処理を分岐する [#x97c40a0]
''.git\hooks\pre-commit''
#!/bin/sh
if [ "$OS" = "Windows_NT" ]; then
./test.ps1
else
./test.sh
fi
- Windowsの場合はtest.ps1を、それ以外のOSの場合はtest.shを呼び出す