Skip to content

Commit 2914aab

Browse files
committed
Move the merge commit creation in a helper method
1 parent 17aaedd commit 2914aab

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

ObjectiveGit/GTRepository+Merging.m

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,51 @@ - (BOOL)mergeAnnotatedCommits:(NSArray <GTAnnotatedCommit *> *)annotatedCommits
129129
return YES;
130130
}
131131

132+
- (BOOL)finalizeMergeOfBranch:(GTBranch *)localBranch mergedTree:(GTTree *)mergedTree parents:(NSArray <GTCommit *> *)parents error:(NSError **)error {
133+
134+
// Load the message to use
135+
NSURL *mergeMsgFile = [[self gitDirectoryURL] URLByAppendingPathComponent:@"MERGE_MSG"];
136+
NSString *message = [NSString stringWithContentsOfURL:mergeMsgFile
137+
encoding:NSUTF8StringEncoding
138+
error:NULL];
139+
if (!message) {
140+
message = [NSString stringWithFormat:@"Merge branch '%@'", localBranch.shortName];
141+
}
142+
143+
// Create the merge commit
144+
GTCommit *mergeCommit = [self createCommitWithTree:mergedTree message:message parents:parents updatingReferenceNamed:localBranch.reference.name error:error];
145+
if (!mergeCommit) {
146+
return NO;
147+
}
148+
149+
BOOL success = [self cleanupStateWithError:error];
150+
if (!success) {
151+
return NO;
152+
}
153+
154+
success = [self checkoutReference:localBranch.reference options:[GTCheckoutOptions checkoutOptionsWithStrategy:GTCheckoutStrategyForce] error:error];
155+
return success;
156+
}
157+
132158
- (BOOL)mergeBranchIntoCurrentBranch:(GTBranch *)branch withError:(NSError **)error {
133159
// Check if merge is necessary
134160
GTBranch *localBranch = [self currentBranchWithError:error];
135-
if (!localBranch) {
161+
if (localBranch == nil) {
136162
return NO;
137163
}
138164

139165
GTCommit *localCommit = [localBranch targetCommitWithError:error];
140-
if (!localCommit) {
166+
if (localCommit == nil) {
141167
return NO;
142168
}
143169

144170
GTCommit *remoteCommit = [branch targetCommitWithError:error];
145-
if (!remoteCommit) {
171+
if (remoteCommit == nil) {
146172
return NO;
147173
}
148174

149175
GTAnnotatedCommit *remoteAnnotatedCommit = [GTAnnotatedCommit annotatedCommitFromReference:branch.reference error:error];
150-
if (!remoteAnnotatedCommit) {
176+
if (remoteAnnotatedCommit == nil) {
151177
return NO;
152178
}
153179

@@ -176,7 +202,7 @@ - (BOOL)mergeBranchIntoCurrentBranch:(GTBranch *)branch withError:(NSError **)er
176202

177203
if (analysis & GTMergeAnalysisFastForward) {
178204
NSString *message = [NSString stringWithFormat:@"merge %@: Fast-forward", branch.name];
179-
GTReference *reference = [localBranch.reference referenceByUpdatingTarget:remoteCommit.SHA message:message error:error];
205+
GTReference *reference = [localBranch.reference referenceByUpdatingTarget:remoteCommit.OID.SHA message:message error:error];
180206
BOOL checkoutSuccess = [self checkoutReference:reference options:[GTCheckoutOptions checkoutOptionsWithStrategy:GTCheckoutStrategyForce] error:error];
181207
return checkoutSuccess;
182208
}
@@ -222,29 +248,7 @@ - (BOOL)mergeBranchIntoCurrentBranch:(GTBranch *)branch withError:(NSError **)er
222248
return NO;
223249
}
224250

225-
// Create merge commit
226-
NSError *mergeMsgError = nil;
227-
NSURL *mergeMsgFile = [[self gitDirectoryURL] URLByAppendingPathComponent:@"MERGE_MSG"];
228-
NSString *message = [NSString stringWithContentsOfURL:mergeMsgFile
229-
encoding:NSUTF8StringEncoding
230-
error:&mergeMsgError];
231-
if (!message) {
232-
message = [NSString stringWithFormat:@"Merge branch '%@'", localBranch.shortName];
233-
}
234-
235-
NSArray *parents = @[ localCommit, remoteCommit ];
236-
GTCommit *mergeCommit = [self createCommitWithTree:mergedTree message:message parents:parents updatingReferenceNamed:localBranch.reference.name error:error];
237-
if (!mergeCommit) {
238-
return NO;
239-
}
240-
241-
success = [self cleanupStateWithError:error];
242-
if (!success) {
243-
return NO;
244-
}
245-
246-
success = [self checkoutReference:localBranch.reference options:[GTCheckoutOptions checkoutOptionsWithStrategy:GTCheckoutStrategyForce] error:error];
247-
return success;
251+
return [self finalizeMergeOfBranch:localBranch mergedTree:mergedTree parents:@[ localCommit, remoteCommit ] error:error];
248252
}
249253

250254
- (NSString * _Nullable)contentsOfDiffWithAncestor:(GTIndexEntry *)ancestor ourSide:(GTIndexEntry *)ourSide theirSide:(GTIndexEntry *)theirSide error:(NSError **)error {

0 commit comments

Comments
 (0)