simplify git signature use

This commit is contained in:
Kiril Videlov 2024-05-29 23:37:32 +02:00
parent 8882e8efc0
commit ff9e36fb21
4 changed files with 36 additions and 38 deletions

View File

@ -1,5 +1,3 @@
use crate::users;
pub struct Signature<'a> {
pub signature: git2::Signature<'a>,
}
@ -30,26 +28,6 @@ impl<'a> From<git2::Signature<'a>> for Signature<'a> {
}
}
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)

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,9 +340,18 @@ 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)
.commit(
None,
&author.into(),
&committer.into(),
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

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