derive commit author from user

This commit is contained in:
Nikita Galaiko 2023-02-21 12:11:47 +01:00
parent b0bb3b00f4
commit 9c0228346d
No known key found for this signature in database
GPG Key ID: EBAB54E845BA519D
2 changed files with 69 additions and 7 deletions

View File

@ -501,7 +501,7 @@ fn flush(
.write()
.with_context(|| format!("failed to write tree"))?;
let commit_oid = write_gb_commit(tree, &repo, project).with_context(|| {
let commit_oid = write_gb_commit(tree, &repo, user, project).with_context(|| {
format!(
"failed to write gb commit for {}",
repo.workdir().unwrap().display()
@ -836,20 +836,26 @@ fn add_session_path(
fn write_gb_commit(
gb_tree: git2::Oid,
repo: &git2::Repository,
user: &Option<users::User>,
project: &projects::Project,
) -> Result<git2::Oid, git2::Error> {
// find the Oid of the commit that refs/.../current points to, none if it doesn't exist
let refname = project.refname();
let signature = git2::Signature::now("gitbutler", "gitbutler@localhost")?;
let comitter = git2::Signature::now("gitbutler", "gitbutler@localhost")?;
let author = match user {
None => comitter.clone(),
Some(user) => git2::Signature::now(user.name.as_str(), user.email.as_str())?,
};
match repo.revparse_single(refname.as_str()) {
Ok(obj) => {
let last_commit = repo.find_commit(obj.id()).unwrap();
let new_commit = repo.commit(
Some(refname.as_str()),
&signature, // author
&signature, // committer
&author, // author
&comitter, // committer
"gitbutler check", // commit message
&repo.find_tree(gb_tree).unwrap(), // tree
&[&last_commit], // parents
@ -859,8 +865,8 @@ fn write_gb_commit(
Err(_) => {
let new_commit = repo.commit(
Some(refname.as_str()),
&signature, // author
&signature, // committer
&author, // author
&comitter, // committer
"gitbutler check", // commit message
&repo.find_tree(gb_tree).unwrap(), // tree
&[], // parents

View File

@ -1,9 +1,22 @@
use std::path::Path;
use crate::projects;
use crate::{projects, users};
use anyhow::Result;
use tempfile::tempdir;
fn test_user() -> users::User {
users::User {
id: 0,
name: "test".to_string(),
email: "test@email.com".to_string(),
picture: "test".to_string(),
locale: None,
created_at: "0".to_string(),
updated_at: "0".to_string(),
access_token: "0".to_string(),
}
}
fn test_project() -> Result<(git2::Repository, projects::Project)> {
let path = tempdir()?.path().to_str().unwrap().to_string();
std::fs::create_dir_all(&path)?;
@ -87,6 +100,49 @@ fn test_flush() {
assert!(flush_result.is_ok());
assert!(created_session.hash.is_some());
let head_commit = repo
.find_reference(&project.refname())
.unwrap()
.peel_to_commit()
.unwrap();
assert_eq!(head_commit.author().name().unwrap(), "gitbutler");
assert_eq!(head_commit.author().email().unwrap(), "gitbutler@localhost");
assert_eq!(head_commit.committer().name().unwrap(), "gitbutler");
assert_eq!(
head_commit.committer().email().unwrap(),
"gitbutler@localhost"
);
let current_session = super::sessions::Session::current(&repo, &project);
assert!(current_session.is_ok());
let current_session = current_session.unwrap();
assert!(current_session.is_none());
}
#[test]
fn test_flush_with_user() {
let (repo, project) = test_project().unwrap();
let created_session = super::sessions::Session::from_head(&repo, &project);
assert!(created_session.is_ok());
let mut created_session = created_session.unwrap();
let flush_result = created_session.flush(&repo, &Some(test_user()), &project);
assert!(flush_result.is_ok());
assert!(created_session.hash.is_some());
let head_commit = repo
.find_reference(&project.refname())
.unwrap()
.peel_to_commit()
.unwrap();
assert_eq!(head_commit.author().name().unwrap(), "test");
assert_eq!(head_commit.author().email().unwrap(), "test@email.com");
assert_eq!(head_commit.committer().name().unwrap(), "gitbutler");
assert_eq!(
head_commit.committer().email().unwrap(),
"gitbutler@localhost"
);
let current_session = super::sessions::Session::current(&repo, &project);
assert!(current_session.is_ok());
let current_session = current_session.unwrap();