Skip to content

Commit 18ef285

Browse files
authored
Merge pull request #590 from tiennou/shallow-repo
Split the OID lookup from the object lookup in GTEnumerator
2 parents bd26fca + f63763d commit 18ef285

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

ObjectiveGit/Categories/NSError+Git.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@
2929

3030
#import <Foundation/Foundation.h>
3131

32+
/// The error domain used by Objective-Git
3233
extern NSString * const GTGitErrorDomain;
3334

35+
/// Error userinfo keys
36+
extern NSString * const GTGitErrorOID;
37+
3438
@interface NSError (Git)
3539

3640
/// Describes the given libgit2 error code, using any message provided by

ObjectiveGit/Categories/NSError+Git.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#import "git2/errors.h"
3232

3333
NSString * const GTGitErrorDomain = @"GTGitErrorDomain";
34+
NSString * const GTGitErrorOID = @"GTOID";
3435

3536
@implementation NSError (Git)
3637

ObjectiveGit/GTEnumerator.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ NS_ASSUME_NONNULL_BEGIN
117117
/// Returns a (possibly empty) array of GTCommits, or nil if an error occurs.
118118
- (nullable NSArray<GTCommit *> *)allObjectsWithError:(NSError **)error;
119119

120+
/// Get the next OID.
121+
///
122+
/// success - If not NULL, this will be set to whether getting the next object
123+
/// was successful. This will be YES if the receiver is exhausted, so
124+
/// it can be used to interpret the meaning of a nil return value.
125+
/// error - If not NULL, set to any error that occurs during traversal.
126+
///
127+
/// Returns nil if an error occurs or the enumeration is done.
128+
- (nullable GTOID *)nextOIDWithSuccess:(nullable BOOL *)success error:(NSError **)error;
129+
120130
/// Gets the next commit.
121131
///
122132
/// success - If not NULL, this will be set to whether getting the next object

ObjectiveGit/GTEnumerator.m

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,34 @@ - (void)resetWithOptions:(GTEnumeratorOptions)options {
145145

146146
#pragma mark Enumerating
147147

148-
- (GTCommit *)nextObjectWithSuccess:(BOOL *)success error:(NSError **)error {
148+
- (GTOID *)nextOIDWithSuccess:(BOOL *)success error:(NSError **)error {
149149
git_oid oid;
150+
150151
int gitError = git_revwalk_next(&oid, self.walk);
151152
if (gitError == GIT_ITEROVER) {
152153
if (success != NULL) *success = YES;
153154
return nil;
154155
}
156+
if (gitError != GIT_OK) {
157+
if (success != NULL) *success = NO;
158+
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Enumeration failed"];
159+
return nil;
160+
}
161+
162+
if (success != NULL) *success = YES;
163+
return [GTOID oidWithGitOid:&oid];
164+
}
165+
166+
- (GTCommit *)nextObjectWithSuccess:(BOOL *)success error:(NSError **)error {
167+
GTOID *oid = [self nextOIDWithSuccess:success error:error];
168+
if (oid == nil) {
169+
// We don't care whether the iteration completed, or an error occurred,
170+
// there's nothing to lookup.
171+
return nil;
172+
}
155173

156174
// Ignore error if we can't lookup object and just return nil.
157-
GTCommit *commit = [self.repository lookUpObjectByGitOid:&oid objectType:GTObjectTypeCommit error:error];
175+
GTCommit *commit = [self.repository lookUpObjectByOID:oid objectType:GTObjectTypeCommit error:error];
158176
if (success != NULL) *success = (commit != nil);
159177
return commit;
160178
}

ObjectiveGit/GTRepository.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ - (id)lookUpObjectByGitOid:(const git_oid *)oid objectType:(GTObjectType)type er
290290
if (error != NULL) {
291291
char oid_str[GIT_OID_HEXSZ+1];
292292
git_oid_tostr(oid_str, sizeof(oid_str), oid);
293-
*error = [NSError git_errorFor:gitError description:@"Failed to lookup object %s in repository.", oid_str];
293+
*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];
294294
}
295295
return nil;
296296
}

0 commit comments

Comments
 (0)