-
Notifications
You must be signed in to change notification settings - Fork 281
Give GTTree some love #208
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6f83a7d
Add methods to enumerate a GTTree.
tiennou c9298d9
Log the error if the enumeration fails.
tiennou 496c36b
Add private methods to get a GTTreeEntry underlying object type
tiennou 9a2f293
Implement GTTreeEntry equality.
tiennou 2f3906b
Make `-contents` not recursive.
tiennou 14f1549
Provide the correct parent to GTTreeEntry initializer.
tiennou 4911936
Tests for `-contents`.
tiennou d522e75
Document those.
tiennou afcbc51
Return a BOOL instead of the libgit2 error code.
tiennou e40b9bc
Document the enumeration block return type.
tiennou 68627ce
Use `-stringByAppendingPathComponent:` here
tiennou 9132439
Use object subscripting.
tiennou 81a9d58
NULL pointer dereference.
tiennou ae0d9d2
Use dot-syntax.
tiennou f4d7519
Move private API in a separate file.
tiennou d4a592a
The enumeration block can't be nil.
tiennou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,15 @@ | |
#import "GTRepository.h" | ||
#import "GTIndex.h" | ||
#import "NSError+Git.h" | ||
#import "GTTreeEntry+Private.h" | ||
|
||
typedef int(^GTTreeEnumerationBlock)(NSString *root, GTTreeEntry *entry); | ||
|
||
typedef struct GTTreeEnumerationStruct { | ||
__unsafe_unretained GTTree *myself; | ||
__unsafe_unretained GTTreeEnumerationBlock block; | ||
__unsafe_unretained NSMutableDictionary *directoryStructure; | ||
} GTTreeEnumerationStruct; | ||
|
||
@implementation GTTree | ||
|
||
|
@@ -61,6 +70,55 @@ - (git_tree *)git_tree { | |
return (git_tree *)self.git_object; | ||
} | ||
|
||
#pragma mark Contents | ||
|
||
static int treewalk_cb(const char *root, const git_tree_entry *git_entry, void *payload) { | ||
GTTreeEnumerationStruct *enumStruct = (GTTreeEnumerationStruct *)payload; | ||
NSString *rootString = @(root); | ||
GTTreeEntry *parentEntry = enumStruct->directoryStructure[rootString]; | ||
GTTree *parentTree = parentEntry ? parentEntry.tree : enumStruct->myself; | ||
|
||
GTTreeEntry *entry = [[GTTreeEntry alloc] initWithEntry:git_entry parentTree:parentTree]; | ||
if ([entry type] == GTObjectTypeTree) { | ||
NSString *path = [rootString stringByAppendingPathComponent:entry.name]; | ||
enumStruct->directoryStructure[path] = entry; | ||
} | ||
return enumStruct->block(rootString, entry); | ||
} | ||
|
||
|
||
- (BOOL)enumerateContentsWithOptions:(GTTreeEnumerationOptions)option error:(NSError **)error block:(GTTreeEnumerationBlock)block { | ||
NSParameterAssert(block != nil); | ||
|
||
NSMutableDictionary *structure = [[NSMutableDictionary alloc] initWithCapacity:[self entryCount]]; | ||
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: dot syntax for |
||
|
||
GTTreeEnumerationStruct enumStruct = { | ||
.myself = self, | ||
.block = block, | ||
.directoryStructure = structure, | ||
}; | ||
|
||
int gitError = git_tree_walk(self.git_tree, (git_treewalk_mode)option, treewalk_cb, &enumStruct); | ||
if (gitError != GIT_OK) { | ||
if (error != NULL) *error = [NSError git_errorFor:gitError withAdditionalDescription:@"Failed to enumerate tree"]; | ||
} | ||
return gitError != GIT_OK; | ||
} | ||
|
||
- (NSArray *)contents { | ||
NSError *error = nil; | ||
__block NSMutableArray *_contents = [NSMutableArray array]; | ||
int gitError = [self enumerateContentsWithOptions:GTTreeEnumerationOptionPre error:&error block:^int(NSString *root, GTTreeEntry *entry) { | ||
[_contents addObject:entry]; | ||
return [entry type] == GTObjectTypeTree ? 1 : 0; | ||
}]; | ||
if (gitError < GIT_OK) { | ||
NSLog(@"%@", error); | ||
return nil; | ||
} | ||
return _contents; | ||
} | ||
|
||
#pragma mark Merging | ||
|
||
- (GTIndex *)merge:(GTTree *)otherTree ancestor:(GTTree *)ancestorTree error:(NSError **)error { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// GTTreeEntry+Private.h | ||
// ObjectiveGitFramework | ||
// | ||
// Created by Etienne on 10/07/13. | ||
// Copyright (c) 2013 GitHub, Inc. All rights reserved. | ||
// | ||
|
||
#import <ObjectiveGit/ObjectiveGit.h> | ||
|
||
@interface GTTreeEntry () | ||
- (GTObjectType)type; | ||
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can this be nil?
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.
Not really. I clarified the docs.