Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
tclem committed Mar 23, 2011
2 parents 6a51f02 + e669088 commit 35d61ef
Show file tree
Hide file tree
Showing 43 changed files with 914 additions and 852 deletions.
25 changes: 25 additions & 0 deletions Classes/Categories/GTOdbObject.h
@@ -0,0 +1,25 @@
//
// GTOdbObject.h
// ObjectiveGitFramework
//
// Created by Timothy Clem on 3/23/11.
// Copyright 2011 GitHub, Inc. All rights reserved.
//

#import <git2.h>
#import "GTObject.h"

@interface GTOdbObject : NSObject {}

@property (nonatomic, assign, readonly) git_odb_object *odbObject;

- (id)initWithObject:(git_odb_object *)object;
+ (id)objectWithObject:(git_odb_object *)object;

- (NSString *)hash;
- (GTObjectType)type;
- (NSUInteger)length;
- (NSData *)data;
- (NSString *)dataAsUTF8String;

@end
63 changes: 63 additions & 0 deletions Classes/Categories/GTOdbObject.m
@@ -0,0 +1,63 @@
//
// GTOdbObject.m
// ObjectiveGitFramework
//
// Created by Timothy Clem on 3/23/11.
// Copyright 2011 GitHub, Inc. All rights reserved.
//

#import "GTOdbObject.h"
#import "GTLib.h"
#import "NSString+Git.h"

@interface GTOdbObject()
@property (nonatomic, assign) git_odb_object *odbObject;
@end

@implementation GTOdbObject

#pragma mark API
@synthesize odbObject;

- (id)initWithObject:(git_odb_object *)object {

if((self = [super init])) {
self.odbObject = object;
}
return self;
}

+ (id)objectWithObject:(git_odb_object *)object {

return [[[self alloc] initWithObject:object] autorelease];
}

- (NSString *)hash {

return [GTLib convertOidToSha:git_odb_object_id(self.odbObject)];
}

- (GTObjectType)type {

return git_odb_object_type(self.odbObject);
}

- (NSUInteger)length {

return git_odb_object_size(self.odbObject);
}

- (NSData *)data {

return [NSData dataWithBytes:git_odb_object_data(self.odbObject) length:[self length]];
}

- (NSString *)dataAsUTF8String {

NSData *data = [self data];
if(!data) return nil;

return [NSString stringWithUTF8String:[data bytes]];
}

@end
2 changes: 2 additions & 0 deletions Classes/Categories/NSError+Git.h
Expand Up @@ -62,4 +62,6 @@
+ (NSError *)gitErrorForListAllRefs: (int)code;
+ (NSError *)gitErrorForNoBlockProvided;

+ (NSError *)gitErrorFor:(int)code withDescription:(NSString *)desc;

@end
9 changes: 9 additions & 0 deletions Classes/Categories/NSError+Git.m
Expand Up @@ -296,4 +296,13 @@ + (NSError *)gitErrorForNoBlockProvided {
forKey:NSLocalizedDescriptionKey]];
}

+ (NSError *)gitErrorFor:(int)code withDescription:(NSString *)desc {

return [NSError errorWithDomain:GTGitErrorDomain
code:-1
userInfo:
[NSDictionary dictionaryWithObject:desc
forKey:NSLocalizedDescriptionKey]];
}

@end
19 changes: 16 additions & 3 deletions Classes/GTBlob.h
Expand Up @@ -33,9 +33,22 @@

@interface GTBlob : GTObject {}

@property (nonatomic, assign, readonly) NSInteger size;
@property (nonatomic, assign) NSString *content;
// Convenience initializers
//
// These call the associated createInRepo: methods, but then lookup the newly
// created blob in the repo and return it
+ (GTBlob *)blobInRepo:(GTRepository *)theRepo content:(NSString *)content error:(NSError **)error;
+ (GTBlob *)blobInRepo:(GTRepository *)theRepo data:(NSData *)data error:(NSError **)error;
+ (GTBlob *)blobInRepo:(GTRepository *)theRepo file:(NSURL *)file error:(NSError **)error;

- (id)initInRepo:(GTRepository *)theRepo error:(NSError **)error;
// Create a blob in the specified repository
//
// returns the sha1 of the created blob or nil if an error occurred
+ (NSString *)createInRepo:(GTRepository *)theRepo content:(NSString *)content error:(NSError **)error;
+ (NSString *)createInRepo:(GTRepository *)theRepo data:(NSData *)data error:(NSError **)error;
+ (NSString *)createInRepo:(GTRepository *)theRepo file:(NSURL *)file error:(NSError **)error;

- (NSInteger)size;
- (NSString *)content;

