|
| 1 | +use anyhow::{anyhow, Result}; |
| 2 | +use gitbutler_commit::commit_headers::CommitHeadersV2; |
| 3 | +use gitbutler_config::git::{GbConfig, GitConfig as _}; |
| 4 | +use gitbutler_error::error::Code; |
| 5 | +use gitbutler_oxidize::{git2_signature_to_gix_signature, git2_to_gix_object_id, gix_to_git2_oid}; |
| 6 | +use gitbutler_reference::Refname; |
| 7 | +use gix::objs::WriteTo as _; |
| 8 | + |
| 9 | +use crate::sigining::sign_buffer; |
| 10 | + |
| 11 | +pub trait RepositoryExt { |
| 12 | + #[allow(clippy::too_many_arguments)] |
| 13 | + fn commit_with_signature( |
| 14 | + &self, |
| 15 | + update_ref: Option<&Refname>, |
| 16 | + author: &git2::Signature<'_>, |
| 17 | + committer: &git2::Signature<'_>, |
| 18 | + message: &str, |
| 19 | + tree: &git2::Tree<'_>, |
| 20 | + parents: &[&git2::Commit<'_>], |
| 21 | + commit_headers: Option<CommitHeadersV2>, |
| 22 | + ) -> Result<git2::Oid>; |
| 23 | +} |
| 24 | + |
| 25 | +impl RepositoryExt for git2::Repository { |
| 26 | + #[allow(clippy::too_many_arguments)] |
| 27 | + fn commit_with_signature( |
| 28 | + &self, |
| 29 | + update_ref: Option<&Refname>, |
| 30 | + author: &git2::Signature<'_>, |
| 31 | + committer: &git2::Signature<'_>, |
| 32 | + message: &str, |
| 33 | + tree: &git2::Tree<'_>, |
| 34 | + parents: &[&git2::Commit<'_>], |
| 35 | + commit_headers: Option<CommitHeadersV2>, |
| 36 | + ) -> Result<git2::Oid> { |
| 37 | + let repo = gix::open(self.path())?; |
| 38 | + let mut commit = gix::objs::Commit { |
| 39 | + message: message.into(), |
| 40 | + tree: git2_to_gix_object_id(tree.id()), |
| 41 | + author: git2_signature_to_gix_signature(author), |
| 42 | + committer: git2_signature_to_gix_signature(committer), |
| 43 | + encoding: None, |
| 44 | + parents: parents |
| 45 | + .iter() |
| 46 | + .map(|commit| git2_to_gix_object_id(commit.id())) |
| 47 | + .collect(), |
| 48 | + extra_headers: commit_headers.unwrap_or_default().into(), |
| 49 | + }; |
| 50 | + |
| 51 | + if self.gb_config()?.sign_commits.unwrap_or(false) { |
| 52 | + let mut buf = Vec::new(); |
| 53 | + commit.write_to(&mut buf)?; |
| 54 | + let signature = sign_buffer(self, &buf); |
| 55 | + match signature { |
| 56 | + Ok(signature) => { |
| 57 | + commit.extra_headers.push(("gpgsig".into(), signature)); |
| 58 | + } |
| 59 | + Err(e) => { |
| 60 | + // If signing fails, set the "gitbutler.signCommits" config to false before erroring out |
| 61 | + self.set_gb_config(GbConfig { |
| 62 | + sign_commits: Some(false), |
| 63 | + ..GbConfig::default() |
| 64 | + })?; |
| 65 | + return Err( |
| 66 | + anyhow!("Failed to sign commit: {}", e).context(Code::CommitSigningFailed) |
| 67 | + ); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + // TODO: extra-headers should be supported in `gix` directly. |
| 72 | + let oid = gix_to_git2_oid(repo.write_object(&commit)?); |
| 73 | + |
| 74 | + // update reference |
| 75 | + if let Some(refname) = update_ref { |
| 76 | + self.reference(&refname.to_string(), oid, true, message)?; |
| 77 | + } |
| 78 | + Ok(oid) |
| 79 | + } |
| 80 | +} |
0 commit comments