| 1 | //! Path resolution for the per-workspace snapshot side-repos. |
| 2 | //! |
| 3 | //! Snapshots live in `~/.deepseek/snapshots/<project_hash>/<worktree_hash>/`. |
| 4 | //! The two-level hash split lets us snapshot multiple worktrees of the same |
| 5 | //! project independently — `git worktree list` users won't get cross-talk |
| 6 | //! between feature branches. |
| 7 | |
| 8 | use std::io; |
| 9 | use std::path::{Path, PathBuf}; |
| 10 | |
| 11 | /// Compute the snapshot directory for a given workspace path. |
| 12 | /// |
| 13 | /// Returns `~/.deepseek/snapshots/<project_hash>/<worktree_hash>/`. The |
| 14 | /// caller is responsible for creating it on disk; we purposefully don't |
| 15 | /// touch the filesystem here so this is cheap to call repeatedly. |
| 16 | /// |
| 17 | /// The `project_hash` is derived from the canonicalized workspace path |
| 18 | /// after stripping any `.worktrees/<name>` suffix — multiple worktrees |
| 19 | /// of the same repo share the same `project_hash` so users can browse |
| 20 | /// snapshots cross-worktree if they want, but the `worktree_hash` keeps |
| 21 | /// commits isolated by default. |
| 22 | pub fn snapshot_dir_for(workspace: &Path) -> PathBuf { |
| 23 | snapshot_dir_with_home(workspace, dirs::home_dir()) |
| 24 | } |
| 25 | |
| 26 | /// Same as [`snapshot_dir_for`] but with an injectable home directory. |
| 27 | /// Used by tests so we never touch the user's real `~/.deepseek/`. |
| 28 | pub fn snapshot_dir_with_home(workspace: &Path, home: Option<PathBuf>) -> PathBuf { |
| 29 | let home = home.unwrap_or_else(|| PathBuf::from(".")); |
| 30 | let canonical = workspace |
| 31 | .canonicalize() |
| 32 | .unwrap_or_else(|_| workspace.to_path_buf()); |
| 33 | let project_root = strip_worktree_suffix(&canonical); |
| 34 | let project_hash = stable_hex(&project_root); |
| 35 | let worktree_hash = stable_hex(&canonical); |
| 36 | home.join(".deepseek") |
| 37 | .join("snapshots") |
| 38 | .join(project_hash) |
| 39 | .join(worktree_hash) |
| 40 | } |
| 41 | |
| 42 | /// Resolve the `.git` directory inside the snapshot dir. |
| 43 | pub fn snapshot_git_dir(workspace: &Path) -> PathBuf { |
| 44 | snapshot_dir_for(workspace).join(".git") |
| 45 | } |
| 46 | |
| 47 | /// Ensure the snapshot dir exists on disk and return its path. |
| 48 | pub fn ensure_snapshot_dir(workspace: &Path) -> io::Result<PathBuf> { |
| 49 | let dir = snapshot_dir_for(workspace); |
| 50 | std::fs::create_dir_all(&dir)?; |
| 51 | Ok(dir) |
| 52 | } |
| 53 | |
| 54 | /// Strip a trailing `.worktrees/<name>` segment so all worktrees of the |
| 55 | /// same checkout share a `project_hash`. If the path doesn't look like a |
| 56 | /// worktree it's returned unchanged. |
| 57 | fn strip_worktree_suffix(path: &Path) -> PathBuf { |
| 58 | let mut components: Vec<_> = path.components().collect(); |
| 59 | if components.len() >= 2 |
| 60 | && let Some(parent) = components.get(components.len() - 2) |
| 61 | && parent.as_os_str() == ".worktrees" |
| 62 | { |
| 63 | components.truncate(components.len() - 2); |
| 64 | let mut p = PathBuf::new(); |
| 65 | for c in components { |
| 66 | p.push(c.as_os_str()); |
| 67 | } |
| 68 | return p; |
| 69 | } |
| 70 | path.to_path_buf() |
| 71 | } |
| 72 | |
| 73 | /// Hex-encoded deterministic FNV-1a digest. This is only a directory tag, not |
| 74 | /// a security boundary, but it must remain stable across process launches. |
| 75 | fn stable_hex(path: &Path) -> String { |
| 76 | let mut hash = 0xcbf2_9ce4_8422_2325u64; |
| 77 | for byte in path.to_string_lossy().as_bytes() { |
| 78 | hash ^= u64::from(*byte); |
| 79 | hash = hash.wrapping_mul(0x0000_0100_0000_01b3); |
| 80 | } |
| 81 | format!("{hash:016x}") |
| 82 | } |
| 83 | |
| 84 | #[cfg(test)] |
| 85 | mod tests { |
| 86 | use super::*; |
| 87 | use tempfile::tempdir; |
| 88 | |
| 89 | #[test] |
| 90 | fn snapshot_dir_layout_two_levels_under_deepseek() { |
| 91 | let tmp = tempdir().expect("tempdir"); |
| 92 | let dir = snapshot_dir_with_home(tmp.path(), Some(tmp.path().to_path_buf())); |
| 93 | let mut iter = dir.strip_prefix(tmp.path()).unwrap().components(); |
| 94 | assert_eq!(iter.next().unwrap().as_os_str(), ".deepseek"); |
| 95 | assert_eq!(iter.next().unwrap().as_os_str(), "snapshots"); |
| 96 | assert!(iter.next().is_some()); // project_hash |
| 97 | assert!(iter.next().is_some()); // worktree_hash |
| 98 | assert!(iter.next().is_none()); |
| 99 | } |
| 100 | |
| 101 | #[test] |
| 102 | fn worktree_suffix_stripped_for_project_hash() { |
| 103 | let tmp = tempdir().expect("tempdir"); |
| 104 | let main_path = tmp.path().join("repo"); |
| 105 | let wt_path = tmp.path().join("repo").join(".worktrees").join("featX"); |
| 106 | std::fs::create_dir_all(&main_path).unwrap(); |
| 107 | std::fs::create_dir_all(&wt_path).unwrap(); |
| 108 | |
| 109 | let main_dir = snapshot_dir_with_home(&main_path, Some(tmp.path().to_path_buf())); |
| 110 | let wt_dir = snapshot_dir_with_home(&wt_path, Some(tmp.path().to_path_buf())); |
| 111 | |
| 112 | // Same project_hash (parent component before the worktree-specific tail). |
| 113 | let main_components: Vec<_> = main_dir.components().collect(); |
| 114 | let wt_components: Vec<_> = wt_dir.components().collect(); |
| 115 | assert_eq!( |
| 116 | main_components[main_components.len() - 2], |
| 117 | wt_components[wt_components.len() - 2], |
| 118 | "worktrees should share project_hash", |
| 119 | ); |
| 120 | // But different worktree_hash (the tail). |
| 121 | assert_ne!(main_components.last(), wt_components.last()); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn ensure_snapshot_dir_creates_path() { |
| 126 | let tmp = tempdir().expect("tempdir"); |
| 127 | // Use scoped HOME so we don't pollute the real one. |
| 128 | let dir = snapshot_dir_with_home(tmp.path(), Some(tmp.path().to_path_buf())); |
| 129 | std::fs::create_dir_all(&dir).unwrap(); |
| 130 | assert!(dir.exists()); |
| 131 | } |
| 132 | |
| 133 | #[test] |
| 134 | fn snapshot_git_dir_appends_dot_git() { |
| 135 | let tmp = tempdir().expect("tempdir"); |
| 136 | let git_dir = snapshot_git_dir(tmp.path()); |
| 137 | assert_eq!(git_dir.file_name().unwrap(), ".git"); |
| 138 | } |
| 139 | } |
| 140 |