Skip to content

GTOID improvements #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 9, 2013
36 changes: 35 additions & 1 deletion Classes/GTOID.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import "git2.h"

// Represents an object ID.
@interface GTOID : NSObject
@interface GTOID : NSObject <NSCopying>

// The SHA pointed to by the OID.
@property (nonatomic, readonly, copy) NSString *SHA;
Expand All @@ -28,6 +28,40 @@
// Returns the initialized receiver.
- (id)initWithSHA:(NSString *)SHA;

// Initializes the receiver by converting the given SHA to an OID
// optionally returning a NSError instance on failure.
//
// SHA - The to convert to an OID. Cannot be nil.
// error - Will be filled with an error object in if the SHA cannot be parsed
//
// Returns the initialized receiver or nil if an error occured.
- (id)initWithSHA:(NSString *)SHA error:(NSError **)error;

// Initializes the receiver by converting the given SHA C string to an OID.
//
// string - The C string to convert. Cannot be NULL.
//
// Returns the initialized receiver.
- (id)initWithSHACString:(const char *)string;

// Initializes the receiver by converting the given SHA C string to an OID
// optionally returning a NSError instance on failure.
//
// string - The C string to convert. Cannot be NULL.
// error - Will be filled with an error object in if the SHA cannot be parsed
//
// Returns the initialized receiver.
- (id)initWithSHACString:(const char *)string error:(NSError **)error;

// Creates a new instance with the given git_oid using initWithGitOid:
+ (instancetype)oidWithGitOid:(const git_oid *)git_oid;

// Creates a new instance from the given SHA string using initWithSHAString:
+ (instancetype)oidWithSHA:(NSString *)SHA;

// Creates a new instance from the given SHA C string using initWithSHACString:
+ (instancetype)oidWithSHACString:(const char *)SHA;

// Returns the underlying git_oid struct.
- (const git_oid *)git_oid __attribute__((objc_returns_inner_pointer));

Expand Down
44 changes: 40 additions & 4 deletions Classes/GTOID.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "GTOID.h"
#import "NSError+Git.h"

@interface GTOID () {
git_oid _git_oid;
Expand Down Expand Up @@ -46,18 +47,48 @@ - (id)initWithGitOid:(const git_oid *)oid {
return self;
}

- (id)initWithSHA:(NSString *)SHA {
- (id)initWithSHA:(NSString *)SHA error:(NSError **)error {
NSParameterAssert(SHA != nil);
return [self initWithSHACString:SHA.UTF8String error:error];
}

- (id)initWithSHA:(NSString *)SHA {
return [self initWithSHA:SHA error:NULL];
}

- (id)initWithSHACString:(const char *)string error:(NSError **)error {
NSParameterAssert(string != NULL);

self = [super init];
if (self == nil) return nil;

int status = git_oid_fromstr(&_git_oid, SHA.UTF8String);
if (status != GIT_OK) return nil;

int status = git_oid_fromstr(&_git_oid, string);
if (status != GIT_OK) {
if (error != NULL) {
*error = [NSError git_errorFor:status withAdditionalDescription:[NSString stringWithFormat:@"Failed to convert string '%s' to object id", string]];
}
return nil;
}

return self;
}

- (id)initWithSHACString:(const char *)string {
return [self initWithSHACString:string error:NULL];
}

+ (instancetype)oidWithGitOid:(const git_oid *)git_oid {
return [[self alloc] initWithGitOid:git_oid];
}

+ (instancetype)oidWithSHA:(NSString *)SHA {
return [[self alloc] initWithSHA:SHA];
}

+ (instancetype)oidWithSHACString:(const char *)SHA {
return [[self alloc] initWithSHACString:SHA];
}

#pragma mark NSObject

- (NSString *)description {
Expand All @@ -77,4 +108,9 @@ - (BOOL)isEqual:(GTOID *)object {
return (BOOL)git_oid_equal(self.git_oid, object.git_oid);
}

- (id)copyWithZone:(NSZone *)zone {
// Optimization: Since this class is immutable we don't need to create an actual copy.
return self;
}

@end
28 changes: 28 additions & 0 deletions ObjectiveGitTests/GTOIDSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
NSString *secondSHA = @"82dc47f6ba3beecab33080a1136d8913098e1801";
expect(testOID).notTo.equal([[GTOID alloc] initWithSHA:secondSHA]);
});

it(@"should compare equal to an OID created with the same SHA from a C string", ^{
expect(testOID).to.equal([[GTOID alloc] initWithSHACString:"f7ecd8f4404d3a388efbff6711f1bdf28ffd16a0"]);
});
});

it(@"should keep the git_oid alive even if the object goes out of scope", ^{
Expand All @@ -46,4 +50,28 @@
expect(testOID.SHA).to.equal(testSHA);
});

it(@"should return an error when initialized with an empty SHA string", ^{
NSError *error = nil;
GTOID *oid = [[GTOID alloc] initWithSHA:@"" error:&error];

expect(oid).to.beNil();
expect(error).notTo.beNil();
});

it(@"should return an error when initialized with a string that contains non-hex characters", ^{
NSError *error = nil;
GTOID *oid = [[GTOID alloc] initWithSHA:@"zzzzz8f4404d3a388efbff6711f1bdf28ffd16a0" error:&error];

expect(oid).to.beNil();
expect(error).notTo.beNil();
});

it(@"should return an error when initialized with a string shorter than 40 characters", ^{
NSError *error = nil;
GTOID *oid = [[GTOID alloc] initWithSHA:@"f7ecd80" error:&error];

expect(oid).to.beNil();
expect(error).notTo.beNil();
});

SpecEnd