Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c84dde2

Browse filesBrowse files
authored
Merge pull request #1550 from Sineaggi/remove-optional-from-two-variables
Remove optional from two member variables
2 parents 6ed2285 + 4a44fdf commit c84dde2
Copy full SHA for c84dde2

File tree

Expand file treeCollapse file tree

2 files changed

+36
-29
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+36
-29
lines changed

‎git/repo/base.py

Copy file name to clipboardExpand all lines: git/repo/base.py
+29-27Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,17 @@ class Repo(object):
115115
'working_dir' is the working directory of the git command, which is the working tree
116116
directory if available or the .git directory in case of bare repositories
117117
118-
'working_tree_dir' is the working tree directory, but will raise AssertionError
118+
'working_tree_dir' is the working tree directory, but will return None
119119
if we are a bare repository.
120120
121121
'git_dir' is the .git repository directory, which is always set."""
122122

123123
DAEMON_EXPORT_FILE = "git-daemon-export-ok"
124124

125125
git = cast("Git", None) # Must exist, or __del__ will fail in case we raise on `__init__()`
126-
working_dir: Optional[PathLike] = None
126+
working_dir: PathLike
127127
_working_tree_dir: Optional[PathLike] = None
128-
git_dir: PathLike = ""
128+
git_dir: PathLike
129129
_common_dir: PathLike = ""
130130

131131
# precompiled regex
@@ -215,13 +215,14 @@ def __init__(
215215
## Walk up the path to find the `.git` dir.
216216
#
217217
curpath = epath
218+
git_dir = None
218219
while curpath:
219220
# ABOUT osp.NORMPATH
220221
# It's important to normalize the paths, as submodules will otherwise initialize their
221222
# repo instances with paths that depend on path-portions that will not exist after being
222223
# removed. It's just cleaner.
223224
if is_git_dir(curpath):
224-
self.git_dir = curpath
225+
git_dir = curpath
225226
# from man git-config : core.worktree
226227
# Set the path to the root of the working tree. If GIT_COMMON_DIR environment
227228
# variable is set, core.worktree is ignored and not used for determining the
@@ -230,9 +231,9 @@ def __init__(
230231
# directory, which is either specified by GIT_DIR, or automatically discovered.
231232
# If GIT_DIR is specified but none of GIT_WORK_TREE and core.worktree is specified,
232233
# the current working directory is regarded as the top level of your working tree.
233-
self._working_tree_dir = os.path.dirname(self.git_dir)
234+
self._working_tree_dir = os.path.dirname(git_dir)
234235
if os.environ.get("GIT_COMMON_DIR") is None:
235-
gitconf = self.config_reader("repository")
236+
gitconf = self._config_reader("repository", git_dir)
236237
if gitconf.has_option("core", "worktree"):
237238
self._working_tree_dir = gitconf.get("core", "worktree")
238239
if "GIT_WORK_TREE" in os.environ:
@@ -242,14 +243,14 @@ def __init__(
242243
dotgit = osp.join(curpath, ".git")
243244
sm_gitpath = find_submodule_git_dir(dotgit)
244245
if sm_gitpath is not None:
245-
self.git_dir = osp.normpath(sm_gitpath)
246+
git_dir = osp.normpath(sm_gitpath)
246247

247248
sm_gitpath = find_submodule_git_dir(dotgit)
248249
if sm_gitpath is None:
249250
sm_gitpath = find_worktree_git_dir(dotgit)
250251

251252
if sm_gitpath is not None:
252-
self.git_dir = expand_path(sm_gitpath, expand_vars)
253+
git_dir = expand_path(sm_gitpath, expand_vars)
253254
self._working_tree_dir = curpath
254255
break
255256

@@ -260,8 +261,9 @@ def __init__(
260261
break
261262
# END while curpath
262263

263-
if self.git_dir is None:
264+
if git_dir is None:
264265
raise InvalidGitRepositoryError(epath)
266+
self.git_dir = git_dir
265267

266268
self._bare = False
267269
try:
@@ -282,7 +284,7 @@ def __init__(
282284
self._working_tree_dir = None
283285
# END working dir handling
284286

285-
self.working_dir: Optional[PathLike] = self._working_tree_dir or self.common_dir
287+
self.working_dir: PathLike = self._working_tree_dir or self.common_dir
286288
self.git = self.GitCommandWrapperType(self.working_dir)
287289

288290
# special handling, in special times
@@ -320,7 +322,7 @@ def close(self) -> None:
320322
gc.collect()
321323

322324
def __eq__(self, rhs: object) -> bool:
323-
if isinstance(rhs, Repo) and self.git_dir:
325+
if isinstance(rhs, Repo):
324326
return self.git_dir == rhs.git_dir
325327
return False
326328

@@ -332,14 +334,12 @@ def __hash__(self) -> int:
332334

333335
# Description property
334336
def _get_description(self) -> str:
335-
if self.git_dir:
336-
filename = osp.join(self.git_dir, "description")
337+
filename = osp.join(self.git_dir, "description")
337338
with open(filename, "rb") as fp:
338339
return fp.read().rstrip().decode(defenc)
339340

340341
def _set_description(self, descr: str) -> None:
341-
if self.git_dir:
342-
filename = osp.join(self.git_dir, "description")
342+
filename = osp.join(self.git_dir, "description")
343343
with open(filename, "wb") as fp:
344344
fp.write((descr + "\n").encode(defenc))
345345

@@ -357,13 +357,7 @@ def common_dir(self) -> PathLike:
357357
"""
358358
:return: The git dir that holds everything except possibly HEAD,
359359
FETCH_HEAD, ORIG_HEAD, COMMIT_EDITMSG, index, and logs/."""
360-
if self._common_dir:
361-
return self._common_dir
362-
elif self.git_dir:
363-
return self.git_dir
364-
else:
365-
# or could return ""
366-
raise InvalidGitRepositoryError()
360+
return self._common_dir or self.git_dir
367361

368362
@property
369363
def bare(self) -> bool:
@@ -532,7 +526,9 @@ def delete_remote(self, remote: "Remote") -> str:
532526
"""Delete the given remote."""
533527
return Remote.remove(self, remote)
534528

535-
def _get_config_path(self, config_level: Lit_config_levels) -> str:
529+
def _get_config_path(self, config_level: Lit_config_levels, git_dir: Optional[PathLike] = None) -> str:
530+
if git_dir is None:
531+
git_dir = self.git_dir
536532
# we do not support an absolute path of the gitconfig on windows ,
537533
# use the global config instead
538534
if is_win and config_level == "system":
@@ -546,7 +542,7 @@ def _get_config_path(self, config_level: Lit_config_levels) -> str:
546542
elif config_level == "global":
547543
return osp.normpath(osp.expanduser("~/.gitconfig"))
548544
elif config_level == "repository":
549-
repo_dir = self._common_dir or self.git_dir
545+
repo_dir = self._common_dir or git_dir
550546
if not repo_dir:
551547
raise NotADirectoryError
552548
else:
@@ -575,15 +571,21 @@ def config_reader(
575571
you know which file you wish to read to prevent reading multiple files.
576572
:note: On windows, system configuration cannot currently be read as the path is
577573
unknown, instead the global path will be used."""
578-
files = None
574+
return self._config_reader(config_level=config_level)
575+
576+
def _config_reader(
577+
self,
578+
config_level: Optional[Lit_config_levels] = None,
579+
git_dir: Optional[PathLike] = None,
580+
) -> GitConfigParser:
579581
if config_level is None:
580582
files = [
581-
self._get_config_path(cast(Lit_config_levels, f))
583+
self._get_config_path(cast(Lit_config_levels, f), git_dir)
582584
for f in self.config_level
583585
if cast(Lit_config_levels, f)
584586
]
585587
else:
586-
files = [self._get_config_path(config_level)]
588+
files = [self._get_config_path(config_level, git_dir)]
587589
return GitConfigParser(files, read_only=True, repo=self)
588590

