Read and save current git commit hash into script
Hi.
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?
This is not a Unity question. You could try asking the question on Stack Overflow ins$$anonymous$$d.
Answer by Bunny83 · Nov 19, 2019 at 04:15 AM
Like sacredgeometry said this is not really a Unity question. This all just boils down to how to get the hash from git in the first place. I quickly made this little tool class for windows which does exactly that:
public static class GitTools
{
public static string gitExecutable = "C:\\Program Files\\Git\\bin\\git.exe";
public static int maxWaitTime = 1000;
public static string GetRepositoryHash(string aRepositoryPath)
{
var prc = new System.Diagnostics.Process();
prc.StartInfo.FileName = gitExecutable;
prc.StartInfo.Arguments = "-C \"" + aRepositoryPath + "\" show-ref --hash";
prc.StartInfo.RedirectStandardOutput = true;
prc.StartInfo.UseShellExecute = false;
prc.Start();
prc.WaitForExit(maxWaitTime);
string data = prc.StandardOutput.ReadToEnd();
int i = data.IndexOf("\n");
if (i > 0)
return data.Substring(0, i);
return data;
}
}
The usage should be straight forward. Keep in mind to check if the "gitExecutable" path is correct for your system. This simply executes the git commandline tool with the command show-ref which will print the current hash of the latest commit.