Skip to content

Commit

Permalink
Incomplete - vault revert
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhexists committed Dec 21, 2023
1 parent 440cbab commit 76c5bb7
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 144 deletions.
212 changes: 92 additions & 120 deletions src/commands/commit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//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 All @@ -11,6 +10,7 @@ use crate::utils::get_current_branch::get_current_branch;
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::fs;
use std::fs::File;
use std::io::{self, Write};
Expand All @@ -37,53 +37,40 @@ fn handle_commit(dir_path: &Path) -> io::Result<Vec<TreeEntry>> {
handle_commit(&entry.path());
match subdirectory_entries {
Ok(subdirectory_entries) => {
let sub_dir_tree: Result<Tree, io::Error> =
let sub_dir_tree: Tree =
Tree::make_tree(subdirectory_entries);
match sub_dir_tree {
Ok(sub_dir_tree) => {
let string_to_be_hashed: &String =
&sub_dir_tree.content;
let compressed_content: Result<Vec<u8>, io::Error> =
compress_zlib(&string_to_be_hashed);
match compressed_content {
Ok(compressed_content) => {
let hashed_tree_string: String =
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 file_name: &str =
&hashed_tree_string[2..];
let file_path: std::path::PathBuf =
dir_path.join(file_name);
// BAD LOGIC HERE !
if let Err(_) =
fs::metadata(dir_path.clone())
{
let _is_already_created =
fs::create_dir(dir_path)
.expect("Some error occurred");
}
let mut file: File =
File::create(file_path)?;
let _ = file.write_all(&compressed_content);
entries.push(TreeEntry {
name: entry_name
.to_str()
.unwrap()
.to_string(),
object: GitObject::Tree(sub_dir_tree),
hashed_path: hashed_tree_string,
});
}
Err(e) => {
panic!("Error in compressing the file: {e}")
}

let string_to_be_hashed: &String = &sub_dir_tree.content;
let compressed_content: Result<Vec<u8>, io::Error> =
compress_zlib(&string_to_be_hashed);
match compressed_content {
Ok(compressed_content) => {
let hashed_tree_string: String =
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 file_name: &str = &hashed_tree_string[2..];
let file_path: std::path::PathBuf =
dir_path.join(file_name);
// BAD LOGIC HERE !
if let Err(_) = fs::metadata(dir_path.clone()) {
let _is_already_created =
fs::create_dir(dir_path)
.expect("Some error occurred");
}
let mut file: File = File::create(file_path)?;
let _ = file.write_all(&compressed_content);
entries.push(TreeEntry {
name: entry_name.to_str().unwrap().to_string(),
object: GitObject::Tree,
hashed_path: hashed_tree_string,
});
}
Err(e) => {
panic!("Error in compressing the file: {e}")
}
Err(e) => panic!("Some error occurred : {e}"),
}
}
Err(e) => panic!("Some Error Occurred! {e}"),
Expand Down Expand Up @@ -119,7 +106,7 @@ fn handle_commit(dir_path: &Path) -> io::Result<Vec<TreeEntry>> {
let _ = file.write_all(&compressed_content);
entries.push(TreeEntry {
name: entry_name.to_str().unwrap().to_string(),
object: GitObject::Blob(file_blob),
object: GitObject::Blob,
hashed_path: hashed_blob_string,
});
}
Expand All @@ -142,7 +129,6 @@ fn handle_commit(dir_path: &Path) -> io::Result<Vec<TreeEntry>> {
Ok(entries)
}

//@TODO - Sync this function to config.yaml
pub fn commit(dir_path: &Path, message: &str) -> io::Result<()> {
let commit: Result<Vec<TreeEntry>, io::Error> = handle_commit(dir_path);
let vault = Path::new(".vault");
Expand All @@ -153,85 +139,71 @@ pub fn commit(dir_path: &Path, message: &str) -> io::Result<()> {
let vault_path = vault_branch_path.join("objects");
match commit {
Ok(vec_tree) => {
let main_repo_tree: Result<Tree, io::Error> = Tree::make_tree(vec_tree);
match main_repo_tree {
Ok(dir_tree) => {
let string_to_be_hashed: &String = &dir_tree.content;
let compressed_content: Result<Vec<u8>, io::Error> =
compress_zlib(&string_to_be_hashed);
match compressed_content {
Ok(compressed_content) => {
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 file_name: &str = &&hash_main_dir_in_sha256[2..];
let file_path: std::path::PathBuf = dir_path.join(file_name);
// Not Good Logic ig?
if let Err(_) = fs::metadata(dir_path.clone()) {
let _is_already_created =
fs::create_dir(dir_path).expect("Some error occurred");
}
let mut file: File = File::create(file_path)?;
let _ = file.write_all(&compressed_content);
let current_commit: Result<Commit, io::Error> =
Commit::new_commit(message, hash_main_dir_in_sha256);
match current_commit {
Ok(current_commit) => {
let commit_content: String =
Commit::get_content_of_commit(current_commit);
let compressed_commit_content: Result<
Vec<u8>,
io::Error,
> = compress_zlib(&commit_content);
match compressed_commit_content {
Ok(compressed_commit_content) => {
let hash_commit_content_in_sha256: String =
hash_in_sha256(&commit_content);
let dir_name: &str =
&hash_commit_content_in_sha256[..2];
let file_name: &str =
&hash_commit_content_in_sha256[2..];
let dir_path: std::path::PathBuf =
vault_path.join(dir_name);
let file_path: PathBuf =
dir_path.join(file_name);
// Not Good Logic ig?
if let Err(_) = fs::metadata(dir_path.clone()) {
let _is_already_created =
fs::create_dir(dir_path)
.expect("Some error occurred");
}
let mut file: File = File::create(file_path)?;
let _ =
file.write_all(&compressed_commit_content);
let username: String = whoami::realname();
//@TODO - Think what can we do here maybe?
let _write_in_config_file: Result<
(),
io::Error,
> = ConfigLayout::add_commit(
yaml_layouts::Commit {
hash: hash_commit_content_in_sha256,
date: Utc::now().to_string(),
author: username,
message: message.to_string(),
},
);
}
Err(e) => {
panic!("Some Error Occurred: {e}");
}
let main_repo_tree: Tree = Tree::make_tree(vec_tree);
let string_to_be_hashed: &String = &main_repo_tree.content;
let compressed_content: Result<Vec<u8>, io::Error> =
compress_zlib(&string_to_be_hashed);
match compressed_content {
Ok(compressed_content) => {
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 file_name: &str = &&hash_main_dir_in_sha256[2..];
let file_path: std::path::PathBuf = dir_path.join(file_name);
// Not Good Logic ig?
if let Err(_) = fs::metadata(dir_path.clone()) {
let _is_already_created =
fs::create_dir(dir_path).expect("Some error occurred");
}
let mut file: File = File::create(file_path)?;
let _ = file.write_all(&compressed_content);
let current_commit: Result<Commit, io::Error> =
Commit::new_commit(message, hash_main_dir_in_sha256);
match current_commit {
Ok(current_commit) => {
let commit_content: String =
Commit::get_content_of_commit(current_commit);
let compressed_commit_content: Result<Vec<u8>, io::Error> =
compress_zlib(&commit_content);
match compressed_commit_content {
Ok(compressed_commit_content) => {
let hash_commit_content_in_sha256: String =
hash_in_sha256(&commit_content);
let dir_name: &str =
&hash_commit_content_in_sha256[..2];
let file_name: &str =
&hash_commit_content_in_sha256[2..];
let dir_path: std::path::PathBuf =
vault_path.join(dir_name);
let file_path: PathBuf = dir_path.join(file_name);
// Not Good Logic ig?
if let Err(_) = fs::metadata(dir_path.clone()) {
let _is_already_created = fs::create_dir(dir_path)
.expect("Some error occurred");
}
let mut file: File = File::create(file_path)?;
let _ = file.write_all(&compressed_commit_content);
let username: String = whoami::realname();
//@TODO - Think what can we do here maybe?
let _write_in_config_file: Result<(), io::Error> =
ConfigLayout::add_commit(yaml_layouts::Commit {
hash: hash_commit_content_in_sha256,
date: Utc::now().to_string(),
author: username,
message: message.to_string(),
});
}
Err(e) => {
panic!("Some Error Occurred: {e}");
}
Err(e) => panic!("Some error Occurred : {e}"),
}
Ok(())
}
Err(e) => panic!("Some error Occurred: {e}"),
Err(e) => panic!("Some error Occurred : {e}"),
}
Ok(())
}
Err(e) => panic!("Some Error Occurred: {e}"),
Err(e) => panic!("Some error Occurred: {e}"),
}
}
Err(e) => panic!("Failed to Commit: {e}"),
Expand Down
Loading

0 comments on commit 76c5bb7

Please sign in to comment.