Skip to content

Commit

Permalink
new style of calling all git_ properties their own type
Browse files Browse the repository at this point in the history
  • Loading branch information
joshaber committed Dec 29, 2011
1 parent e73f9be commit d7f5056
Show file tree
Hide file tree
Showing 34 changed files with 250 additions and 256 deletions.
2 changes: 1 addition & 1 deletion Classes/GTBlob.h
Expand Up @@ -33,7 +33,7 @@


@interface GTBlob : GTObject {} @interface GTBlob : GTObject {}


@property (nonatomic, readonly) git_blob *blob; @property (nonatomic, readonly) git_blob *git_blob;


+ (id)blobWithString:(NSString *)string inRepository:(GTRepository *)repository error:(NSError **)error; + (id)blobWithString:(NSString *)string inRepository:(GTRepository *)repository error:(NSError **)error;
+ (id)blobWithData:(NSData *)data inRepository:(GTRepository *)repository error:(NSError **)error; + (id)blobWithData:(NSData *)data inRepository:(GTRepository *)repository error:(NSError **)error;
Expand Down
16 changes: 8 additions & 8 deletions Classes/GTBlob.m
Expand Up @@ -56,7 +56,7 @@ + (id)blobWithFile:(NSURL *)file inRepository:(GTRepository *)repository error:(


- (id)initWithOid:(const git_oid *)oid inRepository:(GTRepository *)repository error:(NSError **)error { - (id)initWithOid:(const git_oid *)oid inRepository:(GTRepository *)repository error:(NSError **)error {
git_object *obj; git_object *obj;
int gitError = git_object_lookup(&obj, repository.repo, oid, (git_otype) GTObjectTypeBlob); int gitError = git_object_lookup(&obj, repository.git_repository, oid, (git_otype) GTObjectTypeBlob);
if (gitError < GIT_SUCCESS) { if (gitError < GIT_SUCCESS) {
if (error != NULL) { if (error != NULL) {
*error = [NSError git_errorFor:gitError withDescription:@"Failed to lookup blob"]; *error = [NSError git_errorFor:gitError withDescription:@"Failed to lookup blob"];
Expand All @@ -74,7 +74,7 @@ - (id)initWithString:(NSString *)string inRepository:(GTRepository *)repository


- (id)initWithData:(NSData *)data inRepository:(GTRepository *)repository error:(NSError **)error { - (id)initWithData:(NSData *)data inRepository:(GTRepository *)repository error:(NSError **)error {
git_oid oid; git_oid oid;
int gitError = git_blob_create_frombuffer(&oid, repository.repo, [data bytes], data.length); int gitError = git_blob_create_frombuffer(&oid, repository.git_repository, [data bytes], data.length);
if(gitError < GIT_SUCCESS) { if(gitError < GIT_SUCCESS) {
if(error != NULL) { if(error != NULL) {
*error = [NSError git_errorFor:gitError withDescription:@"Failed to create blob from NSData"]; *error = [NSError git_errorFor:gitError withDescription:@"Failed to create blob from NSData"];
Expand All @@ -87,7 +87,7 @@ - (id)initWithData:(NSData *)data inRepository:(GTRepository *)repository error:


- (id)initWithFile:(NSURL *)file inRepository:(GTRepository *)repository error:(NSError **)error { - (id)initWithFile:(NSURL *)file inRepository:(GTRepository *)repository error:(NSError **)error {
git_oid oid; git_oid oid;
int gitError = git_blob_create_fromfile(&oid, repository.repo, [[file path] UTF8String]); int gitError = git_blob_create_fromfile(&oid, repository.git_repository, [[file path] UTF8String]);
if(gitError < GIT_SUCCESS) { if(gitError < GIT_SUCCESS) {
if(error != NULL) { if(error != NULL) {
*error = [NSError git_errorFor:gitError withDescription:@"Failed to create blob from NSURL"]; *error = [NSError git_errorFor:gitError withDescription:@"Failed to create blob from NSURL"];
Expand All @@ -98,26 +98,26 @@ - (id)initWithFile:(NSURL *)file inRepository:(GTRepository *)repository error:(
return [self initWithOid:&oid inRepository:repository error:error]; return [self initWithOid:&oid inRepository:repository error:error];
} }


- (git_blob *)blob { - (git_blob *)git_blob {
return (git_blob *)self.obj; return (git_blob *) self.git_object;
} }


- (size_t)size { - (size_t)size {
return git_blob_rawsize(self.blob); return git_blob_rawsize(self.git_blob);
} }


- (NSString *)content { - (NSString *)content {
size_t s = [self size]; size_t s = [self size];
if(s == 0) return @""; if(s == 0) return @"";


return [NSString stringWithUTF8String:git_blob_rawcontent(self.blob)]; return [NSString stringWithUTF8String:git_blob_rawcontent(self.git_blob)];
} }


- (NSData *)data { - (NSData *)data {
size_t s = [self size]; size_t s = [self size];
if (s == 0) return [NSData data]; if (s == 0) return [NSData data];


return [NSData dataWithBytes:git_blob_rawcontent(self.blob) length:s]; return [NSData dataWithBytes:git_blob_rawcontent(self.git_blob) length:s];
} }


@end @end
3 changes: 1 addition & 2 deletions Classes/GTBranch.h
Expand Up @@ -33,8 +33,7 @@
typedef enum { typedef enum {
GTBranchTypeLocal = 1, GTBranchTypeLocal = 1,
GTBranchTypeRemote GTBranchTypeRemote
} GTBranchType } GTBranchType;
;


@interface GTBranch : NSObject <GTObject> {} @interface GTBranch : NSObject <GTObject> {}


Expand Down
2 changes: 1 addition & 1 deletion Classes/GTCommit.h
Expand Up @@ -36,7 +36,7 @@


@interface GTCommit : GTObject {} @interface GTCommit : GTObject {}


@property (nonatomic, readonly) git_commit *commit; @property (nonatomic, readonly) git_commit *git_commit;
@property (nonatomic, readonly, strong) GTSignature *author; @property (nonatomic, readonly, strong) GTSignature *author;
@property (nonatomic, readonly, strong) GTSignature *committer; @property (nonatomic, readonly, strong) GTSignature *committer;
@property (nonatomic, readonly, copy) NSArray *parents; @property (nonatomic, readonly, copy) NSArray *parents;
Expand Down
27 changes: 13 additions & 14 deletions Classes/GTCommit.m
Expand Up @@ -33,7 +33,6 @@
#import "NSError+Git.h" #import "NSError+Git.h"
#import "GTRepository.h" #import "GTRepository.h"
#import "NSString+Git.h" #import "NSString+Git.h"
#import "GTLog.h"


@interface GTCommit () @interface GTCommit ()
@property (nonatomic, strong) GTSignature *author; @property (nonatomic, strong) GTSignature *author;
Expand All @@ -48,8 +47,8 @@ - (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p> author: %@, message: %@", NSStringFromClass([self class]), self, self.author, [self message]]; return [NSString stringWithFormat:@"<%@: %p> author: %@, message: %@", NSStringFromClass([self class]), self, self.author, [self message]];
} }


- (git_commit *)commit { - (git_commit *)git_commit {
return (git_commit *)self.obj; return (git_commit *) self.git_object;
} }




Expand All @@ -68,19 +67,19 @@ + (NSString *)shaByCreatingCommitInRepository:(GTRepository *)theRepo updateRefN
NSUInteger count = theParents ? theParents.count : 0; NSUInteger count = theParents ? theParents.count : 0;
const git_commit **parentCommits = calloc(count, sizeof(git_commit *)); const git_commit **parentCommits = calloc(count, sizeof(git_commit *));
for (NSUInteger i = 0; i < count; i++){ for (NSUInteger i = 0; i < count; i++){
parentCommits[i] = ((GTCommit *)[theParents objectAtIndex:i]).commit; parentCommits[i] = ((GTCommit *)[theParents objectAtIndex:i]).git_commit;
} }


git_oid oid; git_oid oid;
int gitError = git_commit_create( int gitError = git_commit_create(
&oid, &oid,
theRepo.repo, theRepo.git_repository,
refName ? [refName UTF8String] : NULL, refName ? [refName UTF8String] : NULL,
authorSig.sig, authorSig.git_signature,
committerSig.sig, committerSig.git_signature,
NULL, NULL,
newMessage ? [newMessage UTF8String] : "", newMessage ? [newMessage UTF8String] : "",
theTree.tree, theTree.git_tree,
(int)count, (int)count,
parentCommits); parentCommits);
if(gitError < GIT_SUCCESS) { if(gitError < GIT_SUCCESS) {
Expand All @@ -94,7 +93,7 @@ + (NSString *)shaByCreatingCommitInRepository:(GTRepository *)theRepo updateRefN
} }


- (NSString *)message { - (NSString *)message {
const char *s = git_commit_message(self.commit); const char *s = git_commit_message(self.git_commit);
if(s == NULL) return nil; if(s == NULL) return nil;
return [NSString stringWithUTF8String:s]; return [NSString stringWithUTF8String:s];
} }
Expand Down Expand Up @@ -129,28 +128,28 @@ - (NSString *)messageSummary {
} }


- (NSDate *)commitDate { - (NSDate *)commitDate {
time_t t = git_commit_time(self.commit); time_t t = git_commit_time(self.git_commit);
return [NSDate dateWithTimeIntervalSince1970:t]; return [NSDate dateWithTimeIntervalSince1970:t];
} }


- (GTSignature *)author { - (GTSignature *)author {
if(author == nil) { if(author == nil) {
author = [GTSignature signatureWithSig:(git_signature *)git_commit_author(self.commit)]; author = [GTSignature signatureWithSignature:(git_signature *)git_commit_author(self.git_commit)];
} }
return author; return author;
} }


- (GTSignature *)committer { - (GTSignature *)committer {
if(committer == nil) { if(committer == nil) {
committer = [GTSignature signatureWithSig:(git_signature *)git_commit_committer(self.commit)]; committer = [GTSignature signatureWithSignature:(git_signature *)git_commit_committer(self.git_commit)];
} }
return committer; return committer;
} }


- (GTTree *)tree { - (GTTree *)tree {
git_tree *t; git_tree *t;


int gitError = git_commit_tree(&t, self.commit); int gitError = git_commit_tree(&t, self.git_commit);
if(gitError < GIT_SUCCESS) { if(gitError < GIT_SUCCESS) {
// todo: might want to return this error (and change method signature) // todo: might want to return this error (and change method signature)
GTLog("Failed to get tree with error code: %d", gitError); GTLog("Failed to get tree with error code: %d", gitError);
Expand All @@ -165,7 +164,7 @@ - (NSArray *)parents {


// todo: do we care if a call to git_commit_parent fails? // todo: do we care if a call to git_commit_parent fails?
git_commit *parent; git_commit *parent;
for(unsigned int i = 0; git_commit_parent(&parent, self.commit, i) == GIT_SUCCESS; i++) { for(unsigned int i = 0; git_commit_parent(&parent, self.git_commit, i) == GIT_SUCCESS; i++) {
[rents addObject:(GTCommit *)[GTObject objectWithObj:(git_object *)parent inRepository:self.repository]]; [rents addObject:(GTCommit *)[GTObject objectWithObj:(git_object *)parent inRepository:self.repository]];
} }


Expand Down
2 changes: 1 addition & 1 deletion Classes/GTEnumerator.m
Expand Up @@ -61,7 +61,7 @@ - (id)initWithRepository:(GTRepository *)theRepo error:(NSError **)error {
if((self = [super init])) { if((self = [super init])) {
self.repository = theRepo; self.repository = theRepo;
git_revwalk *w; git_revwalk *w;
int gitError = git_revwalk_new(&w, self.repository.repo); int gitError = git_revwalk_new(&w, self.repository.git_repository);
if(gitError < GIT_SUCCESS) { if(gitError < GIT_SUCCESS) {
if (error != NULL) if (error != NULL)
*error = [NSError git_errorFor:gitError withDescription:@"Failed to initialize rev walker."]; *error = [NSError git_errorFor:gitError withDescription:@"Failed to initialize rev walker."];
Expand Down
12 changes: 6 additions & 6 deletions Classes/GTIndex.h
Expand Up @@ -34,13 +34,13 @@


@interface GTIndex : NSObject {} @interface GTIndex : NSObject {}


@property (nonatomic, assign) git_index *index; @property (nonatomic, assign) git_index *git_index;
@property (nonatomic, copy) NSURL *path; @property (nonatomic, copy) NSURL *fileURL;
@property (nonatomic, assign) NSInteger entryCount; @property (nonatomic, readonly) NSUInteger entryCount;


// Convenience initializers // Convenience initializers
- (id)initWithPath:(NSURL *)localFileUrl error:(NSError **)error; - (id)initWithFileURL:(NSURL *)localFileUrl error:(NSError **)error;
+ (id)indexWithPath:(NSURL *)localFileUrl error:(NSError **)error; + (id)indexWithFileURL:(NSURL *)localFileUrl error:(NSError **)error;


- (id)initWithGitIndex:(git_index *)theIndex; - (id)initWithGitIndex:(git_index *)theIndex;
+ (id)indexWithGitIndex:(git_index *)theIndex; + (id)indexWithGitIndex:(git_index *)theIndex;
Expand All @@ -57,7 +57,7 @@
- (void)clear; - (void)clear;


// Get entries from the index // Get entries from the index
- (GTIndexEntry *)entryAtIndex:(NSInteger)theIndex; - (GTIndexEntry *)entryAtIndex:(NSUInteger)theIndex;
- (GTIndexEntry *)entryWithName:(NSString *)name; - (GTIndexEntry *)entryWithName:(NSString *)name;


// Add entries to the index // Add entries to the index
Expand Down
43 changes: 21 additions & 22 deletions Classes/GTIndex.m
Expand Up @@ -35,52 +35,51 @@
@implementation GTIndex @implementation GTIndex


- (NSString *)description { - (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p> path: %@, entryCount: %i", NSStringFromClass([self class]), self, self.path, self.entryCount]; return [NSString stringWithFormat:@"<%@: %p> fileURL: %@, entryCount: %i", NSStringFromClass([self class]), self, self.fileURL, self.entryCount];
} }




#pragma mark API #pragma mark API


@synthesize index; @synthesize git_index;
@synthesize path; @synthesize fileURL;
@synthesize entryCount;


+ (id)indexWithPath:(NSURL *)localFileUrl error:(NSError **)error { + (id)indexWithFileURL:(NSURL *)localFileUrl error:(NSError **)error {
return [[self alloc] initWithPath:localFileUrl error:error]; return [[self alloc] initWithFileURL:localFileUrl error:error];
} }


+ (id)indexWithGitIndex:(git_index *)theIndex { + (id)indexWithGitIndex:(git_index *)theIndex {
return [[self alloc] initWithGitIndex:theIndex]; return [[self alloc] initWithGitIndex:theIndex];
} }


- (id)initWithPath:(NSURL *)localFileUrl error:(NSError **)error { - (id)initWithFileURL:(NSURL *)localFileUrl error:(NSError **)error {
if((self = [super init])) { if((self = [super init])) {
self.path = localFileUrl; self.fileURL = localFileUrl;
git_index *i; git_index *i;
int gitError = git_index_open(&i, [[self.path path] UTF8String]); int gitError = git_index_open(&i, [[self.fileURL path] UTF8String]);
if(gitError < GIT_SUCCESS) { if(gitError < GIT_SUCCESS) {
if(error != NULL) if(error != NULL)
*error = [NSError git_errorFor:gitError withDescription:@"Failed to initialize index."]; *error = [NSError git_errorFor:gitError withDescription:@"Failed to initialize index."];
return nil; return nil;
} }
self.index = i; self.git_index = i;
} }
return self; return self;
} }


- (id)initWithGitIndex:(git_index *)theIndex; { - (id)initWithGitIndex:(git_index *)theIndex; {
if((self = [super init])) { if((self = [super init])) {
self.index = theIndex; self.git_index = theIndex;
} }
return self; return self;
} }


- (NSInteger)entryCount { - (NSUInteger)entryCount {
return git_index_entrycount(self.index); return git_index_entrycount(self.git_index);
} }


- (BOOL)refreshWithError:(NSError **)error { - (BOOL)refreshWithError:(NSError **)error {
int gitError = git_index_read(self.index); int gitError = git_index_read(self.git_index);
if(gitError < GIT_SUCCESS) { if(gitError < GIT_SUCCESS) {
if(error != NULL) if(error != NULL)
*error = [NSError git_errorFor:gitError withDescription:@"Failed to refresh index."]; *error = [NSError git_errorFor:gitError withDescription:@"Failed to refresh index."];
Expand All @@ -90,20 +89,20 @@ - (BOOL)refreshWithError:(NSError **)error {
} }


- (void)clear { - (void)clear {
git_index_clear(self.index); git_index_clear(self.git_index);
} }


- (GTIndexEntry *)entryAtIndex:(NSInteger)theIndex { - (GTIndexEntry *)entryAtIndex:(NSUInteger)theIndex {
return [GTIndexEntry indexEntryWithEntry:git_index_get(self.index, (unsigned int)theIndex)]; return [GTIndexEntry indexEntryWithEntry:git_index_get(self.git_index, (unsigned int) theIndex)];
} }


- (GTIndexEntry *)entryWithName:(NSString *)name { - (GTIndexEntry *)entryWithName:(NSString *)name {
int i = git_index_find(self.index, [name UTF8String]); int i = git_index_find(self.git_index, [name UTF8String]);
return [GTIndexEntry indexEntryWithEntry:git_index_get(self.index, (unsigned int) i)]; return [GTIndexEntry indexEntryWithEntry:git_index_get(self.git_index, (unsigned int) i)];
} }


- (BOOL)addEntry:(GTIndexEntry *)entry error:(NSError **)error { - (BOOL)addEntry:(GTIndexEntry *)entry error:(NSError **)error {
int gitError = git_index_add2(self.index, entry.entry); int gitError = git_index_add2(self.git_index, entry.git_index_entry);
if(gitError < GIT_SUCCESS) { if(gitError < GIT_SUCCESS) {
if(error != NULL) if(error != NULL)
*error = [NSError git_errorForAddEntryToIndex:gitError]; *error = [NSError git_errorForAddEntryToIndex:gitError];
Expand All @@ -113,7 +112,7 @@ - (BOOL)addEntry:(GTIndexEntry *)entry error:(NSError **)error {
} }


- (BOOL)addFile:(NSString *)file error:(NSError **)error { - (BOOL)addFile:(NSString *)file error:(NSError **)error {
int gitError = git_index_add(self.index, [file UTF8String], 0); int gitError = git_index_add(self.git_index, [file UTF8String], 0);
if(gitError < GIT_SUCCESS) { if(gitError < GIT_SUCCESS) {
if(error != NULL) if(error != NULL)
*error = [NSError git_errorForAddEntryToIndex:gitError]; *error = [NSError git_errorForAddEntryToIndex:gitError];
Expand All @@ -123,7 +122,7 @@ - (BOOL)addFile:(NSString *)file error:(NSError **)error {
} }


- (BOOL)writeWithError:(NSError **)error { - (BOOL)writeWithError:(NSError **)error {
int gitError = git_index_write(self.index); int gitError = git_index_write(self.git_index);
if(gitError < GIT_SUCCESS) { if(gitError < GIT_SUCCESS) {
if(error != NULL) if(error != NULL)
*error = [NSError git_errorFor:gitError withDescription:@"Failed to write index."]; *error = [NSError git_errorFor:gitError withDescription:@"Failed to write index."];
Expand Down
2 changes: 1 addition & 1 deletion Classes/GTIndexEntry.h
Expand Up @@ -32,7 +32,7 @@


@interface GTIndexEntry : NSObject {} @interface GTIndexEntry : NSObject {}


@property (nonatomic, assign) git_index_entry *entry; @property (nonatomic, assign) git_index_entry *git_index_entry;
@property (nonatomic, copy) NSString *path; @property (nonatomic, copy) NSString *path;
@property (nonatomic, strong) NSDate *modificationDate; @property (nonatomic, strong) NSDate *modificationDate;
@property (nonatomic, strong) NSDate *creationDate; @property (nonatomic, strong) NSDate *creationDate;
Expand Down

0 comments on commit d7f5056

Please sign in to comment.