remove git::signature shell type

This commit is contained in:
Kiril Videlov 2024-05-29 23:51:26 +02:00
parent ff9e36fb21
commit 62a3d02303
11 changed files with 44 additions and 118 deletions

View File

@ -22,9 +22,6 @@ pub use index::*;
mod oid;
pub use oid::*;
mod signature;
pub use signature::*;
mod config;
pub use config::*;

View File

@ -1,4 +1,4 @@
use super::{Branch, Config, Index, Oid, Reference, Refname, Remote, Result, Signature, Url};
use super::{Branch, Config, Index, Oid, Reference, Refname, Remote, Result, Url};
use git2::{BlameOptions, Submodule};
use git2_hooks::HookResult;
#[cfg(unix)]
@ -214,16 +214,16 @@ impl Repository {
pub fn commit(
&self,
update_ref: Option<&Refname>,
author: &Signature<'_>,
committer: &Signature<'_>,
author: &git2::Signature<'_>,
committer: &git2::Signature<'_>,
message: &str,
tree: &git2::Tree<'_>,
parents: &[&git2::Commit<'_>],
change_id: Option<&str>,
) -> Result<Oid> {
let commit_buffer =
self.0
.commit_create_buffer(author.into(), committer.into(), message, tree, parents)?;
let commit_buffer = self
.0
.commit_create_buffer(author, committer, message, tree, parents)?;
let commit_buffer = Self::inject_change_id(&commit_buffer, change_id)?;

View File

@ -1,45 +0,0 @@
pub struct Signature<'a> {
pub signature: git2::Signature<'a>,
}
impl Clone for Signature<'static> {
fn clone(&self) -> Self {
Self {
signature: self.signature.clone(),
}
}
}
impl<'a> From<Signature<'a>> for git2::Signature<'a> {
fn from(value: Signature<'a>) -> Self {
value.signature
}
}
impl<'a> From<&'a Signature<'a>> for &'a git2::Signature<'a> {
fn from(value: &'a Signature<'a>) -> Self {
&value.signature
}
}
impl<'a> From<git2::Signature<'a>> for Signature<'a> {
fn from(value: git2::Signature<'a>) -> Self {
Self { signature: value }
}
}
impl Signature<'_> {
pub fn now(name: &str, email: &str) -> Result<Self, super::Error> {
git2::Signature::now(name, email)
.map(Into::into)
.map_err(Into::into)
}
pub fn name(&self) -> Option<&str> {
self.signature.name()
}
pub fn email(&self) -> Option<&str> {
self.signature.email()
}
}

View File

@ -343,15 +343,7 @@ impl Repository {
let (author, committer) =
super::signatures::signatures(self, user).context("failed to get signatures")?;
self.git_repository
.commit(
None,
&author.into(),
&committer.into(),
message,
tree,
parents,
change_id,
)
.commit(None, &author, &committer, message, tree, parents, change_id)
.context("failed to commit")
}

View File

@ -1,7 +1,5 @@
use serde::{Deserialize, Serialize};
use crate::git;
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
pub struct User {
pub id: u64,
@ -19,17 +17,3 @@ pub struct User {
#[serde(default)]
pub github_username: Option<String>,
}
impl TryFrom<User> for git::Signature<'_> {
type Error = git::Error;
fn try_from(value: User) -> Result<Self, Self::Error> {
if let Some(name) = value.name {
git::Signature::now(&name, &value.email)
} else if let Some(name) = value.given_name {
git::Signature::now(&name, &value.email)
} else {
git::Signature::now(&value.email, &value.email)
}
}
}

View File

@ -20,8 +20,8 @@ const WORKSPACE_HEAD: &str = "Workspace Head";
pub const GITBUTLER_INTEGRATION_COMMIT_AUTHOR_NAME: &str = "GitButler";
pub const GITBUTLER_INTEGRATION_COMMIT_AUTHOR_EMAIL: &str = "gitbutler@gitbutler.com";
fn get_committer<'a>() -> Result<git::Signature<'a>> {
Ok(git::Signature::now(
fn get_committer<'a>() -> Result<git2::Signature<'a>> {
Ok(git2::Signature::now(
GITBUTLER_INTEGRATION_COMMIT_AUTHOR_NAME,
GITBUTLER_INTEGRATION_COMMIT_AUTHOR_EMAIL,
)?)
@ -85,8 +85,8 @@ pub fn get_workspace_head(
// Create merge commit of branch heads.
let workspace_head_id = repo.commit(
None,
&committer.signature,
&committer.signature,
&committer,
&committer,
WORKSPACE_HEAD,
&workspace_tree,
&branch_head_refs,
@ -215,8 +215,8 @@ pub fn update_gitbutler_integration(
// requires committing to the tip of the branch, and we're mostly replacing the tip.
let final_commit = repo.commit(
None,
&committer.signature,
&committer.signature,
&committer,
&committer,
&message,
&integration_commit.tree()?,
&[&target_commit],
@ -253,8 +253,8 @@ pub fn update_gitbutler_integration(
message.push_str("anything else.\n\n");
let branch_head_oid = repo.commit(
None,
&committer.signature,
&committer.signature,
&committer,
&committer,
&message,
&wip_tree,
&[&branch_head],
@ -351,8 +351,8 @@ fn verify_head_is_clean(
.git_repository
.commit(
None,
&commit.author().into(),
&commit.committer().into(),
&commit.author(),
&commit.committer(),
&commit.message_bstr().to_str_lossy(),
&commit.tree().unwrap(),
&[&new_branch_head],

View File

@ -189,12 +189,11 @@ pub fn branch_to_remote_branch_data(
}
pub fn commit_to_remote_commit(commit: &git2::Commit) -> RemoteCommit {
let signature: git::Signature = commit.author().into();
RemoteCommit {
id: commit.id().to_string(),
description: commit.message_bstr().to_owned(),
created_at: commit.time().seconds().try_into().unwrap(),
author: signature.into(),
author: commit.author().into(),
change_id: commit.change_id(),
}
}

View File

@ -26,7 +26,7 @@ use super::{
};
use crate::error::{self, AnyhowContextExt, Code};
use crate::git::diff::{diff_files_into_hunks, trees, FileDiff};
use crate::git::{CommitExt, RepositoryExt, Signature};
use crate::git::{CommitExt, RepositoryExt};
use crate::time::now_since_unix_epoch_ms;
use crate::virtual_branches::branch::HunkHash;
use crate::{
@ -189,8 +189,8 @@ pub struct Author {
pub gravatar_url: url::Url,
}
impl From<git::Signature<'_>> for Author {
fn from(value: git::Signature) -> Self {
impl From<git2::Signature<'_>> for Author {
fn from(value: git2::Signature) -> Self {
let name = value.name().unwrap_or_default().to_string();
let email = value.email().unwrap_or_default().to_string();
@ -1008,7 +1008,6 @@ fn commit_to_vbranch_commit(
is_remote: bool,
) -> Result<VirtualBranchCommit> {
let timestamp = u128::try_from(commit.time().seconds())?;
let signature: Signature = commit.author().into();
let message = commit.message_bstr().to_owned();
let files =
@ -1025,7 +1024,7 @@ fn commit_to_vbranch_commit(
let commit = VirtualBranchCommit {
id: commit.id().into(),
created_at: timestamp * 1000,
author: Author::from(signature),
author: commit.author().into(),
description: message,
is_remote,
files,
@ -2818,8 +2817,8 @@ pub fn move_commit_file(
let new_from_commit_oid = repo
.commit(
None,
&from_commit.author().into(),
&from_commit.committer().into(),
&from_commit.author(),
&from_commit.committer(),
&from_commit.message_bstr().to_str_lossy(),
new_from_tree,
&[&from_parent],
@ -2897,8 +2896,8 @@ pub fn move_commit_file(
.git_repository
.commit(
None,
&amend_commit.author().into(),
&amend_commit.committer().into(),
&amend_commit.author(),
&amend_commit.committer(),
&amend_commit.message_bstr().to_str_lossy(),
&new_tree,
&parents.iter().collect::<Vec<_>>(),
@ -3074,8 +3073,8 @@ pub fn amend(
.git_repository
.commit(
None,
&amend_commit.author().into(),
&amend_commit.committer().into(),
&amend_commit.author(),
&amend_commit.committer(),
&amend_commit.message_bstr().to_str_lossy(),
&new_tree,
&parents.iter().collect::<Vec<_>>(),
@ -3424,8 +3423,8 @@ fn cherry_rebase_group(
.git_repository
.commit(
None,
&to_rebase.author().into(),
&to_rebase.committer().into(),
&to_rebase.author(),
&to_rebase.committer(),
&to_rebase.message_bstr().to_str_lossy(),
&merge_tree,
&[&head],
@ -3513,7 +3512,7 @@ pub fn cherry_pick(
.find_tree(wip_tree_oid)
.context("failed to find tree")?;
let signature = git::Signature::now("GitButler", "gitbutler@gitbutler.com")
let signature = git2::Signature::now("GitButler", "gitbutler@gitbutler.com")
.context("failed to make gb signature")?;
let oid = project_repository
.git_repository
@ -3593,8 +3592,8 @@ pub fn cherry_pick(
.git_repository
.commit(
None,
&target_commit.author().into(),
&target_commit.committer().into(),
&target_commit.author(),
&target_commit.committer(),
&target_commit.message_bstr().to_str_lossy(),
&merge_tree,
&[&branch_head_commit],
@ -3710,8 +3709,8 @@ pub fn squash(
.git_repository
.commit(
None,
&commit_to_squash.author().into(),
&commit_to_squash.committer().into(),
&commit_to_squash.author(),
&commit_to_squash.committer(),
&format!(
"{}\n{}",
parent_commit.message_bstr(),
@ -3824,8 +3823,8 @@ pub fn update_commit_message(
.git_repository
.commit(
None,
&target_commit.author().into(),
&target_commit.committer().into(),
&target_commit.author(),
&target_commit.committer(),
message,
&target_commit.tree().context("failed to find tree")?,
&parents.iter().collect::<Vec<_>>(),

View File

@ -714,7 +714,7 @@ fn commit_id_can_be_generated_or_specified() -> Result<()> {
.expect("failed to add all");
index.write().expect("failed to write index");
let oid = index.write_tree().expect("failed to write tree");
let signature = gitbutler_core::git::Signature::now("test", "test@email.com").unwrap();
let signature = git2::Signature::now("test", "test@email.com").unwrap();
let head = repository.head().expect("failed to get head");
repository
.commit(

View File

@ -169,7 +169,7 @@ pub fn test_repository() -> (gitbutler_core::git::Repository, TempDir) {
.unwrap();
let mut index = repository.index().expect("failed to get index");
let oid = index.write_tree().expect("failed to write tree");
let signature = gitbutler_core::git::Signature::now("test", "test@email.com").unwrap();
let signature = git2::Signature::now("test", "test@email.com").unwrap();
repository
.commit(
Some(&"refs/heads/master".parse().unwrap()),
@ -191,7 +191,7 @@ pub fn commit_all(repository: &gitbutler_core::git::Repository) -> gitbutler_cor
.expect("failed to add all");
index.write().expect("failed to write index");
let oid = index.write_tree().expect("failed to write tree");
let signature = gitbutler_core::git::Signature::now("test", "test@email.com").unwrap();
let signature = git2::Signature::now("test", "test@email.com").unwrap();
let head = repository.head().expect("failed to get head");
let commit_oid = repository
.commit(

View File

@ -34,7 +34,7 @@ impl Default for TestProject {
setup_config(&local_repository.config().unwrap()).unwrap();
let mut index = local_repository.index().expect("failed to get index");
let oid = index.write_tree().expect("failed to write tree");
let signature = git::Signature::now("test", "test@email.com").unwrap();
let signature = git2::Signature::now("test", "test@email.com").unwrap();
local_repository
.commit(
Some(&"refs/heads/master".parse().unwrap()),
@ -241,8 +241,8 @@ impl TestProject {
self.remote_repository
.commit(
Some(&"refs/heads/master".parse().unwrap()),
&branch_commit.author().into(),
&branch_commit.committer().into(),
&branch_commit.author(),
&branch_commit.committer(),
&format!("Merge pull request from {}", branch_name),
&merge_tree,
&[&master_branch_commit, &branch_commit],
@ -302,7 +302,7 @@ impl TestProject {
.expect("failed to add all");
index.write().expect("failed to write index");
let oid = index.write_tree().expect("failed to write tree");
let signature = git::Signature::now("test", "test@email.com").unwrap();
let signature = git2::Signature::now("test", "test@email.com").unwrap();
self.local_repository
.commit(
head.name().as_ref(),