Skip to content

Instantly share code, notes, and snippets.

@Aaron-P
Created May 7, 2017 21:33
Show Gist options
  • Save Aaron-P/8cc38c9648bbe40e7367be8cae89ca4e to your computer and use it in GitHub Desktop.
Save Aaron-P/8cc38c9648bbe40e7367be8cae89ca4e to your computer and use it in GitHub Desktop.
Insert git revision information into compiled project, output in elmah logs, and require changes to be commited for release builds.
# Ignore the auto-generated file or there will be a loop of uncommitted changes.
**/Properties/AssemblyInfoGit.cs
public static class AssemblyInfo
{
private static Assembly s_Assembly { get; set; }
public static Assembly Assembly
{
get
{
if (s_Assembly == null)
s_Assembly = Assembly.GetAssembly(typeof(AssemblyInfo));
return s_Assembly;
}
}
//Custom object?
private static string[] s_GitInfo { get; set; }
private static string[] GitInfo
{
get
{
if (s_GitInfo == null)
s_GitInfo = Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion.Split(new char[] { '.' }, 3);
if (s_GitInfo.Length != 3)
throw new FormatException("The Git information in AssemblyInformationalVersion is missing or invalid.");
return s_GitInfo;
}
}
public static string GitBranch
{
get
{
return GitInfo[0];
}
}
public static bool GitHasUncommitted
{
get
{
return bool.Parse(GitInfo[2]);
}
}
public static string Name
{
get
{
return Assembly.GetCustomAttribute<AssemblyProductAttribute>().Product;
}
}
public static string GitRevision
{
get
{
return GitInfo[1];
}
}
public static string GitRevisionId
{
get
{
return GitRevision.Substring(0, 8);
}
}
public static string Version
{
get
{
return Assembly.GetName().Version.ToString();
}
}
}
protected void ErrorLog_Filtering(object sender, Elmah.ExceptionFilterEventArgs e)
{
//This is super hacky but there's no official way to get custom info into Elmah logs.
var request = System.Web.HttpContext.Current.Request;
request.ServerVariables["GIT_BRANCH"] = AssemblyInfo.GitBranch;
request.ServerVariables["GIT_REVISION"] = AssemblyInfo.GitRevision;
request.ServerVariables["GIT_UNCOMMITTED"] = AssemblyInfo.GitHasUncommitted.ToString();
}
<!-- Using https://www.nuget.org/packages/MSBuildTasks -->
<Target Name="CheckUncommittedChanges" BeforeTargets="Version" Condition="$(Configuration)=='Release'">
<GitPendingChanges LocalPath="$(MSBuildProjectDirectory)">
<Output TaskParameter="HasPendingChanges" PropertyName="HasPendingChanges" />
</GitPendingChanges>
<Error Condition="'$(HasPendingChanges)'=='True'" Text="There are uncommitted changes to the repository. Building '$(Configuration)' requires all changes to be committed." />
</Target>
<Target Name="Version">
<GitPendingChanges LocalPath="$(MSBuildProjectDirectory)">
<Output TaskParameter="HasPendingChanges" PropertyName="HasPendingChanges" />
</GitPendingChanges>
<GitBranch>
<Output TaskParameter="Branch" PropertyName="Branch" />
</GitBranch>
<GitVersion LocalPath="$(MSBuildProjectDirectory)" Short="false">
<Output TaskParameter="CommitHash" PropertyName="Revision" />
</GitVersion>
<AssemblyInfo CodeLanguage="CS" OutputFile="Properties\AssemblyInfoGit.cs" AssemblyInformationalVersion="$(Branch).$(Revision).$(HasPendingChanges)" />
</Target>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment