1

I'm working on a mobile game (Android and iOS) using Unity 2019.1. I'm using git as my version control system and I would like to know how to save my current git commit hash into a C# script.

I would like to save my current git commit in order to display it on the screen (so I would know instantly at which version of the game I'm looking at). Right now instead of current git commit hash I'm using Application.buildGUID, however this isn't working for me, because builds based on the same commit can have different GUIDs.

How can I do that?

2
  • 1
    Don't know about C# but just run this shell from C# to get the hash: git rev-parse HEAD Commented Nov 18, 2019 at 22:37
  • You should maybe rather use something like public static class ApplicationInfo { public const string AppVersion = "1.0.0"; } and rather update that one once you have a new minor or major release.. Commented Nov 19, 2019 at 5:42

2 Answers 2

1

I would normally advise applications to be released with a major/minor version to avoid the need to do this. However if you are set on using the git hash, consider using Unity's preprocess build in conjunction with C#'s process.

  1. Use process to get the git hash via the command line, and read it from standard output using process.StandardOutput.ReadLine();

  2. Update a settings file, or other asset to include this git hash for reference during execution.

Git isn't meant to be run during runtime, and the git hash will not be included in the compiled build.

Sign up to request clarification or add additional context in comments.

Comments

0

You could use GitInfo or GitVersion that make it easy to access this information either in C# or during msbuild build.

Or you could try to do it yourself (it's probably more difficult). I did it with a powershell script called from a msbuild task and that update part of a c# file (that could be the AssemblyInfo.cs file):

  <Target Name="UpdateAssemblyInfoWithHash" BeforeTargets="BeforeBuild">
    <Exec Command="powershell -executionpolicy bypass -File &quot;$(SolutionDir)Solution Items\update_assembly_info.ps1&quot; -configuration $(ConfigurationName)" />
  </Target>
$hash=git rev-parse --short HEAD
Write-Output "Updating Assembly version with git short commit hash ($hash)..."

$content =  Get-Content $PSScriptRoot\VersionInfo.cs -Raw
$content -replace "Hash = `".*`"", "Hash = `"$hash`"" | Set-Content $PSScriptRoot\VersionInfo.cs | Out-Null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.