Skip to content

Commit

Permalink
Added impl and revert command
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhexists committed Dec 21, 2023
1 parent b0c3d41 commit 9b5dfdc
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 13 deletions.
8 changes: 7 additions & 1 deletion src/commands/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ pub fn cat(hash_string: &str) -> io::Result<()> {
let file_path: std::path::PathBuf = hash_dir_path.join(file_path);
// Since this would read from the current directory only, If the file is not found, prompt the user to check if his directory is correct
let file: File = File::open(file_path)?;
let _ = decompress_zlib(file);
let decompressed = decompress_zlib(file);
match decompressed {
Ok(decompressed) => {
println!("{decompressed}")
}
Err(e) => panic!("Unable to read: {e}"),
}
Ok(())
}
3 changes: 1 addition & 2 deletions src/commands/commit.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use chrono::Utc;

//VAULT COMMIT
// Loop around the directory and make the commit
// No add . apparently : )
use chrono::Utc;
use crate::core::blob::Blob;
use crate::core::commit::Commit;
use crate::core::tree::{Tree, TreeEntry};
Expand Down
3 changes: 2 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub mod init;
pub mod switch;
pub mod cat;
pub mod delete;
pub mod log;
pub mod log;
pub mod revert;
28 changes: 28 additions & 0 deletions src/commands/revert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::utils::{
create_fs_from_hash::create_fs, get_current_branch::get_current_branch,
yaml_layouts::ConfigLayout,
};
use std::{fs, io, path::Path};

pub fn revert(level: &usize, path: &String) -> io::Result<()> {
let current_branch: Result<String, std::io::Error> = get_current_branch();
match current_branch {
Ok(current_branch) => {
let vault_path: &Path = Path::new(".vault");
let branch_path: std::path::PathBuf = vault_path.join(&current_branch);
let config_path: std::path::PathBuf = branch_path.join("config.yaml");
let content_bytes: Vec<u8> = fs::read(config_path).expect("Unable to read config.yaml");
let content: std::borrow::Cow<'_, str> = String::from_utf8_lossy(&content_bytes);
let config_content: ConfigLayout = serde_yaml::from_str(&content).unwrap();
let commits: Vec<crate::utils::yaml_layouts::Commit> = config_content.commits;
if let Some(commit) = commits.get(commits.len() - 1 - level) {
let n_th_last_commit_hash: &String = &commit.hash;
let _ = create_fs(n_th_last_commit_hash, path, &current_branch);
} else {
panic!("Level provided is out of bound!")
}
Ok(())
}
Err(e) => panic!("Some error occurred: {e}"),
}
}
23 changes: 23 additions & 0 deletions src/core/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,27 @@ impl Blob {
file_content.push_str(&self.content);
file_content
}

pub fn get_blob_from_content(blob_content: &String) -> Blob {
let blob_content_split: Vec<&str> = blob_content.split("\0").collect();
let is_vaild_blob: bool = Blob::check_valid_blob(&blob_content_split);
if is_vaild_blob {
Blob::parse_vec_of_contents(&blob_content_split)
} else {
panic!("Invalid Blob Content")
}
}

fn check_valid_blob(blob_contents: &Vec<&str>) -> bool {
if blob_contents[0] == "blob " {
if blob_contents.len() == 4 {
return true;
}
}
false
}

fn parse_vec_of_contents(blob_content: &Vec<&str>) -> Blob {
todo!()
}
}
56 changes: 52 additions & 4 deletions src/core/commit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::yaml_layouts;
use crate::utils::yaml_layouts::ConfigLayout;
use chrono::{DateTime, Utc};
use chrono::Utc;
use std::io;

/* SAMPLE COMMIT
Expand All @@ -15,7 +15,7 @@ message `Commit message goes here.`

#[derive(Debug)]
pub struct Commit {
date_time: DateTime<Utc>,
date_time: String,
message: String,
author: String,
commit_hash: String,
Expand All @@ -25,13 +25,13 @@ pub struct Commit {
impl Commit {
//This function is not error handled
pub fn new_commit(commit_message: &str, root_repository_tree_hash: String) -> io::Result<Self> {
let date_time: DateTime<Utc> = Utc::now();
let date_time: String = Utc::now().to_string();
let username: String = whoami::realname();
let parent: Option<yaml_layouts::Commit> = ConfigLayout::get_last_commit();
let commit_hash: String = root_repository_tree_hash;
match parent {
Some(parent) => {
let parent = Some(parent.hash);
let parent: Option<String> = Some(parent.hash);
Ok(Commit {
date_time,
message: commit_message.to_string(),
Expand Down Expand Up @@ -74,4 +74,52 @@ impl Commit {
content.push_str(&self.message);
content
}

pub fn get_commit_from_content(commit_content: &String) -> Option<Commit> {
let break_by_new_line_char: Vec<&str> = commit_content.split("\n").collect();
let is_valid_commit: bool = Commit::check_valid_content(&break_by_new_line_char);
if is_valid_commit {
let commit_object: Commit = Commit::parse_vec_of_contents(&break_by_new_line_char);
return Some(commit_object)
}
None
}

fn check_valid_content(contents: &Vec<&str>) -> bool {
if contents.len() == 5 {
if contents[0].contains("tree ") {
if contents[1].contains("parent") {
if contents[2].contains("author") {
if contents[3].contains("date_time") {
if contents[4].contains("message") {
return true;
}
}
}
}
}
}
false
}

fn parse_vec_of_contents(contents: &Vec<&str>) -> Commit {
let tree_hash: &str = &contents[0][5..];
let parent = || -> Option<String> {
if contents[1][7..].is_empty() {
None
} else {
Some(contents[1][7..].to_string())
}
};
let author: &str = &contents[2][7..];
let date_time: &str = &contents[3][10..];
let message: &str = &contents[4][8..];
Commit {
commit_hash: tree_hash.to_string(),
message: message.to_string(),
date_time: date_time.to_string(),
author: author.to_string(),
parent: parent(),
}
}
}
15 changes: 14 additions & 1 deletion src/core/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Tree {
tree_content.push_str(&tree_entry_content);
tree_content.push('\n');
}
let content_size = String::from(tree_content.clone()).chars().count() as i32;
let content_size: i32 = String::from(tree_content.clone()).chars().count() as i32;
Ok(Tree {
entries,
content: tree_content,
Expand All @@ -56,4 +56,17 @@ impl Tree {
file_content.push_str(&entry.hashed_path);
file_content
}

pub fn get_tree_from_content(tree_content: &String) -> Option<Tree> {
let break_by_new_line: Vec<&str> = tree_content.split("\n").collect();
let tree_entry_contents: Vec<TreeEntry> = Vec::new();
todo!()
}
}

impl TreeEntry {
pub fn check_valid_tree_entry(tree_entry_content: &str) -> bool {
let tree_entry_content: Vec<&str> = tree_entry_content.split("\0").collect();
todo!()
}
}
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::commands::init::init;
use clap::{Parser, Subcommand};
use commands::delete::delete;
use commands::log::log;
use commands::revert::revert;
use commands::{cat::cat, commit::commit, create::create, switch::switch};

#[derive(Parser)]
Expand Down Expand Up @@ -35,8 +36,14 @@ enum Arguments {
Cat { hash_string: String },
/// Deletes the given branch, Use `vault delete .` to entirely remove vault from your directory!
Delete { branch_name: String },
///
/// Get a commit history of the current branch
Log,
///
Revert {
#[arg(short, long)]
level: usize,
path: String
},
}

fn main() {
Expand All @@ -50,6 +57,7 @@ fn main() {
Arguments::Cat { hash_string } => cat(&hash_string).unwrap(),
Arguments::Delete { branch_name } => delete(&branch_name).unwrap(),
Arguments::Log => log().unwrap(),
Arguments::Revert { level , path} => revert(&level, &path).unwrap(),
};
} else {
eprintln!("Failed to get the current working directory");
Expand Down
5 changes: 2 additions & 3 deletions src/utils/compress_zlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ pub fn compress_zlib(data: &str) -> std::io::Result<Vec<u8>> {
encoder.finish()
}

pub fn decompress_zlib(file_path: File) -> io::Result<()> {
pub fn decompress_zlib(file_path: File) -> io::Result<String> {
let mut decoder: ZlibDecoder<File> = ZlibDecoder::new(file_path);
let mut decompressed_data: Vec<u8> = Vec::new();
decoder.read_to_end(&mut decompressed_data)?;
let decompressed_string: String = String::from_utf8_lossy(&decompressed_data).into_owned();
println!("{decompressed_string}");
Ok(())
Ok(decompressed_string)
}
20 changes: 20 additions & 0 deletions src/utils/create_fs_from_hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::{fs::File, io, path::Path};

use crate::core::commit::Commit;

use super::compress_zlib::decompress_zlib;

pub fn create_fs(commit_hash: &String, path: &String, branch: &String) -> io::Result<()> {
let dir_name: &str = &commit_hash[..2];
let file_name: &str = &commit_hash[2..];
let vault_path: &Path = Path::new(".vault");
let current_branch_path: std::path::PathBuf = vault_path.join(branch);
let object_dir_path: std::path::PathBuf = current_branch_path.join("objects");
let hash_dir: std::path::PathBuf = object_dir_path.join(dir_name);
let hash_file_path: std::path::PathBuf = hash_dir.join(file_name);
let file: File = File::open(hash_file_path)?;
let commit_string: String = decompress_zlib(file).unwrap();
let commit_object: Option<Commit> = Commit::get_commit_from_content(&commit_string);
println!("{:?}", commit_object);
Ok(())
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod get_current_branch;
pub mod hash;
pub mod read_files;
pub mod yaml_layouts;
pub mod create_fs_from_hash;

0 comments on commit 9b5dfdc

Please sign in to comment.