From 594978305327ba8769b82277c0331298aa0fe92c Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Mon, 8 Jul 2013 17:02:48 +0200 Subject: [PATCH 01/11] Implement for GTOID. --- Classes/GTOID.h | 2 +- Classes/GTOID.m | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Classes/GTOID.h b/Classes/GTOID.h index 6d50e8204..f21c6488c 100644 --- a/Classes/GTOID.h +++ b/Classes/GTOID.h @@ -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; diff --git a/Classes/GTOID.m b/Classes/GTOID.m index c35e34ada..58487e906 100644 --- a/Classes/GTOID.m +++ b/Classes/GTOID.m @@ -77,4 +77,10 @@ - (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 From fe9d931436a698a981e36ea44008f30672e09a42 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Mon, 8 Jul 2013 17:07:01 +0200 Subject: [PATCH 02/11] Add init method to initialize GTOID with a SHA C String and use this in the initWithSHA: method. --- Classes/GTOID.h | 7 +++++++ Classes/GTOID.m | 12 +++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Classes/GTOID.h b/Classes/GTOID.h index f21c6488c..c6607822f 100644 --- a/Classes/GTOID.h +++ b/Classes/GTOID.h @@ -28,6 +28,13 @@ // Returns the initialized receiver. - (id)initWithSHA:(NSString *)SHA; +// 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. +- initWithSHACString: (const char *)string; + // Returns the underlying git_oid struct. - (const git_oid *)git_oid __attribute__((objc_returns_inner_pointer)); diff --git a/Classes/GTOID.m b/Classes/GTOID.m index 58487e906..f7126a9ae 100644 --- a/Classes/GTOID.m +++ b/Classes/GTOID.m @@ -48,13 +48,19 @@ - (id)initWithGitOid:(const git_oid *)oid { - (id)initWithSHA:(NSString *)SHA { NSParameterAssert(SHA != nil); + return [self initWithSHACString: SHA.UTF8String]; +} +- initWithSHACString: (const char *)string; +{ + NSParameterAssert( string ); + self = [super init]; if (self == nil) return nil; - - int status = git_oid_fromstr(&_git_oid, SHA.UTF8String); + + int status = git_oid_fromstr( &_git_oid, string ); if (status != GIT_OK) return nil; - + return self; } From 2af10de1c6df644c28def570284eeb6e3476de75 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Mon, 8 Jul 2013 17:09:55 +0200 Subject: [PATCH 03/11] Add convenience methods to create GTOID objects. --- Classes/GTOID.h | 4 ++++ Classes/GTOID.m | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/Classes/GTOID.h b/Classes/GTOID.h index c6607822f..055a86ed4 100644 --- a/Classes/GTOID.h +++ b/Classes/GTOID.h @@ -35,6 +35,10 @@ // Returns the initialized receiver. - initWithSHACString: (const char *)string; ++ (instancetype)oidWithGitOid: (const git_oid *)git_oid; ++ (instancetype)oidWithSHA: (NSString *)SHA; ++ (instancetype)oidWithSHACString: (const char *)SHA; + // Returns the underlying git_oid struct. - (const git_oid *)git_oid __attribute__((objc_returns_inner_pointer)); diff --git a/Classes/GTOID.m b/Classes/GTOID.m index f7126a9ae..beff12a99 100644 --- a/Classes/GTOID.m +++ b/Classes/GTOID.m @@ -64,6 +64,21 @@ - (id)initWithSHA:(NSString *)SHA { return self; } ++ (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 { From 4acaa6ee5238cc717a94a4018c5ba3e503445395 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Mon, 8 Jul 2013 20:06:55 +0200 Subject: [PATCH 04/11] Fix style issues. --- Classes/GTOID.h | 6 +++--- Classes/GTOID.m | 21 ++++++++------------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/Classes/GTOID.h b/Classes/GTOID.h index 055a86ed4..0c2a762bd 100644 --- a/Classes/GTOID.h +++ b/Classes/GTOID.h @@ -9,7 +9,7 @@ #import "git2.h" // Represents an object ID. -@interface GTOID : NSObject < NSCopying > +@interface GTOID : NSObject // The SHA pointed to by the OID. @property (nonatomic, readonly, copy) NSString *SHA; @@ -28,12 +28,12 @@ // Returns the initialized receiver. - (id)initWithSHA:(NSString *)SHA; -// Initializes the receiver by converting the given SHA C STring to an OID. +// 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. -- initWithSHACString: (const char *)string; +- (id)initWithSHACString:(const char *)string; + (instancetype)oidWithGitOid: (const git_oid *)git_oid; + (instancetype)oidWithSHA: (NSString *)SHA; diff --git a/Classes/GTOID.m b/Classes/GTOID.m index beff12a99..185b302af 100644 --- a/Classes/GTOID.m +++ b/Classes/GTOID.m @@ -48,34 +48,30 @@ - (id)initWithGitOid:(const git_oid *)oid { - (id)initWithSHA:(NSString *)SHA { NSParameterAssert(SHA != nil); - return [self initWithSHACString: SHA.UTF8String]; + return [self initWithSHACString:SHA.UTF8String]; } -- initWithSHACString: (const char *)string; -{ - NSParameterAssert( string ); +- (id)initWithSHACString:(const char *)string { + NSParameterAssert(string != NULL); self = [super init]; if (self == nil) return nil; int status = git_oid_fromstr( &_git_oid, string ); if (status != GIT_OK) return nil; - + return self; } -+ (instancetype)oidWithGitOid: (const git_oid *)git_oid; -{ ++ (instancetype)oidWithGitOid: (const git_oid *)git_oid { return [[self alloc] initWithGitOid: git_oid]; } -+ (instancetype)oidWithSHA: (NSString *)SHA; -{ ++ (instancetype)oidWithSHA: (NSString *)SHA { return [[self alloc] initWithSHA: SHA]; } -+ (instancetype)oidWithSHACString: (const char *)SHA; -{ ++ (instancetype)oidWithSHACString: (const char *)SHA { return [[self alloc] initWithSHACString: SHA]; } @@ -98,8 +94,7 @@ - (BOOL)isEqual:(GTOID *)object { return (BOOL)git_oid_equal(self.git_oid, object.git_oid); } -- (id)copyWithZone:(NSZone *)zone; -{ +- (id)copyWithZone:(NSZone *)zone { // Optimization: Since this class is immutable we don't need to create an actual copy. return self; } From 98801177c68815d96d16fdc59384ee168ffcf4bb Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Mon, 8 Jul 2013 20:12:26 +0200 Subject: [PATCH 05/11] Add unit test for -initWithSHACString: --- ObjectiveGitTests/GTOIDSpec.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ObjectiveGitTests/GTOIDSpec.m b/ObjectiveGitTests/GTOIDSpec.m index 2479d6f4d..79f560772 100644 --- a/ObjectiveGitTests/GTOIDSpec.m +++ b/ObjectiveGitTests/GTOIDSpec.m @@ -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", ^{ From 2716b159ab52879d31d13794e868dc81f4cf6c93 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Mon, 8 Jul 2013 21:40:50 +0200 Subject: [PATCH 06/11] Fix more style issues. --- Classes/GTOID.m | 14 +++++++------- ObjectiveGitTests/GTOIDSpec.m | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Classes/GTOID.m b/Classes/GTOID.m index 185b302af..8aff67ffc 100644 --- a/Classes/GTOID.m +++ b/Classes/GTOID.m @@ -57,22 +57,22 @@ - (id)initWithSHACString:(const char *)string { self = [super init]; if (self == nil) return nil; - int status = git_oid_fromstr( &_git_oid, string ); + int status = git_oid_fromstr(&_git_oid, string); if (status != GIT_OK) return nil; return self; } -+ (instancetype)oidWithGitOid: (const git_oid *)git_oid { - return [[self alloc] initWithGitOid: git_oid]; ++ (instancetype)oidWithGitOid:(const git_oid *)git_oid { + return [[self alloc] initWithGitOid:git_oid]; } -+ (instancetype)oidWithSHA: (NSString *)SHA { - return [[self alloc] initWithSHA: SHA]; ++ (instancetype)oidWithSHA:(NSString *)SHA { + return [[self alloc] initWithSHA:SHA]; } -+ (instancetype)oidWithSHACString: (const char *)SHA { - return [[self alloc] initWithSHACString: SHA]; ++ (instancetype)oidWithSHACString:(const char *)SHA { + return [[self alloc] initWithSHACString:SHA]; } #pragma mark NSObject diff --git a/ObjectiveGitTests/GTOIDSpec.m b/ObjectiveGitTests/GTOIDSpec.m index 79f560772..8e6569c5e 100644 --- a/ObjectiveGitTests/GTOIDSpec.m +++ b/ObjectiveGitTests/GTOIDSpec.m @@ -34,7 +34,7 @@ }); it(@"should compare equal to an OID created with the same SHA from a C string", ^{ - expect(testOID).to.equal([[GTOID alloc] initWithSHACString: "f7ecd8f4404d3a388efbff6711f1bdf28ffd16a0"]); + expect(testOID).to.equal([[GTOID alloc] initWithSHACString:"f7ecd8f4404d3a388efbff6711f1bdf28ffd16a0"]); }); }); From 87fca6e0dcf9d6b64c219bceddcb6e949750465f Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Mon, 8 Jul 2013 21:50:02 +0200 Subject: [PATCH 07/11] Add documentation and fix style issues for the convenience initializers. --- Classes/GTOID.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Classes/GTOID.h b/Classes/GTOID.h index 0c2a762bd..200800c72 100644 --- a/Classes/GTOID.h +++ b/Classes/GTOID.h @@ -35,9 +35,14 @@ // Returns the initialized receiver. - (id)initWithSHACString:(const char *)string; -+ (instancetype)oidWithGitOid: (const git_oid *)git_oid; -+ (instancetype)oidWithSHA: (NSString *)SHA; -+ (instancetype)oidWithSHACString: (const char *)SHA; +// 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)); From d760b2eaa13e6e3fb9e9a6007ce8fe28168fc9ad Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Tue, 9 Jul 2013 21:04:07 +0200 Subject: [PATCH 08/11] Add initializers that can return a NSError instance on error. --- Classes/GTOID.h | 18 ++++++++++++++++++ Classes/GTOID.m | 22 ++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Classes/GTOID.h b/Classes/GTOID.h index 200800c72..cd13bc0a2 100644 --- a/Classes/GTOID.h +++ b/Classes/GTOID.h @@ -28,6 +28,15 @@ // 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. @@ -35,6 +44,15 @@ // 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; diff --git a/Classes/GTOID.m b/Classes/GTOID.m index 8aff67ffc..183cf8a86 100644 --- a/Classes/GTOID.m +++ b/Classes/GTOID.m @@ -7,6 +7,7 @@ // #import "GTOID.h" +#import "NSError+Git.h" @interface GTOID () { git_oid _git_oid; @@ -46,23 +47,36 @@ - (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]; + return [self initWithSHACString:SHA.UTF8String error:error]; } -- (id)initWithSHACString:(const char *)string { +- (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, string); - if (status != GIT_OK) return nil; + if (status != GIT_OK) { + if (error != NULL) { + *error = [NSError git_errorForMkStr:status]; + } + 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]; } From 695a4729e95b8eba062fa521d98947e57c3baf7c Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Tue, 9 Jul 2013 23:29:50 +0200 Subject: [PATCH 09/11] Fix formatting of documentation comments. --- Classes/GTOID.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Classes/GTOID.h b/Classes/GTOID.h index cd13bc0a2..334972875 100644 --- a/Classes/GTOID.h +++ b/Classes/GTOID.h @@ -31,7 +31,7 @@ // 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. +// 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. @@ -48,7 +48,7 @@ // 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 +// 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; From 39b5c2174bba252f30da24c43cdb17e256bea1bc Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Tue, 9 Jul 2013 23:31:47 +0200 Subject: [PATCH 10/11] Better error message for invalid SHA string. --- Classes/GTOID.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/GTOID.m b/Classes/GTOID.m index 183cf8a86..e44061c58 100644 --- a/Classes/GTOID.m +++ b/Classes/GTOID.m @@ -65,7 +65,7 @@ - (id)initWithSHACString:(const char *)string error:(NSError **)error { int status = git_oid_fromstr(&_git_oid, string); if (status != GIT_OK) { if (error != NULL) { - *error = [NSError git_errorForMkStr:status]; + *error = [NSError git_errorFor:status withAdditionalDescription:[NSString stringWithFormat:@"Failed to convert string '%s' to object id", string]]; } return nil; } From 2309595004f9d71585bd744424860d8118a97d5a Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Tue, 9 Jul 2013 23:45:47 +0200 Subject: [PATCH 11/11] Test that an error is returned when GTOID is initialized with an invalid SHA string. --- ObjectiveGitTests/GTOIDSpec.m | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ObjectiveGitTests/GTOIDSpec.m b/ObjectiveGitTests/GTOIDSpec.m index 8e6569c5e..d24b64c5b 100644 --- a/ObjectiveGitTests/GTOIDSpec.m +++ b/ObjectiveGitTests/GTOIDSpec.m @@ -50,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