From 96d073e898653cb04d88038865e9b4dc73980df1 Mon Sep 17 00:00:00 2001 From: Roman Gardukevich Date: Thu, 22 Sep 2022 11:53:23 +0300 Subject: [PATCH] Sign commits in advanced operations --- GitUpKit/Core/GCRepository+Bare.m | 25 +++++++++++++++++++++++-- GitUpKit/Core/GCRepository+Mock.m | 2 +- GitUpKit/Third-Party/libgit2 | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/GitUpKit/Core/GCRepository+Bare.m b/GitUpKit/Core/GCRepository+Bare.m index b933d1a2..abc15d91 100644 --- a/GitUpKit/Core/GCRepository+Bare.m +++ b/GitUpKit/Core/GCRepository+Bare.m @@ -376,7 +376,7 @@ - (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(cleanupBuffer, git_commit_create_buffer, &commitBuffer, self.private, author ? author : signature, signature, NULL, GCCleanedUpCommitMessage(message).bytes, NULL, tree, count, parents); if (shouldSign) { GCConfigOption* signingKeyOption = [self readConfigOptionForVariable:@"user.signingkey" error:nil]; @@ -479,14 +479,35 @@ - (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); } + git_buf commitBuffer = GIT_BUF_INIT; + CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_create_buffer_for_signature, &commitBuffer, &oid, self.private, NULL, + 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), + git_tree_id(tree), + 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_from_callback, &oid, self.private, NULL, 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), + gpgSignature, git_tree_id(tree), parents ? _CommitParentCallback_Parents : _CommitParentCallback_Commit, parents ? (__bridge void*)parents : (void*)commit); CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_lookup, &newCommit, self.private, &oid); diff --git a/GitUpKit/Core/GCRepository+Mock.m b/GitUpKit/Core/GCRepository+Mock.m index 09054e62..d0d0ba43 100644 --- a/GitUpKit/Core/GCRepository+Mock.m +++ b/GitUpKit/Core/GCRepository+Mock.m @@ -145,7 +145,7 @@ - (NSArray*)createMockCommitHierarchyFromNotation:(NSString*)notation force:(BOO CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_signature_new, &signature, "user", "user@domain.com", NSTimeIntervalSince1970 + commits.count, 0); git_oid oid; void* params[] = {cache, (__bridge void*)parents}; - CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_create_from_callback, &oid, self.private, NULL, signature, signature, NULL, message.UTF8String, &treeOID, _CommitParentCallback, params); + CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_create_from_callback, &oid, self.private, NULL, signature, signature, NULL, message.UTF8String, NULL, &treeOID, _CommitParentCallback, params); git_signature_free(signature); signature = NULL; git_commit* emptyCommit; diff --git a/GitUpKit/Third-Party/libgit2 b/GitUpKit/Third-Party/libgit2 index d7392521..79424722 160000 --- a/GitUpKit/Third-Party/libgit2 +++ b/GitUpKit/Third-Party/libgit2 @@ -1 +1 @@ -Subproject commit d7392521782deb1ff59c21da64df137bf64e9d53 +Subproject commit 794247222c358be1d5b3044114c3b7bd2b96630a