Skip to content

Commit

Permalink
Factor out extraction of symrefs
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Oct 20, 2024
1 parent 7b881b3 commit c6abf72
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
41 changes: 26 additions & 15 deletions dulwich/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,26 @@ def progress(x):
pack_data(data)


def _extract_symrefs_and_agent(capabilities):
"""Extract symrefs and agent from capabilities.
Args:
capabilities: List of capabilities
Returns:
(symrefs, agent) tuple
"""
symrefs = {}
agent = None
for capability in capabilities:
k, v = parse_capability(capability)
if k == CAPABILITY_SYMREF:
(src, dst) = v.split(b":", 1)
symrefs[src] = dst
if k == CAPABILITY_AGENT:
agent = v
return (symrefs, agent)


# TODO(durin42): this doesn't correctly degrade if the server doesn't
# support some capabilities. This should work properly with servers
# that don't support multi_ack.
Expand Down Expand Up @@ -1012,11 +1032,7 @@ def _should_send_pack(new_refs):

def _negotiate_receive_pack_capabilities(self, server_capabilities):
negotiated_capabilities = self._send_capabilities & server_capabilities
agent = None
for capability in server_capabilities:
k, v = parse_capability(capability)
if k == CAPABILITY_AGENT:
agent = v
(agent, _symrefs) = _extract_symrefs_and_agent(server_capabilities)
(extract_capability_names(server_capabilities) - KNOWN_RECEIVE_CAPABILITIES)
# TODO(jelmer): warn about unknown capabilities
return negotiated_capabilities, agent
Expand Down Expand Up @@ -1069,23 +1085,16 @@ def progress(x):
def _negotiate_upload_pack_capabilities(self, server_capabilities):
(extract_capability_names(server_capabilities) - KNOWN_UPLOAD_CAPABILITIES)
# TODO(jelmer): warn about unknown capabilities
symrefs = {}
agent = None
fetch_capa = None
for capability in server_capabilities:
k, v = parse_capability(capability)
if k == CAPABILITY_SYMREF:
(src, dst) = v.split(b":", 1)
symrefs[src] = dst
if k == CAPABILITY_AGENT:
agent = v
if self.protocol_version == 2 and k == CAPABILITY_FETCH:
fetch_capa = CAPABILITY_FETCH
fetch_features = []
v = v.strip()
if b"shallow" in v.split(b" "):
v = v.strip().split(b" ")
if b"shallow" in v:
fetch_features.append(CAPABILITY_SHALLOW)
if b"filter" in v.split(b" "):
if b"filter" in v:
fetch_features.append(CAPABILITY_FILTER)
for i in range(len(fetch_features)):
if i == 0:
Expand All @@ -1094,6 +1103,8 @@ def _negotiate_upload_pack_capabilities(self, server_capabilities):
fetch_capa += b" "
fetch_capa += fetch_features[i]

(symrefs, agent) = _extract_symrefs_and_agent(server_capabilities)

negotiated_capabilities = self._fetch_capabilities & server_capabilities
if fetch_capa:
negotiated_capabilities.add(fetch_capa)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
get_transport_and_path,
get_transport_and_path_from_url,
parse_rsync_url,
_extract_symrefs_and_agent,
)
from dulwich.config import ConfigDict
from dulwich.objects import Commit, Tree
Expand Down Expand Up @@ -1867,3 +1868,14 @@ def test_no_error_line(self):
]
),
)


class TestExtractAgentAndSymrefs(TestCase):

def test_extract_agent_and_symrefs(self):
(agent, symrefs) = _extract_symrefs_and_agent(
[b"agent=git/2.31.1", b"symref=HEAD:refs/heads/master"
])
self.assertEqual(agent, b"git/2.31.1")
self.assertEqual(symrefs, {b"HEAD": b"refs/heads/master"})

0 comments on commit c6abf72

Please sign in to comment.