package git

  1. Overview
  2. Docs

Module for decoding Git pkt lines, as specified at https://github.com/git/git/blob/master/Documentation/technical/protocol-common.txt

We define a "packet line" (aka a "packet") as

| 4 bytes || (enc-pkt-len)-4 | enc-pkt-len pkt-content |------- pkt-len ------|

Example: "0009done\n" where enc-pkt-len = 4 and pkt-content = "done" given we usually trim LF ("\n").

"Encoded" packet length, enc-pkt-len, is the first 4 bytes in the packet that encode the length of the packet in hex. It can have specific values of 0, 1, 2 to encode flush, delimiter, and message (response end) packets respectively. Otherwise, it should be >= 4, i.e., 4 length bytes + the length of the packet content.

In the docs, we define min_pkt_len = 4 as in specs.

type decoder = {
  1. buffer : bytes;
  2. mutable pos : int;
  3. mutable max : int;
}
val io_buffer_size : int
val create : unit -> decoder
val of_string : string -> decoder
val end_of_input : decoder -> int
type error = [
  1. | `End_of_input
  2. | `Expected_char of char
  3. | `Unexpected_char of char
  4. | `Expected_string of string
  5. | `Expected_eol
  6. | `Expected_eol_or_space
  7. | `Unexpected_end_of_input
  8. | `No_enough_space
  9. | `Assert_predicate of char -> bool
  10. | `Invalid_pkt_line of string
]
val pp_error : error Fmt.t
type 'err info = {
  1. error : 'err;
  2. buffer : bytes;
  3. committed : int;
}
exception Leave of error info
type ('v, 'err) state =
  1. | Done of 'v
  2. | Read of {
    1. buffer : bytes;
    2. off : int;
    3. len : int;
    4. continue : int -> ('v, 'err) state;
    5. eof : unit -> ('v, 'err) state;
    }
  3. | Error of 'err info
val return : 'v -> decoder -> ('v, 'err) state
val bind : ('a, 'b) state -> f:('a -> ('c, 'b) state) -> ('c, 'b) state
val (>>=) : ('a, 'b) state -> ('a -> ('c, 'b) state) -> ('c, 'b) state
val leave_with : decoder -> error -> 'never

leave_with d error raises Leave { error; buffer = d.buffer; committed = d.pos }

val safe : (decoder -> ('v, [> error ] as 'err) state) -> decoder -> ('v, 'err) state

safe k decoder wraps a call k decoder in a try-with block; if exception Leave err is raised, the function returns Error of err

val fail : decoder -> [> error ] as 'err -> ('v, 'err) state
val peek_char : decoder -> char option
val string : string -> decoder -> unit
val junk_char : decoder -> unit
val while1 : (char -> bool) -> decoder -> bytes * int * int
  • returns

    decoder.buffer, updated decoder.pos, # of bytes read

val at_least_one_pkt : decoder -> bool

returns true if decoder.max - decoder.pos is >= min_pkt_len and >= pkt_len, where pkt_len is the length of a pkt line starting at decoder.pos.

val at_least_one_line : decoder -> bool
val prompt : ?strict:bool -> (decoder -> ('v, [> error ] as 'err) state) -> decoder -> ('v, 'err) state
val peek_while_eol : decoder -> bytes * int * int
val peek_while_eol_or_space : decoder -> bytes * int * int
val peek_pkt : decoder -> bytes * int * int
val junk_pkt : decoder -> unit

increase decoder.pos by max min_pkt_len pkt_len, where pkt_len is the length of the pkt line starting at the current value of decoder.pos (before increasing) and min_pkt_len = 4.

  • raises Invalid_argument

    if there aren't 4 bytes representing the length

val prompt_pkt : ?strict:bool -> (decoder -> ('a, [> error ] as 'b) state) -> decoder -> ('a, 'b) state

/

val pkt_len_unsafe : decoder -> int
OCaml

Innovation. Community. Security.