Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get Sha of Git? #334

Closed
beachwalker opened this issue Apr 20, 2020 · 12 comments
Closed

How to get Sha of Git? #334

beachwalker opened this issue Apr 20, 2020 · 12 comments
Labels
question Further information is requested

Comments

@beachwalker
Copy link

It is nice in GitVersion that the Hash of the git commit (version that will be created) can be attached. I prefer the more lightweight way of minver but I need that feature and don't know how to access that using minver.

@beachwalker beachwalker added the question Further information is requested label Apr 20, 2020
@adamralph
Copy link
Owner

Hi @beachwalker, what do you want to do with the hash?

@beachwalker
Copy link
Author

Put as info into version. More easy to follow then. Maybe to be used in assemblyinformationalversion for last value.

@adamralph
Copy link
Owner

@beachwalker I do this for my packages using Source Link.

For example:

image

By default, Source Link puts the hash into AssemblyInformationalVersion:

image

Would that be an option for you?

@acblksun
Copy link

@adamralph thanks I had the same question for ages. I added SourceLink as you suggested and it worked perfectly. This might be one for the faq.

I think what @beachwalker was wondering is because the information is already available to minver, would it be possible to add it without having to also include SourceLink?

e.g. I'd also prefer 1.0.1-alpha.0.9+fd1bc45 instead of just 1.0.1-alpha.0.9. But as you indicate, Minver+SourceLink coexist and produced 1.0.1-alpha.0.9+fd1bc45146996ddf9fa67f3f91bf5e95a4083531

 MinVer: Starting at commit fd1bc45 (height 0)...

...

  MinVer: [output] MinVerVersion=1.0.1-alpha.0.9
  MinVer: [output] MinVerMajor=1
  MinVer: [output] MinVerMinor=0
  MinVer: [output] MinVerPatch=1
  MinVer: [output] MinVerPreRelease=alpha.0.9
  MinVer: [output] MinVerBuildMetadata=
  MinVer: [output] AssemblyVersion=1.0.0.0
  MinVer: [output] FileVersion=1.0.1.0
  MinVer: [output] PackageVersion=1.0.1-alpha.0.9
  MinVer: [output] Version=1.0.1-alpha.0.9

Thanks Again!

@adamralph
Copy link
Owner

adamralph commented Apr 24, 2020

@acblksun I guess SourceLink does a lot more than give you the commit hash, since it augments your debug info, etc. so I agree that it may not be desirable to use it just to get the commit hash.

Another option for getting the commit hash is GitInfo, which is explicitly designed to expose Git information. It sets a property named GitSha which you can use to augment things like AssemblyInformationalVersion like so:

<Target Name="MyTarget" AfterTargets="MinVer">
  <PropertyGroup>
    <AssemblyInformationalVersion Condition="'$(GitSha)' != ''">$(MinVerVersion)+$(GitSha)</AssemblyInformationalVersion>
  </PropertyGroup>
</Target>

Although it would be trivial to expose the commit hash in a new MinVer property, the reasons I am reluctant to do that are:

  • It's orthogonal to the purpose of MinVer, which is tag based versioning.
  • Essentially, both the MinVer and minver-cli packages output just a single value: MinVerVersion. MinVer sets an MSBuild property and minver-cli writes the value to stdout. The other output properties from MinVer are just for convenience. It's not difficult to extract them from MinVerVersion. If we added something like MinVerCommitHash to MinVer, there would be no way of outputting that from minver-cli without resorting to some kind of serialisation format (XML, JSON, YAML, etc.) rather than a simple string as it currently is. Either of these two approaches (loss of parity or switching to a serialisation format) are a direct consequence of the first point, that exposing the commit hash is orthogonal to the purpose of both packages.

@adamralph
Copy link
Owner

Assuming either SourceLink or GitVersion solve this requirement, I'm closing this. If there are any further questions, please feel free to chime in again or raise a new issue.

@summer600
Copy link

summer600 commented Jul 3, 2020

<Target Name="MyTarget" AfterTargets="MinVer">
  <PropertyGroup>
    <AssemblyInformationalVersion Condition="'$(GitSha)' != ''">$(MinVerVersion)+$(GitSha)</AssemblyInformationalVersion>
  </PropertyGroup>
</Target>

Where do I put this. I tried SourceLink but don't like the log sha. GitInfo does have the Git.Commit (short sha) but your above lines, where do they go. Not in the csproj,

@adamralph
Copy link
Owner

@summer600 you should put those lines in the csproj which references MinVer.

@summer600
Copy link

Yep did that bottom of my csproj... But the InformationFileVersion still only shows upto height

  
  <ItemGroup>
   
    <PackageReference Include="GitInfo" Version="2.0.26">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
   
    <PackageReference Include="MinVer" Version="2.3.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

  <Target Name="AddGitSha" AfterTargets="MinVer">
    <PropertyGroup>
      <AssemblyInformationalVersion Condition="'$(GitSha)' != ''">$(MinVerVersion)+$(GitSha)</AssemblyInformationalVersion>
    </PropertyGroup>
  </Target>

</Project>

but my console app displaying the informationalverersion field shows: Debug build with full version: 1.0.3-alpha.0.1

@summer600
Copy link

dnspy says:

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETCoreApp,Version=v5.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("C0R3 Enerprise Objects b.v.")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyCopyright("2020")]
[assembly: AssemblyDescription("Product Description")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyInformationalVersion("1.0.3-alpha.0.1")]
[assembly: AssemblyProduct("MinVerTestingWithGit")]
[assembly: AssemblyTitle("MinVerTestingWithGit")]

@adamralph
Copy link
Owner

@summer600 I guess GitSha isn't populated for some reason, or perhaps that's the wrong property name. It's probably best if you consult the GitInfo docs to determine the correct way to get the commit hash. I'm not very familiar with GitInfo as I don't use it.

@ElanHasson
Copy link

ElanHasson commented Aug 12, 2022

See this https://gist.github.com/ElanHasson/30384431651534b215e754a554b5b6ae

Which accomplishes this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants