1use std::collections::HashMap;
4use std::path::{Path, PathBuf};
5use std::sync::LazyLock;
6
7use regex::Regex;
8use serde::{Deserialize, Serialize};
9
10static UNSAFE_RE: LazyLock<Regex> =
12 LazyLock::new(|| Regex::new(r#"[/<>:"|?*\\#@&;$`!~%^()\[\]{}=+]+"#).unwrap());
13static WHITESPACE_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s+").unwrap());
14static MULTI_HYPHEN_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"-+").unwrap());
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
18#[serde(rename_all = "kebab-case")]
19pub enum LaunchMethod {
20 Foreground,
21 Detach,
22 ItermWindow,
24 ItermTab,
25 ItermPaneH,
26 ItermPaneV,
27 Tmux,
29 TmuxWindow,
30 TmuxPaneH,
31 TmuxPaneV,
32 Zellij,
34 ZellijTab,
35 ZellijPaneH,
36 ZellijPaneV,
37 WeztermWindow,
39 WeztermTab,
40 WeztermPaneH,
41 WeztermPaneV,
42 WeztermTabBg,
43}
44
45impl LaunchMethod {
46 pub fn as_str(&self) -> &'static str {
48 match self {
49 Self::Foreground => "foreground",
50 Self::Detach => "detach",
51 Self::ItermWindow => "iterm-window",
52 Self::ItermTab => "iterm-tab",
53 Self::ItermPaneH => "iterm-pane-h",
54 Self::ItermPaneV => "iterm-pane-v",
55 Self::Tmux => "tmux",
56 Self::TmuxWindow => "tmux-window",
57 Self::TmuxPaneH => "tmux-pane-h",
58 Self::TmuxPaneV => "tmux-pane-v",
59 Self::Zellij => "zellij",
60 Self::ZellijTab => "zellij-tab",
61 Self::ZellijPaneH => "zellij-pane-h",
62 Self::ZellijPaneV => "zellij-pane-v",
63 Self::WeztermWindow => "wezterm-window",
64 Self::WeztermTab => "wezterm-tab",
65 Self::WeztermPaneH => "wezterm-pane-h",
66 Self::WeztermPaneV => "wezterm-pane-v",
67 Self::WeztermTabBg => "wezterm-tab-bg",
68 }
69 }
70
71 pub fn from_str_opt(s: &str) -> Option<Self> {
73 match s {
74 "foreground" => Some(Self::Foreground),
75 "detach" => Some(Self::Detach),
76 "iterm-window" => Some(Self::ItermWindow),
77 "iterm-tab" => Some(Self::ItermTab),
78 "iterm-pane-h" => Some(Self::ItermPaneH),
79 "iterm-pane-v" => Some(Self::ItermPaneV),
80 "tmux" => Some(Self::Tmux),
81 "tmux-window" => Some(Self::TmuxWindow),
82 "tmux-pane-h" => Some(Self::TmuxPaneH),
83 "tmux-pane-v" => Some(Self::TmuxPaneV),
84 "zellij" => Some(Self::Zellij),
85 "zellij-tab" => Some(Self::ZellijTab),
86 "zellij-pane-h" => Some(Self::ZellijPaneH),
87 "zellij-pane-v" => Some(Self::ZellijPaneV),
88 "wezterm-window" => Some(Self::WeztermWindow),
89 "wezterm-tab" => Some(Self::WeztermTab),
90 "wezterm-pane-h" => Some(Self::WeztermPaneH),
91 "wezterm-pane-v" => Some(Self::WeztermPaneV),
92 "wezterm-tab-bg" => Some(Self::WeztermTabBg),
93 _ => None,
94 }
95 }
96}
97
98impl LaunchMethod {
99 pub fn display_name(&self) -> &'static str {
101 match self {
102 Self::Foreground => "Foreground",
103 Self::Detach => "Detach (background)",
104 Self::ItermWindow => "iTerm2 — New Window",
105 Self::ItermTab => "iTerm2 — New Tab",
106 Self::ItermPaneH => "iTerm2 — Horizontal Pane",
107 Self::ItermPaneV => "iTerm2 — Vertical Pane",
108 Self::Tmux => "tmux — New Session",
109 Self::TmuxWindow => "tmux — New Window",
110 Self::TmuxPaneH => "tmux — Horizontal Pane",
111 Self::TmuxPaneV => "tmux — Vertical Pane",
112 Self::Zellij => "Zellij — New Session",
113 Self::ZellijTab => "Zellij — New Tab",
114 Self::ZellijPaneH => "Zellij — Horizontal Pane",
115 Self::ZellijPaneV => "Zellij — Vertical Pane",
116 Self::WeztermWindow => "WezTerm — New Window",
117 Self::WeztermTab => "WezTerm — New Tab",
118 Self::WeztermPaneH => "WezTerm — Horizontal Pane",
119 Self::WeztermPaneV => "WezTerm — Vertical Pane",
120 Self::WeztermTabBg => "WezTerm — New Tab (Background)",
121 }
122 }
123}
124
125impl std::fmt::Display for LaunchMethod {
126 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127 f.write_str(self.as_str())
128 }
129}
130
131pub fn launch_method_aliases() -> HashMap<&'static str, &'static str> {
136 HashMap::from([
137 ("fg", "foreground"),
138 ("d", "detach"),
139 ("i-w", "iterm-window"),
141 ("i-t", "iterm-tab"),
142 ("i-p-h", "iterm-pane-h"),
143 ("i-p-v", "iterm-pane-v"),
144 ("t", "tmux"),
146 ("t-w", "tmux-window"),
147 ("t-p-h", "tmux-pane-h"),
148 ("t-p-v", "tmux-pane-v"),
149 ("z", "zellij"),
151 ("z-t", "zellij-tab"),
152 ("z-p-h", "zellij-pane-h"),
153 ("z-p-v", "zellij-pane-v"),
154 ("w-w", "wezterm-window"),
156 ("w-t", "wezterm-tab"),
157 ("w-p-h", "wezterm-pane-h"),
158 ("w-p-v", "wezterm-pane-v"),
159 ("w-t-b", "wezterm-tab-bg"),
160 ])
161}
162
163pub const HOOK_EVENTS: &[&str] = &[
165 "worktree.pre_create",
166 "worktree.post_create",
167 "worktree.pre_delete",
168 "worktree.post_delete",
169 "merge.pre",
170 "merge.post",
171 "pr.pre",
172 "pr.post",
173 "resume.pre",
174 "resume.post",
175 "sync.pre",
176 "sync.post",
177];
178
179pub const PRESET_NAMES: &[&str] = &[
181 "claude",
182 "claude-remote",
183 "claude-yolo",
184 "claude-yolo-remote",
185 "codex",
186 "codex-yolo",
187 "no-op",
188];
189
190pub fn all_term_values() -> Vec<&'static str> {
192 let mut values: Vec<&str> = vec![
193 "foreground",
194 "detach",
195 "iterm-window",
196 "iterm-tab",
197 "iterm-pane-h",
198 "iterm-pane-v",
199 "tmux",
200 "tmux-window",
201 "tmux-pane-h",
202 "tmux-pane-v",
203 "zellij",
204 "zellij-tab",
205 "zellij-pane-h",
206 "zellij-pane-v",
207 "wezterm-window",
208 "wezterm-tab",
209 "wezterm-tab-bg",
210 "wezterm-pane-h",
211 "wezterm-pane-v",
212 ];
213 for alias in launch_method_aliases().keys() {
214 values.push(alias);
215 }
216 values.sort();
217 values
218}
219
220pub const SECS_PER_DAY: u64 = 86400;
222
223pub const SECS_PER_DAY_F64: f64 = 86400.0;
225
226pub const MIN_GIT_VERSION: &str = "2.31.0";
228
229pub const MIN_GIT_VERSION_MAJOR: u32 = 2;
231
232pub const MIN_GIT_VERSION_MINOR: u32 = 31;
234
235pub const AI_TOOL_TIMEOUT_SECS: u64 = 60;
237
238pub const AI_TOOL_POLL_MS: u64 = 100;
240
241pub const MAX_SESSION_NAME_LENGTH: usize = 50;
244
245pub const CLAUDE_SESSION_PREFIX_LENGTH: usize = 200;
247
248pub const CONFIG_KEY_BASE_BRANCH: &str = "branch.{}.worktreeBase";
250pub const CONFIG_KEY_BASE_PATH: &str = "worktree.{}.basePath";
251pub const CONFIG_KEY_INTENDED_BRANCH: &str = "worktree.{}.intendedBranch";
252
253pub fn format_config_key(template: &str, branch: &str) -> String {
255 template.replace("{}", branch)
256}
257
258pub fn home_dir_or_fallback() -> PathBuf {
260 dirs::home_dir().unwrap_or_else(|| PathBuf::from("."))
261}
262
263pub fn path_age_days(path: &Path) -> Option<f64> {
265 let mtime = path.metadata().and_then(|m| m.modified()).ok()?;
266 std::time::SystemTime::now()
267 .duration_since(mtime)
268 .ok()
269 .map(|d| d.as_secs_f64() / SECS_PER_DAY_F64)
270}
271
272pub fn version_meets_minimum(version_str: &str, min_major: u32, min_minor: u32) -> bool {
274 let parts: Vec<u32> = version_str
275 .split('.')
276 .filter_map(|p| p.parse().ok())
277 .collect();
278 parts.len() >= 2 && (parts[0] > min_major || (parts[0] == min_major && parts[1] >= min_minor))
279}
280
281pub fn sanitize_branch_name(branch_name: &str) -> String {
294 let safe = UNSAFE_RE.replace_all(branch_name, "-");
295 let safe = WHITESPACE_RE.replace_all(&safe, "-");
296 let safe = MULTI_HYPHEN_RE.replace_all(&safe, "-");
297 let safe = safe.trim_matches('-');
298
299 if safe.is_empty() {
300 "worktree".to_string()
301 } else {
302 safe.to_string()
303 }
304}
305
306pub fn default_worktree_path(repo_path: &Path, branch_name: &str) -> PathBuf {
308 let repo_path = strip_unc(
309 repo_path
310 .canonicalize()
311 .unwrap_or_else(|_| repo_path.to_path_buf()),
312 );
313 let safe_branch = sanitize_branch_name(branch_name);
314 let repo_name = repo_path
315 .file_name()
316 .map(|n| n.to_string_lossy().to_string())
317 .unwrap_or_else(|| "repo".to_string());
318
319 repo_path
320 .parent()
321 .unwrap_or(repo_path.as_path())
322 .join(format!("{}-{}", repo_name, safe_branch))
323}
324
325pub fn strip_unc(path: PathBuf) -> PathBuf {
328 #[cfg(target_os = "windows")]
329 {
330 let s = path.to_string_lossy();
331 if let Some(stripped) = s.strip_prefix(r"\\?\") {
332 return PathBuf::from(stripped);
333 }
334 }
335 path
336}
337
338#[cfg(test)]
339mod tests {
340 use super::*;
341
342 #[test]
343 fn test_sanitize_branch_name() {
344 assert_eq!(sanitize_branch_name("feat/auth"), "feat-auth");
345 assert_eq!(sanitize_branch_name("bugfix/issue-123"), "bugfix-issue-123");
346 assert_eq!(
347 sanitize_branch_name("feature/user@login"),
348 "feature-user-login"
349 );
350 assert_eq!(sanitize_branch_name("hotfix/v2.0"), "hotfix-v2.0");
351 assert_eq!(sanitize_branch_name("///"), "worktree");
352 assert_eq!(sanitize_branch_name(""), "worktree");
353 assert_eq!(sanitize_branch_name("simple"), "simple");
354 }
355
356 #[test]
357 fn test_launch_method_roundtrip() {
358 for method in [
359 LaunchMethod::Foreground,
360 LaunchMethod::Detach,
361 LaunchMethod::ItermWindow,
362 LaunchMethod::Tmux,
363 LaunchMethod::Zellij,
364 LaunchMethod::WeztermTab,
365 ] {
366 let s = method.as_str();
367 assert_eq!(LaunchMethod::from_str_opt(s), Some(method));
368 }
369 }
370
371 #[test]
372 fn test_format_config_key() {
373 assert_eq!(
374 format_config_key(CONFIG_KEY_BASE_BRANCH, "fix-auth"),
375 "branch.fix-auth.worktreeBase"
376 );
377 }
378
379 #[test]
380 fn test_home_dir_or_fallback() {
381 let home = home_dir_or_fallback();
382 assert!(!home.as_os_str().is_empty());
384 }
385
386 #[test]
387 fn test_path_age_days() {
388 assert!(path_age_days(std::path::Path::new("/nonexistent/path")).is_none());
390
391 let tmp = std::env::temp_dir();
393 if let Some(age) = path_age_days(&tmp) {
394 assert!(age >= 0.0);
395 }
396 }
397
398 #[test]
399 fn test_all_term_values_contains_canonical_and_aliases() {
400 let values = all_term_values();
401 assert!(
403 values.len() >= 36,
404 "expected ≥36 term values, got {}",
405 values.len()
406 );
407 assert!(values.contains(&"foreground"));
409 assert!(values.contains(&"tmux"));
410 assert!(values.contains(&"wezterm-tab"));
411 assert!(values.contains(&"fg"));
413 assert!(values.contains(&"t"));
414 assert!(values.contains(&"w-t"));
415 }
416
417 #[test]
418 fn test_hook_events_not_empty() {
419 assert!(!HOOK_EVENTS.is_empty());
420 assert!(HOOK_EVENTS.contains(&"worktree.post_create"));
421 assert!(HOOK_EVENTS.contains(&"merge.pre"));
422 }
423
424 #[test]
425 fn test_preset_names_not_empty() {
426 assert!(!PRESET_NAMES.is_empty());
427 assert!(PRESET_NAMES.contains(&"claude"));
428 assert!(PRESET_NAMES.contains(&"codex"));
429 assert!(PRESET_NAMES.contains(&"no-op"));
430 }
431
432 #[test]
433 fn test_version_meets_minimum() {
434 assert!(version_meets_minimum("2.31.0", 2, 31));
435 assert!(version_meets_minimum("2.40.0", 2, 31));
436 assert!(version_meets_minimum("3.0.0", 2, 31));
437 assert!(!version_meets_minimum("2.30.0", 2, 31));
438 assert!(!version_meets_minimum("1.99.0", 2, 31));
439 assert!(!version_meets_minimum("", 2, 31));
440 assert!(!version_meets_minimum("2", 2, 31));
441 }
442}