@@ -395,22 +395,55 @@ - (GTReference *)headReferenceWithError:(NSError **)error {
395
395
return [[GTReference alloc ] initWithGitReference: headRef repository: self ];
396
396
}
397
397
398
+ typedef void (^GTRepositoryBranchEnumerationBlock)(GTBranch *branch, BOOL *stop);
399
+
400
+ - (BOOL )enumerateBranchesWithType : (GTBranchType)type error : (NSError **)error usingBlock : (GTRepositoryBranchEnumerationBlock)block {
401
+ git_branch_iterator *iter = NULL ;
402
+ git_reference *gitRef = NULL ;
403
+ int gitError = git_branch_iterator_new (&iter, self.git_repository , (git_branch_t )type);
404
+ if (gitError != GIT_OK) {
405
+ if (error) *error = [NSError git_errorFor: gitError description: @" Branch enumeration failed" ];
406
+ return NO ;
407
+ }
408
+
409
+ git_branch_t branchType;
410
+ while ((gitError = git_branch_next (&gitRef, &branchType, iter)) == GIT_OK) {
411
+ GTReference *ref = [[GTReference alloc ] initWithGitReference: gitRef repository: self ];
412
+ GTBranch *branch = [GTBranch branchWithReference: ref repository: self ];
413
+ BOOL stop = NO ;
414
+ block (branch, &stop);
415
+ if (stop) break ;
416
+ }
417
+
418
+ if (gitError != GIT_OK && gitError != GIT_ITEROVER) {
419
+ if (error) *error = [NSError git_errorFor: gitError description: @" Branch enumeration failed" ];
420
+ return NO ;
421
+ }
422
+
423
+ return YES ;
424
+ }
425
+
398
426
- (NSArray *)localBranchesWithError : (NSError **)error {
399
- return [self branchesWithPrefix: [GTBranch localNamePrefix ] error: error];
427
+ NSMutableArray *localBranches = [NSMutableArray array ];
428
+ BOOL success = [self enumerateBranchesWithType: GTBranchTypeLocal error: error usingBlock: ^(GTBranch *branch, BOOL *stop) {
429
+ [localBranches addObject: branch];
430
+ }];
431
+
432
+ if (success != YES ) return nil ;
433
+
434
+ return [localBranches copy ];
400
435
}
401
436
402
437
- (NSArray *)remoteBranchesWithError : (NSError **)error {
403
- NSArray *remoteBranches = [self branchesWithPrefix: [GTBranch remoteNamePrefix ] error: error];
404
- if (remoteBranches == nil ) return nil ;
438
+ NSMutableArray *remoteBranches = [NSMutableArray array ];
439
+ BOOL success = [self enumerateBranchesWithType: GTBranchTypeRemote error: error usingBlock: ^(GTBranch *branch, BOOL *stop) {
440
+ if (![branch.shortName isEqualToString: @" HEAD" ])
441
+ [remoteBranches addObject: branch];
442
+ }];
405
443
406
- NSMutableArray *filteredList = [NSMutableArray arrayWithCapacity: remoteBranches.count];
407
- for (GTBranch *branch in remoteBranches) {
408
- if (![branch.shortName isEqualToString: @" HEAD" ]) {
409
- [filteredList addObject: branch];
410
- }
411
- }
444
+ if (success != YES ) return nil ;
412
445
413
- return filteredList ;
446
+ return [remoteBranches copy ] ;
414
447
}
415
448
416
449
- (NSArray *)branchesWithPrefix : (NSString *)prefix error : (NSError **)error {
0 commit comments