Skip to content

Commit 000b9d5

Browse files
committed
Write merge conflicted files the right way™️
1 parent 74f9dc5 commit 000b9d5

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

ObjectiveGit/GTRepository+Pull.m

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,23 @@ - (BOOL)pullBranch:(GTBranch *)branch fromRemote:(GTRemote *)remote withOptions:
112112
NSMutableArray <NSString *>*files = [NSMutableArray array];
113113
[index enumerateConflictedFilesWithError:error usingBlock:^(GTIndexEntry * _Nonnull ancestor, GTIndexEntry * _Nonnull ours, GTIndexEntry * _Nonnull theirs, BOOL * _Nonnull stop) {
114114
[files addObject:ours.path];
115-
116-
GTMergeFileResult *result = [index mergeIndexEntries:ancestor ours:ours theirs:theirs error:error];
117-
NSURL *oursFileURL = [self.fileURL URLByAppendingPathComponent:result.path];
118-
[result.contents writeToURL:oursFileURL atomically:YES encoding:NSUTF8StringEncoding error:error];
119115
}];
116+
120117
if (error != NULL) {
121118
NSDictionary *userInfo = @{GTPullMergeConflictedFiles: files};
122119
*error = [NSError git_errorFor:GIT_ECONFLICT description:@"Merge conflict, Pull aborted." userInfo:userInfo failureReason:nil];
123120
}
121+
122+
// Write conflicts
123+
git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT;
124+
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
125+
checkout_opts.checkout_strategy = GIT_CHECKOUT_ALLOW_CONFLICTS;
126+
127+
git_annotated_commit *annotatedCommit;
128+
[self annotatedCommit:&annotatedCommit fromCommit:remoteCommit error:error];
129+
130+
git_merge(repo.git_repository, (const git_annotated_commit **)&annotatedCommit, 1, &merge_opts, &checkout_opts);
131+
124132
return NO;
125133
}
126134

@@ -146,6 +154,16 @@ - (BOOL)pullBranch:(GTBranch *)branch fromRemote:(GTRemote *)remote withOptions:
146154
return NO;
147155
}
148156

157+
- (BOOL)annotatedCommit:(git_annotated_commit **)annotatedCommit fromCommit:(GTCommit *)fromCommit error:(NSError **)error {
158+
int gitError = git_annotated_commit_lookup(annotatedCommit, self.git_repository, fromCommit.OID.git_oid);
159+
if (gitError != GIT_OK) {
160+
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to lookup annotated commit for %@", fromCommit];
161+
return NO;
162+
}
163+
164+
return YES;
165+
}
166+
149167
- (BOOL)analyzeMerge:(GTMergeAnalysis *)analysis fromBranch:(GTBranch *)fromBranch error:(NSError **)error {
150168
NSParameterAssert(analysis != NULL);
151169
NSParameterAssert(fromBranch != nil);
@@ -156,18 +174,13 @@ - (BOOL)analyzeMerge:(GTMergeAnalysis *)analysis fromBranch:(GTBranch *)fromBran
156174
}
157175

158176
git_annotated_commit *annotatedCommit;
159-
160-
int gitError = git_annotated_commit_lookup(&annotatedCommit, self.git_repository, fromCommit.OID.git_oid);
161-
if (gitError != GIT_OK) {
162-
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to lookup annotated comit for %@", fromCommit];
163-
return NO;
164-
}
177+
[self annotatedCommit:&annotatedCommit fromCommit:fromCommit error:error];
165178

166179
// Allow fast-forward or normal merge
167180
git_merge_preference_t preference = GIT_MERGE_PREFERENCE_NONE;
168181

169182
// Merge analysis
170-
gitError = git_merge_analysis((git_merge_analysis_t *)analysis, &preference, self.git_repository, (const git_annotated_commit **) &annotatedCommit, 1);
183+
int gitError = git_merge_analysis((git_merge_analysis_t *)analysis, &preference, self.git_repository, (const git_annotated_commit **) &annotatedCommit, 1);
171184
if (gitError != GIT_OK) {
172185
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to analyze merge"];
173186
return NO;

0 commit comments

Comments
 (0)