返回 CodeWhale
paths.rs
根目录 / crates / snapshot / src / paths.rs
1 //! Path resolution for the per-workspace snapshot side-repos.
2 //!
3 //! Snapshots live under the shared resolved state directory (including an
4 //! explicit `CODEWHALE_HOME`, or the existing primary/legacy snapshot store) with
5 //! a two-level hash split so we can snapshot multiple worktrees of the
6 //! same project independently — `git worktree list` users won't get
7 //! cross-talk between feature branches.
8
9 use std::io;
10 use std::path::{Path, PathBuf};
11
12 /// Compute the snapshot directory for a given workspace path.
13 ///
14 /// Returns `<snapshot state dir>/<project_hash>/<worktree_hash>/`, using
15 /// `codewhale_config::resolve_state_dir("snapshots")` for the shared base.
16 /// This resolves paths without creating directories or migrating state.
17 ///
18 /// The `project_hash` is derived from the canonicalized workspace path
19 /// after stripping any `.worktrees/<name>` suffix — multiple worktrees
20 /// of the same repo share the same `project_hash` so users can browse
21 /// snapshots cross-worktree if they want, but the `worktree_hash` keeps
22 /// commits isolated by default.
23 pub fn snapshot_dir_for(workspace: &Path) -> io::Result<PathBuf> {
24 // An explicit profile must never read or create ambient snapshots. The
25 // shared resolver also preserves legacy stores and rejects invalid
26 // overrides; do not silently fall back to the OS home or working directory.
27 let base = snapshot_state_base()?;
28 Ok(snapshot_dir_with_base(workspace, &base))
29 }
30
31 /// The shared snapshot store. An unsealed test gets a private one: a snapshot
32 /// repo opened by a fixture must not land in the developer's real store.
33 #[cfg(any(test, feature = "test-support"))]
34 fn snapshot_state_base() -> io::Result<PathBuf> {
35 match codewhale_runtime::test_support::unsealed_state_dir("snapshots") {
36 Some(base) => Ok(base),
37 None => codewhale_config::resolve_state_dir("snapshots").map_err(io::Error::other),
38 }
39 }
40
41 #[cfg(not(any(test, feature = "test-support")))]
42 fn snapshot_state_base() -> io::Result<PathBuf> {
43 codewhale_config::resolve_state_dir("snapshots").map_err(io::Error::other)
44 }
45
46 fn snapshot_dir_with_base(workspace: &Path, base: &Path) -> PathBuf {
47 let canonical = workspace
48 .canonicalize()
49 .unwrap_or_else(|_| workspace.to_path_buf());
50 let project_root = strip_worktree_suffix(&canonical);
51 let project_hash = stable_hex(&project_root);
52 let worktree_hash = stable_hex(&canonical);
53 base.join(project_hash).join(worktree_hash)
54 }
55
56 /// Resolve the `.git` directory inside the snapshot dir.
57 pub fn snapshot_git_dir(workspace: &Path) -> io::Result<PathBuf> {
58 Ok(snapshot_dir_for(workspace)?.join(".git"))
59 }
60
61 /// Ensure the snapshot dir exists on disk and return its path.
62 pub fn ensure_snapshot_dir(workspace: &Path) -> io::Result<PathBuf> {
63 let dir = snapshot_dir_for(workspace)?;
64 std::fs::create_dir_all(&dir)?;
65 Ok(dir)
66 }
67
68 pub use codewhale_paths::{stable_hex, strip_worktree_suffix};
69
70 #[cfg(test)]
71 mod tests {
72 use super::*;
73 use tempfile::tempdir;
74
75 #[test]
76 fn snapshot_dir_layout_keeps_two_hash_levels_under_selected_base() {
77 let tmp = tempdir().expect("tempdir");
78 let base = tmp.path().join("snapshots");
79 let dir = snapshot_dir_with_base(tmp.path(), &base);
80 let mut iter = dir.strip_prefix(&base).unwrap().components();
81 assert!(iter.next().is_some()); // project_hash
82 assert!(iter.next().is_some()); // worktree_hash
83 assert!(iter.next().is_none());
84 }
85
86 #[test]
87 fn worktree_suffix_stripped_for_project_hash() {
88 let tmp = tempdir().expect("tempdir");
89 let main_path = tmp.path().join("repo");
90 let wt_path = tmp.path().join("repo").join(".worktrees").join("featX");
91 std::fs::create_dir_all(&main_path).unwrap();
92 std::fs::create_dir_all(&wt_path).unwrap();
93
94 let base = tmp.path().join("snapshots");
95 let main_dir = snapshot_dir_with_base(&main_path, &base);
96 let wt_dir = snapshot_dir_with_base(&wt_path, &base);
97
98 // Same project_hash (parent component before the worktree-specific tail).
99 let main_components: Vec<_> = main_dir.components().collect();
100 let wt_components: Vec<_> = wt_dir.components().collect();
101 assert_eq!(
102 main_components[main_components.len() - 2],
103 wt_components[wt_components.len() - 2],
104 "worktrees should share project_hash",
105 );
106 // But different worktree_hash (the tail).
107 assert_ne!(main_components.last(), wt_components.last());
108 }
109
110 #[test]
111 fn explicit_profile_owns_snapshot_creation_and_lookup() {
112 let _lock = codewhale_runtime::test_support::lock_test_env();
113 let tmp = tempdir().expect("tempdir");
114 let profile = tmp.path().join("selected-profile");
115 let _profile =
116 codewhale_runtime::test_support::EnvVarGuard::set("CODEWHALE_HOME", &profile);
117 let workspace = tmp.path().join("workspace");
118 std::fs::create_dir_all(&workspace).expect("workspace");
119 let expected = snapshot_dir_with_base(&workspace, &profile.join("snapshots"));
120 let dir = ensure_snapshot_dir(&workspace).expect("create selected snapshot directory");
121 assert_eq!(
122 dir, expected,
123 "snapshot writes must use the selected profile"
124 );
125 assert!(dir.exists());
126 assert_eq!(
127 snapshot_git_dir(&workspace).expect("lookup"),
128 dir.join(".git"),
129 "snapshot reads must use the same selected profile as writes"
130 );
131 }
132
133 #[test]
134 fn invalid_profile_is_an_error_without_ambient_fallback() {
135 let _lock = codewhale_runtime::test_support::lock_test_env();
136 let tmp = tempdir().expect("tempdir");
137 let _profile =
138 codewhale_runtime::test_support::EnvVarGuard::set("CODEWHALE_HOME", "relative-profile");
139 assert!(snapshot_dir_for(tmp.path()).is_err());
140 assert!(snapshot_git_dir(tmp.path()).is_err());
141 assert!(ensure_snapshot_dir(tmp.path()).is_err());
142 assert!(
143 tmp.path()
144 .read_dir()
145 .expect("unchanged workspace")
146 .next()
147 .is_none()
148 );
149 }
150 }
151
151 lines RUST