Skip to content

Instantly share code, notes, and snippets.

@NaelsonDouglas
Created July 1, 2021 19:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NaelsonDouglas/9bc3bfa26deec7827cb87816cad88d59 to your computer and use it in GitHub Desktop.
Save NaelsonDouglas/9bc3bfa26deec7827cb87816cad88d59 to your computer and use it in GitHub Desktop.
Function to programmatically get the current commit of a git repository via Python.
from pathlib import Path
def get_commit(repo_path):
git_folder = Path(repo_path,'.git')
head_name = Path(git_folder, 'HEAD').read_text().split('\n')[0].split(' ')[-1]
head_ref = Path(git_folder,head_name)
commit = head_ref.read_text().replace('\n','')
return commit
if __name__ == '__main__':
cloned_repo_path = '/home/ndc/repos/GithubPythonCodeSmellCrawler/src/dumps/pylama'
r = get_commit(cloned_repo_path)
print(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment