From d916a952a73673f7f9c0c890606ab54f1bdb79a1 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 23 Nov 2026 16:31:47 +0000 Subject: [PATCH] Preserve slashes in default branch names for remote diffs (#63386) ## Why When the remote's default branch is `release/stable`, taking only the last segment of its ref can make Codex diff against `stable` and include changes already present on the default branch. ## What changed Strip the `refs/remotes//` prefix from the remote HEAD's symbolic ref to preserve the full branch name. ## Testing Add a regression test with both `origin/stable` and `origin/release/stable`. It checks that the diff uses the `release/stable` commit as its base and includes only the feature change. GitOrigin-RevId: 527cfe47b5fecb9c178f9cfeb095ec66c6219260 --- codex-rs/git-utils/src/diff_tests.rs | 34 ++++++++++++++++++++++++++++ codex-rs/git-utils/src/info.rs | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) 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()); } }