Skip to content

Commit

Permalink
Add ability to checkout trees (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
hateofhades authored Dec 26, 2024
1 parent e5395a2 commit 4bdf048
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src-tauri/src/git/git_blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::errors::git_object_error::GitObjectError;

use super::object::{GitObject, Header};

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct GitBlob {
size: usize,
data: Vec<u8>,
Expand Down
49 changes: 48 additions & 1 deletion src-tauri/src/git/git_tree.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::errors::git_object_error::GitObjectError;

use super::object::{GitObject, Header};
use super::{
git_blob::GitBlob,
git_project::GitProject,
object::{GitObject, Header},
};

#[derive(Debug, Clone, PartialEq)]
pub enum GitTreeMode {
Expand Down Expand Up @@ -138,6 +142,49 @@ impl GitTree {
.filter(|entry| entry.mode != GitTreeMode::Tree)
.collect()
}

/**
* Retrieve all blobs entries in the GitTree
* and from the trees in the GitTree
*
* Returns the file name and blob entries
*/
pub fn get_object_blobs(
&self,
project: &GitProject,
folder: Option<&str>,
) -> Vec<(String, GitBlob)> {
let mut objects: Vec<(String, GitBlob)> = Vec::new();

// Add my blobs
let _ = self.get_blobs().iter().map(|blob| {
if let Ok(blob_obj) = GitBlob::from_hash(project, &blob.hash) {
if let Some(folder_name) = folder {
objects.push((folder_name.to_string() + "/" + &blob.name, blob_obj));
} else {
objects.push((blob.name.clone(), blob_obj));
}
}
});

let _ = self.get_trees().iter().map(|tree| {
if let Ok(tree_obj) = GitTree::from_hash(project, &tree.hash) {
let mut new_folder = tree.name.clone();
if let Some(folder) = folder {
new_folder = folder.to_string() + "/" + &tree.name;
}

tree_obj
.get_object_blobs(project, Some(&new_folder))
.iter()
.for_each(|el| {
objects.push(el.clone());
});
}
});

objects
}
}

impl GitObject for GitTree {
Expand Down

0 comments on commit 4bdf048

Please # to comment.