From 05067f75c9c17f701af02399b172f58afa60c0d4 Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Fri, 15 Jul 2016 14:38:28 +0200 Subject: [PATCH 1/3] Split the OID lookup from the object lookup in GTEnumerator --- ObjectiveGit/GTEnumerator.h | 10 ++++++++++ ObjectiveGit/GTEnumerator.m | 22 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/ObjectiveGit/GTEnumerator.h b/ObjectiveGit/GTEnumerator.h index d3d9d60c2..900d3e60a 100644 --- a/ObjectiveGit/GTEnumerator.h +++ b/ObjectiveGit/GTEnumerator.h @@ -117,6 +117,16 @@ NS_ASSUME_NONNULL_BEGIN /// Returns a (possibly empty) array of GTCommits, or nil if an error occurs. - (nullable NSArray *)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 diff --git a/ObjectiveGit/GTEnumerator.m b/ObjectiveGit/GTEnumerator.m index 673d7f8c6..4b45a59f8 100644 --- a/ObjectiveGit/GTEnumerator.m +++ b/ObjectiveGit/GTEnumerator.m @@ -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; } From 51f6656ffbd2049a1f088fb2af6809d74498945f Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Fri, 23 Sep 2016 00:55:32 +0200 Subject: [PATCH 2/3] Provide the OID whose lookup failed in the error --- ObjectiveGit/Categories/NSError+Git.h | 4 ++++ ObjectiveGit/Categories/NSError+Git.m | 1 + ObjectiveGit/GTRepository.m | 5 ++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ObjectiveGit/Categories/NSError+Git.h b/ObjectiveGit/Categories/NSError+Git.h index bfe453dc0..4f7532f62 100644 --- a/ObjectiveGit/Categories/NSError+Git.h +++ b/ObjectiveGit/Categories/NSError+Git.h @@ -29,8 +29,12 @@ #import +/// 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 diff --git a/ObjectiveGit/Categories/NSError+Git.m b/ObjectiveGit/Categories/NSError+Git.m index cbc31509b..e66d2fcb3 100644 --- a/ObjectiveGit/Categories/NSError+Git.m +++ b/ObjectiveGit/Categories/NSError+Git.m @@ -31,6 +31,7 @@ #import "git2/errors.h" NSString * const GTGitErrorDomain = @"GTGitErrorDomain"; +NSString * const GTGitErrorOID = @"GTOID"; @implementation NSError (Git) diff --git a/ObjectiveGit/GTRepository.m b/ObjectiveGit/GTRepository.m index c2483ab57..96784c309 100644 --- a/ObjectiveGit/GTRepository.m +++ b/ObjectiveGit/GTRepository.m @@ -290,7 +290,10 @@ - (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; } From f63763db194440d4bc4f352bfd3254af70268262 Mon Sep 17 00:00:00 2001 From: Etienne Samson Date: Thu, 27 Oct 2016 10:15:24 +0200 Subject: [PATCH 3/3] Singleline-ify #FollowTheStyleGuide --- ObjectiveGit/GTRepository.m | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ObjectiveGit/GTRepository.m b/ObjectiveGit/GTRepository.m index 96784c309..fc1e22d98 100644 --- a/ObjectiveGit/GTRepository.m +++ b/ObjectiveGit/GTRepository.m @@ -290,10 +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" - userInfo:@{GTGitErrorOID: [GTOID oidWithGitOid:oid]} - failureReason:@"The object %s couldn't be found in the 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; }