Skip to content

Commit

Permalink
Read key from config
Browse files Browse the repository at this point in the history
  • Loading branch information
OdNairy committed Jul 3, 2020
1 parent 6002962 commit 001b4fc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
2 changes: 2 additions & 0 deletions GitUpKit/Core/GCPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,15 @@ extern int git_submodule_foreach_block(git_repository* repo, int (^block)(git_su
count:(NSUInteger)count
author:(const git_signature*)author
message:(NSString*)message
shouldSign:(BOOL)shouldSign
error:(NSError**)error;

- (GCCommit*)createCommitFromIndex:(git_index*)index
withParents:(const git_commit**)parents
count:(NSUInteger)count
author:(const git_signature*)author
message:(NSString*)message
shouldSign:(BOOL)shouldSign
error:(NSError**)error;

- (GCCommit*)createCommitFromCommit:(git_commit*)commit
Expand Down
1 change: 1 addition & 0 deletions GitUpKit/Core/GCRepository+Bare.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ typedef GCCommit* (^GCConflictHandler)(GCIndex* index, GCCommit* ourCommit, GCCo
- (GCCommit*)createCommitFromIndex:(GCIndex*)index
withParents:(NSArray*)parents
message:(NSString*)message
shouldSign:(BOOL)shouldSign
error:(NSError**)error;

- (GCCommit*)copyCommit:(GCCommit*)copyCommit
Expand Down
34 changes: 25 additions & 9 deletions GitUpKit/Core/GCRepository+Bare.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ - (GCCommit*)_mergeTheirCommit:(git_commit*)theirCommit
commit = handler([[GCIndex alloc] initWithRepository:nil index:index], _CopyCommit(self, ourCommit), _CopyCommit(self, theirCommit), array, message, error); // Doesn't make sense to specify a custom author on conflict anyway
index = NULL; // Ownership has been transferred to GCIndex instance
} else {
commit = [self createCommitFromIndex:index withParents:parents count:count author:author message:message error:error];

BOOL shouldSign = YES;
commit = [self createCommitFromIndex:index withParents:parents count:count author:author message:message shouldSign:shouldSign error:error];
}

cleanup:
Expand Down Expand Up @@ -206,6 +208,7 @@ - (GCCommit*)mergeCommit:(GCCommit*)mergeCommit
- (GCCommit*)createCommitFromIndex:(GCIndex*)index
withParents:(NSArray*)parents
message:(NSString*)message
shouldSign:(BOOL)shouldSign
error:(NSError**)error {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wvla"
Expand All @@ -215,7 +218,7 @@ - (GCCommit*)createCommitFromIndex:(GCIndex*)index
for (GCCommit* parent in parents) {
commits[count++] = parent.private;
}
return [self createCommitFromIndex:index.private withParents:commits count:count author:NULL message:message error:error];
return [self createCommitFromIndex:index.private withParents:commits count:count author:NULL message:message shouldSign:shouldSign error:error];
}

- (GCCommit*)copyCommit:(GCCommit*)copyCommit
Expand Down Expand Up @@ -363,19 +366,22 @@ - (GCCommit*)createCommitFromTree:(git_tree*)tree
count:(NSUInteger)count
author:(const git_signature*)author
message:(NSString*)message
shouldSign:(BOOL)shouldSign
error:(NSError**)error {
GCCommit* commit = nil;
git_signature* signature = NULL;

git_oid oid;
BOOL sign = NO;

GCConfigOption* shouldSignOption = [self readConfigOptionForVariable:@"commit.gpgsign" error:nil];
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_signature_default, &signature, self.private);

git_buf commitBuffer = GIT_BUF_INIT_CONST("", 0);
if (sign) {
if ([shouldSignOption.value isEqualToString:@"true"]) {
GCConfigOption* signingKeyOption = [self readConfigOptionForVariable:@"user.signingkey" error:nil];

CALL_LIBGIT2_FUNCTION_GOTO(cleanupBuffer, git_commit_create_buffer, &commitBuffer, self.private, author ? author : signature, signature, NULL, GCCleanedUpCommitMessage(message).bytes, tree, count, parents);
const char *gpgSignature = [self gpgSig:commitBuffer.ptr];
const char *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);
} else {
CALL_LIBGIT2_FUNCTION_GOTO(cleanupBuffer, git_commit_create, &oid, self.private, NULL, author ? author : signature, signature, NULL, GCCleanedUpCommitMessage(message).bytes, tree, count, parents);
Expand All @@ -393,9 +399,18 @@ - (GCCommit*)createCommitFromTree:(git_tree*)tree
return commit;
}

-(const char*)gpgSig:(const char*)body {
NSArray<GPGKey *>* privateKeys = [GPGKey allSecretKeys];
GPGKey* key = privateKeys.firstObject;
-(const char*)gpgSig:(const char*)body keyId:(NSString*)keyId {
GPGKey *key = nil;
if (keyId.length > 0) {
key = [GPGKey secretKeyForId:keyId];
}
if (key == nil) {
key = [[GPGKey allSecretKeys] firstObject];
}
if (key == nil) {
return NULL;
}

NSString* plainToSign = [[NSString alloc] initWithCString:body encoding:NSUTF8StringEncoding];
NSString* signature = [key signSignature:plainToSign];

Expand All @@ -407,6 +422,7 @@ - (GCCommit*)createCommitFromIndex:(git_index*)index
count:(NSUInteger)count
author:(const git_signature*)author
message:(NSString*)message
shouldSign:(BOOL)shouldSign
error:(NSError**)error {
GCCommit* commit = nil;
git_tree* tree = NULL;
Expand All @@ -415,7 +431,7 @@ - (GCCommit*)createCommitFromIndex:(git_index*)index
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_index_write_tree_to, &oid, index, self.private);
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_tree_lookup, &tree, self.private, &oid);

commit = [self createCommitFromTree:tree withParents:parents count:count author:author message:message error:error];
commit = [self createCommitFromTree:tree withParents:parents count:count author:author message:message shouldSign:shouldSign error:error];

cleanup:
git_tree_free(tree);
Expand Down
3 changes: 2 additions & 1 deletion GitUpKit/Core/GCRepository+HEAD.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ - (GCCommit*)createCommitFromHEADAndOtherParent:(GCCommit*)parent withMessage:(N
}

const git_commit* parents[2] = {headCommit, parent.private};
commit = [self createCommitFromIndex:index withParents:parents count:(headCommit ? (parent ? 2 : 1) : 0)author:NULL message:message error:error];
BOOL shouldSign = YES;
commit = [self createCommitFromIndex:index withParents:parents count:(headCommit ? (parent ? 2 : 1) : 0)author:NULL message:message shouldSign:shouldSign error:error];
if (commit == nil) {
goto cleanup;
}
Expand Down

0 comments on commit 001b4fc

Please sign in to comment.