@end
66 changes: 54 additions & 12 deletions Classes/GTBlob.m
Expand Up @@ -29,7 +29,9 @@

#import "GTBlob.h"
#import "NSString+Git.h"
#import "NSError+Git.h"
#import "GTRepository.h"
#import "GTLib.h"


@implementation GTBlob
Expand All @@ -42,17 +44,61 @@ - (git_blob *)blob {
#pragma mark -
#pragma mark API

@synthesize size;
@synthesize content;
+ (GTBlob *)blobInRepo:(GTRepository *)theRepo content:(NSString *)content error:(NSError **)error {

NSString *sha = [GTBlob createInRepo:theRepo content:content error:error];
return sha ? (GTBlob *)[theRepo lookupBySha:sha type:GTObjectTypeBlob error:error] : nil;
}

+ (GTBlob *)blobInRepo:(GTRepository *)theRepo data:(NSData *)data error:(NSError **)error {

NSString *sha = [GTBlob createInRepo:theRepo data:data error:error];
return sha ? (GTBlob *)[theRepo lookupBySha:sha type:GTObjectTypeBlob error:error] : nil;
}

- (id)initInRepo:(GTRepository *)theRepo error:(NSError **)error {
+ (GTBlob *)blobInRepo:(GTRepository *)theRepo file:(NSURL *)file error:(NSError **)error {

if((self = [super init])) {
self.repo = theRepo;
self.object = [GTObject getNewObjectInRepo:self.repo.repo type:GTObjectTypeBlob error:error];
if(self.object == nil)return nil;
NSString *sha = [GTBlob createInRepo:theRepo file:file error:error];
return sha ? (GTBlob *)[theRepo lookupBySha:sha type:GTObjectTypeBlob error:error] : nil;
}

+ (NSString *)createInRepo:(GTRepository *)theRepo content:(NSString *)content error:(NSError **)error {

git_oid oid;
int gitError = git_blob_create_frombuffer(&oid, theRepo.repo, [NSString utf8StringForString:content], content.length);
if(gitError != GIT_SUCCESS) {
if(error != NULL)
*error = [NSError gitErrorFor:gitError withDescription:@"Failed to create blob from NSString"];
return nil;
}
return self;

return [GTLib convertOidToSha:&oid];
}

+ (NSString *)createInRepo:(GTRepository *)theRepo data:(NSData *)data error:(NSError **)error {

git_oid oid;
int gitError = git_blob_create_frombuffer(&oid, theRepo.repo, [data bytes], data.length);
if(gitError != GIT_SUCCESS) {
if(error != NULL)
*error = [NSError gitErrorFor:gitError withDescription:@"Failed to create blob from NSData"];
return nil;
}

return [GTLib convertOidToSha:&oid];
}

+ (NSString *)createInRepo:(GTRepository *)theRepo file:(NSURL *)file error:(NSError **)error {

git_oid oid;
int gitError = git_blob_create_fromfile(&oid, theRepo.repo, [NSString utf8StringForString:[file path]]);
if(gitError != GIT_SUCCESS) {
if(error != NULL)
*error = [NSError gitErrorFor:gitError withDescription:@"Failed to create blob from NSURL"];
return nil;
}

return [GTLib convertOidToSha:&oid];
}

- (NSInteger)size {
Expand All @@ -67,9 +113,5 @@ - (NSString *)content {

return [NSString stringForUTF8String:git_blob_rawcontent(self.blob)];
}
- (void)setContent:(NSString *)newContent {

git_blob_set_rawcontent(self.blob, [NSString utf8StringForString:newContent], [newContent length]);
}

@end
34 changes: 25 additions & 9 deletions Classes/GTCommit.h
Expand Up @@ -36,16 +36,32 @@

@interface GTCommit : GTObject {}

@property (nonatomic, assign, readonly) git_commit *commit;
@property (nonatomic, copy) NSString *message;
@property (nonatomic, copy, readonly) NSString *messageShort;
@property (nonatomic, copy, readonly) NSString *messageDetails;
@property (nonatomic, assign, readonly) NSDate *date;
@property (nonatomic, assign) GTSignature *author;
@property (nonatomic, assign) GTSignature *committer;
@property (nonatomic, assign) GTTree *tree;
@property (nonatomic, assign, readonly) NSArray *parents;

- (id)initInRepo:(GTRepository *)theRepo error:(NSError **)error;
+ (GTCommit *)commitInRepo:(GTRepository *)theRepo
updateRefNamed:(NSString *)refName
author:(GTSignature *)authorSig
committer:(GTSignature *)committerSig
message:(NSString *)newMessage
tree:(GTTree *)theTree
parents:(NSArray *)theParents
error:(NSError **)error;

+ (NSString *)createCommitInRepo:(GTRepository *)theRepo
updateRefNamed:(NSString *)refName
author:(GTSignature *)authorSig
committer:(GTSignature *)committerSig
message:(NSString *)newMessage
tree:(GTTree *)theTree
parents:(NSArray *)theParents
error:(NSError **)error;

- (NSString *)message;
- (NSString *)messageShort;
- (NSString *)messageDetails;
- (NSDate *)date;
- (GTSignature *)author;
- (GTSignature *)committer;
- (GTTree *)tree;

@end
76 changes: 45 additions & 31 deletions Classes/GTCommit.m
Expand Up @@ -30,6 +30,7 @@
#import "GTCommit.h"
#import "GTSignature.h"
#import "GTTree.h"
#import "GTLib.h"
#import "NSString+Git.h"
#import "NSError+Git.h"
#import "GTRepository.h"
Expand All @@ -45,35 +46,60 @@ - (git_commit *)commit {
#pragma mark -
#pragma mark API

@synthesize commit;
@synthesize message;
@synthesize messageShort;
@synthesize messageDetails;
@synthesize date;
@synthesize author;
@synthesize committer;
@synthesize tree;
@synthesize parents;

- (id)initInRepo:(GTRepository *)theRepo error:(NSError **)error {

if((self = [super init])) {
self.repo = theRepo;
self.object = [GTObject getNewObjectInRepo:self.repo.repo type:GTObjectTypeCommit error:error];
if(self.object == nil) return nil;
+ (GTCommit *)commitInRepo:(GTRepository *)theRepo
updateRefNamed:(NSString *)refName
author:(GTSignature *)authorSig
committer:(GTSignature *)committerSig
message:(NSString *)newMessage
tree:(GTTree *)theTree
parents:(NSArray *)theParents
error:(NSError **)error {

NSString *sha = [GTCommit createCommitInRepo:theRepo updateRefNamed:refName author:authorSig committer:committerSig message:newMessage tree:theTree parents:theParents error:error];
return sha ? (GTCommit *)[theRepo lookupBySha:sha type:GTObjectTypeCommit error:error] : nil;
}

+ (NSString *)createCommitInRepo:(GTRepository *)theRepo
updateRefNamed:(NSString *)refName
author:(GTSignature *)authorSig
committer:(GTSignature *)committerSig
message:(NSString *)newMessage
tree:(GTTree *)theTree
parents:(NSArray *)theParents
error:(NSError **)error {

int count = theParents ? theParents.count : 0;
git_commit const *parentCommits[count];
for(int i = 0; i < count; i++){
parentCommits[i] = ((GTCommit *)[theParents objectAtIndex:i]).commit;
}

git_oid oid;
int gitError = git_commit_create_o(
&oid,
theRepo.repo,
refName ? [NSString utf8StringForString:refName] : NULL,
authorSig.signature,
committerSig.signature,
[NSString utf8StringForString:newMessage],
theTree.tree,
count,
parentCommits);
if(gitError != GIT_SUCCESS) {
if(error != NULL)
*error = [NSError gitErrorFor:gitError withDescription:@"Failed to create commit in repository"];
return nil;
}
return self;
return [GTLib convertOidToSha:&oid];
}

- (NSString *)message {

const char *s = git_commit_message(self.commit);
return [NSString stringForUTF8String:s];
}
- (void)setMessage:(NSString *)m {

git_commit_set_message(self.commit, [NSString utf8StringForString:m]);
}

- (NSString *)messageShort {

Expand Down Expand Up @@ -109,20 +135,12 @@ - (GTSignature *)author {
const git_signature *s = git_commit_author(self.commit);
return [GTSignature signatureWithSignature:(git_signature *)s];
}
- (void)setAuthor:(GTSignature *)a {

git_commit_set_author(self.commit, a.signature);
}

- (GTSignature *)committer {

const git_signature *s = git_commit_committer(self.commit);
return [GTSignature signatureWithSignature:(git_signature *)s];
}
- (void)setCommitter:(GTSignature *)c {

git_commit_set_committer(self.commit, c.signature);
}

- (GTTree *)tree {

Expand All @@ -136,10 +154,6 @@ - (GTTree *)tree {
}
return (GTTree *)[GTObject objectInRepo:self.repo withObject:(git_object *)t];
}
- (void)setTree:(GTTree *)t {

git_commit_set_tree(self.commit, t.tree);
}

- (NSArray *)parents {

Expand Down

0 comments on commit 35d61ef

Please sign in to comment.