Skip to content

Commit a5642ff

Browse files
committed
Redo branch enumeration using git_branch_iterator_new
1 parent 455973e commit a5642ff

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

ObjectiveGit/GTRepository.m

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -395,22 +395,55 @@ - (GTReference *)headReferenceWithError:(NSError **)error {
395395
return [[GTReference alloc] initWithGitReference:headRef repository:self];
396396
}
397397

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+
398426
- (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];
400435
}
401436

402437
- (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+
}];
405443

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;
412445

413-
return filteredList;
446+
return [remoteBranches copy];
414447
}
415448

416449
- (NSArray *)branchesWithPrefix:(NSString *)prefix error:(NSError **)error {

0 commit comments

Comments
 (0)