From ed3593056213a937847dd03ef426976f251deabe Mon Sep 17 00:00:00 2001 From: ATechnoHazard Date: Mon, 19 Oct 2020 01:47:57 +0530 Subject: [PATCH] Create lockfile from scanned repos A hashmap was used instead of nested structs because I realized that this would still run into the problem of duplicate keys. Ergo, use a HashMap with the key set to a unique path Signed-off-by: ATechnoHazard --- src/main.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 78a1a15..ec18313 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use git2::Repository; use serde::{Deserialize, Serialize}; -use std::path::Path; +use std::{collections::HashMap, fs, path::Path}; use walkdir::WalkDir; #[derive(Debug, Serialize, Deserialize)] @@ -18,11 +18,13 @@ fn main() -> anyhow::Result<()> { return Ok(()); } }; - let mut repos: Vec = Vec::new(); + + let mut repos: HashMap = HashMap::new(); for entry in WalkDir::new(dir).into_iter().filter_map(|e| e.ok()) { if entry.file_type().is_dir() { let path = format!("{}/.git", entry.path().display()); let git_dir = Path::new(&path); + if git_dir.exists() { let repo = Repository::open(git_dir)?; if repo.is_empty()? { @@ -41,19 +43,23 @@ fn main() -> anyhow::Result<()> { .split('/') .collect::>()[2], ) { - repos.push(PersistableRepo { - // Ideally we wanna do this, but it moves `dir`. - // path: entry.path().to_string_lossy().strip_prefix(dir).unwrap().to_string(), - path: entry.path().to_string_lossy().to_string(), - remote_url: remote.url().unwrap_or("None").to_owned(), - head: head.to_owned(), - }); + let path = entry.path().to_string_lossy().to_string(); + repos.insert( + path.clone(), + PersistableRepo { + // Ideally we wanna do this, but it moves `dir`. + // path: entry.path().to_string_lossy().strip_prefix(dir).unwrap().to_string(), + path, + remote_url: remote.url().unwrap_or("None").to_owned(), + head: head.to_owned(), + }, + ); } } }; } }; } - println!("{:#x?}", repos); + fs::write("gitice.lock", toml::to_string(&repos)?).expect("could not write to lockfile!"); Ok(()) }