Init commit
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::prelude::*,
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use crate::GIT_DIR;
|
||||
use crate::git_fs::read_lines;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Head {
|
||||
pub ref_to: String,
|
||||
}
|
||||
|
||||
impl Head {
|
||||
pub fn load() -> Result<Self, ()> {
|
||||
let path = Path::new(GIT_DIR).join("HEAD");
|
||||
|
||||
let mut lines = match read_lines(path) {
|
||||
Err(_) => return Err(()),
|
||||
Ok(lines) => lines,
|
||||
};
|
||||
|
||||
let content = match lines.next() {
|
||||
Some(Ok(line)) => line,
|
||||
_ => return Err(()),
|
||||
};
|
||||
|
||||
match content.split_once(": ") {
|
||||
None => Err(()),
|
||||
Some((_, r)) => Ok(Head { ref_to: String::from(r) })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&self) -> Result<(), ()> {
|
||||
let path = Path::new(GIT_DIR).join("HEAD");
|
||||
let mut content = String::from("ref: ");
|
||||
content.push_str(&self.ref_to);
|
||||
content.push_str("\n");
|
||||
|
||||
let mut file = match File::create(&path) {
|
||||
Err(_) => return Err(()),
|
||||
Ok(file) => file,
|
||||
};
|
||||
|
||||
match file.write(content.as_bytes()) {
|
||||
Err(_) => Err(()),
|
||||
Ok(_) => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user