Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add initial support for Git protocol v2 #1244

Merged
merged 12 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
set the default git protocol version via global constants
  • Loading branch information
stspdotname committed Jun 25, 2024
commit 343ed68cdeb65d9a47851702cfd38f4cec8e2629
20 changes: 11 additions & 9 deletions dulwich/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
COMMAND_SHALLOW,
COMMAND_UNSHALLOW,
COMMAND_WANT,
DEFAULT_GIT_PROTOCOL_VERSION_FETCH,
DEFAULT_GIT_PROTOCOL_VERSION_SEND,
GIT_PROTOCOL_VERSIONS,
KNOWN_RECEIVE_CAPABILITIES,
KNOWN_UPLOAD_CAPABILITIES,
SIDE_BAND_CHANNEL_DATA,
Expand All @@ -117,7 +120,6 @@
extract_capability_names,
parse_capability,
pkt_line,
GIT_PROTOCOL_VERSIONS,
)
from .refs import PEELED_TAG_SUFFIX, _import_remote_refs, read_info_refs
from .repo import Repo
Expand Down Expand Up @@ -564,7 +566,7 @@ def _handle_upload_pack_head(
assert isinstance(wants, list) and isinstance(wants[0], bytes)
wantcmd = COMMAND_WANT + b" " + wants[0]
if protocol_version is None:
protocol_version = 0
protocol_version = DEFAULT_GIT_PROTOCOL_VERSION_SEND
if protocol_version != 2:
wantcmd += b" " + b" ".join(sorted(capabilities))
wantcmd += b"\n"
Expand Down Expand Up @@ -717,7 +719,7 @@ def __init__(
self._fetch_capabilities.remove(CAPABILITY_THIN_PACK)
if include_tags:
self._fetch_capabilities.add(CAPABILITY_INCLUDE_TAG)
self.protocol_version = 0 # our default Git protocol version
self.protocol_version = 0 # will be overridden later

def get_url(self, path):
"""Retrieves full url to given path.
Expand Down Expand Up @@ -1190,7 +1192,7 @@ def send_pack(self, path, update_refs, generate_pack_data, progress=None):
SendPackError: if server rejects the pack data

"""
self.protocol_version = 0
self.protocol_version = DEFAULT_GIT_PROTOCOL_VERSION_SEND
proto, unused_can_read, stderr = self._connect(b"receive-pack", path)
with proto:
try:
Expand Down Expand Up @@ -1546,11 +1548,11 @@ def close():
path = path[1:]
if cmd == b"upload-pack":
if protocol_version is None:
self.protocol_version = 2
self.protocol_version = DEFAULT_GIT_PROTOCOL_VERSION_FETCH
else:
self.protocol_version = protocol_version
else:
self.protocol_version = 0
self.protocol_version = DEFAULT_GIT_PROTOCOL_VERSION_SEND

if cmd == b"upload-pack" and self.protocol_version == 2:
# Git protocol version advertisement is hidden behind two NUL bytes
Expand Down Expand Up @@ -2323,13 +2325,13 @@ def _discover_references(self, service, base_url, protocol_version=None):
# which lacks the "001f# service=git-receive-pack" marker.
if service == b"git-upload-pack":
if protocol_version is None:
self.protocol_version = 2
self.protocol_version = DEFAULT_GIT_PROTOCOL_VERSION_FETCH
else:
self.protocol_version = protocol_version
if self.protocol_version == 2:
headers["Git-Protocol"] = "version=2"
else:
self.protocol_version = 0
self.protocol_version = DEFAULT_GIT_PROTOCOL_VERSION_SEND
url = urljoin(base_url, tail)
resp, read = self._http_request(url, headers)

Expand Down Expand Up @@ -2411,7 +2413,7 @@ def begin_protocol_v2(proto):
) = read_pkt_refs(proto.read_pkt_seq(), server_capabilities)
return refs, server_capabilities, base_url
else:
self.protocol_version = 0
self.protocol_version = 0 # dumb servers only support protocol v0
return read_info_refs(resp), set(), base_url
finally:
resp.close()
Expand Down
2 changes: 2 additions & 0 deletions dulwich/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
# As of 2024, Git only implements version 2 during 'git fetch' and still uses
# version 0 during 'git push'.
GIT_PROTOCOL_VERSIONS = [0, 1, 2]
Copy link
Owner

Choose a reason for hiding this comment

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

Is there actually a version 1? Can you add a comment about these versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I am adding comments in the next version.

They will appear in an earlier commit which adds this list.
I have rewritten history a bit, also to get rid of the 'ruff format' commit in the middle.

DEFAULT_GIT_PROTOCOL_VERSION_FETCH = 2
DEFAULT_GIT_PROTOCOL_VERSION_SEND = 0

ZERO_SHA = b"0" * 40

Expand Down