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 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<String>;
fn parents_gb(&self) -> Result<Vec<Commit<'_>>>;
fn is_signed(&self) -> bool;
fn tree_gb(&self) -> Result<git2::Tree<'_>>;
}
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<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 {
let cid = self.header_field_bytes("gpgsig").ok();
cid.is_some()
}
fn tree_gb(&self) -> Result<git2::Tree<'repo>> {
self.tree().map_err(Into::into)
self.header_field_bytes("gpgsig").is_ok()
}
}

View File

@ -223,15 +223,9 @@ impl Repository {
parents: &[&git2::Commit<'_>],
change_id: Option<&str>,
) -> Result<Oid> {
let parents: Vec<&git2::Commit> = parents.iter().map(|c| c.to_owned()).collect::<Vec<_>>();
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)?;

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())?;
}
@ -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<git::Oid> = 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();

View File

@ -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)