diff --git a/dulwich/client.py b/dulwich/client.py index 412d83ed7..58e34ab09 100644 --- a/dulwich/client.py +++ b/dulwich/client.py @@ -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, @@ -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): @@ -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): @@ -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: @@ -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 @@ -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 @@ -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 @@ -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)) diff --git a/dulwich/protocol.py b/dulwich/protocol.py index 33b5db8b5..a59ee316a 100644 --- a/dulwich/protocol.py +++ b/dulwich/protocol.py @@ -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.""" diff --git a/dulwich/server.py b/dulwich/server.py index 2c7f936ac..8ce0344f9 100644 --- a/dulwich/server.py +++ b/dulwich/server.py @@ -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, @@ -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): @@ -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) @@ -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() @@ -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, @@ -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.') @@ -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 @@ -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)