Do not use .strip(), add two check git has

This commit is contained in:
ZyX 2014-04-23 06:36:24 +04:00
parent ba45a002dc
commit c603410843
1 changed files with 7 additions and 2 deletions

View File

@ -28,8 +28,13 @@ def git_directory(directory):
path = os.path.join(directory, '.git')
if os.path.isfile(path):
with open(path, 'rb') as f:
raw = f.read().partition(b':')[2].strip()
return os.path.abspath(os.path.join(directory, raw.decode(sys.getfilesystemencoding() or 'utf-8')))
raw = f.read()
if not raw.startswith(b'gitdir: '):
raise IOError('invalid gitfile format')
raw = raw[8:].decode(sys.getfilesystemencoding() or 'utf-8')
if not raw:
raise IOError('no path in gitfile')
return os.path.abspath(os.path.join(directory, raw))
else:
return path