31 lines
960 B
Rust
31 lines
960 B
Rust
use crate::errors::SocialError;
|
|
use git2::{Error as GitError, Repository, Tree};
|
|
use std::{fs::read_to_string, path::Path};
|
|
|
|
pub struct SocialRepo {
|
|
pub repo: Repository,
|
|
}
|
|
|
|
impl<'repo> SocialRepo {
|
|
pub fn new(path: &str) -> Result<Self, SocialError> {
|
|
Repository::open(path)
|
|
.map(|repo| Self { repo })
|
|
.map_err(|_| {
|
|
let err = "fatal: not a git repository";
|
|
SocialError::from(err)
|
|
})
|
|
}
|
|
|
|
pub fn get_tree(&self, rev: &str) -> Result<Tree, GitError> {
|
|
self.repo
|
|
.revparse_single(rev)
|
|
.and_then(|obj| obj.peel_to_tree())
|
|
}
|
|
|
|
pub fn get_description(&self) -> String {
|
|
// The description is in a file named "description",
|
|
// in the .git directory, or the root directory if bare.
|
|
let git_path = self.repo.path().join(Path::new("description"));
|
|
read_to_string(git_path).unwrap_or("".to_string())
|
|
}
|
|
}
|