mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-09-11 12:25:42 +03:00
derive commit author from user
This commit is contained in:
parent
b0bb3b00f4
commit
9c0228346d
@ -501,7 +501,7 @@ fn flush(
|
|||||||
.write()
|
.write()
|
||||||
.with_context(|| format!("failed to write tree"))?;
|
.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!(
|
format!(
|
||||||
"failed to write gb commit for {}",
|
"failed to write gb commit for {}",
|
||||||
repo.workdir().unwrap().display()
|
repo.workdir().unwrap().display()
|
||||||
@ -836,20 +836,26 @@ fn add_session_path(
|
|||||||
fn write_gb_commit(
|
fn write_gb_commit(
|
||||||
gb_tree: git2::Oid,
|
gb_tree: git2::Oid,
|
||||||
repo: &git2::Repository,
|
repo: &git2::Repository,
|
||||||
|
user: &Option<users::User>,
|
||||||
project: &projects::Project,
|
project: &projects::Project,
|
||||||
) -> Result<git2::Oid, git2::Error> {
|
) -> Result<git2::Oid, git2::Error> {
|
||||||
// find the Oid of the commit that refs/.../current points to, none if it doesn't exist
|
// find the Oid of the commit that refs/.../current points to, none if it doesn't exist
|
||||||
let refname = project.refname();
|
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()) {
|
match repo.revparse_single(refname.as_str()) {
|
||||||
Ok(obj) => {
|
Ok(obj) => {
|
||||||
let last_commit = repo.find_commit(obj.id()).unwrap();
|
let last_commit = repo.find_commit(obj.id()).unwrap();
|
||||||
let new_commit = repo.commit(
|
let new_commit = repo.commit(
|
||||||
Some(refname.as_str()),
|
Some(refname.as_str()),
|
||||||
&signature, // author
|
&author, // author
|
||||||
&signature, // committer
|
&comitter, // committer
|
||||||
"gitbutler check", // commit message
|
"gitbutler check", // commit message
|
||||||
&repo.find_tree(gb_tree).unwrap(), // tree
|
&repo.find_tree(gb_tree).unwrap(), // tree
|
||||||
&[&last_commit], // parents
|
&[&last_commit], // parents
|
||||||
@ -859,8 +865,8 @@ fn write_gb_commit(
|
|||||||
Err(_) => {
|
Err(_) => {
|
||||||
let new_commit = repo.commit(
|
let new_commit = repo.commit(
|
||||||
Some(refname.as_str()),
|
Some(refname.as_str()),
|
||||||
&signature, // author
|
&author, // author
|
||||||
&signature, // committer
|
&comitter, // committer
|
||||||
"gitbutler check", // commit message
|
"gitbutler check", // commit message
|
||||||
&repo.find_tree(gb_tree).unwrap(), // tree
|
&repo.find_tree(gb_tree).unwrap(), // tree
|
||||||
&[], // parents
|
&[], // parents
|
||||||
|
@ -1,9 +1,22 @@
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::projects;
|
use crate::{projects, users};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use tempfile::tempdir;
|
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)> {
|
fn test_project() -> Result<(git2::Repository, projects::Project)> {
|
||||||
let path = tempdir()?.path().to_str().unwrap().to_string();
|
let path = tempdir()?.path().to_str().unwrap().to_string();
|
||||||
std::fs::create_dir_all(&path)?;
|
std::fs::create_dir_all(&path)?;
|
||||||
@ -87,6 +100,49 @@ fn test_flush() {
|
|||||||
assert!(flush_result.is_ok());
|
assert!(flush_result.is_ok());
|
||||||
assert!(created_session.hash.is_some());
|
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);
|
let current_session = super::sessions::Session::current(&repo, &project);
|
||||||
assert!(current_session.is_ok());
|
assert!(current_session.is_ok());
|
||||||
let current_session = current_session.unwrap();
|
let current_session = current_session.unwrap();
|
||||||
|
Loading…
Reference in New Issue
Block a user