use std::path::Path;
use worktrunk::HookType;
use worktrunk::git::Repository;
use worktrunk::path::to_posix_path;
use crate::commands::command_executor::CommandContext;
use crate::commands::command_executor::FailureStrategy;
use crate::commands::hooks::execute_hook;
impl<'a> CommandContext<'a> {
pub fn execute_pre_start_commands(&self, extra_vars: &[(&str, &str)]) -> anyhow::Result<()> {
execute_hook(
self,
HookType::PreStart,
extra_vars,
FailureStrategy::FailFast,
&[],
crate::output::post_hook_display_path(self.worktree_path),
)
}
}
pub(crate) struct PostRemoveContext {
worktree_path_str: String,
worktree_name: String,
commit: String,
short_commit: String,
target_path_str: String,
target_branch: String,
}
impl PostRemoveContext {
pub fn new(
removed_worktree_path: &Path,
removed_commit: Option<&str>,
main_path: &Path,
repo: &Repository,
) -> Self {
let worktree_path_str = to_posix_path(&removed_worktree_path.to_string_lossy());
let worktree_name = removed_worktree_path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("unknown")
.to_string();
let commit = removed_commit.unwrap_or("").to_string();
let short_commit = if commit.len() >= 7 {
commit[..7].to_string()
} else {
commit.clone()
};
let target_path_str = to_posix_path(&main_path.to_string_lossy());
let target_branch = repo
.worktree_at(main_path)
.branch()
.ok()
.flatten()
.unwrap_or_default();
Self {
worktree_path_str,
worktree_name,
commit,
short_commit,
target_path_str,
target_branch,
}
}
pub fn extra_vars<'a>(&'a self, removed_branch: &'a str) -> Vec<(&'a str, &'a str)> {
vec![
("branch", removed_branch),
("worktree_path", &self.worktree_path_str),
("worktree", &self.worktree_path_str), ("worktree_name", &self.worktree_name),
("commit", &self.commit),
("short_commit", &self.short_commit),
("target", &self.target_branch),
("target_worktree_path", &self.target_path_str),
]
}
}