@at By "a clean git state", I mean no files have changed since the last commit. In other words, "git status" says "nothing to commit, working tree clean".
The git package (which I believe has to be installed as GitPython) gives you functions you can use for this. My code below calls them.
import git
...
def get_git_hash(self):
"""
Returns the current git state hash, or crashes if not in a clean git state (i.e., there are uncommitted changes).
:return:
"""
repo = git.Repo(search_parent_directories=True)
if self.insist_on_clean_git_state and repo.is_dirty(untracked_files=True):
raise Exception('Not in a clean git state! Commit or revert.')
git_hash = repo.head.object.hexsha
return git_hash