Files
gitks/pack/receive_pack.rs
T
zhenyi ab32e8826e feat(pack): add raw advertise refs and stateless protocol support
- Add raw flag to AdvertiseRefsRequest to enable raw pkt-line output
- Implement advertise_refs_raw function that calls git upload-pack/receive-pack with --advertise-refs
- Add stateless flag to GitProtocolFeatures for HTTP smart protocol support
- Modify upload_pack and receive_pack to accept stateless parameter
- Update command construction to include --stateless-rpc flag when enabled
- Add raw_data field to AdvertiseRefsResponse for raw output
- Update pack cache key computation to include raw service differentiation
- Initialize raw field to false in all request creation calls
2026-06-08 21:46:31 +08:00

154 lines
5.5 KiB
Rust

use std::process::Stdio;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::process::Command;
use tokio_stream::StreamExt;
use tokio_stream::wrappers::ReceiverStream;
use crate::bare::GitBare;
use crate::pb::ReceivePackResponse;
impl GitBare {
/// Receive pack data using git-receive-pack with true concurrent streaming.
///
/// Client-streaming input → server-streaming output.
/// Stdin packets are forwarded to the child process as they arrive from the client,
/// while stdout is concurrently read and streamed back via a channel.
///
/// `stateless` enables `--stateless-rpc` for HTTP smart protocol.
/// Leave `false` for SSH (persistent connection).
pub async fn receive_pack(
&self,
stateless: bool,
input: impl tokio_stream::Stream<Item = Result<crate::pb::ReceivePackRequest, tonic::Status>>
+ Send
+ 'static,
) -> Result<ReceiverStream<Result<ReceivePackResponse, tonic::Status>>, tonic::Status> {
let bare_dir = self.bare_dir.to_string_lossy().into_owned();
tracing::info!(
repo = %bare_dir,
stateless = stateless,
"spawning git receive-pack subprocess"
);
let (tx, rx) = tokio::sync::mpsc::channel(16);
let stream = Box::pin(input);
tokio::spawn(async move {
let stream = stream;
let mut cmd = Command::new("git");
cmd.arg("--git-dir").arg(&bare_dir);
if stateless {
cmd.arg("--stateless-rpc");
}
cmd.arg("receive-pack").arg(&bare_dir);
let mut child = match cmd
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
{
Ok(c) => c,
Err(e) => {
let _ = tx
.send(Err(tonic::Status::internal(format!(
"failed to spawn git receive-pack: {e}"
))))
.await;
return;
}
};
let mut stdin = child.stdin.take();
let mut stdout = child.stdout.take();
let mut stderr = child.stderr.take();
let stdin_task = {
let mut stream = stream;
async move {
if let Some(mut stdin) = stdin.take() {
while let Some(result) = stream.next().await {
match result {
Ok(req) => {
if stdin.write_all(&req.packet).await.is_err() {
break;
}
if req.done {
break;
}
}
Err(_) => break,
}
}
drop(stdin);
}
}
};
let stdout_task = {
let tx = tx.clone();
async move {
if let Some(mut stdout) = stdout.take() {
let mut buf = vec![0u8; 65536];
loop {
match stdout.read(&mut buf).await {
Ok(0) => break,
Ok(n) => {
if tx
.send(Ok(ReceivePackResponse {
packet: buf[..n].to_vec(),
stderr: String::new(),
}))
.await
.is_err()
{
break;
}
}
Err(_) => break,
}
}
}
}
};
let stderr_task = {
let tx = tx.clone();
async move {
if let Some(mut stderr) = stderr.take() {
let mut s = String::new();
if stderr.read_to_string(&mut s).await.is_ok() && !s.is_empty() {
let _ = tx
.send(Ok(ReceivePackResponse {
packet: Vec::new(),
stderr: s,
}))
.await;
}
}
}
};
tokio::join!(stdin_task, stdout_task, stderr_task);
match child.wait().await {
Ok(status) if !status.success() => {
let _ = tx
.send(Err(tonic::Status::internal(
"git receive-pack exited with error",
)))
.await;
}
Err(e) => {
let _ = tx
.send(Err(tonic::Status::internal(format!("wait error: {e}"))))
.await;
}
_ => {}
}
});
Ok(ReceiverStream::new(rx))
}
}