mod branch;
mod clone;
mod local_branch;
mod prune;
mod remote;
pub use branch::{branch_exists_remote, detect_default_branch};
pub use clone::{bare_clone, git_fetch};
pub use local_branch::{branch_exists_local, detect_local_default_branch};
pub use prune::git_worktree_prune;
pub use remote::get_remote_url;
use anyhow::{bail, Context, Result};
use std::path::Path;
use std::process::Command;
pub(super) fn git_cmd() -> Command {
let mut cmd = Command::new("git");
cmd.env_remove("GIT_DIR")
.env_remove("GIT_WORK_TREE")
.env_remove("GIT_INDEX_FILE");
cmd
}
pub fn create_local_worktree(
repo: &Path,
dest: &Path,
branch: &str,
branch_exists: bool,
) -> Result<()> {
let mut cmd = git_cmd();
cmd.args(["-C"]).arg(repo).arg("worktree").arg("add");
if branch_exists {
cmd.arg(dest).arg(branch);
} else {
cmd.arg(dest).arg("-b").arg(branch);
}
let status = cmd.status().context("Failed to run `git worktree add`")?;
if !status.success() {
bail!("git worktree add failed for branch {branch}"); }
Ok(())
}
pub fn create_worktree(
bare: &Path,
dest: &Path,
branch: &str,
base_branch: &str,
branch_exists: bool,
) -> Result<()> {
let mut cmd = git_cmd();
cmd.args(["-C"]).arg(bare).arg("worktree").arg("add");
if branch_exists {
cmd.arg(dest).arg(branch);
} else {
cmd.arg(dest)
.arg("-b")
.arg(branch)
.arg(format!("origin/{base_branch}"));
}
let status = cmd.status().context("Failed to run `git worktree add`")?;
if !status.success() {
bail!("git worktree add failed for branch {branch}"); }
Ok(())
}