2016-04-06 35 views
0

我期待防止提交到我的本地Windows水銀回購,如果下面的字符串出現在任何未提交的文件:如何防止Windows上的Mercurial Commits包含字符串?

不承諾ME

我知道,預提交hook I needpretxncommit。如果我是在Linux上我只想做這樣的事情:

[hooks] 
pretxncommit.donotcommitme = hg export tip | (! grep -E -q -i 'do not commit me') 

(從this link採取但未驗證/測試)

至於egrep的,我有FINDSTR /I /C:"do not commit",替代它似乎工作精細。然而,我找不到任何東西來「否定」它的結果,就像在上面的Linux例子中一樣。

作爲純命令提示符命令的一種可能的替代方法,我也碰到了檢查大二進制文件的this PowerShell script。但是我不知道PowerShell,所以這整個事情對我來說看起來很亂。

有沒有人知道一個簡單的方法來做我沒有安裝Python,Cygwin或其他什麼?或者你知道如何修改上面的PowerShell腳本來進行字符串檢查而不是檢查文件大小嗎?

我們也在使用TortoiseHG,所以任何解決方案都可以使用它來代替純粹的Mercurial。

回答

0

發現PowerShell部分比我預期的要容易。我最終根據this gist編寫了自己的簡單腳本。

這是所有誰是位數的:

# This is like an "include" needed for the MessageBox 
Add-Type -AssemblyName System.Windows.Forms 

function Show-MessageBox() 
{ 
    $message = "Found a ""DO NOT COMMIT ME"" message in your code!`r`n`r`nIf you are doing a pull or rebase this is probably okay.`r`n`r`nCommit anyway?" 
    $title = "DO NOT COMMIT ME DETECTED!" 
    $buttons = [System.Windows.Forms.MessageBoxButtons]::YesNo 
    $icon = [System.Windows.Forms.MessageBoxIcon]::Warning 

    [System.Windows.Forms.MessageBox]::Show($message, $title, $buttons, $icon) 
} 

# Use `hg export` to search for the string "do not commit me" (case insensitive) 
write "Checking for DO NOT COMMIT ME comments in your code..." 
$whoops = (hg export tip | Select-String -pattern "do not commit me"); 

# If we have it in our changeset, abort the commit! 
if ($whoops) 
{ 
    $a = Show-MessageBox 

    if ($a -eq "Yes") 
    { 
     write "`r`n!!! COMMITING ANYWAY !!!`r`n" 
     exit 0 
    } 

    write "`r`n*** ABORTING COMMIT! ***`r`n" 
    exit 1 
} 

write "Okay to commit!" 
exit 0 

注:我也不得不打開與Set-ExecutionPolicy RemoteSigned運行PowerShell腳本(在運行PowerShell中以管理員身份)的能力。命令行-ExecutionPolicy Bypass標誌無法正確工作,並導致TortoiseHG出現各種問題!