Skip to content

Commit 05bd5d4

Browse files
committed
Address comments and new test case
1 parent 26a85b2 commit 05bd5d4

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

ObjectiveGit/GTRepository+Merging.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ typedef NS_OPTIONS(NSInteger, GTMergeAnalysis) {
6363
/// error - The error if one occurred. Can be NULL.
6464
///
6565
/// Returns The file content annotated with conflict markers or null on error
66-
- (NSString* _Nullable)stringForConflictWithAncestor:(GTIndexEntry *)ancestor ourSide:(GTIndexEntry *)ourSide theirSide:(GTIndexEntry *)theirSide withError:(NSError **)error;
66+
- (NSString * _Nullable)contentsOfDiffWithAncestor:(GTIndexEntry *)ancestor ourSide:(GTIndexEntry *)ourSide theirSide:(GTIndexEntry *)theirSide error:(NSError **)error;
6767

6868
/// Analyze which merge to perform.
6969
///

ObjectiveGit/GTRepository+Merging.m

+13-12
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ - (BOOL)mergeBranchIntoCurrentBranch:(GTBranch *)branch withError:(NSError **)er
172172
return NO;
173173
}
174174

175-
- (NSString* _Nullable)stringForConflictWithAncestor:(GTIndexEntry *)ancestor ourSide:(GTIndexEntry *)ourSide theirSide:(GTIndexEntry *)theirSide withError:(NSError **)error {
175+
- (NSString * _Nullable)contentsOfDiffWithAncestor:(GTIndexEntry *)ancestor ourSide:(GTIndexEntry *)ourSide theirSide:(GTIndexEntry *)theirSide error:(NSError **)error {
176176

177177
GTObjectDatabase *database = [self objectDatabaseWithError:error];
178178
if (database == nil) {
@@ -193,9 +193,8 @@ - (NSString* _Nullable)stringForConflictWithAncestor:(GTIndexEntry *)ancestor ou
193193
if (ancestorData == nil) {
194194
return nil;
195195
}
196-
NSString *ancestorString = [[NSString alloc] initWithData: ancestorData encoding:NSUTF8StringEncoding];
197-
ancestorInput.ptr = [ancestorString cStringUsingEncoding:NSUTF8StringEncoding];
198-
ancestorInput.size = ancestorString.length;
196+
ancestorInput.ptr = ancestorData.bytes;
197+
ancestorInput.size = ancestorData.length;
199198

200199

201200
// initialize our merge file input
@@ -212,9 +211,8 @@ - (NSString* _Nullable)stringForConflictWithAncestor:(GTIndexEntry *)ancestor ou
212211
if (ourData == nil) {
213212
return nil;
214213
}
215-
NSString *ourString = [[NSString alloc] initWithData: ourData encoding:NSUTF8StringEncoding];
216-
ourInput.ptr = [ourString cStringUsingEncoding:NSUTF8StringEncoding];
217-
ourInput.size = ourString.length;
214+
ourInput.ptr = ourData.bytes;
215+
ourInput.size = ourData.length;
218216

219217

220218
// initialize their merge file input
@@ -231,15 +229,18 @@ - (NSString* _Nullable)stringForConflictWithAncestor:(GTIndexEntry *)ancestor ou
231229
if (theirData == nil) {
232230
return nil;
233231
}
234-
NSString *theirString = [[NSString alloc] initWithData: theirData encoding:NSUTF8StringEncoding];
235-
theirInput.ptr = [theirString cStringUsingEncoding:NSUTF8StringEncoding];
236-
theirInput.size = theirString.length;
232+
theirInput.ptr = theirData.bytes;
233+
theirInput.size = theirData.length;
237234

238235

239236
git_merge_file_result result;
240-
git_merge_file(&result, &ancestorInput, &ourInput, &theirInput, nil);
237+
gitError = git_merge_file(&result, &ancestorInput, &ourInput, &theirInput, nil);
238+
if (gitError != GIT_OK) {
239+
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to create merge file"];
240+
return nil;
241+
}
241242

242-
char * cString = malloc(result.len * sizeof(char*) + 1);
243+
char *cString = malloc(result.len * sizeof(char *) + 1);
243244
strncpy(cString, result.ptr, result.len);
244245
cString[result.len] = '\0';
245246

ObjectiveGitTests/GTRepositorySpec.m

+44
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,50 @@
260260
});
261261
});
262262

