Skip to content

Instantly share code, notes, and snippets.

@thoemmi
Created May 11, 2011 09:07
Show Gist options
  • Save thoemmi/966148 to your computer and use it in GitHub Desktop.
Save thoemmi/966148 to your computer and use it in GitHub Desktop.
Determine number of git revisions with inline MSBuild task
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="GitVersion" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<LocalPath ParameterType="System.String" />
<Path ParameterType="System.String" />
<CommitCount ParameterType="System.Int32" Output="true" />
</ParameterGroup>
<Task>
<!--<Reference Include="" />-->
<Using Namespace="System"/>
<Using Namespace="System.Diagnostics"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
LocalPath = LocalPath ?? ".";
Path = Path ?? "master";
Log.LogMessage(MessageImportance.Low, "LocalPath is {0}", LocalPath);
Log.LogMessage(MessageImportance.Low, "Path is {0}", Path);
var psi = new ProcessStartInfo("cmd", "/c git rev-list " + Path + " --count") {
UseShellExecute = false,
ErrorDialog = false,
CreateNoWindow = false,
WorkingDirectory = LocalPath,
RedirectStandardOutput = true,
RedirectStandardError = true
};
string result;
using (var p = Process.Start(psi)) {
p.WaitForExit();
if (p.ExitCode != 0) {
using (var standardError = p.StandardError) {
Log.LogError(standardError.ReadToEnd());
}
return false;
}
using (var standardOutput = p.StandardOutput) {
CommitCount = Int32.Parse(standardOutput.ReadToEnd());
}
}
]]>
</Code>
</Task>
</UsingTask>
<Target Name="demo">
<GitVersion Path="master">
<Output PropertyName="GitCommitCount" TaskParameter="CommitCount" />
</GitVersion>
<Message Text="Commit count: $(GitCommitCount)"/>
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment