Created
December 2, 2017 05:04
-
-
Save ruxi/1baa9fd08c962b7bd4004fb59be2b8fa 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
"""gitpath v2, modified | |
Copyright (c) 2017 github.com/ruxi | |
Copyright (c) 2015 Maximilian Nöthe | |
Licensed under the MIT | |
""" | |
import subprocess | |
import os.path | |
def gitpath(chdir = False): | |
"""change dirpath to git root directory, or use-rdefined path | |
chdir: False (default) or True / dirpath | |
""" | |
try: | |
path = subprocess.check_output(['git', 'rev-parse', '--show-toplevel']) | |
path = os.path.abspath(path.decode('utf-8').strip()) | |
except Exception: | |
path = False | |
print('Unexcepted exception', Exception) | |
if not path: | |
path = os.path.abspath(chdir) | |
print('not an git repo') | |
if os.path.exists(path): | |
if chdir: | |
os.chdir(path) | |
print('path changed') | |
return path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment