Skip to content

Commit

Permalink
Update module imports and add new functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhexists committed Dec 22, 2023
1 parent c78ee58 commit 8249ae1
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 73 deletions.
14 changes: 9 additions & 5 deletions src/commands/cat.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use crate::{utils::compress_zlib::decompress_zlib, utils::get_current_branch::get_current_branch};
use std::{fs::File, io, path::Path};
use std::{
fs::File,
io,
path::{Path, PathBuf},
};

pub fn cat(hash_string: &str) -> io::Result<()> {
let path: &Path = Path::new(".vault");
let dir_name: &str = &hash_string[..2];
let file_path: &str = &hash_string[2..];
// Handle Error here
let current_branch_name: String = get_current_branch().unwrap();
let branch_path: std::path::PathBuf = path.join(current_branch_name);
let branch_object_path: std::path::PathBuf = branch_path.join("objects");
let hash_dir_path: std::path::PathBuf = branch_object_path.join(dir_name);
let file_path: std::path::PathBuf = hash_dir_path.join(file_path);
let branch_path: PathBuf = path.join(current_branch_name);
let branch_object_path: PathBuf = branch_path.join("objects");
let hash_dir_path: PathBuf = branch_object_path.join(dir_name);
let file_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 decompressed = decompress_zlib(file);
Expand Down
19 changes: 8 additions & 11 deletions src/commands/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::utils::hash::hash_in_sha256;
use crate::utils::read_files::read_bytes;
use crate::utils::yaml_layouts::{self, ConfigLayout};
use chrono::Utc;
use std::ffi::OsString;
use std::fs::File;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
Expand All @@ -27,7 +28,7 @@ fn handle_commit(dir_path: &Path) -> io::Result<Vec<TreeEntry>> {
if let Ok(entries_result) = fs::read_dir(dir_path) {
for entry_result in entries_result {
if let Ok(entry) = entry_result {
let entry_name: std::ffi::OsString = entry.file_name();
let entry_name: OsString = entry.file_name();
if entry_name != ".vault" {
if entry
.file_type()
Expand All @@ -48,11 +49,9 @@ fn handle_commit(dir_path: &Path) -> io::Result<Vec<TreeEntry>> {
hash_in_sha256(&string_to_be_hashed);
// Make a directory with the 1st two letters of the hash
let dir_name: &str = &hashed_tree_string[0..2];
let dir_path: std::path::PathBuf =
vault_path.join(dir_name);
let dir_path: PathBuf = vault_path.join(dir_name);
let file_name: &str = &hashed_tree_string[2..];
let file_path: std::path::PathBuf =
dir_path.join(file_name);
let file_path: PathBuf = dir_path.join(file_name);
// BAD LOGIC HERE !
if let Err(_) = fs::metadata(&dir_path) {
let _is_already_created =
Expand Down Expand Up @@ -91,11 +90,9 @@ fn handle_commit(dir_path: &Path) -> io::Result<Vec<TreeEntry>> {
hash_in_sha256(&string_to_be_hashed);
// Make a directory with the 1st two letters of the hash
let dir_name: &str = &hashed_blob_string[0..2];
let dir_path: std::path::PathBuf =
vault_path.join(dir_name);
let dir_path: PathBuf = vault_path.join(dir_name);
let file_name: &str = &hashed_blob_string[2..];
let file_path: std::path::PathBuf =
dir_path.join(file_name);
let file_path: PathBuf = dir_path.join(file_name);
// BAD LOGIC HERE !
if let Err(_) = fs::metadata(&dir_path) {
let _is_already_created = fs::create_dir(dir_path)
Expand Down Expand Up @@ -148,9 +145,9 @@ pub fn commit(dir_path: &Path, message: &str) -> io::Result<()> {
let hash_main_dir_in_sha256: String =
hash_in_sha256(&string_to_be_hashed);
let dir_name: &str = &&hash_main_dir_in_sha256[0..2];
let dir_path: std::path::PathBuf = vault_path.join(dir_name);
let dir_path: PathBuf = vault_path.join(dir_name);
let file_name: &str = &&hash_main_dir_in_sha256[2..];
let file_path: std::path::PathBuf = dir_path.join(file_name);
let file_path: PathBuf = dir_path.join(file_name);
// Not Good Logic ig?
if let Err(_) = fs::metadata(&dir_path) {
let _is_already_created =
Expand Down
3 changes: 2 additions & 1 deletion src/commands/create.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::utils::yaml_layouts::{ConfigLayout, InitLayout};
use std::borrow::Cow;
use std::fs;
use std::path::{Path, PathBuf};

pub fn create(branch_name: &String) {
let vault_folder: &Path = Path::new(".vault");
let init_file: PathBuf = vault_folder.join("init.yaml");
let content_bytes: Vec<u8> = fs::read(&init_file).unwrap();
let content: std::borrow::Cow<'_, str> = String::from_utf8_lossy(&content_bytes);
let content: Cow<'_, str> = String::from_utf8_lossy(&content_bytes);
let mut init_content: InitLayout = serde_yaml::from_str(&content).unwrap();

init_content.branches.push(branch_name.to_string());
Expand Down
11 changes: 5 additions & 6 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use chrono::Utc;
//This will have an init function which will currently create a .vault folder in the current directory
// VAULT INIT
use crate::commands::{create::create, switch::switch};
use crate::utils::yaml_layouts::InitLayout;
use chrono::Utc;
use std::fs::{self, create_dir};
use std::path::Path;
use std::path::{Path, PathBuf};

pub fn init() {
let vault_folder: &Path = Path::new(".vault");
let current_dir: std::path::PathBuf =
std::env::current_dir().expect("Unable to get current directory");
let ignore_file_path: std::path::PathBuf = current_dir.join(".vaultignore");
let current_dir: PathBuf = std::env::current_dir().expect("Unable to get current directory");
let ignore_file_path: PathBuf = current_dir.join(".vaultignore");
match create_dir(vault_folder) {
Ok(_) => {
let init_file: std::path::PathBuf = vault_folder.join("init.yaml");
let init_file: PathBuf = vault_folder.join("init.yaml");
let content: String = get_init_data();
fs::File::create(ignore_file_path).unwrap();
fs::write(init_file, content).unwrap();
Expand Down
12 changes: 8 additions & 4 deletions src/commands/log.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{fs, io, path::Path};
use std::{
borrow::Cow,
fs, io,
path::{Path, PathBuf},
};

use crate::utils::{
get_current_branch::get_current_branch,
Expand All @@ -10,10 +14,10 @@ pub fn log() -> io::Result<()> {
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 branch_path: PathBuf = vault_path.join(current_branch);
let config_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 content: Cow<'_, str> = String::from_utf8_lossy(&content_bytes);
let config_content: ConfigLayout = serde_yaml::from_str(&content).unwrap();
let commits: Vec<Commit> = config_content.commits;
for commit in commits {
Expand Down
8 changes: 4 additions & 4 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod cat;
pub mod commit;
pub mod create;
pub mod init;
pub mod switch;
pub mod cat;
pub mod delete;
pub mod init;
pub mod log;
pub mod revert;
pub mod revert;
pub mod switch;
21 changes: 13 additions & 8 deletions src/commands/revert.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
use crate::utils::{
create_fs_from_hash::create_fs, get_current_branch::get_current_branch,
yaml_layouts::ConfigLayout,
create_fs_from_hash::create_fs,
get_current_branch::get_current_branch,
yaml_layouts::{self, ConfigLayout},
};
use std::{
borrow::Cow,
fs, io,
path::{Path, PathBuf},
};
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();
let current_branch: Result<String, 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 branch_path: PathBuf = vault_path.join(&current_branch);
let config_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 content: 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;
let commits: Vec<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);
Expand Down
7 changes: 4 additions & 3 deletions src/commands/switch.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::utils::yaml_layouts::InitLayout;
use std::borrow::Cow;
use std::fs;
use std::path::Path;
use std::path::{Path, PathBuf};

pub fn switch(branch_name: &String) {
let vault_folder: &Path = Path::new(".vault");
let init_file: std::path::PathBuf = vault_folder.join("init.yaml");
let init_file: PathBuf = vault_folder.join("init.yaml");
let content_bytes: Vec<u8> = fs::read(&init_file).unwrap();
let content: std::borrow::Cow<'_, str> = String::from_utf8_lossy(&content_bytes);
let content: Cow<'_, str> = String::from_utf8_lossy(&content_bytes);
let mut init_content: InitLayout = serde_yaml::from_str(&content).unwrap();
if init_content.branches.contains(branch_name) {
init_content.current_branch = branch_name.to_string();
Expand Down
7 changes: 3 additions & 4 deletions src/core/blob.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::types::FileTypes;
use std::io;
use std::{io, num, string};

/* SAMPLE BLOB - for UTF-8 files...
Expand All @@ -20,8 +20,7 @@ pub struct Blob {

impl Blob {
pub fn new_blob(content: Vec<u8>) -> io::Result<Blob> {
let str_content: Result<String, std::string::FromUtf8Error> =
String::from_utf8(content.clone());
let str_content: Result<String, string::FromUtf8Error> = String::from_utf8(content.clone());
match str_content {
Ok(content) => {
let size: i32 = String::from(content.clone()).chars().count() as i32;
Expand Down Expand Up @@ -95,7 +94,7 @@ impl Blob {
_ => panic!("Unknown file type of Blob"),
};
let content: String = blob_content[3].to_string();
let content_size: Result<i32, std::num::ParseIntError> = blob_content[2].parse::<i32>();
let content_size: Result<i32, num::ParseIntError> = blob_content[2].parse::<i32>();
match content_size {
Ok(content_size) => Blob {
content,
Expand Down
4 changes: 2 additions & 2 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod blob;
pub mod types;
pub mod commit;
pub mod tree;
pub mod commit;
pub mod types;
7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod commands;
mod core;
mod utils;
use std::env;

use crate::commands::init::init;
use clap::{Parser, Subcommand};
use commands::delete::delete;
Expand Down Expand Up @@ -38,11 +37,11 @@ enum Arguments {
Delete { branch_name: String },
/// Get a commit history of the current branch
Log,
///
/// Get back to any specified point of your repository!
Revert {
#[arg(short, long)]
level: usize,
path: String
path: String,
},
}

Expand All @@ -57,7 +56,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(),
Arguments::Revert { level, path } => revert(&level, &path).unwrap(),
};
} else {
eprintln!("Failed to get the current working directory");
Expand Down
2 changes: 1 addition & 1 deletion src/utils/compress_zlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use flate2::{write::ZlibEncoder, Compression};
use std::fs::File;
use std::io::{self, Read, Write};

pub fn compress_zlib(data: &str) -> std::io::Result<Vec<u8>> {
pub fn compress_zlib(data: &str) -> io::Result<Vec<u8>> {
let mut encoder: ZlibEncoder<Vec<u8>> = ZlibEncoder::new(Vec::new(), Compression::default());
encoder.write_all(data.as_bytes())?;
encoder.finish()
Expand Down
24 changes: 15 additions & 9 deletions src/utils/create_fs_from_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@ use super::compress_zlib::decompress_zlib;
use crate::core::blob::Blob;
use crate::core::types::GitObject;
use crate::core::{commit::Commit, tree::Tree};
use std::fs;
use std::path::PathBuf;
use std::{env, fs};
use std::{fs::File, io, path::Path};

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 current_branch_path: PathBuf = vault_path.join(branch);
let temp_dir_path: PathBuf = current_branch_path.join("tmp");
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 object_dir_path: PathBuf = current_branch_path.join("objects");
let hash_dir: PathBuf = object_dir_path.join(dir_name);
let hash_file_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: Commit = Commit::get_commit_from_content(&commit_string);
let root_dir_hash: String = commit_object.commit_hash;
let _ = fs::create_dir(&temp_dir_path);
let _ = navigate_through_dir_hash(&root_dir_hash, &current_branch_path, &temp_dir_path);
if path == "./" || path == "." {
} else {
let current_dir: PathBuf = env::current_dir().unwrap();
let new_dir: PathBuf = current_dir.join(path);

}
Ok(())
}

//@TODO- THis function is not error handles at all
//@TODO- THis function is not error handled at all
fn navigate_through_dir_hash(
dir_hash: &String,
path: &PathBuf,
Expand All @@ -33,8 +39,8 @@ fn navigate_through_dir_hash(
let dir_name: &str = &dir_hash[..2];
let file_name: &str = &dir_hash[2..];
let object_dir_path: PathBuf = 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 hash_dir: PathBuf = object_dir_path.join(dir_name);
let hash_file_path: PathBuf = hash_dir.join(file_name);
let file: File = File::open(hash_file_path)?;
let commit_string: String = decompress_zlib(file).unwrap();
let tree_object: Tree = Tree::get_tree_from_content(&commit_string);
Expand All @@ -50,7 +56,7 @@ fn navigate_through_dir_hash(
let blob_string: String = decompress_zlib(file).unwrap();
let blob_object: Blob = Blob::get_blob_from_content(&blob_string);
let final_blob_content: &String = &blob_object.content;
let file_path: std::path::PathBuf = temp_dir_path.join(entry.name);
let file_path: PathBuf = temp_dir_path.join(entry.name);
let _ = fs::write(file_path, final_blob_content);
}
GitObject::Tree => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod compress_zlib;
pub mod create_fs_from_hash;
pub mod get_current_branch;
pub mod hash;
pub mod read_files;
pub mod yaml_layouts;
pub mod create_fs_from_hash;
6 changes: 3 additions & 3 deletions src/utils/read_files.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::fs::{self, OpenOptions};
use std::io::{Read, Result};
use std::path::Path;
use std::fs::OpenOptions;
use std::io::{ Read, Result};

pub fn read_bytes<P: AsRef<Path>>(file_path: P) -> Result<Vec<u8>> {
let mut file: std::fs::File = OpenOptions::new().read(true).open(file_path)?;
let mut file: fs::File = OpenOptions::new().read(true).open(file_path)?;
let mut bytes: Vec<u8> = Vec::new();
file.read_to_end(&mut bytes)?;
Ok(bytes)
Expand Down
Loading

0 comments on commit 8249ae1

Please sign in to comment.