refactor CommitExt

- remove conflicting `*_gb()` methods as the native ones are easy enough to use
- some minor refactor
This commit is contained in:
Sebastian Thiel 2024-05-29 21:26:40 +02:00
parent d5708b38bb
commit 7f9462a857
No known key found for this signature in database
GPG Key ID: 9CB5EE7895E8268B
4 changed files with 29 additions and 52 deletions

View File

@ -1,7 +1,5 @@
// use anyhow::Result; // use anyhow::Result;
use super::Result;
use bstr::BStr; use bstr::BStr;
use git2::Commit;
/// Extension trait for `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. /// Obtain the commit-message as bytes, but without assuming any encoding.
fn message_bstr(&self) -> &BStr; fn message_bstr(&self) -> &BStr;
fn change_id(&self) -> Option<String>; fn change_id(&self) -> Option<String>;
fn parents_gb(&self) -> Result<Vec<Commit<'_>>>;
fn is_signed(&self) -> bool; fn is_signed(&self) -> bool;
fn tree_gb(&self) -> Result<git2::Tree<'_>>;
} }
impl<'repo> CommitExt for git2::Commit<'repo> { impl<'repo> CommitExt for git2::Commit<'repo> {
@ -24,23 +20,10 @@ impl<'repo> CommitExt for git2::Commit<'repo> {
if cid.is_empty() { if cid.is_empty() {
None None
} else { } else {
// convert the Buf to a string String::from_utf8(cid.to_owned()).ok()
let ch_id = std::str::from_utf8(&cid).ok()?.to_owned();
Some(ch_id)
} }
} }
fn parents_gb(&self) -> Result<Vec<Commit<'repo>>> {
let mut parents = vec![];
for i in 0..self.parent_count() {
parents.push(self.parent(i)?);
}
Ok(parents)
}
fn is_signed(&self) -> bool { fn is_signed(&self) -> bool {
let cid = self.header_field_bytes("gpgsig").ok(); self.header_field_bytes("gpgsig").is_ok()
cid.is_some()
}
fn tree_gb(&self) -> Result<git2::Tree<'repo>> {
self.tree().map_err(Into::into)
} }
} }

View File

@ -223,15 +223,9 @@ impl Repository {
parents: &[&git2::Commit<'_>], parents: &[&git2::Commit<'_>],
change_id: Option<&str>, change_id: Option<&str>,
) -> Result<Oid> { ) -> Result<Oid> {
let parents: Vec<&git2::Commit> = parents.iter().map(|c| c.to_owned()).collect::<Vec<_>>(); let commit_buffer =
self.0
let commit_buffer = self.0.commit_create_buffer( .commit_create_buffer(author.into(), committer.into(), message, tree, parents)?;
author.into(),
committer.into(),
message,
tree,
&parents,
)?;
let commit_buffer = Self::inject_change_id(&commit_buffer, change_id)?; let commit_buffer = Self::inject_change_id(&commit_buffer, change_id)?;

View File

@ -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())?; 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")?; list_virtual_commit_files(repository, commit).context("failed to list commit files")?;
let parent_ids: Vec<git::Oid> = commit let parent_ids: Vec<git::Oid> = commit
.parents_gb()? .parents()
.iter()
.map(|c| { .map(|c| {
let c: git::Oid = c.id().into(); let c: git::Oid = c.id().into();
c c
@ -2041,8 +2045,14 @@ pub fn reset_branch(
let repo = &project_repository.git_repository; let repo = &project_repository.git_repository;
let diff = trees( let diff = trees(
repo, repo,
&repo.find_commit(updated_head)?.tree_gb()?, &repo
&repo.find_commit(old_head)?.tree_gb()?, .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. // Assign the new hunks to the branch we're working on.
@ -2877,9 +2887,7 @@ pub fn move_commit_file(
.git_repository .git_repository
.find_tree(new_tree_oid) .find_tree(new_tree_oid)
.context("failed to find new tree")?; .context("failed to find new tree")?;
let parents = amend_commit let parents: Vec<_> = amend_commit.parents().collect();
.parents_gb()
.context("failed to find head commit parents")?;
let change_id = amend_commit.change_id(); let change_id = amend_commit.change_id();
let commit_oid = project_repository let commit_oid = project_repository
.git_repository .git_repository
@ -3057,10 +3065,7 @@ pub fn amend(
.find_tree(new_tree_oid) .find_tree(new_tree_oid)
.context("failed to find new tree")?; .context("failed to find new tree")?;
let parents = amend_commit let parents: Vec<_> = amend_commit.parents().collect();
.parents_gb()
.context("failed to find head commit parents")?;
let commit_oid = project_repository let commit_oid = project_repository
.git_repository .git_repository
.commit( .commit(
@ -3692,9 +3697,7 @@ pub fn squash(
// * has the tree of the target commit // * has the tree of the target commit
// * has the message combined of the target commit and parent commit // * has the message combined of the target commit and parent commit
// * has parents of the parents commit. // * has parents of the parents commit.
let parents = parent_commit let parents: Vec<_> = parent_commit.parents().collect();
.parents_gb()
.context("failed to find head commit parents")?;
// use the squash commit's change id // use the squash commit's change id
let change_id = commit_to_squash.change_id(); let change_id = commit_to_squash.change_id();
@ -3809,9 +3812,7 @@ pub fn update_commit_message(
.find_commit(commit_oid) .find_commit(commit_oid)
.context("failed to find commit")?; .context("failed to find commit")?;
let parents = target_commit let parents: Vec<_> = target_commit.parents().collect();
.parents_gb()
.context("failed to find head commit parents")?;
let change_id = target_commit.change_id(); let change_id = target_commit.change_id();

View File

@ -1,7 +1,7 @@
use std::path; use std::path;
use std::path::PathBuf; use std::path::PathBuf;
use gitbutler_core::git::{self, CommitExt}; use gitbutler_core::git::{self};
use tempfile::TempDir; use tempfile::TempDir;
use crate::{init_opts, VAR_NO_CLEANUP}; use crate::{init_opts, VAR_NO_CLEANUP};
@ -276,16 +276,15 @@ impl TestProject {
.peel_to_commit() .peel_to_commit()
.unwrap(); .unwrap();
let tree = match self.local_repository.find_branch(&branch) { 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(_)) => { Err(git::Error::NotFound(_)) => {
self.local_repository self.local_repository
.reference(&branch, head_commit.id().into(), false, "new branch") .reference(&branch, head_commit.id().into(), false, "new branch")
.unwrap(); .unwrap();
head_commit.tree_gb() head_commit.tree().unwrap()
} }
Err(error) => Err(error), Err(error) => panic!("{:?}", error),
} };
.unwrap();
self.local_repository.set_head(&branch).unwrap(); self.local_repository.set_head(&branch).unwrap();
self.local_repository self.local_repository
.checkout_tree(&tree) .checkout_tree(&tree)