Skip to content

Commit 9d5d143

Browse files
committed
repo: minor code and doc correcions.
+ Expansion of paths also `osp.normalize()` them. + Make Repo-fields --> class-fields to avoid initializations on construct. + Explain and rename `git.repo.fun.find_git_dir()` is for submodules (`find_submodule_git_dir()`).
1 parent b29388a commit 9d5d143

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

git/repo/base.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
from .fun import (
4848
rev_parse,
4949
is_git_dir,
50-
find_git_dir,
50+
find_submodule_git_dir,
5151
touch,
5252
)
5353
from git.compat import (
@@ -79,7 +79,7 @@
7979

8080

8181
def _expand_path(p):
82-
return os.path.abspath(os.path.expandvars(os.path.expanduser(p)))
82+
return os.path.normpath(os.path.abspath(os.path.expandvars(os.path.expanduser(p))))
8383

8484

8585
class Repo(object):
@@ -98,6 +98,11 @@ class Repo(object):
9898
'git_dir' is the .git repository directory, which is always set."""
9999
DAEMON_EXPORT_FILE = 'git-daemon-export-ok'
100100

101+
git = None # Must exist, or __del__ will fail in case we raise on `__init__()`
102+
working_dir = None
103+
_working_tree_dir = None
104+
git_dir = None
105+
101106
# precompiled regex
102107
re_whitespace = re.compile(r'\s+')
103108
re_hexsha_only = re.compile('^[0-9A-Fa-f]{40}$')
@@ -138,16 +143,11 @@ def __init__(self, path=None, odbt=DefaultDBType, search_parent_directories=Fals
138143
:raise InvalidGitRepositoryError:
139144
:raise NoSuchPathError:
140145
:return: git.Repo """
141-
self.git = None # should be set for __del__ not to fail in case we raise
142146
epath = os.getenv('GIT_DIR')
143147
epath = _expand_path(epath or path or os.getcwd())
144148
if not os.path.exists(epath):
145149
raise NoSuchPathError(epath)
146150

147-
self.working_dir = None
148-
self._working_tree_dir = None
149-
self.git_dir = None
150-
151151
## Walk up the path to find the `.git` dir.
152152
#
153153
curpath = epath
@@ -157,20 +157,20 @@ def __init__(self, path=None, odbt=DefaultDBType, search_parent_directories=Fals
157157
# repo instances with paths that depend on path-portions that will not exist after being
158158
# removed. It's just cleaner.
159159
if is_git_dir(curpath):
160-
self.git_dir = os.path.normpath(curpath)
160+
self.git_dir = curpath
161161
self._working_tree_dir = os.path.dirname(self.git_dir)
162162
break
163163

164-
gitpath = find_git_dir(join(curpath, '.git'))
165-
if gitpath is not None:
166-
self.git_dir = os.path.normpath(gitpath)
164+
sm_gitpath = find_submodule_git_dir(join(curpath, '.git'))
165+
if sm_gitpath is not None:
166+
self.git_dir = os.path.normpath(sm_gitpath)
167167
self._working_tree_dir = curpath
168168
break
169169

170170
if not search_parent_directories:
171171
break
172-
curpath, dummy = os.path.split(curpath)
173-
if not dummy:
172+
curpath, tail = os.path.split(curpath)
173+
if not tail:
174174
break
175175
# END while curpath
176176

git/repo/fun.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from git.compat import xrange
2121

2222

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

2626

@@ -49,7 +49,8 @@ def is_git_dir(d):
4949
return False
5050

5151

52-
def find_git_dir(d):
52+
def find_submodule_git_dir(d):
53+
"""Search for a submodule repo."""
5354
if is_git_dir(d):
5455
return d
5556

@@ -64,7 +65,7 @@ def find_git_dir(d):
6465
path = content[8:]
6566
if not os.path.isabs(path):
6667
path = join(dirname(d), path)
67-
return find_git_dir(path)
68+
return find_submodule_git_dir(path)
6869
# end handle exception
6970
return None
7071

git/test/test_submodule.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from git.objects.submodule.base import Submodule
1515
from git.objects.submodule.root import RootModule, RootUpdateProgress
1616
from git.repo.fun import (
17-
find_git_dir,
17+
find_submodule_git_dir,
1818
touch
1919
)
2020
from git.test.lib import (
@@ -757,7 +757,7 @@ def assert_exists(sm, value=True):
757757
else:
758758
assert osp.isfile(module_repo_path)
759759
assert sm.module().has_separate_working_tree()
760-
assert find_git_dir(module_repo_path) is not None, "module pointed to by .git file must be valid"
760+
assert find_submodule_git_dir(module_repo_path) is not None, "module pointed to by .git file must be valid"
761761
# end verify submodule 'style'
762762

763763
# test move

0 commit comments

Comments
 (0)