Skip to content

Update tracking branch #428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ObjectiveGit/GTBranch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
10 changes: 10 additions & 0 deletions ObjectiveGit/GTBranch.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 41 additions & 0 deletions ObjectiveGitTests/GTBranchSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
//
Expand Down