zagens_runtime_adapters/snapshot/
paths.rs1use std::io;
9use std::path::{Path, PathBuf};
10
11pub fn snapshot_dir_for(workspace: &Path) -> PathBuf {
23 snapshot_dir_with_home(workspace, dirs::home_dir())
24}
25
26pub 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(zagens_config::USER_DATA_DIR_NAME)
37 .join("snapshots")
38 .join(project_hash)
39 .join(worktree_hash)
40}
41
42pub fn snapshot_git_dir(workspace: &Path) -> PathBuf {
44 snapshot_dir_for(workspace).join(".git")
45}
46
47pub 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
54fn 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
73fn 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)]
85mod tests {
86 use super::*;
87 use tempfile::tempdir;
88
89 #[test]
90 fn snapshot_dir_layout_two_levels_under_zagens() {
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(), ".zagens");
95 assert_eq!(iter.next().unwrap().as_os_str(), "snapshots");
96 assert!(iter.next().is_some()); assert!(iter.next().is_some()); 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 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 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 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}