Skip to content

Commit c65c094

Browse files
committed
dulwich: preserve data from generators when retrying request
1 parent 697b946 commit c65c094

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/scmrepo/git/backend/dulwich/client.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, Optional
1+
from typing import Dict, Iterator, List, Optional, Union
22

33
from dulwich.client import HTTPUnauthorized, Urllib3HttpGitClient
44

@@ -27,10 +27,28 @@ def _http_request(
2727
self,
2828
url: str,
2929
headers: Optional[Dict[str, str]] = None,
30-
data: Any = None,
30+
data: Optional[Union[bytes, Iterator[bytes]]] = None,
3131
):
32+
cached_chunks: List[bytes] = []
33+
34+
def _cached_data() -> Iterator[bytes]:
35+
assert data is not None
36+
if isinstance(data, bytes):
37+
yield data
38+
return
39+
40+
if cached_chunks:
41+
yield from cached_chunks
42+
return
43+
44+
for chunk in data:
45+
cached_chunks.append(chunk)
46+
yield chunk
47+
3248
try:
33-
result = super()._http_request(url, headers=headers, data=data)
49+
result = super()._http_request(
50+
url, headers=headers, data=None if data is None else _cached_data()
51+
)
3452
except HTTPUnauthorized:
3553
auth_header = self._get_auth()
3654
if not auth_header:
@@ -39,7 +57,9 @@ def _http_request(
3957
headers.update(auth_header)
4058
else:
4159
headers = auth_header
42-
result = super()._http_request(url, headers=headers, data=data)
60+
result = super()._http_request(
61+
url, headers=headers, data=None if data is None else _cached_data()
62+
)
4363
if self._store_credentials is not None:
4464
self._store_credentials.approve()
4565
return result

0 commit comments

Comments
 (0)