Skip to content

Commit cb82b87

Browse files
committed
Merge pull request #420 from libgit2/better-branches
Better -branches:.
2 parents d9cdb5d + e051eff commit cb82b87

File tree

7 files changed

+78
-19
lines changed

7 files changed

+78
-19
lines changed

ObjectiveGit/GTRepository.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,18 @@ extern NSString * const GTRepositoryInitOptionsOriginURLString;
242242

243243
- (GTReference *)headReferenceWithError:(NSError **)error;
244244

245-
/// Convenience methods to return branches in the repository
246-
- (NSArray *)allBranchesWithError:(NSError **)error;
247-
248245
- (NSArray *)localBranchesWithError:(NSError **)error;
249246
- (NSArray *)remoteBranchesWithError:(NSError **)error;
250247
- (NSArray *)branchesWithPrefix:(NSString *)prefix error:(NSError **)error;
251248

249+
/// Get the local and remote branches and merge them together by combining local
250+
/// branches with their remote branch, if they have one.
251+
///
252+
/// error - The error if one occurs.
253+
///
254+
/// Returns the branches or nil if an error occurs.
255+
- (NSArray *)branches:(NSError **)error;
256+
252257
/// List all remotes in the repository
253258
///
254259
/// error - will be filled if an error occurs

ObjectiveGit/GTRepository.m

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -401,28 +401,23 @@ - (NSArray *)branchesWithPrefix:(NSString *)prefix error:(NSError **)error {
401401
return branches;
402402
}
403403

404-
- (NSArray *)allBranchesWithError:(NSError **)error {
405-
NSMutableArray *allBranches = [NSMutableArray array];
404+
- (NSArray *)branches:(NSError **)error {
406405
NSArray *localBranches = [self localBranchesWithError:error];
407-
NSArray *remoteBranches = [self remoteBranchesWithError:error];
408-
if (localBranches == nil || remoteBranches == nil) return nil;
406+
if (localBranches == nil) return nil;
409407

410-
[allBranches addObjectsFromArray:localBranches];
408+
NSMutableArray *remoteBranches = [[self remoteBranchesWithError:error] mutableCopy];
409+
if (remoteBranches == nil) return nil;
411410

412-
// we want to add the remote branches that we don't already have as a local branch
413-
NSMutableDictionary *shortNamesToBranches = [NSMutableDictionary dictionary];
411+
NSMutableArray *branches = [NSMutableArray array];
414412
for (GTBranch *branch in localBranches) {
415-
[shortNamesToBranches setObject:branch forKey:branch.shortName];
413+
GTBranch *trackingBranch = [branch trackingBranchWithError:NULL success:NULL];
414+
if (trackingBranch != nil) [remoteBranches removeObject:trackingBranch];
415+
[branches addObject:branch];
416416
}
417417

418-
for (GTBranch *branch in remoteBranches) {
419-
GTBranch *localBranch = [shortNamesToBranches objectForKey:branch.shortName];
420-
if (localBranch == nil) {
421-
[allBranches addObject:branch];
422-
}
423-
}
418+
[branches addObjectsFromArray:remoteBranches];
424419

425-
return allBranches;
420+
return branches;
426421
}
427422

428423
- (NSArray *)remoteNamesWithError:(NSError **)error {

ObjectiveGitTests/GTIndexSpec.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
it(@"should write to a specific repository and return a tree", ^{
5454
GTRepository *repository = self.bareFixtureRepository;
55-
NSArray *branches = [repository allBranchesWithError:NULL];
55+
NSArray *branches = [repository branches:NULL];
5656
GTCommit *masterCommit = [branches[0] targetCommitAndReturnError:NULL];
5757
GTCommit *packedCommit = [branches[1] targetCommitAndReturnError:NULL];
5858

ObjectiveGitTests/GTRepositorySpec.m

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,58 @@
490490
});
491491
});
492492

493+
describe(@"-branches:", ^{
494+
__block NSArray *branches;
495+
496+
beforeEach(^{
497+
GTRepository *repository = [self testAppForkFixtureRepository];
498+
branches = [repository branches:NULL];
499+
expect(branches).notTo(beNil());
500+
});
501+
502+
it(@"should combine a local branch with its remote branch", ^{
503+
NSMutableArray *localBranches = [NSMutableArray array];
504+
NSMutableArray *remoteBranches = [NSMutableArray array];
505+
for (GTBranch *branch in branches) {
506+
if ([branch.shortName isEqual:@"BranchA"]) {
507+
if (branch.branchType == GTBranchTypeLocal) {
508+
[localBranches addObject:branch];
509+
} else {
510+
[remoteBranches addObject:branch];
511+
}
512+
}
513+
}
514+
515+
expect(@(localBranches.count)).to(equal(@1));
516+
517+
GTBranch *localBranchA = localBranches[0];
518+
GTBranch *trackingBranch = [localBranchA trackingBranchWithError:NULL success:NULL];
519+
expect(trackingBranch.remoteName).to(equal(@"origin"));
520+
521+
expect(@(remoteBranches.count)).to(equal(@1));
522+
523+
GTBranch *remoteBranchA = remoteBranches[0];
524+
expect(remoteBranchA.remoteName).to(equal(@"github"));
525+
});
526+
527+
it(@"should contain local branches", ^{
528+
NSInteger index = [branches indexOfObjectPassingTest:^(GTBranch *branch, NSUInteger idx, BOOL *stop) {
529+
return [branch.shortName isEqual:@"new-shite"];
530+
}];
531+
expect(@(index)).notTo(equal(@(NSNotFound)));
532+
});
533+
534+
it(@"should contain remote branches which exist on multiple remotes", ^{
535+
NSUInteger matches = 0;
536+
for (GTBranch *branch in branches) {
537+
if ([branch.shortName isEqual:@"blah"] && branch.branchType == GTBranchTypeRemote) {
538+
matches++;
539+
}
540+
}
541+
expect(@(matches)).to(equal(@2));
542+
});
543+
});
544+
493545
afterEach(^{
494546
[self tearDown];
495547
});

ObjectiveGitTests/QuickSpec+GTFixtures.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
// A fully fledged repository, great for testing nearly everything.
2323
- (GTRepository *)testAppFixtureRepository;
2424

25+
/// A fork of Test_App.
26+
- (GTRepository *)testAppForkFixtureRepository;
27+
2528
// A bare repository with a minimal history.
2629
- (GTRepository *)bareFixtureRepository;
2730

ObjectiveGitTests/QuickSpec+GTFixtures.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ - (GTRepository *)testAppFixtureRepository {
123123
return [self fixtureRepositoryNamed:@"Test_App"];
124124
}
125125

126+
- (GTRepository *)testAppForkFixtureRepository {
127+
return [self fixtureRepositoryNamed:@"Test_App_fork"];
128+
}
129+
126130
- (GTRepository *)testUnicodeFixtureRepository {
127131
return [self fixtureRepositoryNamed:@"unicode-files-repo"];
128132
}
21.3 MB
Binary file not shown.

0 commit comments

Comments
 (0)