如何防止在Windows上包含字符串的Mercurial Commits?

时间:2016-04-06 20:28:16

标签: mercurial tortoisehg mercurial-hook

如果任何未提交的文件中存在以下字符串,我希望阻止提交到我的本地Windows Mercurial存储库:

  

不要委托我

我知道提前提交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。

1 个答案:

答案 0 :(得分: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出现各种问题!

相关问题