Initialize a new repository at the given path. This creates the `.atomic` directory structure and initializes the database with an empty graph. # Arguments `path` - The directory to initialize as a repository # Errors Returns an error if: - A repository already exists at the path - The directory cannot be created - The database cannot be initialized
(path: P)
| 233 | /// - The directory cannot be created |
| 234 | /// - The database cannot be initialized |
| 235 | pub fn init<P: AsRef<Path>>(path: P) -> Result<Self, RepositoryError> { |
| 236 | let root = path.as_ref().to_path_buf(); |
| 237 | let dot_dir = root.join(DOT_DIR); |
| 238 | |
| 239 | // Check if repository already exists |
| 240 | if dot_dir.exists() { |
| 241 | return Err(RepositoryError::AlreadyExists { |
| 242 | path: root.display().to_string(), |
| 243 | }); |
| 244 | } |
| 245 | |
| 246 | // Create directory structure |
| 247 | std::fs::create_dir_all(&dot_dir)?; |
| 248 | std::fs::create_dir_all(dot_dir.join("changes"))?; |
| 249 | std::fs::create_dir_all(dot_dir.join(WORKSPACES_DIR))?; |
| 250 | |
| 251 | // Create initial config |
| 252 | let config_path = dot_dir.join("config.toml"); |
| 253 | let initial_config = format!( |
| 254 | r#"# Atomic repository configuration |
| 255 | |
| 256 | [view] |
| 257 | default = "{}" |
| 258 | "#, |
| 259 | DEFAULT_VIEW |
| 260 | ); |
| 261 | std::fs::write(&config_path, initial_config)?; |
| 262 | |
| 263 | // Create working copy ID file |
| 264 | let wc_id_path = dot_dir.join("working_copy_id"); |
| 265 | std::fs::write(&wc_id_path, "")?; |
| 266 | |
| 267 | // Initialize the pristine database (redb creates the file) |
| 268 | let pristine = Arc::new( |
| 269 | Pristine::open(dot_dir.join("pristine.redb")) |
| 270 | .map_err(|e| RepositoryError::Database(e.to_string()))?, |
| 271 | ); |
| 272 | |
| 273 | // Create the default view and its workspace directory |
| 274 | { |
| 275 | let mut txn = pristine |
| 276 | .write_txn() |
| 277 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 278 | txn.open_or_create_view(DEFAULT_VIEW) |
| 279 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 280 | txn.commit() |
| 281 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 282 | } |
| 283 | ensure_workspace_dir(&dot_dir, DEFAULT_VIEW)?; |
| 284 | |
| 285 | // Initialize the change store |
| 286 | let change_store = ChangeStore::new(dot_dir.join("changes"), DEFAULT_CACHE_CAPACITY) |
| 287 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 288 | |
| 289 | Ok(Self { |
| 290 | root, |
| 291 | dot_dir, |
| 292 | current_view: DEFAULT_VIEW.to_string(), |
nothing calls this directly
no test coverage detected