Skip to content

Improved remote branch implements #456

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 5 commits into from
Apr 10, 2015
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
7 changes: 6 additions & 1 deletion ObjectiveGit/GTBranch.m
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ - (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.shortName.UTF8String);
int result = GIT_ENOTFOUND;
if (trackingBranch.branchType == GTBranchTypeRemote) {
result = git_branch_set_upstream(self.reference.git_reference, [trackingBranch.name stringByReplacingOccurrencesOfString:[GTBranch remoteNamePrefix] withString:@""].UTF8String);
} else {
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;
Expand Down
12 changes: 12 additions & 0 deletions ObjectiveGit/GTRepository+RemoteOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,16 @@ extern NSString *const GTRepositoryRemoteOptionsCredentialProvider;
/// will point to an error describing what happened).
- (BOOL)pushBranches:(NSArray *)branches toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(void (^)(unsigned int current, unsigned int total, size_t bytes, BOOL *stop))progressBlock;

/// Delete a remote branch
///
/// branch - The branch to push. Must not be nil.
/// remote - The remote to push to. Must not be nil.
/// options - Options applied to the push operation. Can be NULL.
/// Recognized options are:
/// `GTRepositoryRemoteOptionsCredentialProvider`
/// error - The error if one occurred. Can be NULL.
///
/// Returns YES if the push was successful, NO otherwise (and `error`, if provided,
/// will point to an error describing what happened).
- (BOOL)deleteBranch:(GTBranch *)branch fromRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error;
@end
10 changes: 10 additions & 0 deletions ObjectiveGit/GTRepository+RemoteOperations.m
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ - (BOOL)pushBranches:(NSArray *)branches toRemote:(GTRemote *)remote withOptions
return [self pushRefspecs:refspecs toRemote:remote withOptions:options error:error progress:progressBlock];
}

#pragma mark - Deletion (Public)
- (BOOL)deleteBranch:(GTBranch *)branch fromRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error {
NSParameterAssert(branch != nil);
NSParameterAssert(remote != nil);

NSArray *refspecs = @[ [NSString stringWithFormat:@":refs/heads/%@", branch.shortName] ];

return [self pushRefspecs:refspecs toRemote:remote withOptions:options error:error progress:nil];
}

#pragma mark - Push (Private)

- (BOOL)pushRefspecs:(NSArray *)refspecs toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(GTRemotePushTransferProgressBlock)progressBlock {
Expand Down
31 changes: 31 additions & 0 deletions ObjectiveGitTests/GTBranchSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,37 @@
expect(trackingBranch).to(beNil());
expect(@(success)).to(beTruthy());
});

it(@"should set a remote tracking branch without branches amount change", ^{
GTRepository *repository = self.testAppForkFixtureRepository;
expect(repository).notTo(beNil());

NSError *error = nil;
BOOL success = NO;
GTBranch *remoteBranch = [repository lookUpBranchWithName:@"github/BranchC" type:GTBranchTypeRemote success:&success error:&error];
expect(remoteBranch).notTo(beNil());
expect(error).to(beNil());

NSArray *beforeBranches = [repository branches:&error];
expect(error).to(beNil());

GTBranch *localBranch = [repository createBranchNamed:remoteBranch.shortName fromOID:remoteBranch.OID message:nil error:&error];
expect(localBranch).notTo(beNil());
expect(error).to(beNil());

NSArray *inBranches = [repository branches:&error];
expect(error).to(beNil());

[localBranch updateTrackingBranch:remoteBranch error:&error];
expect(error).to(beNil());

NSArray *afterBranches = [repository branches:&error];
expect(error).to(beNil());

expect(@(beforeBranches.count + 1)).to(equal(@(inBranches.count)));
expect(@(beforeBranches.count)).to(equal(@(afterBranches.count)));

});
});

// TODO: Test branch renaming, branch upstream
Expand Down