use gitpoulpi_core::domain::{FastForwardOutcome, Oid, RepoRef};
use gitpoulpi_core::{Error, Result};
use crate::process::{run, run_async};
pub async fn branch_delete_remote(repo: &RepoRef, remote: &str, name: &str) -> Result<()> {
let target = format!("refs/heads/{name}");
run_async(&repo.root, &["push", remote, "--delete", &target]).await?;
crate::remote_ref::sync_tracking_ref(repo, remote, name, None);
Ok(())
}
pub fn branch_create(repo: &RepoRef, name: &str, at: Oid) -> Result<()> {
run(&repo.root, &["branch", name, &at.to_hex()])?;
Ok(())
}
pub fn branch_create_tracking(repo: &RepoRef, local_name: &str, upstream: &str) -> Result<()> {
let start = format!("refs/remotes/{upstream}");
run(
&repo.root,
&["checkout", "--track", "-b", local_name, &start],
)?;
Ok(())
}
pub fn branch_delete(repo: &RepoRef, name: &str) -> Result<()> {
run(&repo.root, &["branch", "-D", name])?;
Ok(())
}
pub fn branch_rename(repo: &RepoRef, old_name: &str, new_name: &str) -> Result<()> {
run(&repo.root, &["branch", "-m", old_name, new_name])?;
Ok(())
}
pub fn branch_set_upstream(repo: &RepoRef, branch: &str, upstream: Option<&str>) -> Result<()> {
let local = format!("refs/heads/{branch}");
match upstream {
Some(upstream) => {
let target = format!("refs/remotes/{upstream}");
run(
&repo.root,
&[
"branch",
&format!("--set-upstream-to={target}"),
"--end-of-options",
branch,
],
)?;
}
None => {
let current = run(
&repo.root,
&["for-each-ref", "--format=%(upstream)", &local],
)?;
if !current.trim().is_empty() {
run(
&repo.root,
&["branch", "--unset-upstream", "--end-of-options", branch],
)?;
}
}
}
Ok(())
}
pub fn fast_forward(repo: &RepoRef, branch: &str, target: Oid) -> Result<FastForwardOutcome> {
let local = format!("refs/heads/{branch}");
let from = run(
&repo.root,
&[
"rev-parse",
"--verify",
"--end-of-options",
&format!("{local}^{{commit}}"),
],
)
.map_err(|_| Error::Backend(format!("there is no local branch {branch}")))?;
let from = Oid::from_hex(from.trim())
.ok_or_else(|| Error::Backend(format!("unreadable tip for {branch}: {from}")))?;
let (from_hex, to_hex) = (from.to_hex(), target.to_hex());
if from == target || is_ancestor(repo, &to_hex, &from_hex)? {
return Ok(FastForwardOutcome::UpToDate);
}
if !is_ancestor(repo, &from_hex, &to_hex)? {
return Err(Error::NotFastForward);
}
let head = run(&repo.root, &["symbolic-ref", "-q", "HEAD"]).unwrap_or_default();
if head.trim() == local {
run(&repo.root, &["merge", "--ff-only", "--no-edit", &to_hex])?;
} else {
if let Some(elsewhere) = crate::worktree_list::worktrees(repo)?
.into_iter()
.find(|wt| wt.branch.as_deref() == Some(branch))
{
return Err(Error::Backend(format!(
"{branch} is checked out in the worktree at {} - fast-forward it there",
elsewhere.path.display()
)));
}
let message = format!("fast-forward {branch} to {}", &to_hex[..7]);
run(
&repo.root,
&["update-ref", "-m", &message, &local, &to_hex, &from_hex],
)?;
}
Ok(FastForwardOutcome::FastForwarded { from, to: target })
}
pub(crate) fn is_ancestor(repo: &RepoRef, ancestor: &str, descendant: &str) -> Result<bool> {
let out = crate::process::run_status(
&repo.root,
&["merge-base", "--is-ancestor", ancestor, descendant],
)?;
if out.success {
Ok(true)
} else if out.stderr.trim().is_empty() {
Ok(false)
} else {
Err(Error::Backend(format!(
"could not compare {ancestor} and {descendant}: {}",
out.stderr.trim()
)))
}
}
pub fn tag_create(
repo: &RepoRef,
name: &str,
at: Oid,
annotated_message: Option<&str>,
) -> Result<()> {
match annotated_message {
Some(message) => {
run(
&repo.root,
&["tag", "-a", name, "-m", message, &at.to_hex()],
)?;
}
None => {
run(&repo.root, &["tag", name, &at.to_hex()])?;
}
}
Ok(())
}
pub fn tag_delete(repo: &RepoRef, name: &str) -> Result<()> {
run(&repo.root, &["tag", "-d", name])?;
Ok(())
}