From 5843a6578480b33a321054d7ae133624c57286b1 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Tue, 4 Apr 2023 22:01:16 +0200 Subject: Check that the git repo is personal before adding it as personal --- src/git.rs | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/git.rs b/src/git.rs index ea27add..ac92908 100644 --- a/src/git.rs +++ b/src/git.rs @@ -105,25 +105,46 @@ pub struct RepoMetadata { } pub fn list() -> Result, ShackleError> { - let mut results = Vec::new(); + fn add_from_dir( + collection_dir: &PathBuf, + is_checking_group: bool, + ) -> Result, ShackleError> { + let mut results = Vec::new(); + if !collection_dir.is_dir() { + return Ok(results); + } - let personal_dir = personal_git_dir()?; - if personal_dir.is_dir() { - for dir in personal_dir.read_dir()? { + for dir in collection_dir.read_dir()? { let path = dir?.path(); let description_path = path.join("description"); let has_git_ext = path.extension().map_or(false, |ext| ext == "git"); - let has_description = description_path.is_file(); - - // TODO: Read the config to tell if this is a shared repo - if has_git_ext && has_description { - let description = fs::read_to_string(description_path)?; - results.push(RepoMetadata { path, description }); + if has_git_ext { + if let Ok(repo) = Repository::open_bare(&path) { + let config = repo.config()?.snapshot()?; + let shared_config = config.get_str("core.sharedRepository").ok(); // TODO: Could shis fail for other reasons? + let is_group_shared = + [Some("group"), Some("1"), Some("true")].contains(&shared_config); + + if is_group_shared == is_checking_group { + let description = if description_path.is_file() { + fs::read_to_string(description_path)? + } else { + String::new() + }; + + results.push(RepoMetadata { path, description }); + } + } } } + Ok(results) } + let mut results = Vec::new(); + + results.append(&mut add_from_dir(&personal_git_dir()?, false)?); + // TODO: Do the same (more or less) for group repos Ok(results) -- cgit v1.2.3