Skip to main content

git_side/commands/
init.rs

1use std::path::Path;
2
3use colored::Colorize;
4
5use crate::config;
6use crate::error::Result;
7use crate::git;
8
9/// Initialize side repo with optional custom path.
10///
11/// # Errors
12///
13/// Returns an error if not in a git repo or if config cannot be written.
14pub fn run(path: Option<&Path>) -> Result<()> {
15    // Get the project identifier
16    let work_tree = git::repo_root()?;
17    let path_hash = config::hash_path(&work_tree);
18
19    // Get or resolve root SHA
20    let root_sha = if let Some(sha) = config::cache_lookup(&path_hash)? {
21        sha
22    } else {
23        let sha = git::initial_commit_sha()?;
24        config::cache_store(&path_hash, &sha)?;
25        sha
26    };
27
28    // Store custom path if provided
29    if let Some(base_path) = path {
30        config::paths_store(&root_sha, base_path)?;
31
32        println!(
33            "{} Side repo will be stored at: {}",
34            "Initialized.".green().bold(),
35            base_path.join(&root_sha).display()
36        );
37    } else {
38        let default_path = config::default_base_path().join(&root_sha);
39        println!(
40            "{} Side repo will be stored at: {}",
41            "Initialized.".green().bold(),
42            default_path.display()
43        );
44    }
45
46    Ok(())
47}