diff options
Diffstat (limited to 'src/git/repository.rs')
-rw-r--r-- | src/git/repository.rs | 207 |
1 files changed, 105 insertions, 102 deletions
diff --git a/src/git/repository.rs b/src/git/repository.rs index d415d82..0ed9ddc 100644 --- a/src/git/repository.rs +++ b/src/git/repository.rs @@ -1,22 +1,11 @@ use std::{ fmt::{Debug, Formatter}, - path::{Path, PathBuf}, sync::Arc, }; -use git2::{Oid, Signature}; use parking_lot::Mutex; -use crate::git::{ - Commit, - CommitDiff, - CommitDiffLoader, - CommitDiffLoaderOptions, - Config, - GitError, - Reference, - RepositoryLoadKind, -}; +use crate::git::{CommitDiff, CommitDiffLoader, CommitDiffLoaderOptions, Config, GitError, RepositoryLoadKind}; /// A light cloneable, simple wrapper around the `git2::Repository` struct #[derive(Clone)] @@ -43,22 +32,6 @@ impl Repository { }) } - /// Attempt to open an already-existing repository at `path`. - /// - /// # Errors - /// Will result in an error if the repository cannot be opened. - pub(crate) fn open_from_path(path: &Path) -> Result<Self, GitError> { - let repository = git2::Repository::open(path).map_err(|e| { - GitError::RepositoryLoad { - kind: RepositoryLoadKind::Path, - cause: e, - } - })?; - Ok(Self { - repository: Arc::new(Mutex::new(repository)), - }) - } - /// Load the git configuration for the repository. /// /// # Errors @@ -93,78 +66,6 @@ impl Repository { .map_err(|e| GitError::CommitLoad { cause: e })? .remove(0)) } - - /// Find a reference by the reference name. - /// - /// # Errors - /// Will result in an error if the reference cannot be found. - pub(crate) fn find_reference(&self, reference: &str) -> Result<Reference, GitError> { - let repo = self.repository.lock(); - let git2_reference = repo - .find_reference(reference) - .map_err(|e| GitError::ReferenceNotFound { cause: e })?; - Ok(Reference::from(&git2_reference)) - } - - /// Find a commit by a reference name. - /// - /// # Errors - /// Will result in an error if the reference cannot be found or is not a commit. - pub(crate) fn find_commit(&self, reference: &str) -> Result<Commit, GitError> { - let repo = self.repository.lock(); - let git2_reference = repo - .find_reference(reference) - .map_err(|e| GitError::ReferenceNotFound { cause: e })?; - Commit::try_from(&git2_reference) - } - - pub(crate) fn repo_path(&self) -> PathBuf { - self.repository.lock().path().to_path_buf() - } - - pub(crate) fn head_id(&self, head_name: &str) -> Result<Oid, git2::Error> { - let repo = self.repository.lock(); - let ref_name = format!("refs/heads/{head_name}"); - let revision = repo.revparse_single(ref_name.as_str())?; - Ok(revision.id()) - } - - pub(crate) fn commit_id_from_ref(&self, reference: &str) -> Result<Oid, git2::Error> { - let repo = self.repository.lock(); - let commit = repo.find_reference(reference)?.peel_to_commit()?; - Ok(commit.id()) - } - - pub(crate) fn add_path_to_index(&self, path: &Path) -> Result<(), git2::Error> { - let repo = self.repository.lock(); - let mut index = repo.index()?; - index.add_path(path) - } - - pub(crate) fn remove_path_from_index(&self, path: &Path) -> Result<(), git2::Error> { - let repo = self.repository.lock(); - let mut index = repo.index()?; - index.remove_path(path) - } - - pub(crate) fn create_commit_on_index( - &self, - reference: &str, - author: &Signature<'_>, - committer: &Signature<'_>, - message: &str, - ) -> Result<(), git2::Error> { - let repo = self.repository.lock(); - let tree = repo.find_tree(repo.index()?.write_tree()?)?; - let head = repo.find_reference(reference)?.peel_to_commit()?; - _ = repo.commit(Some("HEAD"), author, committer, message, &tree, &[&head])?; - Ok(()) - } - - #[cfg(test)] - pub(crate) fn repository(&self) -> Arc<Mutex<git2::Repository>> { - Arc::clone(&self.repository) - } } impl From<git2::Repository> for Repository { @@ -183,10 +84,112 @@ impl Debug for Repository { } } +#[cfg(test)] +mod tests { + use std::{ + path::{Path, PathBuf}, + sync::Arc, + }; + + use git2::{Oid, Signature}; + use parking_lot::Mutex; + + use crate::git::{Commit, GitError, Reference, Repository, RepositoryLoadKind}; + + impl Repository { + /// Attempt to open an already-existing repository at `path`. + /// + /// # Errors + /// Will result in an error if the repository cannot be opened. + pub(crate) fn open_from_path(path: &Path) -> Result<Self, GitError> { + let repository = git2::Repository::open(path).map_err(|e| { + GitError::RepositoryLoad { + kind: RepositoryLoadKind::Path, + cause: e, + } + })?; + Ok(Self { + repository: Arc::new(Mutex::new(repository)), + }) + } + + /// Find a reference by the reference name. + /// + /// # Errors + /// Will result in an error if the reference cannot be found. + pub(crate) fn find_reference(&self, reference: &str) -> Result<Reference, GitError> { + let repo = self.repository.lock(); + let git2_reference = repo + .find_reference(reference) + .map_err(|e| GitError::ReferenceNotFound { cause: e })?; + Ok(Reference::from(&git2_reference)) + } + + /// Find a commit by a reference name. + /// + /// # Errors + /// Will result in an error if the reference cannot be found or is not a commit. + pub(crate) fn find_commit(&self, reference: &str) -> Result<Commit, GitError> { + let repo = self.repository.lock(); + let git2_reference = repo + .find_reference(reference) + .map_err(|e| GitError::ReferenceNotFound { cause: e })?; + Commit::try_from(&git2_reference) + } + + pub(crate) fn repo_path(&self) -> PathBuf { + self.repository.lock().path().to_path_buf() + } + + pub(crate) fn head_id(&self, head_name: &str) -> Result<Oid, git2::Error> { + let repo = self.repository.lock(); + let ref_name = format!("refs/heads/{head_name}"); + let revision = repo.revparse_single(ref_name.as_str())?; + Ok(revision.id()) + } + + pub(crate) fn commit_id_from_ref(&self, reference: &str) -> Result<Oid, git2::Error> { + let repo = self.repository.lock(); + let commit = repo.find_reference(reference)?.peel_to_commit()?; + Ok(commit.id()) + } + + pub(crate) fn add_path_to_index(&self, path: &Path) -> Result<(), git2::Error> { + let repo = self.repository.lock(); + let mut index = repo.index()?; + index.add_path(path) + } + + pub(crate) fn remove_path_from_index(&self, path: &Path) -> Result<(), git2::Error> { + let repo = self.repository.lock(); + let mut index = repo.index()?; + index.remove_path(path) + } + + pub(crate) fn create_commit_on_index( + &self, + reference: &str, + author: &Signature<'_>, + committer: &Signature<'_>, + message: &str, + ) -> Result<(), git2::Error> { + let repo = self.repository.lock(); + let tree = repo.find_tree(repo.index()?.write_tree()?)?; + let head = repo.find_reference(reference)?.peel_to_commit()?; + _ = repo.commit(Some("HEAD"), author, committer, message, &tree, &[&head])?; + Ok(()) + } + + pub(crate) fn repository(&self) -> Arc<Mutex<git2::Repository>> { + Arc::clone(&self.repository) + } + } +} + // Paths in Windows makes these tests difficult, so disable #[cfg(all(unix, test))] -mod tests { - use std::env::set_var; +mod unix_tests { + use std::path::Path; use claims::{assert_err_eq, assert_ok}; use git2::{ErrorClass, ErrorCode}; |