263+
describe(@"-contentsOfDiffWithAncestor:ourSide:theirSide:error:", ^{
264+
it(@"should produce a nice merge conflict description", ^{
265+
NSURL *mainURL = [repository.fileURL URLByAppendingPathComponent:@"main.m"];
266+
NSData *mainData = [[NSFileManager defaultManager] contentsAtPath:mainURL.path];
267+
expect(mainData).notTo(beNil());
268+
269+
NSString *mainString = [[NSString alloc] initWithData:mainData encoding:NSUTF8StringEncoding];
270+
NSData *masterData = [[mainString stringByReplacingOccurrencesOfString:@"return" withString:@"//The meaning of life is 41\n return"] dataUsingEncoding:NSUTF8StringEncoding];
271+
NSData *otherData = [[mainString stringByReplacingOccurrencesOfString:@"return" withString:@"//The meaning of life is 42\n return"] dataUsingEncoding:NSUTF8StringEncoding];
272+
273+
expect(@([[NSFileManager defaultManager] createFileAtPath:mainURL.path contents:masterData attributes:nil])).to(beTruthy());
274+
275+
GTIndex *index = [repository indexWithError:NULL];
276+
expect(@([index addFile:mainURL.lastPathComponent error:NULL])).to(beTruthy());
277+
GTReference *head = [repository headReferenceWithError:NULL];
278+
GTCommit *parent = [repository lookUpObjectByOID:head.targetOID objectType:GTObjectTypeCommit error:NULL];
279+
expect(parent).toNot(beNil());
280+
GTCommit *masterCommit = [repository createCommitWithTree:[index writeTree:NULL] message:@"MOLTAE is 41" parents:[NSArray arrayWithObject:parent] updatingReferenceNamed:head.name error:NULL];
281+
expect(masterCommit).toNot(beNil());
282+
283+
GTBranch *otherBranch = [repository lookUpBranchWithName:@"other-branch" type:GTBranchTypeLocal success:NULL error:NULL];
284+
expect(otherBranch).toNot(beNil());
285+
expect(@([repository checkoutReference:otherBranch.reference options:nil error:NULL])).to(beTruthy());
286+
287+
expect(@([[NSFileManager defaultManager] createFileAtPath:mainURL.path contents:otherData attributes:nil])).to(beTruthy());
288+
289+
index = [repository indexWithError:NULL];
290+
expect(@([index addFile:mainURL.lastPathComponent error:NULL])).to(beTruthy());
291+
parent = [repository lookUpObjectByOID:otherBranch.OID objectType:GTObjectTypeCommit error:NULL];
292+
expect(parent).toNot(beNil());
293+
GTCommit *otherCommit = [repository createCommitWithTree:[index writeTree:NULL] message:@"MOLTAE is 42!" parents:[NSArray arrayWithObject:parent] updatingReferenceNamed:otherBranch.name error:NULL];
294+
expect(otherCommit).toNot(beNil());
295+
296+
GTIndex *conflictIndex = [otherCommit merge:masterCommit error:NULL];
297+
expect(@([conflictIndex hasConflicts])).to(beTruthy());
298+
299+
[conflictIndex enumerateConflictedFilesWithError:NULL usingBlock:^(GTIndexEntry * _Nonnull ancestor, GTIndexEntry * _Nonnull ours, GTIndexEntry * _Nonnull theirs, BOOL * _Nonnull stop) {
300+
301+
NSString *conflictString = [repository contentsOfDiffWithAncestor:ancestor ourSide:ours theirSide:theirs error:NULL];
302+
expect(conflictString).to(equal(@"//\n// main.m\n// Test\n//\n// Created by Joe Ricioppo on 9/28/10.\n// Copyright 2010 __MyCompanyName__. All rights reserved.\n//\n\n#import <Cocoa/Cocoa.h>\n\nint main(int argc, char *argv[])\n{\n<<<<<<< file.txt\n //The meaning of life is 42\n=======\n //The meaning of life is 41\n>>>>>>> file.txt\n return NSApplicationMain(argc, (const char **) argv);\n}\n123456789\n123456789\n123456789\n123456789!blah!\n"));
303+
}];
304+
});
305+
});
306+
263307
describe(@"-mergeBaseBetweenFirstOID:secondOID:error:", ^{
264308
it(@"should find the merge base between two branches", ^{
265309
NSError *error = nil;

0 commit comments

Comments
 (0)