Skip to content

Commit a73a08b

Browse files
committed
Add a method to enumerate a repository's references.
1 parent e16488a commit a73a08b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Classes/GTRepository.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ extern NSString *const GTRepositoryCloneOptionsCredentialProvider;
187187
// returns nil if an error occurred and fills the error parameter
188188
- (NSArray *)referenceNamesWithError:(NSError **)error;
189189

190+
// Enumerate over all references is the repository.
191+
//
192+
// error - If not NULL, this pointer will be set to the actual error that occurred.
193+
// block - A block which will be called for each reference. You will be passed
194+
// the reference object in `ref`, `error` will be set to something if `ref`
195+
// is nil, and `stop` will stop the enumeration if it's set to YES.
196+
//
197+
// Returns YES if enumeration was successful, NO otherwise (and the `error`
198+
// parameter will be set to the actual error that occurred).
199+
- (BOOL)enumerateReferencesWithError:(NSError **)error usingBlock:(void (^)(GTReference *reference, NSError *error, BOOL *stop))block;
200+
190201
- (GTReference *)headReferenceWithError:(NSError **)error;
191202

192203
// Convenience methods to return branches in the repository

Classes/GTRepository.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,23 @@ - (NSArray *)referenceNamesWithError:(NSError **)error {
479479
return referenceNames;
480480
}
481481

482+
- (BOOL)enumerateReferencesWithError:(NSError **)error usingBlock:(void (^)(GTReference *reference, NSError *error, BOOL *stop))block {
483+
NSArray *references = [self referenceNamesWithError:error];
484+
if (!references) return NO;
485+
486+
for (NSString *refName in references) {
487+
NSError *refError;
488+
BOOL stop = NO;
489+
490+
GTReference *ref = [GTReference referenceByLookingUpReferencedNamed:refName inRepository:self error:&refError];
491+
492+
block(ref, refError, &stop);
493+
494+
if (stop == YES) break;
495+
}
496+
return YES;
497+
}
498+
482499
- (NSURL *)fileURL {
483500
const char *path = git_repository_workdir(self.git_repository);
484501
// bare repository, you may be looking for gitDirectoryURL

0 commit comments

Comments
 (0)