fix list_files

This commit is contained in:
Nikita Galaiko 2023-02-20 14:02:55 +01:00
parent ec06b264d2
commit 929562d80f
No known key found for this signature in database
GPG Key ID: EBAB54E845BA519D
2 changed files with 94 additions and 2 deletions

View File

@ -321,6 +321,7 @@ pub fn get(
Ok(None)
}
// returns list of sessions in reverse chronological order
pub fn list(
repo: &git2::Repository,
project: &projects::Project,
@ -376,7 +377,8 @@ pub fn list_files(
reference: &git2::Reference,
session_id: &str,
) -> Result<HashMap<String, String>> {
let list = list(repo, project, reference)?;
let mut list = list(repo, project, reference)?;
list.reverse();
let mut previous_session = None;
let mut session = None;
@ -415,6 +417,9 @@ pub fn list_files(
if !entry_path.starts_with("wd") {
return git2::TreeWalkResult::Ok;
}
if "wd".eq(entry_path.to_str().unwrap()) {
return git2::TreeWalkResult::Ok;
}
let blob = entry.to_object(repo).and_then(|obj| obj.peel_to_blob());
let content = blob.map(|blob| blob.content().to_vec());

View File

@ -1,3 +1,5 @@
use std::path::Path;
use crate::projects;
use anyhow::Result;
use tempfile::tempdir;
@ -6,7 +8,6 @@ fn test_project() -> Result<(git2::Repository, projects::Project)> {
let path = tempdir()?.path().to_str().unwrap().to_string();
std::fs::create_dir_all(&path)?;
let repo = git2::Repository::init(&path)?;
// make init commit to the repo
let mut index = repo.index()?;
let oid = index.write_tree()?;
let sig = repo.signature()?;
@ -144,3 +145,89 @@ fn test_list() {
assert_eq!(sessions[1], second);
assert_eq!(sessions[2], first);
}
#[test]
fn test_list_files_from_first_presistent_session() {
let (repo, project) = test_project().unwrap();
let file_path = Path::new(&project.path).join("test.txt");
std::fs::write(file_path.clone(), "zero").unwrap();
let first = super::sessions::Session::from_head(&repo, &project);
assert!(first.is_ok());
let mut first = first.unwrap();
first.flush(&repo, &None, &project).unwrap();
assert!(first.hash.is_some());
let file_path = Path::new(&project.path).join("test.txt");
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);
assert!(files.is_ok());
let files = files.unwrap();
assert_eq!(files.len(), 1);
assert_eq!(files["test.txt"], "zero");
}
#[test]
fn test_list_files_from_second_current_session() {
let (repo, project) = test_project().unwrap();
let file_path = Path::new(&project.path).join("test.txt");
std::fs::write(file_path.clone(), "zero").unwrap();
let first = super::sessions::Session::from_head(&repo, &project);
assert!(first.is_ok());
let mut first = first.unwrap();
first.flush(&repo, &None, &project).unwrap();
assert!(first.hash.is_some());
std::thread::sleep(std::time::Duration::from_millis(1));
std::fs::write(file_path.clone(), "one").unwrap();
let second = super::sessions::Session::from_head(&repo, &project);
assert!(second.is_ok());
let second = second.unwrap();
let reference = repo.find_reference(&project.refname()).unwrap();
let files = super::sessions::list_files(&repo, &project, &reference, &second.id);
assert!(files.is_ok());
let files = files.unwrap();
assert_eq!(files.len(), 1);
assert_eq!(files["test.txt"], "zero");
}
#[test]
fn test_list_files_from_second_presistent_session() {
let (repo, project) = test_project().unwrap();
let file_path = Path::new(&project.path).join("test.txt");
std::fs::write(file_path.clone(), "zero").unwrap();
let first = super::sessions::Session::from_head(&repo, &project);
assert!(first.is_ok());
let mut first = first.unwrap();
first.flush(&repo, &None, &project).unwrap();
assert!(first.hash.is_some());
std::thread::sleep(std::time::Duration::from_millis(1));
std::fs::write(file_path.clone(), "one").unwrap();
let second = super::sessions::Session::from_head(&repo, &project);
assert!(second.is_ok());
let mut second = second.unwrap();
second.flush(&repo, &None, &project).unwrap();
assert!(second.hash.is_some());
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);
assert!(files.is_ok());
let files = files.unwrap();
assert_eq!(files.len(), 1);
assert_eq!(files["test.txt"], "zero");
}