grit-protocol 0.5.33

Git smart protocol handlers for the grit implementation
Documentation
//! Receive-pack protocol handler (server side of push).
//!
//! Accepts pushes over smart HTTP by calling [`grit_lib::remote::serve::ReceivePack`]
//! in-process.

use std::io::Read;
use std::path::Path;

use grit_lib::config::environment::RepositoryOptions;

use crate::{
    run_receive_pack, run_receive_pack_to, run_receive_pack_with_session, Extensions,
    ReceiveRpcResult, Result, ServeSession,
};

/// Run receive-pack ref advertisement (for `GET /info/refs?service=git-receive-pack`).
///
/// Returns the raw pkt-line advertisement bytes.
///
/// # Parameters
///
/// - `repository_options`: environment used to load global/system config (hide refs,
///   receive policy).
///
/// # Errors
///
/// Fails when `repo_path` is not a repository or advertisement generation fails.
pub fn advertise_refs(repo_path: &Path, repository_options: &RepositoryOptions) -> Result<Vec<u8>> {
    run_receive_pack(
        repo_path,
        false,
        true,
        &mut std::io::empty(),
        repository_options,
        &Extensions::default(),
    )
}

/// [`advertise_refs`] with [`Extensions`] (a `pushBundle` endpoint).
///
/// # Errors
///
/// Same as [`advertise_refs`].
pub fn advertise_refs_with(
    repo_path: &Path,
    repository_options: &RepositoryOptions,
    extensions: &Extensions,
) -> Result<Vec<u8>> {
    run_receive_pack(
        repo_path,
        false,
        true,
        &mut std::io::empty(),
        repository_options,
        extensions,
    )
}

/// Like [`advertise_refs_with`], but writes pkt-lines to `output` as they are
/// produced instead of buffering the full advertisement in memory.
///
/// # Errors
///
/// Same as [`advertise_refs_with`].
pub fn advertise_refs_to(
    repo_path: &Path,
    repository_options: &RepositoryOptions,
    extensions: &Extensions,
    output: &mut dyn std::io::Write,
) -> Result<()> {
    run_receive_pack_to(
        repo_path,
        false,
        true,
        &mut std::io::empty(),
        repository_options,
        extensions,
        output,
    )
}

/// Run a stateless receive-pack RPC exchange (for `POST /git-receive-pack`) on a
/// request body that is already in memory.
///
/// Takes the request body (pkt-line commands + pack data) and returns
/// the response (report-status). Prefer [`stateless_rpc_from_reader`] for HTTP
/// bodies, which can be as large as the pushed pack.
///
/// # Errors
///
/// Fails when the repository cannot be opened or the push is rejected.
pub fn stateless_rpc(
    repo_path: &Path,
    request_body: &[u8],
    repository_options: &RepositoryOptions,
) -> Result<Vec<u8>> {
    stateless_rpc_from_reader(repo_path, &mut &*request_body, repository_options)
}

/// Run a stateless receive-pack RPC exchange, reading the request body from a
/// stream.
///
/// The pushed pack is consumed from `request_body` as it arrives, so the caller
/// never has to hold the whole HTTP body in memory. The only size limit is the
/// repository's `receive.maxInputSize`, as with `git http-backend`.
///
/// # Parameters
///
/// - `repo_path`: the repository receiving the push.
/// - `request_body`: the decoded request body (pkt-line commands, then the pack).
/// - `repository_options`: environment used to load global/system config (hide refs,
///   receive policy).
///
/// # Returns
///
/// The response body (report-status, sideband-framed when the client asked for it).
///
/// # Errors
///
/// Fails when the repository cannot be opened, the command list is malformed, or
/// reading the request stream fails before the commands are parsed. A pack that
/// cannot be read or stored is reported to the client as an unpack error instead.
pub fn stateless_rpc_from_reader(
    repo_path: &Path,
    request_body: &mut dyn Read,
    repository_options: &RepositoryOptions,
) -> Result<Vec<u8>> {
    stateless_rpc_with_session(
        repo_path,
        request_body,
        repository_options,
        &ServeSession::none(),
    )
    .map(|r| r.body)
}

/// Like [`stateless_rpc_from_reader`], with a [`ServeSession`] and a typed [`ReceiveRpcResult::outcome`].
///
/// # Errors
///
/// Same as [`stateless_rpc_from_reader`].
pub fn stateless_rpc_with_session(
    repo_path: &Path,
    request_body: &mut dyn Read,
    repository_options: &RepositoryOptions,
    session: &ServeSession,
) -> Result<ReceiveRpcResult> {
    let mut body = Vec::new();
    let outcome = run_receive_pack_with_session(
        repo_path,
        true,
        false,
        request_body,
        repository_options,
        &Extensions::default(),
        &mut body,
        session,
    )?;
    Ok(ReceiveRpcResult { body, outcome })
}