PowerShell スクリプトを git フックとして実行することはできますか?
PowerShellプロンプトでgitを実行していますが、違いはありませんが、フックは拡張子なしで名前が付けられており、PowerShellには.ps1拡張子が必要(AFAIK)であるため、それらを機能させることができないようです。それが問題なのか、それとも別の問題なのかはわかりません。
PowerShell スクリプトを git フックとして実行することはできますか?
PowerShellプロンプトでgitを実行していますが、違いはありませんが、フックは拡張子なしで名前が付けられており、PowerShellには.ps1拡張子が必要(AFAIK)であるため、それらを機能させることができないようです。それが問題なのか、それとも別の問題なのかはわかりません。
フック フォルダーで pre-commit.sample の名前を pre-commit に変更します。次に、同じフォルダーに pre-commit.ps1 powershell スクリプト ファイルを作成します。
#!/bin/sh
c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -File '.git\hooks\pre-commit.ps1'
ここでの Git の設計による唯一のオプションは、PowerShell を呼び出す bash スクリプトです。残念ながら、Git は Linux 以外の互換性について何も考えていませんでした。
Kim Ki Won
上記の の答えは私にとってはうまくいきませんでしたが、賛成票があるので、一部の人にとってはうまくいくと思います。
私にとってうまくいったのは、bin/shをドロップし、-Fileを使用して実行する代わりに、コマンドを直接実行することでした:
c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -Command .\.git\hooks\pre-commit.ps1
PowerShell Git Hooks
Keith Hill の回答を読んで以来、私が使用している開始 PWSH スクリプトを次に示します。非常に素晴らしい。
#!/usr/bin/env pwsh
Process {
Write-Information -MessageData "I Ran" -InformationAction Continue
}
Begin {
Write-Information -MessageData "Beginning" -InformationAction Continue
}
End {
Write-Information -MessageData "Ending" -InformationAction Continue
Exit 0
}
また、すべてのリポジトリでフックの単一のコピーを共有していることにも言及する必要があります。私のリポジトリはすべて R:\Git にあり、R:\Git\Hooks を作成し、https://git-scm.com/docs/githooksをgit config core.hooksPath=R:\Git\Hooks
グローバルに使用しました。人生は素晴らしい。
For the sake of completeness:
If you only have Windows PowerShell and not PowerShell Core installed then Keith Hill's neat answer doesn't work. The various answers that use a bash script to run PowerShell, passing in the path to the PowerShell script to run, are straight-forward and the way I chose to go in the end. However, I discovered there is another way:
Create two files for the git hook, say pre-commit and pre-commit.ps1. The pre-commit.ps1 file is the file that PowerShell will run. The other pre-commit file (without a file extension) is empty apart from a PowerShell interpreter directive on the first line:
#!/usr/bin/env powershell
Git will run the pre-commit file, parse the PowerShell interpreter directive and run up PowerShell, passing in the path to the pre-commit file. PowerShell will assume the file passed in should have a ".ps1" extension. It will search for pre-commit.ps1 and, since you created a file with that name and extension, PowerShell will find it and run it.
This approach is nice and simple but, in the end, I decided against it because it seemed a little "magical" and might have maintainers scratching their heads about how it works.
私はこれを自分で探していましたが、次のことがわかりました。
Git Powershell プリコミット フック( Source )
## Editor's note: Link is dead as of 2014-5-2. If you have a copy, please add it.
PowerShell での git pre-commit の PHP 構文チェック( Soure )
##############################################################################
#
# PHP Syntax Check for Git pre-commit hook for Windows PowerShell
#
# Author: Vojtech Kusy <wojtha@gmail.com>
#
###############################################################################
### INSTRUCTIONS ###
# Place the code to file "pre-commit" (no extension) and add it to the one of
# the following locations:
# 1) Repository hooks folder - C:\Path\To\Repository\.git\hooks
# 2) User profile template - C:\Users\<USER>\.git\templates\hooks
# 3) Global shared templates - C:\Program Files (x86)\Git\share\git-core\templates\hooks
#
# The hooks from user profile or from shared templates are copied from there
# each time you create or clone new repository.
### SETTINGS ###
# Path to the php.exe
$php_exe = "C:\Program Files (x86)\Zend\ZendServer\bin\php.exe";
# Extensions of the PHP files
$php_ext = "php|engine|theme|install|inc|module|test"
# Flag, if set to 1 git will unstage all files with errors, se to 0 to disable
$unstage_on_error = 0;
### FUNCTIONS ###
function php_syntax_check {
param([string]$php_bin, [string]$extensions, [int]$reset)
$err_counter = 0;
write-host "Pre-commit PHP syntax check:" -foregroundcolor "white"
git diff-index --name-only --cached HEAD -- | foreach {
if ($_ -match ".*\.($extensions)$") {
$file = $matches[0];
$errors = & $php_bin -l $file
if ($errors -match "No syntax errors detected in $file") {
write-host $file ": OK" -foregroundcolor "green"
}
else {
write-host $file ":" $errors -foregroundcolor "red"
if ($reset) {
git reset -q HEAD $file
write-host "Unstaging" $file "..." -foregroundcolor "magenta"
}
$err_counter++
}
}
}
if ($err_counter -gt 0) {
exit 1
}
}
### MAIN ###
php_syntax_check $php_exe $php_ext $unstage_on_error
コードは pre-commit フック用ですが、ほとんど何でもできるように変更できます。あなたがする必要があることを助けるはずです!