Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use paths instead of names on GTIndex
  • Loading branch information
sagmor committed Apr 13, 2015
commit ce160d1ed1892b2b71c5876642a664bcd381521e
17 changes: 11 additions & 6 deletions ObjectiveGit/GTIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ NS_ASSUME_NONNULL_BEGIN
/// Returns a new GTIndexEntry, or nil if an error occurred.
- (nullable GTIndexEntry *)entryAtIndex:(NSUInteger)index;

/// Get the entry with the given name.
- (GTIndexEntry *)entryWithName:(NSString *)name;
/// Get the entry with the given path.
- (GTIndexEntry *)entryWithPath:(NSString *)path;

/// Get the entry with the given name.
///
/// name - The name of the entry to get. Cannot be nil.
/// path - The path of the entry to get. Cannot be nil.
/// error - The error if one occurred.
///
/// Returns a new GTIndexEntry, or nil if an error occurred.
- (nullable GTIndexEntry *)entryWithName:(NSString *)name error:(NSError **)error;
- (nullable GTIndexEntry *)entryWithPath:(NSString *)path error:(NSError **)error;

/// Add an entry to the index.
///
Expand All @@ -141,9 +141,9 @@ NS_ASSUME_NONNULL_BEGIN
/// Will fail if the receiver's repository is nil.
///
/// data - The content of the entry to add. Cannot be nil.
/// name - The name of the entry to add. Cannot be nil.
/// path - The path of the entry to add. Cannot be nil.
/// error - The error if one occurred.
- (BOOL)addData:(NSData *)data withName:(NSString *)name error:(NSError **)error;
- (BOOL)addData:(NSData *)data withPath:(NSString *)path error:(NSError **)error;

/// Reads the contents of the given tree into the index.
///
Expand Down Expand Up @@ -220,6 +220,11 @@ NS_ASSUME_NONNULL_BEGIN
/// Returns `YES` in the event that everything has gone smoothly. Otherwise, `NO`.
- (BOOL)updatePathspecs:(nullable NSArray *)pathspecs error:(NSError **)error passingTest:(nullable BOOL (^)(NSString *matchedPathspec, NSString *path, BOOL *stop))block;

#pragma mark Deprecations
- (nullable GTIndexEntry *)entryWithName:(NSString *)name __deprecated_msg("use entryWithPath: instead.");
- (nullable GTIndexEntry *)entryWithName:(NSString *)name error:(NSError **)error __deprecated_msg("use entryWithPath:error: instead.");


@end

NS_ASSUME_NONNULL_END
27 changes: 18 additions & 9 deletions ObjectiveGit/GTIndex.m
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ - (GTIndexEntry *)entryAtIndex:(NSUInteger)index {
return [[GTIndexEntry alloc] initWithGitIndexEntry:entry index:self error:NULL];
}

- (GTIndexEntry *)entryWithName:(NSString *)name {
return [self entryWithName:name error:NULL];
- (GTIndexEntry *)entryWithPath:(NSString *)path {
return [self entryWithPath:path error:NULL];
}

- (GTIndexEntry *)entryWithName:(NSString *)name error:(NSError **)error {
- (GTIndexEntry *)entryWithPath:(NSString *)path error:(NSError **)error {
size_t pos = 0;
int gitError = git_index_find(&pos, self.git_index, name.UTF8String);
int gitError = git_index_find(&pos, self.git_index, path.UTF8String);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"%@ not found in index", name];
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"%@ not found in index", path];
return NULL;
}
return [self entryAtIndex:pos];
Expand Down Expand Up @@ -182,19 +182,19 @@ - (BOOL)addFile:(NSString *)file error:(NSError **)error {
return YES;
}

