Skip to content

Commit 30219e2

Browse files
committed
Merge pull request #456 from gitcafe-dev/fix-remote-branch-tracking
Improved remote branch implements
2 parents 46919bc + 0c87271 commit 30219e2

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

ObjectiveGit/GTBranch.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,12 @@ - (GTBranch *)trackingBranchWithError:(NSError **)error success:(BOOL *)success
193193
}
194194

195195
- (BOOL)updateTrackingBranch:(GTBranch *)trackingBranch error:(NSError **)error {
196-
int result = git_branch_set_upstream(self.reference.git_reference, trackingBranch.shortName.UTF8String);
196+
int result = GIT_ENOTFOUND;
197+
if (trackingBranch.branchType == GTBranchTypeRemote) {
198+
result = git_branch_set_upstream(self.reference.git_reference, [trackingBranch.name stringByReplacingOccurrencesOfString:[GTBranch remoteNamePrefix] withString:@""].UTF8String);
199+
} else {
200+
result = git_branch_set_upstream(self.reference.git_reference, trackingBranch.shortName.UTF8String);
201+
}
197202
if (result != GIT_OK) {
198203
if (error != NULL) *error = [NSError git_errorFor:result description:@"Failed to update tracking branch for %@", self];
199204
return NO;

ObjectiveGit/GTRepository+RemoteOperations.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,16 @@ extern NSString *const GTRepositoryRemoteOptionsCredentialProvider;
7575
/// will point to an error describing what happened).
7676
- (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;
7777

78+
/// Delete a remote branch
79+
///
80+
/// branch - The branch to push. Must not be nil.
81+
/// remote - The remote to push to. Must not be nil.
82+
/// options - Options applied to the push operation. Can be NULL.
83+
/// Recognized options are:
84+
/// `GTRepositoryRemoteOptionsCredentialProvider`
85+
/// error - The error if one occurred. Can be NULL.
86+
///
87+
/// Returns YES if the push was successful, NO otherwise (and `error`, if provided,
88+
/// will point to an error describing what happened).
89+
- (BOOL)deleteBranch:(GTBranch *)branch fromRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error;
7890
@end

ObjectiveGit/GTRepository+RemoteOperations.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@ - (BOOL)pushBranches:(NSArray *)branches toRemote:(GTRemote *)remote withOptions
196196
return [self pushRefspecs:refspecs toRemote:remote withOptions:options error:error progress:progressBlock];
197197
}
198198

199+
#pragma mark - Deletion (Public)
200+
- (BOOL)deleteBranch:(GTBranch *)branch fromRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error {
201+
NSParameterAssert(branch != nil);
202+
NSParameterAssert(remote != nil);
203+
204+
NSArray *refspecs = @[ [NSString stringWithFormat:@":refs/heads/%@", branch.shortName] ];
205+
206+
return [self pushRefspecs:refspecs toRemote:remote withOptions:options error:error progress:nil];
207+
}
208+
199209
#pragma mark - Push (Private)
200210

201211
- (BOOL)pushRefspecs:(NSArray *)refspecs toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(GTRemotePushTransferProgressBlock)progressBlock {

ObjectiveGitTests/GTBranchSpec.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,37 @@
228228
expect(trackingBranch).to(beNil());
229229
expect(@(success)).to(beTruthy());
230230
});
231+
232+
it(@"should set a remote tracking branch without branches amount change", ^{
233+
GTRepository *repository = self.testAppForkFixtureRepository;
234+
expect(repository).notTo(beNil());
235+
236+
NSError *error = nil;
237+
BOOL success = NO;
238+
GTBranch *remoteBranch = [repository lookUpBranchWithName:@"github/BranchC" type:GTBranchTypeRemote success:&success error:&error];
239+
expect(remoteBranch).notTo(beNil());
240+
expect(error).to(beNil());
241+
242+
NSArray *beforeBranches = [repository branches:&error];
243+
expect(error).to(beNil());
244+
245+
GTBranch *localBranch = [repository createBranchNamed:remoteBranch.shortName fromOID:remoteBranch.OID message:nil error:&error];
246+
expect(localBranch).notTo(beNil());
247+
expect(error).to(beNil());
248+
249+
NSArray *inBranches = [repository branches:&error];
250+
expect(error).to(beNil());
251+
252+
[localBranch updateTrackingBranch:remoteBranch error:&error];
253+
expect(error).to(beNil());
254+
255+
NSArray *afterBranches = [repository branches:&error];
256+
expect(error).to(beNil());
257+
258+
expect(@(beforeBranches.count + 1)).to(equal(@(inBranches.count)));
259+
expect(@(beforeBranches.count)).to(equal(@(afterBranches.count)));
260+
261+
});
231262
});
232263

233264
// TODO: Test branch renaming, branch upstream

0 commit comments

Comments
 (0)