use crate::cli::output::{print_success, print_warning};
use std::path::Path;
use std::process::Command;
pub fn is_herdr_active() -> bool {
if std::env::var("HERDR_ENV").as_deref() != Ok("1") {
return false;
}
if std::env::var("CLAW_DISABLE_HERDR").is_ok() {
return false;
}
if std::env::var("CARGO_MANIFEST_DIR").is_ok() || std::env::var("RUST_TEST_THREADS").is_ok() {
return false;
}
true
}
pub fn open_worktree_in_herdr(repo_root: &Path, branch: &str) -> bool {
if !is_herdr_active() {
return false;
}
let repo_str = repo_root.to_string_lossy();
if repo_root.starts_with(std::env::temp_dir())
|| repo_str.contains("/tmp/")
|| repo_str.starts_with("/tmp")
{
return false;
}
let cwd = repo_root.to_str().unwrap_or(".");
match Command::new("herdr")
.args(["worktree", "open", "--cwd", cwd, "--branch", branch])
.status()
{
Ok(status) if status.success() => {
print_success(format!("Opened worktree in Herdr: {}", branch));
true
}
Ok(status) => {
print_warning(format!(
"Herdr worktree open exited with code: {:?}",
status.code()
));
false
}
Err(err) => {
print_warning(format!("Could not execute herdr: {}", err));
false
}
}
}