- (BOOL)addData:(NSData *)data withName:(NSString *)name error:(NSError **)error {
- (BOOL)addData:(NSData *)data withPath:(NSString *)path error:(NSError **)error {
NSParameterAssert(data != nil);
NSParameterAssert(name != nil);
NSParameterAssert(path != nil);

git_index_entry entry;
memset(&entry, 0x0, sizeof(git_index_entry));
entry.path = [name cStringUsingEncoding:NSUTF8StringEncoding];
entry.path = [path cStringUsingEncoding:NSUTF8StringEncoding];
entry.mode = GIT_FILEMODE_BLOB;

int status = git_index_add_frombuffer(self.git_index, &entry, [data bytes], [data length]);

if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to add data with name %@ into index.", name];
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to add data with name %@ into index.", path];
return NO;
}

Expand Down Expand Up @@ -380,4 +380,13 @@ - (NSString *)composedUnicodeStringWithString:(NSString *)string {
return (shouldPrecompose ? [string precomposedStringWithCanonicalMapping] : [string decomposedStringWithCanonicalMapping]);
}

#pragma mark Deprecations

- (GTIndexEntry *)entryWithName:(NSString *)name {
return [self entryWithPath:name];
}

- (GTIndexEntry *)entryWithName:(NSString *)name error:(NSError **)error {
return [self entryWithPath:name error:error];
}
@end
22 changes: 11 additions & 11 deletions ObjectiveGitTests/GTIndexSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
if (treeEntry.type == GTObjectTypeBlob) {
NSString *path = [root stringByAppendingString:treeEntry.name];

GTIndexEntry *indexEntry = [memoryIndex entryWithName:path];
GTIndexEntry *indexEntry = [memoryIndex entryWithPath:path];
expect(indexEntry).notTo(beNil());
}

Expand Down Expand Up @@ -229,48 +229,48 @@

it(@"it preserves decomposed Unicode in index paths with precomposeunicode disabled", ^{
NSString *decomposedFilename = [filename decomposedStringWithCanonicalMapping];
GTIndexEntry *entry = [index entryWithName:decomposedFilename error:NULL];
GTIndexEntry *entry = [index entryWithPath:decomposedFilename error:NULL];
expect(@(fileStatusEqualsExpected(entry.path, GTStatusDeltaStatusUnmodified, GTStatusDeltaStatusUnmodified))).to(beTruthy());

expect(@([[NSFileManager defaultManager] moveItemAtURL:fileURL toURL:renamedFileURL error:NULL])).to(beTruthy());

entry = [index entryWithName:decomposedFilename error:NULL];
entry = [index entryWithPath:decomposedFilename error:NULL];
expect(@(fileStatusEqualsExpected(entry.path, GTStatusDeltaStatusUnmodified, GTStatusDeltaStatusDeleted))).to(beTruthy());

[index removeFile:filename error:NULL];
[index addFile:renamedFilename error:NULL];
[index write:NULL];

entry = [index entryWithName:[renamedFilename decomposedStringWithCanonicalMapping] error:NULL];
entry = [index entryWithPath:[renamedFilename decomposedStringWithCanonicalMapping] error:NULL];
expect(@(fileStatusEqualsExpected(entry.path, GTStatusDeltaStatusRenamed, GTStatusDeltaStatusUnmodified))).to(beTruthy());
});

it(@"it preserves precomposed Unicode in index paths with precomposeunicode enabled", ^{
GTIndexEntry *fileEntry = [index entryWithName:[filename decomposedStringWithCanonicalMapping] error:NULL];
GTIndexEntry *fileEntry = [index entryWithPath:[filename decomposedStringWithCanonicalMapping] error:NULL];
expect(fileEntry).notTo(beNil());
expect(@(fileStatusEqualsExpected(fileEntry.path, GTStatusDeltaStatusUnmodified, GTStatusDeltaStatusUnmodified))).to(beTruthy());

[configuration setBool:true forKey:@"core.precomposeunicode"];
expect(@([configuration boolForKey:@"core.precomposeunicode"])).to(beTruthy());

GTIndexEntry *decomposedFileEntry = [index entryWithName:[filename decomposedStringWithCanonicalMapping] error:NULL];
GTIndexEntry *decomposedFileEntry = [index entryWithPath:[filename decomposedStringWithCanonicalMapping] error:NULL];
expect(decomposedFileEntry).notTo(beNil());
expect(@(fileStatusEqualsExpected(decomposedFileEntry.path, GTStatusDeltaStatusUnmodified, GTStatusDeltaStatusDeleted))).to(beTruthy());

expect(@([[NSFileManager defaultManager] moveItemAtURL:fileURL toURL:renamedFileURL error:NULL])).to(beTruthy());

GTIndexEntry *precomposedFileEntry = [index entryWithName:filename error:NULL];
GTIndexEntry *precomposedFileEntry = [index entryWithPath:filename error:NULL];
expect(precomposedFileEntry).to(beNil());

decomposedFileEntry = [index entryWithName:[filename decomposedStringWithCanonicalMapping] error:NULL];
decomposedFileEntry = [index entryWithPath:[filename decomposedStringWithCanonicalMapping] error:NULL];
expect(decomposedFileEntry).notTo(beNil());
expect(@(fileStatusEqualsExpected(decomposedFileEntry.path, GTStatusDeltaStatusUnmodified, GTStatusDeltaStatusDeleted))).to(beTruthy());

[index removeFile:filename error:NULL];
[index addFile:renamedFilename error:NULL];
[index write:NULL];

GTIndexEntry *precomposedRenamedFileEntry = [index entryWithName:renamedFilename error:NULL];
GTIndexEntry *precomposedRenamedFileEntry = [index entryWithPath:renamedFilename error:NULL];
expect(precomposedRenamedFileEntry).notTo(beNil());
expect(@(fileStatusEqualsExpected(precomposedFileEntry.path, GTStatusDeltaStatusRenamed, GTStatusDeltaStatusUntracked))).to(beTruthy());
});
Expand All @@ -292,10 +292,10 @@

it(@"should store data at given path", ^{
NSData *data = [NSData dataWithBytes:"foo" length:4];
[index addData:data withName:@"bar/foo" error:&error];
[index addData:data withPath:@"bar/foo" error:&error];
expect(error).to(beNil());

GTIndexEntry *entry = [index entryWithName:@"bar/foo" error:&error];
GTIndexEntry *entry = [index entryWithPath:@"bar/foo" error:&error];
expect(entry).notTo(beNil());
expect(error).to(beNil());
});
Expand Down