Skip to content

Commit 628b89b

Browse files
author
ztzhou
committed
fix(git): report update-ref exit 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 8cb863d commit 628b89b

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
@@ -564,17 +564,16 @@ impl GitCache {
564564
.stderr(Stdio::null())
565565
.spawn()
566566
.context("spawn git update-ref --stdin")?;
567-
child
568-
.stdin
569-
.take()
570-
.context("update-ref: no stdin")?
571-
.write_all(deletions.as_bytes())
572-
.await
573-
.context("write update-ref deletions")?;
567+
let mut stdin = child.stdin.take().context("update-ref: no stdin")?;
568+
let written = stdin.write_all(deletions.as_bytes()).await;
569+
drop(stdin); // EOF so update-ref commits the transaction
574570
let status = child.wait().await.context("git update-ref --stdin")?;
571+
// Report the exit status first: a child that fails early closes its
572+
// stdin, so the write error is only a symptom of that failure.
575573
if !status.success() {
576574
bail!("git update-ref --stdin failed for {}", repo.name);
577575
}
576+
written.context("write update-ref deletions")?;
578577
tracing::info!(
579578
repo = %repo.name,
580579
pruned = refs.len() - self.cfg.max_wants,
@@ -1468,6 +1467,33 @@ mod tests {
14681467
}
14691468
}
14701469

1470+
#[cfg(unix)]
1471+
#[tokio::test]
1472+
async fn prune_wants_reports_exit_status_when_update_ref_stops_reading() {
1473+
let (dir, repo, oids) = wanted_oids_fixture();
1474+
// Loose refs written directly: the deletion list must exceed any pipe
1475+
// buffer so the write blocks until the child exits and fails.
1476+
git_test_command(&repo.cache_dir, &["fetch", "-q", "origin", &oids[0]]);
1477+
let pins = repo.cache_dir.join(PROXY_WANTS_NS);
1478+
std::fs::create_dir_all(&pins).unwrap();
1479+
for i in 0..600 {
1480+
std::fs::write(pins.join(format!("{i:0>200}")), format!("{}\n", oids[0])).unwrap();
1481+
}
1482+
let binary = write_failing_git(
1483+
dir.path(),
1484+
"if [ \"$1\" = update-ref ]; then exit 7; fi\nexec git \"$@\"\n",
1485+
);
1486+
let mut cache = upload_pack_test_cache(binary.to_str().unwrap());
1487+
cache.cfg.max_wants = 1;
1488+
let error = cache.prune_wants(&repo).await.unwrap_err();
1489+
assert!(
1490+
error
1491+
.to_string()
1492+
.contains("git update-ref --stdin failed for repo.git"),
1493+
"{error}"
1494+
);
1495+
}
1496+
14711497
#[cfg(unix)]
14721498
#[tokio::test]
14731499
async fn non_interactive_git_commands_do_not_inherit_stdin() {

0 commit comments

Comments
 (0)