Skip to content

Commit

Permalink
Added support for IsDirty flag
Browse files Browse the repository at this point in the history
Fixes #10 and allows conditionals in code that depend
on whether the build is clean or dirty from Git's point
of view.
  • Loading branch information
kzu committed Dec 26, 2016
1 parent c69b7b0 commit 1a70ef8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/GitInfo/build/GitInfo.cs.pp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma warning disable 0436

#if ADDMETADATA
[assembly: System.Reflection.AssemblyMetadata("GitInfo.IsDirty", RootNamespace.ThisAssembly.Git.IsDirty)]
[assembly: System.Reflection.AssemblyMetadata("GitInfo.Branch", RootNamespace.ThisAssembly.Git.Branch)]
[assembly: System.Reflection.AssemblyMetadata("GitInfo.Commit", RootNamespace.ThisAssembly.Git.Commit)]
[assembly: System.Reflection.AssemblyMetadata("GitInfo.Sha", RootNamespace.ThisAssembly.Git.Sha)]
Expand Down Expand Up @@ -30,6 +31,9 @@
/// <summary>Provides access to the git information for the current assembly.</summary>
public partial class Git
{
/// <summary>IsDirty: $GitIsDirty$</summary>
public const bool IsDirty = $GitIsDirty$;

/// <summary>Branch: $GitBranch$</summary>
public const string Branch = "$GitBranch$";

Expand Down
16 changes: 16 additions & 0 deletions src/GitInfo/build/GitInfo.targets
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@
<Message Text="Determined Git dir as '$(GitDir)'" Importance="$(GitInfoReportImportance)" Condition="'$(GitRoot)' != ''" />
<Warning Text="Directory $(GitInfoBaseDir) is not in a Git repository. Cannot determine Git repository root." Condition="'$(GitRoot)' == ''" />

<Exec Command='$(GitExe) diff-index --quiet HEAD'
EchoOff="true"
Condition="'$(GitRoot)' != ''"
StandardErrorImportance="low"
StandardOutputImportance="low"
ConsoleToMSBuild="true"
WorkingDirectory="$(GitRoot)"
IgnoreExitCode="true">
<Output TaskParameter="ExitCode" PropertyName="GitIsDirty" />
</Exec>
</Target>

<Target Name="_GitInputs" DependsOnTargets="_GitRoot" Returns="@(_GitInput)">
Expand Down Expand Up @@ -682,6 +692,12 @@
<_ThisAssemblyContent Condition="'$(ThisAssemblyNamespace)' == ''">$(_ThisAssemblyContent.Replace('RootNamespace.', ''))</_ThisAssemblyContent>

<_ThisAssemblyContent>$(_ThisAssemblyContent.Replace('_RootNamespace_', '$(ThisAssemblyNamespace)'))</_ThisAssemblyContent>

<_ThisAssemblyContent Condition="'$(Language)' == 'C#' And '$(GitIsDirty)' == '1'">$(_ThisAssemblyContent.Replace('$GitIsDirty$', 'true'))</_ThisAssemblyContent>
<_ThisAssemblyContent Condition="'$(Language)' == 'C#' And '$(GitIsDirty)' == '0'">$(_ThisAssemblyContent.Replace('$GitIsDirty$', 'false'))</_ThisAssemblyContent>
<_ThisAssemblyContent Condition="'$(Language)' == 'VB' And '$(GitIsDirty)' == '1'">$(_ThisAssemblyContent.Replace('$GitIsDirty$', 'True'))</_ThisAssemblyContent>
<_ThisAssemblyContent Condition="'$(Language)' == 'VB' And '$(GitIsDirty)' == '0'">$(_ThisAssemblyContent.Replace('$GitIsDirty$', 'False'))</_ThisAssemblyContent>

<_ThisAssemblyContent>$(_ThisAssemblyContent.Replace('$GitBranch$', '$(GitBranch)'))</_ThisAssemblyContent>
<_ThisAssemblyContent>$(_ThisAssemblyContent.Replace('$GitCommits$', '$(GitCommits)'))</_ThisAssemblyContent>
<_ThisAssemblyContent>$(_ThisAssemblyContent.Replace('$GitCommit$', '$(GitCommit)'))</_ThisAssemblyContent>
Expand Down
4 changes: 4 additions & 0 deletions src/GitInfo/build/GitInfo.vb.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#Const $MetadataDefine$ = 1

#If ADDMETADATA
<Assembly: System.Reflection.AssemblyMetadata("GitInfo.IsDirty", Global.RootNamespace.ThisAssembly.Git.IsDirty)>
<Assembly: System.Reflection.AssemblyMetadata("GitInfo.Branch", Global.RootNamespace.ThisAssembly.Git.Branch)>
<Assembly: System.Reflection.AssemblyMetadata("GitInfo.Commit", Global.RootNamespace.ThisAssembly.Git.Commit)>
<Assembly: System.Reflection.AssemblyMetadata("GitInfo.Sha", Global.RootNamespace.ThisAssembly.Git.Sha)>
Expand All @@ -28,6 +29,9 @@
Partial Class ThisAssembly
''' <summary>Provides access to the git information for the current assembly.</summary>
Partial Public Class Git
''' <summary>IsDirty: $GitIsDirty$</summary>
Public Const IsDirty = $GitIsDirty$
''' <summary>Branch: $GitBranch$</summary>
Public Const Branch = "$GitBranch$"

Expand Down
3 changes: 3 additions & 0 deletions src/GitInfo/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ target that depends on the GitInfo target:
$(GitSemVerLabel)
$(GitSemVerDashLabel)
$(GitSemVerSource)
$(GitIsDirty)

From C# and VB, by default code is generated too so that the same
information can be accessed from code, to construct your own
Expand Down Expand Up @@ -56,6 +57,8 @@ The available constants from code are:
ThisAssembly.Git.SemVer.Label
ThisAssembly.Git.SemVer.DashLabel
ThisAssembly.Git.SemVer.Source
ThisAssembly.Git.IsDirty


Available MSBuild customizations:

Expand Down

8 comments on commit 1a70ef8

@asherber
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would also be useful to have something like ThisAssembly.Git.DotDirty, on the analogy of DashLabel, that gives .Dirty or null, depending on IsDirty. This would let use use this label in constructing assembly attributes :

... + Git.SemVer.DashLabel + "+" + Git.Commit + Git.DotDirty
// 1.2.3-beta+deadbeef.Dirty

@kzu
Copy link
Member Author

@kzu kzu commented on 1a70ef8 Dec 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can trivially provide that behavior by just doing it from msbuild:

<GitSemVerDashLabel Condition="$(GitIsDirty) == '1'">$(GitSemVerDashLabel).Dirty</GitSemVerDashLabel>

@asherber
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I'm trying to find the solution that gets me closest to what I want with the least configuration. I like being able to just drop in something standard like your GlobalAssemblyInfo.cs, but I'm not crazy about editing the csproj for each project I add this package to.

Would you be open to a pull request for this?

@kzu
Copy link
Member Author

@kzu kzu commented on 1a70ef8 Dec 27, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asherber
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks, good idea. It's true that this label may not be something that is needed, but it does help to flag a build that was made without first committing the changes. (Thinking here mostly of local dev builds.)

@kzu
Copy link
Member Author

@kzu kzu commented on 1a70ef8 Dec 27, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asherber
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't disagree – but I don't always get to make those decisions!

@kzu
Copy link
Member Author

@kzu kzu commented on 1a70ef8 Dec 27, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.