Skip to content

Commit 08cda38

Browse files
committed
Add -[GTBranch rename:]
1 parent 0f15dfe commit 08cda38

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

ObjectiveGit/GTBranch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ NS_ASSUME_NONNULL_BEGIN
7878
/// returns a GTCommit object or nil if an error occurred
7979
- (GTCommit * _Nullable)targetCommitWithError:(NSError **)error;
8080

81+
/// Renames the branch. Setting `force` to YES to delete another branch with the same name.
82+
- (BOOL)rename:(NSString *)name force:(BOOL)force error:(NSError **)error;
83+
8184
/// Count all commits in this branch
8285
///
8386
/// error(out) - will be filled if an error occurs

ObjectiveGit/GTBranch.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ - (BOOL)deleteWithError:(NSError **)error {
171171
return YES;
172172
}
173173

174+
- (BOOL)rename:(NSString *)name force:(BOOL)force error:(NSError **)error {
175+
git_reference *git_ref;
176+
int gitError = git_branch_move(&git_ref, self.reference.git_reference, name.UTF8String, (force ? 1 : 0));
177+
if (gitError != GIT_OK) {
178+
if (error) *error = [NSError git_errorFor:gitError description:@"Rename branch failed"];
179+
return NO;
180+
}
181+
182+
_reference = [[GTReference alloc] initWithGitReference:git_ref repository:self.repository];
183+
184+
return YES;
185+
}
186+
174187
- (GTBranch *)trackingBranchWithError:(NSError **)error success:(BOOL *)success {
175188
if (self.branchType == GTBranchTypeRemote) {
176189
if (success != NULL) *success = YES;

ObjectiveGitTests/GTBranchSpec.m

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,41 @@
272272
});
273273
});
274274

275+
describe(@"-rename:force:error", ^{
276+
__block GTBranch *masterBranch;
277+
beforeEach(^{
278+
masterBranch = [repository lookUpBranchWithName:@"master" type:GTBranchTypeLocal success:NULL error:NULL];
279+
expect(masterBranch).notTo(beNil());
280+
});
281+
282+
it(@"should rename the branch", ^{
283+
NSError *error = nil;
284+
BOOL success = [masterBranch rename:@"plop" force:NO error:&error];
285+
expect(@(success)).to(beTruthy());
286+
expect(error).to(beNil());
287+
288+
expect(masterBranch.shortName).to(equal(@"plop"));
289+
});
290+
291+
it(@"should fail on duplicates", ^{
292+
NSError *error = nil;
293+
BOOL success = [masterBranch rename:@"feature" force:NO error:&error];
294+
expect(@(success)).to(beFalsy());
295+
expect(error).notTo(beNil());
296+
297+
expect(masterBranch.shortName).to(equal(@"master"));
298+
});
299+
300+
it(@"should rename when forced", ^{
301+
NSError *error = nil;
302+
BOOL success = [masterBranch rename:@"feature" force:YES error:&error];
303+
expect(@(success)).to(beTruthy());
304+
expect(error).to(beNil());
305+
306+
expect(masterBranch.shortName).to(equal(@"feature"));
307+
});
308+
});
309+
275310
// TODO: Test branch renaming, branch upstream
276311
//- (void)testCanRenameBranch {
277312
//

0 commit comments

Comments
 (0)