summaryrefslogtreecommitdiff
path: root/crates/git-tracker/src/tracker.rs
diff options
context:
space:
mode:
authorseth <[email protected]>2024-10-10 07:26:29 -0400
committerGitHub <[email protected]>2024-10-10 07:26:29 -0400
commitc61b701095a1f6b52777d317275f34687e57ee3e (patch)
treee5b1e80fa8040b593aeebb9daf83bcc9d78c6e81 /crates/git-tracker/src/tracker.rs
parent4e1fab6ff1d17a0fff50e1a87af8c0bbe8c075f9 (diff)
rise once again my glorious creation (#51)
* git-tracker: update tips after fetch + cleanup * nix: use nix-filter * ci: cleanup * crates: update * git-tracker: don't spam log while transferring * nix: fix static package eval
Diffstat (limited to 'crates/git-tracker/src/tracker.rs')
-rw-r--r--crates/git-tracker/src/tracker.rs109
1 files changed, 0 insertions, 109 deletions
diff --git a/crates/git-tracker/src/tracker.rs b/crates/git-tracker/src/tracker.rs
deleted file mode 100644
index e6a3f54..0000000
--- a/crates/git-tracker/src/tracker.rs
+++ /dev/null
@@ -1,109 +0,0 @@
-use std::path::Path;
-
-use git2::{Branch, BranchType, Commit, ErrorCode, Oid, Reference, Repository};
-
-/// Helper struct for tracking Git objects
-pub struct Tracker {
- repository: Repository,
-}
-
-#[derive(Debug, thiserror::Error)]
-pub enum Error {
- #[error("libgit2 error")]
- Git(#[from] git2::Error),
- #[error("Repository path not found at `{0}`")]
- RepositoryPathNotFound(String),
-}
-
-impl Tracker {
- /// Create a new [`Tracker`] using the repository at [`path`]
- ///
- /// # Errors
- ///
- /// Will return [`Err`] if the repository can not be opened
- pub fn from_path(path: &str) -> Result<Self, Error> {
- let repository_path = Path::new(path);
- if repository_path.exists() {
- let repository = Repository::open(repository_path)?;
- Ok(Self { repository })
- } else {
- Err(Error::RepositoryPathNotFound(path.to_string()))
- }
- }
-
- /// Finds a branch of name [`name`]
- ///
- /// # Errors
- ///
- /// Will return [`Err`] if the branch cannot be found locally
- pub fn branch_by_name(&self, name: &str) -> Result<Branch, Error> {
- Ok(self.repository.find_branch(name, BranchType::Remote)?)
- }
-
- /// Finds a commit with a SHA match [`sha`]
- ///
- /// # Errors
- ///
- /// Will return [`Err`] if [`sha`] cannot be converted an [`Oid`] or
- /// a commit matching it cannot be found
- pub fn commit_by_sha(&self, sha: &str) -> Result<Commit, Error> {
- let oid = Oid::from_str(sha)?;
- let commit = self.repository.find_commit(oid)?;
-
- Ok(commit)
- }
-
- /// Check if [`Reference`] [`ref`] contains [`Commit`] [`commit`]
- ///
- /// # Errors
- ///
- /// Will return [`Err`] if the reference cannot be resolved to a commit or the descendants
- /// of the reference cannot be resolved
- pub fn ref_contains_commit(
- &self,
- reference: &Reference,
- commit: &Commit,
- ) -> Result<bool, Error> {
- let head = reference.peel_to_commit()?;
-
- // NOTE: we have to check this as `Repository::graph_descendant_of()` (like the name says)
- // only finds *descendants* of it's parent commit, and will not tell us if the parent commit
- // *is* the child commit. i have no idea why i didn't think of this, but that's why this
- // comment is here now
- let is_head = head.id() == commit.id();
-
- let has_commit = self
- .repository
- .graph_descendant_of(head.id(), commit.id())?;
-
- Ok(is_head || has_commit)
- }
-
- /// Check if a [`Branch`] named [`branch_name`] has a commit with the SHA [`commit_sha`]
- ///
- /// # Errors
- ///
- /// Will return [`Err`] if the commit SHA cannot be resolved to an object id, the branch name cannot
- /// be resolved to a branch, or the descendants of the resolved branch cannot be resolved
- pub fn branch_contains_sha(&self, branch_name: &str, commit_sha: &str) -> Result<bool, Error> {
- let commit = match self.commit_by_sha(commit_sha) {
- Ok(commit) => commit,
- Err(why) => {
- // NOTE: we assume commits not found are just not in the branch *yet*, not an error
- // this is because github decides to report merge commit shas for unmerged PRs...yeah
- if let Error::Git(git_error) = &why {
- if git_error.code() == ErrorCode::NotFound {
- return Ok(false);
- }
- }
-
- return Err(why);
- }
- };
-
- let branch = self.branch_by_name(branch_name)?;
- let has_pr = self.ref_contains_commit(&branch.into_reference(), &commit)?;
-
- Ok(has_pr)
- }
-}