Skip to content

Commit 77f9890

Browse files
committed
Hide amend parent handling in signing helper
1 parent b5d973f commit 77f9890

3 files changed

Lines changed: 43 additions & 29 deletions

File tree

GitUpKit/Core/GCCommitSigning.m

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ static BOOL _LooksLikeInlineSSHKey(NSString* key) {
238238
git_signature* signature = NULL;
239239
git_commit* newCommit = NULL;
240240
NSData* cleanedMessage = nil;
241+
const char* cleanedMessageBytes = NULL;
241242
#if !TARGET_OS_IPHONE
242243
git_buf commitBuffer = {0};
243244
NSData* commitData = nil;
@@ -249,13 +250,14 @@ static BOOL _LooksLikeInlineSSHKey(NSString* key) {
249250
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_signature_default, &signature, repository.private);
250251
authorSignature = author ?: signature;
251252
cleanedMessage = GCCleanedUpCommitMessage(message);
253+
cleanedMessageBytes = (const char*)cleanedMessage.bytes;
252254
#if !TARGET_OS_IPHONE
253255
if (!_ShouldSSHSignCommit(repository, &shouldSign, error)) {
254256
goto cleanup;
255257
}
256258

257259
if (shouldSign) {
258-
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_create_buffer, &commitBuffer, repository.private, authorSignature, signature, NULL, cleanedMessage.bytes, tree, count, parents);
260+
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_create_buffer, &commitBuffer, repository.private, authorSignature, signature, NULL, cleanedMessageBytes, tree, count, parents);
259261
commitData = [[NSData alloc] initWithBytes:commitBuffer.ptr length:commitBuffer.size];
260262
sshSignature = _SSHSignatureForCommitBuffer(repository, commitData, error);
261263
if (!sshSignature) {
@@ -264,7 +266,7 @@ static BOOL _LooksLikeInlineSSHKey(NSString* key) {
264266
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_create_with_signature, &oid, repository.private, commitBuffer.ptr, sshSignature.UTF8String, "gpgsig");
265267
} else {
266268
#endif
267-
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_create, &oid, repository.private, NULL, authorSignature, signature, NULL, cleanedMessage.bytes, tree, count, parents);
269+
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_create, &oid, repository.private, NULL, authorSignature, signature, NULL, cleanedMessageBytes, tree, count, parents);
268270
#if !TARGET_OS_IPHONE
269271
}
270272
#endif
@@ -280,3 +282,40 @@ static BOOL _LooksLikeInlineSSHKey(NSString* key) {
280282
git_signature_free(signature);
281283
return commit;
282284
}
285+
286+
GCCommit* GCCreateCommitFromCommitWithIndexAndOptionalSignature(GCRepository* repository, git_commit* amendedCommit, git_index* index, NSString* message, NSError** error) {
287+
GCCommit* commit = nil;
288+
git_tree* tree = NULL;
289+
git_commit** parentCommits = NULL;
290+
unsigned int parentCount = 0;
291+
const git_commit** parents = NULL;
292+
293+
git_oid oid;
294+
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_index_write_tree_to, &oid, index, repository.private);
295+
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_tree_lookup, &tree, repository.private, &oid);
296+
297+
parentCount = git_commit_parentcount(amendedCommit);
298+
if (parentCount) {
299+
parentCommits = (git_commit**)calloc(parentCount, sizeof(git_commit*));
300+
if (!parentCommits) {
301+
GC_SET_GENERIC_ERROR(@"Unable to allocate commit parent list");
302+
goto cleanup;
303+
}
304+
for (unsigned int i = 0; i < parentCount; ++i) {
305+
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_parent, &parentCommits[i], amendedCommit, i);
306+
}
307+
}
308+
parents = (const git_commit**)parentCommits;
309+
310+
commit = GCCreateCommitFromTreeWithOptionalSignature(repository, tree, parents, parentCount, git_commit_author(amendedCommit), message, error);
311+
312+
cleanup:
313+
if (parentCommits) {
314+
for (unsigned int i = 0; i < parentCount; ++i) {
315+
git_commit_free(parentCommits[i]);
316+
}
317+
free(parentCommits);
318+
}
319+
git_tree_free(tree);
320+
return commit;
321+
}

