How to get git commit hash of python project using python code?

Issue

I have a git repository hosting a python project. It is code for long running simulations, that takes hours-days to complete. I manage the history with git, and run the simulations on a remote server, by pulling code.

I have multiple copies of the repository on the server, which maybe at different commits. When I execute a simulation from one such folder, I want to log the git commit at which I am running the simulations. I write the input parameters of my simulations when the code starts running to a json file. I want to include the git commit hash with this file, so that I can recreate simulations results exactly by matching the commit and input parameters.

Essentially, I want to access the git commit hash of HEAD from within python code so that I can write it to a file, and am looking for simple method with no overhead.

I have come across gitpython but they warn that: Leakage of System Resources: GitPython is not suited for long-running processes (like daemons) as it tends to leak system resources. I would prefer a method that using native python libraries.

Solution

If you are within a git repository : getting the hash of current commit is simply :

git rev-parse HEAD

If you want to run that from a Python script : you can use gitpython:

from git import Repo

repo = Repo("path/to/repo")
commit_hash = repo.git.rev_parse("HEAD")

or choose a way to execute it from python (note: I don’t have much experience with Python, I took this sample from this answer) :

from subprocess import Popen, PIPE

process = Popen(["git", "rev-parse", "HEAD"], stdout=PIPE)
(commit_hash, err) = process.communicate()
exit_code = process.wait()

# you can check err and exit_code for good measure

extra note : my guess is the warning about resource usage for gitpython would be if your long running process extensively uses git while it is running.

In your case you would use it to issue one single command at startup ; you won’t see gitpython spontaneously open file handles and consume memory after that.
IMHO the warning does not apply to your use case.

Answered By – LeGEC

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published