Created
June 19, 2017 02:28
-
-
Save b-luu/3941e80bca4bc1beb42458e434adbb3a to your computer and use it in GitHub Desktop.
This file contains 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
import os | |
def find_repo_root(start_dir='.'): | |
start_dir = os.path.abspath(start_dir) | |
if '.git' not in os.listdir(start_dir): | |
assert start_dir != '/', "No Git repo found in given folder or its parents." | |
return find_repo_root(os.path.join(start_dir, '..')) | |
return start_dir | |
def get_branch_name(start_dir='.'): | |
root = find_repo_root(start_dir) | |
with open(os.path.join(root, '.git/HEAD'), 'r') as f: | |
branch = f.readline().replace('ref: refs/heads/', '').split('\n', 1)[0] | |
return branch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment