Merge pull request #3896 from gitbutlerapp/remove-git-signature-shell-type

remove git signature shell type
This commit is contained in:
Kiril Videlov 2024-05-30 00:03:21 +02:00 committed by GitHub
commit 0d2024e7fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 70 additions and 146 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,67 +0,0 @@
use crate::users;
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 TryFrom<&users::User> for Signature<'_> {
type Error = super::Error;
fn try_from(value: &users::User) -> Result<Self, Self::Error> {
if let Some(name) = &value.name {
git2::Signature::now(name, &value.email)
.map(Into::into)
.map_err(Into::into)
} else if let Some(name) = &value.given_name {
git2::Signature::now(name, &value.email)
.map(Into::into)
.map_err(Into::into)
} else {
git2::Signature::now(&value.email, &value.email)
.map(Into::into)
.map_err(Into::into)
}
}
}
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

@ -114,13 +114,6 @@ impl Repository {
super::Config::from(&self.git_repository)
}
pub fn git_signatures<'a>(
&self,
user: Option<&users::User>,
) -> Result<(git::Signature<'a>, git::Signature<'a>)> {
super::signatures::signatures(self, user).context("failed to get signatures")
}
pub fn project(&self) -> &projects::Project {
&self.project
}
@ -347,7 +340,8 @@ impl Repository {
parents: &[&git2::Commit],
change_id: Option<&str>,
) -> Result<git::Oid> {
let (author, committer) = self.git_signatures(user)?;
let (author, committer) =
super::signatures::signatures(self, user).context("failed to get signatures")?;
self.git_repository
.commit(None, &author, &committer, message, tree, parents, change_id)
.context("failed to commit")

View File

@ -3,20 +3,36 @@ use crate::{git, users};
pub fn signatures<'a>(
project_repository: &super::Repository,
user: Option<&users::User>,
) -> Result<(git::Signature<'a>, git::Signature<'a>), git::Error> {
) -> Result<(git2::Signature<'a>, git2::Signature<'a>), git::Error> {
let config = project_repository.config();
let author = match (user, config.user_name()?, config.user_email()?) {
(_, Some(name), Some(email)) => git::Signature::now(&name, &email)?,
(Some(user), _, _) => git::Signature::try_from(user)?,
_ => git::Signature::now("GitButler", "gitbutler@gitbutler.com")?,
(_, Some(name), Some(email)) => git2::Signature::now(&name, &email)?,
(Some(user), _, _) => try_from(user)?,
_ => git2::Signature::now("GitButler", "gitbutler@gitbutler.com")?,
};
let comitter = if config.user_real_comitter()? {
author.clone()
} else {
git::Signature::now("GitButler", "gitbutler@gitbutler.com")?
git2::Signature::now("GitButler", "gitbutler@gitbutler.com")?
};
Ok((author, comitter))
}
fn try_from<'a>(value: &users::User) -> Result<git2::Signature<'a>, git::Error> {
if let Some(name) = &value.name {
git2::Signature::now(name, &value.email)
.map(Into::into)
.map_err(Into::into)
} else if let Some(name) = &value.given_name {
git2::Signature::now(name, &value.email)
.map(Into::into)
.map_err(Into::into)
} else {
git2::Signature::now(&value.email, &value.email)
.map(Into::into)
.map_err(Into::into)
}
}

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();
@ -361,7 +361,9 @@ pub fn apply_branch(
branch.head = new_branch_head;
} else {
// branch was not pushed to upstream yet. attempt a rebase,
let (_, committer) = project_repository.git_signatures(user)?;
let (_, committer) =
project_repository::signatures::signatures(project_repository, user)
.context("failed to get signatures")?;
let mut rebase_options = git2::RebaseOptions::new();
rebase_options.quiet(true);
rebase_options.inmemory(true);
@ -386,7 +388,7 @@ pub fn apply_branch(
break;
}
if let Ok(commit_id) = rebase.commit(None, &committer.clone().into(), None) {
if let Ok(commit_id) = rebase.commit(None, &committer.clone(), None) {
last_rebase_head = commit_id.into();
} else {
rebase_success = false;
@ -1006,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 =
@ -1023,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,
@ -2816,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],
@ -2895,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<_>>(),
@ -3072,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<_>>(),
@ -3422,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],
@ -3511,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
@ -3591,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],
@ -3708,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(),
@ -3822,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(),