Skip to content

Commit

Permalink
feat(git): apply pgp sig event tag to commit
Browse files Browse the repository at this point in the history
to maintain correct commit ids which is required to apply multiple
commits

its noted that no tests are written and the scenario where the author
and committer differ has not been tested

clearly the validate_patch_applied function has code that corrects an
error where the author / committer 'signatures' do not apply correctly.
this will not be fixed under a pgp signed commit scenario.
  • Loading branch information
DanConwayDev committed Jan 22, 2024
1 parent 675d44b commit c543b2f
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,14 +465,51 @@ fn apply_patch(git_repo: &Repo, patch: &nostr::Event) -> Result<()> {
index.add_all(["."], git2::IndexAddOption::DEFAULT, None)?;
index.write()?;

git_repo.git_repo.commit(
Some("HEAD"),
&extract_sig_from_patch_tags(&patch.tags, "author")?,
&extract_sig_from_patch_tags(&patch.tags, "committer")?,
tag_value(patch, "description")?.as_str(),
&git_repo.git_repo.find_tree(index.write_tree()?)?,
&[&prev_oid],
)?;
let pgp_sig = if let Ok(pgp_sig) = tag_value(patch, "commit-sig") {
if pgp_sig.is_empty() {
None
} else {
Some(pgp_sig)
}
} else {
None
};

if let Some(pgp_sig) = pgp_sig {
let commit_buff = git_repo.git_repo.commit_create_buffer(
&extract_sig_from_patch_tags(&patch.tags, "author")?,
&extract_sig_from_patch_tags(&patch.tags, "committer")?,
tag_value(patch, "description")?.as_str(),
&git_repo.git_repo.find_tree(index.write_tree()?)?,
&[&prev_oid],
)?;
let gpg_commit_id = git_repo.git_repo.commit_signed(
commit_buff.as_str().unwrap(),
pgp_sig.as_str(),
None,
)?;
git_repo.git_repo.reset(
&git_repo.git_repo.find_object(gpg_commit_id, None)?,
git2::ResetType::Mixed,
None,
)?;
if gpg_commit_id.to_string().eq(&tag_value(patch, "commit")?) {
return Ok(());
}
} else {
git_repo.git_repo.commit(
Some("HEAD"),
&extract_sig_from_patch_tags(&patch.tags, "author")?,
&extract_sig_from_patch_tags(&patch.tags, "committer")?,
tag_value(patch, "description")?.as_str(),
&git_repo.git_repo.find_tree(index.write_tree()?)?,
&[&prev_oid],
)?;
}
validate_patch_applied(git_repo, patch)
}

fn validate_patch_applied(git_repo: &Repo, patch: &nostr::Event) -> Result<()> {
// end of stage and commit
// check commit applied
if git_repo
Expand Down

0 comments on commit c543b2f

Please sign in to comment.