Skip to content
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

Merged
merged 8 commits into from
Feb 17, 2015

Conversation

0x4a616e
Copy link
Contributor

  • A patch can directly be copied into a buffer using git_patch_to_buf method
  • A tree entry can be looked up by path using git_tree_entry_bypath method. The found object is freed when the GTTreeEntry is deallocated.

@@ -50,6 +50,9 @@
/// Returns the raw size of the delta, in bytes.
- (NSUInteger)sizeWithContext:(BOOL)includeContext hunkHeaders:(BOOL)includeHunkHeaders fileHeaders:(BOOL)includeFileHeaders;

/// Returns the raw patch data as buffer.
- (NSData*)toBuffer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patchData feels like a more "conventional" name.

@joshaber
Copy link
Member

The different ownership semantics make me wonder if we should just git_tree_entry_dup all entries so that we're consistent.

@0x4a616e
Copy link
Contributor Author

That should solve this, I'll change it

… respective git_tree_entry and is responsible for freeing it.

Therefore, the type of the field has been changed to git_tree_entry* (without the const). To easily create a GTTreeEntry from a managed const git_tree_entry*, a factory method exists to duplicate git_tree_entry and create a new GTTreeEntry
@0x4a616e
Copy link
Contributor Author

Pushed a new version incorporating your feedback

@0x4a616e 0x4a616e closed this Jan 20, 2015
@0x4a616e 0x4a616e reopened this Jan 20, 2015
@@ -50,6 +50,9 @@
/// Returns the raw size of the delta, in bytes.
- (NSUInteger)sizeWithContext:(BOOL)includeContext hunkHeaders:(BOOL)includeHunkHeaders fileHeaders:(BOOL)includeFileHeaders;

/// Returns the raw patch data.
- (NSData*)patchData;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style note (here and elsewhere): space between the type and the asterisk.

@joshaber
Copy link
Member

I'd be interested in seeing what others think about this. My fear is that the current interface still makes it too easy to mess up the ownership of the tree entry.

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: a/an.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@jspahrsummers
Copy link
Contributor

I agree with @joshaber that we should stick to one ownership semantic for this class. It's simpler, even if slightly suboptimal for some use cases.

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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If NULL is passed you'll crash.

@tiennou
Copy link
Contributor

tiennou commented Jan 20, 2015

Agreed about the ownership thing. Though the fact is that some functions return externally-owned tree entries while others don't.

I propose -[GTTreeEntry initWithEntry:parentTree:freeWhenDone:error:] to be added and make the default method to assume shouldFree (changing semantics for existing users though). And externally-owned users would pass NO, doing the right thing when the entry goes away ?

@joshaber
Copy link
Member

Though the fact is that some functions return externally-owned tree entries while others don't.

But if we call git_tree_entry_dup on those entries, we'll always end up with an objective-git-owned entry, right?

@0x4a616e
Copy link
Contributor Author

What do you think about switching the interface back to const git_tree_entry* and always creating a copy of the entry internally within GTTreeEntry. This way, the object would always be solely owned and freed by GTTreeEntry.

@joshaber
Copy link
Member

Sounds good 👍

@tiennou
Copy link
Contributor

tiennou commented Jan 20, 2015

My goal with the "private" method was that if there's someone out there who uses libgit2 directly and has to wrap one of the externally-owned entries itself, they it will have to duplicate what we're doing internally.

But yeah, always duping is good, as it actually will prevent some future weird crashes where the underlying owner gets freed (thus freeing entries) while there's still some GTTreeEntry in e.g. an array.

…g initialisation. The resulting git_tree_entry is thus solely owned (and freed) by GTTreeEntry
@0x4a616e
Copy link
Contributor Author

In the latest version, the git_tree_entry is always duplicated. What do you think? :)

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];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: NSData *buffer

self = [super init];
if (self == nil) return nil;

git_tree_entry* copyOfEntry = nil;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: git_tree_entry *copyOfEntry

@joshaber joshaber self-assigned this Feb 17, 2015
@joshaber
Copy link
Member

Awesome, thanks! I didn't realize the changes were made.

Just a couple more style notes.

@0x4a616e
Copy link
Contributor Author

Style changed

@joshaber
Copy link
Member

🤘

joshaber added a commit that referenced this pull request Feb 17, 2015
Added two new objective c wrappers for libgit2
@joshaber joshaber merged commit 2316a61 into libgit2:master Feb 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants