Skip to content

Bound git diff output while streaming - #146

Open
karansaini46 wants to merge 1 commit into
prooflinedev:mainfrom
karansaini46:bound-git-diff-output
Open

karansaini46 wants to merge 1 commit into
prooflinedev:mainfrom
karansaini46:bound-git-diff-output

Conversation

@karansaini46

Copy link
Copy Markdown

Problem

Proofline rejects patches over 5 MiB (MAX_PATCH_BYTES) and 3,000 files (MAX_PATCH_FILES), but diff() in src/git.rs was still calling git_in(), which uses tokio::process::Command::output().await. Because Command::output() buffers all of git's stdout and stderr until EOF before returning, an oversized diff (such as generated fixtures, large assets, or huge refactors) gets read into memory in full before any size or file-count check runs.

On a ~254 MiB (25 × 10 MiB) multi-file staged diff locally, collecting full git diff stdout pushed VmHWM to ~525 MiB before failing. Even for patches within the 5 MiB limit, diff() also cloned patch.to_vec() while validating UTF-8.

What changed

  • Left git_in() alone: All metadata and index callers (resolve, named_files, line_in, git add, check_one_path) keep their existing behaviour.
  • Streamed diff() incrementally: Added a dedicated diff_output / diff_command_output path in src/git.rs that spawns git with piped stdio and .kill_on_drop(true), draining stdout and stderr concurrently with tokio::try_join! so neither pipe buffer can stall the child. (stderr retains up to 64 KiB for error messages and drains the rest to EOF.)
  • Counted metadata and patch separately: read_diff_stdout walks the --raw -z header records using the same status rules as diff_paths() (one path for A/D/M/T, two paths for R/C). It aborts immediately past 3,000 entries and caps the metadata prefix at 32 MiB (enough for 3,000 renamed entries at POSIX PATH_MAX). Once the empty header separator ends metadata, the remaining bytes move into a separate patch buffer so --raw metadata overhead never pushes a valid <= 5 MiB patch over the limit, and String::from_utf8(patch) takes ownership without patch.to_vec().
  • Killed and reaped oversized git processes: When a limit trips mid-stream, tokio::try_join! cancels stderr draining and drops the pipe ends, then terminate_and_reap calls child.start_kill() and child.wait().await before returning the limit error.
  • Kept GitError signatures intact: GitError::TooLarge(usize) and GitError::TooManyFiles(usize) keep their types, with display wording updated to "larger than {0} MiB" (5) and "more than {0} files" (3000) since early termination intentionally does not read the full unstreamed size.

Testing & verification

Added unit tests in src/git.rs (below, exact 5 MiB, 5 MiB + 1 B, and 12 MiB patches; 3,000 vs 3,001 changed files; 2,500 long-path files alongside a ~3.75 MiB patch; renames/copy-configs/submodules; chunked parser boundaries, corrupt metadata, stderr floods, and non-zero Git errors; plus a Unix test checking child kill and zombie reaping) and an end-to-end integration test in tests/local_patch.rs verifying 6 MiB local diffs exit locally with zero HTTP requests.

Ran on Linux (x86_64, Rust 1.100.0):

  • cargo test --locked --lib git::tests — 9 passed
  • cargo test --locked --test local_patch — 16 passed
  • cargo fmt --check, cargo clippy --locked --all-targets -- -D warnings, cargo deny check — all passed cleanly
  • cargo test --locked — 174 passed, 0 failed, 1 upstream #[ignore] (tests/hooks.rs, requires pre-commit >= 3.2.0 on PATH)
  • Peak VmHWM on the ~254 MiB multi-file staged diff dropped from ~525.6 MiB (unbounded buffer) to ~20.2 MiB (21,920 KiB via /usr/bin/time -v, 0.08s)

One note on open PR #132 (credential preflight on refused diffs): this change leaves preflight::check on accepted diffs unchanged and keeps (raw, patch) separate within 5 MiB; if #132 lands alongside this, any preflight scan of refused oversized patches would be bounded to that captured prefix unless streamed. Runtime and ps reaping checks were executed on Linux; macOS and Windows (TerminateProcess) rely on Tokio's cross-platform child APIs and were not executed locally.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant