Refactor - move commit signature code out of branch crate

This commit is contained in:
Kiril Videlov 2024-10-22 14:41:35 +02:00
parent 5d2f4436e4
commit 95d4fc15d0
10 changed files with 51 additions and 50 deletions

1
Cargo.lock generated
View File

@ -2510,7 +2510,6 @@ dependencies = [
"base64 0.22.1", "base64 0.22.1",
"bstr", "bstr",
"git2", "git2",
"gitbutler-branch",
"gitbutler-cherry-pick", "gitbutler-cherry-pick",
"gitbutler-command-context", "gitbutler-command-context",
"gitbutler-commit", "gitbutler-commit",

View File

@ -2,12 +2,13 @@ use std::path::PathBuf;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use git2::Commit; use git2::Commit;
use gitbutler_branch::{BranchExt, SignaturePurpose}; use gitbutler_branch::BranchExt;
use gitbutler_commit::commit_headers::CommitHeadersV2; use gitbutler_commit::commit_headers::CommitHeadersV2;
use gitbutler_oplog::SnapshotExt; use gitbutler_oplog::SnapshotExt;
use gitbutler_project::access::WorktreeWritePermission; use gitbutler_project::access::WorktreeWritePermission;
use gitbutler_reference::{normalize_branch_name, ReferenceName, Refname}; use gitbutler_reference::{normalize_branch_name, ReferenceName, Refname};
use gitbutler_repo::RepositoryExt; use gitbutler_repo::RepositoryExt;
use gitbutler_repo::SignaturePurpose;
use gitbutler_repo_actions::RepoActionsExt; use gitbutler_repo_actions::RepoActionsExt;
use gitbutler_stack::{Stack, StackId}; use gitbutler_stack::{Stack, StackId};
use tracing::instrument; use tracing::instrument;
@ -189,8 +190,8 @@ impl BranchManager<'_> {
message.push_str("\n\n"); message.push_str("\n\n");
// Commit wip commit // Commit wip commit
let committer = gitbutler_branch::signature(SignaturePurpose::Committer)?; let committer = gitbutler_repo::signature(SignaturePurpose::Committer)?;
let author = gitbutler_branch::signature(SignaturePurpose::Author)?; let author = gitbutler_repo::signature(SignaturePurpose::Author)?;
let parent = branch.get().peel_to_commit()?; let parent = branch.get().peel_to_commit()?;
let commit_headers = CommitHeadersV2::new(); let commit_headers = CommitHeadersV2::new();

View File

@ -3,13 +3,14 @@ use std::{path::PathBuf, vec};
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use bstr::ByteSlice; use bstr::ByteSlice;
use gitbutler_branch::BranchCreateRequest; use gitbutler_branch::BranchCreateRequest;
use gitbutler_branch::{self, SignaturePurpose, GITBUTLER_WORKSPACE_REFERENCE}; use gitbutler_branch::{self, GITBUTLER_WORKSPACE_REFERENCE};
use gitbutler_cherry_pick::RepositoryExt as _; use gitbutler_cherry_pick::RepositoryExt as _;
use gitbutler_command_context::CommandContext; use gitbutler_command_context::CommandContext;
use gitbutler_commit::commit_ext::CommitExt; use gitbutler_commit::commit_ext::CommitExt;
use gitbutler_error::error::Marker; use gitbutler_error::error::Marker;
use gitbutler_operating_modes::OPEN_WORKSPACE_REFS; use gitbutler_operating_modes::OPEN_WORKSPACE_REFS;
use gitbutler_project::access::WorktreeWritePermission; use gitbutler_project::access::WorktreeWritePermission;
use gitbutler_repo::SignaturePurpose;
use gitbutler_repo::{LogUntil, RepositoryExt}; use gitbutler_repo::{LogUntil, RepositoryExt};
use gitbutler_stack::{Stack, VirtualBranchesHandle}; use gitbutler_stack::{Stack, VirtualBranchesHandle};
use gitbutler_stack_api::StackExt; use gitbutler_stack_api::StackExt;
@ -67,8 +68,8 @@ pub(crate) fn get_workspace_head(ctx: &CommandContext) -> Result<git2::Oid> {
} }
} }
let committer = gitbutler_branch::signature(SignaturePurpose::Committer)?; let committer = gitbutler_repo::signature(SignaturePurpose::Committer)?;
let author = gitbutler_branch::signature(SignaturePurpose::Author)?; let author = gitbutler_repo::signature(SignaturePurpose::Author)?;
let mut heads: Vec<git2::Commit<'_>> = virtual_branches let mut heads: Vec<git2::Commit<'_>> = virtual_branches
.iter() .iter()
.filter(|b| b.head() != target.sha) .filter(|b| b.head() != target.sha)
@ -206,8 +207,8 @@ pub fn update_workspace_commit(
message.push_str("For more information about what we're doing here, check out our docs:\n"); message.push_str("For more information about what we're doing here, check out our docs:\n");
message.push_str("https://docs.gitbutler.com/features/virtual-branches/integration-branch\n"); message.push_str("https://docs.gitbutler.com/features/virtual-branches/integration-branch\n");
let committer = gitbutler_branch::signature(SignaturePurpose::Committer)?; let committer = gitbutler_repo::signature(SignaturePurpose::Committer)?;
let author = gitbutler_branch::signature(SignaturePurpose::Author)?; let author = gitbutler_repo::signature(SignaturePurpose::Author)?;
// It would be nice if we could pass an `update_ref` parameter to this function, but that // It would be nice if we could pass an `update_ref` parameter to this function, but that
// requires committing to the tip of the branch, and we're mostly replacing the tip. // requires committing to the tip of the branch, and we're mostly replacing the tip.

View File

@ -8,40 +8,8 @@ mod branch;
pub mod serde; pub mod serde;
pub use branch::{BranchCreateRequest, BranchIdentity, BranchUpdateRequest}; pub use branch::{BranchCreateRequest, BranchIdentity, BranchUpdateRequest};
use gitbutler_oxidize::gix_to_git2_signature;
use lazy_static::lazy_static; use lazy_static::lazy_static;
lazy_static! { lazy_static! {
pub static ref GITBUTLER_WORKSPACE_REFERENCE: gitbutler_reference::LocalRefname = pub static ref GITBUTLER_WORKSPACE_REFERENCE: gitbutler_reference::LocalRefname =
gitbutler_reference::LocalRefname::new("gitbutler/workspace", None); gitbutler_reference::LocalRefname::new("gitbutler/workspace", None);
} }
pub const GITBUTLER_COMMIT_AUTHOR_NAME: &str = "GitButler";
pub const GITBUTLER_COMMIT_AUTHOR_EMAIL: &str = "gitbutler@gitbutler.com";
pub enum SignaturePurpose {
Author,
Committer,
}
/// Provide a signature with the GitButler author, and the current time or the time overridden
/// depending on the value for `purpose`.
pub fn signature(purpose: SignaturePurpose) -> anyhow::Result<git2::Signature<'static>> {
let signature = gix::actor::SignatureRef {
name: GITBUTLER_COMMIT_AUTHOR_NAME.into(),
email: GITBUTLER_COMMIT_AUTHOR_EMAIL.into(),
time: commit_time(match purpose {
SignaturePurpose::Author => "GIT_AUTHOR_DATE",
SignaturePurpose::Committer => "GIT_COMMITTER_DATE",
}),
};
gix_to_git2_signature(signature)
}
/// Return the time of a commit as `now` unless the `overriding_variable_name` contains a parseable date,
/// which is used instead.
fn commit_time(overriding_variable_name: &str) -> gix::date::Time {
std::env::var(overriding_variable_name)
.ok()
.and_then(|time| gix::date::parse(&time, Some(std::time::SystemTime::now())).ok())
.unwrap_or_else(gix::date::Time::now_local_or_utc)
}

View File

@ -5,7 +5,6 @@ use std::str::FromStr;
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use bstr::ByteSlice; use bstr::ByteSlice;
use git2::build::CheckoutBuilder; use git2::build::CheckoutBuilder;
use gitbutler_branch::{signature, SignaturePurpose};
use gitbutler_branch_actions::branch_trees::{ use gitbutler_branch_actions::branch_trees::{
checkout_branch_trees, compute_updated_branch_head, BranchHeadAndTree, checkout_branch_trees, compute_updated_branch_head, BranchHeadAndTree,
}; };
@ -25,6 +24,7 @@ use gitbutler_operating_modes::{
use gitbutler_project::access::{WorktreeReadPermission, WorktreeWritePermission}; use gitbutler_project::access::{WorktreeReadPermission, WorktreeWritePermission};
use gitbutler_reference::{ReferenceName, Refname}; use gitbutler_reference::{ReferenceName, Refname};
use gitbutler_repo::{rebase::cherry_rebase, RepositoryExt}; use gitbutler_repo::{rebase::cherry_rebase, RepositoryExt};
use gitbutler_repo::{signature, SignaturePurpose};
use gitbutler_stack::{Stack, VirtualBranchesHandle}; use gitbutler_stack::{Stack, VirtualBranchesHandle};
use gitbutler_stack_api::StackExt; use gitbutler_stack_api::StackExt;
use serde::Serialize; use serde::Serialize;

View File

@ -8,7 +8,6 @@ use std::{
use anyhow::{anyhow, bail, Context, Result}; use anyhow::{anyhow, bail, Context, Result};
use git2::{DiffOptions, FileMode}; use git2::{DiffOptions, FileMode};
use gitbutler_branch::SignaturePurpose;
use gitbutler_command_context::RepositoryExtLite; use gitbutler_command_context::RepositoryExtLite;
use gitbutler_diff::{hunks_by_filepath, FileDiff}; use gitbutler_diff::{hunks_by_filepath, FileDiff};
use gitbutler_project::{ use gitbutler_project::{
@ -16,6 +15,7 @@ use gitbutler_project::{
Project, Project,
}; };
use gitbutler_repo::RepositoryExt; use gitbutler_repo::RepositoryExt;
use gitbutler_repo::SignaturePurpose;
use gitbutler_stack::{Stack, VirtualBranchesHandle, VirtualBranchesState}; use gitbutler_stack::{Stack, VirtualBranchesHandle, VirtualBranchesState};
use tracing::instrument; use tracing::instrument;
@ -460,8 +460,8 @@ fn commit_snapshot(
.and_then(|head_id| repo.find_commit(head_id).ok()); .and_then(|head_id| repo.find_commit(head_id).ok());
// Construct a new commit // Construct a new commit
let committer = gitbutler_branch::signature(SignaturePurpose::Committer)?; let committer = gitbutler_repo::signature(SignaturePurpose::Committer)?;
let author = gitbutler_branch::signature(SignaturePurpose::Author)?; let author = gitbutler_repo::signature(SignaturePurpose::Author)?;
let parents = oplog_head_commit let parents = oplog_head_commit
.as_ref() .as_ref()
.map(|head| vec![head]) .map(|head| vec![head])

View File

@ -1,8 +1,8 @@
use std::path::Path; use std::path::Path;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use gitbutler_branch::{GITBUTLER_COMMIT_AUTHOR_EMAIL, GITBUTLER_COMMIT_AUTHOR_NAME};
use gitbutler_fs::write; use gitbutler_fs::write;
use gitbutler_repo::{GITBUTLER_COMMIT_AUTHOR_EMAIL, GITBUTLER_COMMIT_AUTHOR_NAME};
use gix::config::tree::Key; use gix::config::tree::Key;
/// Sets a reference to the oplog head commit such that snapshots are reachable and will not be garbage collected. /// Sets a reference to the oplog head commit such that snapshots are reachable and will not be garbage collected.

View File

@ -18,7 +18,7 @@ resolve-path = "0.1.0"
gitbutler-command-context.workspace = true gitbutler-command-context.workspace = true
gitbutler-config.workspace = true gitbutler-config.workspace = true
gitbutler-project.workspace = true gitbutler-project.workspace = true
gitbutler-branch.workspace = true # gitbutler-branch.workspace = true
gitbutler-reference.workspace = true gitbutler-reference.workspace = true
gitbutler-error.workspace = true gitbutler-error.workspace = true
gitbutler-commit.workspace = true gitbutler-commit.workspace = true

View File

@ -13,3 +13,35 @@ mod config;
pub use config::Config; pub use config::Config;
pub mod temporary_workdir; pub mod temporary_workdir;
use gitbutler_oxidize::gix_to_git2_signature;
pub const GITBUTLER_COMMIT_AUTHOR_NAME: &str = "GitButler";
pub const GITBUTLER_COMMIT_AUTHOR_EMAIL: &str = "gitbutler@gitbutler.com";
pub enum SignaturePurpose {
Author,
Committer,
}
/// Provide a signature with the GitButler author, and the current time or the time overridden
/// depending on the value for `purpose`.
pub fn signature(purpose: SignaturePurpose) -> anyhow::Result<git2::Signature<'static>> {
let signature = gix::actor::SignatureRef {
name: GITBUTLER_COMMIT_AUTHOR_NAME.into(),
email: GITBUTLER_COMMIT_AUTHOR_EMAIL.into(),
time: commit_time(match purpose {
SignaturePurpose::Author => "GIT_AUTHOR_DATE",
SignaturePurpose::Committer => "GIT_COMMITTER_DATE",
}),
};
gix_to_git2_signature(signature)
}
/// Return the time of a commit as `now` unless the `overriding_variable_name` contains a parseable date,
/// which is used instead.
fn commit_time(overriding_variable_name: &str) -> gix::date::Time {
std::env::var(overriding_variable_name)
.ok()
.and_then(|time| gix::date::parse(&time, Some(std::time::SystemTime::now())).ok())
.unwrap_or_else(gix::date::Time::now_local_or_utc)
}

View File

@ -5,10 +5,10 @@ use std::os::windows::process::CommandExt;
use std::{io::Write, path::Path, process::Stdio, str}; use std::{io::Write, path::Path, process::Stdio, str};
use crate::Config; use crate::Config;
use crate::SignaturePurpose;
use anyhow::{anyhow, bail, Context, Result}; use anyhow::{anyhow, bail, Context, Result};
use bstr::BString; use bstr::BString;
use git2::{BlameOptions, StatusOptions, Tree}; use git2::{BlameOptions, StatusOptions, Tree};
use gitbutler_branch::SignaturePurpose;
use gitbutler_commit::commit_headers::CommitHeadersV2; use gitbutler_commit::commit_headers::CommitHeadersV2;
use gitbutler_config::git::{GbConfig, GitConfig}; use gitbutler_config::git::{GbConfig, GitConfig};
use gitbutler_error::error::Code; use gitbutler_error::error::Code;
@ -603,9 +603,9 @@ impl RepositoryExt for git2::Repository {
repo.committer() repo.committer()
.transpose()? .transpose()?
.map(gix_to_git2_signature) .map(gix_to_git2_signature)
.unwrap_or_else(|| gitbutler_branch::signature(SignaturePurpose::Committer)) .unwrap_or_else(|| crate::signature(SignaturePurpose::Committer))
} else { } else {
gitbutler_branch::signature(SignaturePurpose::Committer) crate::signature(SignaturePurpose::Committer)
}?; }?;
Ok((author, committer)) Ok((author, committer))