← 返回首页
config: resolve relative includes from relative config paths by officialasishkumar · Pull Request #2123 · gitpython-developers/GitPython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

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

Provide feedback

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

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
15 changes: 11 additions & 4 deletions git/config.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
List,
Dict,
Sequence,
Set,
TYPE_CHECKING,
Tuple,
TypeVar,
Expand Down Expand Up @@ -631,11 +632,17 @@ def read(self) -> None: # type: ignore[override]
files_to_read = list(self._file_or_files)
# END ensure we have a copy of the paths to handle

seen = set(files_to_read)
def path_key(file_path: Union[PathLike, IO]) -> Union[str, IO]:
if isinstance(file_path, (str, os.PathLike)):
return osp.normpath(osp.abspath(file_path))
return file_path

seen: Set[Union[str, IO]] = {path_key(file_path) for file_path in files_to_read}
num_read_include_files = 0
while files_to_read:
file_path = files_to_read.pop(0)
file_ok = False
abs_file_path: Union[str, None] = None

if hasattr(file_path, "seek"):
# Must be a file-object.
Expand All @@ -644,6 +651,7 @@ def read(self) -> None: # type: ignore[override]
self._read(file_path, file_path.name)
else:
try:
abs_file_path = osp.normpath(osp.abspath(file_path))
with open(file_path, "rb") as fp:
file_ok = True
self._read(fp, fp.name)
Expand All @@ -660,9 +668,8 @@ def read(self) -> None: # type: ignore[override]
if not file_ok:
continue
# END ignore relative paths if we don't know the configuration file path
file_path = cast(PathLike, file_path)
assert osp.isabs(file_path), "Need absolute paths to be sure our cycle checks will work"
include_path = osp.join(osp.dirname(file_path), include_path)
assert abs_file_path is not None, "Need a source path to resolve relative include paths"
include_path = osp.join(osp.dirname(abs_file_path), include_path)
# END make include path absolute
include_path = osp.normpath(include_path)
if include_path in seen or not os.access(include_path, os.R_OK):
Expand Down
20 changes: 20 additions & 0 deletions test/test_config.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,26 @@ def check_test_value(cr, value):
with GitConfigParser(fpa, read_only=True) as cr:
check_test_value(cr, tv)

@with_rw_directory
def test_config_include_from_relative_config_path(self, rw_dir):
fpa = osp.join(rw_dir, "a")
nested_dir = osp.join(rw_dir, "subdir")
os.makedirs(nested_dir)
fpb = osp.join(nested_dir, "b")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

One of the included files need to be in a subdirectory, and both need to be named "a" so the relative name is the same for an actual cycle check.


with GitConfigParser(fpa, read_only=False) as cw:
cw.set_value("a", "value", "a")
cw.set_value("include", "path", "subdir/b")

with GitConfigParser(fpb, read_only=False) as cw:
cw.set_value("b", "value", "b")
cw.set_value("include", "path", "../a")

with GitConfigParser(osp.relpath(fpa), read_only=True) as cr:
assert cr.get_value("a", "value") == "a"
assert cr.get_value("b", "value") == "b"
assert cr.get_values("include", "path") == ["subdir/b", "../a"]

@with_rw_directory
def test_multiple_include_paths_with_same_key(self, rw_dir):
"""Test that multiple 'path' entries under [include] are all respected.
Expand Down
Loading
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.