Skip to content

Commit

Permalink
config: handle malformed git submodules (#1224)
Browse files Browse the repository at this point in the history
A git submodule needs two items in its config: url to clone from, path
to clone to. If either is missing, core git simply ignores this
submodule entry. Dulwich should do the same.
  • Loading branch information
jordigh authored Oct 4, 2023
1 parent a36e3c0 commit cd09902
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
12 changes: 9 additions & 3 deletions dulwich/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,9 +780,15 @@ def parse_submodules(config: ConfigFile) -> Iterator[Tuple[bytes, bytes, bytes]]
for section in config.keys():
section_kind, section_name = section
if section_kind == b"submodule":
sm_path = config.get(section, b"path")
sm_url = config.get(section, b"url")
yield (sm_path, sm_url, section_name)
try:
sm_path = config.get(section, b"path")
sm_url = config.get(section, b"url")
yield (sm_path, sm_url, section_name)
except KeyError:
# If either path or url is missing, just ignore this
# submodule entry and move on to the next one. This is
# how git itself handles malformed .gitmodule entries.
pass


def iter_instead_of(config: Config, push: bool = False) -> Iterable[Tuple[str, str]]:
Expand Down
25 changes: 25 additions & 0 deletions dulwich/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,31 @@ def testSubmodules(self):
[submodule "core/lib"]
\tpath = core/lib
\turl = https://github.com/phhusson/QuasselC.git
"""
)
)
got = list(parse_submodules(cf))
self.assertEqual(
[
(
b"core/lib",
b"https://github.com/phhusson/QuasselC.git",
b"core/lib",
)
],
got,
)

def testMalformedSubmodules(self):
cf = ConfigFile.from_file(
BytesIO(
b"""\
[submodule "core/lib"]
\tpath = core/lib
\turl = https://github.com/phhusson/QuasselC.git
[submodule "dulwich"]
\turl = https://github.com/jelmer/dulwich
"""
)
)
Expand Down

0 comments on commit cd09902

Please sign in to comment.