Init commit

This commit is contained in:
Antonin Ruan
2026-02-19 14:46:58 +01:00
commit f600a89f5b
12 changed files with 993 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
use std::{
fs::File,
io::{
self, prelude::*, BufRead
}, path::Path, result
};
use crate::GIT_DIR;
pub mod head;
pub mod index;
pub mod object;
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path> {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
pub struct Ref {
ref_name: String,
commit_hash: String,
}
impl Ref {
pub fn load(name: String) -> result::Result<Self, ()> {
let path = Path::new(GIT_DIR).join(&name);
let mut lines = match read_lines(path) {
Ok(lines) => lines,
Err(_) => return Err(()),
};
let content = match lines.next() {
Some(Ok(line)) => line,
_ => return Err(()),
};
Ok(
Ref {
ref_name: name,
commit_hash: content
}
)
}
pub fn save(&self) -> result::Result<(), ()> {
let path = Path::new(GIT_DIR)
.join("refs")
.join(&self.ref_name);
let mut file = match File::create(&path) {
Err(_) => return Err(()),
Ok(file) => file,
};
match file.write(self.commit_hash.as_bytes()) {
Err(_) => Err(()),
Ok(_) => Ok(()),
}
}
}