Test reading git repo metadata

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-10-12 17:17:58 +05:30
parent 820fc6f1ab
commit 9da03c9207
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80

View file

@ -1,3 +1,4 @@
use git2::Repository;
use std::path::Path; use std::path::Path;
use walkdir::WalkDir; use walkdir::WalkDir;
@ -7,18 +8,33 @@ fn main() {
None => { None => {
println!("Usage:\n gitice <dir>\n"); println!("Usage:\n gitice <dir>\n");
return; return;
}, }
}; };
let mut items: Vec<String> = Vec::new(); let mut items: Vec<String> = Vec::new();
for entry in WalkDir::new(dir) for entry in WalkDir::new(dir).into_iter().filter_map(|e| e.ok()) {
.into_iter()
.filter_map(|e| e.ok())
{
if entry.file_type().is_dir() { if entry.file_type().is_dir() {
let path = format!("{}/.git", entry.path().display()); let path = format!("{}/.git", entry.path().display());
let git_dir = Path::new(&path); let git_dir = Path::new(&path);
if git_dir.exists() { 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()
));
} }
}; };
} }