dulwich.protocol module

Generic functions for talking the git smart server protocol.

class dulwich.protocol.BufferedPktLineWriter(write, bufsize=65515)

Bases: object

Writer that wraps its data in pkt-lines and has an independent buffer.

Consecutive calls to write() wrap the data in a pkt-line and then buffers it until enough lines have been written such that their total length (including length prefix) reach the buffer size.

Initialize the BufferedPktLineWriter.

  • write – A write callback for the underlying writer.
  • bufsize – The internal buffer size, including length prefixes.

Flush all data from the buffer.


Write data, wrapping it in a pkt-line.

class dulwich.protocol.PktLineParser(handle_pkt)

Bases: object

Packet line parser that hands completed packets off to a callback.


Read back any unused data.


Parse a fragment of data and call back for any completed packets.

class dulwich.protocol.Protocol(read, write, close=None, report_activity=None)

Bases: object

Class for interacting with a remote git process over the wire.

Parts of the git wire protocol use ‘pkt-lines’ to communicate. A pkt-line consists of the length of the line as a 4-byte hex string, followed by the payload data. The length includes the 4-byte header. The special line ‘0000’ indicates the end of a section of input and is called a ‘flush-pkt’.

For details on the pkt-line format, see the cgit distribution:

Test whether the protocol stream has reached EOF.

Note that this refers to the actual stream EOF and not just a flush-pkt.

Returns:True if the stream is at EOF, False otherwise.

Read a command and some arguments from the git client

Only used for the TCP git protocol (git://).

Returns:A tuple of (command, [list of arguments]).

Reads a pkt-line from the remote git process.

This method may read from the readahead buffer; see unread_pkt_line.

Returns:The next string from the stream, without the length prefix, or None for a flush-pkt (‘0000’).

Read a sequence of pkt-lines from the remote git process.

Returns:Yields each line of data up to but not including the next flush-pkt.
send_cmd(cmd, *args)

Send a command and some arguments to a git server.

Only used for the TCP git protocol (git://).

  • cmd – The remote service to access.
  • args – List of arguments to send to remove service.

Unread a single line of data into the readahead buffer.

This method can be used to unread a single pkt-line into a fixed readahead buffer.

Parameters:data – The data to unread, without the length prefix.
Raises:ValueError – If more than one pkt-line is unread.

Return a writable file-like object for this protocol.


Sends a pkt-line to the remote git process.

Parameters:line – A string containing the data to send, without the length prefix.
write_sideband(channel, blob)

Write multiplexed data to the sideband.

  • channel – An int specifying the channel to write to.
  • blob – A blob of data (as a string) to send on this channel.
class dulwich.protocol.ProtocolFile(read, write)

Bases: object

A dummy file for network ops that expect file-like objects.

class dulwich.protocol.ReceivableProtocol(recv, write, close=None, report_activity=None, rbufsize=8192)

Bases: dulwich.protocol.Protocol

Variant of Protocol that allows reading up to a size without blocking.

This class has a recv() method that behaves like socket.recv() in addition to a read() method.

If you want to read n bytes from the wire and block until exactly n bytes (or EOF) are read, use read(n). If you want to read at most n bytes from the wire but don’t care if you get less, use recv(n). Note that recv(n) will still block until at least one byte is read.


Extract the ack type from a capabilities list.

dulwich.protocol.capability_symref(from_ref, to_ref)

Extract a capabilities list from a string, if present.

Parameters:text – String to extract from
Returns:Tuple with text with capabilities removed and list of capabilities

Extract a capabilities list from a want line, if present.

Note that want lines have capabilities separated from the rest of the line by a space instead of a null byte. Thus want lines have the form:

want obj-id cap1 cap2 …
Parameters:text – Want line to extract from
Returns:Tuple with text with capabilities removed and list of capabilities

Wrap data in a pkt-line.

Parameters:data – The data to wrap, as a str or None.
Returns:The data prefixed with its length in pkt-line format; if data was None, returns the flush-pkt (‘0000’).