From 9c3c6504782057f6d6898ea88412d14dd0cf2bf9 Mon Sep 17 00:00:00 2001 From: Konstantin Olshanov Date: Mon, 10 Apr 2017 14:25:59 +0300 Subject: [PATCH] add minimal handling for git worktree Signed-off-by: Konstantin Olshanov --- git/repo/fun.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/git/repo/fun.py b/git/repo/fun.py index 6b06663a0..397aa941d 100644 --- a/git/repo/fun.py +++ b/git/repo/fun.py @@ -44,30 +44,30 @@ def is_git_dir(d): return isfile(headref) or \ (os.path.islink(headref) and os.readlink(headref).startswith('refs')) - elif isfile(join(d, 'gitdir')) and isfile(join(d, 'commondir')) and isfile(join(d, 'gitfile')): - raise WorkTreeRepositoryUnsupported(d) return False - -def find_git_dir(d): - if is_git_dir(d): - return d - +def read_gitdir_link(link): try: - with open(d) as fp: + with open(link) as fp: content = fp.read().rstrip() + if not content.startswith('gitdir:'): + return None + + path = content.split(':', 1)[1].strip() + if not os.path.abspath(path): + path = join(dirname(link), path) + + with open(join(path, 'commondir')) as fp: + return join(path, fp.read().strip()) except (IOError, OSError): - # it's probably not a file pass - else: - if content.startswith('gitdir: '): - path = content[8:] - if not os.path.isabs(path): - path = join(dirname(d), path) - return find_git_dir(path) - # end handle exception return None +def find_git_dir(d): + if is_git_dir(d): + return d + + return read_gitdir_link(d) def short_to_long(odb, hexsha): """:return: long hexadecimal sha1 from the given less-than-40 byte hexsha