Skip to content

Commit e0e5918

Browse files
Byroncodex
andcommitted
Handle uninitialized submodule and commit streams
<!-- agent --> RootModule.update could swallow setup failures with keep_going and then iterate sms before assignment. Start with an empty submodule list so the handled failure safely skips updates. Commit iteration could likewise read stream before assignment for processes without stdout or unsupported inputs. Raise explicit input errors instead. Assisted-by: GPT 5.6 Co-authored-by: GPT 5.6 <codex@openai.com>
1 parent 2d7f861 commit e0e5918

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

git/objects/commit.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,14 @@ def _iter_from_process_or_stream(cls, repo: "Repo", proc_or_stream: Union[Popen,
575575

576576
if hasattr(proc_or_stream, "wait"):
577577
proc_or_stream = cast(Popen, proc_or_stream)
578-
if proc_or_stream.stdout is not None:
579-
stream = proc_or_stream.stdout
578+
stream = proc_or_stream.stdout
579+
if stream is None:
580+
raise ValueError("Process has no stdout stream")
580581
elif hasattr(proc_or_stream, "readline"):
581582
proc_or_stream = cast(IO, proc_or_stream) # type: ignore[redundant-cast]
582583
stream = proc_or_stream
584+
else:
585+
raise TypeError("Expected a process or stream")
583586

584587
readline = stream.readline
585588
while True:

git/objects/submodule/root.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import git
99
from git.exc import InvalidGitRepositoryError
10+
from git.util import IterableList
1011

1112
from .base import Submodule, UpdateProgress
1213
from .util import find_first_remote_branch
@@ -19,7 +20,6 @@
1920

2021
if TYPE_CHECKING:
2122
from git.repo import Repo
22-
from git.util import IterableList
2323

2424
# ----------------------------------------------------------------------------
2525

@@ -162,6 +162,7 @@ def update( # type: ignore[override]
162162
prefix = "DRY-RUN: "
163163

164164
repo = self.repo
165+
sms: "IterableList[Submodule]" = IterableList("name")
165166

166167
try:
167168
# SETUP BASE COMMIT
@@ -182,7 +183,7 @@ def update( # type: ignore[override]
182183
# END handle previous commit
183184

184185
psms: "IterableList[Submodule]" = self.list_items(repo, parent_commit=previous_commit)
185-
sms: "IterableList[Submodule]" = self.list_items(repo)
186+
sms = self.list_items(repo)
186187
spsms = set(psms)
187188
ssms = set(sms)
188189

test/test_commit.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ def test_rev_list_bisect_all(self):
327327
for sha1, commit in zip(expected_ids, commits):
328328
self.assertEqual(sha1, commit.hexsha)
329329

330+
def test_iter_from_invalid_process_or_stream(self):
331+
for source, error in ((Mock(wait=Mock(), stdout=None), ValueError), (object(), TypeError)):
332+
with self.assertRaises(error):
333+
list(Commit._iter_from_process_or_stream(self.rorepo, source))
334+
330335
@with_rw_directory
331336
def test_ambiguous_arg_iteration(self, rw_dir):
332337
rw_repo = Repo.init(osp.join(rw_dir, "test_ambiguous_arg"))

test/test_submodule.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,9 @@ def test_root_module(self, rwrepo):
511511
# Cannot set the parent commit as root module's path didn't exist.
512512
self.assertRaises(ValueError, rm.set_parent_commit, "HEAD")
513513

514+
with mock.patch.object(RootModule, "list_items", side_effect=ValueError("boom")):
515+
rm.update(keep_going=True)
516+
514517
# TEST UPDATE
515518
#############
516519
# Set up a commit that removes existing, adds new and modifies existing

0 commit comments

Comments
 (0)