use crate::error::RailResult;
use crate::git::SystemGit;
pub fn detect_default_base_ref(git: &SystemGit) -> RailResult<String> {
let output = git
.git_cmd()
.args(["symbolic-ref", "refs/remotes/origin/HEAD", "--short"])
.output();
if let Ok(out) = output
&& out.status.success()
{
let symref = String::from_utf8_lossy(&out.stdout).trim().to_string();
if !symref.is_empty() {
return Ok(symref);
}
}
if git.resolve_reference("origin/main").is_ok() {
return Ok("origin/main".to_string());
}
Ok("HEAD~1".to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_base_prefers_remote_head_then_main_then_local_fallback() {
let directory = tempfile::tempdir().unwrap();
crate::git::init_repo(directory.path(), "main").unwrap();
let git = SystemGit::open(directory.path()).unwrap();
git.set_config("user.name", "Test User").unwrap();
git.set_config("user.email", "test@example.com").unwrap();
git.set_config("commit.gpgsign", "false").unwrap();
assert_eq!(detect_default_base_ref(&git).unwrap(), "HEAD~1");
let commit = git
.git_cmd()
.args(["commit", "--allow-empty", "-m", "initial"])
.output()
.unwrap();
assert!(commit.status.success(), "{commit:?}");
for name in ["main", "stable"] {
let output = git
.git_cmd()
.args(["update-ref", &format!("refs/remotes/origin/{name}"), "HEAD"])
.output()
.unwrap();
assert!(output.status.success(), "{output:?}");
}
assert_eq!(detect_default_base_ref(&git).unwrap(), "origin/main");
let output = git
.git_cmd()
.args(["symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/stable"])
.output()
.unwrap();
assert!(output.status.success(), "{output:?}");
assert_eq!(detect_default_base_ref(&git).unwrap(), "origin/stable");
}
}