【问题标题】:Verify that all Git commits of a C# project compile after rewriting history验证 C# 项目的所有 Git 提交是否在重写历史记录后编译
【发布时间】:2016-09-16 07:49:09
【问题描述】:

是否有任何自动化方法来验证 Git 存储库中的所有提交是否处于可编译状态?

我需要这个来验证我在重写历史后没有破坏任何东西。由于我正在重写历史记录,这不包括构建服务器 - 在提交时工作的提交可能在重写历史记录后被破坏。

例如,我有一个 Visual Studio 2015 C# 项目,我想像一些脚本:

git filter-branch --tree-filter msbuild

我希望它在每次提交时运行构建,并在构建过程返回非零时停止并显示错误消息。

【问题讨论】:

标签: c# git


【解决方案1】:

考虑到树过滤器将从 Git bash 执行命令,您可能想要使用

git filter-branch --tree-filter "MSBuild.exe" 

(确保您的 %PATH% 包含 c:\Program Files (x86)\MSBuild\14.0\Bin
(或use forward slash as in here

这对于提到的other option in the comments 同样有效

git rebase -i --exec MSBuild.exe <first sha you want to test>~

您可以使用CMD session similar to this gist,来自Tim Abell

@echo off
REM  batch script for loading git-bash and the vs tools in the same window
REM  inspiration: http://www.drrandom.org/post/2011/11/16/Grappling-with-multiple-remotes-in-git-tfs.aspx
REM  screenshot: https://twitter.com/#!/tim_abell/status/199474387731226624/photo/1
%HOMEDRIVE%
cd %HOMEPATH%
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
echo Use full exe names when running under bash, e.g. "msbuild.exe"
echo Loading bash, you may now use git and msbuild in the same console \o/.
"C:\Program Files (x86)\Git\bin\sh.exe" --login -i

【讨论】:

    【解决方案2】:

    假设您要检查 repo 中的所有历史提交,而不是未来的提交:

    您可以使用git bisect 来做到这一点:

    git bisect start
    git bisect bad # Mark the current HEAD as bad
    git bisect good <the first commit>
    

    然后您需要一个运行 msbuild 的脚本,返回 1 表示构建错误,返回 125 表示成功构建。这是因为我们无法将任何构建标记为“好”,因为我们不知道在此之前的提交是否也有效,因此我们跳过那些有效的提交。

    然后,使用run 命令开始平分:

    git bisect run myscript
    

    然后它将开始运行构建(以非连续顺序),直到找到损坏的构建并停止。更多解释见https://git-scm.com/docs/git-bisect#_bisect_run

    【讨论】:

    • 我想在重写历史记录后在每次提交时运行它。根据我对 Git bisect 的了解,它将执行 binsearch 而不是遍历每个提交。更多的是为了捕捉一个特定的错误,我不确定它与我的问题有什么关系。无论如何,我已经编辑了我的问题以添加关于重写历史的部分。
    • @sashoalm 我明白了,我忽略了一些问题。我稍微修改了答案 - 它不按时间顺序执行构建,但除此之外,似乎符合您的要求。
    【解决方案3】:

    怎么样(在Git BashGit Posh 上测试):

    1. 为 msbuild 命令创建 git 别名:

      git config --global --add alias.msbuild "!c:\path\to\msbuild.exe"

    如果PATH env 中存在“msbuild.exe”路径。 var,您可以只写“!msbuild”而不是完整路径。

    1. 然后运行命令:

      git filter-branch --tree-filter "git msbuild"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-06
      • 2020-05-30
      • 1970-01-01
      • 2011-11-08
      • 2014-04-05
      • 2011-05-09
      • 1970-01-01
      • 2012-04-06
      相关资源
      最近更新 更多