diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2015-01-10 18:22:48 +0100 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2015-01-10 18:22:48 +0100 |
commit | 96c6ab4f7572fd5ca8638f3cb6e342d5000955e7 (patch) | |
tree | 5f5bf29b1e866892796cffe61d0bc71d00321823 | |
parent | bfce49feb0be6c69f7fffc57ebdd22b6da241278 (diff) | |
download | gitpython-96c6ab4f7572fd5ca8638f3cb6e342d5000955e7.tar.gz |
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
-rw-r--r-- | doc/source/changes.rst | 1 | ||||
-rw-r--r-- | git/repo/base.py | 10 | ||||
-rw-r--r-- | git/repo/fun.py | 29 | ||||
-rw-r--r-- | git/test/performance/lib.py | 4 |
4 files changed, 16 insertions, 28 deletions
diff --git a/doc/source/changes.rst b/doc/source/changes.rst index b4535a69..67384eb9 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -8,6 +8,7 @@ Changelog * diff() can now properly detect renames, both in patch and raw format. Previously it only worked when create_patch was True. * repo.odb.update_cache() is now called automatically after fetch and pull operations. In case you did that in your own code, you might want to remove your line to prevent a double-update that causes unnecessary IO. * A list of all fixed issues can be found here: https://github.com/gitpython-developers/GitPython/issues?q=milestone%3A%22v0.3.5+-+bugfixes%22+ +* `Repo(path)` will not automatically search upstream anymore and find any git directory on its way up. If you need that behaviour, you can turn it back on using the new `search_parent_directories=True` flag when constructing a `Repo` object. 0.3.4 - Python 3 Support ======================== diff --git a/git/repo/base.py b/git/repo/base.py index dbca4697..97e49aa2 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -46,7 +46,6 @@ from .fun import ( rev_parse, is_git_dir, find_git_dir, - read_gitfile, touch, ) from git.compat import ( @@ -96,7 +95,7 @@ class Repo(object): # represents the configuration level of a configuration file config_level = ("system", "global", "repository") - def __init__(self, path=None, odbt=DefaultDBType): + def __init__(self, path=None, odbt=DefaultDBType, search_parent_directories=False): """Create a new Repo instance :param path: is the path to either the root git directory or the bare git repo:: @@ -128,15 +127,14 @@ class Repo(object): self.git_dir = curpath self._working_tree_dir = os.path.dirname(curpath) break + gitpath = find_git_dir(join(curpath, '.git')) if gitpath is not None: self.git_dir = gitpath self._working_tree_dir = curpath break - gitpath = read_gitfile(curpath) - if gitpath: - self.git_dir = gitpath - self._working_tree_dir = curpath + + if not search_parent_directories: break curpath, dummy = os.path.split(curpath) if not dummy: 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. diff --git a/git/test/performance/lib.py b/git/test/performance/lib.py index 46a198d4..bb3f7a99 100644 --- a/git/test/performance/lib.py +++ b/git/test/performance/lib.py @@ -56,8 +56,8 @@ class TestBigRepoR(TestBase): "your choice - defaulting to the gitpython repository") repo_path = os.path.dirname(__file__) # end set some repo path - self.gitrorepo = Repo(repo_path, odbt=GitCmdObjectDB) - self.puregitrorepo = Repo(repo_path, odbt=GitDB) + self.gitrorepo = Repo(repo_path, odbt=GitCmdObjectDB, search_parent_directories=True) + self.puregitrorepo = Repo(repo_path, odbt=GitDB, search_parent_directories=True) def tearDown(self): self.gitrorepo.git.clear_cache() |