Skip to content

Commit

Permalink
Merge pull request libgit2#460 from sagmor/api_consistency
Browse files Browse the repository at this point in the history
API consistency sugestions
  • Loading branch information
joshaber committed Jun 22, 2015
2 parents 757e19c + bfe8367 commit c3188a4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 33 deletions.
5 changes: 4 additions & 1 deletion ObjectiveGit/GTBranch.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ NS_ASSUME_NONNULL_BEGIN
/// error(out) - will be filled if an error occurs
///
/// returns a GTCommit object or nil if an error occurred
- (nullable GTCommit *)targetCommitAndReturnError:(NSError **)error;
- (nullable GTCommit *)targetCommitWithError:(NSError **)error;

/// Count all commits in this branch
///
Expand Down Expand Up @@ -130,6 +130,9 @@ NS_ASSUME_NONNULL_BEGIN
/// Returns whether the calculation was successful.
- (BOOL)calculateAhead:(size_t *)ahead behind:(size_t *)behind relativeTo:(GTBranch *)branch error:(NSError **)error;

#pragma mark Deprecations
- (nullable GTCommit *)targetCommitAndReturnError:(NSError **)error __deprecated_msg("use targetCommitWithError: instead.");

@end

NS_ASSUME_NONNULL_END
7 changes: 6 additions & 1 deletion ObjectiveGit/GTBranch.m
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ - (NSString *)remoteName {
return [[NSString alloc] initWithBytes:name length:end - name encoding:NSUTF8StringEncoding];
}

- (GTCommit *)targetCommitAndReturnError:(NSError **)error {
- (GTCommit *)targetCommitWithError:(NSError **)error {
if (self.OID == nil) {
if (error != NULL) *error = GTReference.invalidReferenceError;
return nil;
Expand Down Expand Up @@ -218,4 +218,9 @@ - (BOOL)calculateAhead:(size_t *)ahead behind:(size_t *)behind relativeTo:(GTBra
return [self.repository calculateAhead:ahead behind:behind ofOID:self.OID relativeToOID:branch.OID error:error];
}

#pragma mark Deprecations
- (GTCommit *)targetCommitAndReturnError:(NSError **)error {
return [self targetCommitWithError:error];
}

@end
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
6 changes: 3 additions & 3 deletions ObjectiveGitTests/GTBranchSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@
it(@"should reload the branch from disk", ^{
static NSString * const originalSHA = @"a4bca6b67a5483169963572ee3da563da33712f7";
static NSString * const updatedSHA = @"6b0c1c8b8816416089c534e474f4c692a76ac14f";
expect([masterBranch targetCommitAndReturnError:NULL].SHA).to(equal(originalSHA));
expect([masterBranch targetCommitWithError:NULL].SHA).to(equal(originalSHA));
[masterBranch.reference referenceByUpdatingTarget:updatedSHA message:nil error:NULL];

GTBranch *reloadedBranch = [masterBranch reloadedBranchWithError:NULL];
expect(reloadedBranch).notTo(beNil());
expect([reloadedBranch targetCommitAndReturnError:NULL].SHA).to(equal(updatedSHA));
expect([masterBranch targetCommitAndReturnError:NULL].SHA).to(equal(originalSHA));
expect([reloadedBranch targetCommitWithError:NULL].SHA).to(equal(updatedSHA));
expect([masterBranch targetCommitWithError:NULL].SHA).to(equal(originalSHA));
});
});

Expand Down
26 changes: 13 additions & 13 deletions ObjectiveGitTests/GTIndexSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
it(@"should write to a specific repository and return a tree", ^{
GTRepository *repository = self.bareFixtureRepository;
NSArray *branches = [repository branches:NULL];
GTCommit *masterCommit = [branches[0] targetCommitAndReturnError:NULL];
GTCommit *packedCommit = [branches[1] targetCommitAndReturnError:NULL];
GTCommit *masterCommit = [branches[0] targetCommitWithError:NULL];
GTCommit *packedCommit = [branches[1] targetCommitWithError:NULL];

expect(masterCommit).notTo(beNil());
expect(packedCommit).notTo(beNil());
Expand Down 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

0 comments on commit c3188a4

Please sign in to comment.