-
Notifications
You must be signed in to change notification settings - Fork 280
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
Added two new objective c wrappers for libgit2 #435
Changes from 4 commits
3337fbd
5b9a285
8edb888
dca548e
c897111
23d9981
83322a1
9b6b255
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,16 @@ - (NSUInteger)sizeWithContext:(BOOL)includeContext hunkHeaders:(BOOL)includeHunk | |
return git_patch_size(self.git_patch, includeContext, includeHunkHeaders, includeFileHeaders); | ||
} | ||
|
||
- (NSData *)patchData { | ||
git_buf buf = GIT_BUF_INIT_CONST(0, NULL); | ||
git_patch_to_buf(&buf, self.git_patch); | ||
|
||
NSData* buffer = [[NSData alloc] initWithBytes:buf.ptr length:buf.size]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style: |
||
git_buf_free(&buf); | ||
|
||
return buffer; | ||
} | ||
|
||
#pragma mark Hunks | ||
|
||
- (BOOL)enumerateHunksUsingBlock:(void (^)(GTDiffHunk *hunk, BOOL *stop))block { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,13 @@ typedef NS_ENUM(NSInteger, GTTreeEnumerationOptions) { | |
/// returns a GTTreeEntry or nil if there is nothing with the specified name | ||
- (GTTreeEntry *)entryWithName:(NSString *)name; | ||
|
||
/// Get a entry by path | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: a/an. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was actually a copy-paste error ;-) I'll fix the other occurrences as well. |
||
/// | ||
/// path - the path of the entry relative to the repository root | ||
/// | ||
/// returns a GTTreeEntry or nil if there is nothing with the specified path | ||
- (GTTreeEntry *)entryWithPath:(NSString *)path error:(NSError **)error; | ||
|
||
/// Enumerates the contents of the tree | ||
/// | ||
/// options - One of `GTTreeEnumerationOptionPre` (for pre-order walks) or | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,16 +56,27 @@ - (NSUInteger)entryCount { | |
return (NSUInteger)git_tree_entrycount(self.git_tree); | ||
} | ||
|
||
- (GTTreeEntry *)createEntryWithEntry:(const git_tree_entry *)entry { | ||
return (entry != NULL ? [GTTreeEntry entryWithEntry:entry parentTree:self] : nil); | ||
- (GTTreeEntry *)createEntryWithCopyOfEntry:(const git_tree_entry *)entry { | ||
return (entry != NULL ? [GTTreeEntry entryWithCopyOfEntry:entry parentTree:self error:nil] : nil); | ||
} | ||
|
||
- (GTTreeEntry *)entryAtIndex:(NSUInteger)index { | ||
return [self createEntryWithEntry:git_tree_entry_byindex(self.git_tree, index)]; | ||
return [self createEntryWithCopyOfEntry:git_tree_entry_byindex(self.git_tree, index)]; | ||
} | ||
|
||
- (GTTreeEntry *)entryWithName:(NSString *)name { | ||
return [self createEntryWithEntry:git_tree_entry_byname(self.git_tree, name.UTF8String)]; | ||
return [self createEntryWithCopyOfEntry:git_tree_entry_byname(self.git_tree, name.UTF8String)]; | ||
} | ||
|
||
- (GTTreeEntry *)entryWithPath:(NSString *)path error:(NSError **)error { | ||
git_tree_entry *entry = NULL; | ||
int gitError = git_tree_entry_bypath(&entry, self.git_tree, path.UTF8String); | ||
if (error != GIT_OK) { | ||
*error = [NSError git_errorFor:gitError description:@"Failed to get tree ntry %@", path]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
return nil; | ||
} | ||
|
||
return [GTTreeEntry entryWithEntry:entry parentTree:self]; | ||
} | ||
|
||
- (git_tree *)git_tree { | ||
|
@@ -79,7 +90,7 @@ static int treewalk_cb(const char *root, const git_tree_entry *git_entry, void * | |
NSString *rootString = @(root); | ||
GTTreeEntry *parentEntry = enumerationStruct->directoryStructure[rootString]; | ||
GTTree *parentTree = parentEntry != nil ? parentEntry.tree : enumerationStruct->myself; | ||
GTTreeEntry *entry = [GTTreeEntry entryWithEntry:git_entry parentTree:parentTree]; | ||
GTTreeEntry *entry = [GTTreeEntry entryWithCopyOfEntry:git_entry parentTree:parentTree error:nil]; | ||
|
||
if (entry.type == GTObjectTypeTree) { | ||
NSString *path = [rootString stringByAppendingPathComponent:entry.name]; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ | |
#import "git2/errors.h" | ||
|
||
@interface GTTreeEntry () | ||
@property (nonatomic, assign, readonly) const git_tree_entry *git_tree_entry; | ||
@property (nonatomic, assign, readonly) git_tree_entry *git_tree_entry; | ||
@end | ||
|
||
@implementation GTTreeEntry | ||
|
@@ -64,9 +64,13 @@ - (BOOL)isEqualToEntry:(GTTreeEntry *)treeEntry { | |
return git_tree_entry_cmp(self.git_tree_entry, treeEntry.git_tree_entry) == 0 ? YES : NO; | ||
} | ||
|
||
- (void)dealloc { | ||
git_tree_entry_free(_git_tree_entry); | ||
} | ||
|
||
#pragma mark API | ||
|
||
- (instancetype)initWithEntry:(const git_tree_entry *)theEntry parentTree:(GTTree *)parent { | ||
- (instancetype)initWithEntry:(git_tree_entry *)theEntry parentTree:(GTTree *)parent { | ||
NSParameterAssert(theEntry != NULL); | ||
if((self = [super init])) { | ||
_git_tree_entry = theEntry; | ||
|
@@ -75,10 +79,23 @@ - (instancetype)initWithEntry:(const git_tree_entry *)theEntry parentTree:(GTTre | |
return self; | ||
} | ||
|
||
+ (instancetype)entryWithEntry:(const git_tree_entry *)theEntry parentTree:(GTTree *)parent { | ||
+ (instancetype)entryWithEntry:(git_tree_entry *)theEntry parentTree:(GTTree *)parent { | ||
return [[self alloc] initWithEntry:theEntry parentTree:parent]; | ||
} | ||
|
||
+ (instancetype)entryWithCopyOfEntry:(const git_tree_entry *)theEntry parentTree:(GTTree *)parent error:(NSError **)error{ | ||
NSParameterAssert(theEntry != NULL); | ||
git_tree_entry* copyOfEntry = nil; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style: |
||
int gitError = git_tree_entry_dup(©OfEntry, theEntry); | ||
if (gitError != GIT_OK) { | ||
if (error != NULL) { | ||
*error = [NSError git_errorFor:gitError description:@"Failed to duplicate tree entry."]; | ||
} | ||
return nil; | ||
} | ||
return [GTTreeEntry entryWithEntry:copyOfEntry parentTree:parent]; | ||
} | ||
|
||
- (NSString *)name { | ||
return @(git_tree_entry_name(self.git_tree_entry)); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could prevent a copying using
-initWithBytesNoCopy:length:deallocator:
, but that's 10.9+ and I don't think we're that high yet.