【问题标题】:How to prevent Mercurial Commits containing string on Windows?如何防止在 Windows 上包含字符串的 Mercurial Commits?
【发布时间】:2016-04-06 20:28:15
【问题描述】:

如果以下字符串出现在任何未提交的文件中,我希望阻止提交到我的本地 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。

【问题讨论】:

    标签: mercurial tortoisehg mercurial-hook


    【解决方案1】:

    事实证明,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 出现各种问题!

    【讨论】:

      猜你喜欢
      • 2011-06-07
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      相关资源
      最近更新 更多