diff --git a/codex-rs/git-utils/src/diff_tests.rs b/codex-rs/git-utils/src/diff_tests.rs index 89186469e4..5a7cdc0da0 100644 --- a/codex-rs/git-utils/src/diff_tests.rs +++ b/codex-rs/git-utils/src/diff_tests.rs @@ -15,6 +15,40 @@ fn git(cwd: &Path, args: &[&str]) { assert!(output.status.success(), "Git {args:?} failed: {output:?}"); } +#[tokio::test] +async fn remote_diff_preserves_slashes_in_the_default_branch_name() { + let repo = tempfile::tempdir().expect("create repository"); + let cwd = repo.path(); + git(cwd, &["init", "-b", "feature"]); + git(cwd, &["config", "user.name", "Codex Tests"]); + git(cwd, &["config", "user.email", "codex-tests@example.com"]); + git(cwd, &["config", "commit.gpgsign", "false"]); + git( + cwd, + &["config", "core.hooksPath", super::DISABLED_HOOKS_PATH], + ); + git(cwd, &["remote", "add", "origin", "."]); + git(cwd, &["commit", "--allow-empty", "-m", "initial"]); + git(cwd, &["update-ref", "refs/remotes/origin/stable", "HEAD"]); + std::fs::write(cwd.join("release.txt"), "released\n").expect("write released file"); + git(cwd, &["add", "release.txt"]); + git(cwd, &["commit", "-m", "release"]); + git( + cwd, + &["update-ref", "refs/remotes/origin/release/stable", "HEAD"], + ); + git(cwd, &["remote", "set-head", "origin", "release/stable"]); + let release = super::get_head_commit_hash(cwd).await.expect("release SHA"); + std::fs::write(cwd.join("feature.txt"), "feature\n").expect("write feature file"); + git(cwd, &["add", "feature.txt"]); + git(cwd, &["commit", "-m", "unpushed"]); + + let diff = super::git_diff_to_remote(cwd).await.expect("remote diff"); + assert_eq!(diff.sha, release); + assert!(diff.diff.contains("feature.txt")); + assert!(!diff.diff.contains("release.txt")); +} + #[tokio::test] async fn tracked_and_untracked_patches_preserve_order_and_apply() { let repo = tempfile::tempdir().expect("create repository"); diff --git a/codex-rs/git-utils/src/info.rs b/codex-rs/git-utils/src/info.rs index b710fe3d7f..4592957433 100644 --- a/codex-rs/git-utils/src/info.rs +++ b/codex-rs/git-utils/src/info.rs @@ -450,7 +450,7 @@ async fn get_default_branch(cwd: &Path) -> Option { && let Ok(sym) = String::from_utf8(symref_output.stdout) { let trimmed = sym.trim(); - if let Some((_, name)) = trimmed.rsplit_once('/') { + if let Some(name) = trimmed.strip_prefix(&format!("refs/remotes/{remote}/")) { return Some(name.to_string()); } }