运行PowerShell脚本作为git挂钩

问题描述:

是否有可能以git挂钩运行PowerShell脚本?运行PowerShell脚本作为git挂钩

我在PowerShell提示符下运行git,这应该没有什么区别,但我似乎无法让它们工作,因为钩子命名时没有扩展名,PowerShell需要(AFAIK).ps1延期。我不确定这是否是问题或其他问题。

感谢, 埃里克

+0

是不是有可能使脚本调用PowerShell脚本(或任何其他脚本对于这个问题,无论其扩展名)? – holygeek 2011-04-12 01:32:59

+1

你可以提供一些关于git钩子的更多信息。 – JPBlanc 2011-04-12 04:45:03

+0

@JPBlanc:[The'githooks' manpage。](http://www.kernel.org/pub/software/scm/git/docs/githooks.html)我不知道是否为Windows提供了不同的文档(诸)版本。 – intuited 2011-04-12 06:09:41

从我所收集的唯一选择,由于Git的设计在这里将是一个bash脚本调用的PowerShell。不幸的是,Git没有考虑非Linux的兼容性。

+0

这似乎是答案。这是否可惜 - 我们并不都是bash爱好者,而windows上的bash始终是第二位。谢谢。 – 2011-04-13 04:42:53

+0

如果git支持任意平台的脚本,那么这些钩子的配置文件在bash脚本引导程序中看起来有多不同? – brianary 2013-04-09 16:20:14

我一直在寻找这个我自己,我发现:

Git的Powershell的pre-commit钩子Source

## Editor's note: Link is dead as of 2014-5-2. If you have a copy, please add it. 

PHP语法检查git的预先承诺的PowerShell的Soure

############################################################################## 
# 
# PHP Syntax Check for Git pre-commit hook for Windows PowerShell 
# 
# Author: Vojtech Kusy <[email protected]> 
# 
############################################################################### 

### 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 

代码是用于预先提交的钩子,但您可以修改它以执行任何操作。应该帮助你需要做的事情!

+0

第一个不适合我。 PowerShell脚本的相对路径无法正确解析。 – 2013-05-27 19:30:37

将pre-commit.sample重命名为预先提交到hooks文件夹中。 然后在相同的文件夹中创建pre-commit.ps1 powershell脚本文件。

#!/bin/sh 
c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -Command -File '.git\hooks\pre-commit.ps1' 
+2

我觉得这个命令行中的语法在-Command之后是错误的,它期望一个内联的powershell命令,但是你也指定了一个文件。它会抛出一个关于-File未被识别为cmdlet,函数或脚本文件的错误的错误。 – leinad13 2016-09-30 11:33:16

+0

请参阅[我的答案](http://stackoverflow.com/a/39796096/1225497)如果您遇到类似“#!无法识别...”或“文件无法识别...”的错误, 。 – Taran 2016-09-30 16:32:57

Kim Ki Won的回答上面并没有为我工作,但它具有upvotes,所以我会假设它为一些人。

什么工作对我来说是下降的bin/sh和,而不是使用 - 文件执行,直接执行命令:

c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy RemoteSigned -Command .\.git\hooks\pre-commit.ps1 
+1

这也适用于我,谢谢! – David 2016-10-11 20:46:52

这是在Windows的git我的钩位于\ git的\挂钩。

更新后的

#!/bin/sh 
c:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -ExecutionPolicy Bypass -Command '.\post-update.ps1' 

位于项目的根文件夹(您最初运行git的init)PowerShell脚本。 Powershell进入另一个存储库并调用pull,更新该存储库。

后update.ps1

Set-Location "E:\Websites\my_site_test" 
$env:GIT_DIR = 'E:\Websites\my_site_test\.git'; 
$env:GIT_EXEC_PATH= 'C:\Program Files (x86)\Git/libexec/git-core'; 
git pull