Init commit
This commit is contained in:
@@ -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(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user