diff --git a/ObjectiveGit/GTBranch.h b/ObjectiveGit/GTBranch.h index d2d7a56c8..43734ffb4 100644 --- a/ObjectiveGit/GTBranch.h +++ b/ObjectiveGit/GTBranch.h @@ -79,6 +79,15 @@ typedef NS_ENUM(NSInteger, GTBranchType) { /// found, returns nil and sets `success` to YES. - (GTBranch *)trackingBranchWithError:(NSError **)error success:(BOOL *)success; +/// Update the tracking branch. +/// +/// trackingBranch - The tracking branch for the receiver. If nil, it unsets the +/// tracking branch. +/// error - The error if one occurred. +/// +/// Returns whether it was successful. +- (BOOL)updateTrackingBranch:(GTBranch *)trackingBranch error:(NSError **)error; + /// Reloads the branch's reference and creates a new branch based off that newly /// loaded reference. /// diff --git a/ObjectiveGit/GTBranch.m b/ObjectiveGit/GTBranch.m index 8516a424f..f5762bb01 100644 --- a/ObjectiveGit/GTBranch.m +++ b/ObjectiveGit/GTBranch.m @@ -205,6 +205,16 @@ - (GTBranch *)trackingBranchWithError:(NSError **)error success:(BOOL *)success return [[self class] branchWithReference:[[GTReference alloc] initWithGitReference:trackingRef repository:self.repository] repository:self.repository]; } +- (BOOL)updateTrackingBranch:(GTBranch *)trackingBranch error:(NSError **)error { + int result = git_branch_set_upstream(self.reference.git_reference, trackingBranch.shortName.UTF8String); + if (result != GIT_OK) { + if (error != NULL) *error = [NSError git_errorFor:result description:@"Failed to update tracking branch for %@", self]; + return NO; + } + + return YES; +} + - (GTBranch *)reloadedBranchWithError:(NSError **)error { GTReference *reloadedRef = [self.reference reloadedReferenceWithError:error]; if (reloadedRef == nil) return nil; diff --git a/ObjectiveGitTests/GTBranchSpec.m b/ObjectiveGitTests/GTBranchSpec.m index 631207b05..f3537ea1e 100644 --- a/ObjectiveGitTests/GTBranchSpec.m +++ b/ObjectiveGitTests/GTBranchSpec.m @@ -189,6 +189,47 @@ }); }); +describe(@"-updateTrackingBranch:error:", ^{ + __block GTBranch *masterBranch; + beforeEach(^{ + masterBranch = [repository lookUpBranchWithName:@"master" type:GTBranchTypeLocal success:NULL error:NULL]; + expect(masterBranch).notTo(beNil()); + }); + + it(@"should set a tracking branch", ^{ + GTBranch *branch = [repository lookUpBranchWithName:@"feature" type:GTBranchTypeLocal success:NULL error:NULL]; + expect(branch).notTo(beNil()); + + BOOL success = NO; + GTBranch *trackingBranch = [branch trackingBranchWithError:NULL success:&success]; + expect(trackingBranch).to(beNil()); + expect(@(success)).to(beTruthy()); + + NSError *error; + success = [branch updateTrackingBranch:masterBranch error:&error]; + expect(@(success)).to(beTruthy()); + expect(error).to(beNil()); + + trackingBranch = [branch trackingBranchWithError:NULL success:&success]; + expect(trackingBranch).notTo(beNil()); + expect(@(success)).to(beTruthy()); + }); + + it(@"should unset a tracking branch", ^{ + BOOL success = NO; + GTBranch *trackingBranch = [masterBranch trackingBranchWithError:NULL success:&success]; + expect(trackingBranch).notTo(beNil()); + expect(@(success)).to(beTruthy()); + + success = [masterBranch updateTrackingBranch:nil error:NULL]; + expect(@(success)).to(beTruthy()); + + trackingBranch = [masterBranch trackingBranchWithError:NULL success:&success]; + expect(trackingBranch).to(beNil()); + expect(@(success)).to(beTruthy()); + }); +}); + // TODO: Test branch renaming, branch upstream //- (void)testCanRenameBranch { //