Skip to content

Commit

Permalink
added a method to create a new branch in the repo
Browse files Browse the repository at this point in the history
  • Loading branch information
joshaber committed Mar 23, 2011
1 parent a3dafee commit 9508cb0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Classes/GTBranch.h
Expand Up @@ -35,6 +35,8 @@
@property (nonatomic, readonly, assign) GTRepository *repository;
@property (nonatomic, readonly, assign) GTReference *reference;

+ (NSString *)namePrefix;

// Convenience initializers
- (id)initWithName:(NSString *)branchName repository:(GTRepository *)repo error:(NSError **)error;
+ (id)branchWithName:(NSString *)branchName repository:(GTRepository *)repo error:(NSError **)error;
Expand Down
6 changes: 5 additions & 1 deletion Classes/GTBranch.m
Expand Up @@ -64,6 +64,10 @@ - (NSString *)description {
@synthesize reference;
@synthesize repository;

+ (NSString *)namePrefix {
return @"refs/heads/";
}

- (id)initWithName:(NSString *)branchName repository:(GTRepository *)repo error:(NSError **)error {

if((self = [super init])) {
Expand Down Expand Up @@ -128,7 +132,7 @@ + (NSArray *)listAllBranchesInRepository:(GTRepository *)repo error:(NSError **)

NSMutableArray *branches = [NSMutableArray array];
for(NSString *ref in references) {
if([ref hasPrefix:@"refs/heads/"]) {
if([ref hasPrefix:[[self class] namePrefix]]) {
GTBranch *b = [GTBranch branchWithName:ref repository:repo error:error];
if(b != nil)
[branches addObject:b];
Expand Down
2 changes: 2 additions & 0 deletions Classes/GTRepository.h
Expand Up @@ -97,5 +97,7 @@
//
// returns number of commits in the current branch or NSNotFound if an error occurred
- (NSInteger)numberOfCommitsInCurrentBranchAndReturnError:(NSError **)error;

- (GTBranch *)createBranchFrom:(GTReference *)ref named:(NSString *)name error:(NSError **)error;

@end
5 changes: 5 additions & 0 deletions Classes/GTRepository.m
Expand Up @@ -364,4 +364,9 @@ - (NSInteger)numberOfCommitsInCurrentBranchAndReturnError:(NSError **)error {
return [self.walker countFromSha:head.target error:error];
}

- (GTBranch *)createBranchFrom:(GTReference *)ref named:(NSString *)name error:(NSError **)error {
GTReference *newRef = [GTReference referenceByCreatingRef:[NSString stringWithFormat:@"%@%@", [GTBranch namePrefix], name] fromRef:[ref target] inRepo:self error:error];
return [GTBranch branchWithReference:newRef repository:self];
}

@end

0 comments on commit 9508cb0

Please sign in to comment.