diff --git a/crates/gitbutler-core/src/git/commit_ext.rs b/crates/gitbutler-core/src/git/commit_ext.rs index a859d54e0..6e506ef23 100644 --- a/crates/gitbutler-core/src/git/commit_ext.rs +++ b/crates/gitbutler-core/src/git/commit_ext.rs @@ -1,7 +1,5 @@ // use anyhow::Result; -use super::Result; use bstr::BStr; -use git2::Commit; /// Extension trait for `git2::Commit`. /// @@ -10,9 +8,7 @@ pub trait CommitExt { /// Obtain the commit-message as bytes, but without assuming any encoding. fn message_bstr(&self) -> &BStr; fn change_id(&self) -> Option; - fn parents_gb(&self) -> Result>>; fn is_signed(&self) -> bool; - fn tree_gb(&self) -> Result>; } impl<'repo> CommitExt for git2::Commit<'repo> { @@ -24,23 +20,10 @@ impl<'repo> CommitExt for git2::Commit<'repo> { if cid.is_empty() { None } else { - // convert the Buf to a string - let ch_id = std::str::from_utf8(&cid).ok()?.to_owned(); - Some(ch_id) + String::from_utf8(cid.to_owned()).ok() } } - fn parents_gb(&self) -> Result>> { - let mut parents = vec![]; - for i in 0..self.parent_count() { - parents.push(self.parent(i)?); - } - Ok(parents) - } fn is_signed(&self) -> bool { - let cid = self.header_field_bytes("gpgsig").ok(); - cid.is_some() - } - fn tree_gb(&self) -> Result> { - self.tree().map_err(Into::into) + self.header_field_bytes("gpgsig").is_ok() } } diff --git a/crates/gitbutler-core/src/git/repository.rs b/crates/gitbutler-core/src/git/repository.rs index fc48e53f7..397f6c18f 100644 --- a/crates/gitbutler-core/src/git/repository.rs +++ b/crates/gitbutler-core/src/git/repository.rs @@ -223,15 +223,9 @@ impl Repository { parents: &[&git2::Commit<'_>], change_id: Option<&str>, ) -> Result { - let parents: Vec<&git2::Commit> = parents.iter().map(|c| c.to_owned()).collect::>(); - - let commit_buffer = self.0.commit_create_buffer( - author.into(), - committer.into(), - message, - tree, - &parents, - )?; + let commit_buffer = + self.0 + .commit_create_buffer(author.into(), committer.into(), message, tree, parents)?; let commit_buffer = Self::inject_change_id(&commit_buffer, change_id)?; diff --git a/crates/gitbutler-core/src/virtual_branches/virtual.rs b/crates/gitbutler-core/src/virtual_branches/virtual.rs index 3f79a63d4..bb94779f3 100644 --- a/crates/gitbutler-core/src/virtual_branches/virtual.rs +++ b/crates/gitbutler-core/src/virtual_branches/virtual.rs @@ -428,7 +428,12 @@ pub fn apply_branch( } } - branch.tree = repo.find_commit(branch.head)?.tree_gb()?.id().into(); + branch.tree = repo + .find_commit(branch.head)? + .tree() + .map_err(anyhow::Error::from)? + .id() + .into(); vb_state.set_branch(branch.clone())?; } @@ -1008,8 +1013,7 @@ fn commit_to_vbranch_commit( list_virtual_commit_files(repository, commit).context("failed to list commit files")?; let parent_ids: Vec = commit - .parents_gb()? - .iter() + .parents() .map(|c| { let c: git::Oid = c.id().into(); c @@ -2041,8 +2045,14 @@ pub fn reset_branch( let repo = &project_repository.git_repository; let diff = trees( repo, - &repo.find_commit(updated_head)?.tree_gb()?, - &repo.find_commit(old_head)?.tree_gb()?, + &repo + .find_commit(updated_head)? + .tree() + .map_err(anyhow::Error::from)?, + &repo + .find_commit(old_head)? + .tree() + .map_err(anyhow::Error::from)?, )?; // Assign the new hunks to the branch we're working on. @@ -2877,9 +2887,7 @@ pub fn move_commit_file( .git_repository .find_tree(new_tree_oid) .context("failed to find new tree")?; - let parents = amend_commit - .parents_gb() - .context("failed to find head commit parents")?; + let parents: Vec<_> = amend_commit.parents().collect(); let change_id = amend_commit.change_id(); let commit_oid = project_repository .git_repository @@ -3057,10 +3065,7 @@ pub fn amend( .find_tree(new_tree_oid) .context("failed to find new tree")?; - let parents = amend_commit - .parents_gb() - .context("failed to find head commit parents")?; - + let parents: Vec<_> = amend_commit.parents().collect(); let commit_oid = project_repository .git_repository .commit( @@ -3692,9 +3697,7 @@ pub fn squash( // * has the tree of the target commit // * has the message combined of the target commit and parent commit // * has parents of the parents commit. - let parents = parent_commit - .parents_gb() - .context("failed to find head commit parents")?; + let parents: Vec<_> = parent_commit.parents().collect(); // use the squash commit's change id let change_id = commit_to_squash.change_id(); @@ -3809,9 +3812,7 @@ pub fn update_commit_message( .find_commit(commit_oid) .context("failed to find commit")?; - let parents = target_commit - .parents_gb() - .context("failed to find head commit parents")?; + let parents: Vec<_> = target_commit.parents().collect(); let change_id = target_commit.change_id(); diff --git a/crates/gitbutler-testsupport/src/test_project.rs b/crates/gitbutler-testsupport/src/test_project.rs index 4fbbc9897..c30aef944 100644 --- a/crates/gitbutler-testsupport/src/test_project.rs +++ b/crates/gitbutler-testsupport/src/test_project.rs @@ -1,7 +1,7 @@ use std::path; use std::path::PathBuf; -use gitbutler_core::git::{self, CommitExt}; +use gitbutler_core::git::{self}; use tempfile::TempDir; use crate::{init_opts, VAR_NO_CLEANUP}; @@ -276,16 +276,15 @@ impl TestProject { .peel_to_commit() .unwrap(); let tree = match self.local_repository.find_branch(&branch) { - Ok(branch) => branch.peel_to_tree(), + Ok(branch) => branch.peel_to_tree().unwrap(), Err(git::Error::NotFound(_)) => { self.local_repository .reference(&branch, head_commit.id().into(), false, "new branch") .unwrap(); - head_commit.tree_gb() + head_commit.tree().unwrap() } - Err(error) => Err(error), - } - .unwrap(); + Err(error) => panic!("{:?}", error), + }; self.local_repository.set_head(&branch).unwrap(); self.local_repository .checkout_tree(&tree)