Git tool to automatically import the git hash at build

I’ve made this little helper script which automatically saves the git hash into a text asset before build and (optionally) warns you if you still have uncommitted changes. I know there are some solutions out there but I just wanted a very lightweight one. Most of the code is actually for settings.

It’s just one single file and free (MIT license) on GitHub (more info there) or just use the file which is attached to this thread:

I use it to display the hash at runtime and also remind myself to commit changes before builds.

Requirements

  • You need to have git installed (“git” command in PATH). To test simply open your commandline, type in “git” and see what happens.
  • Your project must use git (obviously).
  • Unity 2017+ (may work with earlier versions too, have not tested it there) up to 2020+

Usage
At build time it will try to fetch the current hash from git and save it in a text asset (default: “Assets/Resources/GitHash.asset”).

This is how the warning looks:

Update: Fix applied thanks to “EVC_Keys”.

More details and readme on GitHub.
Cheers!

6783542–804230–EditorGitTool.cs (10.3 KB)

2 Likes

I was just trying to solve this problem, and seems like you did so way nicer than I would have. Amazing!

Oh, wait, I can’t build it. Compiler doesn’t know what either of these 2 functions are:

EditorTools.ExecAndReadFirstLine(“git rev-parse --short HEAD”);
EditorTools.Exec(“git status --porcelain”);

1 Like

Oh hi, sry I missed your post. My bad, I forgot to add that class to the git repo.

It should be fixed now both on gitlab and here (I updated the file here in my first post). While I was fixing that I integrated the dependencies into the class directly to keep it just one .cs file.

Please let me know if it works for you now.