allow to only request some session files

This commit is contained in:
Nikita Galaiko 2023-02-22 08:47:03 +01:00
parent 3c08aef077
commit f4a24faa29
No known key found for this signature in database
GPG Key ID: EBAB54E845BA519D
4 changed files with 21 additions and 12 deletions

View File

@ -258,6 +258,7 @@ fn list_session_files(
handle: tauri::AppHandle,
project_id: &str,
session_id: &str,
paths: Option<Vec<&str>>,
) -> Result<HashMap<String, String>, Error> {
let path_resolver = handle.path_resolver();
let storage = storage::Storage::from_path_resolver(&path_resolver);
@ -272,7 +273,7 @@ fn list_session_files(
}
})?;
let files = repo.files(session_id).map_err(|e| {
let files = repo.files(session_id, paths).map_err(|e| {
log::error!("{}", e);
Error {
message: "Failed to list files".to_string(),

View File

@ -47,12 +47,17 @@ impl Repository {
sessions::list(&self.git_repository, &self.project, &self.reference()?)
}
pub fn files(&self, session_id: &str) -> Result<HashMap<String, String>> {
pub fn files(
&self,
session_id: &str,
files: Option<Vec<&str>>,
) -> Result<HashMap<String, String>> {
sessions::list_files(
&self.git_repository,
&self.project,
&self.reference()?,
session_id,
files,
)
}

View File

@ -414,6 +414,7 @@ pub fn list_files(
project: &projects::Project,
reference: &git2::Reference,
session_id: &str,
paths: Option<Vec<&str>>,
) -> Result<HashMap<String, String>> {
let commit = if is_current_session_id(project, session_id)? {
let head_commit = reference.peel_to_commit()?;
@ -465,14 +466,16 @@ pub fn list_files(
let blob = entry.to_object(repo).and_then(|obj| obj.peel_to_blob());
let content = blob.map(|blob| blob.content().to_vec());
let relpath = entry_path.strip_prefix("wd").unwrap();
if let Some(paths) = paths.as_ref() {
if !paths.contains(&relpath.to_str().unwrap()) {
return git2::TreeWalkResult::Ok;
}
}
files.insert(
entry_path
.strip_prefix("wd")
.unwrap()
.to_owned()
.to_str()
.unwrap()
.to_owned(),
relpath.to_owned().to_str().unwrap().to_owned(),
String::from_utf8(content.unwrap_or_default()).unwrap_or_default(),
);

View File

@ -219,7 +219,7 @@ fn test_list_files_from_first_presistent_session() {
std::fs::write(file_path.clone(), "one").unwrap();
let reference = repo.find_reference(&project.refname()).unwrap();
let files = super::sessions::list_files(&repo, &project, &reference, &first.id);
let files = super::sessions::list_files(&repo, &project, &reference, &first.id, None);
assert!(files.is_ok());
let files = files.unwrap();
assert_eq!(files.len(), 1);
@ -248,7 +248,7 @@ fn test_list_files_from_second_current_session() {
let second = second.unwrap();
let reference = repo.find_reference(&project.refname()).unwrap();
let files = super::sessions::list_files(&repo, &project, &reference, &second.id);
let files = super::sessions::list_files(&repo, &project, &reference, &second.id, None);
assert!(files.is_ok());
let files = files.unwrap();
assert_eq!(files.len(), 1);
@ -281,7 +281,7 @@ fn test_list_files_from_second_presistent_session() {
std::fs::write(file_path.clone(), "two").unwrap();
let reference = repo.find_reference(&project.refname()).unwrap();
let files = super::sessions::list_files(&repo, &project, &reference, &second.id);
let files = super::sessions::list_files(&repo, &project, &reference, &second.id, None);
assert!(files.is_ok());
let files = files.unwrap();
assert_eq!(files.len(), 1);