From f09bdddce153b630162c7114b0c56e8257a5f116 Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Sun, 28 Oct 2018 19:16:44 +0100 Subject: [PATCH] Move the merge commit creation in a helper method --- ObjectiveGit/GTRepository+Merging.m | 60 +++++++++++++++-------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/ObjectiveGit/GTRepository+Merging.m b/ObjectiveGit/GTRepository+Merging.m index b713b8091..6d2ac1c08 100644 --- a/ObjectiveGit/GTRepository+Merging.m +++ b/ObjectiveGit/GTRepository+Merging.m @@ -129,25 +129,51 @@ - (BOOL)mergeAnnotatedCommits:(NSArray *)annotatedCommits return YES; } +- (BOOL)finalizeMergeOfBranch:(GTBranch *)localBranch mergedTree:(GTTree *)mergedTree parents:(NSArray *)parents error:(NSError **)error { + + // Load the message to use + NSURL *mergeMsgFile = [[self gitDirectoryURL] URLByAppendingPathComponent:@"MERGE_MSG"]; + NSString *message = [NSString stringWithContentsOfURL:mergeMsgFile + encoding:NSUTF8StringEncoding + error:NULL]; + if (!message) { + message = [NSString stringWithFormat:@"Merge branch '%@'", localBranch.shortName]; + } + + // Create the merge commit + GTCommit *mergeCommit = [self createCommitWithTree:mergedTree message:message parents:parents updatingReferenceNamed:localBranch.reference.name error:error]; + if (!mergeCommit) { + return NO; + } + + BOOL success = [self cleanupStateWithError:error]; + if (!success) { + return NO; + } + + success = [self checkoutReference:localBranch.reference options:[GTCheckoutOptions checkoutOptionsWithStrategy:GTCheckoutStrategyForce] error:error]; + return success; +} + - (BOOL)mergeBranchIntoCurrentBranch:(GTBranch *)branch withError:(NSError **)error { // Check if merge is necessary GTBranch *localBranch = [self currentBranchWithError:error]; - if (!localBranch) { + if (localBranch == nil) { return NO; } GTCommit *localCommit = [localBranch targetCommitWithError:error]; - if (!localCommit) { + if (localCommit == nil) { return NO; } GTCommit *remoteCommit = [branch targetCommitWithError:error]; - if (!remoteCommit) { + if (remoteCommit == nil) { return NO; } GTAnnotatedCommit *remoteAnnotatedCommit = [GTAnnotatedCommit annotatedCommitFromReference:branch.reference error:error]; - if (!remoteAnnotatedCommit) { + if (remoteAnnotatedCommit == nil) { return NO; } @@ -176,7 +202,7 @@ - (BOOL)mergeBranchIntoCurrentBranch:(GTBranch *)branch withError:(NSError **)er if (analysis & GTMergeAnalysisFastForward) { NSString *message = [NSString stringWithFormat:@"merge %@: Fast-forward", branch.name]; - GTReference *reference = [localBranch.reference referenceByUpdatingTarget:remoteCommit.SHA message:message error:error]; + GTReference *reference = [localBranch.reference referenceByUpdatingTarget:remoteCommit.OID.SHA message:message error:error]; BOOL checkoutSuccess = [self checkoutReference:reference options:[GTCheckoutOptions checkoutOptionsWithStrategy:GTCheckoutStrategyForce] error:error]; return checkoutSuccess; } @@ -222,29 +248,7 @@ - (BOOL)mergeBranchIntoCurrentBranch:(GTBranch *)branch withError:(NSError **)er return NO; } - // Create merge commit - NSError *mergeMsgError = nil; - NSURL *mergeMsgFile = [[self gitDirectoryURL] URLByAppendingPathComponent:@"MERGE_MSG"]; - NSString *message = [NSString stringWithContentsOfURL:mergeMsgFile - encoding:NSUTF8StringEncoding - error:&mergeMsgError]; - if (!message) { - message = [NSString stringWithFormat:@"Merge branch '%@'", localBranch.shortName]; - } - - NSArray *parents = @[ localCommit, remoteCommit ]; - GTCommit *mergeCommit = [self createCommitWithTree:mergedTree message:message parents:parents updatingReferenceNamed:localBranch.reference.name error:error]; - if (!mergeCommit) { - return NO; - } - - success = [self cleanupStateWithError:error]; - if (!success) { - return NO; - } - - success = [self checkoutReference:localBranch.reference options:[GTCheckoutOptions checkoutOptionsWithStrategy:GTCheckoutStrategyForce] error:error]; - return success; + return [self finalizeMergeOfBranch:localBranch mergedTree:mergedTree parents:@[ localCommit, remoteCommit ] error:error]; } - (NSString * _Nullable)contentsOfDiffWithAncestor:(GTIndexEntry *)ancestor ourSide:(GTIndexEntry *)ourSide theirSide:(GTIndexEntry *)theirSide error:(NSError **)error {