Skip to content

Commit a44a197

Browse files
ztzhourolandjitsu
authored andcommitted
fix(git): report update-ref status on early exit
A child that exits before reading its input made the write fail with a broken pipe that hid the real failure. Report the exit status first. Assisted-by: Claude:claude-opus-4-8
1 parent 8c29df2 commit a44a197

1 file changed

Lines changed: 33 additions & 7 deletions

File tree

‎src/git.rs‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -640,17 +640,16 @@ impl GitCache {
640640
.stderr(Stdio::null())
641641
.spawn()
642642
.context("spawn git update-ref --stdin")?;
643-
child
644-
.stdin
645-
.take()
646-
.context("update-ref: no stdin")?
647-
.write_all(deletions.as_bytes())
648-
.await
649-
.context("write update-ref deletions")?;
643+
let mut stdin = child.stdin.take().context("update-ref: no stdin")?;
644+
let written = stdin.write_all(deletions.as_bytes()).await;
645+
drop(stdin); // EOF so update-ref commits the transaction
650646
let status = child.wait().await.context("git update-ref --stdin")?;
647+
// Report the exit status first: a child that fails early closes its
648+
// stdin, so the write error is only a symptom of that failure.
651649
if !status.success() {
652650
bail!("git update-ref --stdin failed for {}", repo.name);
653651
}
652+
written.context("write update-ref deletions")?;
654653
tracing::info!(
655654
repo = %repo.name,
656655
pruned = refs.len() - self.cfg.max_wants,
@@ -1900,6 +1899,33 @@ mod tests {
19001899
}
19011900
}
19021901

1902+
#[cfg(unix)]
1903+
#[tokio::test]
1904+
async fn prune_wants_reports_exit_status_when_update_ref_stops_reading() {
1905+
let (dir, repo, oids) = wanted_oids_fixture();
1906+
// Loose refs written directly: the deletion list must exceed any pipe
1907+
// buffer so the write blocks until the child exits and fails.
1908+
git_test_command(&repo.cache_dir, &["fetch", "-q", "origin", &oids[0]]);
1909+
let pins = repo.cache_dir.join(PROXY_WANTS_NS);
1910+
std::fs::create_dir_all(&pins).unwrap();
1911+
for i in 0..600 {
1912+
std::fs::write(pins.join(format!("{i:0>200}")), format!("{}\n", oids[0])).unwrap();
1913+
}
1914+
let binary = write_failing_git(
1915+
dir.path(),
1916+
"if [ \"$1\" = update-ref ]; then exit 7; fi\nexec git \"$@\"\n",
1917+
);
1918+
let mut cache = upload_pack_test_cache(binary.to_str().unwrap());
1919+
cache.cfg.max_wants = 1;
1920+
let error = cache.prune_wants(&repo).await.unwrap_err();
1921+
assert!(
1922+
error
1923+
.to_string()
1924+
.contains("git update-ref --stdin failed for repo.git"),
1925+
"{error}"
1926+
);
1927+
}
1928+
19031929
#[cfg(unix)]
19041930
#[tokio::test]
19051931
async fn non_interactive_git_commands_do_not_inherit_stdin() {

0 commit comments

Comments
 (0)