1

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.

1

1 Answer 1

4

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.

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

2 Comments

Thanks for explaining both methods. I guess the only way to use it with native python library is to use subprocess, like some answers mentioned in this question.
And thanks for the note regarding gitpython warning. I was unsure how to interpret the warning, which is what made me put up this question in the first place. It probably does not apply to me. But anyways I will avoid adding the dependency and just use subprocess. Thanks.

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.