You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.4 KiB
59 lines
1.4 KiB
4 years ago
|
//! Wrappers for libgit2
|
||
|
|
||
|
pub mod log;
|
||
|
pub mod tree;
|
||
|
|
||
|
use git2::{self, Repository};
|
||
|
use log::CommitGraph;
|
||
|
use tree::Tree;
|
||
|
|
||
|
/// A top-level wrapper API for all libgit2 functions
|
||
|
pub struct Repo {
|
||
|
inner: Repository,
|
||
|
commits: Option<CommitGraph>,
|
||
|
rev: Option<String>,
|
||
|
}
|
||
|
|
||
|
impl Repo {
|
||
|
pub(crate) fn new(path: &str) -> Self {
|
||
|
Self {
|
||
|
inner: Repository::open(path).expect(&format!("`{}` is not a valid git repo", path)),
|
||
|
commits: None,
|
||
|
rev: None,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub(self) fn get_inner(&self) -> &Repository {
|
||
|
&self.inner
|
||
|
}
|
||
|
|
||
|
pub(self) fn get_tree<'r>(&'r self, rev: &str) -> git2::Tree<'r> {
|
||
|
self.inner
|
||
|
.revparse_single(rev)
|
||
|
.unwrap()
|
||
|
.peel_to_tree()
|
||
|
.unwrap()
|
||
|
}
|
||
|
|
||
|
pub(crate) fn clear_cache(&mut self) {
|
||
|
self.rev = None;
|
||
|
self.commits = None;
|
||
|
}
|
||
|
|
||
|
/// Load and cache commits for a specific rev
|
||
|
pub(crate) fn load_commits(&mut self, rev: String) {
|
||
|
self.rev = Some(rev.clone());
|
||
|
self.commits = Some(log::create_commit_log(rev, &self));
|
||
|
}
|
||
|
|
||
|
/// Load the tree of files for the current rev
|
||
|
///
|
||
|
/// Will fail if no rev was previously cached
|
||
|
pub(crate) fn get_file_tree(&self) -> Tree {
|
||
|
tree::parse_tree(
|
||
|
self.get_tree(self.rev.as_ref().unwrap().as_str()),
|
||
|
self.get_inner(),
|
||
|
)
|
||
|
}
|
||
|
}
|