Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Split the OID lookup from the object lookup in GTEnumerator #590

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 3 commits into from
Oct 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ObjectiveGit/Categories/NSError+Git.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@

#import <Foundation/Foundation.h>

/// The error domain used by Objective-Git
extern NSString * const GTGitErrorDomain;

/// Error userinfo keys
extern NSString * const GTGitErrorOID;

@interface NSError (Git)

/// Describes the given libgit2 error code, using any message provided by
Expand Down
1 change: 1 addition & 0 deletions ObjectiveGit/Categories/NSError+Git.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#import "git2/errors.h"

NSString * const GTGitErrorDomain = @"GTGitErrorDomain";
NSString * const GTGitErrorOID = @"GTOID";

@implementation NSError (Git)

Expand Down
10 changes: 10 additions & 0 deletions ObjectiveGit/GTEnumerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ NS_ASSUME_NONNULL_BEGIN
/// Returns a (possibly empty) array of GTCommits, or nil if an error occurs.
- (nullable NSArray<GTCommit *> *)allObjectsWithError:(NSError **)error;

/// Get the next OID.
///
/// success - If not NULL, this will be set to whether getting the next object
/// was successful. This will be YES if the receiver is exhausted, so
/// it can be used to interpret the meaning of a nil return value.
/// error - If not NULL, set to any error that occurs during traversal.
///
/// Returns nil if an error occurs or the enumeration is done.
- (nullable GTOID *)nextOIDWithSuccess:(nullable BOOL *)success error:(NSError **)error;

/// Gets the next commit.
///
/// success - If not NULL, this will be set to whether getting the next object
Expand Down
22 changes: 20 additions & 2 deletions ObjectiveGit/GTEnumerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,34 @@ - (void)resetWithOptions:(GTEnumeratorOptions)options {

#pragma mark Enumerating

- (GTCommit *)nextObjectWithSuccess:(BOOL *)success error:(NSError **)error {
- (GTOID *)nextOIDWithSuccess:(BOOL *)success error:(NSError **)error {
git_oid oid;

int gitError = git_revwalk_next(&oid, self.walk);
if (gitError == GIT_ITEROVER) {
if (success != NULL) *success = YES;
return nil;
}
if (gitError != GIT_OK) {
if (success != NULL) *success = NO;
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Enumeration failed"];
return nil;
}

if (success != NULL) *success = YES;
return [GTOID oidWithGitOid:&oid];
}

- (GTCommit *)nextObjectWithSuccess:(BOOL *)success error:(NSError **)error {
GTOID *oid = [self nextOIDWithSuccess:success error:error];
if (oid == nil) {
// We don't care whether the iteration completed, or an error occurred,
// there's nothing to lookup.
return nil;
}

// Ignore error if we can't lookup object and just return nil.
GTCommit *commit = [self.repository lookUpObjectByGitOid:&oid objectType:GTObjectTypeCommit error:error];
GTCommit *commit = [self.repository lookUpObjectByOID:oid objectType:GTObjectTypeCommit error:error];
if (success != NULL) *success = (commit != nil);
return commit;
}
Expand Down
2 changes: 1 addition & 1 deletion ObjectiveGit/GTRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ - (id)lookUpObjectByGitOid:(const git_oid *)oid objectType:(GTObjectType)type er
if (error != NULL) {
char oid_str[GIT_OID_HEXSZ+1];
git_oid_tostr(oid_str, sizeof(oid_str), oid);
*error = [NSError git_errorFor:gitError description:@"Failed to lookup object %s in repository.", oid_str];
*error = [NSError git_errorFor:gitError description:@"Failed to lookup object" userInfo:@{GTGitErrorOID: [GTOID oidWithGitOid:oid]} failureReason:@"The object %s couldn't be found in the repository.", oid_str];
}
return nil;
}
Expand Down