Created
March 22, 2021 19:21
-
-
Save munro/353734e83993e508ec25ddce3db8fdba to your computer and use it in GitHub Desktop.
Python get last pushed commit
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 subprocess import check_output | |
| def get_git_current_commit(): | |
| return _get_git_hashes("rev-parse", "HEAD")[0] | |
| def get_git_last_pushed_commit(): | |
| unpushed_commits = get_git_unpushed_commits() | |
| if not unpushed_commits: | |
| return get_git_current_commit() | |
| first_unpushed_commit = unpushed_commits[-1] | |
| return _get_git_hashes("rev-parse", f"{first_unpushed_commit}^")[0] | |
| def get_git_unpushed_commits(): | |
| return _get_git_hashes( | |
| "log", "--pretty=format:%H", "HEAD", "--not", "--remotes=origin" | |
| ) | |
| def _get_git_hashes(*args): | |
| return check_output(["git", *args]).decode("utf-8").split("\n") | |
| def test_git(): | |
| print(f"Current commit : {get_git_current_commit()}") | |
| print(f"Unpushed commits : {', '.join(get_git_unpushed_commits())}") | |
| print(f"Last pushed commit: {get_git_last_pushed_commit()}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment