Created
July 1, 2021 19:04
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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