Skip to content

Instantly share code, notes, and snippets.

@ruxi
Created December 2, 2017 05:04
Show Gist options
  • Save ruxi/1baa9fd08c962b7bd4004fb59be2b8fa to your computer and use it in GitHub Desktop.
Save ruxi/1baa9fd08c962b7bd4004fb59be2b8fa to your computer and use it in GitHub Desktop.
"""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