Skip to content

Commit 96c6ab4

Browse files
committed
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
1 parent bfce49f commit 96c6ab4

File tree

4 files changed

+16
-28
lines changed

4 files changed

+16
-28
lines changed

‎doc/source/changes.rst

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Changelog
88
* diff() can now properly detect renames, both in patch and raw format. Previously it only worked when create_patch was True.
99
* 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.
1010
* 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+
11+
* `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.
1112

1213
0.3.4 - Python 3 Support
1314
========================

‎git/repo/base.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
rev_parse,
4747
is_git_dir,
4848
find_git_dir,
49-
read_gitfile,
5049
touch,
5150
)
5251
from git.compat import (
@@ -96,7 +95,7 @@ class Repo(object):
9695
# represents the configuration level of a configuration file
9796
config_level = ("system", "global", "repository")
9897

99-
def __init__(self, path=None, odbt=DefaultDBType):
98+
def __init__(self, path=None, odbt=DefaultDBType, search_parent_directories=False):
10099
"""Create a new Repo instance
101100
102101
:param path: is the path to either the root git directory or the bare git repo::
@@ -128,15 +127,14 @@ def __init__(self, path=None, odbt=DefaultDBType):
128127
self.git_dir = curpath
129128
self._working_tree_dir = os.path.dirname(curpath)
130129
break
130+
131131
gitpath = find_git_dir(join(curpath, '.git'))
132132
if gitpath is not None:
133133
self.git_dir = gitpath
134134
self._working_tree_dir = curpath
135135
break
136-
gitpath = read_gitfile(curpath)
137-
if gitpath:
138-
self.git_dir = gitpath
139-
self._working_tree_dir = curpath
136+
137+
if not search_parent_directories:
140138
break
141139
curpath, dummy = os.path.split(curpath)
142140
if not dummy:

‎git/repo/fun.py

+9-20
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
from git.compat import xrange
2020

2121

22-
__all__ = ('rev_parse', 'is_git_dir', 'touch', 'read_gitfile', 'find_git_dir', 'name_to_object',
23-
'short_to_long', 'deref_tag', 'to_commit')
22+
__all__ = ('rev_parse', 'is_git_dir', 'touch', 'find_git_dir', 'name_to_object', 'short_to_long', 'deref_tag',
23+
'to_commit')
2424

2525

2626
def touch(filename):
@@ -44,32 +44,21 @@ def is_git_dir(d):
4444
def find_git_dir(d):
4545
if is_git_dir(d):
4646
return d
47-
elif isfile(d):
47+
48+
try:
4849
with open(d) as fp:
4950
content = fp.read().rstrip()
51+
except (IOError, OSError):
52+
# it's probably not a file
53+
pass
54+
else:
5055
if content.startswith('gitdir: '):
5156
d = join(dirname(d), content[8:])
5257
return find_git_dir(d)
58+
# end handle exception
5359
return None
5460

5561

56-
def read_gitfile(f):
57-
""" This is taken from the git setup.c:read_gitfile function.
58-
:return gitdir path or None if gitfile is invalid."""
59-
if f is None:
60-
return None
61-
try:
62-
line = open(f, 'r').readline().rstrip()
63-
except (OSError, IOError):
64-
# File might not exist or is unreadable - ignore
65-
return None
66-
# end handle file access
67-
if line[0:8] != 'gitdir: ':
68-
return None
69-
path = os.path.realpath(line[8:])
70-
return path if is_git_dir(path) else None
71-
72-
7362
def short_to_long(odb, hexsha):
7463
""":return: long hexadecimal sha1 from the given less-than-40 byte hexsha
7564
or None if no candidate could be found.

‎git/test/performance/lib.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def setUp(self):
5656
"your choice - defaulting to the gitpython repository")
5757
repo_path = os.path.dirname(__file__)
5858
# end set some repo path
59-
self.gitrorepo = Repo(repo_path, odbt=GitCmdObjectDB)
60-
self.puregitrorepo = Repo(repo_path, odbt=GitDB)
59+
self.gitrorepo = Repo(repo_path, odbt=GitCmdObjectDB, search_parent_directories=True)
60+
self.puregitrorepo = Repo(repo_path, odbt=GitDB, search_parent_directories=True)
6161

6262
def tearDown(self):
6363
self.gitrorepo.git.clear_cache()

0 commit comments

Comments
 (0)