Skip to content

Instantly share code, notes, and snippets.

@b-luu
Created June 19, 2017 02:28
Show Gist options
  • Save b-luu/3941e80bca4bc1beb42458e434adbb3a to your computer and use it in GitHub Desktop.
Save b-luu/3941e80bca4bc1beb42458e434adbb3a to your computer and use it in GitHub Desktop.
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