Skip to content

Commit

Permalink
Merge pull request #428 from libgit2/update-tracking-branch
Browse files Browse the repository at this point in the history
Update tracking branch
  • Loading branch information
mdiep committed Dec 12, 2014
2 parents 47572f5 + 6085301 commit 5b7e15c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
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

0 comments on commit 5b7e15c

Please sign in to comment.