chore: refator in repository module
This commit is contained in:
4 files changed
+191
-141
No files matched your search
@@ -0,0 +1,57 @@
|
||||
use anyhow::Result;
|
||||
use std::{
|
||||
env,
|
||||
fs::{create_dir, read_to_string},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
pub struct Repository {
|
||||
pub path: PathBuf,
|
||||
pub ignore: Vec<String>,
|
||||
}
|
||||
|
||||
pub fn default_init_path() -> PathBuf {
|
||||
env::var("REPO_PATH")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|_| PathBuf::from("."))
|
||||
}
|
||||
|
||||
impl Repository {
|
||||
pub fn new() -> Result<Repository> {
|
||||
let path = default_init_path();
|
||||
|
||||
let mut repo = Repository {
|
||||
path,
|
||||
ignore: Vec::new(),
|
||||
};
|
||||
|
||||
repo.load_ignore()?;
|
||||
|
||||
Ok(repo)
|
||||
}
|
||||
|
||||
fn load_ignore(&mut self) -> Result<bool> {
|
||||
let ignore_path = self.path.join(".gitignore");
|
||||
if !ignore_path.exists() {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
let ignore_content = read_to_string(ignore_path)?;
|
||||
self.ignore = ignore_content.lines().map(String::from).collect();
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn init_repository(&mut self, path: &PathBuf) -> Result<PathBuf> {
|
||||
self.path = path.clone();
|
||||
let git_dir = self.path.join(".git");
|
||||
|
||||
create_dir(&git_dir)?;
|
||||
create_dir(git_dir.join("objects"))?;
|
||||
create_dir(git_dir.join("refs"))?;
|
||||
|
||||
std::fs::write(git_dir.join("HEAD"), "ref: refs/heads/main\n")?;
|
||||
|
||||
Ok(self.path.clone())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user