From 301a70c4995b07fd29b8b32edad72dc77797e8c1 Mon Sep 17 00:00:00 2001 From: joshaber Date: Fri, 12 Dec 2014 12:44:29 -0500 Subject: [PATCH 1/4] Stub out a test. --- ObjectiveGitTests/GTBranchSpec.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ObjectiveGitTests/GTBranchSpec.m b/ObjectiveGitTests/GTBranchSpec.m index 631207b05..b11eb79be 100644 --- a/ObjectiveGitTests/GTBranchSpec.m +++ b/ObjectiveGitTests/GTBranchSpec.m @@ -189,6 +189,10 @@ }); }); +describe(@"-updateTrackingBranch:error:", ^{ + +}); + // TODO: Test branch renaming, branch upstream //- (void)testCanRenameBranch { // From 7c64ab71171ad22b3e774fd653f146ca91e92c55 Mon Sep 17 00:00:00 2001 From: joshaber Date: Fri, 12 Dec 2014 11:56:32 -0500 Subject: [PATCH 2/4] Added -updateTrackingBranch:error:. --- ObjectiveGit/GTBranch.h | 9 +++++++++ ObjectiveGit/GTBranch.m | 10 ++++++++++ 2 files changed, 19 insertions(+) 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..0fc734483 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.name.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; From 526936318f039f85e134ea4e9ff7819d7094be41 Mon Sep 17 00:00:00 2001 From: joshaber Date: Fri, 12 Dec 2014 13:54:14 -0500 Subject: [PATCH 3/4] Use the short name. --- ObjectiveGit/GTBranch.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ObjectiveGit/GTBranch.m b/ObjectiveGit/GTBranch.m index 0fc734483..f5762bb01 100644 --- a/ObjectiveGit/GTBranch.m +++ b/ObjectiveGit/GTBranch.m @@ -206,7 +206,7 @@ - (GTBranch *)trackingBranchWithError:(NSError **)error success:(BOOL *)success } - (BOOL)updateTrackingBranch:(GTBranch *)trackingBranch error:(NSError **)error { - int result = git_branch_set_upstream(self.reference.git_reference, trackingBranch.name.UTF8String); + 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; From 6085301725d4fa6974817a736a60a23d0b2850c4 Mon Sep 17 00:00:00 2001 From: joshaber Date: Fri, 12 Dec 2014 13:54:21 -0500 Subject: [PATCH 4/4] Test it. --- ObjectiveGitTests/GTBranchSpec.m | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/ObjectiveGitTests/GTBranchSpec.m b/ObjectiveGitTests/GTBranchSpec.m index b11eb79be..f3537ea1e 100644 --- a/ObjectiveGitTests/GTBranchSpec.m +++ b/ObjectiveGitTests/GTBranchSpec.m @@ -190,7 +190,44 @@ }); 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