Verify that all Git commits of a C# project compile after rewriting history
Is there any automated way to verify that all commits in a Git repo are in a compilable state?
I need this to verify I haven’t broken anything after rewriting history. Since I’m rewriting history, this excludes a build server – a commit that worked at the time it was committed might be broken after rewriting history.
For example I have a Visual Studio 2015 C# project, and I imagine some script like:
git filter-branch --tree-filter msbuild
I want it to run a build on each commit, and stop with an error message if the build process returns nonzero.
3 Solutions collect form web for “Verify that all Git commits of a C# project compile after rewriting history”
Considering the tree-filter will execute the command from a Git bash, you might want to use
git filter-branch --tree-filter "MSBuild.exe"
(making sure your
%PATH% does include
c:\Program Files (x86)\MSBuild\14.0\Bin)
(or use forward slash as in here)
That would be equally valid for the other option mentioned in the comments
git rebase -i --exec MSBuild.exe <first sha you want to test>~
You can use a CMD session similar to this gist, from 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
Assuming you want to check all the historical commits in the repo, not the future commits:
You can use
git bisect to do that:
git bisect start git bisect bad # Mark the current HEAD as bad git bisect good <the first commit>
You then need a script that runs msbuild, returns 1 for build errors, and 125 for successful build. This is because we can’t mark any build as “good”, since we don’t know if the commits before this one also work, so instead we skip those that do work.
Then, start bisecting with the
git bisect run myscript
This will then start running builds (in a non-consecutive order) until it finds a broken build, and stops. See https://git-scm.com/docs/git-bisect#_bisect_run for more explanation.
What about (tested on
Git Bash and
Creating git alias for msbuild command:
git config --global --add alias.msbuild "!c:\path\to\msbuild.exe"
If “msbuild.exe” path is present in
PATHenv. var, you can just write “!msbuild” instead of a full path.
Then run the command:
git filter-branch --tree-filter "git msbuild"