GitUpKit/Core/GCPrivate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ extern BOOL GCGitOIDFromSHA1(NSString* sha1, git_oid* oid, NSError** error);
119119
extern BOOL GCGitOIDFromSHA1Prefix(NSString* prefix, git_oid* oid, NSError** error);
120120
extern NSData* GCCleanedUpCommitMessage(NSString* message);
121121
extern GCCommit* GCCreateCommitFromTreeWithOptionalSignature(GCRepository* repository, git_tree* tree, const git_commit** parents, NSUInteger count, const git_signature* author, NSString* message, NSError** error);
122+
extern GCCommit* GCCreateCommitFromCommitWithIndexAndOptionalSignature(GCRepository* repository, git_commit* commit, git_index* index, NSString* message, NSError** error);
122123
extern NSString* GCUserFromSignature(const git_signature* signature);
123124
extern const void* GCOIDCopyCallBack(CFAllocatorRef allocator, const void* value);
124125
extern Boolean GCOIDEqualCallBack(const void* value1, const void* value2);

GitUpKit/Core/GCRepository+HEAD.m

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ - (GCCommit*)createCommitFromHEADAndOtherParent:(GCCommit*)parent withMessage:(N
130130
goto cleanup;
131131
}
132132

133-
// The signing helper creates a commit object from an explicit libgit2 parent list; this array covers both normal and merge commits.
134133
parents[0] = headCommit;
135134
parents[1] = parent.private;
136135
commit = _CreateCommitFromIndex(self, index, parents, (headCommit ? (parent ? 2 : 1) : 0), NULL, message, error);
@@ -168,9 +167,6 @@ - (GCCommit*)createCommitByAmendingHEADWithMessage:(NSString*)message error:(NSE
168167
git_reference* headReference = NULL;
169168
git_commit* headCommit = NULL;
170169
git_index* index = NULL;
171-
git_tree* tree = NULL;
172-
git_commit** parentCommits = NULL;
173-
unsigned int parentCount = 0;
174170
NSString* reflogMessage;
175171

176172
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_repository_head, &headReference, self.private); // Returns a direct reference or GIT_EUNBORNBRANCH
@@ -182,22 +178,7 @@ - (GCCommit*)createCommitByAmendingHEADWithMessage:(NSString*)message error:(NSE
182178
goto cleanup;
183179
}
184180

185-
git_oid oid;
186-
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_index_write_tree_to, &oid, index, self.private);
187-
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_tree_lookup, &tree, self.private, &oid);
188-
parentCount = git_commit_parentcount(headCommit);
189-
if (parentCount) {
190-
parentCommits = (git_commit**)calloc(parentCount, sizeof(git_commit*));
191-
if (!parentCommits) {
192-
GC_SET_GENERIC_ERROR(@"Unable to allocate commit parent list");
193-
goto cleanup;
194-
}
195-
for (unsigned int i = 0; i < parentCount; ++i) {
196-
CALL_LIBGIT2_FUNCTION_GOTO(cleanup, git_commit_parent, &parentCommits[i], headCommit, i);
197-
}
198-
}
199-
// Amending preserves HEAD's original parents while creating a new commit object, so keep these parent commits alive through signing.
200-
commit = GCCreateCommitFromTreeWithOptionalSignature(self, tree, (const git_commit**)parentCommits, parentCount, git_commit_author(headCommit), message, error);
181+
commit = GCCreateCommitFromCommitWithIndexAndOptionalSignature(self, headCommit, index, message, error);
201182
if (commit == nil) {
202183
goto cleanup;
203184
}
@@ -213,13 +194,6 @@ - (GCCommit*)createCommitByAmendingHEADWithMessage:(NSString*)message error:(NSE
213194
success = YES;
214195

215196
cleanup:
216-
if (parentCommits) {
217-
for (unsigned int i = 0, count = headCommit ? git_commit_parentcount(headCommit) : 0; i < count; ++i) {
218-
git_commit_free(parentCommits[i]);
219-
}
220-
free(parentCommits);
221-
}
222-
git_tree_free(tree);
223197
git_index_free(index);
224198
git_commit_free(headCommit);
225199
git_reference_free(headReference);

0 commit comments

Comments
 (0)