589591
def config_writer(self, config_level: Lit_config_levels = "repository") -> GitConfigParser:

‎test/test_base.py

Copy file name to clipboardExpand all lines: test/test_base.py
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import tempfile
1010
from unittest import SkipTest, skipIf
1111

12+
from git import Repo
1213
from git.objects import Blob, Tree, Commit, TagObject
1314
from git.compat import is_win
1415
from git.objects.util import get_object_type_by_name
@@ -95,14 +96,18 @@ def test_object_resolution(self):
9596
self.assertEqual(self.rorepo.head.reference.object, self.rorepo.active_branch.object)
9697

9798
@with_rw_repo("HEAD", bare=True)
98-
def test_with_bare_rw_repo(self, bare_rw_repo):
99+
def test_with_bare_rw_repo(self, bare_rw_repo: Repo):
99100
assert bare_rw_repo.config_reader("repository").getboolean("core", "bare")
100101
assert osp.isfile(osp.join(bare_rw_repo.git_dir, "HEAD"))
102+
assert osp.isdir(bare_rw_repo.working_dir)
103+
assert bare_rw_repo.working_tree_dir is None
101104

102105
@with_rw_repo("0.1.6")
103-
def test_with_rw_repo(self, rw_repo):
106+
def test_with_rw_repo(self, rw_repo: Repo):
104107
assert not rw_repo.config_reader("repository").getboolean("core", "bare")
108+
assert osp.isdir(rw_repo.working_tree_dir)
105109
assert osp.isdir(osp.join(rw_repo.working_tree_dir, "lib"))
110+
assert osp.isdir(rw_repo.working_dir)
106111

107112
@skipIf(HIDE_WINDOWS_FREEZE_ERRORS, "FIXME: Freezes! sometimes...")
108113
@with_rw_and_rw_remote_repo("0.1.6")

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.