aboutsummaryrefslogtreecommitdiff
path: root/apps/servers/octopus/supergit/src/repo.rs
diff options
context:
space:
mode:
Diffstat (limited to 'apps/servers/octopus/supergit/src/repo.rs')
-rw-r--r--apps/servers/octopus/supergit/src/repo.rs32
1 files changed, 24 insertions, 8 deletions
diff --git a/apps/servers/octopus/supergit/src/repo.rs b/apps/servers/octopus/supergit/src/repo.rs
index 37991c3a560f..3d802a929e77 100644
--- a/apps/servers/octopus/supergit/src/repo.rs
+++ b/apps/servers/octopus/supergit/src/repo.rs
@@ -2,7 +2,7 @@
use crate::{Branch, BranchCommit};
use git2::{self, Oid};
-use std::sync::Arc;
+use std::{fmt, sync::Arc};
pub type GitResult<T> = Result<T, GitError>;
@@ -10,6 +10,12 @@ pub type GitResult<T> = Result<T, GitError>;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct HashId(String);
+impl fmt::Display for HashId {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{}", self.0)
+ }
+}
+
impl HashId {
pub fn to_oid(&self) -> Oid {
self.clone().into()
@@ -63,6 +69,7 @@ pub struct Repository {
}
impl Repository {
+ /// Open a repository read-only at a specific path
pub fn open(path: &str) -> GitResult<Self> {
Ok(Self {
inner: Arc::new(git2::Repository::open(path)?),
@@ -71,9 +78,12 @@ impl Repository {
/// Parse branch data from repository
///
+ /// If you only care about a single branch, you can also use the
+ /// convenience function `get_branch()`.
+ ///
/// ## Panics
///
- /// If there is an error around getting the name, or head commit.
+ /// This function can panic when branch metadata is missing.
pub fn branches(&self) -> GitResult<Vec<Branch>> {
Ok(self
.inner
@@ -88,11 +98,17 @@ impl Repository {
.collect())
}
- /// Get the files touched by a commit
- pub fn get_files_for(&self, id: HashId) -> GitResult<Vec<()>> {
- let c = self.inner.find_commit(id.into())?;
- let tree = c.tree()?;
-
- todo!()
+ /// Get a single branch by name
+ ///
+ /// This function will enumerate all branches, and then select the
+ /// desired one. If you want to make repeated queries onto the
+ /// branch set, it's recommended you call `branches()`, and cache
+ /// the data yourself.
+ pub fn get_branch(&self, name: String) -> Option<Branch> {
+ self.branches().ok().and_then(|ok| {
+ ok.into_iter()
+ .filter(|b| b.name().is_some())
+ .find(|b| &b.name().unwrap() == &name)
+ })
}
}