Skip to content

Commit 5b7e15c

Browse files
committed
Merge pull request #428 from libgit2/update-tracking-branch
Update tracking branch
2 parents 47572f5 + 6085301 commit 5b7e15c

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

ObjectiveGit/GTBranch.h

+9
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ typedef NS_ENUM(NSInteger, GTBranchType) {
7979
/// found, returns nil and sets `success` to YES.
8080
- (GTBranch *)trackingBranchWithError:(NSError **)error success:(BOOL *)success;
8181

82+
/// Update the tracking branch.
83+
///
84+
/// trackingBranch - The tracking branch for the receiver. If nil, it unsets the
85+
/// tracking branch.
86+
/// error - The error if one occurred.
87+
///
88+
/// Returns whether it was successful.
89+
- (BOOL)updateTrackingBranch:(GTBranch *)trackingBranch error:(NSError **)error;
90+
8291
/// Reloads the branch's reference and creates a new branch based off that newly
8392
/// loaded reference.
8493
///

ObjectiveGit/GTBranch.m

+10
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ - (GTBranch *)trackingBranchWithError:(NSError **)error success:(BOOL *)success
205205
return [[self class] branchWithReference:[[GTReference alloc] initWithGitReference:trackingRef repository:self.repository] repository:self.repository];
206206
}
207207

208+
- (BOOL)updateTrackingBranch:(GTBranch *)trackingBranch error:(NSError **)error {
209+
int result = git_branch_set_upstream(self.reference.git_reference, trackingBranch.shortName.UTF8String);
210+
if (result != GIT_OK) {
211+
if (error != NULL) *error = [NSError git_errorFor:result description:@"Failed to update tracking branch for %@", self];
212+
return NO;
213+
}
214+
215+
return YES;
216+
}
217+
208218
- (GTBranch *)reloadedBranchWithError:(NSError **)error {
209219
GTReference *reloadedRef = [self.reference reloadedReferenceWithError:error];
210220
if (reloadedRef == nil) return nil;

ObjectiveGitTests/GTBranchSpec.m

+41
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,47 @@
189189
});
190190
});
191191

192+
describe(@"-updateTrackingBranch:error:", ^{
193+
__block GTBranch *masterBranch;
194+
beforeEach(^{
195+
masterBranch = [repository lookUpBranchWithName:@"master" type:GTBranchTypeLocal success:NULL error:NULL];
196+
expect(masterBranch).notTo(beNil());
197+
});
198+
199+
it(@"should set a tracking branch", ^{
200+
GTBranch *branch = [repository lookUpBranchWithName:@"feature" type:GTBranchTypeLocal success:NULL error:NULL];
201+
expect(branch).notTo(beNil());
202+
203+
BOOL success = NO;
204+
GTBranch *trackingBranch = [branch trackingBranchWithError:NULL success:&success];
205+
expect(trackingBranch).to(beNil());
206+
expect(@(success)).to(beTruthy());
207+
208+
NSError *error;
209+
success = [branch updateTrackingBranch:masterBranch error:&error];
210+
expect(@(success)).to(beTruthy());
211+
expect(error).to(beNil());
212+
213+
trackingBranch = [branch trackingBranchWithError:NULL success:&success];
214+
expect(trackingBranch).notTo(beNil());
215+
expect(@(success)).to(beTruthy());
216+
});
217+
218+
it(@"should unset a tracking branch", ^{
219+
BOOL success = NO;
220+
GTBranch *trackingBranch = [masterBranch trackingBranchWithError:NULL success:&success];
221+
expect(trackingBranch).notTo(beNil());
222+
expect(@(success)).to(beTruthy());
223+
224+
success = [masterBranch updateTrackingBranch:nil error:NULL];
225+
expect(@(success)).to(beTruthy());
226+
227+
trackingBranch = [masterBranch trackingBranchWithError:NULL success:&success];
228+
expect(trackingBranch).to(beNil());
229+
expect(@(success)).to(beTruthy());
230+
});
231+
});
232+
192233
// TODO: Test branch renaming, branch upstream
193234
//- (void)testCanRenameBranch {
194235
//

0 commit comments

Comments
 (0)