Skip to content

Commit

Permalink
Sign commits in advanced operations
Browse files Browse the repository at this point in the history
  • Loading branch information
OdNairy committed Sep 30, 2022
1 parent 7e17d4e commit ac1ce50
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
30 changes: 22 additions & 8 deletions GitUpKit/Core/GCRepository+Bare.m
Original file line number Diff line number Diff line change
Expand Up @@ -376,23 +376,21 @@ - (GCCommit*)createCommitFromTree:(git_tree*)tree
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_signature_default, &signature, self.private);

git_buf commitBuffer = GIT_BUF_INIT;
CALL_LIBGIT2_FUNCTION_GOTO(cleanupBuffer, git_commit_create_buffer, &commitBuffer, self.private, author ? author : signature, signature, NULL, GCCleanedUpCommitMessage(message).bytes, tree, count, parents);
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_create_buffer, &commitBuffer, self.private, author ? author : signature, signature, NULL, GCCleanedUpCommitMessage(message).bytes, tree, count, parents);

if (shouldSign) {
GCConfigOption* signingKeyOption = [self readConfigOptionForVariable:@"user.signingkey" error:nil];
gpgSignature = [self gpgSig:commitBuffer.ptr keyId:signingKeyOption.value];
}

CALL_LIBGIT2_FUNCTION_GOTO(cleanupBuffer, git_commit_create_with_signature, &oid, self.private, commitBuffer.ptr, gpgSignature, NULL);
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_create_with_signature, &oid, self.private, commitBuffer.ptr, gpgSignature, NULL);

git_commit* newCommit = NULL;
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_lookup, &newCommit, self.private, &oid);
commit = [[GCCommit alloc] initWithRepository:self commit:newCommit];

cleanupBuffer:
git_buf_dispose(&commitBuffer);

cleanup:
git_buf_dispose(&commitBuffer);
git_signature_free(signature);
return commit;
}
Expand Down Expand Up @@ -479,20 +477,36 @@ - (GCCommit*)createCommitFromCommit:(git_commit*)commit
git_signature* signature = NULL;
git_oid oid;

GCConfigOption* shouldSignOption = [self readConfigOptionForVariable:@"commit.gpgsign" error:nil];

if (updateCommitter) {
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_signature_default, &signature, self.private);
}

CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_create_from_callback, &oid, self.private, NULL,
git_buf commitBuffer = GIT_BUF_INIT;
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_create_buffer_for_parents_cb, &commitBuffer, self.private,
git_commit_author(commit),
updateCommitter ? signature : git_commit_committer(commit),
message ? NULL : git_commit_message_encoding(commit), message ? GCCleanedUpCommitMessage(message).bytes : git_commit_message(commit),
message ? NULL : git_commit_message_encoding(commit),
message ? GCCleanedUpCommitMessage(message).bytes : git_commit_message(commit),
git_tree_id(tree),
parents ? _CommitParentCallback_Parents : _CommitParentCallback_Commit, parents ? (__bridge void*)parents : (void*)commit);
parents ? _CommitParentCallback_Parents : _CommitParentCallback_Commit, parents ? (__bridge void*)parents : (void*)commit,
true);

const char *gpgSignature = NULL;
if ([shouldSignOption.value isEqualToString:@"true"]) {
GCConfigOption* signingKeyOption = [self readConfigOptionForVariable:@"user.signingkey" error:nil];

gpgSignature = [self gpgSig:commitBuffer.ptr keyId:signingKeyOption.value];
}

CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_create_with_signature, &oid, self.private, commitBuffer.ptr, gpgSignature, NULL);

CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_lookup, &newCommit, self.private, &oid);
XLOG_DEBUG_CHECK(!git_oid_equal(git_commit_id(newCommit), git_commit_id(commit)));

cleanup:
git_buf_dispose(&commitBuffer);
git_signature_free(signature);
return newCommit ? [[GCCommit alloc] initWithRepository:self commit:newCommit] : nil;
}
Expand Down
2 changes: 1 addition & 1 deletion GitUpKit/Third-Party/libgit2

0 comments on commit ac1ce50

Please sign in to comment.