Skip to content

Commit

Permalink
Add constants for capabilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Apr 19, 2015
1 parent 417e4be commit 44e24e3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
30 changes: 19 additions & 11 deletions dulwich/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
)
from dulwich.protocol import (
_RBUFSIZE,
CAPABILITY_DELETE_REFS,
CAPABILITY_MULTI_ACK,
CAPABILITY_MULTI_ACK_DETAILED,
CAPABILITY_OFS_DELTA,
CAPABILITY_REPORT_STATUS,
CAPABILITY_SIDE_BAND_64K,
CAPABILITY_THIN_PACK,
PktLineParser,
Protocol,
ProtocolFile,
Expand All @@ -79,10 +86,11 @@ def _fileno_can_read(fileno):
"""Check if a file descriptor is readable."""
return len(select.select([fileno], [], [], 0)[0]) > 0

COMMON_CAPABILITIES = [b'ofs-delta', b'side-band-64k']
FETCH_CAPABILITIES = ([b'thin-pack', b'multi_ack', b'multi_ack_detailed'] +
COMMON_CAPABILITIES = [CAPABILITY_OFS_DELTA, CAPABILITY_SIDE_BAND_64K]
FETCH_CAPABILITIES = ([CAPABILITY_THIN_PACK, CAPABILITY_MULTI_ACK,
CAPABILITY_MULTI_ACK_DETAILED] +
COMMON_CAPABILITIES)
SEND_CAPABILITIES = [b'report-status'] + COMMON_CAPABILITIES
SEND_CAPABILITIES = [CAPABILITY_REPORT_STATUS] + COMMON_CAPABILITIES


class ReportStatusParser(object):
Expand Down Expand Up @@ -180,7 +188,7 @@ def __init__(self, thin_packs=True, report_activity=None):
self._fetch_capabilities = set(FETCH_CAPABILITIES)
self._send_capabilities = set(SEND_CAPABILITIES)
if not thin_packs:
self._fetch_capabilities.remove(b'thin-pack')
self._fetch_capabilities.remove(CAPABILITY_THIN_PACK)

def send_pack(self, path, determine_wants, generate_pack_contents,
progress=None, write_pack=write_pack_objects):
Expand Down Expand Up @@ -335,12 +343,12 @@ def _handle_receive_pack_tail(self, proto, capabilities, progress=None):
if progress is None:
progress = lambda x: None
channel_callbacks = {2: progress}
if b'report-status' in capabilities:
if CAPABILITY_REPORT_STATUS in capabilities:
channel_callbacks[1] = PktLineParser(
self._report_status_parser.handle_packet).parse
self._read_side_band64k_data(proto, channel_callbacks)
else:
if b'report-status' in capabilities:
if CAPABILITY_REPORT_STATUS in capabilities:
for pkt in proto.read_pkt_seq():
self._report_status_parser.handle_packet(pkt)
if self._report_status_parser is not None:
Expand Down Expand Up @@ -401,7 +409,7 @@ def _handle_upload_pack_tail(self, proto, capabilities, graph_walker,
b'ready', b'continue', b'common'):
break
pkt = proto.read_pkt_line()
if b"side-band-64k" in capabilities:
if CAPABILITY_SIDE_BAND_64K in capabilities:
if progress is None:
# Just ignore progress data
progress = lambda x: None
Expand Down Expand Up @@ -451,7 +459,7 @@ def send_pack(self, path, determine_wants, generate_pack_contents,
old_refs, server_capabilities = read_pkt_refs(proto)
negotiated_capabilities = self._send_capabilities & server_capabilities

if b'report-status' in negotiated_capabilities:
if CAPABILITY_REPORT_STATUS in negotiated_capabilities:
self._report_status_parser = ReportStatusParser()
report_status_parser = self._report_status_parser

Expand All @@ -461,12 +469,12 @@ def send_pack(self, path, determine_wants, generate_pack_contents,
proto.write_pkt_line(None)
raise

if not b'delete-refs' in server_capabilities:
if not CAPABILITY_DELETE_REFS in server_capabilities:
# Server does not support deletions. Fail later.
new_refs = dict(orig_new_refs)
for ref, sha in orig_new_refs.items():
if sha == ZERO_SHA:
if b'report-status' in negotiated_capabilities:
if CAPABILITY_REPORT_STATUS in negotiated_capabilities:
report_status_parser._ref_statuses.append(
b'ng ' + sha + b' remote does not support deleting refs')
report_status_parser._ref_status_ok = False
Expand Down Expand Up @@ -1023,7 +1031,7 @@ def send_pack(self, path, determine_wants, generate_pack_contents,
b"git-receive-pack", url)
negotiated_capabilities = self._send_capabilities & server_capabilities

if b'report-status' in negotiated_capabilities:
if CAPABILITY_REPORT_STATUS in negotiated_capabilities:
self._report_status_parser = ReportStatusParser()

new_refs = determine_wants(dict(old_refs))
Expand Down
10 changes: 10 additions & 0 deletions dulwich/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
MULTI_ACK = 1
MULTI_ACK_DETAILED = 2

CAPABILITY_NO_PROGRESS = b'no-progress'
CAPABILITY_INCLUDE_TAG = b'include-tag'
CAPABILITY_OFS_DELTA = b'ofs-delta'
CAPABILITY_SIDE_BAND_64K = b'side-band-64k'
CAPABILITY_THIN_PACK = b'thin-pack'
CAPABILITY_MULTI_ACK = b'multi_ack'
CAPABILITY_MULTI_ACK_DETAILED = b'multi_ack_detailed'
CAPABILITY_REPORT_STATUS = b'report-status'
CAPABILITY_DELETE_REFS = b'delete-refs'
CAPABILITY_SHALLOW = b'shallow'

class ProtocolFile(object):
"""A dummy file for network ops that expect file-like objects."""
Expand Down
31 changes: 21 additions & 10 deletions dulwich/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
)
from dulwich.protocol import (
BufferedPktLineWriter,
CAPABILITY_DELETE_REFS,
CAPABILITY_INCLUDE_TAG,
CAPABILITY_MULTI_ACK_DETAILED,
CAPABILITY_MULTI_ACK,
CAPABILITY_NO_PROGRESS,
CAPABILITY_OFS_DELTA,
CAPABILITY_REPORT_STATUS,
CAPABILITY_SHALLOW,
CAPABILITY_SIDE_BAND_64K,
CAPABILITY_THIN_PACK,
MULTI_ACK,
MULTI_ACK_DETAILED,
Protocol,
Expand Down Expand Up @@ -195,7 +205,8 @@ def capabilities(cls):

@classmethod
def innocuous_capabilities(cls):
return (b"include-tag", b"thin-pack", b"no-progress", b"ofs-delta")
return (CAPABILITY_INCLUDE_TAG, CAPABILITY_THIN_PACK,
CAPABILITY_NO_PROGRESS, CAPABILITY_OFS_DELTA)

@classmethod
def required_capabilities(cls):
Expand Down Expand Up @@ -235,15 +246,15 @@ def __init__(self, backend, args, proto, http_req=None,

@classmethod
def capabilities(cls):
return (b"multi_ack_detailed", b"multi_ack", b"side-band-64k", b"thin-pack",
b"ofs-delta", b"no-progress", b"include-tag", b"shallow")
return (CAPABILITY_MULTI_ACK_DETAILED, CAPABILITY_MULTI_ACK, CAPABILITY_SIDE_BAND_64K, CAPABILITY_THIN_PACK,
CAPABILITY_OFS_DELTA, CAPABILITY_NO_PROGRESS, CAPABILITY_INCLUDE_TAG, CAPABILITY_SHALLOW)

@classmethod
def required_capabilities(cls):
return (b"side-band-64k", b"thin-pack", b"ofs-delta")
return (CAPABILITY_SIDE_BAND_64K, CAPABILITY_THIN_PACK, CAPABILITY_OFS_DELTA)

def progress(self, message):
if self.has_capability(b"no-progress"):
if self.has_capability(CAPABILITY_NO_PROGRESS):
return
self.proto.write_sideband(2, message)

Expand All @@ -257,7 +268,7 @@ def get_tagged(self, refs=None, repo=None):
:return: dict of peeled_sha -> tag_sha, where tag_sha is the sha of a
tag whose peeled value is peeled_sha.
"""
if not self.has_capability(b"include-tag"):
if not self.has_capability(CAPABILITY_INCLUDE_TAG):
return {}
if refs is None:
refs = self.repo.get_refs()
Expand Down Expand Up @@ -716,7 +727,7 @@ def __init__(self, backend, args, proto, http_req=None,

@classmethod
def capabilities(cls):
return (b"report-status", b"delete-refs", b"side-band-64k")
return (CAPABILITY_REPORT_STATUS, CAPABILITY_DELETE_REFS, CAPABILITY_SIDE_BAND_64K)

def _apply_pack(self, refs):
all_exceptions = (IOError, OSError, ChecksumMismatch, ApplyDeltaError,
Expand Down Expand Up @@ -748,7 +759,7 @@ def _apply_pack(self, refs):
ref_status = b'ok'
try:
if sha == ZERO_SHA:
if not b'delete-refs' in self.capabilities():
if not CAPABILITY_DELETE_REFS in self.capabilities():
raise GitProtocolError(
'Attempted to delete refs without delete-refs '
'capability.')
Expand All @@ -768,7 +779,7 @@ def _apply_pack(self, refs):
return status

def _report_status(self, status):
if self.has_capability(b'side-band-64k'):
if self.has_capability(CAPABILITY_SIDE_BAND_64K):
writer = BufferedPktLineWriter(
lambda d: self.proto.write_sideband(1, d))
write = writer.write
Expand Down Expand Up @@ -829,7 +840,7 @@ def handle(self):

# when we have read all the pack from the client, send a status report
# if the client asked for it
if self.has_capability(b'report-status'):
if self.has_capability(CAPABILITY_REPORT_STATUS):
self._report_status(status)


Expand Down

0 comments on commit 44e24e3

Please sign in to comment.