From fe891b1b7af7ec7fb08314abfb060bdfc7ace6e2 Mon Sep 17 00:00:00 2001 From: Yifei Zhou Date: Sun, 5 Apr 2015 15:04:35 +0800 Subject: [PATCH 1/3] add remote branch deletion feature --- ObjectiveGit/GTRepository+RemoteOperations.h | 13 +++++++++++++ ObjectiveGit/GTRepository+RemoteOperations.m | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/ObjectiveGit/GTRepository+RemoteOperations.h b/ObjectiveGit/GTRepository+RemoteOperations.h index d5a25786c..34a8961fd 100644 --- a/ObjectiveGit/GTRepository+RemoteOperations.h +++ b/ObjectiveGit/GTRepository+RemoteOperations.h @@ -75,4 +75,17 @@ 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. +/// progressBlock - An optional callback for monitoring progress. +/// +/// 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 progress:(void (^)(unsigned int current, unsigned int total, size_t bytes, BOOL *stop))progressBlock; @end diff --git a/ObjectiveGit/GTRepository+RemoteOperations.m b/ObjectiveGit/GTRepository+RemoteOperations.m index 302ffbabf..4f2bda0f8 100644 --- a/ObjectiveGit/GTRepository+RemoteOperations.m +++ b/ObjectiveGit/GTRepository+RemoteOperations.m @@ -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 progress:(GTRemotePushTransferProgressBlock)progressBlock { + 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:progressBlock]; +} + #pragma mark - Push (Private) - (BOOL)pushRefspecs:(NSArray *)refspecs toRemote:(GTRemote *)remote withOptions:(NSDictionary *)options error:(NSError **)error progress:(GTRemotePushTransferProgressBlock)progressBlock { From 81dbdfcdee9a7f7c07b0d7f54c16b3b9dbb23328 Mon Sep 17 00:00:00 2001 From: Yifei Zhou Date: Wed, 8 Apr 2015 09:34:02 +0800 Subject: [PATCH 2/3] Fixed a bug which can cause remote branch considered as local --- ObjectiveGit/GTBranch.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ObjectiveGit/GTBranch.m b/ObjectiveGit/GTBranch.m index fbcaa5380..b301b701d 100644 --- a/ObjectiveGit/GTBranch.m +++ b/ObjectiveGit/GTBranch.m @@ -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; From b0de97633a6ff4c1a1f2be4a4f05eb7cd2b3fec0 Mon Sep 17 00:00:00 2001 From: Yifei Zhou Date: Wed, 8 Apr 2015 15:10:11 +0800 Subject: [PATCH 3/3] Updated Branch Spec Test --- ObjectiveGitTests/GTBranchSpec.m | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/ObjectiveGitTests/GTBranchSpec.m b/ObjectiveGitTests/GTBranchSpec.m index 9a502ed3e..add2b483b 100644 --- a/ObjectiveGitTests/GTBranchSpec.m +++ b/ObjectiveGitTests/GTBranchSpec.m @@ -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