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/<remote>/` 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
This commit is contained in:
Charlie Marsh
2026-11-23 17:26:33 +00:00
committed by copyberry
parent af81d2770f
commit d916a952a7
2 changed files with 35 additions and 1 deletions
+34
View File
@@ -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");
+1 -1
View File
@@ -450,7 +450,7 @@ async fn get_default_branch(cwd: &Path) -> Option<String> {
&& 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());
}
}