From 96c6ab4f7572fd5ca8638f3cb6e342d5000955e7 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sat, 10 Jan 2015 18:22:48 +0100 Subject: Added search_parent_directories keyword argument to Repo type. Now by default, we will not walk up the directory structure and possibly find git directories that the user didn't intend to find. If required, that kind of behaviour can be turned back on. Fixes #65 --- git/repo/fun.py | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) (limited to 'git/repo/fun.py') diff --git a/git/repo/fun.py b/git/repo/fun.py index f8342260..a0dc5ce9 100644 --- a/git/repo/fun.py +++ b/git/repo/fun.py @@ -19,8 +19,8 @@ from gitdb.util import ( from git.compat import xrange -__all__ = ('rev_parse', 'is_git_dir', 'touch', 'read_gitfile', 'find_git_dir', 'name_to_object', - 'short_to_long', 'deref_tag', 'to_commit') +__all__ = ('rev_parse', 'is_git_dir', 'touch', 'find_git_dir', 'name_to_object', 'short_to_long', 'deref_tag', + 'to_commit') def touch(filename): @@ -44,32 +44,21 @@ def is_git_dir(d): def find_git_dir(d): if is_git_dir(d): return d - elif isfile(d): + + try: with open(d) as fp: content = fp.read().rstrip() + except (IOError, OSError): + # it's probably not a file + pass + else: if content.startswith('gitdir: '): d = join(dirname(d), content[8:]) return find_git_dir(d) + # end handle exception return None -def read_gitfile(f): - """ This is taken from the git setup.c:read_gitfile function. - :return gitdir path or None if gitfile is invalid.""" - if f is None: - return None - try: - line = open(f, 'r').readline().rstrip() - except (OSError, IOError): - # File might not exist or is unreadable - ignore - return None - # end handle file access - if line[0:8] != 'gitdir: ': - return None - path = os.path.realpath(line[8:]) - return path if is_git_dir(path) else None - - def short_to_long(odb, hexsha): """:return: long hexadecimal sha1 from the given less-than-40 byte hexsha or None if no candidate could be found. -- cgit v1.2.1