7

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.

sashoalm
  • 75,001
  • 122
  • 434
  • 781

3 Answers3

5

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
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

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 run command:

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.

1615903
  • 32,635
  • 12
  • 70
  • 99
  • I want to run it on every single commit after I have rewritten history. From what I know of Git bisect, it will perform a binsearch instead of traversing every single commit. It's more for catching a particular bug and I'm not sure how it's relevant to my question. Anyway I've edited my question to add the part about rewriting history. – sashoalm Sep 16 '16 at 08:28
  • @sashoalm I see, I overlooked some bits of the question. I have modified the answer slightly - it does not execute the builds in chronological order, but other than that, seems to fit your requirements. – 1615903 Sep 16 '16 at 08:43
2

What about (tested on Git Bash and Git Posh):

  1. Creating git alias for msbuild command:

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

If "msbuild.exe" path is present in PATH env. var, you can just write "!msbuild" instead of a full path.

  1. Then run the command:

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

Community
  • 1
  • 1
Gabrielius
  • 1,045
  • 12
  • 18