From 9da03c92072e756d520d83871ddda01e933b2079 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Mon, 12 Oct 2020 17:17:58 +0530 Subject: [PATCH] Test reading git repo metadata Signed-off-by: Harsh Shandilya --- src/main.rs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4ff7029..5f68f4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use git2::Repository; use std::path::Path; use walkdir::WalkDir; @@ -7,18 +8,33 @@ fn main() { None => { println!("Usage:\n gitice \n"); return; - }, + } }; let mut items: Vec = Vec::new(); - for entry in WalkDir::new(dir) - .into_iter() - .filter_map(|e| e.ok()) - { + 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() { - items.push(entry.path().to_string_lossy().to_string()); + let repo = match Repository::open(git_dir) { + Ok(repo) => repo, + Err(e) => { + println!("Failed to open repository: {}", e); + return; + } + }; + let head = match repo.head() { + Ok(head) => head, + Err(e) => { + println!("Failed getting repository head: {}", e); + return; + } + }; + items.push(format!( + "{} = {}", + entry.path().to_string_lossy().to_string(), + head.name().unwrap() + )); } }; }