Skip to content

Commit

Permalink
.vault ignored in vault commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhexists committed Dec 15, 2023
1 parent d783e5a commit 7f41412
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 82 deletions.
168 changes: 88 additions & 80 deletions src/commands/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,95 +24,103 @@ 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 {
if entry
.file_type()
.map_or(false, |ft: fs::FileType| ft.is_dir())
{
let subdirectory_entries: Result<Vec<TreeEntry>, io::Error> =
handle_commit(&entry.path());
match subdirectory_entries {
Ok(subdirectory_entries) => {
let sub_dir_tree: Result<Tree, io::Error> =
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 entry_name = entry.file_name();
if entry_name != ".vault" {
if entry
.file_type()
.map_or(false, |ft: fs::FileType| ft.is_dir())
{
let subdirectory_entries: Result<Vec<TreeEntry>, io::Error> =
handle_commit(&entry.path());
match subdirectory_entries {
Ok(subdirectory_entries) => {
let sub_dir_tree: Result<Tree, io::Error> =
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: "".to_string(),
object: GitObject::Tree(sub_dir_tree),
hashed_path: hashed_tree_string,
});
}
Err(e) => {
panic!("Error in compressing the file: {e}")
}
let mut file: File = File::create(file_path)?;
let _ = file.write_all(&compressed_content);
entries.push(TreeEntry {
name: "".to_string(),
object: GitObject::Tree(sub_dir_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}"),
}
Err(e) => panic!("Some Error Occurred! {e}"),
}
Err(e) => panic!("Some Error Occurred! {e}"),
}
} else {
// Read file content and create a blob object
if let Ok(_content) = fs::read(entry.path()) {
//Using Read string as I don't down if I can read binary from a text file
let file_contents: String = read_string(entry.path()).unwrap();
let file_blob: Result<Blob, std::io::Error> =
Blob::new_blob(file_contents);
match file_blob {
Ok(file_blob) => {
let string_to_be_hashed: &String =
&file_blob.clone().get_content_of_blob();
let compressed_content: Vec<u8> =
compress_zlib(&string_to_be_hashed)?;
let hashed_blob_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_blob_string[0..2];
let dir_path: std::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);
} else {
// Read file content and create a blob object
if let Ok(_content) = fs::read(entry.path()) {
//Using Read string as I don't down if I can read binary from a text file
let file_contents: String = read_string(entry.path()).unwrap();
let file_blob: Result<Blob, std::io::Error> =
Blob::new_blob(file_contents);
match file_blob {
Ok(file_blob) => {
let string_to_be_hashed: &String =
&file_blob.clone().get_content_of_blob();
let compressed_content: Vec<u8> =
compress_zlib(&string_to_be_hashed)?;
let hashed_blob_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_blob_string[0..2];
let dir_path: std::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);
// BAD LOGIC HERE !
if let Err(_) = fs::metadata(dir_path.clone()) {
let _is_already_created = fs::create_dir(dir_path)
.expect("Some error occurred");
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 {
// Enter file Name here
name: "".to_string(),
object: GitObject::Blob(file_blob),
hashed_path: hashed_blob_string,
});
}
let mut file: File = File::create(file_path)?;
let _ = file.write_all(&compressed_content);
entries.push(TreeEntry {
// Enter file Name here
name: "".to_string(),
object: GitObject::Blob(file_blob),
hashed_path: hashed_blob_string,
});
//Think what could be the error actually shown to the user?
Err(_e) => panic!("Some Error Occurred"),
}
//Think what could be the error actually shown to the user?
Err(_e) => panic!("Some Error Occurred"),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tree 41\0folderA\0ab89abcdef0123456789abcdef0123456789ab

#[derive(Debug)]
pub struct Tree {
entries: Vec<TreeEntry>,
pub entries: Vec<TreeEntry>,
pub content: String,
content_size: i32,
}
Expand All @@ -26,7 +26,7 @@ impl Tree {
pub fn make_tree(entries: Vec<TreeEntry>) -> io::Result<Tree> {
let mut tree_content: String = String::new();
for tree_element in &entries {
let tree_entry_content = Tree::get_contents_of_tree_element(tree_element);
let tree_entry_content: String = Tree::get_contents_of_tree_element(tree_element);
tree_content.push_str(&tree_entry_content);
tree_content.push('\n');
}
Expand Down

0 comments on commit 7f41412

Please sign in to comment.