rvcs_lib/
init.rs

1use std::{env, ffi::OsStr, fs, fs::File, io, io::Write, path::Path};
2use crate::ok;
3
4
5pub fn init_repo() -> io::Result<()> {
6    if Path::new(".rvcs").exists() {
7        fs::remove_dir_all(".rvcs")?;
8    }
9
10    fs::create_dir_all(".rvcs/objects")?;
11    fs::create_dir_all(".rvcs/commits/master")?;
12    File::create(".rvcs/commits/master/HEAD")?;
13
14    let mut fb = File::create(".rvcs/CURRENT_BRANCH")?;
15    fb.write_all(b"master")?;
16
17    let current = env::current_dir()?;
18
19    println!(
20        "{} Sucessfully initialized repository in `{}`.",
21        ok(),
22        format!(
23            "{}/.rvcs",
24            current
25                .file_name()
26                .unwrap_or(OsStr::new(""))
27                .to_str()
28                .unwrap()
29        )
30    );
31
32    Ok(())
33}