Permalink
Browse files

Merge davedelong/gtblob - fix minor conflicts

  • Loading branch information...
2 parents 86774e0 + 7bc50a5 commit 87d38c4827717327fc85031739dd46d327fdcf73 @tclem tclem committed May 23, 2011
Showing with 55 additions and 51 deletions.
  1. +7 −14 Classes/GTBlob.h
  2. +43 −33 Classes/GTBlob.m
  3. +5 −4 Tests/GTBlobTest.m
View
@@ -35,20 +35,13 @@
@property (nonatomic, readonly) git_blob *blob;
-// Convenience initializers
-//
-// These call the associated createInRepo: methods, but then lookup the newly
-// created blob in the repo and return it
-+ (GTBlob *)blobInRepository:(GTRepository *)theRepo content:(NSString *)content error:(NSError **)error;
-+ (GTBlob *)blobInRepository:(GTRepository *)theRepo data:(NSData *)data error:(NSError **)error;
-+ (GTBlob *)blobInRepository:(GTRepository *)theRepo file:(NSURL *)file error:(NSError **)error;
-
-// Create a blob in the specified repository
-//
-// returns the sha1 of the created blob or nil if an error occurred
-+ (NSString *)shaByCreatingBlobInRepository:(GTRepository *)theRepo content:(NSString *)content error:(NSError **)error;
-+ (NSString *)shaByCreatingBlobInRepository:(GTRepository *)theRepo data:(NSData *)data error:(NSError **)error;
-+ (NSString *)shaByCreatingBlobInRepository:(GTRepository *)theRepo file:(NSURL *)file error:(NSError **)error;
++ (id)blobWithString:(NSString *)string inRepository:(GTRepository *)repository error:(NSError **)error;
++ (id)blobWithData:(NSData *)data inRepository:(GTRepository *)repository error:(NSError **)error;
++ (id)blobWithFile:(NSURL *)file inRepository:(GTRepository *)repository error:(NSError **)error;
+
+- (id)initWithString:(NSString *)string inRepository:(GTRepository *)repository error:(NSError **)error;
+- (id)initWithData:(NSData *)data inRepository:(GTRepository *)repository error:(NSError **)error;
+- (id)initWithFile:(NSURL *)file inRepository:(GTRepository *)repository error:(NSError **)error;
- (NSInteger)size;
- (NSString *)content;
View
@@ -43,61 +43,70 @@ - (git_blob *)blob {
#pragma mark -
#pragma mark API
-+ (GTBlob *)blobInRepository:(GTRepository *)theRepo content:(NSString *)content error:(NSError **)error {
-
- NSString *sha = [GTBlob shaByCreatingBlobInRepository:theRepo content:content error:error];
- return sha ? (GTBlob *)[theRepo lookupObjectBySha:sha objectType:GTObjectTypeBlob error:error] : nil;
++ (id)blobWithString:(NSString *)string inRepository:(GTRepository *)repository error:(NSError **)error {
+
+ return [[[self alloc] initWithString:string inRepository:repository error:error] autorelease];
}
-+ (GTBlob *)blobInRepository:(GTRepository *)theRepo data:(NSData *)data error:(NSError **)error {
-
- NSString *sha = [GTBlob shaByCreatingBlobInRepository:theRepo data:data error:error];
- return sha ? (GTBlob *)[theRepo lookupObjectBySha:sha objectType:GTObjectTypeBlob error:error] : nil;
++ (id)blobWithData:(NSData *)data inRepository:(GTRepository *)repository error:(NSError **)error {
+
+ return [[[self alloc] initWithData:data inRepository:repository error:error] autorelease];
}
-+ (GTBlob *)blobInRepository:(GTRepository *)theRepo file:(NSURL *)file error:(NSError **)error {
-
- NSString *sha = [GTBlob shaByCreatingBlobInRepository:theRepo file:file error:error];
- return sha ? (GTBlob *)[theRepo lookupObjectBySha:sha objectType:GTObjectTypeBlob error:error] : nil;
++ (id)blobWithFile:(NSURL *)file inRepository:(GTRepository *)repository error:(NSError **)error {
+
+ return [[[self alloc] initWithFile:file inRepository:repository error:error] autorelease];
}
-+ (NSString *)shaByCreatingBlobInRepository:(GTRepository *)theRepo content:(NSString *)content error:(NSError **)error {
-
- git_oid oid;
- int gitError = git_blob_create_frombuffer(&oid, theRepo.repo, [content UTF8String], content.length);
- if(gitError != GIT_SUCCESS) {
- if(error != NULL)
- *error = [NSError gitErrorFor:gitError withDescription:@"Failed to create blob from NSString"];
- return nil;
- }
+- (id)initWithOid:(const git_oid *)oid inRepository:(GTRepository *)repository error:(NSError **)error {
+
+ git_object *obj;
+ int gitError = git_object_lookup(&obj, repository.repo, oid, GTObjectTypeBlob);
+ if (gitError != GIT_SUCCESS) {
+ if (error != NULL) {
+ *error = [NSError gitErrorFor:gitError withDescription:@"Failed to lookup blob"];
+ }
+ [self release];
+ return nil;
+ }
- return [NSString git_stringWithOid:&oid];
+ return [self initWithObj:obj inRepository:repository];
}
-+ (NSString *)shaByCreatingBlobInRepository:(GTRepository *)theRepo data:(NSData *)data error:(NSError **)error {
-
+- (id)initWithString:(NSString *)string inRepository:(GTRepository *)repository error:(NSError **)error {
+
+ NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
+ return [self initWithData:data inRepository:repository error:error];
+}
+
+- (id)initWithData:(NSData *)data inRepository:(GTRepository *)repository error:(NSError **)error {
+
git_oid oid;
- int gitError = git_blob_create_frombuffer(&oid, theRepo.repo, [data bytes], data.length);
+ int gitError = git_blob_create_frombuffer(&oid, repository.repo, [data bytes], data.length);
if(gitError != GIT_SUCCESS) {
- if(error != NULL)
+ if(error != NULL) {
*error = [NSError gitErrorFor:gitError withDescription:@"Failed to create blob from NSData"];
+ }
+ [self release];
return nil;
}
-
- return [NSString git_stringWithOid:&oid];
+
+ return [self initWithOid:&oid inRepository:repository error:error];
}
-+ (NSString *)shaByCreatingBlobInRepository:(GTRepository *)theRepo file:(NSURL *)file error:(NSError **)error {
+- (id)initWithFile:(NSURL *)file inRepository:(GTRepository *)repository error:(NSError **)error {
git_oid oid;
- int gitError = git_blob_create_fromfile(&oid, theRepo.repo, [[file path] UTF8String]);
+ int gitError = git_blob_create_fromfile(&oid, repository.repo, [[file path] UTF8String]);
if(gitError != GIT_SUCCESS) {
- if(error != NULL)
+ if(error != NULL) {
*error = [NSError gitErrorFor:gitError withDescription:@"Failed to create blob from NSURL"];
+ }
+ [self release];
return nil;
}
- return [NSString git_stringWithOid:&oid];
+ return [self initWithOid:&oid inRepository:repository error:error];
}
- (NSInteger)size {
@@ -114,7 +123,8 @@ - (NSString *)content {
}
- (NSData *)data {
- NSInteger s = [self size];
+
+ NSInteger s = [self size];
if (s == 0) return [NSData data];
return [NSData dataWithBytes:git_blob_rawcontent(self.blob) length:s];
View
@@ -76,7 +76,8 @@ - (void)testCanRewriteBlobData {
- (void)testCanWriteNewBlobData {
NSError *error = nil;
- NSString *newSha = [GTBlob shaByCreatingBlobInRepository:repo content:@"a new blob content" error:&error];
+ GTBlob *blob = [GTBlob blobWithString:@"a new blob content" inRepository:repo error:&error];
+ NSString *newSha = [blob sha];
GHAssertNotNil(newSha, [error localizedDescription]);
rm_loose(newSha);
@@ -85,7 +86,7 @@ - (void)testCanWriteNewBlobData {
- (void)testCanWriteNewBlobData2 {
NSError *error = nil;
- GTBlob *blob = [GTBlob blobInRepository:repo content:@"a new blob content" error:&error];
+ GTBlob *blob = [GTBlob blobWithString:@"a new blob content" inRepository:repo error:&error];
GHAssertNotNil(blob, [error localizedDescription]);
rm_loose(blob.sha);
@@ -97,8 +98,8 @@ - (void)testCanGetCompleteContentWithNulls {
char bytes[] = "100644 example_helper.rb\00\xD3\xD5\xED\x9D A4_\x00 40000 examples";
NSData *content = [NSData dataWithBytes:bytes length:sizeof(bytes)];
-
- NSString *newSha = [GTBlob shaByCreatingBlobInRepository:repo data:content error:&error];
+ GTBlob *blob = [GTBlob blobWithData:content inRepository:repo error:&error];
+ NSString *newSha = [blob sha];
GHAssertNotNil(newSha, [error localizedDescription]);
rm_loose(newSha);

0 comments on commit 87d38c4

Please sign in to comment.