Skip to content

Commit

Permalink
Move the merge commit creation in a helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
tiennou committed Mar 24, 2019
1 parent 6435f82 commit f09bddd
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions ObjectiveGit/GTRepository+Merging.m
Original file line number Diff line number Diff line change
Expand Up @@ -129,25 +129,51 @@ - (BOOL)mergeAnnotatedCommits:(NSArray <GTAnnotatedCommit *> *)annotatedCommits
return YES;
}

- (BOOL)finalizeMergeOfBranch:(GTBranch *)localBranch mergedTree:(GTTree *)mergedTree parents:(NSArray <GTCommit *> *)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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f09bddd

Please sign in to comment.