Skip to content

Commit

Permalink
Merge pull request extrawurst#1 from utkarshgupta137/gpg-commit-signing
Browse files Browse the repository at this point in the history
add workaround for amending & rewording signed commits
  • Loading branch information
hendrikmaus committed Jun 8, 2023
2 parents ec8f456 + 5c568d2 commit 24bb8ac
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
12 changes: 12 additions & 0 deletions asyncgit/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ pub enum Error {
///
#[error("sign error: {0}")]
Sign(#[from] crate::sync::sign::SignError),

///
#[error("amend error: config commit.gpgsign=true detected.\ngpg signing is not supported for amending non-last commits")]
SignAmendNonLastCommit,

///
#[error("reword error: config commit.gpgsign=true detected.\ngpg signing is not supported for rewording non-last commits")]
SignRewordNonLastCommit,

///
#[error("reword error: config commit.gpgsign=true detected.\ngpg signing is not supported for rewording commits with staged changes\ntry unstaging or stashing your changes")]
SignRewordLastCommitStaged,
}

///
Expand Down
17 changes: 16 additions & 1 deletion asyncgit/src/sync/commit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{CommitId, RepoPath};
use crate::sync::sign::{SignBuilder, SignError};
use crate::{
error::Result,
error::{Error, Result},
sync::{repository::repo, utils::get_head_repo},
};
use git2::{ErrorCode, ObjectType, Repository, Signature};
Expand All @@ -16,12 +16,27 @@ pub fn amend(
scope_time!("amend");

let repo = repo(repo_path)?;
let config = repo.config()?;

let commit = repo.find_commit(id.into())?;

let mut index = repo.index()?;
let tree_id = index.write_tree()?;
let tree = repo.find_tree(tree_id)?;

if config.get_bool("commit.gpgsign").unwrap_or(false) {
// HACK: we undo the last commit and create a new one
use crate::sync::utils::undo_last_commit;

let head = get_head_repo(&repo)?;
if head == commit.id().into() {
undo_last_commit(repo_path)?;
return self::commit(repo_path, msg);
}

return Err(Error::SignAmendNonLastCommit);
}

let new_id = commit.amend(
Some("HEAD"),
None,
Expand Down
28 changes: 27 additions & 1 deletion asyncgit/src/sync/reword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use git2::{Oid, RebaseOptions, Repository};
use super::{
commit::signature_allow_undefined_name,
repo,
utils::{bytes2string, get_head_refname},
utils::{bytes2string, get_head_refname, get_head_repo},
CommitId, RepoPath,
};
use crate::error::{Error, Result};
Expand All @@ -15,6 +15,32 @@ pub fn reword(
message: &str,
) -> Result<CommitId> {
let repo = repo(repo_path)?;
let config = repo.config()?;

if config.get_bool("commit.gpgsign").unwrap_or(false) {
// HACK: we undo the last commit and create a new one
use crate::sync::utils::undo_last_commit;

let head = get_head_repo(&repo)?;
if head == commit {
// Check if there are any staged changes
let parent = repo.find_commit(head.into())?;
let tree = parent.tree()?;
if repo
.diff_tree_to_index(Some(&tree), None, None)?
.deltas()
.len() == 0
{
undo_last_commit(repo_path)?;
return super::commit(repo_path, message);
}

return Err(Error::SignRewordLastCommitStaged);
}

return Err(Error::SignRewordNonLastCommit);
}

let cur_branch_ref = get_head_refname(&repo)?;

match reword_internal(&repo, commit.get_oid(), message) {
Expand Down

0 comments on commit 24bb8ac

Please sign in to comment.